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