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