allow to fetch feeds from google reader
[enigma2-plugins.git] / simplerss / src / RSSSetup.py
1 # for localized messages
2 from . import _
3
4 from Screens.Screen import Screen
5 from Components.config import config, ConfigSubsection, ConfigEnableDisable, \
6         ConfigText, getConfigListEntry
7 from Components.ConfigList import ConfigListScreen
8 from Components.Button import Button
9 from Components.ActionMap import ActionMap
10
11 class RSSFeedEdit(ConfigListScreen, Screen):
12         """Edit an RSS-Feed"""
13         skin = """
14                 <screen name="RSSFeedEdit" position="100,100" size="550,120" title="Simple RSS Reader Setup" >
15                         <widget name="config" position="20,10" size="510,75" scrollbarMode="showOnDemand" />
16                         <ePixmap name="red" position="0,75" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
17                         <ePixmap name="green" position="140,75" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
18                         <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" />
19                         <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" />
20                 </screen>"""
21
22         def __init__(self, session, id):
23                 Screen.__init__(self, session)
24
25                 s = config.plugins.simpleRSS.feed[id]
26                 list = [
27                         getConfigListEntry(_("Autoupdate"), s.autoupdate),
28                         getConfigListEntry(_("Feed URI"), s.uri)
29                 ]
30
31                 ConfigListScreen.__init__(self, list, session)
32
33                 self["key_red"] = Button(_("Cancel"))
34                 self["key_green"] = Button(_("OK"))
35
36                 self["setupActions"] = ActionMap(["SetupActions"],
37                 {
38                         "save": self.save,
39                         "cancel": self.keyCancel
40                 }, -1)
41
42                 self.id = id
43
44                 self.onLayoutFinish.append(self.setCustomTitle)
45
46         def setCustomTitle(self):
47                 self.setTitle(_("Simple RSS Reader Setup"))
48
49         def save(self):
50                 config.plugins.simpleRSS.feed[self.id].save()
51                 config.plugins.simpleRSS.feed.save()
52                 self.close()
53
54 class RSSSetup(ConfigListScreen, Screen):
55         """Setup for SimpleRSS, quick-edit for Feed-URIs and settings present."""
56         skin = """
57                 <screen name="RSSSetup" position="100,100" size="550,400" title="Simple RSS Reader Setup" >
58                         <widget name="config" position="20,10" size="510,350" scrollbarMode="showOnDemand" />
59                         <ePixmap name="red" position="0,360" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
60                         <ePixmap name="green" position="140,360" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
61                         <ePixmap name="yellow" position="280,360" zPosition="4" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
62                         <ePixmap name="blue" position="420,360" zPosition="4" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
63                         <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" />
64                         <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" />
65                         <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" />
66                         <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" />
67                 </screen>"""
68
69         def __init__(self, session, rssPoller = None):
70                 Screen.__init__(self, session)
71                 self.rssPoller = rssPoller
72
73                 self.createSetup()
74                 config.plugins.simpleRSS.autostart.addNotifier(self.elementChanged, initial_call = False)
75                 config.plugins.simpleRSS.enable_google_reader.addNotifier(self.elementChanged, initial_call = False)
76
77                 # Initialize ConfigListScreen
78                 ConfigListScreen.__init__(self, self.list, session)
79
80                 self["key_red"] = Button(_("Cancel"))
81                 self["key_green"] = Button(_("OK"))
82                 self["key_yellow"] = Button(_("New"))
83                 self["key_blue"] = Button(_("Delete"))
84
85                 self["setupActions"] = ActionMap(["SetupActions", "ColorActions"],
86                 {
87                         "blue": self.delete,
88                         "yellow": self.new,
89                         "save": self.keySave,
90                         "cancel": self.keyCancel,
91                         "ok": self.ok
92                 }, -1)
93
94                 self.onLayoutFinish.append(self.setCustomTitle)
95                 self.onClose.append(self.abort)
96
97         def setCustomTitle(self):
98                 self.setTitle(_("Simple RSS Reader Setup"))
99
100         def createSetup(self):
101                 simpleRSS = config.plugins.simpleRSS
102
103                 # Create List of all Feeds
104                 list = [
105                         getConfigListEntry(_("Feed"), x.uri)
106                                 for x in simpleRSS.feed
107                 ]
108
109                 list.append(getConfigListEntry(_("Start automatically with Enigma2"), simpleRSS.autostart))
110
111                 # Save keep_running in instance as we want to dynamically add/remove it
112                 self.keep_running = getConfigListEntry(_("Keep running in background"), simpleRSS.keep_running)
113                 if not simpleRSS.autostart.value:
114                         list.append(self.keep_running)
115
116                 # Append Last two config Elements
117                 list.extend((
118                         getConfigListEntry(_("Show new Messages as"), simpleRSS.update_notification),
119                         getConfigListEntry(_("Update Interval (min)"), simpleRSS.interval),
120                         getConfigListEntry(_("Fetch feed from Google Reader?"), simpleRSS.enable_google_reader),
121                 ))
122
123                 if simpleRSS.enable_google_reader.value:
124                         list.extend((
125                                 getConfigListEntry(_("Google Username"), simpleRSS.google_username),
126                                 getConfigListEntry(_("Google Password"), simpleRSS.google_password),
127                         ))
128
129                 self.list = list
130
131         def elementChanged(self, instance):
132                 self.createSetup()
133                 self["config"].setList(self.list)
134
135         def delete(self):
136                 from Screens.MessageBox import MessageBox
137
138                 self.session.openWithCallback(
139                         self.deleteConfirm,
140                         MessageBox,
141                         _("Really delete this entry?\nIt cannot be recovered!")
142                 )
143
144         def deleteConfirm(self, result):
145                 if result:
146                         id = self["config"].getCurrentIndex()
147                         del config.plugins.simpleRSS.feed[id]
148                         config.plugins.simpleRSS.feedcount.value -= 1
149
150                         self.createSetup()
151                         self["config"].setList(self.list)
152
153         def ok(self):
154                 id = self["config"].getCurrentIndex()
155                 if id < len(config.plugins.simpleRSS.feed):
156                         self.session.openWithCallback(self.refresh, RSSFeedEdit, id)
157
158         def refresh(self):
159                 # TODO: anything to be done here?
160                 pass
161
162         def new(self):
163                 l = config.plugins.simpleRSS.feed
164                 s = ConfigSubsection()
165                 s.uri = ConfigText(default="http://", fixed_size = False)
166                 s.autoupdate = ConfigEnableDisable(default=True)
167                 id = len(l)
168                 l.append(s)
169
170                 self.session.openWithCallback(self.conditionalNew, RSSFeedEdit, id)
171
172         def conditionalNew(self):
173                 id = len(config.plugins.simpleRSS.feed)-1
174                 uri = config.plugins.simpleRSS.feed[id].uri
175
176                 # Check if new feed differs from default
177                 if uri.value == "http://":
178                         del config.plugins.simpleRSS.feed[id]
179                 else:
180                         config.plugins.simpleRSS.feedcount.value = id+1
181                         self.createSetup()
182                         self["config"].setList(self.list)
183
184         def keySave(self):
185                 # Tell Poller to recreate List if present
186                 if self.rssPoller is not None:
187                         self.rssPoller.triggerReload()
188                 ConfigListScreen.keySave(self)
189
190         def abort(self):
191                 print "[SimpleRSS] Closing Setup Dialog"
192                 simpleRSS = config.plugins.simpleRSS
193
194                 # Remove Notifier
195                 simpleRSS.autostart.notifiers.remove(self.elementChanged)
196                 simpleRSS.enable_google_reader.notifiers.remove(self.elementChanged)
197
198                 # Keep feedcount sane
199                 simpleRSS.feedcount.value = len(simpleRSS.feed)
200                 simpleRSS.feedcount.save()
201
202 def addFeed(address, auto = False):
203         l = config.plugins.simpleRSS.feed
204
205         # Create new Item
206         s = ConfigSubsection()
207         s.uri = ConfigText(default="http://", fixed_size = False)
208         s.autoupdate = ConfigEnableDisable(default=True)
209
210         # Set values
211         s.uri.value = address
212         s.autoupdate.value = auto
213
214         # Save
215         l.append(s)
216         l.save()
217