Move download management from frontends to rss_sqlite.py.
[feedingit] / src / update_feeds.py
index 5122a5b..9516594 100644 (file)
@@ -2,6 +2,7 @@
 
 # 
 # Copyright (c) 2007-2008 INdT.
+# Copyright (c) 2011 Neal H. Walfield
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Lesser General Public License as published by
 # the Free Software Foundation, either version 3 of the License, or
 
 from rss_sqlite import Listing
 from config import Config
+from updatedbus import get_lock, UpdateServerObject
 
-import threading
 import os
 import gobject
+import traceback
+
+from jobmanager import JobManager
+import mainthread
 
 CONFIGDIR="/home/user/.feedingit/"
 #DESKTOP_FILE = "/usr/share/applications/hildon-status-menu/feedingit_status.desktop"
@@ -39,100 +44,59 @@ timeout = 5
 setdefaulttimeout(timeout)
 del timeout
 
-from updatedbus import UpdateServerObject, get_lock
-
-class Download(threading.Thread):
-    def __init__(self, config, dbusHandler):
-        global dbug
-        threading.Thread.__init__(self)
-        self.running = True
-        self.config = config
-        self.dbusHandler = dbusHandler
-        if dbug:
-            self.dbug = open(CONFIGDIR+"dbug.log", "w")
-        
-    def run(self):
-        global dbug
-        if dbug:
-            self.dbug.write("Starting updates\n")
-        try:
-            self.dbusHandler.UpdateStarted()
-            (use_proxy, proxy) = self.config.getProxy()
-            listing = Listing(CONFIGDIR)
-            for key in listing.getListOfFeeds():
-                if dbug:
-                    self.dbug.write("updating %s\n" %key)
-                try:
-                    if use_proxy:
-                        from urllib2 import install_opener, build_opener
-                        install_opener(build_opener(proxy))
-                        listing.updateFeed(key, self.config.getExpiry(), proxy=proxy, imageCache=self.config.getImageCache() )
-                    else:
-                        listing.updateFeed(key, self.config.getExpiry(), imageCache=self.config.getImageCache() )
-                except:
-                    import traceback
-                    file = open("/home/user/.feedingit/feedingit_update.log", "a")
-                    traceback.print_exc(file=file)
-                    file.close()
-                if not self.running:
-                    if dbug:
-                        self.dbug.write("received stopUpdate after %s\n" %key)
-                    break
-            self.dbusHandler.UpdateFinished()
-            self.dbusHandler.ArticleCountUpdated()
-            if dbug:
-                self.dbug.write("Dbus ArticleCountUpdated signal sent\n")
-        except:
-            import traceback
-            file = open("/home/user/.feedingit/feedingit_update.log", "a")
-            traceback.print_exc(file=file)
-            file.close()
-            #pass
-        if dbug:
-            self.dbug.write("About to main_quit\n")
-        mainloop.quit()
-        if dbug:
-            self.dbug.write("After main_quit\n")
-            self.dbug.close()
+from updatedbus import UpdateServerObject
+
+debug_file = None
+def debug(*args):
+    global debug_file
+    if not debug_file:
+        debug_file = open("/home/user/.feedingit/feedingit_update.log", "a")
+
+    debug_file.write (*args)
 
 class FeedUpdate():
     def __init__(self):
         self.config = Config(self, CONFIGDIR+"config.ini")
         self.dbusHandler = UpdateServerObject(self)
-        self.updateThread = False
-        
-    def automaticUpdate(self):
-        #self.listing.updateFeeds()
-        if self.updateThread == False:
-            self.updateThread = Download(self.config, self.dbusHandler)
-            self.updateThread.start()
+        self.listing = Listing(self.config, CONFIGDIR)
+        self.done = False
+
+        jm = JobManager(True)
+        jm.stats_hook_register (self.job_manager_update,
+                                run_in_main_thread=True)
+
+        self.dbusHandler.UpdateStarted()
+        for k in self.listing.getListOfFeeds():
+            self.listing.updateFeed (k)
         
-    def stopUpdate(self):
-        try:
-            self.updateThread.running = False
-        except:
+    def job_manager_update(self, jm, old_stats, new_stats, updated_feed):
+        if not new_stats['jobs-queued'] and not new_stats['jobs-in-progress']:
+            self.dbusHandler.UpdateFinished()
+            self.dbusHandler.ArticleCountUpdated()
+            self.done = True
             mainloop.quit()
 
+    def stopUpdate(self):
+        print "Stop update called."
+        JobManager().quit()
+
 import dbus.mainloop.glib
 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
 
 gobject.threads_init()
+mainthread.init()
 mainloop = gobject.MainLoop()
 
 app_lock = get_lock("app_lock")
 
 if app_lock != None:
-    try:
-        feed = FeedUpdate()
-        mainloop.run()
-        del app_lock
-    except:
-        import traceback
-        file = open("/home/user/.feedingit/feedingit_update.log", "w")
-        traceback.print_exc(file=file)
-        file.close()
+    feed = FeedUpdate()
+    while not feed.done:
+        try:
+            mainloop.run()
+        except KeyboardInterrupt:
+            print "Interrupted.  Quitting."
+            JobManager().quit()
+    del app_lock
 else:
-    file = open("/home/user/.feedingit/feedingit_update.log", "a")
-    file.write("Update in progress")
-    file.close()
-    
+    debug("Update in progress")