make red/green rc button work in importer
[vuplus_dvbapp-plugin] / autotimer / src / AutoTimerImporter.py
1 # GUI (Screens)
2 from Screens.Screen import Screen
3 from Screens.MessageBox import MessageBox
4 from Screens.InputBox import InputBox
5
6 # GUI (Components)
7 from Components.ActionMap import ActionMap
8 from Components.Button import Button
9 from Components.TimerList import TimerList
10 from Components.SelectionList import SelectionList, SelectionEntryComponent
11
12 # Timer
13 from RecordTimer import AFTEREVENT
14
15 # Needed to convert our timestamp back and forth
16 from time import localtime
17
18 afterevent = { AFTEREVENT.NONE: _("do nothing"), AFTEREVENT.DEEPSTANDBY: _("go to deep standby"), AFTEREVENT.STANDBY: _("go to standby")}
19
20 class AutoTimerImportSelector(Screen):
21         def __init__(self, session, autotimer):
22                 Screen.__init__(self, session)
23                 self.skinName = "TimerEditList"
24
25                 self.autotimer = autotimer
26
27                 self.list = []
28                 self.fillTimerList()
29
30                 self["timerlist"] = TimerList(self.list)
31
32                 self["key_red"] = Button(_("Cancel"))
33                 self["key_green"] = Button(_("OK"))
34                 self["key_yellow"] = Button("")
35                 self["key_blue"] = Button("")
36
37                 self["actions"] = ActionMap(["OkCancelActions", "ColorActions"],
38                 {
39                         "ok": self.openImporter,
40                         "cancel": self.cancel,
41                         "green": self.openImporter,
42                         "red": self.cancel
43                 }, -1)
44                 self.onLayoutFinish.append(self.setCustomTitle)
45
46         def setCustomTitle(self):
47                 self.setTitle(_("Select a Timer to Import"))
48
49         def fillTimerList(self):
50                 del self.list[:]
51
52                 for timer in self.session.nav.RecordTimer.timer_list:
53                         self.list.append((timer, False))
54
55                 for timer in self.session.nav.RecordTimer.processed_timers:
56                         self.list.append((timer, True))
57                 self.list.sort(cmp = lambda x, y: x[0].begin < y[0].begin)
58
59         def importerClosed(self, ret):
60                 if ret is not None:
61                         ret.name = ret.match
62                 self.close(ret)
63
64         def openImporter(self):
65                 cur=self["timerlist"].getCurrent()
66                 if cur:
67                         self.session.openWithCallback(
68                                 self.importerClosed,
69                                 AutoTimerImporter,
70                                 cur,
71                                 self.autotimer
72                         )
73
74         def cancel(self):
75                 self.close(None)
76
77 class AutoTimerImporter(Screen):
78         """Import AutoTimer from Timer"""
79
80         skin = """<screen name="AutoTimerImporter" title="Import AutoTimer" position="75,155" size="565,280">
81                 <widget name="list" position="5,5" size="555,225" scrollbarMode="showOnDemand" />
82                 <ePixmap position="0,235" zPosition="4" size="140,40" pixmap="skin_default/key-red.png" transparent="1" alphatest="on" />
83                 <ePixmap position="140,235" zPosition="4" size="140,40" pixmap="skin_default/key-green.png" transparent="1" alphatest="on" />
84                 <ePixmap position="280,235" zPosition="4" size="140,40" pixmap="skin_default/key-yellow.png" transparent="1" alphatest="on" />
85                 <ePixmap position="420,235" zPosition="4" size="140,40" pixmap="skin_default/key-blue.png" transparent="1" alphatest="on" />
86                 <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" />
87                 <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" />
88                 <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" />
89                 <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" />
90         </screen>"""
91
92         def __init__(self, session, timer, autotimer):
93                 Screen.__init__(self, session)
94
95                 # Keep AutoTimer
96                 self.autotimer = autotimer
97
98                 # Initialize Buttons
99                 self["key_red"] = Button(_("Cancel"))
100                 self["key_green"] = Button(_("OK"))
101                 self["key_yellow"] = Button()
102                 self["key_blue"] = Button()
103
104                 begin = localtime(timer.begin)
105                 end = localtime(timer.end)
106                 list = [
107                         SelectionEntryComponent(
108                                 _("Match title: %s") % (timer.name),
109                                 timer.name,
110                                 0,
111                                 True
112                         ),
113                         SelectionEntryComponent(
114                                 _("Match Timespan: %02d:%02d - %02d:%02d") % (begin[3], begin[4], end[3], end[4]),
115                                 ((begin[3], begin[4]), (end[3], end[4])),
116                                 1,
117                                 True
118                         ),
119                         SelectionEntryComponent(
120                                 _("Only on Service: %s") % (timer.service_ref.getServiceName().replace('\xc2\x86', '').replace('\xc2\x87', '')),
121                                 str(timer.service_ref),
122                                 2,
123                                 True
124                         ),
125                         SelectionEntryComponent(
126                                 _("AfterEvent: %s") % (afterevent[timer.afterEvent]),
127                                 timer.afterEvent,
128                                 3,
129                                 True
130                         )
131                 ]
132
133                 self["list"] = SelectionList(list)
134
135                 # Define Actions
136                 self["actions"] = ActionMap(["OkCancelActions", "ColorActions"], 
137                 {
138                         "ok": self["list"].toggleSelection,
139                         "cancel": self.cancel,
140                         "red": self.cancel,
141                         "green": self.accept
142                 }, -1)
143
144         def cancel(self):
145                 self.session.openWithCallback(self.cancelConfirm, MessageBox, _("Really close without saving settings?"))
146
147         def cancelConfirm(self, ret):
148                 if ret:
149                         self.close(None)
150
151         def gotCustomMatch(self, ret):
152                 if ret:
153                         self.autotimer.match = ret
154                         # Check if we have a trailing whitespace
155                         if ret[-1:] == " ":
156                                 self.session.openWithCallback(
157                                         self.trailingWhitespaceRemoval,
158                                         MessageBox,
159                                         _('You entered "%s" as Text to match.\nDo you want to remove trailing whitespaces?') % (ret)
160                                 )
161                         # Just confirm else
162                         else:
163                                 self.close(self.autotimer)
164
165         def trailingWhitespaceRemoval(self, ret):
166                 if ret is not None:
167                         if ret:
168                                 self.autotimer.match = self.autotimer.match.rstrip()
169                         self.close(self.autotimer)
170
171         def accept(self):
172                 list = self["list"].getSelectionsList()
173
174                 for item in list:
175                         if item[2] == 0: # Match (should maybe be forced?)
176                                 self.autotimer.match = item[1]
177                         if item[2] == 1: # Timespan
178                                 self.autotimer.timespan = item[1]
179                         if item[2] == 2: # Service
180                                 value = item[1]
181
182                                 # strip all after last :
183                                 pos = value.rfind(':')
184                                 if pos != -1:
185                                         value = value[:pos+1]
186
187                                 self.autotimer.services = [value]
188                         if item[2] == 3: # AfterEvent
189                                 self.autotimer.afterevent = [(item[1], None)]
190
191                 if self.autotimer.match == "":
192                         self.session.openWithCallback(
193                                         self.gotCustomMatch,
194                                         InputBox,
195                                         title = _("Please provide a Text to Match")
196                         )
197                 else:
198                         self.close(self.autotimer)