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