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