GUISkin: add getSkinTitle
[openblackhole/openblackhole-enigma2.git] / lib / python / Screens / EventView.py
1 from Screen import Screen
2 from Screens.TimerEdit import TimerSanityConflict
3 from Screens.MessageBox import MessageBox
4 from Screens.ChoiceBox import ChoiceBox
5 from Components.ActionMap import ActionMap
6 from Components.Button import Button
7 from Components.Label import Label
8 from Components.ScrollLabel import ScrollLabel
9 from Components.PluginComponent import plugins
10 from Components.MenuList import MenuList
11 from Components.TimerList import TimerList
12 from Components.UsageConfig import preferredTimerPath
13 from Components.Sources.ServiceEvent import ServiceEvent
14 from Components.Sources.Event import Event
15 from enigma import eEPGCache, eTimer, eServiceReference
16 from RecordTimer import RecordTimerEntry, parseEvent, AFTEREVENT
17 from TimerEntry import TimerEntry
18 from Plugins.Plugin import PluginDescriptor
19 from Tools.BoundFunction import boundFunction
20 from time import localtime
21 from Components.config import config
22
23 class EventViewBase:
24         ADD_TIMER = 0
25         REMOVE_TIMER = 1
26
27         def __init__(self, event, Ref, callback=None, similarEPGCB=None):
28                 self.similarEPGCB = similarEPGCB
29                 self.cbFunc = callback
30                 self.currentService=Ref
31                 self.isRecording = (not Ref.ref.flags & eServiceReference.isGroup) and Ref.ref.getPath()
32                 self.event = event
33                 self["Service"] = ServiceEvent()
34                 self["Event"] = Event()
35                 self["epg_description"] = ScrollLabel()
36                 self["FullDescription"] = ScrollLabel()
37                 self["datetime"] = Label()
38                 self["channel"] = Label()
39                 self["duration"] = Label()
40                 self["key_red"] = Button("")
41                 if similarEPGCB is not None:
42                         self.SimilarBroadcastTimer = eTimer()
43                         self.SimilarBroadcastTimer.callback.append(self.getSimilarEvents)
44                 else:
45                         self.SimilarBroadcastTimer = None
46                 self.key_green_choice = self.ADD_TIMER
47                 if self.isRecording:
48                         self["key_green"] = Button("")
49                 else:
50                         self["key_green"] = Button(_("Add timer"))
51                 self["key_yellow"] = Button("")
52                 self["key_blue"] = Button("")
53                 self["actions"] = ActionMap(["OkCancelActions", "EventViewActions"],
54                         {
55                                 "cancel": self.close,
56                                 "ok": self.close,
57                                 "pageUp": self.pageUp,
58                                 "pageDown": self.pageDown,
59                                 "prevEvent": self.prevEvent,
60                                 "nextEvent": self.nextEvent,
61                                 "timerAdd": self.timerAdd,
62                                 "openSimilarList": self.openSimilarList,
63                                 "contextMenu": self.doContext,
64                         })
65                 self.onShown.append(self.onCreate)
66
67         def onCreate(self):
68                 self.setService(self.currentService)
69                 self.setEvent(self.event)
70
71         def prevEvent(self):
72                 if self.cbFunc is not None:
73                         self.cbFunc(self.setEvent, self.setService, -1)
74
75         def nextEvent(self):
76                 if self.cbFunc is not None:
77                         self.cbFunc(self.setEvent, self.setService, +1)
78
79         def removeTimer(self, timer):
80                 timer.afterEvent = AFTEREVENT.NONE
81                 self.session.nav.RecordTimer.removeEntry(timer)
82                 self["key_green"].setText(_("Add timer"))
83                 self.key_green_choice = self.ADD_TIMER
84
85         def timerAdd(self):
86                 if self.isRecording:
87                         return
88                 event = self.event
89                 serviceref = self.currentService
90                 if event is None:
91                         return
92                 eventid = event.getEventId()
93                 begin = event.getBeginTime()
94                 end = begin + event.getDuration()
95                 refstr = ':'.join(serviceref.ref.toString().split(':')[:11])
96                 isRecordEvent = False
97                 for timer in self.session.nav.RecordTimer.timer_list:
98                         needed_ref = ':'.join(timer.service_ref.ref.toString().split(':')[:11]) == refstr
99                         if needed_ref and timer.eit == eventid and (begin < timer.begin <= end or timer.begin <= begin <= timer.end):
100                                 isRecordEvent = True
101                                 break
102                         elif needed_ref and timer.repeated and self.session.nav.RecordTimer.isInRepeatTimer(timer, event):
103                                 isRecordEvent = True
104                                 break
105                 if isRecordEvent:
106                         title_text = timer.repeated and _("Attention, this is repeated timer!\n") or ""
107                         menu = [(_("Delete timer"), "delete"),(_("Edit timer"), "edit")]
108                         buttons = ["red", "green"]
109                         def timerAction(choice):
110                                 if choice is not None:
111                                         if choice[1] == "delete":
112                                                 self.removeTimer(timer)
113                                         elif choice[1] == "edit":
114                                                 self.session.openWithCallback(self.finishedEdit, TimerEntry, timer)
115                         self.session.openWithCallback(timerAction, ChoiceBox, title=title_text + _("Select action for timer '%s'.") % timer.name, list=menu, keys=buttons)
116                 else:
117                         newEntry = RecordTimerEntry(self.currentService, checkOldTimers = True, dirname = preferredTimerPath(), *parseEvent(self.event))
118                         self.session.openWithCallback(self.finishedAdd, TimerEntry, newEntry)
119
120         def finishedEdit(self, answer=None):
121                 if answer[0]:
122                         entry = answer[1]
123                         simulTimerList = self.session.nav.RecordTimer.record(entry)
124                         if simulTimerList is not None:
125                                 for x in simulTimerList:
126                                         if x.setAutoincreaseEnd(entry):
127                                                 self.session.nav.RecordTimer.timeChanged(x)
128                                 simulTimerList = self.session.nav.RecordTimer.record(entry)
129                                 if simulTimerList is not None:
130                                         self.session.openWithCallback(self.finishedEdit, TimerSanityConflict, simulTimerList)
131                                         return
132                                 else:
133                                         self.session.nav.RecordTimer.timeChanged(entry)
134                 if answer is not None and len(answer) > 1:
135                         entry = answer[1]
136                         if not entry.disabled:
137                                 self["key_green"].setText(_("Change timer"))
138                                 self.key_green_choice = self.REMOVE_TIMER
139                         else:
140                                 self["key_green"].setText(_("Add timer"))
141                                 self.key_green_choice = self.ADD_TIMER
142
143         def finishedAdd(self, answer):
144                 print "finished add"
145                 if answer[0]:
146                         entry = answer[1]
147                         simulTimerList = self.session.nav.RecordTimer.record(entry)
148                         if simulTimerList is not None:
149                                 for x in simulTimerList:
150                                         if x.setAutoincreaseEnd(entry):
151                                                 self.session.nav.RecordTimer.timeChanged(x)
152                                 simulTimerList = self.session.nav.RecordTimer.record(entry)
153                                 if simulTimerList is not None:
154                                         if not entry.repeated and not config.recording.margin_before.value and not config.recording.margin_after.value and len(simulTimerList) > 1:
155                                                 change_time = False
156                                                 conflict_begin = simulTimerList[1].begin
157                                                 conflict_end = simulTimerList[1].end
158                                                 if conflict_begin == entry.end:
159                                                         entry.end -= 30
160                                                         change_time = True
161                                                 elif entry.begin == conflict_end:
162                                                         entry.begin += 30
163                                                         change_time = True
164                                                 if change_time:
165                                                         simulTimerList = self.session.nav.RecordTimer.record(entry)
166                                         if simulTimerList is not None:
167                                                 self.session.openWithCallback(self.finishSanityCorrection, TimerSanityConflict, simulTimerList)
168                         self["key_green"].setText(_("Change timer"))
169                         self.key_green_choice = self.REMOVE_TIMER
170                 else:
171                         self["key_green"].setText(_("Add timer"))
172                         self.key_green_choice = self.ADD_TIMER
173                         print "Timeredit aborted"
174
175         def finishSanityCorrection(self, answer):
176                 self.finishedAdd(answer)
177
178         def setService(self, service):
179                 self.currentService=service
180                 self["Service"].newService(service.ref)
181                 if self.isRecording:
182                         self["channel"].setText(_("Recording"))
183                 else:
184                         name = service.getServiceName()
185                         if name is not None:
186                                 self["channel"].setText(name)
187                         else:
188                                 self["channel"].setText(_("unknown service"))
189
190         def sort_func(self,x,y):
191                 if x[1] < y[1]:
192                         return -1
193                 elif x[1] == y[1]:
194                         return 0
195                 else:
196                         return 1
197
198         def setEvent(self, event):
199                 self.event = event
200                 self["Event"].newEvent(event)
201                 if event is None:
202                         return
203                 text = event.getEventName()
204                 short = event.getShortDescription()
205                 ext = event.getExtendedDescription()
206                 if short == text:
207                         short = ""
208                 if short and ext:
209                         ext = short + "\n\n" + ext
210                 elif short:
211                         ext = short
212
213                 if text and ext:
214                         text += "\n\n"
215                 text += ext
216
217                 self.setTitle(event.getEventName())
218                 self["epg_description"].setText(text)
219                 self["FullDescription"].setText(ext)
220                 self["datetime"].setText(event.getBeginTimeString())
221                 self["duration"].setText(_("%d min")%(event.getDuration()/60))
222                 self["key_red"].setText("")
223                 if self.SimilarBroadcastTimer is not None:
224                         self.SimilarBroadcastTimer.start(400,True)
225
226                 serviceref = self.currentService
227                 eventid = self.event.getEventId()
228                 begin = event.getBeginTime()
229                 end = begin + event.getDuration()
230                 refstr = ':'.join(serviceref.ref.toString().split(':')[:11])
231                 isRecordEvent = False
232                 for timer in self.session.nav.RecordTimer.timer_list:
233                         needed_ref = ':'.join(timer.service_ref.ref.toString().split(':')[:11]) == refstr
234                         if needed_ref and (timer.eit == eventid and (begin < timer.begin <= end or timer.begin <= begin <= timer.end) or timer.repeated and self.session.nav.RecordTimer.isInRepeatTimer(timer, event)):
235                                 isRecordEvent = True
236                                 break
237                 if isRecordEvent and self.key_green_choice != self.REMOVE_TIMER:
238                         self["key_green"].setText(_("Change timer"))
239                         self.key_green_choice = self.REMOVE_TIMER
240                 elif not isRecordEvent and self.key_green_choice != self.ADD_TIMER:
241                         self["key_green"].setText(_("Add timer"))
242                         self.key_green_choice = self.ADD_TIMER
243
244
245         def pageUp(self):
246                 self["epg_description"].pageUp()
247                 self["FullDescription"].pageUp()
248
249         def pageDown(self):
250                 self["epg_description"].pageDown()
251                 self["FullDescription"].pageDown()
252
253         def getSimilarEvents(self):
254                 # search similar broadcastings
255                 if not self.event:
256                         return
257                 refstr = str(self.currentService)
258                 id = self.event.getEventId()
259                 epgcache = eEPGCache.getInstance()
260                 ret = epgcache.search(('NB', 100, eEPGCache.SIMILAR_BROADCASTINGS_SEARCH, refstr, id))
261                 if ret is not None:
262                         text = '\n\n' + _('Similar broadcasts:')
263                         ret.sort(self.sort_func)
264                         for x in ret:
265                                 t = localtime(x[1])
266                                 text += '\n%d.%d.%d, %2d:%02d  -  %s'%(t[2], t[1], t[0], t[3], t[4], x[0])
267                         descr = self["epg_description"]
268                         descr.setText(descr.getText()+text)
269                         descr = self["FullDescription"]
270                         descr.setText(descr.getText()+text)
271                         self["key_red"].setText(_("Similar"))
272
273         def openSimilarList(self):
274                 if self.similarEPGCB is not None and self["key_red"].getText():
275                         id = self.event and self.event.getEventId()
276                         refstr = str(self.currentService)
277                         if id is not None:
278                                 self.similarEPGCB(id, refstr)
279
280         def doContext(self):
281                 if self.event:
282                         text = _("Select action")
283                         menu = [(p.name, boundFunction(self.runPlugin, p)) for p in plugins.getPlugins(where = PluginDescriptor.WHERE_EVENTINFO) \
284                                 if 'servicelist' not in p.__call__.func_code.co_varnames \
285                                         if 'selectedevent' not in p.__call__.func_code.co_varnames ]
286                         if len(menu) == 1:
287                                 menu and menu[0][1]()
288                         elif len(menu) > 1:
289                                 def boxAction(choice):
290                                         if choice:
291                                                 choice[1]()
292                                 text += _(": %s") % self.event.getEventName()
293                                 self.session.openWithCallback(boxAction, ChoiceBox, title=text, list=menu)
294
295         def runPlugin(self, plugin):
296                 plugin(session=self.session, service=self.currentService, event=self.event, eventName=self.event.getEventName())
297
298 class EventViewSimple(Screen, EventViewBase):
299         def __init__(self, session, Event, Ref, callback=None, similarEPGCB=None):
300                 Screen.__init__(self, session)
301                 self.skinName = "EventView"
302                 EventViewBase.__init__(self, Event, Ref, callback, similarEPGCB)
303
304 class EventViewEPGSelect(Screen, EventViewBase):
305         def __init__(self, session, Event, Ref, callback=None, singleEPGCB=None, multiEPGCB=None, similarEPGCB=None):
306                 Screen.__init__(self, session)
307                 self.skinName = "EventView"
308                 EventViewBase.__init__(self, Event, Ref, callback, similarEPGCB)
309                 self["key_yellow"].setText(_("Single EPG"))
310                 self["key_blue"].setText(_("Multi EPG"))
311                 self["epgactions"] = ActionMap(["EventViewEPGActions"],
312                         {
313                                 "openSingleServiceEPG": singleEPGCB,
314                                 "openMultiServiceEPG": multiEPGCB,
315                         })