- fixed left/right in language selection (updates language on the fly)
[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 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()
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):
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["config"].list = self.list
184                 self["config"].l.setList(self.list)
185
186         def newConfig(self):
187                 print self["config"].getCurrent()
188                 if self["config"].getCurrent() == self.timerTypeEntry:
189                         self.createSetup()
190                 if self["config"].getCurrent() == self.frequencyEntry:
191                         self.createSetup()
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                 print dt
241                 return int(mktime(dt.timetuple()))
242
243         def keyGo(self):
244                 self.timer.name = config.timerentry.name.value
245                 self.timer.description = config.timerentry.description.value
246                 self.timer.resetRepeated()
247                 
248                 if (config.timerentry.type.value == 0): # once
249                         self.timer.begin = self.getTimestamp(config.timerentry.startdate.value, config.timerentry.starttime.value)
250                         self.timer.end = self.getTimestamp(config.timerentry.enddate.value, config.timerentry.endtime.value)
251                 if (config.timerentry.type.value == 1): # repeated
252                         if (config.timerentry.repeated.value == 0): # daily
253                                 for x in range(0,7):
254                                         self.timer.setRepeated(x)
255
256                         if (config.timerentry.repeated.value == 1): # weekly
257                                 self.timer.setRepeated(config.timerentry.weekday.value)
258                                 
259                         if (config.timerentry.repeated.value == 2): # Mon-Fri
260                                 for x in range(0,5):
261                                         self.timer.setRepeated(x)
262                                 
263                         if (config.timerentry.repeated.value == 3): # user defined
264                                 for x in range(0,7):
265                                         if (config.timerentry.day[x].value == 0): self.timer.setRepeated(x)
266
267                         self.timer.begin = self.getTimestamp(time.time(), config.timerentry.starttime.value)
268                         self.timer.end = self.getTimestamp(time.time(), config.timerentry.endtime.value)
269
270                 if self.timer.eit is not None:
271                         event = eEPGCache.getInstance().lookupEventId(self.timer.service_ref.ref, self.timer.eit)
272                         if event is not None:
273                                 if event.getNumOfLinkageServices() > 0:
274                                         self.session.openWithCallback(self.subserviceSelected, SubserviceSelection, event, self.timer.service_ref.ref)
275                                         return
276                 self.close((True, self.timer))
277
278         def subserviceSelected(self, service):
279                 if not service is None:
280                         self.timer.service_ref = ServiceReference(service)
281                 self.close((True, self.timer))
282
283         def keyCancel(self):
284                 self.close((False,))
285
286 class TimerLog(Screen):
287         def __init__(self, session, timer):
288                 Screen.__init__(self, session)
289                 self.timer = timer;
290                 self.log_entries = self.timer.log_entries[:]
291                 
292                 self.fillLogList()
293                 
294                 self["loglist"] = MenuList(self.list)
295                 self["logentry"] = Label()
296                 
297                 self["key_red"] = Button(_("Delete entry"))
298                 self["key_green"] = Button()
299                 self["key_yellow"] = Button("")
300                 self["key_blue"] = Button(_("Clear log"))
301                 
302                 self.onShown.append(self.updateText)
303
304                 self["actions"] = NumberActionMap(["OkCancelActions", "DirectionActions", "ColorActions"],
305                 {
306                         "ok": self.keyClose,
307                         "cancel": self.keyClose,
308                         "up": self.up,
309                         "down": self.down,
310                         "left": self.left,
311                         "right": self.right,
312                         "red": self.deleteEntry,
313                         "blue": self.clearLog
314                 }, -1)
315
316         def deleteEntry(self):
317                 self.log_entries.remove(self["loglist"].getCurrent()[1])
318                 self.fillLogList()
319                 self["loglist"].l.setList(self.list)
320                 self.updateText()
321
322         def fillLogList(self):
323                 self.list = [ ]
324                 for x in self.log_entries:
325                         self.list.append((str(strftime("%Y-%m-%d %H-%M", localtime(x[0])) + " - " + x[2]), x))
326         
327         def clearLog(self):             
328                 self.log_entries = []
329                 self.fillLogList()
330                 self["loglist"].l.setList(self.list)
331                 self.updateText()
332                 
333         def keyClose(self):
334                 if self.timer.log_entries != self.log_entries:
335                         self.timer.log_entries = self.log_entries
336                         self.close((True, self.timer))
337                 else:
338                         self.close((False,))
339                 
340         def up(self):
341                 self["loglist"].instance.moveSelection(self["loglist"].instance.moveUp)
342                 self.updateText()
343                 
344         def down(self):
345                 self["loglist"].instance.moveSelection(self["loglist"].instance.moveDown)
346                 self.updateText()
347
348         def left(self):
349                 self["loglist"].instance.moveSelection(self["loglist"].instance.pageUp)
350                 self.updateText()
351                 
352         def right(self):
353                 self["loglist"].instance.moveSelection(self["loglist"].instance.pageDown)
354                 self.updateText()
355
356         def updateText(self):
357                 if len(self.list) > 0:
358                         self["logentry"].setText(str(self["loglist"].getCurrent()[1][2]))
359                 else:
360                         self["logentry"].setText("")