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