From 170dd6539a4b234d044daf8ccf2fdb7deddc5fc6 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 28 May 2010 14:52:05 -0600 Subject: [PATCH] Hook up open feedback and user feedback to the GUI --- src/calibre/devices/apple/driver.py | 21 +++++---------------- src/calibre/devices/errors.py | 17 ++++++++++++++--- src/calibre/devices/interface.py | 4 ++++ src/calibre/gui2/device.py | 4 ++++ src/calibre/gui2/ui.py | 11 +++++++++++ 5 files changed, 38 insertions(+), 19 deletions(-) diff --git a/src/calibre/devices/apple/driver.py b/src/calibre/devices/apple/driver.py index 2e7fbc46ea..7e9ca43ce2 100644 --- a/src/calibre/devices/apple/driver.py +++ b/src/calibre/devices/apple/driver.py @@ -17,6 +17,7 @@ from calibre.ptempfile import PersistentTemporaryFile from calibre.utils.config import Config, config_dir from calibre.utils.date import parse_date from calibre.utils.logging import Log +from calibre.devices.errors import UserFeedback from PIL import Image as PILImage @@ -26,30 +27,18 @@ if isosx: #if iswindows: # import win32com.client -class UserInteractionRequired(Exception): - pass - -class UserFeedback(Exception): - INFO = 0 - WARN = 1 - ERROR = 2 - - def __init__(self, msg, details, level): - Exception.__init__(self, msg) - self.level = level - self.details = details - self.msg = msg - class ITUNES(DevicePlugin): + name = 'Apple device interface' gui_name = 'Apple device' icon = I('devices/ipad.png') description = _('Communicate with iBooks through iTunes.') - supported_platforms = ['windows','osx'] + supported_platforms = ['osx'] author = 'GRiker' driver_version = '0.1' - OPEN_FEEDBACK_MESSAGE = _('Apple device detected, launching iTunes') + OPEN_FEEDBACK_MESSAGE = _( + 'Apple device detected, launching iTunes, please wait...') FORMATS = ['epub'] diff --git a/src/calibre/devices/errors.py b/src/calibre/devices/errors.py index 6ac76176f3..8191b15db6 100644 --- a/src/calibre/devices/errors.py +++ b/src/calibre/devices/errors.py @@ -22,9 +22,20 @@ class DeviceError(ProtocolError): """ Raised when device is not found """ def __init__(self, msg=None): if msg is None: - msg = "Unable to find SONY Reader. Is it connected?" + msg = "Unable to find SONY Reader. Is it connected?" ProtocolError.__init__(self, msg) +class UserFeedback(DeviceError): + INFO = 0 + WARN = 1 + ERROR = 2 + + def __init__(self, msg, details, level): + Exception.__init__(self, msg) + self.level = level + self.details = details + self.msg = msg + class DeviceBusy(ProtocolError): """ Raised when device is busy """ def __init__(self, uerr=""): @@ -57,8 +68,8 @@ class ControlError(ProtocolError): self.query = query self.response = response ProtocolError.__init__(self, desc) - - def __str__(self): + + def __str__(self): if self.query and self.response: return "Got unexpected response:\n" + \ "query:\n"+str(self.query.query)+"\n"+\ diff --git a/src/calibre/devices/interface.py b/src/calibre/devices/interface.py index 80c0b3d339..35617d8097 100644 --- a/src/calibre/devices/interface.py +++ b/src/calibre/devices/interface.py @@ -47,6 +47,10 @@ class DevicePlugin(Plugin): # Used by gui2.ui:annotations_fetched() and devices.kindle.driver:get_annotations() UserAnnotation = namedtuple('Annotation','type, value') + #: GUI displays this as a message if not None. Useful if opening can take a + #: long time + OPEN_FEEDBACK_MESSAGE = None + @classmethod def get_gui_name(cls): if hasattr(cls, 'gui_name'): diff --git a/src/calibre/gui2/device.py b/src/calibre/gui2/device.py index 41abc6cb95..283d3bf5ec 100644 --- a/src/calibre/gui2/device.py +++ b/src/calibre/gui2/device.py @@ -81,6 +81,8 @@ class DeviceJob(BaseJob): class DeviceManager(Thread): + open_feedback = pyqtSignal(object) + def __init__(self, connected_slot, job_manager, sleep_time=2): ''' :sleep_time: Time to sleep between device probes in secs @@ -114,6 +116,8 @@ class DeviceManager(Thread): def do_connect(self, connected_devices, is_folder_device): for dev, detected_device in connected_devices: + if dev.OPEN_FEEDBACK_MESSAGE is not None: + self.open_feedback.emit(dev.OPEN_FEEDBACK_MESSAGE) dev.reset(detected_device=detected_device, report_progress=self.report_progress) try: diff --git a/src/calibre/gui2/ui.py b/src/calibre/gui2/ui.py index 91b2353469..8669aa55eb 100644 --- a/src/calibre/gui2/ui.py +++ b/src/calibre/gui2/ui.py @@ -30,6 +30,7 @@ from calibre.ptempfile import PersistentTemporaryFile from calibre.utils.config import prefs, dynamic from calibre.utils.ipc.server import Server from calibre.utils.search_query_parser import saved_searches +from calibre.devices.errors import UserFeedback from calibre.gui2 import warning_dialog, choose_files, error_dialog, \ question_dialog,\ pixmap_to_data, choose_dir, \ @@ -234,6 +235,8 @@ class Main(MainWindow, Ui_MainWindow, DeviceGUI): ####################### Setup device detection ######################## self.device_manager = DeviceManager(Dispatcher(self.device_detected), self.job_manager) + self.device_manager.open_feedback.connect(self.status.showMessage, + type=Qt.QueuedConnection) self.device_manager.start() @@ -2327,6 +2330,14 @@ class Main(MainWindow, Ui_MainWindow, DeviceGUI): ''' Handle exceptions in threaded device jobs. ''' + if isinstance(getattr(job, 'exception', None), UserFeedback): + ex = job.exception + func = {UserFeedback.ERROR:error_dialog, + UserFeedback.WARNING:warning_dialog, + UserFeedback.INFO:info_dialog}[ex.level] + return func(self, _('Failed'), ex.msg, det_msg=ex.details if + ex.details else '', show=True) + try: if 'Could not read 32 bytes on the control bus.' in \ unicode(job.details):