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