timer: fix displayed state. Don't save instant records. properly remove timerentries.
[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         epgdata = xml.getAttribute("epgdata")
90         #filename = xml.getAttribute("filename")
91         return RecordTimerEntry(begin, end, serviceref, epgdata, description)
92
93 class RecordTimer(timer.Timer):
94         def __init__(self):
95                 timer.Timer.__init__(self)
96                 
97                 self.Filename = Directories.resolveFilename(Directories.SCOPE_USERETC, "timers.xml")
98                 
99                 try:
100                         self.loadTimer()
101                 except:
102                         print "unable to load timers from file!"
103         
104         def loadTimer(self):
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                         # some timers (instant records) don't want to be saved.
120                         # skip them
121                         if timer.dontSave:
122                                 continue
123                         t = doc.createTextNode("\t")
124                         root_element.appendChild(t)
125                         t = doc.createElement('timer')
126                         t.setAttribute("begin", str(timer.begin))
127                         t.setAttribute("end", str(timer.end))
128                         t.setAttribute("serviceref", str(timer.service_ref))
129                         #t.setAttribute("epgdata", timer.)
130                         t.setAttribute("description", timer.description)
131                         root_element.appendChild(t)
132                         t = doc.createTextNode("\n")
133                         root_element.appendChild(t)
134
135                 file = open(self.Filename, "w")
136                 doc.writexml(codecs.getwriter('UTF-8')(file))
137                 file.write("\n")
138                 file.close()
139         
140         def record(self, entry):
141                 entry.Timer = self
142                 self.addTimerEntry(entry)
143
144         def removeEntry(self, entry):
145                 if entry.state == timer.TimerEntry.StateRunning:
146                         entry.end = time.time()
147                         self.timeChanged(entry)
148                 elif entry.state != timer.TimerEntry.StateEnded:
149                         entry.activate(timer.TimerEntry.EventAbort)
150                         self.timer_list.remove(entry)
151                         self.calcNextActivation()
152                         print "timer did not yet start - removing"
153                 else:
154                         print "timer did already end - doing nothing."
155                 
156                 # now the timer should be in the processed_timers list. remove it from there.
157                 self.processed_timers.remove(entry)
158
159         def shutdown(self):
160                 self.saveTimer()