add prewidgetremove,
[enigma2-plugins.git] / simplerss / src / RSSList.py
1 from Components.GUIComponent import GUIComponent
2 from Components.MultiContent import MultiContentEntryText
3 from enigma import eListboxPythonMultiContent, eListbox, gFont, RT_HALIGN_LEFT, RT_WRAP
4
5 class RSSBaseList(GUIComponent):
6         """Base List Component for RSSFeeds."""
7         def __init__(self, entries, itemheight):
8                 GUIComponent.__init__(self)
9                 self.list = entries
10                 self.itemheight = itemheight
11                 self.l = eListboxPythonMultiContent()
12                 self.l.setFont(0, gFont("Regular", 22))
13                 self.l.setFont(1, gFont("Regular", 18))
14                 self.l.setList(self.list)
15
16                 self.onSelectionChanged = [ ]
17
18         def connectSelChanged(self, fnc):
19                 if not fnc in self.onSelectionChanged:
20                         self.onSelectionChanged.append(fnc)
21
22         def disconnectSelChanged(self, fnc):
23                 if fnc in self.onSelectionChanged:
24                         self.onSelectionChanged.remove(fnc)
25
26         def selectionChanged(self):
27                 for x in self.onSelectionChanged:
28                         try:
29                                 x()
30                         except:
31                                 pass
32
33         GUI_WIDGET = eListbox
34
35         def postWidgetCreate(self, instance):
36                 instance.setContent(self.l)
37                 instance.setItemHeight(self.itemheight)
38                 instance.selectionChanged.get().append(self.selectionChanged)
39
40         def preWidgetRemove(self, instance):
41                 instance.selectionChanged.get().remove(self.selectionChanged)
42                 instance.setContent(None)
43
44         def getCurrentEntry(self):
45                 return self.l.getCurrentSelection()
46
47         def getCurrentIndex(self):
48                 return self.instance.getCurrentIndex()
49
50         def moveToIndex(self, index):
51                 self.instance.moveSelectionTo(index)
52
53         def moveToEntry(self, identifier):
54                 pass
55
56         def moveDown(self):
57                 self.instance.moveSelection(self.instance.moveDown)
58
59         def moveUp(self):
60                 self.instance.moveSelection(self.instance.moveUp)
61
62         def invalidate(self):
63                 self.l.invalidate()
64
65 class RSSFeedList(RSSBaseList):
66         def __init__(self, entries):
67                 RSSBaseList.__init__(self, entries, 100)
68                 self.l.setBuildFunc(self.buildListboxEntry)
69
70         def moveToEntry(self, feed):
71                 if feed is None:
72                         return
73
74                 idx = 0
75                 for x in self.list:
76                         if feed.uri == x[0].uri:
77                                 self.instance.moveSelectionTo(idx)
78                                 break
79                         idx += 1
80
81         def buildListboxEntry(self, feed):
82                 res = [ None ]
83                 width = self.l.getItemSize().width()
84                 res.append(MultiContentEntryText(pos=(0, 0), size=(width, 75), font=0, flags = RT_HALIGN_LEFT|RT_WRAP, text = feed.title))
85                 res.append(MultiContentEntryText(pos=(0, 75), size=(width, 20), font=1, flags = RT_HALIGN_LEFT, text = feed.description))
86                 return res
87
88         def getCurrentEntry(self):
89                 # We know that the list will never be empty...
90                 return self.l.getCurrentSelection()[0]
91
92 class RSSEntryList(RSSBaseList):
93         def __init__(self, entries):
94                 RSSBaseList.__init__(self, entries, 50)
95                 self.l.setBuildFunc(self.buildListboxEntry)
96
97         def moveToEntry(self, entry):
98                 if entry is None:
99                         return
100
101                 idx = 0
102                 for x in self.list:
103                         if entry[0] == x[0]:
104                                 self.instance.moveSelectionTo(idx)
105                                 break
106                         idx += 1
107
108         def buildListboxEntry(self, title, link, summary, enclosures):
109                 res = [ None ]
110                 width = self.l.getItemSize().width()
111                 res.append(MultiContentEntryText(pos=(0, 3), size=(width, 50), font=0, flags = RT_HALIGN_LEFT|RT_WRAP, text = title))
112                 return res