AutoTimer: Use named strings for translation
[enigma2-plugins.git] / autotimer / src / AutoTimerImporter.py
1 # -*- coding: UTF-8 -*-
2 # for localized messages
3 from . import _
4
5 # GUI (Screens)
6 from Screens.Screen import Screen
7 from Screens.MessageBox import MessageBox
8 from Screens.InputBox import InputBox
9
10 # GUI (Components)
11 from Components.ActionMap import ActionMap
12 from Components.Button import Button
13 from Components.SelectionList import SelectionList, SelectionEntryComponent
14 from Components.Sources.StaticText import StaticText
15 from Components.TimerList import TimerList
16
17 # Timer
18 from RecordTimer import AFTEREVENT
19
20 # Needed to convert our timestamp back and forth
21 from time import localtime
22 from AutoTimerEditor import weekdays
23
24 from enigma import eServiceReference
25
26 afterevent = {
27         AFTEREVENT.NONE: _("do nothing"),
28         AFTEREVENT.DEEPSTANDBY: _("go to deep standby"),
29         AFTEREVENT.STANDBY: _("go to standby"),
30         AFTEREVENT.AUTO: _("auto")
31 }
32
33 class AutoTimerImportSelector(Screen):
34         def __init__(self, session, autotimer):
35                 Screen.__init__(self, session)
36                 self.skinName = "TimerEditList"
37
38                 self.autotimer = autotimer
39
40                 self.list = []
41                 self.fillTimerList()
42
43                 self["timerlist"] = TimerList(self.list)
44
45                 self["key_red"] = Button(_("Cancel"))
46                 self["key_green"] = Button(_("OK"))
47                 self["key_yellow"] = Button("")
48                 self["key_blue"] = Button("")
49
50                 self["actions"] = ActionMap(["OkCancelActions", "ColorActions"],
51                 {
52                         "ok": self.openImporter,
53                         "cancel": self.cancel,
54                         "green": self.openImporter,
55                         "red": self.cancel
56                 }, -1)
57                 self.onLayoutFinish.append(self.setCustomTitle)
58
59         def setCustomTitle(self):
60                 self.setTitle(_("Select a timer to import"))
61
62         def fillTimerList(self):
63                 l = self.list
64                 del l[:]
65
66                 l.extend([(timer, False) for timer in self.session.nav.RecordTimer.timer_list])
67                 l.extend([(timer, True) for timer in self.session.nav.RecordTimer.processed_timers])
68                 l.sort(key = lambda x: x[0].begin)
69
70         def importerClosed(self, ret):
71                 ret = ret and ret[0]
72                 if ret is not None:
73                         ret.name = ret.match
74                 self.close(ret)
75
76         def openImporter(self):
77                 cur=self["timerlist"].getCurrent()
78                 if cur:
79                         self.session.openWithCallback(
80                                 self.importerClosed,
81                                 AutoTimerImporter,
82                                 self.autotimer,
83                                 cur.name,
84                                 cur.begin,
85                                 cur.end,
86                                 cur.disabled,
87                                 cur.service_ref,
88                                 cur.afterEvent,
89                                 cur.justplay,
90                                 cur.dirname,
91                                 cur.tags
92                         )
93
94         def cancel(self):
95                 self.close(None)
96
97 class AutoTimerImporter(Screen):
98         """Import AutoTimer from Timer"""
99
100         skin = """<screen name="AutoTimerImporter" title="Import AutoTimer" position="center,center" size="565,290">
101                 <ePixmap position="0,0" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
102                 <ePixmap position="140,0" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
103                 <ePixmap position="280,0" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
104                 <ePixmap position="420,0" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
105                 <widget source="key_red" render="Label" position="0,0" zPosition="1" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
106                 <widget source="key_green" render="Label" position="140,0" zPosition="1" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
107                 <widget source="key_yellow" render="Label" position="280,0" zPosition="1" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
108                 <widget source="key_blue" render="Label" position="420,0" zPosition="1" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
109                 <widget name="list" position="5,45" size="555,240" scrollbarMode="showOnDemand" />
110         </screen>"""
111
112         def __init__(self, session, autotimer, name, begin, end, disabled, sref, afterEvent, justplay, dirname, tags):
113                 Screen.__init__(self, session)
114
115                 # Keep AutoTimer
116                 self.autotimer = autotimer
117
118                 # Initialize Buttons
119                 self["key_red"] = StaticText(_("Cancel"))
120                 self["key_green"] = StaticText(_("OK"))
121                 self["key_yellow"] = StaticText()
122                 self["key_blue"] = StaticText()
123
124                 entries = []
125                 append = entries.append
126
127                 if disabled is not None:
128                         append(
129                                 SelectionEntryComponent(
130                                         ': '.join((_("Enabled"), {True: _("disable"), False: _("enable")}[bool(disabled)])),
131                                         not disabled,
132                                         0,
133                                         True
134                         ))
135
136                 if name != "":
137                         append(
138                                 SelectionEntryComponent(
139                                         _("Match title: %s") % (name),
140                                         name,
141                                         1,
142                                         True
143                         ))
144                         append(
145                                 SelectionEntryComponent(
146                                         _("Exact match"),
147                                         True,
148                                         8,
149                                         True
150                         ))
151
152                 if begin and end:
153                         begin = localtime(begin)
154                         end = localtime(end)
155                         append(
156                                 SelectionEntryComponent(
157                                         _("Match Timespan: %(begin_hour)02d:%(begin_minutes)02d - %(end_hour)02d:%(end_minutes)02d") % \
158                                         {"begin_hour":begin[3], "begin_minutes":begin[4], "end_hour":end[3], "end_minutes":end[4]},
159                                         ((begin[3], begin[4]), (end[3], end[4])),
160                                         2,
161                                         True
162                         ))
163                         append(
164                                 SelectionEntryComponent(
165                                         _("Only on Weekday: %s") % (weekdays[begin.tm_wday][1],), # XXX: the lookup is dirty but works :P
166                                         str(begin.tm_wday),
167                                         9,
168                                         True
169                         ))
170
171                 if sref:
172                         append(
173                                 SelectionEntryComponent(
174                                         _("Only on Service: %s") % (sref.getServiceName().replace('\xc2\x86', '').replace('\xc2\x87', '')),
175                                         str(sref),
176                                         3,
177                                         True
178                         ))
179
180                 if afterEvent is not None:
181                         append(
182                                 SelectionEntryComponent(
183                                         ': '.join((_("After event"), afterevent[afterEvent])),
184                                         afterEvent,
185                                         4,
186                                         True
187                         ))
188
189                 if justplay is not None:
190                         append(
191                                 SelectionEntryComponent(
192                                         ': '.join((_("Timer type"), {0: _("record"), 1: _("zap")}[int(justplay)])),
193                                         int(justplay),
194                                         5,
195                                         True
196                         ))
197
198                 if dirname is not None:
199                         append(
200                                 SelectionEntryComponent(
201                                         ': '.join((_("Location"), dirname or "/media/hdd/movie/")),
202                                         dirname,
203                                         6,
204                                         True
205                         ))
206
207                 if tags:
208                         append(
209                                 SelectionEntryComponent(
210                                         ': '.join((_("Tags"), ', '.join(tags))),
211                                         tags,
212                                         7,
213                                         True
214                         ))
215
216                 self["list"] = SelectionList(entries)
217
218                 # Define Actions
219                 self["actions"] = ActionMap(["OkCancelActions", "ColorActions"],
220                 {
221                         "ok": self["list"].toggleSelection,
222                         "cancel": self.cancel,
223                         "red": self.cancel,
224                         "green": self.accept
225                 }, -1)
226
227                 self.onLayoutFinish.append(self.setCustomTitle)
228
229         def setCustomTitle(self):
230                 self.setTitle(_("Import AutoTimer"))
231
232         def cancel(self):
233                 self.session.openWithCallback(
234                         self.cancelConfirm,
235                         MessageBox,
236                         _("Really close without saving settings?")
237                 )
238
239         def cancelConfirm(self, ret):
240                 if ret:
241                         self.close(None)
242
243         def gotCustomMatch(self, ret):
244                 if ret:
245                         self.autotimer.match = ret
246                         # Check if we have a trailing whitespace
247                         if ret[-1:] == " ":
248                                 self.session.openWithCallback(
249                                         self.trailingWhitespaceRemoval,
250                                         MessageBox,
251                                         _('You entered "%s" as Text to match.\nDo you want to remove trailing whitespaces?') % (ret)
252                                 )
253                         # Just confirm else
254                         else:
255                                 self.close((
256                                 self.autotimer,
257                                 self.session
258                         ))
259
260         def trailingWhitespaceRemoval(self, ret):
261                 if ret is not None:
262                         if ret:
263                                 self.autotimer.match = self.autotimer.match.rstrip()
264                         self.close((
265                                 self.autotimer,
266                                 self.session
267                         ))
268
269         def accept(self):
270                 list = self["list"].getSelectionsList()
271                 autotimer = self.autotimer
272
273                 for item in list:
274                         if item[2] == 0: # Enable
275                                 autotimer.enabled = item[1]
276                         elif item[2] == 1: # Match
277                                 autotimer.match = item[1]
278                         elif item[2] == 2: # Timespan
279                                 autotimer.timespan = item[1]
280                         elif item[2] == 3: # Service
281                                 value = item[1]
282
283                                 myref = eServiceReference(value)
284                                 if not (myref.flags & eServiceReference.isGroup):
285                                         # strip all after last :
286                                         pos = value.rfind(':')
287                                         if pos != -1:
288                                                 if value[pos-1] == ':':
289                                                         pos -= 1
290                                                 value = value[:pos+1]
291
292                                 autotimer.services = [value]
293                         elif item[2] == 4: # AfterEvent
294                                 autotimer.afterevent = [(item[1], None)]
295                         elif item[2] == 5: # Justplay
296                                 autotimer.justplay = item[1]
297                         elif item[2] == 6: # Location
298                                 autotimer.destination = item[1]
299                         elif item[2] == 7: # Tags
300                                 autotimer.tags = item[1]
301                         elif item[2] == 8: # Exact match
302                                 autotimer.searchType = "exact"
303                                 autotimer.searchCase = "sensitive"
304                         elif item[2] == 9: # Weekday
305                                 includes = [
306                                                 autotimer.getIncludedTitle(),
307                                                 autotimer.getIncludedShort(),
308                                                 autotimer.getIncludedDescription(),
309                                                 [item[1]],
310                                 ]
311                                 autotimer.include = includes
312
313                 if autotimer.match == "":
314                         self.session.openWithCallback(
315                                         self.gotCustomMatch,
316                                         InputBox,
317                                         title = _("Please provide a Text to match")
318                         )
319                 else:
320                         self.close((
321                                 autotimer,
322                                 self.session
323                         ))
324