Revert "Advanced functions(edit/delete/disable) repeated timers in EPG list"
[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                 refstr = ':'.join(serviceref.ref.toString().split(':')[:11])
94                 for timer in self.session.nav.RecordTimer.timer_list:
95                         if timer.eit == eventid and ':'.join(timer.service_ref.ref.toString().split(':')[:11]) == refstr:
96                                 menu = [(_("Delete timer"), "delete"),(_("Edit timer"), "edit")]
97                                 buttons = ["red", "green"]
98                                 def timerAction(choice):
99                                         if choice is not None:
100                                                 if choice[1] == "delete":
101                                                         self.removeTimer(timer)
102                                                 elif choice[1] == "edit":
103                                                         self.session.open(TimerEntry, timer)
104                                 self.session.openWithCallback(timerAction, ChoiceBox, title=_("Select action for timer %s:") % event.getEventName(), list=menu, keys=buttons)
105                                 break
106                 else:
107                         newEntry = RecordTimerEntry(self.currentService, checkOldTimers = True, dirname = preferredTimerPath(), *parseEvent(self.event))
108                         self.session.openWithCallback(self.finishedAdd, TimerEntry, newEntry)
109
110         def finishedAdd(self, answer):
111                 print "finished add"
112                 if answer[0]:
113                         entry = answer[1]
114                         simulTimerList = self.session.nav.RecordTimer.record(entry)
115                         if simulTimerList is not None:
116                                 for x in simulTimerList:
117                                         if x.setAutoincreaseEnd(entry):
118                                                 self.session.nav.RecordTimer.timeChanged(x)
119                                 simulTimerList = self.session.nav.RecordTimer.record(entry)
120                                 if simulTimerList is not None:
121                                         if not entry.repeated and not config.recording.margin_before.value and not config.recording.margin_after.value and len(simulTimerList) > 1:
122                                                 change_time = False
123                                                 conflict_begin = simulTimerList[1].begin
124                                                 conflict_end = simulTimerList[1].end
125                                                 if conflict_begin == entry.end:
126                                                         entry.end -= 30
127                                                         change_time = True
128                                                 elif entry.begin == conflict_end:
129                                                         entry.begin += 30
130                                                         change_time = True
131                                                 if change_time:
132                                                         simulTimerList = self.session.nav.RecordTimer.record(entry)
133                                         if simulTimerList is not None:
134                                                 self.session.openWithCallback(self.finishSanityCorrection, TimerSanityConflict, simulTimerList)
135                         self["key_green"].setText(_("Change timer"))
136                         self.key_green_choice = self.REMOVE_TIMER
137                 else:
138                         self["key_green"].setText(_("Add timer"))
139                         self.key_green_choice = self.ADD_TIMER
140                         print "Timeredit aborted"
141
142         def finishSanityCorrection(self, answer):
143                 self.finishedAdd(answer)
144
145         def setService(self, service):
146                 self.currentService=service
147                 self["Service"].newService(service.ref)
148                 if self.isRecording:
149                         self["channel"].setText(_("Recording"))
150                 else:
151                         name = service.getServiceName()
152                         if name is not None:
153                                 self["channel"].setText(name)
154                         else:
155                                 self["channel"].setText(_("unknown service"))
156
157         def sort_func(self,x,y):
158                 if x[1] < y[1]:
159                         return -1
160                 elif x[1] == y[1]:
161                         return 0
162                 else:
163                         return 1
164
165         def setEvent(self, event):
166                 self.event = event
167                 self["Event"].newEvent(event)
168                 if event is None:
169                         return
170                 text = event.getEventName()
171                 short = event.getShortDescription()
172                 ext = event.getExtendedDescription()
173                 if short == text:
174                         short = ""
175                 if short and ext:
176                         ext = short + "\n\n" + ext
177                 elif short:
178                         ext = short
179
180                 if text and ext:
181                         text += "\n\n"
182                 text += ext
183
184                 self.setTitle(event.getEventName())
185                 self["epg_description"].setText(text)
186                 self["FullDescription"].setText(ext)
187                 self["datetime"].setText(event.getBeginTimeString())
188                 self["duration"].setText(_("%d min")%(event.getDuration()/60))
189                 self["key_red"].setText("")
190                 if self.SimilarBroadcastTimer is not None:
191                         self.SimilarBroadcastTimer.start(400,True)
192
193                 serviceref = self.currentService
194                 eventid = self.event.getEventId()
195                 refstr = ':'.join(serviceref.ref.toString().split(':')[:11])
196                 isRecordEvent = False
197                 for timer in self.session.nav.RecordTimer.timer_list:
198                         if timer.eit == eventid and ':'.join(timer.service_ref.ref.toString().split(':')[:11]) == refstr:
199                                 isRecordEvent = True
200                                 break
201                 if isRecordEvent and self.key_green_choice != self.REMOVE_TIMER:
202                         self["key_green"].setText(_("Change timer"))
203                         self.key_green_choice = self.REMOVE_TIMER
204                 elif not isRecordEvent and self.key_green_choice != self.ADD_TIMER:
205                         self["key_green"].setText(_("Add timer"))
206                         self.key_green_choice = self.ADD_TIMER
207
208
209         def pageUp(self):
210                 self["epg_description"].pageUp()
211                 self["FullDescription"].pageUp()
212
213         def pageDown(self):
214                 self["epg_description"].pageDown()
215                 self["FullDescription"].pageDown()
216
217         def getSimilarEvents(self):
218                 # search similar broadcastings
219                 if not self.event:
220                         return
221                 refstr = str(self.currentService)
222                 id = self.event.getEventId()
223                 epgcache = eEPGCache.getInstance()
224                 ret = epgcache.search(('NB', 100, eEPGCache.SIMILAR_BROADCASTINGS_SEARCH, refstr, id))
225                 if ret is not None:
226                         text = '\n\n' + _('Similar broadcasts:')
227                         ret.sort(self.sort_func)
228                         for x in ret:
229                                 t = localtime(x[1])
230                                 text += '\n%d.%d.%d, %2d:%02d  -  %s'%(t[2], t[1], t[0], t[3], t[4], x[0])
231                         descr = self["epg_description"]
232                         descr.setText(descr.getText()+text)
233                         descr = self["FullDescription"]
234                         descr.setText(descr.getText()+text)
235                         self["key_red"].setText(_("Similar"))
236
237         def openSimilarList(self):
238                 if self.similarEPGCB is not None and self["key_red"].getText():
239                         id = self.event and self.event.getEventId()
240                         refstr = str(self.currentService)
241                         if id is not None:
242                                 self.similarEPGCB(id, refstr)
243
244         def doContext(self):
245                 if self.event:
246                         text = _("Select action")
247                         menu = [(p.name, boundFunction(self.runPlugin, p)) for p in plugins.getPlugins(where = PluginDescriptor.WHERE_EVENTINFO) \
248                                 if 'servicelist' not in p.__call__.func_code.co_varnames \
249                                         if 'selectedevent' not in p.__call__.func_code.co_varnames ]
250                         if len(menu) == 1:
251                                 menu and menu[0][1]()
252                         elif len(menu) > 1:
253                                 def boxAction(choice):
254                                         if choice:
255                                                 choice[1]()
256                                 text += _(": %s") % self.event.getEventName()
257                                 self.session.openWithCallback(boxAction, ChoiceBox, title=text, list=menu)
258
259         def runPlugin(self, plugin):
260                 plugin(session=self.session, service=self.currentService, event=self.event, eventName=self.event.getEventName())
261
262 class EventViewSimple(Screen, EventViewBase):
263         def __init__(self, session, Event, Ref, callback=None, similarEPGCB=None):
264                 Screen.__init__(self, session)
265                 self.skinName = "EventView"
266                 EventViewBase.__init__(self, Event, Ref, callback, similarEPGCB)
267
268 class EventViewEPGSelect(Screen, EventViewBase):
269         def __init__(self, session, Event, Ref, callback=None, singleEPGCB=None, multiEPGCB=None, similarEPGCB=None):
270                 Screen.__init__(self, session)
271                 self.skinName = "EventView"
272                 EventViewBase.__init__(self, Event, Ref, callback, similarEPGCB)
273                 self["key_yellow"].setText(_("Single EPG"))
274                 self["key_blue"].setText(_("Multi EPG"))
275                 self["epgactions"] = ActionMap(["EventViewEPGActions"],
276                         {
277                                 "openSingleServiceEPG": singleEPGCB,
278                                 "openMultiServiceEPG": multiEPGCB,
279                         })