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