New Items displayed in Feeds,
[enigma2-plugins.git] / simplerss / src / RSSSetup.py
1 from Screens.Screen import Screen
2 from Components.config import config, ConfigSubsection, ConfigSubList, ConfigEnableDisable, ConfigInteger, ConfigText, getConfigListEntry
3 from Components.ConfigList import ConfigListScreen
4 from Components.Button import Button
5 from Components.ActionMap import ActionMap
6
7 class RSSFeedEdit(ConfigListScreen, Screen):
8         """Edit an RSS-Feed"""
9         skin = """
10                 <screen name="RSSFeedEdit" position="100,100" size="550,120" title="Simple RSS Reader Setup" >
11                         <widget name="config" position="20,10" size="510,75" scrollbarMode="showOnDemand" />
12                         <ePixmap name="red"    position="0,75"   zPosition="4" size="140,40" pixmap="key_red-fs8.png" transparent="1" alphatest="on" />
13                         <ePixmap name="green"  position="140,75" zPosition="4" size="140,40" pixmap="key_green-fs8.png" transparent="1" alphatest="on" />
14                         <widget name="key_red" position="0,75" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
15                         <widget name="key_green" position="140,75" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
16                 </screen>"""
17
18         def __init__(self, session, id):
19                 Screen.__init__(self, session)
20
21                 self.list = [
22                         getConfigListEntry(_("Autoupdate: "), config.plugins.simpleRSS.feed[id].autoupdate),
23                         getConfigListEntry(_("Feed URI: "), config.plugins.simpleRSS.feed[id].uri)
24                 ]
25
26                 ConfigListScreen.__init__(self, self.list, session)
27
28                 self["key_red"] = Button(_("Cancel"))
29                 self["key_green"] = Button(_("OK"))
30
31                 self["setupActions"] = ActionMap(["SetupActions"],
32                 {
33                         "save": self.save,
34                         "cancel": self.keyCancel
35                 }, -1)
36
37                 self.id = id
38
39         def save(self):
40                 config.plugins.simpleRSS.feed[self.id].save()
41                 config.plugins.simpleRSS.feed.save()
42                 self.close()
43
44 class RSSSetup(ConfigListScreen, Screen):
45         """Setup for SimpleRSS, quick-edit for Feed-URIs and settings present."""
46         skin = """
47                 <screen name="RSSSetup" position="100,100" size="550,400" title="Simple RSS Reader Setup" >
48                         <widget name="config"  position="20,10" size="510,350" scrollbarMode="showOnDemand" />
49                         <ePixmap name="red"    position="0,360"   zPosition="4" size="140,40" pixmap="key_red-fs8.png" transparent="1" alphatest="on" />
50                         <ePixmap name="green"  position="140,360" zPosition="4" size="140,40" pixmap="key_green-fs8.png" transparent="1" alphatest="on" />
51                         <ePixmap name="yellow" position="280,360" zPosition="4" size="140,40" pixmap="key_yellow-fs8.png" transparent="1" alphatest="on" />
52                         <ePixmap name="blue"   position="420,360" zPosition="4" size="140,40" pixmap="key_blue-fs8.png" transparent="1" alphatest="on" />
53                         <widget name="key_red"    position="0,360" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
54                         <widget name="key_green"  position="140,360" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
55                         <widget name="key_yellow" position="280,360" zPosition="5" size="140,40" valign="center" halign="center"  font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
56                         <widget name="key_blue"   position="420,360" zPosition="5" size="140,40" valign="center" halign="center"  font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
57                 </screen>"""
58
59         def __init__(self, session, rssPoller = None):
60                 Screen.__init__(self, session)
61
62                 self.onClose.append(self.abort)
63
64                 self.rssPoller = rssPoller
65
66                 # nun erzeugen wir eine liste von elementen fuer die menu liste.
67                 self.list = [
68                         getConfigListEntry(_("Feed: "), config.plugins.simpleRSS.feed[i].uri)
69                                 for i in range(0, config.plugins.simpleRSS.feedcount.value)
70                 ]
71                 self.list.append(getConfigListEntry(_("Show new Messages: "), config.plugins.simpleRSS.show_new))
72                 self.list.append(getConfigListEntry(_("Update Interval (min): "), config.plugins.simpleRSS.interval))
73
74                 # die liste selbst
75                 ConfigListScreen.__init__(self, self.list, session)
76
77                 self["key_red"] = Button(_("Cancel"))
78                 self["key_green"] = Button(_("OK"))
79                 self["key_yellow"] = Button(_("New"))
80                 self["key_blue"] = Button(_("Delete"))
81
82                 self["setupActions"] = ActionMap(["SetupActions", "ColorActions"],
83                 {
84                         "blue": self.delete,
85                         "yellow": self.new,
86                         "save": self.keySave,
87                         "cancel": self.keyCancel,
88                         "ok": self.ok
89                 }, -1)
90         
91         def delete(self):
92                 self.session.openWithCallback(self.deleteConfirm, MessageBox, "Really delete this entry?\nIt cannot be recovered!")
93
94         def deleteConfirm(self, result):
95                 if result:
96                         id = self["config"].instance.getCurrentIndex()
97                         del config.plugins.simpleRSS.feed[id]
98                         config.plugins.simpleRSS.feedcount.value -= 1
99                         self.list.pop(id)
100                         # redraw list
101                         self["config"].setList(self.list)
102
103         def ok(self):
104                 id = self["config"].instance.getCurrentIndex()
105                 self.session.openWithCallback(self.refresh, RSSFeedEdit, id)
106
107         def refresh(self):
108                 # TODO: anything to be done here?
109                 pass
110
111         def new(self):
112                 id = len(config.plugins.simpleRSS.feed)
113                 config.plugins.simpleRSS.feed.append(ConfigSubsection())
114                 config.plugins.simpleRSS.feed[id].uri = ConfigText(default="http://", fixed_size = False)
115                 config.plugins.simpleRSS.feed[id].autoupdate = ConfigEnableDisable(default=True)
116                 self.session.openWithCallback(self.conditionalNew, RSSFeedEdit, id)
117
118         def conditionalNew(self):
119                 id = len(config.plugins.simpleRSS.feed)-1
120                 # Check if new feed differs from default
121                 if config.plugins.simpleRSS.feed[id].uri.value == "http://":
122                         del config.plugins.simpleRSS.feed[id]
123                 else:
124                         self.list.insert(id, getConfigListEntry(_("Feed: "), config.plugins.simpleRSS.feed[id].uri))
125                         config.plugins.simpleRSS.feedcount.value = id+1
126
127         def keySave(self):
128                 if self.rssPoller is not None:
129                         self.rssPoller.triggerReload()
130                 ConfigListScreen.keySave(self)
131
132         def abort(self):
133                 print "[SimpleRSS] Closing Setup Dialog"
134                 # Keep feedcount sane
135                 config.plugins.simpleRSS.feedcount.value = len(config.plugins.simpleRSS.feed)
136                 config.plugins.simpleRSS.feedcount.save()