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