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