fix order for cable setup
[enigma2.git] / lib / python / Screens / TimerEntry.py
1 from Screen import Screen
2 import ChannelSelection
3 from ServiceReference import ServiceReference
4 from Components.config import *
5 from Components.ActionMap import ActionMap, NumberActionMap
6 from Components.ConfigList import ConfigList
7 from Components.MenuList import MenuList
8 from Components.Button import Button
9 from Components.NimManager import nimmanager
10 from Components.Label import Label
11 from Components.Pixmap import Pixmap
12 from Screens.SubserviceSelection import SubserviceSelection
13 from Screens.MessageBox import MessageBox
14 from enigma import eEPGCache
15 import time
16 import datetime
17
18 class TimerEntry(Screen):
19         def __init__(self, session, timer):
20                 Screen.__init__(self, session)
21                 self.timer = timer
22                 
23                 self["oktext"] = Label(_("OK"))
24                 self["canceltext"] = Label(_("Cancel"))
25                 self["ok"] = Pixmap()
26                 self["cancel"] = Pixmap()
27
28                 self.createConfig()
29
30                 self["actions"] = NumberActionMap(["SetupActions", "TextEntryActions"],
31                 {
32                         "ok": self.keySelect,
33                         "save": self.keyGo,
34                         "cancel": self.keyCancel,
35                         "left": self.keyLeft,
36                         "right": self.keyRight,
37                         "delete": self.keyDelete,
38                         "1": self.keyNumberGlobal,
39                         "2": self.keyNumberGlobal,
40                         "3": self.keyNumberGlobal,
41                         "4": self.keyNumberGlobal,
42                         "5": self.keyNumberGlobal,
43                         "6": self.keyNumberGlobal,
44                         "7": self.keyNumberGlobal,
45                         "8": self.keyNumberGlobal,
46                         "9": self.keyNumberGlobal,
47                         "0": self.keyNumberGlobal
48                 }, -1)
49
50                 self.list = []
51                 self["config"] = ConfigList(self.list)
52                 self.createSetup("config")
53
54         def createConfig(self):
55                         config.timerentry = ConfigSubsection()
56                         
57                         # calculate default values
58                         day = []
59                         weekday = 0
60                         for x in range(0,7):
61                                 day.append(1)
62                         if (self.timer.repeated != 0): # repeated
63                                 type = 1 # repeated
64                                 if (self.timer.repeated == 31): # Mon-Fri
65                                         repeated = 2 # Mon - Fri
66                                 elif (self.timer.repeated == 127): # daily
67                                         repeated = 0 # daily
68                                 else:
69                                         flags = self.timer.repeated
70                                         repeated = 3 # user defined
71                                         count = 0
72                                         for x in range(0, 7):
73                                                 if (flags == 1): # weekly
74                                                         print "Set to weekday " + str(x)
75                                                         weekday = x
76                                                 if (flags & 1 == 1): # set user defined flags
77                                                         day[x] = 0
78                                                         count += 1
79                                                 else:
80                                                         day[x] = 1
81                                                 flags = flags >> 1
82                                         if (count == 1):
83                                                 repeated = 1 # weekly
84                         else: # once
85                                 type = 0
86                                 repeated = 0
87                                 weekday = (int(strftime("%w", time.localtime(self.timer.begin))) - 1) % 7
88                                 day[weekday] = 0
89                         
90                         config.timerentry.type = configElement_nonSave("config.timerentry.type", configSelection, type, (_("once"), _("repeated")))
91                         config.timerentry.name = configElement_nonSave("config.timerentry.name", configText, self.timer.name, (configText.extendableSize, self.keyRightCallback))
92                         config.timerentry.description = configElement_nonSave("config.timerentry.description", configText, self.timer.description, (configText.extendableSize, self.keyRightCallback))
93
94                         config.timerentry.repeated = configElement_nonSave("config.timerentry.repeated", configSelection, repeated, (_("daily"), _("weekly"), _("Mon-Fri"), _("user defined")))
95
96                         config.timerentry.startdate = configElement_nonSave("config.timerentry.startdate", configDateTime, self.timer.begin, (_("%d.%B %Y"), 86400))
97                         config.timerentry.starttime = configElement_nonSave("config.timerentry.starttime", configSequence, [int(time.strftime("%H", time.localtime(self.timer.begin))), int(time.strftime("%M", time.localtime(self.timer.begin)))], configsequencearg.get("CLOCK"))
98
99                         config.timerentry.enddate = configElement_nonSave("config.timerentry.enddate", configDateTime, self.timer.end, (_("%d.%B %Y"), 86400))
100                         config.timerentry.endtime = configElement_nonSave("config.timerentry.endtime", configSequence, [int(time.strftime("%H", time.localtime(self.timer.end))), int(time.strftime("%M", time.localtime(self.timer.end)))], configsequencearg.get("CLOCK"))
101
102                         config.timerentry.weekday = configElement_nonSave("config.timerentry.weekday", configSelection, weekday, (_("Monday"), _("Tuesday"), _("Wednesday"), _("Thursday"), _("Friday"), _("Saturday"), _("Sunday")))
103
104                         config.timerentry.day = []
105                         for x in range(0,7):
106                                 config.timerentry.day.append(configElement_nonSave("config.timerentry.day[" + str(x) + "]", configSelection, day[x], (_("yes"), _("no"))))
107
108
109                         # FIXME some service-chooser needed here
110                         servicename = "N/A"
111                         try: # no current service available?
112                                 servicename = str(self.timer.service_ref.getServiceName())
113                         except:
114                                 pass
115                         config.timerentry.service = configElement_nonSave("config.timerentry.service", configSelection, 0, ((servicename),))
116                         
117                         config.timerentry.startdate.addNotifier(self.checkDate)
118                         config.timerentry.enddate.addNotifier(self.checkDate)
119
120         def checkDate(self, configElement):
121                 if (configElement.getConfigPath() == "config.timerentry.startdate"):
122                         if (config.timerentry.enddate.value < config.timerentry.startdate.value):
123                                 config.timerentry.enddate.value = config.timerentry.startdate.value
124                                 config.timerentry.enddate.change()
125                                 try:
126                                         self["config"].invalidate(config.timerentry.enddate)
127                                 except:
128                                         pass
129                 if (configElement.getConfigPath() == "config.timerentry.enddate"):
130                         if (config.timerentry.enddate.value < config.timerentry.startdate.value):
131                                 config.timerentry.startdate.value = config.timerentry.enddate.value
132                                 config.timerentry.startdate.change()
133                                 try:
134                                         self["config"].invalidate(config.timerentry.startdate)
135                                 except:
136                                         pass
137
138         def createSetup(self, widget):
139                 self.list = []
140                 self.list.append(getConfigListEntry(_("Name"), config.timerentry.name))
141                 self.list.append(getConfigListEntry(_("Description"), config.timerentry.description))
142                 self.timerTypeEntry = getConfigListEntry(_("Timer Type"), config.timerentry.type)
143                 self.list.append(self.timerTypeEntry)
144
145                 if (config.timerentry.type.value == 0): # once
146                         self.frequencyEntry = None
147                 else: # repeated
148                         self.frequencyEntry = getConfigListEntry(_("Frequency"), config.timerentry.repeated)
149                         self.list.append(self.frequencyEntry)
150                         if (config.timerentry.repeated.value == 0): # daily
151                                 pass
152                         if (config.timerentry.repeated.value == 2): # Mon-Fri
153                                 pass
154                         if (config.timerentry.repeated.value == 1): # weekly
155                                 self.list.append(getConfigListEntry(_("Weekday"), config.timerentry.weekday))
156
157                         if (config.timerentry.repeated.value == 3): # user defined
158                                 self.list.append(getConfigListEntry(_("Monday"), config.timerentry.day[0]))
159                                 self.list.append(getConfigListEntry(_("Tuesday"), config.timerentry.day[1]))
160                                 self.list.append(getConfigListEntry(_("Wednesday"), config.timerentry.day[2]))
161                                 self.list.append(getConfigListEntry(_("Thursday"), config.timerentry.day[3]))
162                                 self.list.append(getConfigListEntry(_("Friday"), config.timerentry.day[4]))
163                                 self.list.append(getConfigListEntry(_("Saturday"), config.timerentry.day[5]))
164                                 self.list.append(getConfigListEntry(_("Sunday"), config.timerentry.day[6]))
165
166                         #self.list.append(getConfigListEntry("StartDate", config.timerentry.startdate))
167 #               self.list.append(getConfigListEntry("Weekday", config.timerentry.weekday))
168
169                 if (config.timerentry.type.value == 0): # once
170                         self.list.append(getConfigListEntry(_("Start"), config.timerentry.startdate))
171                         self.list.append(getConfigListEntry(" ", config.timerentry.starttime))
172                 else:
173                         self.list.append(getConfigListEntry(_("StartTime"), config.timerentry.starttime))
174                 if (config.timerentry.type.value == 0): # once
175                         self.list.append(getConfigListEntry(_("End"), config.timerentry.enddate))
176                         self.list.append(getConfigListEntry(" ", config.timerentry.endtime))
177                 else:
178                         self.list.append(getConfigListEntry(_("EndTime"), config.timerentry.endtime))
179
180                 self.channelEntry = getConfigListEntry(_("Channel"), config.timerentry.service)
181                 self.list.append(self.channelEntry)
182
183                 self[widget].list = self.list
184                 self[widget].l.setList(self.list)
185
186         def newConfig(self):
187                 print "newConfig", self["config"].getCurrent()
188                 if self["config"].getCurrent() == self.timerTypeEntry:
189                         self.createSetup("config")
190                 if self["config"].getCurrent() == self.frequencyEntry:
191                         self.createSetup("config")
192
193         def keyLeft(self):
194                 if self["config"].getCurrent() == self.channelEntry:
195                         self.keySelect()
196                 else:
197                         self["config"].handleKey(config.key["prevElement"])
198                         self.newConfig()
199
200         def keyDelete(self):
201                 self["config"].handleKey(config.key["delete"])
202                         
203         def keyRightCallback(self, configPath):
204                 currentConfigPath = self["config"].getCurrent()[1].parent.getConfigPath()
205                 # check if we are still on the same config entry
206                 if (currentConfigPath == configPath):
207                         self.keyRight()
208
209         def keyRight(self):
210                 if self["config"].getCurrent() == self.channelEntry:
211                         self.keySelect()
212                 else:
213                         self["config"].handleKey(config.key["nextElement"])
214                         self.newConfig()
215                 
216         def keySelect(self):
217                 if self["config"].getCurrent() == self.channelEntry:
218                         self.session.openWithCallback(self.finishedChannelSelection, ChannelSelection.SimpleChannelSelection, _("Select channel to record from"))
219                 else:
220                         self.keyGo()
221
222         def finishedChannelSelection(self, args):
223                 oldref = self.timer.service_ref
224                 try:
225                         self.timer.service_ref = ServiceReference(args)
226                         config.timerentry.service.vals = (str(self.timer.service_ref.getServiceName()),)
227                         self["config"].invalidate(config.timerentry.service)
228                 except:
229                         print "you pressed cancel"
230                         self.timer.service_ref = oldref
231
232         def keyNumberGlobal(self, number):
233                 print "You pressed number " + str(number)
234                 if (self["config"].getCurrent()[1].parent.enabled == True):
235                         self["config"].handleKey(config.key[str(number)])
236
237         def getTimestamp(self, date, mytime):
238                 d = time.localtime(date)
239                 dt = datetime.datetime(d.tm_year, d.tm_mon, d.tm_mday, mytime[0], mytime[1])
240                 return int(mktime(dt.timetuple()))
241
242         def keyGo(self):
243                 self.timer.name = config.timerentry.name.value
244                 self.timer.description = config.timerentry.description.value
245                 self.timer.resetRepeated()
246                 
247                 if (config.timerentry.type.value == 0): # once
248                         self.timer.begin = self.getTimestamp(config.timerentry.startdate.value, config.timerentry.starttime.value)
249                         self.timer.end = self.getTimestamp(config.timerentry.enddate.value, config.timerentry.endtime.value)
250                 if (config.timerentry.type.value == 1): # repeated
251                         if (config.timerentry.repeated.value == 0): # daily
252                                 for x in range(0,7):
253                                         self.timer.setRepeated(x)
254
255                         if (config.timerentry.repeated.value == 1): # weekly
256                                 self.timer.setRepeated(config.timerentry.weekday.value)
257                                 
258                         if (config.timerentry.repeated.value == 2): # Mon-Fri
259                                 for x in range(0,5):
260                                         self.timer.setRepeated(x)
261                                 
262                         if (config.timerentry.repeated.value == 3): # user defined
263                                 for x in range(0,7):
264                                         if (config.timerentry.day[x].value == 0): self.timer.setRepeated(x)
265
266                         self.timer.begin = self.getTimestamp(time.time(), config.timerentry.starttime.value)
267                         self.timer.end = self.getTimestamp(time.time(), config.timerentry.endtime.value)
268
269                 if self.timer.eit is not None:
270                         event = eEPGCache.getInstance().lookupEventId(self.timer.service_ref.ref, self.timer.eit)
271                         if event is not None:
272                                 if event.getNumOfLinkageServices() > 0:
273                                         self.session.openWithCallback(self.subserviceSelected, SubserviceSelection, event, self.timer.service_ref.ref)
274                                         return
275                 self.close((True, self.timer))
276
277         def subserviceSelected(self, service):
278                 if not service is None:
279                         self.timer.service_ref = ServiceReference(service)
280                 self.close((True, self.timer))
281
282         def keyCancel(self):
283                 self.close((False,))
284                 
285 class TimerLog(Screen):
286         def __init__(self, session, timer):
287                 Screen.__init__(self, session)
288                 self.timer = timer;
289                 self.log_entries = self.timer.log_entries[:]
290                 
291                 self.fillLogList()
292                 
293                 self["loglist"] = MenuList(self.list)
294                 self["logentry"] = Label()
295                 
296                 self["key_red"] = Button(_("Delete entry"))
297                 self["key_green"] = Button()
298                 self["key_yellow"] = Button("")
299                 self["key_blue"] = Button(_("Clear log"))
300                 
301                 self.onShown.append(self.updateText)
302
303                 self["actions"] = NumberActionMap(["OkCancelActions", "DirectionActions", "ColorActions"],
304                 {
305                         "ok": self.keyClose,
306                         "cancel": self.keyClose,
307                         "up": self.up,
308                         "down": self.down,
309                         "left": self.left,
310                         "right": self.right,
311                         "red": self.deleteEntry,
312                         "blue": self.clearLog
313                 }, -1)
314
315         def deleteEntry(self):
316                 self.log_entries.remove(self["loglist"].getCurrent()[1])
317                 self.fillLogList()
318                 self["loglist"].l.setList(self.list)
319                 self.updateText()
320
321         def fillLogList(self):
322                 self.list = [ ]
323                 for x in self.log_entries:
324                         self.list.append((str(strftime("%Y-%m-%d %H-%M", localtime(x[0])) + " - " + x[2]), x))
325         
326         def clearLog(self):             
327                 self.log_entries = []
328                 self.fillLogList()
329                 self["loglist"].l.setList(self.list)
330                 self.updateText()
331                 
332         def keyClose(self):
333                 if self.timer.log_entries != self.log_entries:
334                         self.timer.log_entries = self.log_entries
335                         self.close((True, self.timer))
336                 else:
337                         self.close((False,))
338                 
339         def up(self):
340                 self["loglist"].instance.moveSelection(self["loglist"].instance.moveUp)
341                 self.updateText()
342                 
343         def down(self):
344                 self["loglist"].instance.moveSelection(self["loglist"].instance.moveDown)
345                 self.updateText()
346
347         def left(self):
348                 self["loglist"].instance.moveSelection(self["loglist"].instance.pageUp)
349                 self.updateText()
350                 
351         def right(self):
352                 self["loglist"].instance.moveSelection(self["loglist"].instance.pageDown)
353                 self.updateText()
354
355         def updateText(self):
356                 if len(self.list) > 0:
357                         self["logentry"].setText(str(self["loglist"].getCurrent()[1][2]))
358                 else:
359                         self["logentry"].setText("")