simplify some config handling,
[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                 self.list = [
27                         getConfigListEntry(_("Autoupdate"), s.autoupdate),
28                         getConfigListEntry(_("Feed URI"), s.uri)
29                 ]
30
31                 ConfigListScreen.__init__(self, 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
72                 self.onClose.append(self.abort)
73
74                 self.rssPoller = rssPoller
75
76                 simpleRSS = config.plugins.simpleRSS
77
78                 # Create List of all Feeds
79                 self.list = [
80                         getConfigListEntry(_("Feed"), x.uri)
81                                 for x in simpleRSS.feed
82                 ]
83
84                 # Attach notifier to autostart and append ConfigListEntry to List
85                 simpleRSS.autostart.addNotifier(self.autostartChanged, initial_call = False)
86                 self.list.append(getConfigListEntry(_("Start automatically with Enigma2"), simpleRSS.autostart))
87
88                 # Save keep_running in instance as we want to dynamically add/remove it
89                 self.keep_running = getConfigListEntry(_("Keep running in background"), simpleRSS.keep_running)
90                 if not simpleRSS.autostart.value:
91                         self.list.append(self.keep_running)
92
93                 # Append Last two config Elements
94                 self.list.append(getConfigListEntry(_("Show new Messages as"), simpleRSS.update_notification))
95                 self.list.append(getConfigListEntry(_("Update Interval (min)"), simpleRSS.interval))
96
97                 # Initialize ConfigListScreen
98                 ConfigListScreen.__init__(self, self.list, session)
99
100                 self["key_red"] = Button(_("Cancel"))
101                 self["key_green"] = Button(_("OK"))
102                 self["key_yellow"] = Button(_("New"))
103                 self["key_blue"] = Button(_("Delete"))
104
105                 self["setupActions"] = ActionMap(["SetupActions", "ColorActions"],
106                 {
107                         "blue": self.delete,
108                         "yellow": self.new,
109                         "save": self.keySave,
110                         "cancel": self.keyCancel,
111                         "ok": self.ok
112                 }, -1)
113
114                 self.onLayoutFinish.append(self.setCustomTitle)
115
116         def setCustomTitle(self):
117                 self.setTitle(_("Simple RSS Reader Setup"))
118
119         def autostartChanged(self, instance):
120                 # Remove keep_running from list if autostart is active
121                 if instance.value:
122                         self.list.remove(self.keep_running)
123                 # Otherwise add it at third position from behind
124                 else:
125                         self.list.insert(-2, self.keep_running)
126
127                 # Assign new List to ConfigList
128                 self["config"].setList(self.list)
129
130         def delete(self):
131                 from Screens.MessageBox import MessageBox
132
133                 self.session.openWithCallback(
134                         self.deleteConfirm,
135                         MessageBox,
136                         _("Really delete this entry?\nIt cannot be recovered!")
137                 )
138
139         def deleteConfirm(self, result):
140                 if result:
141                         id = self["config"].instance.getCurrentIndex()
142                         del config.plugins.simpleRSS.feed[id]
143                         config.plugins.simpleRSS.feedcount.value -= 1
144                         self.list.pop(id)
145                         # redraw list
146                         self["config"].setList(self.list)
147
148         def ok(self):
149                 id = self["config"].instance.getCurrentIndex()
150                 self.session.openWithCallback(self.refresh, RSSFeedEdit, id)
151
152         def refresh(self):
153                 # TODO: anything to be done here?
154                 pass
155
156         def new(self):
157                 l = config.plugins.simpleRSS.feed
158                 s = ConfigSubsection()
159                 s.uri = ConfigText(default="http://", fixed_size = False)
160                 s.autoupdate = ConfigEnableDisable(default=True)
161                 id = len(l)
162                 l.append(s)
163
164                 self.session.openWithCallback(self.conditionalNew, RSSFeedEdit, id)
165
166         def conditionalNew(self):
167                 id = len(config.plugins.simpleRSS.feed)-1
168                 uri = config.plugins.simpleRSS.feed[id].uri
169
170                 # Check if new feed differs from default
171                 if uri.value == "http://":
172                         del config.plugins.simpleRSS.feed[id]
173                 else:
174                         self.list.insert(id, getConfigListEntry(_("Feed"), uri))
175                         config.plugins.simpleRSS.feedcount.value = id+1
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.autostartChanged)
189
190                 # Keep feedcount sane
191                 simpleRSS.feedcount.value = len(simpleRSS.feed)
192                 simpleRSS.feedcount.save()
193
194 def addFeed(address, auto = False):
195         l = config.plugins.simpleRSS.feed
196
197         # Create new Item
198         s = ConfigSubsection()
199         s.uri = ConfigText(default="http://", fixed_size = False)
200         s.autoupdate = ConfigEnableDisable(default=True)
201
202         # Set values
203         s.uri.value = address
204         s.autoupdate.value = auto
205
206         # Save
207         l.append(s)
208         l.save()
209