- work on timers
[enigma2.git] / RecordTimer.py
1 import time
2 import codecs
3
4 from timer import *
5 import xml.dom.minidom
6
7 import NavigationInstance
8
9 from Tools.XMLTools import elementsWithTag
10 from ServiceReference import ServiceReference
11
12 class RecordTimerEntry(TimerEntry):
13         def __init__(self, begin, end, serviceref, epg):
14                 TimerEntry.__init__(self, int(begin), int(end))
15                 
16                 assert isinstance(serviceref, ServiceReference)
17                 
18                 self.service_ref = serviceref
19                 
20                 print self.service_ref.getServiceName()
21                 self.epg_data = epg
22                 self.timer = None
23                 self.record_service = None
24                 
25                 # build filename from epg
26                 
27                 # pff das geht noch nicht...
28 #               if epg == None:
29 #                       self.Filename = "recording.ts"
30 #               else:
31 #                       self.Filename = "record_" + str(epg.m_event_name) + ".ts"
32 #               
33 #               print "------------ record filename: %s" % (self.Filename)
34                 
35         def activate(self, event):
36                 if event == self.EventPrepare:
37                         self.record_service = NavigationInstance.instance.recordService(self.service_ref)
38                         if self.record_service == None:
39                                 print "timer record failed."
40                         else:   
41                                 self.record_service.prepare()
42                 elif self.record_service == None:
43                         if event != self.EventAbort:
44                                 print "timer record start failed, can't finish recording."
45                 elif event == self.EventStart:
46                         self.record_service.start()
47                         print "timer started!"
48                 elif event == self.EventEnd or event == self.EventAbort:
49                         self.record_service.stop()
50                         self.record_service = None
51                         print "Timer successfully ended"
52
53
54 def createTimer(xml):
55         begin = int(xml.getAttribute("begin"))
56         end = int(xml.getAttribute("end"))
57         serviceref = ServiceReference(str(xml.getAttribute("serviceref")))
58         epgdata = xml.getAttribute("epgdata")
59         #filename = xml.getAttribute("filename")
60         return RecordTimerEntry(begin, end, serviceref, epgdata)
61
62 class RecordTimer(Timer):
63         def __init__(self):
64                 Timer.__init__(self)
65                 
66                 self.Filename = "timers.xml"
67                 
68 #               try:
69                 self.loadTimer()
70 #               except:
71 #                       print "unable to load timers from file!"
72         
73         def loadTimer(self):
74                 
75                 # TODO: PATH!
76                 doc = xml.dom.minidom.parse(self.Filename)
77                 
78                 root = doc.childNodes[0]
79                 for timer in elementsWithTag(root.childNodes, "timer"):
80                         self.record(createTimer(timer))
81         
82         def saveTimer(self):
83                 doc = xml.dom.minidom.Document()
84                 root_element = doc.createElement('timers')
85                 doc.appendChild(root_element)
86                 root_element.appendChild(doc.createTextNode("\n"))
87                 
88                 for timer in self.timer_list + self.processed_timers:
89                         t = doc.createTextNode("\t")
90                         root_element.appendChild(t)
91                         t = doc.createElement('timer')
92                         t.setAttribute("begin", str(timer.begin))
93                         t.setAttribute("end", str(timer.end))
94                         t.setAttribute("serviceref", str(timer.service_ref))
95                         #t.setAttribute("epgdata", timer.)
96                         root_element.appendChild(t)
97                         t = doc.createTextNode("\n")
98                         root_element.appendChild(t)
99                 
100                 file = open(self.Filename, "w")
101                 doc.writexml(codecs.getwriter('UTF-8')(file))
102                 file.close()
103         
104         def record(self, entry):
105                 entry.Timer = self
106                 self.addTimerEntry(entry)
107
108         def removeEntry(self, entry):
109                 if entry.state == TimerEntry.StateRunning:
110                         entry.End = time.time()
111                         print "aborting timer"
112                 elif entry.state != TimerEntry.StateEnded:
113                         entry.activate(TimerEntry.EventAbort)
114                         self.timer_list.remove(entry)
115                         print "timer did not yet start - removing"
116                 else:
117                         print "timer did already end - doing nothing."
118
119                 self.calcNextActivation()
120
121
122         def shutdown(self):
123                 self.saveTimer()