intial commit of autotimer, epgrefresh and werbezapper to enigma2-plugins
[enigma2-plugins.git] / autotimer / src / AutoTimerEditor.py
1 # GUI (Screens)
2 from Screens.Screen import Screen
3 from Components.ConfigList import ConfigListScreen
4 from Screens.ChannelSelection import SimpleChannelSelection
5 from Screens.MessageBox import MessageBox
6 from Screens.ChoiceBox import ChoiceBox
7
8 # GUI (Summary)
9 from Screens.Setup import SetupSummary
10
11 # GUI (Components)
12 from Components.ActionMap import ActionMap
13 from Components.Button import Button
14
15 # Configuration
16 from Components.config import getConfigListEntry, ConfigEnableDisable, ConfigYesNo, ConfigText, ConfigClock, ConfigInteger, ConfigSelection
17
18 # Timer
19 from RecordTimer import AFTEREVENT
20
21 # Needed to convert our timestamp back and forth
22 from time import localtime, mktime
23
24 # Show ServiceName instead of ServiceReference
25 from ServiceReference import ServiceReference
26
27 weekdays = [("0", _("Monday")), ("1", _("Tuesday")),  ("2", _("Wednesday")),  ("3", _("Thursday")),  ("4", _("Friday")),  ("5", _("Saturday")),  ("6", _("Sunday")), ("weekend", _("Weekend"))]
28
29 class AutoTimerEditor(Screen, ConfigListScreen):
30         """Edit AutoTimer"""
31
32         skin = """<screen name="AutoTimerEdit" title="Edit AutoTimer" position="75,155" size="565,280">
33                 <widget name="config" position="5,5" size="555,225" scrollbarMode="showOnDemand" />
34                 <ePixmap position="0,235" zPosition="4" size="140,40" pixmap="skin_default/key-red.png" transparent="1" alphatest="on" />
35                 <ePixmap position="140,235" zPosition="4" size="140,40" pixmap="skin_default/key-green.png" transparent="1" alphatest="on" />
36                 <ePixmap position="280,235" zPosition="4" size="140,40" pixmap="skin_default/key-yellow.png" transparent="1" alphatest="on" />
37                 <ePixmap position="420,235" zPosition="4" size="140,40" pixmap="skin_default/key-blue.png" transparent="1" alphatest="on" />
38                 <widget name="key_red" position="0,235" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
39                 <widget name="key_green" position="140,235" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
40                 <widget name="key_yellow" position="280,235" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
41                 <widget name="key_blue" position="420,235" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
42         </screen>"""
43
44         def __init__(self, session, timer):
45                 Screen.__init__(self, session)
46
47                 # Keep Timer
48                 self.timer = timer
49
50                 # Summary
51                 self.setup_title = "AutoTimer Editor"
52                 self.onChangedEntry = []
53
54                 # See if we are filtering some strings
55                 self.excludes = (timer.getExcludedTitle(), timer.getExcludedShort(), timer.getExcludedDescription(), timer.getExcludedDays())
56                 self.includes = (timer.getIncludedTitle(), timer.getIncludedShort(), timer.getIncludedDescription(), timer.getIncludedDays())
57                 if len(self.excludes[0]) or len(self.excludes[1]) or len(self.excludes[2]) or len(self.excludes[3]) or len(self.includes[0]) or len(self.includes[1]) or len(self.includes[2]) or len(self.includes[3]):
58                         self.filterSet = True
59                 else:
60                         self.filterSet = False
61
62                 # See if services are restricted
63                 self.services = timer.getServices()
64                 if len(self.services):
65                         self.serviceRestriction = True
66                 else:
67                         self.serviceRestriction = False
68
69                 self.createSetup(timer)
70
71                 # We might need to change shown items, so add some notifiers
72                 self.timespan.addNotifier(self.reloadList, initial_call = False)
73                 self.offset.addNotifier(self.reloadList, initial_call = False)
74                 self.duration.addNotifier(self.reloadList, initial_call = False)
75                 self.afterevent.addNotifier(self.reloadList, initial_call = False)
76                 self.afterevent_timespan.addNotifier(self.reloadList, initial_call = False)
77                 self.counter.addNotifier(self.reloadList, initial_call = False)
78
79                 self.refresh()
80
81                 ConfigListScreen.__init__(self, self.list, session = session, on_change = self.changed)
82
83                 # Initialize Buttons
84                 self["key_red"] = Button(_("Cancel"))
85                 self["key_green"] = Button(_("OK"))
86                 self["key_yellow"] = Button()
87                 self["key_blue"] = Button()
88
89                 # Set Button texts
90                 self.renameChannelButton()
91                 self.renameFilterButton()
92
93                 # Define Actions
94                 self["actions"] = ActionMap(["SetupActions", "ColorActions"],
95                         {
96                                 "cancel": self.cancel,
97                                 "save": self.maybeSave,
98                                 "yellow": self.editFilter,
99                                 "blue": self.editChannels
100                         }
101                 )
102
103                 # Trigger change
104                 self.changed()
105
106         def renameFilterButton(self):
107                 if self.filterSet:
108                         self["key_yellow"].setText(_("Edit Filters"))
109                 else:
110                         self["key_yellow"].setText(_("Add Filters"))
111
112         def renameChannelButton(self):
113                 if self.serviceRestriction:
114                         self["key_blue"].setText(_("Edit Channels"))
115                 else:
116                         self["key_blue"].setText(_("Add Channels"))
117
118         def changed(self):
119                 for x in self.onChangedEntry:
120                         try:
121                                 x()
122                         except:
123                                 pass
124
125         def getCurrentEntry(self):
126                 return self["config"].getCurrent()[0]
127
128         def getCurrentValue(self):
129                 return str(self["config"].getCurrent()[1].getText())
130
131         def createSummary(self):
132                 return SetupSummary
133
134         def createSetup(self, timer):
135                 # Name
136                 self.name = ConfigText(default = timer.name, fixed_size = False)
137
138                 # Match
139                 self.match = ConfigText(default = timer.match, fixed_size = False)
140
141                 # Justplay
142                 self.justplay = ConfigSelection(choices = [("zap", _("zap")), ("record", _("record"))], default = {0: "record", 1: "zap"}[int(timer.justplay)])
143
144                 # Timespan
145                 now = [x for x in localtime()]
146                 if timer.hasTimespan():
147                         default = True
148                         now[3] = timer.timespan[0][0]
149                         now[4] = timer.timespan[0][1]
150                         begin = mktime(now)
151                         now[3] = timer.timespan[1][0]
152                         now[4] = timer.timespan[1][1]
153                         end = mktime(now)
154                 else:
155                         default = False
156                         now[3] = 20
157                         now[4] = 15
158                         begin = mktime(now)
159                         now[3] = 23
160                         now[4] = 15
161                         end = mktime(now)
162                 self.timespan = ConfigEnableDisable(default = default)
163                 self.timespanbegin = ConfigClock(default = begin)
164                 self.timespanend = ConfigClock(default = end)
165
166                 # Services have their own Screen
167
168                 # Offset
169                 if timer.hasOffset():
170                         default = True
171                         begin = timer.getOffsetBegin()
172                         end = timer.getOffsetEnd()
173                 else:
174                         default = False
175                         begin = 5
176                         end = 5
177                 self.offset = ConfigEnableDisable(default = default)
178                 self.offsetbegin = ConfigInteger(default = begin, limits = (0, 60))
179                 self.offsetend = ConfigInteger(default = end, limits = (0, 60))
180
181                 # AfterEvent
182                 if timer.hasAfterEvent():
183                         afterevent = { None: "default", AFTEREVENT.NONE: "nothing", AFTEREVENT.DEEPSTANDBY: "deepstandby", AFTEREVENT.STANDBY: "standby"}[timer.afterevent[0][0]]
184                 else:
185                         afterevent = "default"
186                 self.afterevent = ConfigSelection(choices = [("default", _("standard")), ("nothing", _("do nothing")), ("standby", _("go to standby")), ("deepstandby", _("go to deep standby"))], default = afterevent)
187
188                 # AfterEvent (Timespan)
189                 if timer.hasAfterEvent() and timer.afterevent[0][1][0] is not None:
190                         default = True
191                         now[3] = timer.afterevent[0][1][0][0]
192                         now[4] = timer.afterevent[0][1][0][1]
193                         begin = mktime(now)
194                         now[3] = timer.afterevent[0][1][1][0]
195                         now[4] = timer.afterevent[0][1][1][1]
196                         end = mktime(now)
197                 else:
198                         default = False
199                         now[3] = 23
200                         now[4] = 15
201                         begin = mktime(now)
202                         now[3] = 7
203                         now[4] = 0
204                         end = mktime(now)
205                 self.afterevent_timespan = ConfigEnableDisable(default = default)
206                 self.afterevent_timespanbegin = ConfigClock(default = begin)
207                 self.afterevent_timespanend = ConfigClock(default = end)
208
209                 # Enabled
210                 self.enabled = ConfigYesNo(default = timer.enabled)
211
212                 # Maxduration
213                 if timer.hasDuration():
214                         default = True
215                         duration = timer.getDuration()
216                 else:
217                         default = False
218                         duration =70
219                 self.duration = ConfigEnableDisable(default = default)
220                 self.durationlength = ConfigInteger(default = duration, limits = (0, 600))
221
222                 # Counter
223                 if timer.hasCounter():
224                         default = timer.matchCount
225                 else:
226                         default = 0
227                 self.counter = ConfigInteger(default = default, limits = (0, 50))
228                 self.counterLeft = ConfigInteger(default = timer.matchLeft, limits = (0, 50))
229                 self.counterFormatString = ConfigSelection([("", _("Never")), ("%m", _("Monthly")), ("%U", _("Weekly (Sunday)")), ("%W", _("Weekly (Monday)"))], default = timer.matchFormatString)
230
231         def refresh(self):
232                 # First four entries are always shown
233                 self.list = [
234                         getConfigListEntry(_("Enabled"), self.enabled),
235                         getConfigListEntry(_("Description"), self.name),
236                         getConfigListEntry(_("Match Title"), self.match),
237                         getConfigListEntry(_("Timer Type"), self.justplay),
238                         getConfigListEntry(_("Only match during Timespan"), self.timespan)
239                 ]
240
241                 # Only allow editing timespan when it's enabled
242                 if self.timespan.value:
243                         self.list.extend([
244                                 getConfigListEntry(_("Begin of Timespan"), self.timespanbegin),
245                                 getConfigListEntry(_("End of Timespan"), self.timespanend)
246                         ])
247
248                 self.list.append(getConfigListEntry(_("Custom offset"), self.offset))
249
250                 # Only allow editing offsets when it's enabled
251                 if self.offset.value:
252                         self.list.extend([
253                                 getConfigListEntry(_("Offset before recording (in m)"), self.offsetbegin),
254                                 getConfigListEntry(_("Offset after recording (in m)"), self.offsetend)
255                         ])
256
257                 self.list.append(getConfigListEntry(_("Set maximum Duration"), self.duration))
258
259                 # Only allow editing maxduration when it's enabled
260                 if self.duration.value:
261                         self.list.extend([
262                                 getConfigListEntry(_("Maximum Duration (in m)"), self.durationlength)
263                         ])
264
265                 self.list.append(getConfigListEntry(_("After event"), self.afterevent))
266
267                 # Only allow setting afterevent timespan when afterevent is active
268                 if self.afterevent.value != "default":
269                         self.list.append(getConfigListEntry(_("Execute after Event during Timespan"), self.afterevent_timespan))
270
271                         # Only allow editing timespan when it's enabled
272                         if self.afterevent_timespan.value:
273                                 self.list.extend([
274                                         getConfigListEntry(_("Begin of after Event Timespan"), self.afterevent_timespanbegin),
275                                         getConfigListEntry(_("End of after Event Timespan"), self.afterevent_timespanend)
276                                 ])
277
278                 self.list.append(getConfigListEntry(_("Record a maximum of x times"), self.counter))
279
280                 # Only allow setting matchLeft when counting hits
281                 if self.counter.value:
282                         self.list.append(getConfigListEntry(_("Ammount of recordings left"), self.counterLeft))
283                         self.list.append(getConfigListEntry(_("Reset Count"), self.counterFormatString))
284
285         def reloadList(self, value):
286                 self.refresh()
287                 self["config"].setList(self.list)
288
289         def editFilter(self):
290                 self.session.openWithCallback(
291                         self.editFilterCallback,
292                         AutoTimerFilterEditor,
293                         self.filterSet,
294                         self.excludes,
295                         self.includes
296                 )
297
298         def editFilterCallback(self, ret):
299                 if ret:
300                         self.filterSet = ret[0]
301                         self.excludes = ret[1]
302                         self.includes = ret[2]
303                         self.renameFilterButton()
304
305         def editChannels(self):
306                 self.session.openWithCallback(
307                         self.editChannelsCallback,
308                         AutoTimerChannelEditor,
309                         self.serviceRestriction,
310                         self.services
311                 )
312
313         def editChannelsCallback(self, ret):
314                 if ret:
315                         self.serviceRestriction = ret[0]
316                         self.services = ret[1]
317                         self.renameChannelButton()
318
319         def cancel(self):
320                 if self["config"].isChanged():
321                         self.session.openWithCallback(self.cancelConfirm, MessageBox, _("Really close without saving settings?"))
322                 else:
323                         self.close(None)
324
325         def cancelConfirm(self, ret):
326                 if ret:
327                         self.close(None)
328
329         def maybeSave(self):
330                 # Check if we have a trailing whitespace
331                 if self.match.value[-1:] == " ":
332                         self.session.openWithCallback(
333                                 self.saveCallback,
334                                 MessageBox,
335                                 _('You entered "%s" as Text to match.\nDo you want to remove trailing whitespaces?') % (self.match.value)
336                         )
337                 # Just save else
338                 else:
339                         self.save()
340
341         def saveCallback(self, ret):
342                 if ret is not None:
343                         if ret:
344                                 self.match.value = self.match.value.rstrip()
345                         self.save()
346                 # Don't to anything if MessageBox was canceled!
347
348         def save(self):
349                 # Match
350                 self.timer.match = self.match.value
351
352                 # Name
353                 self.timer.name = self.name.value or self.timer.match
354
355                 # Enabled
356                 self.timer.enabled = self.enabled.value
357
358                 # Justplay
359                 self.timer.justplay = self.justplay.value == "zap"
360
361                 # Timespan
362                 if self.timespan.value:
363                         start = self.timespanbegin.value
364                         end = self.timespanend.value
365                         self.timer.timespan = (start, end)
366                 else:
367                         self.timer.timespan = None
368
369                 # Services
370                 if self.serviceRestriction:
371                         self.timer.services = self.services
372                 else:
373                         self.timer.services = None
374
375                 # Offset
376                 if self.offset.value:
377                         self.timer.offset = (self.offsetbegin.value*60, self.offsetend.value*60)
378                 else:
379                         self.timer.offset = None
380
381                 # AfterEvent
382                 if self.afterevent.value == "default":
383                         self.timer.afterevent = []
384                 else:
385                         afterevent = {"nothing": AFTEREVENT.NONE, "deepstandby": AFTEREVENT.DEEPSTANDBY, "standby": AFTEREVENT.STANDBY}[self.afterevent.value]
386                         # AfterEvent Timespan
387                         if self.afterevent_timespan.value:
388                                 start = self.afterevent_timespanbegin.value
389                                 end = self.afterevent_timespanend.value
390                                 self.timer.afterevent = [(afterevent, (start, end))]
391                         else:
392                                 self.timer.afterevent = [(afterevent, None)]
393
394                 # Maxduration
395                 if self.duration.value:
396                         self.timer.maxduration = self.durationlength.value*60
397                 else:
398                         self.timer.maxduration = None
399
400                 # Ex-&Includes
401                 if self.filterSet:
402                         self.timer.exclude = self.excludes
403                         self.timer.include = self.includes
404                 else:
405                         self.timer.exclude = None
406                         self.timer.include = None
407
408                 # Counter
409                 if self.counter.value:
410                         self.timer.matchCount = self.counter.value
411                         if self.counterLeft.value <= self.counter.value:
412                                 self.timer.matchLeft = self.counterLeft.value
413                         else:
414                                 self.timer.matchLeft = self.counter.value
415                         if self.counterFormatString.value:
416                                 self.timer.matchFormatString = self.counterFormatString.value
417                         else:
418                                 self.timer.matchFormatString = ''
419                 else:
420                         self.timer.matchCount = 0
421                         self.timer.matchLeft = 0
422                         self.timer.matchFormatString = ''
423
424                 # Close
425                 self.close(self.timer)
426
427 class AutoTimerFilterEditor(Screen, ConfigListScreen):
428         """Edit AutoTimer Filter"""
429
430         skin = """<screen name="AutoFilterEditor" title="Edit AutoTimer Filters" position="75,150" size="565,245">
431                 <widget name="config" position="5,5" size="555,200" scrollbarMode="showOnDemand" />
432                 <ePixmap position="5,205" zPosition="4" size="140,40" pixmap="skin_default/key-red.png" transparent="1" alphatest="on" />
433                 <ePixmap position="145,205" zPosition="4" size="140,40" pixmap="skin_default/key-green.png" transparent="1" alphatest="on" />
434                 <ePixmap position="285,205" zPosition="4" size="140,40" pixmap="skin_default/key-yellow.png" transparent="1" alphatest="on" />
435                 <ePixmap position="425,205" zPosition="4" size="140,40" pixmap="skin_default/key-blue.png" transparent="1" alphatest="on" />
436                 <widget name="key_red" position="5,205" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
437                 <widget name="key_green" position="145,205" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
438                 <widget name="key_yellow" position="285,205" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
439                 <widget name="key_blue" position="425,205" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
440         </screen>"""
441
442         def __init__(self, session, filterset, excludes, includes):
443                 Screen.__init__(self, session)
444
445                 # Summary
446                 self.setup_title = "AutoTimer Filters"
447                 self.onChangedEntry = []
448
449                 self.typeSelection = ConfigSelection(choices = [("title", _("in Title")), ("short", _("in Shortdescription")), ("desc", _("in Description")), ("day", _("on Weekday"))])
450                 self.typeSelection.addNotifier(self.refresh, initial_call = False)
451
452                 self.enabled = ConfigEnableDisable(default = filterset)
453
454                 self.excludes = excludes
455                 self.includes = includes
456
457                 self.reloadList()
458
459                 ConfigListScreen.__init__(self, self.list, session = session, on_change = self.changed)
460
461                 # Initialize Buttons
462                 self["key_red"] = Button(_("Cancel"))
463                 self["key_green"] = Button(_("Save"))
464                 self["key_yellow"] = Button(_("delete"))
465                 self["key_blue"] = Button(_("New"))
466
467                 # Define Actions
468                 self["actions"] = ActionMap(["SetupActions", "ColorActions"],
469                         {
470                                 "cancel": self.cancel,
471                                 "save": self.save,
472                                 "yellow": self.remove,
473                                 "blue": self.new
474                         }
475                 )
476
477                 # Trigger change
478                 self.changed()
479
480         def changed(self):
481                 for x in self.onChangedEntry:
482                         try:
483                                 x()
484                         except:
485                                 pass
486
487         def getCurrentEntry(self):
488                 return self["config"].getCurrent()[0]
489
490         def getCurrentValue(self):
491                 return str(self["config"].getCurrent()[1].getText())
492
493         def createSummary(self):
494                 return SetupSummary
495
496         def saveCurrent(self):
497                 del self.excludes[self.idx][:]
498                 del self.includes[self.idx][:]
499
500                 # Warning, accessing a ConfigListEntry directly might be considered evil!
501
502                 idx = -1
503                 for item in self["config"].getList():
504                         idx += 1
505                         # Skip empty entries (and those which are no filters)
506                         if item[1].value == "" or idx < 2:
507                                 continue
508                         elif idx < self.lenExcludes:
509                                 self.excludes[self.idx].append(item[1].value.encode("UTF-8"))
510                         else:
511                                 self.includes[self.idx].append(item[1].value.encode("UTF-8"))
512
513         def refresh(self, value):
514                 self.saveCurrent()
515
516                 self.reloadList()
517                 self["config"].setList(self.list)
518
519         def reloadList(self):
520                 self.list = [
521                         getConfigListEntry(_("Enable Filtering"), self.enabled),
522                         getConfigListEntry(_("Filter"), self.typeSelection)
523                 ]
524
525                 if self.typeSelection.value == "title":
526                         self.idx = 0
527                 elif self.typeSelection.value == "short":
528                         self.idx = 1
529                 elif self.typeSelection.value == "desc":
530                         self.idx = 2
531                 else:
532                         self.idx = 3
533
534                 self.list.extend([
535                         getConfigListEntry(_("Exclude"), ConfigText(default = x, fixed_size = False))
536                                 for x in self.excludes[self.idx]
537                 ])
538                 self.lenExcludes = len(self.list)
539                 self.list.extend([
540                         getConfigListEntry(_("Include"), ConfigText(default = x, fixed_size = False))
541                                 for x in self.includes[self.idx]
542                 ])
543
544         def remove(self):
545                 idx = self["config"].getCurrentIndex()
546                 if idx and idx > 1:
547                         if idx < self.lenExcludes:
548                                 self.lenExcludes -= 1
549
550                         list = self["config"].getList()
551                         list.remove(self["config"].getCurrent())
552                         self["config"].setList(list)
553
554         def new(self):
555                 self.session.openWithCallback(
556                         self.typeSelected,
557                         ChoiceBox,
558                         _("Select type of Filter"),
559                         [
560                                 (_("Exclude"), 0),
561                                 (_("Include"), 1),
562                         ]
563                 )
564
565         def typeSelected(self, ret):
566                 if ret is not None:
567                         list = self["config"].getList()
568
569                         if ret[1] == 0:
570                                 pos = self.lenExcludes
571                                 self.lenExcludes += 1
572                                 text = ret[0]
573                         else:
574                                 pos = len(self.list)
575                                 text = ret[0]
576
577                         if self.typeSelection.value == "day":
578                                 entry = getConfigListEntry(text, ConfigSelection(choices = weekdays))
579                         else:
580                                 entry = getConfigListEntry(text, ConfigText(fixed_size = False))
581
582                         list.insert(pos, entry)
583                         self["config"].setList(list)
584
585         def cancel(self):
586                 if self["config"].isChanged():
587                         self.session.openWithCallback(self.cancelConfirm, MessageBox, _("Really close without saving settings?"))
588                 else:
589                         self.close(None)
590
591         def cancelConfirm(self, ret):
592                 if ret:
593                         self.close(None)
594
595         def save(self):
596                 self.saveCurrent()
597
598                 self.close((
599                         self.enabled.value,
600                         self.excludes,
601                         self.includes
602                 ))
603
604 class AutoTimerChannelEditor(Screen, ConfigListScreen):
605         """Edit allowed Channels of a AutoTimer"""
606
607         skin = """<screen name="AutoChannelEditor" title="Edit AutoTimer Channels" position="75,150" size="565,245">
608                 <widget name="config" position="5,5" size="555,200" scrollbarMode="showOnDemand" />
609                 <ePixmap position="5,205" zPosition="4" size="140,40" pixmap="skin_default/key-red.png" transparent="1" alphatest="on" />
610                 <ePixmap position="145,205" zPosition="4" size="140,40" pixmap="skin_default/key-green.png" transparent="1" alphatest="on" />
611                 <ePixmap position="285,205" zPosition="4" size="140,40" pixmap="skin_default/key-yellow.png" transparent="1" alphatest="on" />
612                 <ePixmap position="425,205" zPosition="4" size="140,40" pixmap="skin_default/key-blue.png" transparent="1" alphatest="on" />
613                 <widget name="key_red" position="5,205" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
614                 <widget name="key_green" position="145,205" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
615                 <widget name="key_yellow" position="285,205" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
616                 <widget name="key_blue" position="425,205" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
617         </screen>"""
618
619         def __init__(self, session, servicerestriction, servicelist):
620                 Screen.__init__(self, session)
621
622                 # Summary
623                 self.setup_title = "AutoTimer Channels"
624                 self.onChangedEntry = []
625
626                 self.list = [
627                         getConfigListEntry(_("Enable Channel Restriction"), ConfigEnableDisable(default = servicerestriction))
628                 ]
629
630                 self.list.extend([
631                         getConfigListEntry(_("Record on"), ConfigSelection(choices = [(str(x), ServiceReference(str(x)).getServiceName().replace('\xc2\x86', '').replace('\xc2\x87', ''))]))
632                                 for x in servicelist
633                 ])
634
635                 ConfigListScreen.__init__(self, self.list, session = session, on_change = self.changed)
636
637                 # Initialize Buttons
638                 self["key_red"] = Button(_("Cancel"))
639                 self["key_green"] = Button(_("OK"))
640                 self["key_yellow"] = Button(_("delete"))
641                 self["key_blue"] = Button(_("New"))
642
643                 # Define Actions
644                 self["actions"] = ActionMap(["SetupActions", "ColorActions"],
645                         {
646                                 "cancel": self.cancel,
647                                 "save": self.save,
648                                 "yellow": self.removeChannel,
649                                 "blue": self.newChannel
650                         }
651                 )
652
653                 # Trigger change
654                 self.changed()
655
656         def changed(self):
657                 for x in self.onChangedEntry:
658                         try:
659                                 x()
660                         except:
661                                 pass
662
663         def getCurrentEntry(self):
664                 return self["config"].getCurrent()[0]
665
666         def getCurrentValue(self):
667                 return str(self["config"].getCurrent()[1].getText())
668
669         def createSummary(self):
670                 return SetupSummary
671
672         def removeChannel(self):
673                 if self["config"].getCurrentIndex() != 0:
674                         list = self["config"].getList()
675                         list.remove(self["config"].getCurrent())
676                         self["config"].setList(list)
677
678         def newChannel(self):
679                 self.session.openWithCallback(
680                         self.finishedChannelSelection,
681                         SimpleChannelSelection,
682                         _("Select channel to record from")
683                 )
684
685         def finishedChannelSelection(self, *args):
686                 if len(args):
687                         list = self["config"].getList()
688                         sname = args[0].toString()
689
690                         # strip all after last :
691                         pos = sname.rfind(':')
692                         if pos != -1:
693                                 sname = sname[:pos+1]
694
695                         list.append(getConfigListEntry(_("Record on"), ConfigSelection(choices = [(sname, ServiceReference(args[0]).getServiceName().replace('\xc2\x86', '').replace('\xc2\x87', ''))])))
696                         self["config"].setList(list)
697
698         def cancel(self):
699                 if self["config"].isChanged():
700                         self.session.openWithCallback(self.cancelConfirm, MessageBox, _("Really close without saving settings?"))
701                 else:
702                         self.close(None)
703
704         def cancelConfirm(self, ret):
705                 if ret:
706                         self.close(None)
707
708         def save(self):
709                 list = self["config"].getList()
710                 restriction = list.pop(0)
711
712                 # Warning, accessing a ConfigListEntry directly might be considered evil!
713                 self.close((
714                         restriction[1].value,
715                         [
716                                 x[1].value.encode("UTF-8")
717                                         for x in list
718                         ]
719                 ))