replaced deprecated ConfigEnableDisable by ConfigOnOff
[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, ConfigOnOff, \
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 notificationChanged(self, instance):
129                 import RSSTickerView as tv
130                 if instance and instance.value == "ticker":
131                         if tv.tickerView is None:
132                                 print("[SimpleRSS] Ticker instantiated on startup")
133                                 tv.tickerView = self.session.instantiateDialog(tv.RSSTickerView)
134                 else:
135                         if tv.tickerView is not None:
136                                 self.session.deleteDialog(tv.tickerView)
137                                 tv.tickerView = None
138
139         def delete(self):
140                 from Screens.MessageBox import MessageBox
141
142                 self.session.openWithCallback(
143                         self.deleteConfirm,
144                         MessageBox,
145                         _("Really delete this entry?\nIt cannot be recovered!")
146                 )
147
148         def deleteConfirm(self, result):
149                 if result:
150                         id = self["config"].getCurrentIndex()
151                         del config.plugins.simpleRSS.feed[id]
152                         config.plugins.simpleRSS.feedcount.value -= 1
153
154                         self.createSetup()
155                         self["config"].setList(self.list)
156
157         def ok(self):
158                 id = self["config"].getCurrentIndex()
159                 if id < len(config.plugins.simpleRSS.feed):
160                         self.session.openWithCallback(self.refresh, RSSFeedEdit, id)
161
162         def refresh(self):
163                 # TODO: anything to be done here?
164                 pass
165
166         def new(self):
167                 l = config.plugins.simpleRSS.feed
168                 s = ConfigSubsection()
169                 s.uri = ConfigText(default="http://", fixed_size = False)
170                 s.autoupdate = ConfigOnOff(default=True)
171                 id = len(l)
172                 l.append(s)
173
174                 self.session.openWithCallback(self.conditionalNew, RSSFeedEdit, id)
175
176         def conditionalNew(self):
177                 id = len(config.plugins.simpleRSS.feed)-1
178                 uri = config.plugins.simpleRSS.feed[id].uri
179
180                 # Check if new feed differs from default
181                 if uri.value == "http://":
182                         del config.plugins.simpleRSS.feed[id]
183                 else:
184                         config.plugins.simpleRSS.feedcount.value = id+1
185                         self.createSetup()
186                         self["config"].setList(self.list)
187
188         def keySave(self):
189                 # Tell Poller to recreate List if present
190                 if self.rssPoller is not None:
191                         self.rssPoller.triggerReload()
192                 ConfigListScreen.keySave(self)
193
194         def abort(self):
195                 simpleRSS = config.plugins.simpleRSS
196
197                 # Remove Notifier
198                 simpleRSS.autostart.removeNotifier(self.elementChanged)
199                 simpleRSS.enable_google_reader.removeNotifier(self.elementChanged)
200
201                 # Handle ticker
202                 self.notificationChanged(simpleRSS.update_notification)
203
204                 # Keep feedcount sane
205                 simpleRSS.feedcount.value = len(simpleRSS.feed)
206                 simpleRSS.feedcount.save()
207
208 def addFeed(address, auto = False):
209         l = config.plugins.simpleRSS.feed
210
211         # Create new Item
212         s = ConfigSubsection()
213         s.uri = ConfigText(default="http://", fixed_size = False)
214         s.autoupdate = ConfigOnOff(default=True)
215
216         # Set values
217         s.uri.value = address
218         s.autoupdate.value = auto
219
220         # Save
221         l.append(s)
222         l.save()
223