3 #from time import datetime
4 from Tools import Directories
9 import NavigationInstance
11 from Tools.XMLTools import elementsWithTag
12 from ServiceReference import ServiceReference
14 class RecordTimerEntry(timer.TimerEntry):
15 def __init__(self, begin, end, serviceref, epg, description):
16 timer.TimerEntry.__init__(self, int(begin), int(end))
18 assert isinstance(serviceref, ServiceReference)
20 self.service_ref = serviceref
24 #str(epg.m_event_name)
29 self.description = description
31 self.record_service = None
33 def calculateFilename(self):
34 service_name = self.service_ref.getServiceName()
35 # begin_date = datetime.fromtimestamp(begin).strf...
37 if self.epg_data is not None:
38 description = " - " + self.epg_data
42 print "begin_date: " + str(begin_date)
43 print "service_name: " + str(service_name)
44 print "description: " + str(description)
45 self.Filename = Directories.getRecordingFilename(service_name)
46 #begin_date + " - " + service_name + description)
48 # build filename from epg
50 # pff das geht noch nicht...
52 # self.Filename = "recording.ts"
54 # self.Filename = "record_" + str(epg.m_event_name) + ".ts"
56 # print "------------ record filename: %s" % (self.Filename)
59 def activate(self, event):
60 if event == self.EventPrepare:
61 self.calculateFilename()
62 self.record_service = NavigationInstance.instance.recordService(self.service_ref)
63 if self.record_service == None:
64 print "timer record failed."
66 self.record_service.prepare(self.Filename + ".ts")
67 f = open(self.Filename + ".ts.meta", "w")
68 f.write(str(self.service_ref) + "\n")
69 f.write(self.epg_data + "\n")
72 elif self.record_service == None:
73 if event != self.EventAbort:
74 print "timer record start failed, can't finish recording."
75 elif event == self.EventStart:
76 self.record_service.start()
77 print "timer started!"
78 elif event == self.EventEnd or event == self.EventAbort:
79 self.record_service.stop()
80 self.record_service = None
81 print "Timer successfully ended"
85 begin = int(xml.getAttribute("begin"))
86 end = int(xml.getAttribute("end"))
87 serviceref = ServiceReference(str(xml.getAttribute("serviceref")))
88 description = xml.getAttribute("description")
89 repeated = xml.getAttribute("repeated")
90 epgdata = xml.getAttribute("epgdata")
91 #filename = xml.getAttribute("filename")
92 entry = RecordTimerEntry(begin, end, serviceref, epgdata, description)
93 entry.repeated = int(repeated)
96 class RecordTimer(timer.Timer):
98 timer.Timer.__init__(self)
100 self.Filename = Directories.resolveFilename(Directories.SCOPE_USERETC, "timers.xml")
105 print "unable to load timers from file!"
107 def isRecording(self):
109 for timer in self.timer_list:
110 if timer.isRunning():
116 doc = xml.dom.minidom.parse(self.Filename)
118 root = doc.childNodes[0]
119 for timer in elementsWithTag(root.childNodes, "timer"):
120 self.record(createTimer(timer))
123 doc = xml.dom.minidom.Document()
124 root_element = doc.createElement('timers')
125 doc.appendChild(root_element)
126 root_element.appendChild(doc.createTextNode("\n"))
128 for timer in self.timer_list + self.processed_timers:
129 # some timers (instant records) don't want to be saved.
133 t = doc.createTextNode("\t")
134 root_element.appendChild(t)
135 t = doc.createElement('timer')
136 t.setAttribute("begin", str(timer.begin))
137 t.setAttribute("end", str(timer.end))
138 t.setAttribute("serviceref", str(timer.service_ref))
139 t.setAttribute("repeated", str(timer.repeated))
140 #t.setAttribute("epgdata", timer.)
141 t.setAttribute("description", timer.description)
142 root_element.appendChild(t)
143 t = doc.createTextNode("\n")
144 root_element.appendChild(t)
146 file = open(self.Filename, "w")
147 doc.writexml(codecs.getwriter('UTF-8')(file))
151 def record(self, entry):
152 print "[Timer] Record " + str(entry)
154 self.addTimerEntry(entry)
156 def removeEntry(self, entry):
157 print "[Timer] Remove " + str(entry)
159 if entry.state == timer.TimerEntry.StateRunning:
160 print "remove running timer."
161 entry.end = time.time()
162 self.timeChanged(entry)
163 elif entry.state != timer.TimerEntry.StateEnded:
164 entry.activate(timer.TimerEntry.EventAbort)
165 try: # FIXME: is needed, because after loading the timers, every timer (even already ended ones)
166 # have the state EventWaiting..
167 self.timer_list.remove(entry)
171 self.calcNextActivation()
172 print "timer did not yet start - removing"
174 print "timer did already end - doing nothing."
176 print "state: ", entry.state
177 print "in processed: ", entry in self.processed_timers
178 print "in running: ", entry in self.timer_list
179 # now the timer should be in the processed_timers list. remove it from there.
180 self.processed_timers.remove(entry)