SeriesPlugin: 1.5.8 Fixed encoding, popups, channel list integration
[enigma2-plugins.git] / seriesplugin / src / SeriesPluginConfiguration.py
1 # -*- coding: utf-8 -*-
2 #######################################################################
3 #
4 #    Series Plugin for Enigma-2
5 #    Coded by betonme (c) 2012 <glaserfrank(at)gmail.com>
6 #    Support: http://www.i-have-a-dreambox.com/wbb2/thread.php?threadid=TBD
7 #
8 #    This program is free software; you can redistribute it and/or
9 #    modify it under the terms of the GNU General Public License
10 #    as published by the Free Software Foundation; either version 2
11 #    of the License, or (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #######################################################################
19
20 import os
21
22
23 # for localized messages
24 from . import _
25
26 # Config
27 from Components.config import *
28 from Components.ConfigList import ConfigListScreen
29 from Components.Sources.StaticText import StaticText
30
31 from Components.ActionMap import ActionMap
32 from Screens.MessageBox import MessageBox
33 from Screens.Screen import Screen
34 from Screens.Setup import SetupSummary
35
36 from Plugins.Plugin import PluginDescriptor
37
38 # Plugin internal
39 from SeriesPlugin import resetInstance, getInstance
40 from SeriesPluginIndependent import startIndependent, stopIndependent
41 from EpisodePatterns import readPatternFile
42 from Logger import splog, Logger
43 from ShowLogScreen import ShowLogScreen
44
45
46 def checkList(cfg):
47         for choices in cfg.choices.choices:
48                 if cfg.value == choices[0]:
49                         return
50         for choices in cfg.choices.choices:
51                 if cfg.default == choices[0]:
52                         cfg.value = cfg.default
53                         return
54         cfg.value = cfg.choices.choices[0][0]
55
56
57 #######################################################
58 # Configuration screen
59 class SeriesPluginConfiguration(ConfigListScreen, Screen, Logger):
60         def __init__(self, session):
61                 Screen.__init__(self, session)
62                 self.skinName = [ "SeriesServiceConfiguration", "Setup" ]
63                 
64                 from plugin import NAME, VERSION
65                 self.setup_title = NAME + " " + _("Configuration") + " " + VERSION
66                 
67                 self.onChangedEntry = [ ]
68                 
69                 # Buttons
70                 self["key_red"] = StaticText(_("Cancel"))
71                 self["key_green"] = StaticText(_("OK"))
72                 self["key_blue"] = StaticText(_("Show Log"))
73                 
74                 # Define Actions
75                 self["actions"] = ActionMap(["SetupActions", "ChannelSelectBaseActions", "ColorActions"],
76                 {
77                         "cancel":               self.keyCancel,
78                         "save":                 self.keySave,
79                         "nextBouquet":  self.pageUp,
80                         "prevBouquet":  self.pageDown,
81                         "blue":                 self.blue,
82                         "ok":                   self.keyOK,
83                         "left":                 self.keyLeft,
84                         "right":                self.keyRight,
85                 }, -2) # higher priority
86                 
87                 stopIndependent()
88                 resetInstance()
89                 self.seriesPlugin = getInstance()
90                 
91                 # Create temporary identifier config elements
92                 identifiers = self.seriesPlugin.modules
93                 identifiers_elapsed = [k for k,v in identifiers.items() if v.knowsElapsed()]
94                 identifiers_today   = [k for k,v in identifiers.items() if v.knowsToday()]
95                 identifiers_future  = [k for k,v in identifiers.items() if v.knowsFuture()]
96                 self.cfg_identifier_elapsed = NoSave( ConfigSelection(choices = identifiers_elapsed, default = config.plugins.seriesplugin.identifier_elapsed.value or identifiers_elapsed[0]) )
97                 self.cfg_identifier_today   = NoSave( ConfigSelection(choices = identifiers_today,   default = config.plugins.seriesplugin.identifier_today.value   or identifiers_today[0]) )
98                 self.cfg_identifier_future  = NoSave( ConfigSelection(choices = identifiers_future,  default = config.plugins.seriesplugin.identifier_future.value  or identifiers_future[0]) )
99                 
100                 # Load patterns
101                 patterns = readPatternFile()
102                 self.cfg_pattern_title       = NoSave( ConfigSelection(choices = patterns, default = config.plugins.seriesplugin.pattern_title.value ) )
103                 self.cfg_pattern_description = NoSave( ConfigSelection(choices = patterns, default = config.plugins.seriesplugin.pattern_description.value ) )
104                 #self.cfg_pattern_record      = NoSave( ConfigSelection(choices = patterns, default = config.plugins.seriesplugin.pattern_record.value ) )
105                 
106                 checkList( self.cfg_pattern_title )
107                 checkList( self.cfg_pattern_description )               
108                 #checkList( self.cfg_pattern_record )
109                 
110                 # Initialize Configuration
111                 self.list = []
112                 self.buildConfig()
113                 ConfigListScreen.__init__(self, self.list, session = session, on_change = self.changed)
114                 
115                 self.changed()
116                 self.onLayoutFinish.append(self.layoutFinished)
117
118         def layoutFinished(self):
119                 self.setTitle(_(self.setup_title))
120
121         def buildConfig(self):
122                 #    _config list entry
123                 #    _                                                                                     , config element
124                 
125                 self.list.append( getConfigListEntry(  _("Enable SeriesPlugin")                            , config.plugins.seriesplugin.enabled ) )
126                 
127                 if config.plugins.seriesplugin.enabled.value:
128                         self.list.append( getConfigListEntry(  _("Show in info menu")                          , config.plugins.seriesplugin.menu_info ) )
129                         self.list.append( getConfigListEntry(  _("Show in extensions menu")                    , config.plugins.seriesplugin.menu_extensions ) )
130                         self.list.append( getConfigListEntry(  _("Show in epg menu")                           , config.plugins.seriesplugin.menu_epg ) )
131                         self.list.append( getConfigListEntry(  _("Show in channel menu")                       , config.plugins.seriesplugin.menu_channel ) )
132                         self.list.append( getConfigListEntry(  _("Show Info in movie list menu")               , config.plugins.seriesplugin.menu_movie_info ) )
133                         self.list.append( getConfigListEntry(  _("Show Rename in movie list menu")             , config.plugins.seriesplugin.menu_movie_rename ) )
134                         self.list.append( getConfigListEntry(  _("Check timer list from extension menu")       , config.plugins.seriesplugin.check_timer_list ) )
135                         
136                         #if len( config.plugins.seriesplugin.identifier_elapsed.choices ) > 1:
137                         self.list.append( getConfigListEntry(  _("Select identifier for elapsed events")       , self.cfg_identifier_elapsed ) )
138                         #if len( config.plugins.seriesplugin.identifier_today.choices ) > 1:
139                         self.list.append( getConfigListEntry(  _("Select identifier for today events")         , self.cfg_identifier_today ) )
140                         #if len( config.plugins.seriesplugin.identifier_future.choices ) > 1:
141                         self.list.append( getConfigListEntry(  _("Select identifier for future events")        , self.cfg_identifier_future ) )
142                         
143                         #if len( config.plugins.seriesplugin.manager.choices ) > 1:
144 #                       self.list.append( getConfigListEntry(  _("Select manager service")                     , config.plugins.seriesplugin.manager ) )
145                         #if len( config.plugins.seriesplugin.guide.choices ) > 1:
146 #                       self.list.append( getConfigListEntry(  _("Select guide service")                       , config.plugins.seriesplugin.guide ) )
147                         
148                         self.list.append( getConfigListEntry(  _("Episode pattern file")                       , config.plugins.seriesplugin.pattern_file ) )
149                         self.list.append( getConfigListEntry(  _("Record title episode pattern")               , self.cfg_pattern_title ) )
150                         self.list.append( getConfigListEntry(  _("Record description episode pattern")         , self.cfg_pattern_description ) )
151                         
152                         self.list.append( getConfigListEntry(  _("Replace special characters from title")      , config.plugins.seriesplugin.title_replace_chars ) )
153                         
154                         self.list.append( getConfigListEntry(  _("Alternative channel names file")             , config.plugins.seriesplugin.channel_file ) )
155                         self.list.append( getConfigListEntry(  _("Ask for channel matching")                   , config.plugins.seriesplugin.channel_popups ) )
156                         
157                         self.list.append( getConfigListEntry(  _("Rename files")                               , config.plugins.seriesplugin.rename_file ) )
158                         if config.plugins.seriesplugin.rename_file.value:
159                                 self.list.append( getConfigListEntry(  _("Tidy up filename on rename")             , config.plugins.seriesplugin.rename_tidy ) )
160                                 self.list.append( getConfigListEntry(  _("Use legacy filenames")                   , config.plugins.seriesplugin.rename_legacy ) )
161                                 self.list.append( getConfigListEntry(  _("Append '_' if file exist")               , config.plugins.seriesplugin.rename_existing_files ) )
162                         self.list.append( getConfigListEntry(  _("Show warnings after Record renaming")        , config.plugins.seriesplugin.rename_popups ) )
163                         self.list.append( getConfigListEntry(  _("Show success after Record renaming")         , config.plugins.seriesplugin.rename_popups_success ) )
164                         if (-1 < config.plugins.seriesplugin.rename_popups.value) or (-1 < config.plugins.seriesplugin.rename_popups_success.value):
165                                 self.list.append( getConfigListEntry(  _("Timeout for Rename Popup")               , config.plugins.seriesplugin.rename_popups_timeout ) )
166                         
167                         self.list.append( getConfigListEntry(  _("Max time drift to match episode")            , config.plugins.seriesplugin.max_time_drift ) )
168                         self.list.append( getConfigListEntry(  _("Title search depths")                        , config.plugins.seriesplugin.search_depths ) )
169                         
170                         self.list.append( getConfigListEntry(  _("Skip search during records")                 , config.plugins.seriesplugin.skip_during_records ) )
171                         
172                         self.list.append( getConfigListEntry(  _("AutoTimer independent mode")                 , config.plugins.seriesplugin.autotimer_independent ) )
173                         if config.plugins.seriesplugin.autotimer_independent.value:
174                                 self.list.append( getConfigListEntry(  _("Check timer every x minutes")            , config.plugins.seriesplugin.independent_cycle ) )
175                                 self.list.append( getConfigListEntry(  _("Always retry to find series info")       , config.plugins.seriesplugin.independent_retry ) )
176                         
177                         self.list.append( getConfigListEntry(  _("Show warnings after Timer handling")         , config.plugins.seriesplugin.timer_popups ) )
178                         self.list.append( getConfigListEntry(  _("Show success after Timer handling")          , config.plugins.seriesplugin.timer_popups_success ) )
179                         if (-1 < config.plugins.seriesplugin.timer_popups.value) or (-1 < config.plugins.seriesplugin.timer_popups_success.value):
180                                 self.list.append( getConfigListEntry(  _("Timeout for Timer Popup")                , config.plugins.seriesplugin.timer_popups_timeout ) )
181                         
182                         self.list.append( getConfigListEntry(  _("Use local caching")                          , config.plugins.seriesplugin.caching ) )
183                         
184                         self.list.append( getConfigListEntry(  _("Allow Google Analytics")                     , config.plugins.seriesplugin.ganalytics ) )
185                         
186                         self.list.append( getConfigListEntry(  _("E2: Composition of the recording filenames") , config.recording.filename_composition ) )
187                         
188                         try:
189                                 self.list.append( getConfigListEntry(  _("AT: Poll automatically")                 , config.plugins.autotimer.autopoll ) )
190                                 self.list.append( getConfigListEntry(  _("AT: Startup delay (in min)")             , config.plugins.autotimer.delay ) )
191                                 self.list.append( getConfigListEntry(  _("AT: Poll Interval (in h)")               , config.plugins.autotimer.interval ) )
192                                 self.list.append( getConfigListEntry(  _("AT: Timeout (in min)")                   , config.plugins.autotimer.timeout ) )
193                         except:
194                                 pass
195                         
196                         self.list.append( getConfigListEntry(  _("Debug: Write Log")                           , config.plugins.seriesplugin.write_log ) )
197                         if config.plugins.seriesplugin.write_log.value:
198                                 self.list.append( getConfigListEntry(  _("Debug: Log file path")                   , config.plugins.seriesplugin.log_file ) )
199                                 self.list.append( getConfigListEntry(  _("Debug: Forum user name")                 , config.plugins.seriesplugin.log_reply_user ) )
200                                 self.list.append( getConfigListEntry(  _("Debug: User mail address")               , config.plugins.seriesplugin.log_reply_mail ) )
201                         
202                         try:
203                                 self.list.append( getConfigListEntry(  _("E2: Enable recording debug (Timer log)") , config.recording.debug ) )
204                         except:
205                                 pass
206
207         def changeConfig(self):
208                 self.list = []
209                 self.buildConfig()
210                 self["config"].setList(self.list)
211
212         def changed(self):
213                 for x in self.onChangedEntry:
214                         x()
215                 current = self["config"].getCurrent()[1]
216                 if (current == config.plugins.seriesplugin.enabled or 
217                         current == config.plugins.seriesplugin.autotimer_independent or 
218                         current == config.plugins.seriesplugin.write_log or
219                         current == config.plugins.seriesplugin.rename_file):
220                         self.changeConfig()
221
222         # Overwrite ConfigListScreen keySave function
223         def keySave(self):
224                 self.saveAll()
225                 
226                 config.plugins.seriesplugin.identifier_elapsed.value = self.cfg_identifier_elapsed.value
227                 config.plugins.seriesplugin.identifier_today.value   = self.cfg_identifier_today.value
228                 config.plugins.seriesplugin.identifier_future.value  = self.cfg_identifier_future.value
229                 config.plugins.seriesplugin.pattern_title.value       = self.cfg_pattern_title.value
230                 config.plugins.seriesplugin.pattern_description.value = self.cfg_pattern_description.value
231                 #config.plugins.seriesplugin.pattern_record.value      = self.cfg_pattern_record.value
232                 config.plugins.seriesplugin.save()
233                 
234                 from plugin import overwriteAutoTimer, recoverAutoTimer
235                 
236                 if config.plugins.seriesplugin.enabled.value:
237                         overwriteAutoTimer()
238                 else:
239                         recoverAutoTimer()
240                 
241                 # Set new configuration
242                 from plugin import WHERE_EPGMENU, WHERE_CHANNELMENU, addSeriesPlugin, removeSeriesPlugin, SHOWINFO, RENAMESERIES, CHECKTIMERS, info, sp_extension, channel, movielist_info, movielist_rename, checkTimers
243                 
244                 if config.plugins.seriesplugin.menu_info.value:
245                         addSeriesPlugin(PluginDescriptor.WHERE_EVENTINFO, SHOWINFO, info)
246                 else:
247                         removeSeriesPlugin(PluginDescriptor.WHERE_EVENTINFO, SHOWINFO)
248                 
249                 if config.plugins.seriesplugin.menu_extensions.value:
250                         addSeriesPlugin(PluginDescriptor.WHERE_EXTENSIONSMENU, SHOWINFO, sp_extension)
251                 else:
252                         removeSeriesPlugin(PluginDescriptor.WHERE_EXTENSIONSMENU, SHOWINFO)
253                 
254                 if config.plugins.seriesplugin.menu_epg.value:
255                         addSeriesPlugin(WHERE_EPGMENU, SHOWINFO)
256                 else:
257                         removeSeriesPlugin(WHERE_EPGMENU, SHOWINFO)
258                 
259                 if config.plugins.seriesplugin.menu_channel.value:
260                         addSeriesPlugin(WHERE_CHANNELMENU, SHOWINFO, channel)
261                 else:
262                         removeSeriesPlugin(WHERE_CHANNELMENU, SHOWINFO)
263                 
264                 if config.plugins.seriesplugin.menu_movie_info.value:
265                         addSeriesPlugin(PluginDescriptor.WHERE_MOVIELIST, SHOWINFO, movielist_info)
266                 else:
267                         removeSeriesPlugin(PluginDescriptor.WHERE_MOVIELIST, SHOWINFO)
268                 
269                 if config.plugins.seriesplugin.menu_movie_rename.value:
270                         addSeriesPlugin(PluginDescriptor.WHERE_MOVIELIST, RENAMESERIES, movielist_rename)
271                 else:
272                         removeSeriesPlugin(PluginDescriptor.WHERE_MOVIELIST, RENAMESERIES)
273                 
274                 if config.plugins.seriesplugin.check_timer_list.value:
275                         addSeriesPlugin(PluginDescriptor.WHERE_EXTENSIONSMENU, CHECKTIMERS, checkTimers)
276                 else:
277                         removeSeriesPlugin(PluginDescriptor.WHERE_EXTENSIONSMENU, CHECKTIMERS)
278                 
279                 # To set new module configuration
280                 resetInstance()
281                 
282                 if config.plugins.seriesplugin.autotimer_independent.value:
283                         from SeriesPluginIndependent import startIndependent
284                         startIndependent()
285                         
286                 self.close()
287
288         # Overwrite ConfigListScreen keyCancel function
289         def keyCancel(self):
290                 if self["config"].isChanged():
291                         self.session.openWithCallback(self.cancelConfirm, MessageBox, _("Really close without saving settings?"))
292                 else:
293                         self.close()
294
295         # Overwrite Screen close function
296         def close(self):
297                 from plugin import ABOUT
298                 about = ABOUT.format( **{'lookups': config.plugins.seriesplugin.lookup_counter.value} )
299                 self.session.openWithCallback(self.closeConfirm, MessageBox, about, MessageBox.TYPE_INFO)
300
301         def closeConfirm(self, dummy=None):
302                 # Call baseclass function
303                 Screen.close(self)
304         
305         def getCurrentEntry(self):
306                 return self["config"].getCurrent()[0]
307
308         def getCurrentValue(self):
309                 return str(self["config"].getCurrent()[1].getText())
310
311         def createSummary(self):
312                 return SetupSummary
313
314         def pageUp(self):
315                 self["config"].instance.moveSelection(self["config"].instance.pageUp)
316
317         def pageDown(self):
318                 self["config"].instance.moveSelection(self["config"].instance.pageDown)
319
320         def blue(self):
321                 #self.sendLog()
322                 self.session.open(ShowLogScreen, config.plugins.seriesplugin.log_file.value)
323