some TimerEntry-fixes
[enigma2.git] / lib / python / Screens / TimerEntry.py
1 from Screen import Screen
2 from Components.config import *
3 from Components.ActionMap import NumberActionMap
4 from Components.ConfigList import ConfigList
5 from Components.NimManager import nimmanager
6 from Components.Label import Label
7 from time import *
8 from datetime import *
9 from math import log
10
11 class TimerEntry(Screen):
12         def __init__(self, session, timer):
13                 Screen.__init__(self, session)
14                 self.timer = timer;
15
16                 self.createConfig()
17
18                 self["actions"] = NumberActionMap(["SetupActions"],
19                 {
20                         "ok": self.keyGo,
21                         "cancel": self.keyCancel,
22                         "left": self.keyLeft,
23                         "right": self.keyRight,
24                         "1": self.keyNumberGlobal,
25                         "2": self.keyNumberGlobal,
26                         "3": self.keyNumberGlobal,
27                         "4": self.keyNumberGlobal,
28                         "5": self.keyNumberGlobal,
29                         "6": self.keyNumberGlobal,
30                         "7": self.keyNumberGlobal,
31                         "8": self.keyNumberGlobal,
32                         "9": self.keyNumberGlobal,
33                         "0": self.keyNumberGlobal
34                 }, -1)
35
36                 self.list = []
37                 self["config"] = ConfigList(self.list)
38                 self.createSetup()
39
40         def createConfig(self):
41                         config.timerentry = ConfigSubsection()
42                         
43                         # calculate default values
44                         day = []
45                         weekday = 0
46                         for x in range(0,7):
47                                 day.append(1)
48                         if (self.timer.repeated != 0): # repeated
49                                 type = 1 # repeated
50                                 if (self.timer.repeated == 31): # Mon-Fri
51                                         repeated = 2 # Mon - Fri
52                                 elif (self.timer.repeated == 127): # daily
53                                         repeated = 0 # daily
54                                 else:
55                                         flags = self.timer.repeated
56                                         repeated = 3 # user-defined
57                                         count = 0
58                                         for x in range(0, 6):
59                                                 if (flags == 1): # weekly
60                                                         print "Set to weekday " + str(x)
61                                                         weekday = x
62                                                 if (flags & 1 == 1): # set user-defined flags
63                                                         day[x] = 0
64                                                         count += 1
65                                                 else:
66                                                         day[x] = 1
67                                                 flags = flags >> 1
68                                         if (count == 1):
69                                                 repeated = 1 # weekly
70                         else: # once
71                                 type = 0
72                                 repeated = 0
73                         
74                         config.timerentry.type = configElement_nonSave("config.timerentry.type", configSelection, type, ("once", "repeated"))
75                         config.timerentry.description = configElement_nonSave("config.timerentry.description", configText, self.timer.description, (configText.extendableSize, self.keyRightCallback))
76
77                         config.timerentry.repeated = configElement_nonSave("config.timerentry.repeated", configSelection, repeated, ("daily", "weekly", "Mon-Fri", "user-defined"))
78
79                         config.timerentry.startdate = configElement_nonSave("config.timerentry.startdate", configDateTime, self.timer.begin, ("%d.%B %Y", 86400))
80                         config.timerentry.starttime = configElement_nonSave("config.timerentry.starttime", configSequence, [int(strftime("%H", localtime(self.timer.begin))), int(strftime("%M", localtime(self.timer.begin)))], configsequencearg.get("CLOCK"))
81
82                         config.timerentry.enddate = configElement_nonSave("config.timerentry.enddate", configDateTime, self.timer.end, ("%d.%B %Y", 86400))
83                         config.timerentry.endtime = configElement_nonSave("config.timerentry.endtime", configSequence, [int(strftime("%H", localtime(self.timer.end))), int(strftime("%M", localtime(self.timer.end)))], configsequencearg.get("CLOCK"))
84
85                         config.timerentry.weekday = configElement_nonSave("config.timerentry.weekday", configSelection, weekday, ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"))
86
87                         config.timerentry.day = []
88                         for x in range(0,7):
89                                 config.timerentry.day.append(configElement_nonSave("config.timerentry.day[" + str(x) + "]", configSelection, day[x], ("yes", "no")))
90
91
92                         # FIXME some service-chooser needed here
93                         config.timerentry.service = configElement_nonSave("config.timerentry.service", configSelection, 0, ((str(self.timer.service_ref.getServiceName())),))
94                         
95                         config.timerentry.startdate.addNotifier(self.checkDate)
96                         config.timerentry.enddate.addNotifier(self.checkDate)
97
98         def checkDate(self, configElement):
99                 if (configElement.getConfigPath() == "config.timerentry.startdate"):
100                         if (config.timerentry.enddate.value < config.timerentry.startdate.value):
101                                 config.timerentry.enddate.value = config.timerentry.startdate.value
102                                 config.timerentry.enddate.change()
103                                 self["config"].invalidate(config.timerentry.enddate)
104                 if (configElement.getConfigPath() == "config.timerentry.enddate"):
105                         if (config.timerentry.enddate.value < config.timerentry.startdate.value):
106                                 config.timerentry.startdate.value = config.timerentry.enddate.value
107                                 config.timerentry.startdate.change()
108                                 self["config"].invalidate(config.timerentry.startdate)
109
110         def createSetup(self):
111                 self.list = []
112                 self.list.append(getConfigListEntry("Description", config.timerentry.description))
113                 self.list.append(getConfigListEntry("TimerType", config.timerentry.type))
114
115                 if (config.timerentry.type.value == 0): # once
116                         pass
117                 else: # repeated
118                         self.list.append(getConfigListEntry("Frequency", config.timerentry.repeated))
119                         if (config.timerentry.repeated.value == 0): # daily
120                                 pass
121                         if (config.timerentry.repeated.value == 2): # Mon-Fri
122                                 pass
123                         if (config.timerentry.repeated.value == 1): # weekly
124                                 self.list.append(getConfigListEntry("Weekday", config.timerentry.weekday))
125
126                         if (config.timerentry.repeated.value == 3): # user-defined
127                                 self.list.append(getConfigListEntry("Monday", config.timerentry.day[0]))
128                                 self.list.append(getConfigListEntry("Tuesday", config.timerentry.day[1]))
129                                 self.list.append(getConfigListEntry("Wednesday", config.timerentry.day[2]))
130                                 self.list.append(getConfigListEntry("Thursday", config.timerentry.day[3]))
131                                 self.list.append(getConfigListEntry("Friday", config.timerentry.day[4]))
132                                 self.list.append(getConfigListEntry("Saturday", config.timerentry.day[5]))
133                                 self.list.append(getConfigListEntry("Sunday", config.timerentry.day[6]))
134
135                         #self.list.append(getConfigListEntry("StartDate", config.timerentry.startdate))
136 #               self.list.append(getConfigListEntry("Weekday", config.timerentry.weekday))
137
138                 if (config.timerentry.type.value == 0): # once
139                         self.list.append(getConfigListEntry("StartDate", config.timerentry.startdate))
140                 self.list.append(getConfigListEntry("StartTime", config.timerentry.starttime))
141                 if (config.timerentry.type.value == 0): # once
142                         self.list.append(getConfigListEntry("EndDate", config.timerentry.enddate))
143                 self.list.append(getConfigListEntry("EndTime", config.timerentry.endtime))
144
145                 self.list.append(getConfigListEntry("Channel", config.timerentry.service))
146
147                 self["config"].list = self.list
148                 self["config"].l.setList(self.list)
149
150         def newConfig(self):
151                 print self["config"].getCurrent()
152                 if self["config"].getCurrent()[0] == "TimerType":
153                         self.createSetup()
154                 if self["config"].getCurrent()[0] == "Frequency":
155                         self.createSetup()
156
157         def keyLeft(self):
158                 self["config"].handleKey(config.key["prevElement"])
159                 self.newConfig()
160
161         def keyRightCallback(self, configPath):
162                 currentConfigPath = self["config"].getCurrent()[1].parent.getConfigPath()
163                 # check if we are still on the same config entry
164                 if (currentConfigPath == configPath):
165                         self.keyRight()
166
167         def keyRight(self):
168                 self["config"].handleKey(config.key["nextElement"])
169                 self.newConfig()
170
171         def keyNumberGlobal(self, number):
172                 print "You pressed number " + str(number)
173                 if (self["config"].getCurrent()[1].parent.enabled == True):
174                         self["config"].handleKey(config.key[str(number)])
175
176         def getTimestamp(self, date, time):
177                 d = localtime(date) # for gettin indexes 0(year), 1(month) and 2(day)
178                 dt = datetime(d.tm_year, d.tm_mon, d.tm_mday, time[0], time[1])
179                 print dt
180                 return int(mktime(dt.timetuple()))
181
182         def keyGo(self):
183                 self.timer.resetRepeated()
184                 
185                 if (config.timerentry.type.value == 0): # once
186                         self.timer.begin = self.getTimestamp(config.timerentry.startdate.value, config.timerentry.starttime.value)
187                         self.timer.end = self.getTimestamp(config.timerentry.enddate.value, config.timerentry.endtime.value)
188                 if (config.timerentry.type.value == 1): # repeated
189                         if (config.timerentry.repeated.value == 0): # daily
190                                 self.timer.setRepeated(0) # Mon
191                                 self.timer.setRepeated(1) # Tue
192                                 self.timer.setRepeated(2) # Wed
193                                 self.timer.setRepeated(3) # Thu
194                                 self.timer.setRepeated(4) # Fri
195                                 self.timer.setRepeated(5) # Sat
196                                 self.timer.setRepeated(6) # Sun
197
198                         if (config.timerentry.repeated.value == 1): # weekly
199                                 self.timer.setRepeated(config.timerentry.weekday.value)
200                                 
201                         if (config.timerentry.repeated.value == 2): # Mon-Fri
202                                 self.timer.setRepeated(0) # Mon
203                                 self.timer.setRepeated(1) # Tue
204                                 self.timer.setRepeated(2) # Wed
205                                 self.timer.setRepeated(3) # Thu
206                                 self.timer.setRepeated(4) # Fri
207                                 
208                         if (config.timerentry.repeated.value == 3): # user-defined
209                                 for x in range(0,7):
210                                         if (config.timerentry.day[x].value == 0): self.timer.setRepeated(x)
211
212                                 
213
214                 self.close((True, self.timer))
215
216         def keyCancel(self):
217                 self.close((False,))