simplify some config handling,
[enigma2-plugins.git] / simplerss / src / plugin.py
1 # for localized messages
2 from . import _
3
4 from Components.config import config, ConfigSubsection, ConfigSubList, \
5         ConfigEnableDisable, ConfigNumber, ConfigText, ConfigSelection
6
7 # Initialize Configuration
8 config.plugins.simpleRSS = ConfigSubsection()
9 simpleRSS = config.plugins.simpleRSS
10 simpleRSS.update_notification = ConfigSelection(
11         choices = [
12                 ("notification", _("Notification")),
13                 ("preview", _("Preview")),
14                 ("none", _("none"))
15         ],
16         default = "preview"
17 )
18 simpleRSS.interval = ConfigNumber(default=15)
19 simpleRSS.feedcount = ConfigNumber(default=0)
20 simpleRSS.autostart = ConfigEnableDisable(default=False)
21 simpleRSS.keep_running = ConfigEnableDisable(default=True)
22 simpleRSS.feed = ConfigSubList()
23 for i in range(0, simpleRSS.feedcount.value):
24         s = ConfigSubsection()
25         s.uri = ConfigText(default="http://", fixed_size = False)
26         s.autoupdate = ConfigEnableDisable(default=True)
27         simpleRSS.feed.append(s)
28         del s
29
30 del simpleRSS
31
32 # Global Poller-Object
33 rssPoller = None
34
35 # Main Function
36 def main(session, **kwargs):
37         # Get Global rssPoller-Object
38         global rssPoller
39
40         # Create one if we have none (no autostart)
41         if rssPoller is None:
42                 from RSSPoller import RSSPoller
43                 rssPoller = RSSPoller(session)
44
45         # Show Overview when we have feeds
46         if len(rssPoller.feeds):
47                 from RSSScreens import RSSOverview
48                 session.openWithCallback(closed, RSSOverview, rssPoller)
49         # Show Setup otherwise
50         else:
51                 from RSSSetup import RSSSetup
52                 session.openWithCallback(closed, RSSSetup, rssPoller)
53
54 # Plugin window has been closed
55 def closed():
56         # If SimpleRSS should not run in Background: shutdown
57         if not (config.plugins.simpleRSS.autostart.value or \
58                         config.plugins.simpleRSS.keep_running.value):
59
60                 # Get Global rssPoller-Object
61                 global rssPoller
62
63                 rssPoller.shutdown()
64                 rssPoller = None
65
66 # Autostart
67 def autostart(reason, **kwargs):
68         global rssPoller
69
70         # Instanciate when autostart active, session present and enigma2 is launching
71         if config.plugins.simpleRSS.autostart.value and \
72                 kwargs.has_key("session") and reason == 0:
73
74                 from RSSPoller import RSSPoller
75                 rssPoller = RSSPoller(kwargs["session"])
76         elif reason == 1:
77                 if rssPoller is not None:
78                         rssPoller.shutdown()
79                         rssPoller = None
80
81 # Filescan
82 def filescan_open(item, session, **kwargs):
83         from RSSSetup import addFeed
84
85         # Add earch feed
86         for each in item:
87                 addFeed(each)
88
89         from Screens.MessageBox import MessageBox
90
91         # Display Message
92         session.open(
93                 MessageBox,
94                 _("%d Feed(s) were added to configuration.") % (len(item)),
95                 type = MessageBox.TYPE_INFO,
96                 timeout = 5
97         )
98
99 # Filescanner
100 def filescan(**kwargs):
101         from Components.Scanner import Scanner, ScanPath
102
103         # Overwrite checkFile to detect remote files
104         class RemoteScanner(Scanner):
105                 def checkFile(self, file):
106                         return file.path.startswith(("http://", "https://"))
107
108         return [
109                 RemoteScanner(
110                         mimetypes = ["application/rss+xml", "application/atom+xml"],
111                         paths_to_scan =
112                                 [
113                                         ScanPath(path = "", with_subdirs = False),
114                                 ],
115                         name = "RSS-Reader",
116                         description = "Subscribe Newsfeed...",
117                         openfnc = filescan_open,
118                 )
119         ]
120
121 def Plugins(**kwargs):
122         from Plugins.Plugin import PluginDescriptor
123         return [
124                 PluginDescriptor(
125                         name = "RSS Reader",
126                         description = _("A simple to use RSS reader"),
127                         where = PluginDescriptor.WHERE_PLUGINMENU,
128                         fnc=main
129                 ),
130                 PluginDescriptor(
131                         where = [PluginDescriptor.WHERE_SESSIONSTART, PluginDescriptor.WHERE_AUTOSTART],
132                         fnc = autostart
133                 ),
134                 PluginDescriptor(
135                         name = "View RSS",
136                         description = "Let's you view current RSS entries",
137                         where = PluginDescriptor.WHERE_EXTENSIONSMENU,
138                         fnc=main
139                 ),
140                 PluginDescriptor(
141                         where = PluginDescriptor.WHERE_FILESCAN,
142                         fnc = filescan
143                 )
144         ]