initial checkin of MerlinSkinThemes
[enigma2-plugins.git] / simplerss / src / plugin.py
1 from Components.config import config, ConfigSubsection, ConfigSubList, \
2         ConfigOnOff, ConfigNumber, ConfigText, ConfigSelection, \
3         ConfigYesNo, ConfigPassword
4
5 from Components.PluginComponent import plugins
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                 ("ticker", _("Ticker")),
15                 ("none", _("none"))
16         ],
17         default = "preview"
18 )
19 simpleRSS.interval = ConfigNumber(default=15)
20 simpleRSS.feedcount = ConfigNumber(default=0)
21 simpleRSS.autostart = ConfigOnOff(default=False)
22 simpleRSS.keep_running = ConfigOnOff(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 = ConfigOnOff(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 (or retrieving them from google)
52         if rssPoller.feeds or config.plugins.simpleRSS.enable_google_reader.value:
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         if "session" in kwargs and config.plugins.simpleRSS.update_notification.value == "ticker":
77                 import RSSTickerView as tv
78                 if tv.tickerView is None:
79                         tv.tickerView = kwargs["session"].instantiateDialog(tv.RSSTickerView)
80
81         # Instanciate when enigma2 is launching, autostart active and session present or installed during runtime
82         if reason == 0 and config.plugins.simpleRSS.autostart.value and \
83                 (not plugins.firstRun or "session" in kwargs):
84
85                 from RSSPoller import RSSPoller
86                 rssPoller = RSSPoller()
87         elif reason == 1:
88                 if rssPoller is not None:
89                         rssPoller.shutdown()
90                         rssPoller = None
91
92 # Filescan
93 def filescan_open(item, session, **kwargs):
94         from RSSSetup import addFeed
95
96         # Add earch feed
97         for each in item:
98                 addFeed(each)
99
100         from Screens.MessageBox import MessageBox
101
102         # Display Message
103         session.open(
104                 MessageBox,
105                 _("%d Feed(s) were added to configuration.") % (len(item)),
106                 type = MessageBox.TYPE_INFO,
107                 timeout = 5
108         )
109
110 # Filescanner
111 def filescan(**kwargs):
112         from Components.Scanner import Scanner, ScanPath
113
114         # Overwrite checkFile to detect remote files
115         class RemoteScanner(Scanner):
116                 def checkFile(self, file):
117                         return file.path.startswith(("http://", "https://"))
118
119         return [
120                 RemoteScanner(
121                         mimetypes = ("application/rss+xml", "application/atom+xml"),
122                         paths_to_scan =
123                                 (
124                                         ScanPath(path = "", with_subdirs = False),
125                                 ),
126                         name = "RSS-Reader",
127                         description = _("Subscribe Newsfeed..."),
128                         openfnc = filescan_open,
129                 )
130         ]
131
132 def Plugins(**kwargs):
133         from Plugins.Plugin import PluginDescriptor
134         return [
135                 PluginDescriptor(
136                         name = "RSS Reader",
137                         description = _("A simple to use RSS reader"),
138                         where = PluginDescriptor.WHERE_PLUGINMENU,
139                         fnc=main,
140                         needsRestart=False,
141                 ),
142                 PluginDescriptor(
143                         where = [PluginDescriptor.WHERE_SESSIONSTART, PluginDescriptor.WHERE_AUTOSTART],
144                         fnc = autostart,
145                         needsRestart=False,
146                 ),
147                 PluginDescriptor(
148                         name = _("View RSS..."),
149                         description = "Let's you view current RSS entries",
150                         where = PluginDescriptor.WHERE_EXTENSIONSMENU,
151                         fnc=main,
152                         needsRestart=False,
153                 ),
154                 PluginDescriptor(
155                         where = PluginDescriptor.WHERE_FILESCAN,
156                         fnc = filescan,
157                         needsRestart=False,
158                 )
159         ]