small fix for avoiding a crash when calling epglist not from epgselection
[enigma2-plugins.git] / partnerbox / src / PartnerboxEPGList.py
1 #
2 #  Partnerbox E2
3 #
4 #  $Id$
5 #
6 #  Coded by Dr.Best (c) 2009
7 #  Support: www.dreambox-tools.info
8 #
9 #  This program is free software; you can redistribute it and/or
10 #  modify it under the terms of the GNU General Public License
11 #  as published by the Free Software Foundation; either version 2
12 #  of the License, or (at your option) any later version.
13 #
14 #  This program is distributed in the hope that it will be useful,
15 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 #  GNU General Public License for more details.
18 #
19
20 from Screens.Screen import Screen
21 from Components.config import config
22 from Components.EpgList import EPGList
23 from enigma import eListboxPythonMultiContent, \
24         RT_HALIGN_LEFT, RT_HALIGN_RIGHT, RT_VALIGN_CENTER, RT_HALIGN_CENTER
25
26 from time import localtime
27
28 from Tools.LoadPixmap import LoadPixmap
29 from Tools.BoundFunction import boundFunction
30 import PartnerboxFunctions as partnerboxfunctions
31
32 baseEPGList__init__ = None
33 basebuildSingleEntry = None
34 basebuildSimilarEntry = None
35 basebuildMultiEntry = None
36
37 def Partnerbox_EPGListInit():
38         global baseEPGList__init__, basebuildSingleEntry, basebuildSimilarEntry, basebuildMultiEntry
39         if baseEPGList__init__ is None:
40                 baseEPGList__init__ = EPGList.__init__
41         if basebuildSingleEntry is None:
42                 basebuildSingleEntry = EPGList.buildSingleEntry
43         if basebuildSimilarEntry is None:
44                 basebuildSimilarEntry = EPGList.buildSimilarEntry
45         if basebuildMultiEntry is None:
46                 basebuildMultiEntry = EPGList.buildMultiEntry
47         if partnerboxfunctions.remote_timer_list is None:
48                 partnerboxfunctions.remote_timer_list = []
49         EPGList.__init__ = Partnerbox_EPGList__init__
50         EPGList.buildSingleEntry = Partnerbox_SingleEntry
51         EPGList.buildSimilarEntry = Partnerbox_SimilarEntry
52         EPGList.buildMultiEntry = Partnerbox_MultiEntry
53
54 def Partnerbox_EPGList__init__(self, type=0, selChangedCB=None, timer = None):
55         baseEPGList__init__(self, type, selChangedCB, timer)
56         # Partnerbox Clock Icons
57         self.remote_clock_pixmap = LoadPixmap('/usr/lib/enigma2/python/Plugins/Extensions/Partnerbox/icons/remote_epgclock.png')
58         self.remote_clock_add_pixmap = LoadPixmap('/usr/lib/enigma2/python/Plugins/Extensions/Partnerbox/icons/remote_epgclock_add.png')
59         self.remote_clock_pre_pixmap = LoadPixmap('/usr/lib/enigma2/python/Plugins/Extensions/Partnerbox/icons/remote_epgclock_pre.png')
60         self.remote_clock_post_pixmap = LoadPixmap('/usr/lib/enigma2/python/Plugins/Extensions/Partnerbox/icons/remote_epgclock_post.png')
61         self.remote_clock_prepost_pixmap = LoadPixmap('/usr/lib/enigma2/python/Plugins/Extensions/Partnerbox/icons/remote_epgclock_prepost.png')
62
63 def Partnerbox_SingleEntry(self, service, eventId, beginTime, duration, EventName):
64         rec1=beginTime and (self.timer.isInTimer(eventId, beginTime, duration, service))
65         rec2=beginTime and (isInRemoteTimer(self,beginTime, duration, service))
66         r1=self.weekday_rect
67         r2=self.datetime_rect
68         r3=self.descr_rect
69         t = localtime(beginTime)
70         res = [
71                 None, # no private data needed
72                 (eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width(), r1.height(), 0, RT_HALIGN_RIGHT, self.days[t[6]]),
73                 (eListboxPythonMultiContent.TYPE_TEXT, r2.left(), r2.top(), r2.width(), r1.height(), 0, RT_HALIGN_RIGHT, "%02d.%02d, %02d:%02d"%(t[2],t[1],t[3],t[4]))
74         ]
75         if rec1 or rec2:
76                 if rec1:                        
77                         clock_pic = self.getClockPixmap(service, beginTime, duration, eventId)
78                         #eventuell auch in der Partnerbox
79                         if rec2:
80                                 clock_pic_partnerbox = getRemoteClockPixmap(self,service, beginTime, duration, eventId)
81                 else:
82                         clock_pic = getRemoteClockPixmap(self,service, beginTime, duration, eventId)
83                 if rec1 and rec2:
84                         # wenn sowohl lokal als auch auf Partnerbox
85                         res.extend((
86                                 (eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r3.left(), r3.top(), 21, 21, clock_pic),
87                                 (eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r3.left() + 25, r3.top(), 21, 21, clock_pic_partnerbox),
88                                 (eListboxPythonMultiContent.TYPE_TEXT, r3.left() + 50, r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName)))
89                 else:
90                         res.extend((
91                                 (eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r3.left(), r3.top(), 21, 21, clock_pic),
92                                 (eListboxPythonMultiContent.TYPE_TEXT, r3.left() + 25, r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName)))
93         else:
94                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName))
95         return res
96
97
98 def Partnerbox_SimilarEntry(self, service, eventId, beginTime, service_name, duration):
99         rec1=beginTime and (self.timer.isInTimer(eventId, beginTime, duration, service))
100         rec2=beginTime and (isInRemoteTimer(self,beginTime, duration, service))
101         r1=self.weekday_rect
102         r2=self.datetime_rect
103         r3=self.service_rect
104         t = localtime(beginTime)
105         res = [
106                 None,  # no private data needed
107                 (eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width(), r1.height(), 0, RT_HALIGN_RIGHT, self.days[t[6]]),
108                 (eListboxPythonMultiContent.TYPE_TEXT, r2.left(), r2.top(), r2.width(), r1.height(), 0, RT_HALIGN_RIGHT, "%02d.%02d, %02d:%02d"%(t[2],t[1],t[3],t[4]))
109         ]
110         if rec1 or rec2:
111                 if rec1:                        
112                         clock_pic = self.getClockPixmap(service, beginTime, duration, eventId)
113                         #eventuell auch in der Partnerbox
114                         if rec2:
115                                 clock_pic_partnerbox = getRemoteClockPixmap(self,service, beginTime, duration, eventId)
116                 else:
117                         clock_pic = getRemoteClockPixmap(self,service, beginTime, duration, eventId)
118                 if rec1 and rec2:
119                         # wenn sowohl lokal als auch auf Partnerbox
120                         res.extend((
121                                 (eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r3.left(), r3.top(), 21, 21, clock_pic),
122                                 (eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r3.left() + 25, r3.top(), 21, 21, clock_pic_partnerbox),
123                                 (eListboxPythonMultiContent.TYPE_TEXT, r3.left() + 50, r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, service_name)
124                         ))
125                 else:
126                         res.extend((
127                                 (eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r3.left(), r3.top(), 21, 21, clock_pic),
128                                 (eListboxPythonMultiContent.TYPE_TEXT, r3.left() + 25, r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, service_name)
129                         ))
130         else:
131                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, service_name))
132         return res
133
134 def Partnerbox_MultiEntry(self, changecount, service, eventId, begTime, duration, EventName, nowTime, service_name):
135         # so wie es aussieht wird der MultiEPG ueber ein Plugin angefahren...lasse den Code dennoch mal drinnen...
136         rec1=begTime and (self.timer.isInTimer(eventId, begTime, duration, service))
137         rec2=begTime and (isInRemoteTimer(self,begTime, duration, service))
138         r1=self.service_rect
139         r2=self.progress_rect
140         r3=self.descr_rect
141         r4=self.start_end_rect
142         res = [ None ] # no private data needed
143         if rec1 or rec2:
144                 if rec1:                        
145                         clock_pic = self.getClockPixmap(service, begTime, duration, eventId)
146                         #eventuell auch in der Partnerbox
147                         if rec2:
148                                 clock_pic_partnerbox = getRemoteClockPixmap(self,service, begTime, duration, eventId)
149                 else:
150                         clock_pic = getRemoteClockPixmap(self,service, begTime, duration, eventId)
151                 if rec1 and rec2:
152                         # wenn sowohl lokal als auch auf Partnerbox
153                         res.extend((
154                                 (eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width()-41, r1.height(), 0, RT_HALIGN_LEFT, service_name),
155                                 (eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r1.left()+r1.width()-32, r1.top(), 21, 21, clock_pic),
156                                 (eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r1.left()+r1.width()-16, r1.top(), 21, 21, clock_pic_partnerbox)
157                         ))
158                 else:
159                         res.extend((
160                                 (eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width()-21, r1.height(), 0, RT_HALIGN_LEFT, service_name),
161                                 (eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, r1.left()+r1.width()-16, r1.top(), 21, 21, clock_pic)
162                         ))
163         else:
164                 res.append((eListboxPythonMultiContent.TYPE_TEXT, r1.left(), r1.top(), r1.width(), r1.height(), 0, RT_HALIGN_LEFT, service_name))
165         if begTime is not None:
166                 if nowTime < begTime:
167                         begin = localtime(begTime)
168                         end = localtime(begTime+duration)
169                         res.extend((
170                                 (eListboxPythonMultiContent.TYPE_TEXT, r4.left(), r4.top(), r4.width(), r4.height(), 1, RT_HALIGN_CENTER|RT_VALIGN_CENTER, "%02d.%02d - %02d.%02d"%(begin[3],begin[4],end[3],end[4])),
171                                 (eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName)
172                         ))
173                 else:
174                         percent = (nowTime - begTime) * 100 / duration
175                         res.extend((
176                                 (eListboxPythonMultiContent.TYPE_PROGRESS, r2.left(), r2.top(), r2.width(), r2.height(), percent),
177                                 (eListboxPythonMultiContent.TYPE_TEXT, r3.left(), r3.top(), r3.width(), r3.height(), 0, RT_HALIGN_LEFT, EventName)
178                         ))
179         return res
180
181
182
183 def isInRemoteTimer(self, begin, duration, service):
184         time_match = 0
185         chktime = None
186         chktimecmp = None
187         chktimecmp_end = None
188         end = begin + duration
189         for x in partnerboxfunctions.remote_timer_list:
190                 if x.servicereference.upper() == service.upper():
191                         if x.repeated != 0:
192                                 if chktime is None:
193                                         chktime = localtime(begin)
194                                         chktimecmp = chktime.tm_wday * 1440 + chktime.tm_hour * 60 + chktime.tm_min
195                                         chktimecmp_end = chktimecmp + (duration / 60)
196                                 time = localtime(x.timebegin)
197                                 for y in range(7):
198                                         if x.repeated & (2 ** y):
199                                                 timecmp = y * 1440 + time.tm_hour * 60 + time.tm_min
200                                                 if timecmp <= chktimecmp < (timecmp + ((x.timeend - x.timebegin) / 60)):
201                                                         time_match = ((timecmp + ((x.timeend - x.timebegin) / 60)) - chktimecmp) * 60
202                                                 elif chktimecmp <= timecmp < chktimecmp_end:
203                                                         time_match = (chktimecmp_end - timecmp) * 60
204                         else:
205                                 if begin <= x.timebegin <= end:
206                                         diff = end - x.timebegin
207                                         if time_match < diff:
208                                                 time_match = diff
209                                 elif x.timebegin <= begin <= x.timeend:
210                                         diff = x.timeend - begin
211                                         if time_match < diff:
212                                                 time_match = diff
213                         if time_match:
214                                 break
215         return time_match
216
217
218 def getRemoteClockPixmap(self, refstr, beginTime, duration, eventId):
219         pre_clock = 1
220         post_clock = 2
221         clock_type = 0
222         endTime = beginTime + duration
223         for x in partnerboxfunctions.remote_timer_list:
224                 if x.servicereference.upper() == refstr.upper():
225                         if x.eventId == eventId:
226                                 return self.remote_clock_pixmap
227                         beg = x.timebegin
228                         end = x.timeend
229                         if beginTime > beg and beginTime < end and endTime > end:
230                                 clock_type |= pre_clock
231                         elif beginTime < beg and endTime > beg and endTime < end:
232                                 clock_type |= post_clock
233         if clock_type == 0:
234                 return self.remote_clock_add_pixmap
235         elif clock_type == pre_clock:
236                 return self.remote_clock_pre_pixmap
237         elif clock_type == post_clock:
238                 return self.remote_clock_post_pixmap
239         else:
240                 return self.remote_clock_prepost_pixmap
241