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