show event information of current selected event in channelselection
[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
29                 if len <= 0: #recalc len when not already done
30                         cur_idx = self.l.getCurrentSelectionIndex()
31                         x = self.list[cur_idx]
32                         len = x[1].getLength(x[0]) #recalc the movie length...
33                         self.list[cur_idx] = (x[0], x[1], x[2], len) #update entry in list... so next time we don't need to recalc
34
35                 if len > 0:
36                         len = "%d:%02d" % (len / 60, len % 60)
37                 else:
38                         len = "?:??"
39
40                 res = [ None ]
41
42                 res.append(MultiContentEntryText(pos=(0, 0), size=(420, 30), font = 0, flags = RT_HALIGN_LEFT, text = info.getName(serviceref)))
43                 service = ServiceReference(info.getInfoString(serviceref, iServiceInformation.sServiceref))
44                 if service is not None:
45                         res.append(MultiContentEntryText(pos=(420, 0), size=(140, 30), font = 2, flags = RT_HALIGN_RIGHT, text = service.getServiceName()))
46
47                 description = info.getInfoString(serviceref, iServiceInformation.sDescription)
48
49                 begin_string = ""
50                 if begin > 0:
51                         t = FuzzyTime(begin)
52                         begin_string = t[0] + ", " + t[1]
53
54                 res.append(MultiContentEntryText(pos=(0, 30), size=(560, 20), font=1, flags=RT_HALIGN_LEFT, text=description))
55                 res.append(MultiContentEntryText(pos=(0, 50), size=(270, 20), font=1, flags=RT_HALIGN_LEFT, text=begin_string))
56                 res.append(MultiContentEntryText(pos=(290, 50), size=(270, 20), font=1, flags=RT_HALIGN_RIGHT, text=len))
57
58                 return res
59
60         def moveToIndex(self, index):
61                 self.instance.moveSelectionTo(index)
62
63         def getCurrentIndex(self):
64                 return self.instance.getCurrentIndex()
65
66         def getCurrentEvent(self):
67                 l = self.l.getCurrentSelection()
68                 return l and l[0] and l[1] and l[1].getEvent(l[0])
69
70         def getCurrent(self):
71                 l = self.l.getCurrentSelection()
72                 return l and l[0]
73
74         GUI_WIDGET = eListbox
75         
76         def postWidgetCreate(self, instance):
77                 instance.setContent(self.l)
78         
79         def reload(self, root = None, filter_tags = None):
80                 if root is not None:
81                         self.load(root, filter_tags)
82                 else:
83                         self.load(self.root, filter_tags)
84                 self.l.setList(self.list)
85
86         def removeService(self, service):
87                 for l in self.list[:]:
88                         if l[0] == service:
89                                 self.list.remove(l)
90                 self.l.setList(self.list)
91
92         def __len__(self):
93                 return len(self.list)
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)