take care of scrollbar size when build movielist and timerlist entries
[enigma2.git] / lib / python / Components / MovieList.py
1 from GUIComponent import GUIComponent
2 from Tools.FuzzyDate import FuzzyTime
3 from ServiceReference import ServiceReference
4 from Components.MultiContent import MultiContentEntryText
5
6 from enigma import eListboxPythonMultiContent, eListbox, gFont, iServiceInformation, \
7         RT_HALIGN_LEFT, RT_HALIGN_RIGHT, eServiceReference, eServiceCenter
8
9 class MovieList(GUIComponent):
10         def __init__(self, root):
11                 GUIComponent.__init__(self)
12                 self.l = eListboxPythonMultiContent()
13                 self.tags = set()
14                 if root is not None:
15                         self.reload(root)
16                 self.l.setFont(0, gFont("Regular", 22))
17                 self.l.setFont(1, gFont("Regular", 18))
18                 self.l.setFont(2, gFont("Regular", 16))
19                 self.l.setBuildFunc(self.buildMovieListEntry)
20                 self.l.setItemHeight(75)
21
22         #
23         # | name of movie              |
24         #
25         def buildMovieListEntry(self, serviceref, info, begin, len):
26                 if serviceref.flags & eServiceReference.mustDescent:
27                         return None
28                 width = self.entry_width
29
30                 if len <= 0: #recalc len when not already done
31                         cur_idx = self.l.getCurrentSelectionIndex()
32                         x = self.list[cur_idx]
33                         len = x[1].getLength(x[0]) #recalc the movie length...
34                         self.list[cur_idx] = (x[0], x[1], x[2], len) #update entry in list... so next time we don't need to recalc
35
36                 if len > 0:
37                         len = "%d:%02d" % (len / 60, len % 60)
38                 else:
39                         len = "?:??"
40
41                 res = [ None ]
42
43                 res.append(MultiContentEntryText(pos=(0, 0), size=(width-140, 30), font = 0, flags = RT_HALIGN_LEFT, text = info.getName(serviceref)))
44                 service = ServiceReference(info.getInfoString(serviceref, iServiceInformation.sServiceref))
45                 if service is not None:
46                         res.append(MultiContentEntryText(pos=(width-140, 0), size=(140, 30), font = 2, flags = RT_HALIGN_RIGHT, text = service.getServiceName()))
47
48                 description = info.getInfoString(serviceref, iServiceInformation.sDescription)
49
50                 begin_string = ""
51                 if begin > 0:
52                         t = FuzzyTime(begin)
53                         begin_string = t[0] + ", " + t[1]
54
55                 res.append(MultiContentEntryText(pos=(0, 30), size=(width, 20), font=1, flags=RT_HALIGN_LEFT, text=description))
56                 res.append(MultiContentEntryText(pos=(0, 50), size=(width-270, 20), font=1, flags=RT_HALIGN_LEFT, text=begin_string))
57                 res.append(MultiContentEntryText(pos=(width, 50), size=(270, 20), font=1, flags=RT_HALIGN_RIGHT, text=len))
58
59                 return res
60
61         def moveToIndex(self, index):
62                 self.instance.moveSelectionTo(index)
63
64         def getCurrentIndex(self):
65                 return self.instance.getCurrentIndex()
66
67         def getCurrentEvent(self):
68                 l = self.l.getCurrentSelection()
69                 return l and l[0] and l[1] and l[1].getEvent(l[0])
70
71         def getCurrent(self):
72                 l = self.l.getCurrentSelection()
73                 return l and l[0]
74
75         GUI_WIDGET = eListbox
76         
77         def postWidgetCreate(self, instance):
78                 instance.setContent(self.l)
79                 self.entry_width = self.l.getItemSize().width()
80
81         def reload(self, root = None, filter_tags = None):
82                 if root is not None:
83                         self.load(root, filter_tags)
84                 else:
85                         self.load(self.root, filter_tags)
86                 self.l.setList(self.list)
87                 self.entry_width = self.l.getItemSize().width()
88
89         def removeService(self, service):
90                 for l in self.list[:]:
91                         if l[0] == service:
92                                 self.list.remove(l)
93                 self.l.setList(self.list)
94                 self.entry_width = self.l.getItemSize().width()
95
96         def __len__(self):
97                 return len(self.list)
98
99         def load(self, root, filter_tags):
100                 # this lists our root service, then building a 
101                 # nice list
102                 
103                 self.list = [ ]
104                 self.root = root
105                 
106                 self.serviceHandler = eServiceCenter.getInstance()
107                 list = self.serviceHandler.list(root)
108                 
109                 tags = set()
110                 
111                 if list is None:
112                         raise "listing of movies failed"
113
114                 while 1:
115                         serviceref = list.getNext()
116                         if not serviceref.valid():
117                                 break
118                         if serviceref.flags & eServiceReference.mustDescent:
119                                 continue
120                         info = self.serviceHandler.info(serviceref)
121                         if info is None:
122                                 continue
123                         begin = info.getInfo(serviceref, iServiceInformation.sTimeCreate)
124                         
125                         # convert space-seperated list of tags into a set
126                         this_tags = info.getInfoString(serviceref, iServiceInformation.sTags).split(' ')
127                         if this_tags == ['']:
128                                 this_tags = []
129                         this_tags = set(this_tags)
130                         
131                         # filter_tags is either None (which means no filter at all), or 
132                         # a set. In this case, all elements of filter_tags must be present,
133                         # otherwise the entry will be dropped.                  
134                         if filter_tags is not None and not this_tags.issuperset(filter_tags):
135                                 continue
136                         
137                         tags |= this_tags
138                         self.list.append((serviceref, info, begin, -1))
139                 
140                 # sort: key is 'begin'
141                 self.list.sort(key=lambda x: -x[2])
142                 
143                 # finally, store a list of all tags which were found. these can be presented
144                 # to the user to filter the list
145                 self.tags = tags
146
147         def moveTo(self, serviceref):
148                 found = 0
149                 count = 0
150                 for x in self.list:
151                         if x[0] == serviceref:
152                                 found = count
153                         count += 1
154                 self.instance.moveSelectionTo(found)
155
156         def moveDown(self):
157                 self.instance.moveSelection(self.instance.moveDown)