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