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