add slowmotion / fast forward
[enigma2.git] / RecordTimer.py
1 import time
2 import codecs
3 #from time import datetime
4 from Tools import Directories, Notifications
5
6 import timer
7 import xml.dom.minidom
8
9 from Screens.MessageBox import MessageBox
10 import NavigationInstance
11
12 from Tools.XMLTools import elementsWithTag
13 from ServiceReference import ServiceReference
14
15 class RecordTimerEntry(timer.TimerEntry):
16         def __init__(self, begin, end, serviceref, epg, description):
17                 timer.TimerEntry.__init__(self, int(begin), int(end))
18                 
19                 assert isinstance(serviceref, ServiceReference)
20                 
21                 self.service_ref = serviceref
22                 
23                 if epg is not None:
24                         self.epg_data = ""
25                         #str(epg.m_event_name)
26                 else:
27                         self.epg_data = ""
28                 
29                 self.dontSave = False
30                 self.description = description
31                 self.timer = None
32                 self.record_service = None
33                 self.wantStart = False
34                 
35         def calculateFilename(self):
36                 service_name = self.service_ref.getServiceName()
37 #               begin_date = datetime.fromtimestamp(begin).strf...
38                 begin_date = ""
39                 if self.epg_data is not None:
40                         description = " - " + self.epg_data
41                 else:
42                         description = ""
43                 
44                 print "begin_date: " + str(begin_date)
45                 print "service_name: " + str(service_name)
46                 print "description: " + str(description)
47                 self.Filename = Directories.getRecordingFilename(service_name)
48                 #begin_date + " - " + service_name + description)
49                 
50                 # build filename from epg
51                 
52                 # pff das geht noch nicht...
53 #               if epg == None:
54 #                       self.Filename = "recording.ts"
55 #               else:
56 #                       self.Filename = "record_" + str(epg.m_event_name) + ".ts"
57 #               
58 #               print "------------ record filename: %s" % (self.Filename)
59         
60         def tryPrepare(self):
61                 self.calculateFilename()
62                 self.record_service = NavigationInstance.instance.recordService(self.service_ref)
63                 if self.record_service == None:
64                         return False
65                 else:   
66                         if self.record_service.prepare(self.Filename + ".ts"):
67                                 self.record_service = None
68                                 return False
69
70                         f = open(self.Filename + ".ts.meta", "w")
71                         f.write(str(self.service_ref) + "\n")
72                         f.write(self.epg_data + "\n")
73                         del f
74                         return True
75
76         def activate(self, event):
77                 if event == self.EventPrepare:
78                         self.prepareOK = False
79                         if self.tryPrepare():
80                                 self.prepareOK = True
81                         else:
82                                 # error.
83                                 Notifications.AddNotificationWithCallback(self.failureCB, MessageBox, _("A timer failed to record!\nDisable TV and try again?\n"))
84                 elif event == self.EventStart:
85                         if self.prepareOK:
86                                 self.record_service.start()
87                                 print "timer started!"
88                         else:
89                                 print "prepare failed, thus start failed, too."
90                                 self.wantStart = True
91                 elif event == self.EventEnd or event == self.EventAbort:
92                         self.wantStart = False
93                         if self.prepareOK:
94                                 self.record_service.stop()
95                                 self.record_service = None
96                                 print "Timer successfully ended"
97                         else:
98                                 print "prepare failed, thus nothing was recorded."
99
100         def abort():
101                 # fixme
102                 pass
103
104         def failureCB(self, answer):
105                 if answer == True:
106                         NavigationInstance.instance.stopUserServices()
107                         self.activate(self.EventPrepare)
108                         if self.wantStart:
109                                 print "post-activating record"
110                                 self.activate(self.EventStart)
111                 else:
112                         print "user killed record"
113
114 def createTimer(xml):
115         begin = int(xml.getAttribute("begin"))
116         end = int(xml.getAttribute("end"))
117         serviceref = ServiceReference(str(xml.getAttribute("serviceref")))
118         description = xml.getAttribute("description")
119         repeated = xml.getAttribute("repeated")
120         epgdata = xml.getAttribute("epgdata")
121         #filename = xml.getAttribute("filename")
122         entry = RecordTimerEntry(begin, end, serviceref, epgdata, description)
123         entry.repeated = int(repeated)
124         return entry
125
126 class RecordTimer(timer.Timer):
127         def __init__(self):
128                 timer.Timer.__init__(self)
129                 
130                 self.Filename = Directories.resolveFilename(Directories.SCOPE_CONFIG, "timers.xml")
131                 
132                 try:
133                         self.loadTimer()
134                 except:
135                         print "unable to load timers from file!"
136                         
137         def isRecording(self):
138                 isRunning = False
139                 for timer in self.timer_list:
140                         if timer.isRunning():
141                                 isRunning = True
142                 return isRunning
143         
144         def loadTimer(self):
145                 # TODO: PATH!
146                 doc = xml.dom.minidom.parse(self.Filename)
147                 
148                 root = doc.childNodes[0]
149                 for timer in elementsWithTag(root.childNodes, "timer"):
150                         self.record(createTimer(timer))
151         
152         def saveTimer(self):
153                 doc = xml.dom.minidom.Document()
154                 root_element = doc.createElement('timers')
155                 doc.appendChild(root_element)
156                 root_element.appendChild(doc.createTextNode("\n"))
157                 
158                 for timer in self.timer_list + self.processed_timers:
159                         # some timers (instant records) don't want to be saved.
160                         # skip them
161                         if timer.dontSave:
162                                 continue
163                         t = doc.createTextNode("\t")
164                         root_element.appendChild(t)
165                         t = doc.createElement('timer')
166                         t.setAttribute("begin", str(timer.begin))
167                         t.setAttribute("end", str(timer.end))
168                         t.setAttribute("serviceref", str(timer.service_ref))
169                         t.setAttribute("repeated", str(timer.repeated))                 
170                         #t.setAttribute("epgdata", timer.)
171                         t.setAttribute("description", "no description") # timer.description)
172                         root_element.appendChild(t)
173                         t = doc.createTextNode("\n")
174                         root_element.appendChild(t)
175
176                 file = open(self.Filename, "w")
177                 doc.writexml(codecs.getwriter('UTF-8')(file))
178                 file.write("\n")
179                 file.close()
180         
181         def record(self, entry):
182                 print "[Timer] Record " + str(entry)
183                 entry.Timer = self
184                 self.addTimerEntry(entry)
185
186         def removeEntry(self, entry):
187                 print "[Timer] Remove " + str(entry)
188                 
189                 entry.repeated = False
190
191                 entry.repeated = False
192
193                 if entry.state == timer.TimerEntry.StateRunning:
194                         print "remove running timer."
195                         entry.end = time.time()
196                         self.timeChanged(entry)
197                 elif entry.state != timer.TimerEntry.StateEnded:
198                         entry.activate(timer.TimerEntry.EventAbort)
199                         self.timer_list.remove(entry)
200
201                         self.calcNextActivation()
202                         print "timer did not yet start - removing"
203
204                         # the timer was aborted, and removed.
205                         return
206                 else:
207                         print "timer did already end - doing nothing."
208                 
209                 print "state: ", entry.state
210                 print "in processed: ", entry in self.processed_timers
211                 print "in running: ", entry in self.timer_list
212                 # now the timer should be in the processed_timers list. remove it from there.
213                 self.processed_timers.remove(entry)
214
215         def shutdown(self):
216                 self.saveTimer()