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