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