Introduction of localization including german messages
[vuplus_dvbapp-plugin] / autotimer / src / AutoTimerImporter.py
1 # for localized messages
2 from . import _
3
4 # GUI (Screens)
5 from Screens.Screen import Screen
6 from Screens.MessageBox import MessageBox
7 from Screens.InputBox import InputBox
8
9 # GUI (Components)
10 from Components.ActionMap import ActionMap
11 from Components.Button import Button
12 from Components.TimerList import TimerList
13 from Components.SelectionList import SelectionList, SelectionEntryComponent
14
15 # Timer
16 from RecordTimer import AFTEREVENT
17
18 # Needed to convert our timestamp back and forth
19 from time import localtime
20
21 afterevent = { AFTEREVENT.NONE: _("do nothing"), AFTEREVENT.DEEPSTANDBY: _("go to deep standby"), AFTEREVENT.STANDBY: _("go to standby")}
22
23 class AutoTimerImportSelector(Screen):
24         def __init__(self, session, autotimer):
25                 Screen.__init__(self, session)
26                 self.skinName = "TimerEditList"
27
28                 self.autotimer = autotimer
29
30                 self.list = []
31                 self.fillTimerList()
32
33                 self["timerlist"] = TimerList(self.list)
34
35                 self["key_red"] = Button(_("Cancel"))
36                 self["key_green"] = Button(_("OK"))
37                 self["key_yellow"] = Button("")
38                 self["key_blue"] = Button("")
39
40                 self["actions"] = ActionMap(["OkCancelActions", "ColorActions"],
41                 {
42                         "ok": self.openImporter,
43                         "cancel": self.cancel,
44                         "green": self.openImporter,
45                         "red": self.cancel
46                 }, -1)
47                 self.onLayoutFinish.append(self.setCustomTitle)
48
49         def setCustomTitle(self):
50                 self.setTitle(_("Select a Timer to Import"))
51
52         def fillTimerList(self):
53                 del self.list[:]
54
55                 for timer in self.session.nav.RecordTimer.timer_list:
56                         self.list.append((timer, False))
57
58                 for timer in self.session.nav.RecordTimer.processed_timers:
59                         self.list.append((timer, True))
60                 self.list.sort(cmp = lambda x, y: x[0].begin < y[0].begin)
61
62         def importerClosed(self, ret):
63                 ret = ret and ret[0]
64                 if ret is not None:
65                         ret.name = ret.match
66                 self.close(ret)
67
68         def openImporter(self):
69                 cur=self["timerlist"].getCurrent()
70                 if cur:
71                         self.session.openWithCallback(
72                                 self.importerClosed,
73                                 AutoTimerImporter,
74                                 self.autotimer,
75                                 cur.name,
76                                 cur.begin,
77                                 cur.end,
78                                 cur.disabled,
79                                 cur.service_ref,
80                                 cur.afterEvent,
81                                 cur.justplay,
82                                 cur.dirname
83                         )
84
85         def cancel(self):
86                 self.close(None)
87
88 class AutoTimerImporter(Screen):
89         """Import AutoTimer from Timer"""
90
91         skin = """<screen name="AutoTimerImporter" title="Import AutoTimer" position="75,155" size="565,280">
92                 <widget name="list" position="5,5" size="555,225" scrollbarMode="showOnDemand" />
93                 <ePixmap position="0,235" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
94                 <ePixmap position="140,235" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
95                 <ePixmap position="280,235" zPosition="4" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
96                 <ePixmap position="420,235" zPosition="4" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
97                 <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" />
98                 <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" />
99                 <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" />
100                 <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" />
101         </screen>"""
102
103         def __init__(self, session, autotimer, name, begin, end, disabled, sref, afterEvent, justplay, dirname):
104                 Screen.__init__(self, session)
105
106                 # Keep AutoTimer
107                 self.autotimer = autotimer
108
109                 # Initialize Buttons
110                 self["key_red"] = Button(_("Cancel"))
111                 self["key_green"] = Button(_("OK"))
112                 self["key_yellow"] = Button()
113                 self["key_blue"] = Button()
114
115                 list = []
116
117                 if disabled is not None:
118                         list.append(
119                                 SelectionEntryComponent(
120                                         ': '.join([_("Enabled"), {True: _("disable"), False: _("enable")}[bool(disabled)]]),
121                                         not disabled,
122                                         0,
123                                         True
124                         ))
125
126                 if name != "":
127                         list.append(
128                                 SelectionEntryComponent(
129                                         _("Match title: %s") % (name),
130                                         name,
131                                         1,
132                                         True
133                         ))
134                 
135                 if begin and end:
136                         begin = localtime(begin)
137                         end = localtime(end)
138                         list.append(
139                                 SelectionEntryComponent(
140                                         _("Match Timespan: %02d:%02d - %02d:%02d") % (begin[3], begin[4], end[3], end[4]),
141                                         ((begin[3], begin[4]), (end[3], end[4])),
142                                         2,
143                                         True
144                         ))
145                 
146                 if sref:
147                         list.append(
148                                 SelectionEntryComponent(
149                                         _("Only on Service: %s") % (sref.getServiceName().replace('\xc2\x86', '').replace('\xc2\x87', '')),
150                                         str(sref),
151                                         3,
152                                         True
153                         ))
154                 
155                 if afterEvent is not None:
156                         list.append(
157                                 SelectionEntryComponent(
158                                         ': '.join([_("After event"), afterevent[afterEvent]]),
159                                         afterEvent,
160                                         4,
161                                         True
162                         ))
163                         
164                 if justplay is not None:
165                         list.append(
166                                 SelectionEntryComponent(
167                                         ': '.join([_("Timer Type"), {0: _("record"), 1: _("zap")}[int(justplay)]]),
168                                         int(justplay),
169                                         5,
170                                         True
171                         ))
172
173                 if dirname is not None:
174                         list.append(
175                                 SelectionEntryComponent(
176                                         ': '.join([_("Location"), dirname or "/hdd/movie/"]),
177                                         dirname,
178                                         6,
179                                         True
180                         ))
181
182                 self["list"] = SelectionList(list)
183
184                 # Define Actions
185                 self["actions"] = ActionMap(["OkCancelActions", "ColorActions"], 
186                 {
187                         "ok": self["list"].toggleSelection,
188                         "cancel": self.cancel,
189                         "red": self.cancel,
190                         "green": self.accept
191                 }, -1)
192
193         def cancel(self):
194                 self.session.openWithCallback(
195                         self.cancelConfirm,
196                         MessageBox,
197                         _("Really close without saving settings?")
198                 )
199
200         def cancelConfirm(self, ret):
201                 if ret:
202                         self.close(None)
203
204         def gotCustomMatch(self, ret):
205                 if ret:
206                         self.autotimer.match = ret
207                         # Check if we have a trailing whitespace
208                         if ret[-1:] == " ":
209                                 self.session.openWithCallback(
210                                         self.trailingWhitespaceRemoval,
211                                         MessageBox,
212                                         _('You entered "%s" as Text to match.\nDo you want to remove trailing whitespaces?') % (ret)
213                                 )
214                         # Just confirm else
215                         else:
216                                 self.close((
217                                 self.autotimer,
218                                 self.session
219                         ))
220
221         def trailingWhitespaceRemoval(self, ret):
222                 if ret is not None:
223                         if ret:
224                                 self.autotimer.match = self.autotimer.match.rstrip()
225                         self.close((
226                                 self.autotimer,
227                                 self.session
228                         ))
229
230         def accept(self):
231                 list = self["list"].getSelectionsList()
232
233                 for item in list:
234                         if item[2] == 0: # Enable
235                                 self.autotimer.enabled = item[1]
236                         elif item[2] == 1: # Match
237                                 self.autotimer.match = item[1]
238                         elif item[2] == 2: # Timespan
239                                 self.autotimer.timespan = item[1]
240                         elif item[2] == 3: # Service
241                                 value = item[1]
242
243                                 # strip all after last :
244                                 pos = value.rfind(':')
245                                 if pos != -1:
246                                         value = value[:pos+1]
247
248                                 self.autotimer.services = [value]
249                         elif item[2] == 4: # AfterEvent
250                                 self.autotimer.afterevent = [(item[1], None)]
251                         elif item[2] == 5: # Justplay
252                                 self.autotimer.justplay = item[1]
253                         elif item[2] == 6: # Location
254                                 self.autotimer.destination = item[1]
255
256                 if self.autotimer.match == "":
257                         self.session.openWithCallback(
258                                         self.gotCustomMatch,
259                                         InputBox,
260                                         title = _("Please provide a Text to match")
261                         )
262                 else:
263                         self.close((
264                                 self.autotimer,
265                                 self.session
266                         ))