Dutch translation by Benny
[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
72                 self.rssPoller = rssPoller
73                 simpleRSS = config.plugins.simpleRSS
74
75                 # Create List of all Feeds
76                 list = [
77                         getConfigListEntry(_("Feed"), x.uri)
78                                 for x in simpleRSS.feed
79                 ]
80
81                 # Attach notifier to autostart and append ConfigListEntry to List
82                 simpleRSS.autostart.addNotifier(self.autostartChanged, initial_call = False)
83                 list.append(getConfigListEntry(_("Start automatically with Enigma2"), simpleRSS.autostart))
84
85                 # Save keep_running in instance as we want to dynamically add/remove it
86                 self.keep_running = getConfigListEntry(_("Keep running in background"), simpleRSS.keep_running)
87                 if not simpleRSS.autostart.value:
88                         list.append(self.keep_running)
89
90                 # Append Last two config Elements
91                 list.extend((
92                         getConfigListEntry(_("Show new Messages as"), simpleRSS.update_notification),
93                         getConfigListEntry(_("Update Interval (min)"), simpleRSS.interval)
94                 ))
95
96                 # Initialize ConfigListScreen
97                 self.list = list
98                 ConfigListScreen.__init__(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                 self.onClose.append(self.abort)
116
117         def setCustomTitle(self):
118                 self.setTitle(_("Simple RSS Reader Setup"))
119
120         def autostartChanged(self, instance):
121                 # Remove keep_running from list if autostart is active
122                 if instance.value:
123                         self.list.remove(self.keep_running)
124                 # Otherwise add it at third position from behind
125                 else:
126                         self.list.insert(-2, self.keep_running)
127
128                 # Assign new List to ConfigList
129                 self["config"].setList(self.list)
130
131         def delete(self):
132                 from Screens.MessageBox import MessageBox
133
134                 self.session.openWithCallback(
135                         self.deleteConfirm,
136                         MessageBox,
137                         _("Really delete this entry?\nIt cannot be recovered!")
138                 )
139
140         def deleteConfirm(self, result):
141                 if result:
142                         id = self["config"].getCurrentIndex()
143                         del config.plugins.simpleRSS.feed[id]
144                         config.plugins.simpleRSS.feedcount.value -= 1
145                         self.list.pop(id)
146
147                         # redraw list
148                         self["config"].l.invalidate()
149
150         def ok(self):
151                 id = self["config"].getCurrentIndex()
152                 self.session.openWithCallback(self.refresh, RSSFeedEdit, id)
153
154         def refresh(self):
155                 # TODO: anything to be done here?
156                 pass
157
158         def new(self):
159                 l = config.plugins.simpleRSS.feed
160                 s = ConfigSubsection()
161                 s.uri = ConfigText(default="http://", fixed_size = False)
162                 s.autoupdate = ConfigEnableDisable(default=True)
163                 id = len(l)
164                 l.append(s)
165
166                 self.session.openWithCallback(self.conditionalNew, RSSFeedEdit, id)
167
168         def conditionalNew(self):
169                 id = len(config.plugins.simpleRSS.feed)-1
170                 uri = config.plugins.simpleRSS.feed[id].uri
171
172                 # Check if new feed differs from default
173                 if uri.value == "http://":
174                         del config.plugins.simpleRSS.feed[id]
175                 else:
176                         self.list.insert(id, getConfigListEntry(_("Feed"), uri))
177                         config.plugins.simpleRSS.feedcount.value = id+1
178
179         def keySave(self):
180                 # Tell Poller to recreate List if present
181                 if self.rssPoller is not None:
182                         self.rssPoller.triggerReload()
183                 ConfigListScreen.keySave(self)
184
185         def abort(self):
186                 print "[SimpleRSS] Closing Setup Dialog"
187                 simpleRSS = config.plugins.simpleRSS
188
189                 # Remove Notifier
190                 simpleRSS.autostart.notifiers.remove(self.autostartChanged)
191
192                 # Keep feedcount sane
193                 simpleRSS.feedcount.value = len(simpleRSS.feed)
194                 simpleRSS.feedcount.save()
195
196 def addFeed(address, auto = False):
197         l = config.plugins.simpleRSS.feed
198
199         # Create new Item
200         s = ConfigSubsection()
201         s.uri = ConfigText(default="http://", fixed_size = False)
202         s.autoupdate = ConfigEnableDisable(default=True)
203
204         # Set values
205         s.uri.value = address
206         s.autoupdate.value = auto
207
208         # Save
209         l.append(s)
210         l.save()
211