intial commit of autotimer, epgrefresh and werbezapper to enigma2-plugins
[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"],
38                 {
39                         "ok": self.openImporter,
40                         "cancel": self.cancel,
41                 }, -1)
42                 self.onLayoutFinish.append(self.setCustomTitle)
43
44         def setCustomTitle(self):
45                 self.setTitle(_("Select a Timer to Import"))
46
47         def fillTimerList(self):
48                 del self.list[:]
49
50                 for timer in self.session.nav.RecordTimer.timer_list:
51                         self.list.append((timer, False))
52
53                 for timer in self.session.nav.RecordTimer.processed_timers:
54                         self.list.append((timer, True))
55                 self.list.sort(cmp = lambda x, y: x[0].begin < y[0].begin)
56
57         def importerClosed(self, ret):
58                 if ret is not None:
59                         ret.name = ret.match
60                 self.close(ret)
61
62         def openImporter(self):
63                 cur=self["timerlist"].getCurrent()
64                 if cur:
65                         self.session.openWithCallback(
66                                 self.importerClosed,
67                                 AutoTimerImporter,
68                                 cur,
69                                 self.autotimer
70                         )
71
72         def cancel(self):
73                 self.close(None)
74
75 class AutoTimerImporter(Screen):
76         """Import AutoTimer from Timer"""
77
78         skin = """<screen name="AutoTimerImporter" title="Import AutoTimer" position="75,155" size="565,280">
79                 <widget name="list" position="5,5" size="555,225" scrollbarMode="showOnDemand" />
80                 <ePixmap position="0,235" zPosition="4" size="140,40" pixmap="skin_default/key-red.png" transparent="1" alphatest="on" />
81                 <ePixmap position="140,235" zPosition="4" size="140,40" pixmap="skin_default/key-green.png" transparent="1" alphatest="on" />
82                 <ePixmap position="280,235" zPosition="4" size="140,40" pixmap="skin_default/key-yellow.png" transparent="1" alphatest="on" />
83                 <ePixmap position="420,235" zPosition="4" size="140,40" pixmap="skin_default/key-blue.png" transparent="1" alphatest="on" />
84                 <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" />
85                 <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" />
86                 <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" />
87                 <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" />
88         </screen>"""
89
90         def __init__(self, session, timer, autotimer):
91                 Screen.__init__(self, session)
92
93                 # Keep AutoTimer
94                 self.autotimer = autotimer
95
96                 # Initialize Buttons
97                 self["key_red"] = Button(_("Cancel"))
98                 self["key_green"] = Button(_("OK"))
99                 self["key_yellow"] = Button()
100                 self["key_blue"] = Button()
101
102                 begin = localtime(timer.begin)
103                 end = localtime(timer.end)
104                 list = [
105                         SelectionEntryComponent(
106                                 _("Match title: %s") % (timer.name),
107                                 timer.name,
108                                 0,
109                                 True
110                         ),
111                         SelectionEntryComponent(
112                                 _("Match Timespan: %02d:%02d - %02d:%02d") % (begin[3], begin[4], end[3], end[4]),
113                                 ((begin[3], begin[4]), (end[3], end[4])),
114                                 1,
115                                 True
116                         ),
117                         SelectionEntryComponent(
118                                 _("Only on Service: %s") % (timer.service_ref.getServiceName().replace('\xc2\x86', '').replace('\xc2\x87', '')),
119                                 str(timer.service_ref),
120                                 2,
121                                 True
122                         ),
123                         SelectionEntryComponent(
124                                 _("AfterEvent: %s") % (afterevent[timer.afterEvent]),
125                                 timer.afterEvent,
126                                 3,
127                                 True
128                         )
129                 ]
130
131                 self["list"] = SelectionList(list)
132
133                 # Define Actions
134                 self["actions"] = ActionMap(["OkCancelActions", "ColorActions"], 
135                 {
136                         "ok": self["list"].toggleSelection,
137                         "cancel": self.cancel,
138                         "green": self.accept
139                 }, -1)
140
141         def cancel(self):
142                 self.session.openWithCallback(self.cancelConfirm, MessageBox, _("Really close without saving settings?"))
143
144         def cancelConfirm(self, ret):
145                 if ret:
146                         self.close(None)
147
148         def gotCustomMatch(self, ret):
149                 if ret:
150                         self.autotimer.match = ret
151                         # Check if we have a trailing whitespace
152                         if ret[-1:] == " ":
153                                 self.session.openWithCallback(
154                                         self.trailingWhitespaceRemoval,
155                                         MessageBox,
156                                         _('You entered "%s" as Text to match.\nDo you want to remove trailing whitespaces?') % (ret)
157                                 )
158                         # Just confirm else
159                         else:
160                                 self.close(self.autotimer)
161
162         def trailingWhitespaceRemoval(self, ret):
163                 if ret is not None:
164                         if ret:
165                                 self.autotimer.match = self.autotimer.match.rstrip()
166                         self.close(self.autotimer)
167
168         def accept(self):
169                 list = self["list"].getSelectionsList()
170
171                 for item in list:
172                         if item[2] == 0: # Match (should maybe be forced?)
173                                 self.autotimer.match = item[1]
174                         if item[2] == 1: # Timespan
175                                 self.autotimer.timespan = item[1]
176                         if item[2] == 2: # Service
177                                 value = item[1]
178
179                                 # strip all after last :
180                                 pos = value.rfind(':')
181                                 if pos != -1:
182                                         value = value[:pos+1]
183
184                                 self.autotimer.services = [value]
185                         if item[2] == 3: # AfterEvent
186                                 self.autotimer.afterevent = [(item[1], None)]
187
188                 if self.autotimer.match == "":
189                         self.session.openWithCallback(
190                                         self.gotCustomMatch,
191                                         InputBox,
192                                         title = _("Please provide a Text to Match")
193                         )
194                 else:
195                         self.close(self.autotimer)