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