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