From 86d858488c4562368bdbdcf349580f472f8ef9d0 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 1 Aug 2012 15:35:33 +0530 Subject: [PATCH] Refactor smart device menu entry to only show a dialog box when starting the device, not when stopping it. --- .../devices/smart_device_app/driver.py | 12 ++-- src/calibre/gui2/actions/device.py | 29 +++++--- src/calibre/gui2/dialogs/smartdevice.py | 33 +--------- src/calibre/gui2/dialogs/smartdevice.ui | 66 +++++++++---------- src/calibre/gui2/ui.py | 6 +- 5 files changed, 64 insertions(+), 82 deletions(-) diff --git a/src/calibre/devices/smart_device_app/driver.py b/src/calibre/devices/smart_device_app/driver.py index b7857b14d4..072d8dfc99 100644 --- a/src/calibre/devices/smart_device_app/driver.py +++ b/src/calibre/devices/smart_device_app/driver.py @@ -42,6 +42,10 @@ def synchronous(tlockname): return _synchronizer return _synched +def do_zeroconf(f, port): + f('calibre smart device client', + '_calibresmartdeviceapp._tcp', port, {}) + class SMART_DEVICE_APP(DeviceConfig, DevicePlugin): name = 'SmartDevice App Interface' @@ -788,7 +792,7 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin): self._debug('creation of listen socket failed') return - for i in range(0, 100): # try up to 100 random port numbers + for i in xrange(100): # try up to 100 random port numbers port = random.randint(8192, 32000) try: self._debug('try port', port) @@ -815,8 +819,7 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin): return try: - publish_zeroconf('calibre smart device client', - '_calibresmartdeviceapp._tcp', port, {}) + do_zeroconf(publish_zeroconf, port) except: self._debug('registration with bonjour failed') self.listen_socket.close() @@ -829,10 +832,9 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin): @synchronous('sync_lock') def shutdown(self): if getattr(self, 'listen_socket', None) is not None: + do_zeroconf(unpublish_zeroconf, self.port) self.listen_socket.close() self.listen_socket = None - unpublish_zeroconf('calibre smart device client', - '_calibresmartdeviceapp._tcp', self.port, {}) # Methods for dynamic control diff --git a/src/calibre/gui2/actions/device.py b/src/calibre/gui2/actions/device.py index 92ed77e324..f8c8e51f04 100644 --- a/src/calibre/gui2/actions/device.py +++ b/src/calibre/gui2/actions/device.py @@ -28,6 +28,9 @@ class ShareConnMenu(QMenu): # {{{ control_smartdevice = pyqtSignal() dont_add_to = frozenset(['context-menu-device']) + DEVICE_MSGS = [_('Start wireless device connection'), + _('Stop wireless device connection')] + def __init__(self, parent=None): QMenu.__init__(self, parent) mitem = self.addAction(QIcon(I('devices/folder.png')), _('Connect to folder')) @@ -59,8 +62,8 @@ class ShareConnMenu(QMenu): # {{{ self.toggle_server_action.triggered.connect(lambda x: self.toggle_server.emit()) self.control_smartdevice_action = \ - self.addAction(QIcon(I('dot_green.png')), - _('Control Smart Device Connections')) + self.addAction(QIcon(I('dot_red.png')), + self.DEVICE_MSGS[0]) self.control_smartdevice_action.triggered.connect(lambda x: self.control_smartdevice.emit()) self.addSeparator() @@ -215,17 +218,23 @@ class ConnectShareAction(InterfaceAction): self.stopping_msg.accept() def control_smartdevice(self): - sd_dialog = SmartdeviceDialog(self.gui) - sd_dialog.exec_() - self.set_smartdevice_icon() + dm = self.gui.device_manager + running = dm.is_running('smartdevice') + if running: + dm.stop_plugin('smartdevice') + else: + sd_dialog = SmartdeviceDialog(self.gui) + sd_dialog.exec_() + self.set_smartdevice_action_state() def check_smartdevice_menus(self): if not self.gui.device_manager.is_enabled('smartdevice'): self.share_conn_menu.hide_smartdevice_menus() - def set_smartdevice_icon(self): + def set_smartdevice_action_state(self): running = self.gui.device_manager.is_running('smartdevice') - if running: - self.share_conn_menu.control_smartdevice_action.setIcon(QIcon(I('dot_green.png'))) - else: - self.share_conn_menu.control_smartdevice_action.setIcon(QIcon(I('dot_red.png'))) + ac = self.share_conn_menu.control_smartdevice_action + text, icon = (1, 'green') if running else (0, 'red') + ac.setIcon(QIcon(I('dot_%s.png'%icon))) + ac.setText(self.share_conn_menu.DEVICE_MSGS[text]) + diff --git a/src/calibre/gui2/dialogs/smartdevice.py b/src/calibre/gui2/dialogs/smartdevice.py index ebdacc7781..b9e16cfcd3 100644 --- a/src/calibre/gui2/dialogs/smartdevice.py +++ b/src/calibre/gui2/dialogs/smartdevice.py @@ -13,54 +13,28 @@ class SmartdeviceDialog(QDialog, Ui_Dialog): Ui_Dialog.__init__(self) self.setupUi(self) - self.msg.setText( - _('This dialog starts and stops the smart device app interface. ' - 'When you start the interface, you might see some messages from ' - 'your computer\'s firewall or anti-virus manager asking you ' - 'if it is OK for calibre to connect to the network. Please ' - 'answer yes. If you do not, the app will not work. It will ' - 'be unable to connect to calibre.')) - self.password_box.setToolTip('

' + _('Use a password if calibre is running on a network that ' 'is not secure. For example, if you run calibre on a laptop, ' 'use that laptop in an airport, and want to connect your ' 'smart device to calibre, you should use a password.') + '

') - self.run_box.setToolTip('

' + - _('Check this box to allow calibre to accept connections from the ' - 'smart device. Uncheck the box to prevent connections.') + '

') - self.autostart_box.setToolTip('

' + _('Check this box if you want calibre to automatically start the ' 'smart device interface when calibre starts. You should not do ' 'this if you are using a network that is not secure and you ' 'are not setting a password.') + '

') self.connect(self.show_password, SIGNAL('stateChanged(int)'), self.toggle_password) - self.autostart_box.stateChanged.connect(self.autostart_changed) self.device_manager = parent.device_manager - if self.device_manager.is_running('smartdevice'): - self.run_box.setChecked(True) - else: - self.run_box.setChecked(False) if self.device_manager.get_option('smartdevice', 'autostart'): self.autostart_box.setChecked(True) - self.run_box.setChecked(True) - self.run_box.setEnabled(False) pw = self.device_manager.get_option('smartdevice', 'password') if pw: self.password_box.setText(pw) - def autostart_changed(self): - if self.autostart_box.isChecked(): - self.run_box.setChecked(True) - self.run_box.setEnabled(False) - else: - self.run_box.setEnabled(True) - def toggle_password(self, state): if state == Qt.Unchecked: self.password_box.setEchoMode(QLineEdit.Password) @@ -72,9 +46,6 @@ class SmartdeviceDialog(QDialog, Ui_Dialog): unicode(self.password_box.text())) self.device_manager.set_option('smartdevice', 'autostart', self.autostart_box.isChecked()) - if self.run_box.isChecked(): - self.device_manager.start_plugin('smartdevice') - else: - self.device_manager.stop_plugin('smartdevice') - + self.device_manager.start_plugin('smartdevice') QDialog.accept(self) + diff --git a/src/calibre/gui2/dialogs/smartdevice.ui b/src/calibre/gui2/dialogs/smartdevice.ui index 9ac44b4dd1..0e31cd12ac 100644 --- a/src/calibre/gui2/dialogs/smartdevice.ui +++ b/src/calibre/gui2/dialogs/smartdevice.ui @@ -7,7 +7,7 @@ 0 0 600 - 209 + 226 @@ -15,23 +15,26 @@ - :/images/mimetypes/unknown.png:/images/mimetypes/unknown.png + :/images/devices/galaxy_s3.png:/images/devices/galaxy_s3.png - - - - &Automatically allow connections at startup + + + + + 500 + 0 + - - - - - - - 0 - 100 - + + QLabel { margin-bottom: 1ex; } + + + <p>Start wireless device connections. + <p>You may see some messages from your computer's firewall or anti-virus manager asking you if it is OK for calibre to connect to the network. <b>Please answer yes</b>. If you do not, wireless connections will not work. + + + true @@ -51,23 +54,6 @@ - - - - &Allow connections - - - - - - - TextLabel - - - true - - - @@ -85,7 +71,7 @@ - + Qt::Horizontal @@ -95,6 +81,20 @@ + + + + &Automatically allow connections at startup + + + + + + + &Allow connections + + + diff --git a/src/calibre/gui2/ui.py b/src/calibre/gui2/ui.py index f07fb0d211..67415bee9e 100644 --- a/src/calibre/gui2/ui.py +++ b/src/calibre/gui2/ui.py @@ -339,14 +339,14 @@ class Main(MainWindow, MainWindowMixin, DeviceMixin, EmailMixin, # {{{ if config['autolaunch_server']: self.start_content_server() - smartdevice_actions = self.iactions['Connect Share'] - smartdevice_actions.check_smartdevice_menus() + smartdevice_action = self.iactions['Connect Share'] + smartdevice_action.check_smartdevice_menus() if self.device_manager.get_option('smartdevice', 'autostart'): try: self.device_manager.start_plugin('smartdevice') except: pass - smartdevice_actions.set_smartdevice_icon() + smartdevice_action.set_smartdevice_action_state() self.keyboard_interrupt.connect(self.quit, type=Qt.QueuedConnection)