Merge branch 'bug_352_fix_some_cutlist_bugs'
[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 Components.UsageConfig import preferredTimerPath
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 from Components.config import config
15
16 class EventViewBase:
17         ADD_TIMER = 0
18         REMOVE_TIMER = 1
19         
20         def __init__(self, Event, Ref, callback=None, similarEPGCB=None):
21                 self.similarEPGCB = similarEPGCB
22                 self.cbFunc = callback
23                 self.currentService=Ref
24                 self.isRecording = (not Ref.ref.flags & eServiceReference.isGroup) and Ref.ref.getPath()
25                 self.event = Event
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.callback.append(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                 if self.isRecording:
117                         self["channel"].setText(_("Recording"))
118                 else:
119                         name = self.currentService.getServiceName()
120                         if name is not None:
121                                 self["channel"].setText(name)
122                         else:
123                                 self["channel"].setText(_("unknown service"))
124
125         def sort_func(self,x,y):
126                 if x[1] < y[1]:
127                         return -1
128                 elif x[1] == y[1]:
129                         return 0
130                 else:
131                         return 1
132
133         def setEvent(self, event):
134                 self.event = event
135                 if event is None:
136                         return
137                 text = event.getEventName()
138                 short = event.getShortDescription()
139                 ext = event.getExtendedDescription()
140                 if short and short != text:
141                         text += '\n\n' + short
142                 if ext:
143                         if text:
144                                 text += '\n\n'
145                         text += ext
146
147                 self.setTitle(event.getEventName())
148                 self["epg_description"].setText(text)
149                 self["datetime"].setText(event.getBeginTimeString())
150                 self["duration"].setText(_("%d min")%(event.getDuration()/60))
151                 self["key_red"].setText("")
152                 if self.SimilarBroadcastTimer is not None:
153                         self.SimilarBroadcastTimer.start(400,True)
154
155                 serviceref = self.currentService
156                 eventid = self.event.getEventId()
157                 refstr = serviceref.ref.toString()
158                 isRecordEvent = False
159                 for timer in self.session.nav.RecordTimer.timer_list:
160                         if timer.eit == eventid and timer.service_ref.ref.toString() == refstr:
161                                 isRecordEvent = True
162                                 break
163                 if isRecordEvent and self.key_green_choice != self.REMOVE_TIMER:
164                         self["key_green"].setText(_("Remove timer"))
165                         self.key_green_choice = self.REMOVE_TIMER
166                 elif not isRecordEvent and self.key_green_choice != self.ADD_TIMER:
167                         self["key_green"].setText(_("Add timer"))
168                         self.key_green_choice = self.ADD_TIMER
169
170
171         def pageUp(self):
172                 self["epg_description"].pageUp()
173
174         def pageDown(self):
175                 self["epg_description"].pageDown()
176
177         def getSimilarEvents(self):
178          # search similar broadcastings
179                 refstr = str(self.currentService)
180                 id = self.event.getEventId()
181                 epgcache = eEPGCache.getInstance()
182                 ret = epgcache.search(('NB', 100, eEPGCache.SIMILAR_BROADCASTINGS_SEARCH, refstr, id))
183                 if ret is not None:
184                         descr = self["epg_description"]
185                         text = descr.getText()
186                         text += '\n\n' + _('Similar broadcasts:')
187                         ret.sort(self.sort_func)
188                         for x in ret:
189                                 t = localtime(x[1])
190                                 text += '\n%d.%d.%d, %02d:%02d  -  %s'%(t[2], t[1], t[0], t[3], t[4], x[0])
191                         descr.setText(text)
192                         self["key_red"].setText(_("Similar"))
193
194         def openSimilarList(self):
195                 if self.similarEPGCB is not None and self["key_red"].getText():
196                         id = self.event and self.event.getEventId()
197                         refstr = str(self.currentService)
198                         if id is not None:
199                                 self.similarEPGCB(id, refstr)
200
201 class EventViewSimple(Screen, EventViewBase):
202         def __init__(self, session, Event, Ref, callback=None, similarEPGCB=None):
203                 Screen.__init__(self, session)
204                 self.skinName = "EventView"
205                 EventViewBase.__init__(self, Event, Ref, callback, similarEPGCB)
206
207 class EventViewEPGSelect(Screen, EventViewBase):
208         def __init__(self, session, Event, Ref, callback=None, singleEPGCB=None, multiEPGCB=None, similarEPGCB=None):
209                 Screen.__init__(self, session)
210                 self.skinName = "EventView"
211                 EventViewBase.__init__(self, Event, Ref, callback, similarEPGCB)
212                 self["key_yellow"].setText(_("Single EPG"))
213                 self["key_blue"].setText(_("Multi EPG"))
214                 self["epgactions"] = ActionMap(["EventViewEPGActions"],
215                         {
216                                 "openSingleServiceEPG": singleEPGCB,
217                                 "openMultiServiceEPG": multiEPGCB,
218                         })