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