be a little more careful with the config lists (fixes a crash in the wizard)
[enigma2-plugins.git] / autotimer / src / AutoTimerWizard.py
1 # l10n
2 from . import _
3
4 # GUI (Screens)
5 from Screens.WizardLanguage import WizardLanguage
6 from Screens.Rc import Rc
7 from AutoTimerEditor import AutoTimerEditorBase, AutoTimerServiceEditor, \
8                 AutoTimerFilterEditor
9
10 # GUI (Components)
11 from Components.ActionMap import ActionMap
12
13 # Configuration
14 from Components.config import getConfigListEntry, KEY_0, KEY_DELETE, \
15                 KEY_BACKSPACE
16
17 # Wizard XML Path
18 from Tools import Directories
19
20 class AutoTimerWizard(WizardLanguage, AutoTimerEditorBase, Rc):
21         STEP_ID_BASIC = 2
22         STEP_ID_TIMESPAN = 5
23         STEP_ID_SERVICES = 7
24         STEP_ID_FILTER = 8
25
26         skin = """
27                 <screen position="0,0" size="720,576" title="Welcome..." flags="wfNoBorder" >
28                         <widget name="text" position="153,50" size="340,300" font="Regular;22" />
29                         <widget source="list" render="Listbox" position="53,310" size="440,220" scrollbarMode="showOnDemand" >
30                                 <convert type="StringList" />
31                         </widget>
32                         <widget name="config" position="53,310" zPosition="1" size="440,220" transparent="1" scrollbarMode="showOnDemand" />
33                         <ePixmap pixmap="skin_default/wizard.png" position="40,50" zPosition="10" size="110,174" transparent="1" alphatest="on"/>
34                         <ePixmap pixmap="skin_default/buttons/button_red.png" position="40,225" zPosition="0" size="15,16" transparent="1" alphatest="on" />
35                         <widget name="languagetext" position="55,225" size="95,30" font="Regular;18" />
36                         <widget name="rc" pixmaps="skin_default/rc.png,skin_default/rcold.png" position="500,50" zPosition="10" size="154,500" alphatest="on" />
37                         <widget name="arrowdown" pixmap="skin_default/arrowdown.png" position="0,0" zPosition="11" size="37,70" transparent="1" alphatest="on"/>
38                         <widget name="arrowdown2" pixmap="skin_default/arrowdown.png" position="0,0" zPosition="11" size="37,70" transparent="1" alphatest="on"/>
39                         <widget name="arrowup" pixmap="skin_default/arrowup.png" position="-100,-100" zPosition="11" size="37,70" transparent="1" alphatest="on"/>
40                         <widget name="arrowup2" pixmap="skin_default/arrowup.png" position="-100,-100" zPosition="11" size="37,70" transparent="1" alphatest="on"/>
41                 </screen>"""
42
43         def __init__(self, session, newTimer):
44                 self.xmlfile = Directories.resolveFilename(Directories.SCOPE_PLUGINS, "Extensions/AutoTimer/autotimerwizard.xml")
45
46                 WizardLanguage.__init__(self, session, showSteps = False, showStepSlider = False)
47                 AutoTimerEditorBase.__init__(self, newTimer)
48                 Rc.__init__(self)
49
50                 self.doCancel = False
51                 self.emptyMatch = False
52                 self.tailingWhitespacesMatch = False
53
54                 # We might need to change shown items, so add some notifiers
55                 self.timespan.addNotifier(self.regenTimespanList, initial_call = False)
56                 self.generateTimespanList()
57
58                 self.servicesDlg = self.session.instantiateDialog(
59                                 AutoTimerServiceEditor,
60                                 self.serviceRestriction, self.services, self.bouquets
61                 )
62
63                 self.filterDlg = self.session.instantiateDialog(
64                                 AutoTimerFilterEditor,
65                                 self.filterSet, self.excludes, self.includes
66                 )
67
68                 self["TextEntryActions"] = ActionMap(["TextEntryActions"],
69                         {
70                                 "deleteForward": self.deleteForward,
71                                 "deleteBackward": self.deleteBackward
72                         }, -2
73                 )
74
75         def getTranslation(self, text):
76                 return _(text)
77
78         def regenTimespanList(self, *args, **kwargs):
79                 self.generateTimespanList()
80                 if self.currStep == AutoTimerWizard.STEP_ID_TIMESPAN:
81                         self["config"].setList(self.timespanList)
82
83         def generateTimespanList(self):
84                 self.timespanList = [
85                         getConfigListEntry(_("Only match during Timespan"), self.timespan)
86                 ]
87
88                 # Only allow editing timespan when it's enabled
89                 if self.timespan.value:
90                         self.timespanList.extend([
91                                 getConfigListEntry(_("Begin of Timespan"), self.timespanbegin),
92                                 getConfigListEntry(_("End of Timespan"), self.timespanend)
93                         ])
94
95         def getConfigList(self):
96                 if self.currStep == AutoTimerWizard.STEP_ID_BASIC: # Basic
97                         return [
98                                 getConfigListEntry(_("Enabled"), self.enabled),
99                                 getConfigListEntry(_("Description"), self.name),
100                                 getConfigListEntry(_("Match Title"), self.match),
101                                 getConfigListEntry(_("Timer Type"), self.justplay),
102                         ]
103                 elif self.currStep == AutoTimerWizard.STEP_ID_TIMESPAN: # Timespan
104                         return self.timespanList
105                 elif self.currStep == AutoTimerWizard.STEP_ID_SERVICES: # Services
106                         return self.servicesDlg["config"].getList()
107                 elif self.currStep == AutoTimerWizard.STEP_ID_FILTER: # Filters
108                         return self.filterDlg["config"].getList()
109                 return []
110
111         def selectionMade(self):
112                 if self.currStep == AutoTimerWizard.STEP_ID_BASIC: # Basic
113                         self.timer.enabled = self.enabled.value
114                         self.timer.name = self.name.value.strip() or self.match.value
115                         self.timer.match = self.match.value
116                         self.timer.justplay = self.justplay.value == "zap"
117                         self.emptyMatch = not self.timer.match.strip()
118                         self.trailingWhitespacesMatch = (self.timer.match[-1:] == " ")
119                 elif self.currStep == AutoTimerWizard.STEP_ID_TIMESPAN: # Timespan
120                         if self.timespan.value:
121                                 start = self.timespanbegin.value
122                                 end = self.timespanend.value
123                                 self.timer.timespan = (start, end)
124                         else:
125                                 self.timer.timespan = None
126                 elif self.currStep == AutoTimerWizard.STEP_ID_SERVICES: # Services
127                         self.servicesDlg.refresh()
128
129                         if self.servicesDlg.enabled.value:
130                                 self.timer.services = self.servicesDlg.services[0]
131                                 self.timer.bouquets = self.servicesDlg.services[1]
132                         else:
133                                 self.timer.services = []
134                                 self.timer.bouquets = []
135                 elif self.currStep == AutoTimerWizard.STEP_ID_FILTER: # Filters
136                         self.filterDlg.refresh()
137
138                         if self.filterDlg.enabled.value:
139                                 self.timer.includes = self.filterDlg.includes
140                                 self.timer.excludes = self.filterDlg.excludes
141                         else:
142                                 self.timer.includes = []
143                                 self.timer.excludes = []
144
145         def keyNumberGlobal(self, number):
146                 if self.currStep == AutoTimerWizard.STEP_ID_BASIC or self.currStep == AutoTimerWizard.STEP_ID_TIMESPAN:
147                         self["config"].handleKey(KEY_0 + number)
148                 else:
149                         WizardLanguage.keyNumberGlobal(self, number)
150
151         def blue(self):
152                 if self.currStep == AutoTimerWizard.STEP_ID_SERVICES:
153                         self.servicesDlg.new()
154                 elif self.currStep == AutoTimerWizard.STEP_ID_FILTER:
155                         self.filterDlg.new()
156
157         def yellow(self):
158                 if self.currStep == AutoTimerWizard.STEP_ID_SERVICES:
159                         self.servicesDlg.remove()
160                 elif self.currStep == AutoTimerWizard.STEP_ID_FILTER:
161                         self.filterDlg.remove()
162
163         def maybeRemoveWhitespaces(self):
164                 # XXX: Hack alert
165                 if self["list"].current[1] == "removeTrailingWhitespaces":
166                         print "Next step would be to remove trailing whitespaces, removing them and redirecting to 'conf2'"
167                         self.timer.match = self.timer.match.rstrip()
168                         self.match.value = self.match.value.rstrip()
169                         self.currStep = self.getStepWithID("conf2")
170                 self.trailingWhitespacesMatch = False
171
172         def deleteForward(self):
173                 self["config"].handleKey(KEY_DELETE)
174
175         def deleteBackward(self):
176                 self["config"].handleKey(KEY_BACKSPACE)
177
178         def cancel(self):
179                 self.doCancel = True
180                 self.currStep = len(self.wizard)
181
182         def close(self, *args, **kwargs):
183                 if self.doCancel:
184                         WizardLanguage.close(self, None)
185                 else:
186                         WizardLanguage.close(self, self.timer)
187