Instantly hide services when you add them to the blacklist
[openblackhole/openblackhole-enigma2.git] / lib / python / Components / TimerList.py
1 from HTMLComponent import HTMLComponent
2 from GUIComponent import GUIComponent
3 from skin import parseFont
4
5 from Tools.FuzzyDate import FuzzyTime
6
7 from enigma import eListboxPythonMultiContent, eListbox, gFont,\
8         RT_HALIGN_LEFT, RT_HALIGN_RIGHT, RT_VALIGN_CENTER, RT_VALIGN_TOP, RT_VALIGN_BOTTOM
9 from Tools.Alternatives import GetWithAlternative
10 from Tools.LoadPixmap import LoadPixmap
11 from Tools.TextBoundary import getTextBoundarySize
12 from timer import TimerEntry
13 from Tools.Directories import resolveFilename, SCOPE_CURRENT_SKIN, SCOPE_SKIN_IMAGE
14
15 class TimerList(HTMLComponent, GUIComponent, object):
16 #
17 #  | <Name of the Timer>     <Service>  |
18 #  | <state>  <orb.pos.>  <start, end>  |
19 #
20         def buildTimerEntry(self, timer, processed):
21                 width = self.l.getItemSize().width()
22                 res = [ None ]
23                 serviceName = timer.service_ref.getServiceName()
24
25                 serviceNameWidth = getTextBoundarySize(self.instance, self.serviceNameFont, self.l.getItemSize(), serviceName).width()
26                 if 200 > width - serviceNameWidth - self.iconWidth - self.iconMargin:
27                         serviceNameWidth = width - 200 - self.iconWidth - self.iconMargin
28
29                 res.append((eListboxPythonMultiContent.TYPE_TEXT, width - serviceNameWidth, 0, serviceNameWidth, self.rowSplit, 0, RT_HALIGN_RIGHT|RT_VALIGN_BOTTOM, serviceName))
30                 res.append((eListboxPythonMultiContent.TYPE_TEXT, self.iconWidth + self.iconMargin, 0, width - serviceNameWidth - self.iconWidth - self.iconMargin, self.rowSplit, 2, RT_HALIGN_LEFT|RT_VALIGN_BOTTOM, timer.name))
31
32                 begin = FuzzyTime(timer.begin)
33                 if timer.repeated:
34                         days = ( _("Mon"), _("Tue"), _("Wed"), _("Thu"), _("Fri"), _("Sat"), _("Sun") )
35                         repeatedtext = []
36                         flags = timer.repeated
37                         for x in (0, 1, 2, 3, 4, 5, 6):
38                                 if (flags & 1 == 1):
39                                         repeatedtext.append(days[x])
40                                 flags = flags >> 1
41                         repeatedtext = ", ".join(repeatedtext)
42                         if self.iconRepeat:
43                                 res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, self.iconMargin / 2, self.rowSplit + (self.itemHeight - self.rowSplit - self.iconHeight) / 2, self.iconWidth, self.iconHeight, self.iconRepeat))
44                 else:
45                         repeatedtext = begin[0] # date
46                 if timer.justplay:
47                         text = repeatedtext + ((" %s "+ _("(ZAP)")) % (begin[1]))
48                 else:
49                         text = repeatedtext + ((" %s ... %s (%d " + _("mins") + ")") % (begin[1], FuzzyTime(timer.end)[1], (timer.end - timer.begin) / 60))
50                 icon = None
51                 if not processed:
52                         if timer.state == TimerEntry.StateWaiting:
53                                 state = _("waiting")
54                                 icon = self.iconWait
55                         elif timer.state == TimerEntry.StatePrepared:
56                                 state = _("about to start")
57                                 icon = self.iconPrepared
58                         elif timer.state == TimerEntry.StateRunning:
59                                 if timer.justplay:
60                                         state = _("zapped")
61                                         icon = self.iconZapped
62                                 else:
63                                         state = _("recording...")
64                                         icon = self.iconRecording
65                         elif timer.state == TimerEntry.StateEnded:
66                                 state = _("done!")
67                                 icon = self.iconDone
68                         else:
69                                 state = _("<unknown>")
70                                 icon = None
71                 elif timer.disabled:
72                         state = _("disabled")
73                         icon = self.iconDisabled
74                 else:
75                         state = _("done!")
76                         icon = self.iconDone
77
78                 icon and res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, self.iconMargin / 2, (self.rowSplit - self.iconHeight) / 2, self.iconWidth, self.iconHeight, icon))
79                 orbpos = self.getOrbitalPos(timer.service_ref)
80                 orbposWidth = getTextBoundarySize(self.instance, self.font, self.l.getItemSize(), orbpos).width()
81                 res.append((eListboxPythonMultiContent.TYPE_TEXT, self.satPosLeft, self.rowSplit, orbposWidth, self.itemHeight - self.rowSplit, 1, RT_HALIGN_LEFT|RT_VALIGN_TOP, orbpos))
82                 res.append((eListboxPythonMultiContent.TYPE_TEXT, self.iconWidth + self.iconMargin, self.rowSplit, self.satPosLeft - self.iconWidth - self.iconMargin, self.itemHeight - self.rowSplit, 1, RT_HALIGN_LEFT|RT_VALIGN_TOP, state))
83                 res.append((eListboxPythonMultiContent.TYPE_TEXT, self.satPosLeft + orbposWidth, self.rowSplit, width - self.satPosLeft - orbposWidth, self.itemHeight - self.rowSplit, 1, RT_HALIGN_RIGHT|RT_VALIGN_TOP, text))
84                 return res
85
86         def __init__(self, list):
87                 GUIComponent.__init__(self)
88                 self.l = eListboxPythonMultiContent()
89                 self.l.setBuildFunc(self.buildTimerEntry)
90                 self.serviceNameFont = gFont("Regular", 20)
91                 self.font = gFont("Regular", 18)
92                 self.eventNameFont = gFont("Regular", 18)
93                 self.l.setList(list)
94                 self.itemHeight = 50
95                 self.rowSplit = 25
96                 self.iconMargin = 4
97                 self.satPosLeft = 160
98                 self.iconWait = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/icons/timer_wait.png"))
99                 #currently intended that all icons have the same size
100                 self.iconWidth = self.iconWait.size().width()
101                 self.iconHeight = self.iconWait.size().height()
102                 self.iconRecording = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/icons/timer_rec.png"))
103                 self.iconPrepared = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/icons/timer_prep.png"))
104                 self.iconDone = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/icons/timer_done.png"))
105                 self.iconRepeat = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/icons/timer_rep.png"))
106                 self.iconZapped = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/icons/timer_zap.png"))
107                 self.iconDisabled = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/icons/timer_off.png"))
108
109         def applySkin(self, desktop, parent):
110                 def itemHeight(value):
111                         self.itemHeight = int(value)
112                 def setServiceNameFont(value):
113                         self.serviceNameFont = parseFont(value, ((1,1),(1,1)))
114                 def setEventNameFont(value):
115                         self.eventNameFont = parseFont(value, ((1,1),(1,1)))
116                 def setFont(value):
117                         self.font = parseFont(value, ((1,1),(1,1)))
118                 def rowSplit(value):
119                         self.rowSplit = int(value)
120                 def iconMargin(value):
121                         self.iconMargin = int(value)
122                 def satPosLeft(value):
123                         self.satPosLeft = int(value)
124                 for (attrib, value) in list(self.skinAttributes):
125                         try:
126                                 locals().get(attrib)(value)
127                                 self.skinAttributes.remove((attrib, value))
128                         except:
129                                 pass
130                 self.l.setItemHeight(self.itemHeight)
131                 self.l.setFont(0, self.serviceNameFont)
132                 self.l.setFont(1, self.font)
133                 self.l.setFont(2, self.eventNameFont)
134                 return GUIComponent.applySkin(self, desktop, parent)
135
136         def getCurrent(self):
137                 cur = self.l.getCurrentSelection()
138                 return cur and cur[0]
139
140         GUI_WIDGET = eListbox
141
142         def postWidgetCreate(self, instance):
143                 instance.setContent(self.l)
144                 self.instance = instance
145                 instance.setWrapAround(True)
146
147         def moveToIndex(self, index):
148                 self.instance.moveSelectionTo(index)
149
150         def getCurrentIndex(self):
151                 return self.instance.getCurrentIndex()
152
153         currentIndex = property(getCurrentIndex, moveToIndex)
154         currentSelection = property(getCurrent)
155
156         def moveDown(self):
157                 self.instance.moveSelection(self.instance.moveDown)
158
159         def invalidate(self):
160                 self.l.invalidate()
161
162         def entryRemoved(self, idx):
163                 self.l.entryRemoved(idx)
164
165         def getOrbitalPos(self, ref):
166                 refstr = ''
167                 if hasattr(ref, 'sref'):
168                         refstr = str(ref.sref)
169                 else:
170                         refstr = str(ref)
171                 refstr = refstr and GetWithAlternative(refstr)
172                 if '%3a//' in refstr:
173                         return "%s" % _("Stream")
174                 op = int(refstr.split(':', 10)[6][:-4] or "0",16)
175                 if op == 0xeeee:
176                         return "%s" % _("DVB-T")
177                 if op == 0xffff:
178                         return "%s" % _("DVB-C")
179                 direction = 'E'
180                 if op > 1800:
181                         op = 3600 - op
182                         direction = 'W'
183                 return ("%d.%d\xc2\xb0%s") % (op // 10, op % 10, direction)