finish the TimerEntry editor... should work now for editing repeated timers
[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                         for x in range(0,7):
46                                 day.append(1)
47                         if (self.timer.repeated != 0): # repeated
48                                 type = 1 # repeated
49                                 if (self.timer.repeated == 31): # Mon-Fri
50                                         repeated = 2 # Mon - Fri
51                                 elif (self.timer.repeated == 127): # daily
52                                         repeated = 0 # daily
53                                 else:
54                                         flags = self.timer.repeated
55                                         repeated = 3 # user-defined
56                                         count = 0
57                                         for x in range(0, 6):
58                                                 if (flags == 1): # weekly
59                                                         weekday = x
60                                                 if (flags & 1 == 1): # set user-defined flags
61                                                         day[x] = 0
62                                                         count += 1
63                                                 else:
64                                                         day[x] = 1
65                                                 flags = flags >> 1
66                                         if (count == 1):
67                                                 repeated = 1 # weekly
68                         else: # once
69                                 type = 0
70                                 repeated = 0
71                         
72                         config.timerentry.type = configElement_nonSave("config.timerentry.type", configSelection, type, ("once", "repeated"))
73                         config.timerentry.description = configElement_nonSave("config.timerentry.description", configText, self.timer.description, (configText.extendableSize, self.keyRightCallback))
74
75                         config.timerentry.repeated = configElement_nonSave("config.timerentry.repeated", configSelection, repeated, ("daily", "weekly", "Mon-Fri", "user-defined"))
76
77                         config.timerentry.startdate = configElement_nonSave("config.timerentry.startdate", configDateTime, self.timer.begin, ("%d.%B %Y", 86400))
78                         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"))
79
80                         config.timerentry.enddate = configElement_nonSave("config.timerentry.enddate", configDateTime, self.timer.end, ("%d.%B %Y", 86400))
81                         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"))
82
83                         config.timerentry.weekday = configElement_nonSave("config.timerentry.weekday", configSelection, 0, ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"))
84
85                         config.timerentry.day = []
86                         for x in range(0,7):
87                                 config.timerentry.day.append(configElement_nonSave("config.timerentry.day[" + str(x) + "]", configSelection, day[x], ("yes", "no")))
88
89
90                         # FIXME some service-chooser needed here
91                         config.timerentry.service = configElement_nonSave("config.timerentry.service", configSelection, 0, ((str(self.timer.service_ref.getServiceName())),))
92                         
93                         config.timerentry.startdate.addNotifier(self.checkDate)
94                         config.timerentry.enddate.addNotifier(self.checkDate)
95
96         def checkDate(self, configElement):
97                 if (configElement.getConfigPath() == "config.timerentry.startdate"):
98                         if (config.timerentry.enddate.value < config.timerentry.startdate.value):
99                                 config.timerentry.enddate.value = config.timerentry.startdate.value
100                                 config.timerentry.enddate.change()
101                                 self["config"].invalidate(config.timerentry.enddate)
102                 if (configElement.getConfigPath() == "config.timerentry.enddate"):
103                         if (config.timerentry.enddate.value < config.timerentry.startdate.value):
104                                 config.timerentry.startdate.value = config.timerentry.enddate.value
105                                 config.timerentry.startdate.change()
106                                 self["config"].invalidate(config.timerentry.startdate)
107
108         def createSetup(self):
109                 self.list = []
110                 self.list.append(getConfigListEntry("Description", config.timerentry.description))
111                 self.list.append(getConfigListEntry("TimerType", config.timerentry.type))
112
113                 if (config.timerentry.type.value == 0): # once
114                         pass
115                 else: # repeated
116                         self.list.append(getConfigListEntry("Frequency", config.timerentry.repeated))
117                         if (config.timerentry.repeated.value == 0): # daily
118                                 pass
119                         if (config.timerentry.repeated.value == 2): # Mon-Fri
120                                 pass
121                         if (config.timerentry.repeated.value == 1): # weekly
122                                 self.list.append(getConfigListEntry("Weekday", config.timerentry.weekday))
123
124                         if (config.timerentry.repeated.value == 3): # user-defined
125                                 self.list.append(getConfigListEntry("Monday", config.timerentry.day[0]))
126                                 self.list.append(getConfigListEntry("Tuesday", config.timerentry.day[1]))
127                                 self.list.append(getConfigListEntry("Wednesday", config.timerentry.day[2]))
128                                 self.list.append(getConfigListEntry("Thursday", config.timerentry.day[3]))
129                                 self.list.append(getConfigListEntry("Friday", config.timerentry.day[4]))
130                                 self.list.append(getConfigListEntry("Saturday", config.timerentry.day[5]))
131                                 self.list.append(getConfigListEntry("Sunday", config.timerentry.day[6]))
132
133                         #self.list.append(getConfigListEntry("StartDate", config.timerentry.startdate))
134 #               self.list.append(getConfigListEntry("Weekday", config.timerentry.weekday))
135
136                 if (config.timerentry.type.value == 0): # once
137                         self.list.append(getConfigListEntry("StartDate", config.timerentry.startdate))
138                 self.list.append(getConfigListEntry("StartTime", config.timerentry.starttime))
139                 if (config.timerentry.type.value == 0): # once
140                         self.list.append(getConfigListEntry("EndDate", config.timerentry.enddate))
141                 self.list.append(getConfigListEntry("EndTime", config.timerentry.endtime))
142
143                 self.list.append(getConfigListEntry("Channel", config.timerentry.service))
144
145                 self["config"].list = self.list
146                 self["config"].l.setList(self.list)
147
148         def newConfig(self):
149                 print self["config"].getCurrent()
150                 if self["config"].getCurrent()[0] == "TimerType":
151                         self.createSetup()
152                 if self["config"].getCurrent()[0] == "Frequency":
153                         self.createSetup()
154
155         def keyLeft(self):
156                 self["config"].handleKey(config.key["prevElement"])
157                 self.newConfig()
158
159         def keyRightCallback(self, configPath):
160                 currentConfigPath = self["config"].getCurrent()[1].parent.getConfigPath()
161                 # check if we are still on the same config entry
162                 if (currentConfigPath == configPath):
163                         self.keyRight()
164
165         def keyRight(self):
166                 self["config"].handleKey(config.key["nextElement"])
167                 self.newConfig()
168
169         def keyNumberGlobal(self, number):
170                 print "You pressed number " + str(number)
171                 if (self["config"].getCurrent()[1].parent.enabled == True):
172                         self["config"].handleKey(config.key[str(number)])
173
174         def getTimestamp(self, date, time):
175                 d = localtime(date) # for gettin indexes 0(year), 1(month) and 2(day)
176                 dt = datetime(d.tm_year, d.tm_mon, d.tm_mday, time[0], time[1])
177                 print dt
178                 return int(mktime(dt.timetuple()))
179
180         def keyGo(self):
181                 if (config.timerentry.type.value == 0): # once
182                         self.timer.begin = self.getTimestamp(config.timerentry.startdate.value, config.timerentry.starttime.value)
183                         self.timer.end = self.getTimestamp(config.timerentry.enddate.value, config.timerentry.endtime.value)
184                 if (config.timerentry.type.value == 1): # repeated
185                         if (config.timerentry.repeated.value == 0): # daily
186                                 self.timer.setRepeated(0) # Mon
187                                 self.timer.setRepeated(1) # Tue
188                                 self.timer.setRepeated(2) # Wed
189                                 self.timer.setRepeated(3) # Thu
190                                 self.timer.setRepeated(4) # Fri
191                                 self.timer.setRepeated(5) # Sat
192                                 self.timer.setRepeated(6) # Sun
193
194                         if (config.timerentry.repeated.value == 1): # weekly
195                                 self.timer.setRepeated(config.timerentry.weekday.value)
196                                 
197                         if (config.timerentry.repeated.value == 2): # Mon-Fri
198                                 self.timer.setRepeated(0) # Mon
199                                 self.timer.setRepeated(1) # Tue
200                                 self.timer.setRepeated(2) # Wed
201                                 self.timer.setRepeated(3) # Thu
202                                 self.timer.setRepeated(4) # Fri
203                                 
204                         if (config.timerentry.repeated.value == 3): # user-defined
205                                 for x in range(0,7):
206                                         if (config.timerentry.day[x].value == 0): self.timer.setRepeated(x)
207
208                                 
209
210                 self.close((True, self.timer))
211
212         def keyCancel(self):
213                 self.close((False,))