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