df912014150859d8d174b97de0f67dabd183134a
[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.NimManager import nimmanager
8 from Components.Label import Label
9 from Components.Pixmap import Pixmap
10 from Screens.SubserviceSelection import SubserviceSelection
11 from enigma import eEPGCache
12 import time
13 import datetime
14
15 class TimerEntry(Screen):
16         def __init__(self, session, timer):
17                 Screen.__init__(self, session)
18                 self.timer = timer;
19                 
20                 self["oktext"] = Label(_("OK"))
21                 self["canceltext"] = Label(_("Cancel"))
22                 self["ok"] = Pixmap()
23                 self["cancel"] = Pixmap()
24
25                 self.createConfig()
26
27                 self["actions"] = NumberActionMap(["SetupActions", "TextEntryActions"],
28                 {
29                         "ok": self.keySelect,
30                         "save": self.keyGo,
31                         "cancel": self.keyCancel,
32                         "left": self.keyLeft,
33                         "right": self.keyRight,
34                         "delete": self.keyDelete,
35                         "1": self.keyNumberGlobal,
36                         "2": self.keyNumberGlobal,
37                         "3": self.keyNumberGlobal,
38                         "4": self.keyNumberGlobal,
39                         "5": self.keyNumberGlobal,
40                         "6": self.keyNumberGlobal,
41                         "7": self.keyNumberGlobal,
42                         "8": self.keyNumberGlobal,
43                         "9": self.keyNumberGlobal,
44                         "0": self.keyNumberGlobal
45                 }, -1)
46
47                 self.list = []
48                 self["config"] = ConfigList(self.list)
49                 self.createSetup()
50
51         def createConfig(self):
52                         config.timerentry = ConfigSubsection()
53                         
54                         # calculate default values
55                         day = []
56                         weekday = 0
57                         for x in range(0,7):
58                                 day.append(1)
59                         if (self.timer.repeated != 0): # repeated
60                                 type = 1 # repeated
61                                 if (self.timer.repeated == 31): # Mon-Fri
62                                         repeated = 2 # Mon - Fri
63                                 elif (self.timer.repeated == 127): # daily
64                                         repeated = 0 # daily
65                                 else:
66                                         flags = self.timer.repeated
67                                         repeated = 3 # user defined
68                                         count = 0
69                                         for x in range(0, 7):
70                                                 if (flags == 1): # weekly
71                                                         print "Set to weekday " + str(x)
72                                                         weekday = x
73                                                 if (flags & 1 == 1): # set user defined flags
74                                                         day[x] = 0
75                                                         count += 1
76                                                 else:
77                                                         day[x] = 1
78                                                 flags = flags >> 1
79                                         if (count == 1):
80                                                 repeated = 1 # weekly
81                         else: # once
82                                 type = 0
83                                 repeated = 0
84                                 weekday = (int(strftime("%w", time.localtime(self.timer.begin))) - 1) % 7
85                                 day[weekday] = 0
86                         
87                         config.timerentry.type = configElement_nonSave("config.timerentry.type", configSelection, type, (_("once"), _("repeated")))
88                         config.timerentry.name = configElement_nonSave("config.timerentry.name", configText, self.timer.name, (configText.extendableSize, self.keyRightCallback))
89                         config.timerentry.description = configElement_nonSave("config.timerentry.description", configText, self.timer.description, (configText.extendableSize, self.keyRightCallback))
90
91                         config.timerentry.repeated = configElement_nonSave("config.timerentry.repeated", configSelection, repeated, (_("daily"), _("weekly"), _("Mon-Fri"), _("user defined")))
92
93                         config.timerentry.startdate = configElement_nonSave("config.timerentry.startdate", configDateTime, self.timer.begin, (_("%d.%B %Y"), 86400))
94                         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"))
95
96                         config.timerentry.enddate = configElement_nonSave("config.timerentry.enddate", configDateTime, self.timer.end, (_("%d.%B %Y"), 86400))
97                         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"))
98
99                         config.timerentry.weekday = configElement_nonSave("config.timerentry.weekday", configSelection, weekday, (_("Monday"), _("Tuesday"), _("Wednesday"), _("Thursday"), _("Friday"), _("Saturday"), _("Sunday")))
100
101                         config.timerentry.day = []
102                         for x in range(0,7):
103                                 config.timerentry.day.append(configElement_nonSave("config.timerentry.day[" + str(x) + "]", configSelection, day[x], (_("yes"), _("no"))))
104
105
106                         # FIXME some service-chooser needed here
107                         servicename = "N/A"
108                         try: # no current service available?
109                                 servicename = str(self.timer.service_ref.getServiceName())
110                         except:
111                                 pass
112                         config.timerentry.service = configElement_nonSave("config.timerentry.service", configSelection, 0, ((servicename),))
113                         
114                         config.timerentry.startdate.addNotifier(self.checkDate)
115                         config.timerentry.enddate.addNotifier(self.checkDate)
116
117         def checkDate(self, configElement):
118                 if (configElement.getConfigPath() == "config.timerentry.startdate"):
119                         if (config.timerentry.enddate.value < config.timerentry.startdate.value):
120                                 config.timerentry.enddate.value = config.timerentry.startdate.value
121                                 config.timerentry.enddate.change()
122                                 try:
123                                         self["config"].invalidate(config.timerentry.enddate)
124                                 except:
125                                         pass
126                 if (configElement.getConfigPath() == "config.timerentry.enddate"):
127                         if (config.timerentry.enddate.value < config.timerentry.startdate.value):
128                                 config.timerentry.startdate.value = config.timerentry.enddate.value
129                                 config.timerentry.startdate.change()
130                                 try:
131                                         self["config"].invalidate(config.timerentry.startdate)
132                                 except:
133                                         pass
134
135         def createSetup(self):
136                 self.list = []
137                 self.list.append(getConfigListEntry(_("Name"), config.timerentry.name))
138                 self.list.append(getConfigListEntry(_("Description"), config.timerentry.description))
139                 self.timerTypeEntry = getConfigListEntry(_("Timer Type"), config.timerentry.type)
140                 self.list.append(self.timerTypeEntry)
141
142                 if (config.timerentry.type.value == 0): # once
143                         self.frequencyEntry = None
144                 else: # repeated
145                         self.frequencyEntry = getConfigListEntry(_("Frequency"), config.timerentry.repeated)
146                         self.list.append(self.frequencyEntry)
147                         if (config.timerentry.repeated.value == 0): # daily
148                                 pass
149                         if (config.timerentry.repeated.value == 2): # Mon-Fri
150                                 pass
151                         if (config.timerentry.repeated.value == 1): # weekly
152                                 self.list.append(getConfigListEntry(_("Weekday"), config.timerentry.weekday))
153
154                         if (config.timerentry.repeated.value == 3): # user defined
155                                 self.list.append(getConfigListEntry(_("Monday"), config.timerentry.day[0]))
156                                 self.list.append(getConfigListEntry(_("Tuesday"), config.timerentry.day[1]))
157                                 self.list.append(getConfigListEntry(_("Wednesday"), config.timerentry.day[2]))
158                                 self.list.append(getConfigListEntry(_("Thursday"), config.timerentry.day[3]))
159                                 self.list.append(getConfigListEntry(_("Friday"), config.timerentry.day[4]))
160                                 self.list.append(getConfigListEntry(_("Saturday"), config.timerentry.day[5]))
161                                 self.list.append(getConfigListEntry(_("Sunday"), config.timerentry.day[6]))
162
163                         #self.list.append(getConfigListEntry("StartDate", config.timerentry.startdate))
164 #               self.list.append(getConfigListEntry("Weekday", config.timerentry.weekday))
165
166                 if (config.timerentry.type.value == 0): # once
167                         self.list.append(getConfigListEntry(_("Start"), config.timerentry.startdate))
168                         self.list.append(getConfigListEntry(" ", config.timerentry.starttime))
169                 else:
170                         self.list.append(getConfigListEntry(_("StartTime"), config.timerentry.starttime))
171                 if (config.timerentry.type.value == 0): # once
172                         self.list.append(getConfigListEntry(_("End"), config.timerentry.enddate))
173                         self.list.append(getConfigListEntry(" ", config.timerentry.endtime))
174                 else:
175                         self.list.append(getConfigListEntry(_("EndTime"), config.timerentry.endtime))
176
177                 self.channelEntry = getConfigListEntry(_("Channel"), config.timerentry.service)
178                 self.list.append(self.channelEntry)
179
180                 self["config"].list = self.list
181                 self["config"].l.setList(self.list)
182
183         def newConfig(self):
184                 print self["config"].getCurrent()
185                 if self["config"].getCurrent() == self.timerTypeEntry:
186                         self.createSetup()
187                 if self["config"].getCurrent() == self.frequencyEntry:
188                         self.createSetup()
189
190         def keyLeft(self):
191                 if self["config"].getCurrent() == self.channelEntry:
192                         self.keySelect()
193                 else:
194                         self["config"].handleKey(config.key["prevElement"])
195                         self.newConfig()
196
197         def keyDelete(self):
198                 self["config"].handleKey(config.key["delete"])
199                         
200         def keyRightCallback(self, configPath):
201                 currentConfigPath = self["config"].getCurrent()[1].parent.getConfigPath()
202                 # check if we are still on the same config entry
203                 if (currentConfigPath == configPath):
204                         self.keyRight()
205
206         def keyRight(self):
207                 if self["config"].getCurrent() == self.channelEntry:
208                         self.keySelect()
209                 else:
210                         self["config"].handleKey(config.key["nextElement"])
211                         self.newConfig()
212                 
213         def keySelect(self):
214                 if self["config"].getCurrent() == self.channelEntry:
215                         self.session.openWithCallback(self.finishedChannelSelection, ChannelSelection.SimpleChannelSelection, _("Select channel to record from"))
216                 else:
217                         self.keyGo()
218
219         def finishedChannelSelection(self, args):
220                 oldref = self.timer.service_ref
221                 try:
222                         self.timer.service_ref = ServiceReference(args)
223                         config.timerentry.service.vals = (str(self.timer.service_ref.getServiceName()),)
224                         self["config"].invalidate(config.timerentry.service)
225                 except:
226                         print "you pressed cancel"
227                         self.timer.service_ref = oldref
228
229         def keyNumberGlobal(self, number):
230                 print "You pressed number " + str(number)
231                 if (self["config"].getCurrent()[1].parent.enabled == True):
232                         self["config"].handleKey(config.key[str(number)])
233
234         def getTimestamp(self, date, mytime):
235                 d = time.localtime(date)
236                 dt = datetime.datetime(d.tm_year, d.tm_mon, d.tm_mday, mytime[0], mytime[1])
237                 print dt
238                 return int(mktime(dt.timetuple()))
239
240         def keyGo(self):
241                 self.timer.name = config.timerentry.name.value
242                 self.timer.description = config.timerentry.description.value
243                 self.timer.resetRepeated()
244                 
245                 if (config.timerentry.type.value == 0): # once
246                         self.timer.begin = self.getTimestamp(config.timerentry.startdate.value, config.timerentry.starttime.value)
247                         self.timer.end = self.getTimestamp(config.timerentry.enddate.value, config.timerentry.endtime.value)
248                 if (config.timerentry.type.value == 1): # repeated
249                         if (config.timerentry.repeated.value == 0): # daily
250                                 for x in range(0,7):
251                                         self.timer.setRepeated(x)
252
253                         if (config.timerentry.repeated.value == 1): # weekly
254                                 self.timer.setRepeated(config.timerentry.weekday.value)
255                                 
256                         if (config.timerentry.repeated.value == 2): # Mon-Fri
257                                 for x in range(0,5):
258                                         self.timer.setRepeated(x)
259                                 
260                         if (config.timerentry.repeated.value == 3): # user defined
261                                 for x in range(0,7):
262                                         if (config.timerentry.day[x].value == 0): self.timer.setRepeated(x)
263
264                         self.timer.begin = self.getTimestamp(time.time(), config.timerentry.starttime.value)
265                         self.timer.end = self.getTimestamp(time.time(), config.timerentry.endtime.value)
266
267                 if self.timer.eit is not None:
268                         event = eEPGCache.getInstance().lookupEventId(self.timer.service_ref.ref, self.timer.eit)
269                         if event is not None:
270                                 if event.getNumOfLinkageServices() > 0:
271                                         self.session.openWithCallback(self.subserviceSelected, SubserviceSelection, event, self.timer.service_ref.ref)
272                                         return
273                 self.close((True, self.timer))
274
275         def subserviceSelected(self, service):
276                 if not service is None:
277                         self.timer.service_ref = ServiceReference(service)
278                 self.close((True, self.timer))
279
280         def keyCancel(self):
281                 self.close((False,))