fix no more turning positioner after leave positioner setup in some cases
[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 from Components.config import config
6
7 from enigma import eListboxPythonMultiContent, eListbox, gFont, iServiceInformation, \
8         RT_HALIGN_LEFT, RT_HALIGN_RIGHT, eServiceReference, eServiceCenter
9
10 class MovieList(GUIComponent):
11         SORT_ALPHANUMERIC = 1
12         SORT_RECORDED = 2
13
14         LISTTYPE_ORIGINAL = 1
15         LISTTYPE_COMPACT_DESCRIPTION = 2
16         LISTTYPE_COMPACT = 3
17         LISTTYPE_MINIMAL = 4
18
19         HIDE_DESCRIPTION = 1
20         SHOW_DESCRIPTION = 2
21
22         def __init__(self, root, list_type=None, sort_type=None, descr_state=None):
23                 GUIComponent.__init__(self)
24                 self.list_type = list_type or self.LISTTYPE_ORIGINAL
25                 self.descr_state = descr_state or self.HIDE_DESCRIPTION
26                 self.sort_type = sort_type or self.SORT_RECORDED
27
28                 self.l = eListboxPythonMultiContent()
29                 self.tags = set()
30                 
31                 if root is not None:
32                         self.reload(root)
33                 
34                 self.redrawList()
35                 self.l.setBuildFunc(self.buildMovieListEntry)
36                 
37                 self.onSelectionChanged = [ ]
38
39         def connectSelChanged(self, fnc):
40                 if not fnc in self.onSelectionChanged:
41                         self.onSelectionChanged.append(fnc)
42
43         def disconnectSelChanged(self, fnc):
44                 if fnc in self.onSelectionChanged:
45                         self.onSelectionChanged.remove(fnc)
46
47         def selectionChanged(self):
48                 for x in self.onSelectionChanged:
49                         x()
50
51         def setListType(self, type):
52                 self.list_type = type
53
54         def setDescriptionState(self, val):
55                 self.descr_state = val
56
57         def setSortType(self, type):
58                 self.sort_type = type
59
60         def redrawList(self):
61                 if self.list_type == MovieList.LISTTYPE_ORIGINAL:
62                         self.l.setFont(0, gFont("Regular", 22))
63                         self.l.setFont(1, gFont("Regular", 18))
64                         self.l.setFont(2, gFont("Regular", 16))
65                         self.l.setItemHeight(75)
66                 elif self.list_type == MovieList.LISTTYPE_COMPACT_DESCRIPTION or self.list_type == MovieList.LISTTYPE_COMPACT:
67                         self.l.setFont(0, gFont("Regular", 20))
68                         self.l.setFont(1, gFont("Regular", 14))
69                         self.l.setItemHeight(37)
70                 else:
71                         self.l.setFont(0, gFont("Regular", 20))
72                         self.l.setFont(1, gFont("Regular", 16))
73                         self.l.setItemHeight(25)
74
75         #
76         # | name of movie              |
77         #
78         def buildMovieListEntry(self, serviceref, info, begin, len):
79                 if serviceref.flags & eServiceReference.mustDescent:
80                         return None
81
82                 width = self.l.getItemSize().width()
83
84                 if len <= 0: #recalc len when not already done
85                         cur_idx = self.l.getCurrentSelectionIndex()
86                         x = self.list[cur_idx]
87                         if config.usage.load_length_of_movies_in_moviellist.value:
88                                 len = x[1].getLength(x[0]) #recalc the movie length...
89                         else:
90                                 len = 0 #dont recalc movielist to speedup loading the list
91                         self.list[cur_idx] = (x[0], x[1], x[2], len) #update entry in list... so next time we don't need to recalc
92                 
93                 if len > 0:
94                         len = "%d:%02d" % (len / 60, len % 60)
95                 else:
96                         len = ""
97                 
98                 res = [ None ]
99                 
100                 txt = info.getName(serviceref)
101                 service = ServiceReference(info.getInfoString(serviceref, iServiceInformation.sServiceref))
102                 description = info.getInfoString(serviceref, iServiceInformation.sDescription)
103                 tags = info.getInfoString(serviceref, iServiceInformation.sTags)
104
105                 begin_string = ""
106                 if begin > 0:
107                         t = FuzzyTime(begin)
108                         begin_string = t[0] + ", " + t[1]
109                 
110                 if self.list_type == MovieList.LISTTYPE_ORIGINAL:
111                         res.append(MultiContentEntryText(pos=(0, 0), size=(width-182, 30), font = 0, flags = RT_HALIGN_LEFT, text=txt))
112                         if self.tags:
113                                 res.append(MultiContentEntryText(pos=(width-180, 0), size=(180, 30), font = 2, flags = RT_HALIGN_RIGHT, text = tags))
114                                 if service is not None:
115                                         res.append(MultiContentEntryText(pos=(200, 50), size=(200, 20), font = 1, flags = RT_HALIGN_LEFT, text = service.getServiceName()))
116                         else:
117                                 if service is not None:
118                                         res.append(MultiContentEntryText(pos=(width-180, 0), size=(180, 30), font = 2, flags = RT_HALIGN_RIGHT, text = service.getServiceName()))
119                         res.append(MultiContentEntryText(pos=(0, 30), size=(width, 20), font=1, flags=RT_HALIGN_LEFT, text=description))
120                         res.append(MultiContentEntryText(pos=(0, 50), size=(200, 20), font=1, flags=RT_HALIGN_LEFT, text=begin_string))
121                         res.append(MultiContentEntryText(pos=(width-200, 50), size=(198, 20), font=1, flags=RT_HALIGN_RIGHT, text=len))
122                 elif self.list_type == MovieList.LISTTYPE_COMPACT_DESCRIPTION:
123                         res.append(MultiContentEntryText(pos=(0, 0), size=(width-120, 20), font = 0, flags = RT_HALIGN_LEFT, text = txt))
124                         res.append(MultiContentEntryText(pos=(0, 20), size=(width-212, 17), font=1, flags=RT_HALIGN_LEFT, text=description))
125                         res.append(MultiContentEntryText(pos=(width-120, 6), size=(120, 20), font=1, flags=RT_HALIGN_RIGHT, text=begin_string))
126                         if service is not None:
127                                 res.append(MultiContentEntryText(pos=(width-212, 20), size=(154, 17), font = 1, flags = RT_HALIGN_RIGHT, text = service.getServiceName()))
128                         res.append(MultiContentEntryText(pos=(width-58, 20), size=(58, 20), font=1, flags=RT_HALIGN_RIGHT, text=len))
129                 elif self.list_type == MovieList.LISTTYPE_COMPACT:
130                         res.append(MultiContentEntryText(pos=(0, 0), size=(width-77, 20), font = 0, flags = RT_HALIGN_LEFT, text = txt))
131                         if self.tags:
132                                 res.append(MultiContentEntryText(pos=(width-200, 20), size=(200, 17), font = 1, flags = RT_HALIGN_RIGHT, text = tags))
133                                 if service is not None:
134                                         res.append(MultiContentEntryText(pos=(200, 20), size=(200, 17), font = 1, flags = RT_HALIGN_LEFT, text = service.getServiceName()))
135                         else:
136                                 if service is not None:
137                                         res.append(MultiContentEntryText(pos=(width-200, 20), size=(200, 17), font = 1, flags = RT_HALIGN_RIGHT, text = service.getServiceName()))
138                         res.append(MultiContentEntryText(pos=(0, 20), size=(200, 17), font=1, flags=RT_HALIGN_LEFT, text=begin_string))
139                         res.append(MultiContentEntryText(pos=(width-75, 0), size=(75, 20), font=0, flags=RT_HALIGN_RIGHT, text=len))
140                 else:
141                         assert(self.list_type == MovieList.LISTTYPE_MINIMAL)
142                         if self.descr_state == MovieList.SHOW_DESCRIPTION:
143                                 res.append(MultiContentEntryText(pos=(0, 0), size=(width-146, 20), font = 0, flags = RT_HALIGN_LEFT, text = txt))
144                                 res.append(MultiContentEntryText(pos=(width-145, 4), size=(145, 20), font=1, flags=RT_HALIGN_RIGHT, text=begin_string))
145                         else:
146                                 res.append(MultiContentEntryText(pos=(0, 0), size=(width-77, 20), font = 0, flags = RT_HALIGN_LEFT, text = txt))
147                                 res.append(MultiContentEntryText(pos=(width-75, 0), size=(75, 20), font=0, flags=RT_HALIGN_RIGHT, text=len))
148                 
149                 return res
150
151         def moveToIndex(self, index):
152                 self.instance.moveSelectionTo(index)
153
154         def getCurrentIndex(self):
155                 return self.instance.getCurrentIndex()
156
157         def getCurrentEvent(self):
158                 l = self.l.getCurrentSelection()
159                 return l and l[0] and l[1] and l[1].getEvent(l[0])
160
161         def getCurrent(self):
162                 l = self.l.getCurrentSelection()
163                 return l and l[0]
164
165         GUI_WIDGET = eListbox
166
167         def postWidgetCreate(self, instance):
168                 instance.setContent(self.l)
169                 instance.selectionChanged.get().append(self.selectionChanged)
170
171         def preWidgetRemove(self, instance):
172                 instance.setContent(None)
173                 instance.selectionChanged.get().remove(self.selectionChanged)
174
175         def reload(self, root = None, filter_tags = None):
176                 if root is not None:
177                         self.load(root, filter_tags)
178                 else:
179                         self.load(self.root, filter_tags)
180                 self.l.setList(self.list)
181
182         def removeService(self, service):
183                 for l in self.list[:]:
184                         if l[0] == service:
185                                 self.list.remove(l)
186                 self.l.setList(self.list)
187
188         def __len__(self):
189                 return len(self.list)
190
191         def load(self, root, filter_tags):
192                 # this lists our root service, then building a 
193                 # nice list
194                 
195                 self.list = [ ]
196                 self.serviceHandler = eServiceCenter.getInstance()
197                 
198                 self.root = root
199                 list = self.serviceHandler.list(root)
200                 if list is None:
201                         print "listing of movies failed"
202                         list = [ ]      
203                         return
204                 tags = set()
205                 
206                 while 1:
207                         serviceref = list.getNext()
208                         if not serviceref.valid():
209                                 break
210                         if serviceref.flags & eServiceReference.mustDescent:
211                                 continue
212                 
213                         info = self.serviceHandler.info(serviceref)
214                         if info is None:
215                                 continue
216                         begin = info.getInfo(serviceref, iServiceInformation.sTimeCreate)
217                 
218                         # convert space-seperated list of tags into a set
219                         this_tags = info.getInfoString(serviceref, iServiceInformation.sTags).split(' ')
220                         if this_tags == ['']:
221                                 this_tags = []
222                         this_tags = set(this_tags)
223                         tags |= this_tags
224                 
225                         # filter_tags is either None (which means no filter at all), or 
226                         # a set. In this case, all elements of filter_tags must be present,
227                         # otherwise the entry will be dropped.                  
228                         if filter_tags is not None and not this_tags.issuperset(filter_tags):
229                                 continue
230                 
231                         self.list.append((serviceref, info, begin, -1))
232                 
233                 if self.sort_type == MovieList.SORT_ALPHANUMERIC:
234                         self.list.sort(key=self.buildAlphaNumericSortKey)
235                 else:
236                         # sort: key is 'begin'
237                         self.list.sort(key=lambda x: -x[2])
238                 
239                 # finally, store a list of all tags which were found. these can be presented
240                 # to the user to filter the list
241                 self.tags = tags
242
243         def buildAlphaNumericSortKey(self, x):
244                 ref = x[0]
245                 info = self.serviceHandler.info(ref)
246                 name = info and info.getName(ref)
247                 return (name and name.lower() or "", -x[2])
248
249         def moveTo(self, serviceref):
250                 count = 0
251                 for x in self.list:
252                         if x[0] == serviceref:
253                                 self.instance.moveSelectionTo(count)
254                                 return True
255                         count += 1
256                 return False
257         
258         def moveDown(self):
259                 self.instance.moveSelection(self.instance.moveDown)