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