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