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