add new item to importer to automatically set exact and case-sensitive match
[vuplus_dvbapp-plugin] / 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.TimerList import TimerList
14 from Components.SelectionList import SelectionList, SelectionEntryComponent
15
16 # Timer
17 from RecordTimer import AFTEREVENT
18
19 # Needed to convert our timestamp back and forth
20 from time import localtime
21
22 afterevent = {
23         AFTEREVENT.NONE: _("do nothing"),
24         AFTEREVENT.DEEPSTANDBY: _("go to deep standby"),
25         AFTEREVENT.STANDBY: _("go to standby"),
26         AFTEREVENT.AUTO: _("auto")
27 }
28
29 class AutoTimerImportSelector(Screen):
30         def __init__(self, session, autotimer):
31                 Screen.__init__(self, session)
32                 self.skinName = "TimerEditList"
33
34                 self.autotimer = autotimer
35
36                 self.list = []
37                 self.fillTimerList()
38
39                 self["timerlist"] = TimerList(self.list)
40
41                 self["key_red"] = Button(_("Cancel"))
42                 self["key_green"] = Button(_("OK"))
43                 self["key_yellow"] = Button("")
44                 self["key_blue"] = Button("")
45
46                 self["actions"] = ActionMap(["OkCancelActions", "ColorActions"],
47                 {
48                         "ok": self.openImporter,
49                         "cancel": self.cancel,
50                         "green": self.openImporter,
51                         "red": self.cancel
52                 }, -1)
53                 self.onLayoutFinish.append(self.setCustomTitle)
54
55         def setCustomTitle(self):
56                 self.setTitle(_("Select a Timer to Import"))
57
58         def fillTimerList(self):
59                 l = self.list
60                 del l[:]
61
62                 for timer in self.session.nav.RecordTimer.timer_list:
63                         l.append((timer, False))
64
65                 for timer in self.session.nav.RecordTimer.processed_timers:
66                         l.append((timer, True))
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="75,155" size="565,280">
100                 <widget name="list" position="5,5" size="555,225" scrollbarMode="showOnDemand" />
101                 <ePixmap position="0,235" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
102                 <ePixmap position="140,235" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
103                 <ePixmap position="280,235" zPosition="4" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
104                 <ePixmap position="420,235" zPosition="4" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
105                 <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" />
106                 <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" />
107                 <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" />
108                 <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" />
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"] = Button(_("Cancel"))
119                 self["key_green"] = Button(_("OK"))
120                 self["key_yellow"] = Button()
121                 self["key_blue"] = Button()
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                                 # strip all after last :
274                                 pos = value.rfind(':')
275                                 if pos != -1:
276                                         value = value[:pos+1]
277
278                                 autotimer.services = [value]
279                         elif item[2] == 4: # AfterEvent
280                                 autotimer.afterevent = [(item[1], None)]
281                         elif item[2] == 5: # Justplay
282                                 autotimer.justplay = item[1]
283                         elif item[2] == 6: # Location
284                                 autotimer.destination = item[1]
285                         elif item[2] == 7: # Tags
286                                 autotimer.tags = item[1]
287                         elif item[2] == 8: # Exact match
288                                 autotimer.searchType = "exact"
289                                 autotimer.searchCase = "sensitive"
290
291                 if autotimer.match == "":
292                         self.session.openWithCallback(
293                                         self.gotCustomMatch,
294                                         InputBox,
295                                         title = _("Please provide a Text to match")
296                         )
297                 else:
298                         self.close((
299                                 autotimer,
300                                 self.session
301                         ))
302