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