b73b7c0dfcad9c2af9005a1a6a7f31f3c1dc2910
[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                         servicename = "N/A"
99                         try: # no current service available?
100                                 servicename = str(self.timer.service_ref.getServiceName())
101                         except:
102                                 pass
103                         config.timerentry.service = configElement_nonSave("config.timerentry.service", configSelection, 0, ((servicename),))
104                         
105                         config.timerentry.startdate.addNotifier(self.checkDate)
106                         config.timerentry.enddate.addNotifier(self.checkDate)
107
108         def checkDate(self, configElement):
109                 if (configElement.getConfigPath() == "config.timerentry.startdate"):
110                         if (config.timerentry.enddate.value < config.timerentry.startdate.value):
111                                 config.timerentry.enddate.value = config.timerentry.startdate.value
112                                 config.timerentry.enddate.change()
113                                 self["config"].invalidate(config.timerentry.enddate)
114                 if (configElement.getConfigPath() == "config.timerentry.enddate"):
115                         if (config.timerentry.enddate.value < config.timerentry.startdate.value):
116                                 config.timerentry.startdate.value = config.timerentry.enddate.value
117                                 config.timerentry.startdate.change()
118                                 self["config"].invalidate(config.timerentry.startdate)
119
120         def createSetup(self):
121                 self.list = []
122                 self.list.append(getConfigListEntry(_("Description"), config.timerentry.description))
123                 self.list.append(getConfigListEntry(_("TimerType"), config.timerentry.type))
124
125                 if (config.timerentry.type.value == 0): # once
126                         pass
127                 else: # repeated
128                         self.list.append(getConfigListEntry(_("Frequency"), config.timerentry.repeated))
129                         if (config.timerentry.repeated.value == 0): # daily
130                                 pass
131                         if (config.timerentry.repeated.value == 2): # Mon-Fri
132                                 pass
133                         if (config.timerentry.repeated.value == 1): # weekly
134                                 self.list.append(getConfigListEntry(_("Weekday"), config.timerentry.weekday))
135
136                         if (config.timerentry.repeated.value == 3): # user-defined
137                                 self.list.append(getConfigListEntry(_("Monday"), config.timerentry.day[0]))
138                                 self.list.append(getConfigListEntry(_("Tuesday"), config.timerentry.day[1]))
139                                 self.list.append(getConfigListEntry(_("Wednesday"), config.timerentry.day[2]))
140                                 self.list.append(getConfigListEntry(_("Thursday"), config.timerentry.day[3]))
141                                 self.list.append(getConfigListEntry(_("Friday"), config.timerentry.day[4]))
142                                 self.list.append(getConfigListEntry(_("Saturday"), config.timerentry.day[5]))
143                                 self.list.append(getConfigListEntry(_("Sunday"), config.timerentry.day[6]))
144
145                         #self.list.append(getConfigListEntry("StartDate", config.timerentry.startdate))
146 #               self.list.append(getConfigListEntry("Weekday", config.timerentry.weekday))
147
148                 if (config.timerentry.type.value == 0): # once
149                         self.list.append(getConfigListEntry(_("Start"), config.timerentry.startdate))
150                         self.list.append(getConfigListEntry("", config.timerentry.starttime))
151                 else:
152                         self.list.append(getConfigListEntry(_("StartTime"), config.timerentry.starttime))
153                 if (config.timerentry.type.value == 0): # once
154                         self.list.append(getConfigListEntry(_("End"), config.timerentry.enddate))
155                         self.list.append(getConfigListEntry("", config.timerentry.endtime))
156                 else:
157                         self.list.append(getConfigListEntry(_("EndTime"), config.timerentry.endtime))
158
159                 self.list.append(getConfigListEntry(_("Channel"), config.timerentry.service))
160
161                 self["config"].list = self.list
162                 self["config"].l.setList(self.list)
163
164         def newConfig(self):
165                 print self["config"].getCurrent()
166                 if self["config"].getCurrent()[0] == _("TimerType"):
167                         self.createSetup()
168                 if self["config"].getCurrent()[0] == _("Frequency"):
169                         self.createSetup()
170
171         def keyLeft(self):
172                 if self["config"].getCurrent()[0] == _("Channel"):
173                         self.keySelect()
174                 else:
175                         self["config"].handleKey(config.key["prevElement"])
176                         self.newConfig()
177
178         def keyRightCallback(self, configPath):
179                 currentConfigPath = self["config"].getCurrent()[1].parent.getConfigPath()
180                 # check if we are still on the same config entry
181                 if (currentConfigPath == configPath):
182                         self.keyRight()
183
184         def keyRight(self):
185                 if self["config"].getCurrent()[0] == _("Channel"):
186                         self.keySelect()
187                 else:
188                         self["config"].handleKey(config.key["nextElement"])
189                         self.newConfig()
190                 
191         def keySelect(self):
192                 if self["config"].getCurrent()[0] == _("Channel"):
193                         self.session.openWithCallback(self.finishedChannelSelection, ChannelSelection.SimpleChannelSelection, _("Select channel to record from"))
194
195         def finishedChannelSelection(self, args):
196                 self.timer.service_ref = ServiceReference(args)
197                 config.timerentry.service.vals = (str(self.timer.service_ref.getServiceName()),)
198                 self["config"].invalidate(config.timerentry.service)
199
200         def keyNumberGlobal(self, number):
201                 print "You pressed number " + str(number)
202                 if (self["config"].getCurrent()[1].parent.enabled == True):
203                         self["config"].handleKey(config.key[str(number)])
204
205         def getTimestamp(self, date, mytime):
206                 d = time.localtime(date)
207                 dt = datetime.datetime(d.tm_year, d.tm_mon, d.tm_mday, mytime[0], mytime[1])
208                 print dt
209                 return int(mktime(dt.timetuple()))
210
211         def keyGo(self):
212                 self.timer.resetRepeated()
213                 
214                 if (config.timerentry.type.value == 0): # once
215                         self.timer.begin = self.getTimestamp(config.timerentry.startdate.value, config.timerentry.starttime.value)
216                         self.timer.end = self.getTimestamp(config.timerentry.enddate.value, config.timerentry.endtime.value)
217                 if (config.timerentry.type.value == 1): # repeated
218                         if (config.timerentry.repeated.value == 0): # daily
219                                 for x in range(0,7):
220                                         self.timer.setRepeated(x)
221
222                         if (config.timerentry.repeated.value == 1): # weekly
223                                 self.timer.setRepeated(config.timerentry.weekday.value)
224                                 
225                         if (config.timerentry.repeated.value == 2): # Mon-Fri
226                                 for x in range(0,5):
227                                         self.timer.setRepeated(x)
228                                 
229                         if (config.timerentry.repeated.value == 3): # user-defined
230                                 for x in range(0,7):
231                                         if (config.timerentry.day[x].value == 0): self.timer.setRepeated(x)
232
233                         self.timer.begin = self.getTimestamp(time.time(), config.timerentry.starttime.value)
234                         self.timer.end = self.getTimestamp(time.time(), config.timerentry.endtime.value)                                
235
236                 self.close((True, self.timer))
237
238         def keyCancel(self):
239                 self.close((False,))