bc659114f308bbf7a596a04a37d1dbf793129e20
[enigma2.git] / lib / python / Components / MovieList.py
1 from HTMLComponent import *
2 from GUIComponent import *
3 from Tools.FuzzyDate import FuzzyTime
4 from ServiceReference import ServiceReference
5 from Components.MultiContent import MultiContentEntryText, RT_HALIGN_LEFT, RT_HALIGN_RIGHT
6
7 from enigma import eListboxPythonMultiContent, eListbox, gFont, iServiceInformation
8
9 from enigma import eServiceReference, eServiceCenter, \
10         eServiceCenterPtr, iListableServicePtr, \
11         iStaticServiceInformationPtr
12
13 #
14 # | name of movie              |
15 #
16 def MovieListEntry(serviceref, serviceHandler):
17         if serviceref.flags & eServiceReference.mustDescent:
18                 return None
19
20         info = serviceHandler.info(serviceref)
21         
22         if info is None:
23                 # ignore service which refuse to info
24                 return None
25         
26         len = info.getLength(serviceref)
27         if len > 0:
28                 len = "%d:%02d" % (len / 60, len % 60)
29         else:
30                 len = "?:??"
31         
32         begin = info.getInfo(serviceref, iServiceInformation.sTimeCreate)
33         res = [ (serviceref, begin) ]
34
35         res.append(MultiContentEntryText(pos=(0, 0), size=(560, 30), font = 0, flags = RT_HALIGN_LEFT, text = info.getName(serviceref)))
36         
37         description = info.getInfoString(serviceref, iServiceInformation.sDescription)
38
39         begin_string = ""
40         if begin > 0:
41                 t = FuzzyTime(begin)
42                 begin_string = t[0] + ", " + t[1]
43         
44         res.append(MultiContentEntryText(pos=(0, 30), size=(560, 20), font=1, flags=RT_HALIGN_LEFT, text=description))
45         res.append(MultiContentEntryText(pos=(0, 50), size=(270, 20), font=1, flags=RT_HALIGN_LEFT, text=begin_string))
46         res.append(MultiContentEntryText(pos=(290, 50), size=(270, 20), font=1, flags=RT_HALIGN_RIGHT, text=len))
47         
48         return res
49
50 class MovieList(HTMLComponent, GUIComponent):
51         def __init__(self, root):
52                 GUIComponent.__init__(self)
53                 self.l = eListboxPythonMultiContent()
54                 if root is not None:
55                         self.reload(root)
56                 self.l.setFont(0, gFont("Regular", 30))
57                 self.l.setFont(1, gFont("Regular", 18))
58                 
59         def moveToIndex(self, index):
60                 self.instance.moveSelectionTo(index)
61
62         def getCurrentIndex(self):
63                 return self.instance.getCurrentIndex()
64
65         def getCurrent(self):
66                 l = self.l.getCurrentSelection()
67                 return l and l[0]
68         
69         def GUIcreate(self, parent):
70                 self.instance = eListbox(parent)
71                 self.instance.setContent(self.l)
72                 self.instance.setItemHeight(75)
73         
74         def GUIdelete(self):
75                 self.instance.setContent(None)
76                 self.instance = None
77
78         def reload(self, root = None):
79                 if root is not None:
80                         self.load(root)
81                 else:
82                         self.load(self.root)
83                 self.l.setList(self.list)
84
85         def removeService(self, service):
86                 for l in self.list[:]:
87                         if l[0][0] == service:
88                                 self.list.remove(l)
89                 self.l.setList(self.list)
90
91         def load(self, root):
92                 # this lists our root service, then building a 
93                 # nice list
94                 
95                 self.list = [ ]
96                 self.root = root
97                 
98                 serviceHandler = eServiceCenter.getInstance()
99                 list = serviceHandler.list(root)
100                 
101                 if list is None:
102                         raise "listing of movies failed"
103
104                 movieList = [ ]
105                 while 1:
106                         s = list.getNext()
107                         if not s.valid():
108                                 del list
109                                 break
110                         movieList.append(s)
111                 
112                 # now process them...
113                 for ref in movieList:
114                         a = MovieListEntry(ref, serviceHandler)
115                         if a is not None:
116                                 self.list.append(a)
117                 
118                 self.list.sort(key=lambda x: -x[0][1])
119
120         def moveTo(self, serviceref):
121                 found = 0
122                 count = 0
123                 for x in self.list:
124                         if str(ServiceReference(x[0][0])) == str(ServiceReference(serviceref)):
125                                 found = count
126                         count += 1
127                 self.instance.moveSelectionTo(found)
128
129         def moveDown(self):
130                 self.instance.moveSelection(self.instance.moveDown)