sanity checks for the timer
[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
10 class TimerEntry(Screen):
11     def __init__(self, session, timer):
12         Screen.__init__(self, session)
13         self.timer = timer;
14                 
15         self.createConfig()
16         
17         self["actions"] = NumberActionMap(["SetupActions"],
18         {
19             "ok": self.keyGo,
20             "cancel": self.keyCancel,
21             "left": self.keyLeft,
22             "right": self.keyRight,
23             "1": self.keyNumberGlobal,
24             "2": self.keyNumberGlobal,
25             "3": self.keyNumberGlobal,
26             "4": self.keyNumberGlobal,
27             "5": self.keyNumberGlobal,
28             "6": self.keyNumberGlobal,
29             "7": self.keyNumberGlobal,
30             "8": self.keyNumberGlobal,
31             "9": self.keyNumberGlobal,
32             "0": self.keyNumberGlobal
33         }, -1)
34                 
35         self.list = []
36         self["config"] = ConfigList(self.list)
37         self.createSetup()
38
39     def createConfig(self):
40             config.timerentry = ConfigSubsection()
41
42             config.timerentry.type = configElement_nonSave("config.timerentry.type", configSelection, 0, ("once", "repeated"))
43             config.timerentry.description = configElement_nonSave("config.timerentry.description", configText, self.timer.description, (configText.extendableSize,))
44
45             config.timerentry.repeated = configElement_nonSave("config.timerentry.repeated", configSelection, 0, ("daily", "weekly", "Mon-Fri", "user-defined"))
46
47             config.timerentry.startdate = configElement_nonSave("config.timerentry.startdate", configDateTime, self.timer.begin, ("%d.%B %Y", 86400))
48             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"))
49
50             config.timerentry.enddate = configElement_nonSave("config.timerentry.enddate", configDateTime, self.timer.end, ("%d.%B %Y", 86400))
51             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"))
52             
53             config.timerentry.weekday = configElement_nonSave("config.timerentry.weekday", configDateTime, time(), ("%A", 86400))
54             
55             config.timerentry.monday = configElement_nonSave("config.timerentry.monday", configSelection, 0, ("yes", "no"))
56             config.timerentry.tuesday = configElement_nonSave("config.timerentry.tuesday", configSelection, 0, ("yes", "no"))
57             config.timerentry.wednesday = configElement_nonSave("config.timerentry.wednesday", configSelection, 0, ("yes", "no"))
58             config.timerentry.thursday = configElement_nonSave("config.timerentry.thursday", configSelection, 0, ("yes", "no"))
59             config.timerentry.friday = configElement_nonSave("config.timerentry.friday", configSelection, 0, ("yes", "no"))
60             config.timerentry.saturday = configElement_nonSave("config.timerentry.saturday", configSelection, 0, ("yes", "no"))
61             config.timerentry.sunday = configElement_nonSave("config.timerentry.sunday", configSelection, 0, ("yes", "no"))
62             
63             # FIXME some service-chooser needed here
64             config.timerentry.service = configElement_nonSave("config.timerentry.service", configSelection, 0, ((str(self.timer.service_ref.getServiceName())),))
65             
66             config.timerentry.startdate.addNotifier(self.checkDate)
67             config.timerentry.enddate.addNotifier(self.checkDate)
68
69     def checkDate(self, configElement):
70         if (configElement.getConfigPath() == "config.timerentry.startdate"):
71             if (config.timerentry.enddate.value < config.timerentry.startdate.value):
72                 config.timerentry.enddate.value = config.timerentry.startdate.value
73                 config.timerentry.enddate.change()
74                 #FIXME invalidate the config-entry... for redrawing purposes - HOW?
75         if (configElement.getConfigPath() == "config.timerentry.enddate"):
76             if (config.timerentry.enddate.value < config.timerentry.startdate.value):
77                 config.timerentry.startdate.value = config.timerentry.enddate.value
78                 config.timerentry.startdate.change()
79                 #FIXME invalidate the config-entry... for redrawing purposes - HOW?
80
81     def createSetup(self):
82         self.list = []
83         self.list.append(getConfigListEntry("Description", config.timerentry.description))
84         self.list.append(getConfigListEntry("TimerType", config.timerentry.type))
85         
86         if (config.timerentry.type.value == 0): # once
87             pass
88         else: # repeated
89             self.list.append(getConfigListEntry("Frequency", config.timerentry.repeated))
90             if (config.timerentry.repeated.value == 0): # daily
91                 pass
92             if (config.timerentry.repeated.value == 2): # Mon-Fri
93                 pass
94             if (config.timerentry.repeated.value == 1): # weekly
95                 self.list.append(getConfigListEntry("Weekday", config.timerentry.weekday))
96
97             if (config.timerentry.repeated.value == 3): # user-defined
98                 self.list.append(getConfigListEntry("Monday", config.timerentry.monday))
99                 self.list.append(getConfigListEntry("Tuesday", config.timerentry.tuesday))
100                 self.list.append(getConfigListEntry("Wednesday", config.timerentry.wednesday))
101                 self.list.append(getConfigListEntry("Thursday", config.timerentry.thursday))
102                 self.list.append(getConfigListEntry("Friday", config.timerentry.friday))
103                 self.list.append(getConfigListEntry("Saturday", config.timerentry.saturday))
104                 self.list.append(getConfigListEntry("Sunday", config.timerentry.sunday))
105                 
106             #self.list.append(getConfigListEntry("StartDate", config.timerentry.startdate))
107 #        self.list.append(getConfigListEntry("Weekday", config.timerentry.weekday))
108         
109         if (config.timerentry.type.value == 0): # once
110             self.list.append(getConfigListEntry("StartDate", config.timerentry.startdate))
111         self.list.append(getConfigListEntry("StartTime", config.timerentry.starttime))
112         if (config.timerentry.type.value == 0): # once
113             self.list.append(getConfigListEntry("EndDate", config.timerentry.enddate))
114         self.list.append(getConfigListEntry("EndTime", config.timerentry.endtime))
115         
116         self.list.append(getConfigListEntry("Channel", config.timerentry.service))        
117         
118         self["config"].list = self.list
119         self["config"].l.setList(self.list)
120         
121     def newConfig(self):
122         print self["config"].getCurrent()
123         if self["config"].getCurrent()[0] == "TimerType":
124             self.createSetup()
125         if self["config"].getCurrent()[0] == "Frequency":
126             self.createSetup()
127                     
128     def keyLeft(self):
129         self["config"].handleKey(config.key["prevElement"])
130         self.newConfig()
131
132     def keyRight(self):
133         self["config"].handleKey(config.key["nextElement"])
134         self.newConfig()
135
136     def keyNumberGlobal(self, number):
137         print "You pressed number " + str(number)
138         if (self["config"].getCurrent()[1].parent.enabled == True):
139             self["config"].handleKey(config.key[str(number)])
140
141     def getTimestamp(self, date, time):
142         d = localtime(date) # for gettin indexes 0(year), 1(month) and 2(day)
143         dt = datetime(d.tm_year, d.tm_mon, d.tm_mday, time[0], time[1])
144         print dt
145         return int(mktime(dt.timetuple()))
146
147     def keyGo(self):        
148         if (config.timerentry.type.value == 0): # once
149             self.timer.begin = self.getTimestamp(config.timerentry.startdate.value, config.timerentry.starttime.value)
150             self.timer.end = self.getTimestamp(config.timerentry.enddate.value, config.timerentry.endtime.value)
151         self.close((True, self.timer))
152
153     def keyCancel(self):
154         self.close((False,))