make string translatable
[enigma2.git] / lib / python / Components / MovieList.py
1 from GUIComponent import *
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
29                 if len > 0:
30                         len = "%d:%02d" % (len / 60, len % 60)
31                 else:
32                         len = "?:??"
33
34                 res = [ None ]
35
36                 res.append(MultiContentEntryText(pos=(0, 0), size=(420, 30), font = 0, flags = RT_HALIGN_LEFT, text = info.getName(serviceref)))
37                 service = ServiceReference(info.getInfoString(serviceref, iServiceInformation.sServiceref))
38                 if service is not None:
39                         res.append(MultiContentEntryText(pos=(420, 0), size=(140, 30), font = 2, flags = RT_HALIGN_RIGHT, text = service.getServiceName()))
40
41                 description = info.getInfoString(serviceref, iServiceInformation.sDescription)
42
43                 begin_string = ""
44                 if begin > 0:
45                         t = FuzzyTime(begin)
46                         begin_string = t[0] + ", " + t[1]
47
48                 res.append(MultiContentEntryText(pos=(0, 30), size=(560, 20), font=1, flags=RT_HALIGN_LEFT, text=description))
49                 res.append(MultiContentEntryText(pos=(0, 50), size=(270, 20), font=1, flags=RT_HALIGN_LEFT, text=begin_string))
50                 res.append(MultiContentEntryText(pos=(290, 50), size=(270, 20), font=1, flags=RT_HALIGN_RIGHT, text=len))
51
52                 return res
53
54         def moveToIndex(self, index):
55                 self.instance.moveSelectionTo(index)
56
57         def getCurrentIndex(self):
58                 return self.instance.getCurrentIndex()
59
60         def getCurrentEvent(self):
61                 l = self.l.getCurrentSelection()
62                 return l and l[0] and l[1] and l[1].getEvent(l[0])
63
64         def getCurrent(self):
65                 l = self.l.getCurrentSelection()
66                 return l and l[0]
67
68         GUI_WIDGET = eListbox
69         
70         def postWidgetCreate(self, instance):
71                 instance.setContent(self.l)
72         
73         def reload(self, root = None, filter_tags = None):
74                 if root is not None:
75                         self.load(root, filter_tags)
76                 else:
77                         self.load(self.root, filter_tags)
78                 self.l.setList(self.list)
79
80         def removeService(self, service):
81                 for l in self.list[:]:
82                         if l[0] == service:
83                                 self.list.remove(l)
84                 self.l.setList(self.list)
85
86         def __len__(self):
87                 return len(self.list)
88
89         def updateLengthOfIndex(self, index):
90                 if len(self.list) > index:
91                         x = self.list[index]
92                         self.list[index] = (x[0], x[1], x[2], x[1].getLength(x[0]))
93                         self.l.invalidateEntry(index)
94
95         def load(self, root, filter_tags):
96                 # this lists our root service, then building a 
97                 # nice list
98                 
99                 self.list = [ ]
100                 self.root = root
101                 
102                 self.serviceHandler = eServiceCenter.getInstance()
103                 list = self.serviceHandler.list(root)
104                 
105                 tags = set()
106                 
107                 if list is None:
108                         raise "listing of movies failed"
109
110                 while 1:
111                         serviceref = list.getNext()
112                         if not serviceref.valid():
113                                 break
114                         if serviceref.flags & eServiceReference.mustDescent:
115                                 continue
116                         info = self.serviceHandler.info(serviceref)
117                         if info is None:
118                                 continue
119                         begin = info.getInfo(serviceref, iServiceInformation.sTimeCreate)
120                         
121                         # convert space-seperated list of tags into a set
122                         this_tags = info.getInfoString(serviceref, iServiceInformation.sTags).split(' ')
123                         if this_tags == ['']:
124                                 this_tags = []
125                         this_tags = set(this_tags)
126                         
127                         # filter_tags is either None (which means no filter at all), or 
128                         # a set. In this case, all elements of filter_tags must be present,
129                         # otherwise the entry will be dropped.                  
130                         if filter_tags is not None and not this_tags.issuperset(filter_tags):
131                                 continue
132                         
133                         tags |= this_tags
134                         self.list.append((serviceref, info, begin, -1))
135                 
136                 # sort: key is 'begin'
137                 self.list.sort(key=lambda x: -x[2])
138                 
139                 # finally, store a list of all tags which were found. these can be presented
140                 # to the user to filter the list
141                 self.tags = tags
142
143         def moveTo(self, serviceref):
144                 found = 0
145                 count = 0
146                 for x in self.list:
147                         if x[0] == serviceref:
148                                 found = count
149                         count += 1
150                 self.instance.moveSelectionTo(found)
151
152         def moveDown(self):
153                 self.instance.moveSelection(self.instance.moveDown)