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