Move download management from frontends to rss_sqlite.py.
[feedingit] / src / update_feeds.py
1 #!/usr/bin/env python2.5
2
3
4 # Copyright (c) 2007-2008 INdT.
5 # Copyright (c) 2011 Neal H. Walfield
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Lesser General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 #  This program is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #  GNU Lesser General Public License for more details.
15 #
16 #  You should have received a copy of the GNU Lesser General Public License
17 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19
20 # ============================================================================
21 # Name        : update_feeds.py
22 # Author      : Yves Marcoz
23 # Version     : 0.6.1
24 # Description : Simple RSS Reader
25 # ============================================================================
26
27 from rss_sqlite import Listing
28 from config import Config
29 from updatedbus import get_lock, UpdateServerObject
30
31 import os
32 import gobject
33 import traceback
34
35 from jobmanager import JobManager
36 import mainthread
37
38 CONFIGDIR="/home/user/.feedingit/"
39 #DESKTOP_FILE = "/usr/share/applications/hildon-status-menu/feedingit_status.desktop"
40 dbug = False
41
42 from socket import setdefaulttimeout
43 timeout = 5
44 setdefaulttimeout(timeout)
45 del timeout
46
47 from updatedbus import UpdateServerObject
48
49 debug_file = None
50 def debug(*args):
51     global debug_file
52     if not debug_file:
53         debug_file = open("/home/user/.feedingit/feedingit_update.log", "a")
54
55     debug_file.write (*args)
56
57 class FeedUpdate():
58     def __init__(self):
59         self.config = Config(self, CONFIGDIR+"config.ini")
60         self.dbusHandler = UpdateServerObject(self)
61         self.listing = Listing(self.config, CONFIGDIR)
62         self.done = False
63
64         jm = JobManager(True)
65         jm.stats_hook_register (self.job_manager_update,
66                                 run_in_main_thread=True)
67
68         self.dbusHandler.UpdateStarted()
69         for k in self.listing.getListOfFeeds():
70             self.listing.updateFeed (k)
71         
72     def job_manager_update(self, jm, old_stats, new_stats, updated_feed):
73         if not new_stats['jobs-queued'] and not new_stats['jobs-in-progress']:
74             self.dbusHandler.UpdateFinished()
75             self.dbusHandler.ArticleCountUpdated()
76             self.done = True
77             mainloop.quit()
78
79     def stopUpdate(self):
80         print "Stop update called."
81         JobManager().quit()
82
83 import dbus.mainloop.glib
84 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
85
86 gobject.threads_init()
87 mainthread.init()
88 mainloop = gobject.MainLoop()
89
90 app_lock = get_lock("app_lock")
91
92 if app_lock != None:
93     feed = FeedUpdate()
94     while not feed.done:
95         try:
96             mainloop.run()
97         except KeyboardInterrupt:
98             print "Interrupted.  Quitting."
99             JobManager().quit()
100     del app_lock
101 else:
102     debug("Update in progress")