timer: fix remove of timer
[enigma2.git] / RecordTimer.py
1 import time
2 import codecs
3 #from time import datetime
4 from Tools import Directories
5
6 import timer
7 import xml.dom.minidom
8
9 import NavigationInstance
10
11 from Tools.XMLTools import elementsWithTag
12 from ServiceReference import ServiceReference
13
14 class RecordTimerEntry(timer.TimerEntry):
15         def __init__(self, begin, end, serviceref, epg, description):
16                 timer.TimerEntry.__init__(self, int(begin), int(end))
17                 
18                 assert isinstance(serviceref, ServiceReference)
19                 
20                 self.service_ref = serviceref
21                 
22                 if epg is not None:
23                         self.epg_data = ""
24                         #str(epg.m_event_name)
25                 else:
26                         self.epg_data = ""
27                 
28                 self.dontSave = False
29                 self.description = description
30                 self.timer = None
31                 self.record_service = None
32                 
33         def calculateFilename(self):
34                 service_name = self.service_ref.getServiceName()
35 #               begin_date = datetime.fromtimestamp(begin).strf...
36                 begin_date = ""
37                 if self.epg_data is not None:
38                         description = " - " + self.epg_data
39                 else:
40                         description = ""
41                 
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)
47                 
48                 # build filename from epg
49                 
50                 # pff das geht noch nicht...
51 #               if epg == None:
52 #                       self.Filename = "recording.ts"
53 #               else:
54 #                       self.Filename = "record_" + str(epg.m_event_name) + ".ts"
55 #               
56 #               print "------------ record filename: %s" % (self.Filename)
57         
58         
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."
65                         else:   
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")
70                                 del f
71                                 
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"
82
83
84 def createTimer(xml):
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)
94         return entry
95
96 class RecordTimer(timer.Timer):
97         def __init__(self):
98                 timer.Timer.__init__(self)
99                 
100                 self.Filename = Directories.resolveFilename(Directories.SCOPE_USERETC, "timers.xml")
101                 
102                 try:
103                         self.loadTimer()
104                 except:
105                         print "unable to load timers from file!"
106         
107         def loadTimer(self):
108                 # TODO: PATH!
109                 doc = xml.dom.minidom.parse(self.Filename)
110                 
111                 root = doc.childNodes[0]
112                 for timer in elementsWithTag(root.childNodes, "timer"):
113                         self.record(createTimer(timer))
114         
115         def saveTimer(self):
116                 doc = xml.dom.minidom.Document()
117                 root_element = doc.createElement('timers')
118                 doc.appendChild(root_element)
119                 root_element.appendChild(doc.createTextNode("\n"))
120                 
121                 for timer in self.timer_list + self.processed_timers:
122                         # some timers (instant records) don't want to be saved.
123                         # skip them
124                         if timer.dontSave:
125                                 continue
126                         t = doc.createTextNode("\t")
127                         root_element.appendChild(t)
128                         t = doc.createElement('timer')
129                         t.setAttribute("begin", str(timer.begin))
130                         t.setAttribute("end", str(timer.end))
131                         t.setAttribute("serviceref", str(timer.service_ref))
132                         t.setAttribute("repeated", str(timer.repeated))                 
133                         #t.setAttribute("epgdata", timer.)
134                         t.setAttribute("description", timer.description)
135                         root_element.appendChild(t)
136                         t = doc.createTextNode("\n")
137                         root_element.appendChild(t)
138
139                 file = open(self.Filename, "w")
140                 doc.writexml(codecs.getwriter('UTF-8')(file))
141                 file.write("\n")
142                 file.close()
143         
144         def record(self, entry):
145                 print "[Timer] Record " + str(entry)
146                 entry.Timer = self
147                 self.addTimerEntry(entry)
148
149         def removeEntry(self, entry):
150                 print "[Timer] Remove " + str(entry)
151
152                 if entry.state == timer.TimerEntry.StateRunning:
153                         print "remove running timer."
154                         entry.end = time.time()
155                         self.timeChanged(entry)
156                 elif entry.state != timer.TimerEntry.StateEnded:
157                         entry.activate(timer.TimerEntry.EventAbort)
158                         self.timer_list.remove(entry)
159                         self.calcNextActivation()
160                         print "timer did not yet start - removing"
161                 else:
162                         print "timer did already end - doing nothing."
163                 
164                 print "state: ", entry.state
165                 print "in processed: ", entry in self.processed_timers
166                 print "in running: ", entry in self.timer_list
167                 # now the timer should be in the processed_timers list. remove it from there.
168                 self.processed_timers.remove(entry)
169
170         def shutdown(self):
171                 self.saveTimer()