fix bsod when opening similar event list when no similar events exist
[enigma2.git] / lib / python / Screens / EventView.py
1 from Screen import Screen
2 from Components.ActionMap import ActionMap
3 from Components.Button import Button
4 from Components.Label import Label
5 from Components.ScrollLabel import ScrollLabel
6 from enigma import eServiceEventPtr, eEPGCachePtr, eEPGCache, eTimer
7 from ServiceReference import ServiceReference
8 from RecordTimer import RecordTimerEntry, parseEvent
9 from TimerEntry import TimerEntry
10 from time import localtime, asctime
11
12 class EventViewBase:
13         def __init__(self, Event, Ref, callback=None, similarEPGCB=None):
14                 self.similarEPGCB = similarEPGCB
15                 self.cbFunc = callback
16                 self.currentService=Ref
17                 self.isRecording = len(Ref.ref.getPath())
18                 self.event = Event
19                 self["epg_description"] = ScrollLabel()
20                 self["datetime"] = Label()
21                 self["channel"] = Label()
22                 self["duration"] = Label()
23                 self["key_red"] = Button("")
24                 if similarEPGCB is not None:
25                         self.SimilarBroadcastTimer = eTimer()
26                         self.SimilarBroadcastTimer.timeout.get().append(self.getSimilarEvents)
27                 else:
28                         self.SimilarBroadcastTimer = None
29                 if self.isRecording:
30                         self["key_green"] = Button("")
31                 else:
32                         self["key_green"] = Button(_("Add timer"))
33                 self["key_yellow"] = Button("")
34                 self["key_blue"] = Button("")
35                 self["actions"] = ActionMap(["OkCancelActions", "EventViewActions"],
36                         {
37                                 "cancel": self.close,
38                                 "ok": self.close,
39                                 "pageUp": self.pageUp,
40                                 "pageDown": self.pageDown,
41                                 "prevEvent": self.prevEvent,
42                                 "nextEvent": self.nextEvent,
43                                 "timerAdd": self.timerAdd,
44                                 "openSimilarList": self.openSimilarList
45                         })
46                 self.onShown.append(self.onCreate)
47
48         def onCreate(self):
49                 self.setService(self.currentService)
50                 self.setEvent(self.event)
51
52         def prevEvent(self):
53                 if self.cbFunc is not None:
54                         self.cbFunc(self.setEvent, self.setService, -1)
55
56         def nextEvent(self):
57                 if self.cbFunc is not None:
58                         self.cbFunc(self.setEvent, self.setService, +1)
59
60         def timerAdd(self):
61                 if not self.isRecording:
62                         newEntry = RecordTimerEntry(self.currentService, *parseEvent(self.event))
63                         self.session.openWithCallback(self.timerEditFinished, TimerEntry, newEntry)
64
65         def timerEditFinished(self, answer):
66                 if (answer[0]):
67                         self.session.nav.RecordTimer.record(answer[1])
68                 else:
69                         print "Timeredit aborted"
70
71         def setService(self, service):
72                 self.currentService=service
73                 if self.isRecording:
74                         self["channel"].setText(_("Recording"))
75                 else:
76                         name = self.currentService.getServiceName()
77                         if name is not None:
78                                 self["channel"].setText(name)
79                         else:
80                                 self["channel"].setText(_("unknown service"))
81
82         def sort_func(self,x,y):
83                 if x[1] < y[1]:
84                         return -1
85                 elif x[1] == y[1]:
86                         return 0
87                 else:
88                         return 1
89
90         def setEvent(self, event):
91                 self.event = event
92                 if event is None:
93                         return
94                 text = event.getEventName()
95                 short = event.getShortDescription()
96                 ext = event.getExtendedDescription()
97                 if len(short) > 0 and short != text:
98                         text = text + '\n\n' + short
99                 if len(ext) > 0:
100                         if len(text) > 0:
101                                 text = text + '\n\n'
102                         text = text + ext
103
104                 self.setTitle(event.getEventName())
105                 self["epg_description"].setText(text)
106                 self["datetime"].setText(event.getBeginTimeString())
107                 self["duration"].setText(_("%d min")%(event.getDuration()/60))
108                 self["key_red"].setText("")
109                 if self.SimilarBroadcastTimer is not None:
110                         self.SimilarBroadcastTimer.start(400,True)
111
112         def pageUp(self):
113                 self["epg_description"].pageUp()
114
115         def pageDown(self):
116                 self["epg_description"].pageDown()
117
118         def getSimilarEvents(self):
119          # search similar broadcastings
120                 refstr = str(self.currentService)
121                 id = self.event.getEventId()
122                 epgcache = eEPGCache.getInstance()
123                 ret = epgcache.search(('NB', 100, eEPGCache.SIMILAR_BROADCASTINGS_SEARCH, refstr, id))
124                 if ret is not None:
125                         descr = self["epg_description"]
126                         text = descr.getText()
127                         text += '\n\n' + _('Similar broadcasts:')
128                         ret.sort(self.sort_func)
129                         for x in ret:
130                                 t = localtime(x[1])
131                                 text += '\n%d.%d.%d, %02d:%02d  -  %s'%(t[2], t[1], t[0], t[3], t[4], x[0])
132                         descr.setText(text)
133                         self["key_red"].setText(_("Similar"))
134
135         def openSimilarList(self):
136                 if self.similarEPGCB is not None and len(self["key_red"].getText()):
137                         id = self.event and self.event.getEventId()
138                         refstr = str(self.currentService)
139                         if id is not None:
140                                 self.similarEPGCB(id, refstr)
141
142 class EventViewSimple(Screen, EventViewBase):
143         def __init__(self, session, Event, Ref, callback=None, similarEPGCB=None):
144                 Screen.__init__(self, session)
145                 self.skinName = "EventView"
146                 EventViewBase.__init__(self, Event, Ref, callback, similarEPGCB)
147
148 class EventViewEPGSelect(Screen, EventViewBase):
149         def __init__(self, session, Event, Ref, callback=None, singleEPGCB=None, multiEPGCB=None, similarEPGCB=None):
150                 Screen.__init__(self, session)
151                 self.skinName = "EventView"
152                 EventViewBase.__init__(self, Event, Ref, callback, similarEPGCB)
153                 self["key_yellow"].setText(_("Single EPG"))
154                 self["key_blue"].setText(_("Multi EPG"))
155                 self["epgactions"] = ActionMap(["EventViewEPGActions"],
156                         {
157                                 "openSingleServiceEPG": singleEPGCB,
158                                 "openMultiServiceEPG": multiEPGCB,
159                         })