Move update check into its own thread

This commit is contained in:
Kovid Goyal 2010-05-05 12:17:58 -06:00
parent 941849c58f
commit b64092f881
2 changed files with 25 additions and 29 deletions

View File

@ -258,9 +258,9 @@ class Main(MainWindow, Ui_MainWindow, DeviceGUI):
self.device_info = ' ' self.device_info = ' '
if not opts.no_update_check: if not opts.no_update_check:
self.update_checker = CheckForUpdates(self) self.update_checker = CheckForUpdates(self)
QObject.connect(self.update_checker, self.update_checker.update_found.connect(self.update_found,
SIGNAL('update_found(PyQt_PyObject)'), self.update_found) type=Qt.QueuedConnection)
self.update_checker.start(2000) self.update_checker.start()
####################### Status Bar ##################### ####################### Status Bar #####################
self.status_bar.initialize(self.system_tray_icon) self.status_bar.initialize(self.system_tray_icon)
self.status_bar.show_book_info.connect(self.show_book_info) self.status_bar.show_book_info.connect(self.show_book_info)
@ -2493,7 +2493,7 @@ class Main(MainWindow, Ui_MainWindow, DeviceGUI):
if write_settings: if write_settings:
self.write_settings() self.write_settings()
self.check_messages_timer.stop() self.check_messages_timer.stop()
self.update_checker.stop() self.update_checker.terminate()
self.listener.close() self.listener.close()
self.job_manager.server.close() self.job_manager.server.close()
while self.spare_servers: while self.spare_servers:

View File

@ -3,7 +3,7 @@ __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
import traceback import traceback
from PyQt4.QtCore import QObject, SIGNAL, QTimer from PyQt4.QtCore import QThread, pyqtSignal
import mechanize import mechanize
from calibre.constants import __version__, iswindows, isosx from calibre.constants import __version__, iswindows, isosx
@ -12,31 +12,27 @@ from calibre.utils.config import prefs
URL = 'http://status.calibre-ebook.com/latest' URL = 'http://status.calibre-ebook.com/latest'
class CheckForUpdates(QObject): class CheckForUpdates(QThread):
update_found = pyqtSignal(object)
INTERVAL = 24*60*60
def __init__(self, parent): def __init__(self, parent):
QObject.__init__(self, parent) QThread.__init__(self, parent)
self.timer = QTimer(self)
self.first = True
self.connect(self.timer, SIGNAL('timeout()'), self)
self.start = self.timer.start
self.stop = self.timer.stop
def __call__(self): def run(self):
if self.first: while True:
self.timer.setInterval(1000*24*60*60) try:
self.first = False br = browser()
req = mechanize.Request(URL)
try: req.add_header('CALIBRE_VERSION', __version__)
br = browser() req.add_header('CALIBRE_OS',
req = mechanize.Request(URL) 'win' if iswindows else 'osx' if isosx else 'oth')
req.add_header('CALIBRE_VERSION', __version__) req.add_header('CALIBRE_INSTALL_UUID', prefs['installation_uuid'])
req.add_header('CALIBRE_OS', version = br.open(req).read().strip()
'win' if iswindows else 'osx' if isosx else 'oth') if version and version != __version__:
req.add_header('CALIBRE_INSTALL_UUID', prefs['installation_uuid']) self.update_found.emit(version)
version = br.open(req).read().strip() except:
if version and version != __version__: traceback.print_exc()
self.emit(SIGNAL('update_found(PyQt_PyObject)'), version) self.sleep(self.INTERVAL)
except:
traceback.print_exc()