remove yellow and green-buttons from ChannelSelection
[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 isRecording(self):
108                 isRunning = False
109                 for timer in self.timer_list:
110                         if timer.isRunning():
111                                 isRunning = True
112                 return isRunning
113         
114         def loadTimer(self):
115                 # TODO: PATH!
116                 doc = xml.dom.minidom.parse(self.Filename)
117                 
118                 root = doc.childNodes[0]
119                 for timer in elementsWithTag(root.childNodes, "timer"):
120                         self.record(createTimer(timer))
121         
122         def saveTimer(self):
123                 doc = xml.dom.minidom.Document()
124                 root_element = doc.createElement('timers')
125                 doc.appendChild(root_element)
126                 root_element.appendChild(doc.createTextNode("\n"))
127                 
128                 for timer in self.timer_list + self.processed_timers:
129                         # some timers (instant records) don't want to be saved.
130                         # skip them
131                         if timer.dontSave:
132                                 continue
133                         t = doc.createTextNode("\t")
134                         root_element.appendChild(t)
135                         t = doc.createElement('timer')
136                         t.setAttribute("begin", str(timer.begin))
137                         t.setAttribute("end", str(timer.end))
138                         t.setAttribute("serviceref", str(timer.service_ref))
139                         t.setAttribute("repeated", str(timer.repeated))                 
140                         #t.setAttribute("epgdata", timer.)
141                         t.setAttribute("description", timer.description)
142                         root_element.appendChild(t)
143                         t = doc.createTextNode("\n")
144                         root_element.appendChild(t)
145
146                 file = open(self.Filename, "w")
147                 doc.writexml(codecs.getwriter('UTF-8')(file))
148                 file.write("\n")
149                 file.close()
150         
151         def record(self, entry):
152                 print "[Timer] Record " + str(entry)
153                 entry.Timer = self
154                 self.addTimerEntry(entry)
155
156         def removeEntry(self, entry):
157                 print "[Timer] Remove " + str(entry)
158
159                 if entry.state == timer.TimerEntry.StateRunning:
160                         print "remove running timer."
161                         entry.end = time.time()
162                         self.timeChanged(entry)
163                 elif entry.state != timer.TimerEntry.StateEnded:
164                         entry.activate(timer.TimerEntry.EventAbort)
165                         try: # FIXME: is needed, because after loading the timers, every timer (even already ended ones)
166                                  # have the state EventWaiting..
167                                 self.timer_list.remove(entry)
168                         except:
169                                 pass
170                                 
171                         self.calcNextActivation()
172                         print "timer did not yet start - removing"
173                 else:
174                         print "timer did already end - doing nothing."
175                 
176                 print "state: ", entry.state
177                 print "in processed: ", entry in self.processed_timers
178                 print "in running: ", entry in self.timer_list
179                 # now the timer should be in the processed_timers list. remove it from there.
180                 self.processed_timers.remove(entry)
181
182         def shutdown(self):
183                 self.saveTimer()