allow custom localization in simplerss and add italian translation (thx spaeleus)
[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                 self.list = [
26                         getConfigListEntry(_("Autoupdate"), config.plugins.simpleRSS.feed[id].autoupdate),
27                         getConfigListEntry(_("Feed URI"), config.plugins.simpleRSS.feed[id].uri)
28                 ]
29
30                 ConfigListScreen.__init__(self, self.list, session)
31
32                 self["key_red"] = Button(_("Cancel"))
33                 self["key_green"] = Button(_("OK"))
34
35                 self["setupActions"] = ActionMap(["SetupActions"],
36                 {
37                         "save": self.save,
38                         "cancel": self.keyCancel
39                 }, -1)
40
41                 self.id = id
42
43         def save(self):
44                 config.plugins.simpleRSS.feed[self.id].save()
45                 config.plugins.simpleRSS.feed.save()
46                 self.close()
47
48 class RSSSetup(ConfigListScreen, Screen):
49         """Setup for SimpleRSS, quick-edit for Feed-URIs and settings present."""
50         skin = """
51                 <screen name="RSSSetup" position="100,100" size="550,400" title="Simple RSS Reader Setup" >
52                         <widget name="config"  position="20,10" size="510,350" scrollbarMode="showOnDemand" />
53                         <ePixmap name="red"    position="0,360"   zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
54                         <ePixmap name="green"  position="140,360" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
55                         <ePixmap name="yellow" position="280,360" zPosition="4" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
56                         <ePixmap name="blue"   position="420,360" zPosition="4" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
57                         <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" />
58                         <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" />
59                         <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" />
60                         <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" />
61                 </screen>"""
62
63         def __init__(self, session, rssPoller = None):
64                 Screen.__init__(self, session)
65
66                 self.onClose.append(self.abort)
67
68                 self.rssPoller = rssPoller
69
70                 # Create List of all Feeds
71                 self.list = [
72                         getConfigListEntry(_("Feed"), config.plugins.simpleRSS.feed[i].uri)
73                                 for i in range(0, config.plugins.simpleRSS.feedcount.value)
74                 ]
75
76                 # Attach notifier to autostart and append ConfigListEntry to List
77                 config.plugins.simpleRSS.autostart.addNotifier(self.autostartChanged, initial_call = False)
78                 self.list.append(getConfigListEntry(_("Start automatically with Enigma2"), config.plugins.simpleRSS.autostart))
79
80                 # Save keep_running in instance as we want to dynamically add/remove it
81                 self.keep_running = getConfigListEntry(_("Keep running in background"), config.plugins.simpleRSS.keep_running)
82                 if not config.plugins.simpleRSS.autostart.value:
83                         self.list.append(self.keep_running)
84
85                 # Append Last two config Elements
86                 self.list.append(getConfigListEntry(_("Show new Messages as"), config.plugins.simpleRSS.update_notification))
87                 self.list.append(getConfigListEntry(_("Update Interval (min)"), config.plugins.simpleRSS.interval))
88
89                 # Initialize ConfigListScreen
90                 ConfigListScreen.__init__(self, self.list, session)
91
92                 self["key_red"] = Button(_("Cancel"))
93                 self["key_green"] = Button(_("OK"))
94                 self["key_yellow"] = Button(_("New"))
95                 self["key_blue"] = Button(_("Delete"))
96
97                 self["setupActions"] = ActionMap(["SetupActions", "ColorActions"],
98                 {
99                         "blue": self.delete,
100                         "yellow": self.new,
101                         "save": self.keySave,
102                         "cancel": self.keyCancel,
103                         "ok": self.ok
104                 }, -1)
105
106         def autostartChanged(self, instance):
107                 # Remove keep_running from list if autostart is active
108                 if instance.value:
109                         self.list.remove(self.keep_running)
110                 # Otherwise add it at third position from behind
111                 else:
112                         self.list.insert(-2, self.keep_running)
113
114                 # Assign new List to ConfigList
115                 self["config"].setList(self.list)
116
117         def delete(self):
118                 from Screens.MessageBox import MessageBox
119
120                 self.session.openWithCallback(
121                         self.deleteConfirm,
122                         MessageBox,
123                         _("Really delete this entry?\nIt cannot be recovered!")
124                 )
125
126         def deleteConfirm(self, result):
127                 if result:
128                         id = self["config"].instance.getCurrentIndex()
129                         del config.plugins.simpleRSS.feed[id]
130                         config.plugins.simpleRSS.feedcount.value -= 1
131                         self.list.pop(id)
132                         # redraw list
133                         self["config"].setList(self.list)
134
135         def ok(self):
136                 id = self["config"].instance.getCurrentIndex()
137                 self.session.openWithCallback(self.refresh, RSSFeedEdit, id)
138
139         def refresh(self):
140                 # TODO: anything to be done here?
141                 pass
142
143         def new(self):
144                 id = len(config.plugins.simpleRSS.feed)
145                 config.plugins.simpleRSS.feed.append(ConfigSubsection())
146                 config.plugins.simpleRSS.feed[id].uri = ConfigText(default="http://", fixed_size = False)
147                 config.plugins.simpleRSS.feed[id].autoupdate = ConfigEnableDisable(default=True)
148                 self.session.openWithCallback(self.conditionalNew, RSSFeedEdit, id)
149
150         def conditionalNew(self):
151                 id = len(config.plugins.simpleRSS.feed)-1
152                 # Check if new feed differs from default
153                 if config.plugins.simpleRSS.feed[id].uri.value == "http://":
154                         del config.plugins.simpleRSS.feed[id]
155                 else:
156                         self.list.insert(id, getConfigListEntry(_("Feed"), config.plugins.simpleRSS.feed[id].uri))
157                         config.plugins.simpleRSS.feedcount.value = id+1
158
159         def keySave(self):
160                 # Tell Poller to recreate List if present
161                 if self.rssPoller is not None:
162                         self.rssPoller.triggerReload()
163                 ConfigListScreen.keySave(self)
164
165         def abort(self):
166                 print "[SimpleRSS] Closing Setup Dialog"
167
168                 # Remove Notifier
169                 config.plugins.simpleRSS.autostart.notifiers.remove(self.autostartChanged)
170
171                 # Keep feedcount sane
172                 config.plugins.simpleRSS.feedcount.value = len(config.plugins.simpleRSS.feed)
173                 config.plugins.simpleRSS.feedcount.save()
174
175 def addFeed(address, auto = False):
176         # Read out ID
177         id = len(config.plugins.simpleRSS.feed)
178
179         # Create new Item
180         config.plugins.simpleRSS.feed.append(ConfigSubsection())
181         config.plugins.simpleRSS.feed[id].uri = ConfigText(default="http://", fixed_size = False)
182         config.plugins.simpleRSS.feed[id].autoupdate = ConfigEnableDisable(default=True)
183
184         # Set values
185         config.plugins.simpleRSS.feed[id].uri.value = address
186         config.plugins.simpleRSS.feed[id].autoupdate.value = auto
187
188         # Save (needed?)
189         config.plugins.simpleRSS.feed.save()