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