show similar broadcastings deferred in eventview
[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                 text = event.getEventName()
93                 short = event.getShortDescription()
94                 ext = event.getExtendedDescription()
95                 if len(short) > 0 and short != text:
96                         text = text + '\n\n' + short
97                 if len(ext) > 0:
98                         if len(text) > 0:
99                                 text = text + '\n\n'
100                         text = text + ext
101
102                 self.setTitle(event.getEventName())
103                 self["epg_description"].setText(text)
104                 self["datetime"].setText(event.getBeginTimeString())
105                 self["duration"].setText(_("%d min")%(event.getDuration()/60))
106                 self["key_red"].setText("")
107                 if self.SimilarBroadcastTimer is not None:
108                         self.SimilarBroadcastTimer.start(400,True)
109
110         def pageUp(self):
111                 self["epg_description"].pageUp()
112
113         def pageDown(self):
114                 self["epg_description"].pageDown()
115
116         def getSimilarEvents(self):
117          # search similar broadcastings
118                 refstr = str(self.currentService)
119                 id = self.event.getEventId()
120                 epgcache = eEPGCache.getInstance()
121                 ret = epgcache.search(('NB', 100, eEPGCache.SIMILAR_BROADCASTINGS_SEARCH, refstr, id))
122                 if ret is not None:
123                         descr = self["epg_description"]
124                         text = descr.getText()
125                         text += '\n\n' + _('Similar broadcasts:')
126                         ret.sort(self.sort_func)
127                         for x in ret:
128                                 t = localtime(x[1])
129                                 text += '\n%d.%d.%d, %02d:%02d  -  %s'%(t[2], t[1], t[0], t[3], t[4], x[0])
130                         descr.setText(text)
131                         self["key_red"].setText(_("Similar"))
132
133         def openSimilarList(self):
134                 if self.similarEPGCB is not None:
135                         id = self.event and self.event.getEventId()
136                         refstr = str(self.currentService)
137                         if id is not None:
138                                 self.similarEPGCB(id, refstr)
139
140 class EventViewSimple(Screen, EventViewBase):
141         def __init__(self, session, Event, Ref, callback=None, similarEPGCB=None):
142                 Screen.__init__(self, session)
143                 self.skinName = "EventView"
144                 EventViewBase.__init__(self, Event, Ref, callback, similarEPGCB)
145
146 class EventViewEPGSelect(Screen, EventViewBase):
147         def __init__(self, session, Event, Ref, callback=None, singleEPGCB=None, multiEPGCB=None, similarEPGCB=None):
148                 Screen.__init__(self, session)
149                 self.skinName = "EventView"
150                 EventViewBase.__init__(self, Event, Ref, callback, similarEPGCB)
151                 self["key_yellow"].setText(_("Single EPG"))
152                 self["key_blue"].setText(_("Multi EPG"))
153                 self["epgactions"] = ActionMap(["EventViewEPGActions"],
154                         {
155                                 "openSingleServiceEPG": singleEPGCB,
156                                 "openMultiServiceEPG": multiEPGCB,
157                         })