4.3.0r3
[enigma2.git] / usr / lib / enigma2 / python / Screens / EventView.py
1 from Screen import Screen
2 from Screens.TimerEdit import TimerSanityConflict
3 from Screens.MessageBox import MessageBox
4 from Components.ActionMap import ActionMap
5 from Components.Button import Button
6 from Components.Label import Label
7 from Components.ScrollLabel import ScrollLabel
8 from Components.UsageConfig import preferredTimerPath
9 from Components.Sources.ServiceEvent import ServiceEvent
10 from enigma import eEPGCache, eTimer, eServiceReference
11 from RecordTimer import RecordTimerEntry, parseEvent, AFTEREVENT
12 from TimerEntry import TimerEntry
13 from time import localtime
14
15 class EventViewBase:
16         ADD_TIMER = 0
17         REMOVE_TIMER = 1
18         
19         def __init__(self, Event, Ref, callback=None, similarEPGCB=None):
20                 self.similarEPGCB = similarEPGCB
21                 self.cbFunc = callback
22                 self.currentService=Ref
23                 self.isRecording = (not Ref.ref.flags & eServiceReference.isGroup) and Ref.ref.getPath() and Ref.ref.getPath()[0] == '/'
24                 self.event = Event
25                 self["Service"] = ServiceEvent()
26                 self["epg_description"] = ScrollLabel()
27                 self["datetime"] = Label()
28                 self["channel"] = Label()
29                 self["duration"] = Label()
30                 self["key_red"] = Button("")
31                 if similarEPGCB is not None:
32                         self.SimilarBroadcastTimer = eTimer()
33                         self.SimilarBroadcastTimer_conn = self.SimilarBroadcastTimer.timeout.connect(self.getSimilarEvents)
34                 else:
35                         self.SimilarBroadcastTimer = None
36                 self.key_green_choice = self.ADD_TIMER
37                 if self.isRecording:
38                         self["key_green"] = Button("")
39                 else:
40                         self["key_green"] = Button(_("Add timer"))
41                 self["key_yellow"] = Button("")
42                 self["key_blue"] = Button("")
43                 self["actions"] = ActionMap(["OkCancelActions", "EventViewActions"],
44                         {
45                                 "cancel": self.close,
46                                 "ok": self.close,
47                                 "pageUp": self.pageUp,
48                                 "pageDown": self.pageDown,
49                                 "prevEvent": self.prevEvent,
50                                 "nextEvent": self.nextEvent,
51                                 "timerAdd": self.timerAdd,
52                                 "openSimilarList": self.openSimilarList
53                         })
54                 self.onShown.append(self.onCreate)
55
56         def onCreate(self):
57                 self.setService(self.currentService)
58                 self.setEvent(self.event)
59
60         def prevEvent(self):
61                 if self.cbFunc is not None:
62                         self.cbFunc(self.setEvent, self.setService, -1)
63
64         def nextEvent(self):
65                 if self.cbFunc is not None:
66                         self.cbFunc(self.setEvent, self.setService, +1)
67
68         def removeTimer(self, timer):
69                 timer.afterEvent = AFTEREVENT.NONE
70                 self.session.nav.RecordTimer.removeEntry(timer)
71                 self["key_green"].setText(_("Add timer"))
72                 self.key_green_choice = self.ADD_TIMER
73         
74         def timerAdd(self):
75                 if self.isRecording:
76                         return
77                 event = self.event
78                 serviceref = self.currentService
79                 if event is None:
80                         return
81                 eventid = event.getEventId()
82                 refstr = serviceref.ref.toString()
83                 for timer in self.session.nav.RecordTimer.timer_list:
84                         if timer.eit == eventid and timer.service_ref.ref.toString() == refstr:
85                                 cb_func = lambda ret : not ret or self.removeTimer(timer)
86                                 self.session.openWithCallback(cb_func, MessageBox, _("Do you really want to delete %s?") % event.getEventName())
87                                 break
88                 else:
89                         newEntry = RecordTimerEntry(self.currentService, checkOldTimers = True, dirname = preferredTimerPath(), *parseEvent(self.event))
90                         self.session.openWithCallback(self.finishedAdd, TimerEntry, newEntry)
91
92         def finishedAdd(self, answer):
93                 print "finished add"
94                 if answer[0]:
95                         entry = answer[1]
96                         simulTimerList = self.session.nav.RecordTimer.record(entry)
97                         if simulTimerList is not None:
98                                 for x in simulTimerList:
99                                         if x.setAutoincreaseEnd(entry):
100                                                 self.session.nav.RecordTimer.timeChanged(x)
101                                 simulTimerList = self.session.nav.RecordTimer.record(entry)
102                                 if simulTimerList is not None:
103                                         self.session.openWithCallback(self.finishSanityCorrection, TimerSanityConflict, simulTimerList)
104                         self["key_green"].setText(_("Remove timer"))
105                         self.key_green_choice = self.REMOVE_TIMER
106                 else:
107                         self["key_green"].setText(_("Add timer"))
108                         self.key_green_choice = self.ADD_TIMER
109                         print "Timeredit aborted"               
110
111         def finishSanityCorrection(self, answer):
112                 self.finishedAdd(answer)
113
114         def setService(self, service):
115                 self.currentService=service
116                 self["Service"].newService(self.currentService.ref)
117                 if self.isRecording:
118                         self["channel"].setText(_("Recording"))
119                 else:
120                         name = self.currentService.getServiceName()
121                         if name is not None:
122                                 self["channel"].setText(name)
123                         else:
124                                 self["channel"].setText(_("unknown service"))
125
126         def sort_func(self,x,y):
127                 if x[1] < y[1]:
128                         return -1
129                 elif x[1] == y[1]:
130                         return 0
131                 else:
132                         return 1
133
134         def setEvent(self, event):
135                 self.event = event
136                 if event is None:
137                         return
138                 text = event.getEventName()
139                 short = event.getShortDescription()
140                 ext = event.getExtendedDescription()
141                 if short and short != text:
142                         text += '\n\n' + short
143                 if ext:
144                         if text:
145                                 text += '\n\n'
146                         text += ext
147
148                 self.setTitle(event.getEventName())
149                 self["epg_description"].setText(text)
150                 self["datetime"].setText(event.getBeginTimeString())
151                 self["duration"].setText(_("%d min")%(event.getDuration()/60))
152                 self["key_red"].setText("")
153                 if self.SimilarBroadcastTimer is not None:
154                         self.SimilarBroadcastTimer.start(400,True)
155
156                 serviceref = self.currentService
157                 eventid = self.event.getEventId()
158                 refstr = serviceref.ref.toString()
159                 isRecordEvent = False
160                 for timer in self.session.nav.RecordTimer.timer_list:
161                         if timer.eit == eventid and timer.service_ref.ref.toString() == refstr:
162                                 isRecordEvent = True
163                                 break
164                 if isRecordEvent and self.key_green_choice != self.REMOVE_TIMER:
165                         self["key_green"].setText(_("Remove timer"))
166                         self.key_green_choice = self.REMOVE_TIMER
167                 elif not isRecordEvent and self.key_green_choice != self.ADD_TIMER:
168                         self["key_green"].setText(_("Add timer"))
169                         self.key_green_choice = self.ADD_TIMER
170
171
172         def pageUp(self):
173                 self["epg_description"].pageUp()
174
175         def pageDown(self):
176                 self["epg_description"].pageDown()
177
178         def getSimilarEvents(self):
179          # search similar broadcastings
180                 refstr = str(self.currentService)
181                 id = self.event.getEventId()
182                 epgcache = eEPGCache.getInstance()
183                 ret = epgcache.search(('NB', 100, eEPGCache.SIMILAR_BROADCASTINGS_SEARCH, refstr, id))
184                 if ret is not None:
185                         descr = self["epg_description"]
186                         text = descr.getText()
187                         text += '\n\n' + _('Similar broadcasts:')
188                         ret.sort(self.sort_func)
189                         for x in ret:
190                                 t = localtime(x[1])
191                                 text += '\n%d.%d.%d, %02d:%02d  -  %s'%(t[2], t[1], t[0], t[3], t[4], x[0])
192                         descr.setText(text)
193                         self["key_red"].setText(_("Similar"))
194
195         def openSimilarList(self):
196                 if self.similarEPGCB is not None and self["key_red"].getText():
197                         id = self.event and self.event.getEventId()
198                         refstr = str(self.currentService)
199                         if id is not None:
200                                 self.similarEPGCB(id, refstr)
201
202 class EventViewSimple(Screen, EventViewBase):
203         def __init__(self, session, Event, Ref, callback=None, similarEPGCB=None):
204                 Screen.__init__(self, session)
205                 self.skinName = "EventView"
206                 EventViewBase.__init__(self, Event, Ref, callback, similarEPGCB)
207
208 class EventViewEPGSelect(Screen, EventViewBase):
209         def __init__(self, session, Event, Ref, callback=None, singleEPGCB=None, multiEPGCB=None, similarEPGCB=None):
210                 Screen.__init__(self, session)
211                 self.skinName = "EventView"
212                 EventViewBase.__init__(self, Event, Ref, callback, similarEPGCB)
213                 self["key_yellow"].setText(_("Single EPG"))
214                 self["key_blue"].setText(_("Multi EPG"))
215                 self["epgactions"] = ActionMap(["EventViewEPGActions"],
216                         {
217                                 "openSingleServiceEPG": singleEPGCB,
218                                 "openMultiServiceEPG": multiEPGCB,
219                         })