Refactor smart device menu entry to only show a dialog box when starting the device, not when stopping it.

This commit is contained in:
Kovid Goyal 2012-08-01 15:35:33 +05:30
parent 2848314bf9
commit 86d858488c
5 changed files with 64 additions and 82 deletions

View File

@ -42,6 +42,10 @@ def synchronous(tlockname):
return _synchronizer return _synchronizer
return _synched return _synched
def do_zeroconf(f, port):
f('calibre smart device client',
'_calibresmartdeviceapp._tcp', port, {})
class SMART_DEVICE_APP(DeviceConfig, DevicePlugin): class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
name = 'SmartDevice App Interface' name = 'SmartDevice App Interface'
@ -788,7 +792,7 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
self._debug('creation of listen socket failed') self._debug('creation of listen socket failed')
return 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) port = random.randint(8192, 32000)
try: try:
self._debug('try port', port) self._debug('try port', port)
@ -815,8 +819,7 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
return return
try: try:
publish_zeroconf('calibre smart device client', do_zeroconf(publish_zeroconf, port)
'_calibresmartdeviceapp._tcp', port, {})
except: except:
self._debug('registration with bonjour failed') self._debug('registration with bonjour failed')
self.listen_socket.close() self.listen_socket.close()
@ -829,10 +832,9 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
@synchronous('sync_lock') @synchronous('sync_lock')
def shutdown(self): def shutdown(self):
if getattr(self, 'listen_socket', None) is not None: if getattr(self, 'listen_socket', None) is not None:
do_zeroconf(unpublish_zeroconf, self.port)
self.listen_socket.close() self.listen_socket.close()
self.listen_socket = None self.listen_socket = None
unpublish_zeroconf('calibre smart device client',
'_calibresmartdeviceapp._tcp', self.port, {})
# Methods for dynamic control # Methods for dynamic control

View File

@ -28,6 +28,9 @@ class ShareConnMenu(QMenu): # {{{
control_smartdevice = pyqtSignal() control_smartdevice = pyqtSignal()
dont_add_to = frozenset(['context-menu-device']) dont_add_to = frozenset(['context-menu-device'])
DEVICE_MSGS = [_('Start wireless device connection'),
_('Stop wireless device connection')]
def __init__(self, parent=None): def __init__(self, parent=None):
QMenu.__init__(self, parent) QMenu.__init__(self, parent)
mitem = self.addAction(QIcon(I('devices/folder.png')), _('Connect to folder')) 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_action.triggered.connect(lambda x:
self.toggle_server.emit()) self.toggle_server.emit())
self.control_smartdevice_action = \ self.control_smartdevice_action = \
self.addAction(QIcon(I('dot_green.png')), self.addAction(QIcon(I('dot_red.png')),
_('Control Smart Device Connections')) self.DEVICE_MSGS[0])
self.control_smartdevice_action.triggered.connect(lambda x: self.control_smartdevice_action.triggered.connect(lambda x:
self.control_smartdevice.emit()) self.control_smartdevice.emit())
self.addSeparator() self.addSeparator()
@ -215,17 +218,23 @@ class ConnectShareAction(InterfaceAction):
self.stopping_msg.accept() self.stopping_msg.accept()
def control_smartdevice(self): def control_smartdevice(self):
sd_dialog = SmartdeviceDialog(self.gui) dm = self.gui.device_manager
sd_dialog.exec_() running = dm.is_running('smartdevice')
self.set_smartdevice_icon() 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): def check_smartdevice_menus(self):
if not self.gui.device_manager.is_enabled('smartdevice'): if not self.gui.device_manager.is_enabled('smartdevice'):
self.share_conn_menu.hide_smartdevice_menus() 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') running = self.gui.device_manager.is_running('smartdevice')
if running: ac = self.share_conn_menu.control_smartdevice_action
self.share_conn_menu.control_smartdevice_action.setIcon(QIcon(I('dot_green.png'))) text, icon = (1, 'green') if running else (0, 'red')
else: ac.setIcon(QIcon(I('dot_%s.png'%icon)))
self.share_conn_menu.control_smartdevice_action.setIcon(QIcon(I('dot_red.png'))) ac.setText(self.share_conn_menu.DEVICE_MSGS[text])

View File

@ -13,54 +13,28 @@ class SmartdeviceDialog(QDialog, Ui_Dialog):
Ui_Dialog.__init__(self) Ui_Dialog.__init__(self)
self.setupUi(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. <B>Please '
'answer yes</b>. If you do not, the app will not work. It will '
'be unable to connect to calibre.'))
self.password_box.setToolTip('<p>' + self.password_box.setToolTip('<p>' +
_('Use a password if calibre is running on a network that ' _('Use a password if calibre is running on a network that '
'is not secure. For example, if you run calibre on a laptop, ' 'is not secure. For example, if you run calibre on a laptop, '
'use that laptop in an airport, and want to connect your ' 'use that laptop in an airport, and want to connect your '
'smart device to calibre, you should use a password.') + '</p>') 'smart device to calibre, you should use a password.') + '</p>')
self.run_box.setToolTip('<p>' +
_('Check this box to allow calibre to accept connections from the '
'smart device. Uncheck the box to prevent connections.') + '</p>')
self.autostart_box.setToolTip('<p>' + self.autostart_box.setToolTip('<p>' +
_('Check this box if you want calibre to automatically start the ' _('Check this box if you want calibre to automatically start the '
'smart device interface when calibre starts. You should not do ' 'smart device interface when calibre starts. You should not do '
'this if you are using a network that is not secure and you ' 'this if you are using a network that is not secure and you '
'are not setting a password.') + '</p>') 'are not setting a password.') + '</p>')
self.connect(self.show_password, SIGNAL('stateChanged(int)'), self.toggle_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 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'): if self.device_manager.get_option('smartdevice', 'autostart'):
self.autostart_box.setChecked(True) self.autostart_box.setChecked(True)
self.run_box.setChecked(True)
self.run_box.setEnabled(False)
pw = self.device_manager.get_option('smartdevice', 'password') pw = self.device_manager.get_option('smartdevice', 'password')
if pw: if pw:
self.password_box.setText(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): def toggle_password(self, state):
if state == Qt.Unchecked: if state == Qt.Unchecked:
self.password_box.setEchoMode(QLineEdit.Password) self.password_box.setEchoMode(QLineEdit.Password)
@ -72,9 +46,6 @@ class SmartdeviceDialog(QDialog, Ui_Dialog):
unicode(self.password_box.text())) unicode(self.password_box.text()))
self.device_manager.set_option('smartdevice', 'autostart', self.device_manager.set_option('smartdevice', 'autostart',
self.autostart_box.isChecked()) self.autostart_box.isChecked())
if self.run_box.isChecked(): self.device_manager.start_plugin('smartdevice')
self.device_manager.start_plugin('smartdevice')
else:
self.device_manager.stop_plugin('smartdevice')
QDialog.accept(self) QDialog.accept(self)

View File

@ -7,7 +7,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>600</width> <width>600</width>
<height>209</height> <height>226</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@ -15,23 +15,26 @@
</property> </property>
<property name="windowIcon"> <property name="windowIcon">
<iconset resource="../../../../resources/images.qrc"> <iconset resource="../../../../resources/images.qrc">
<normaloff>:/images/mimetypes/unknown.png</normaloff>:/images/mimetypes/unknown.png</iconset> <normaloff>:/images/devices/galaxy_s3.png</normaloff>:/images/devices/galaxy_s3.png</iconset>
</property> </property>
<layout class="QGridLayout"> <layout class="QGridLayout">
<item row="4" column="1"> <item row="0" column="0" colspan="3">
<widget class="QCheckBox" name="autostart_box"> <widget class="QLabel" name="msg">
<property name="text"> <property name="minimumSize">
<string>&amp;Automatically allow connections at startup</string> <size>
<width>500</width>
<height>0</height>
</size>
</property> </property>
</widget> <property name="styleSheet">
</item> <string notr="true">QLabel { margin-bottom: 1ex; }</string>
<item row="5" column="1"> </property>
<widget class="QLabel" name="label_43"> <property name="text">
<property name="sizePolicy"> <string>&lt;p&gt;Start wireless device connections.
<sizepolicy hsizetype="Expanding" vsizetype="Expanding"> &lt;p&gt;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. &lt;b&gt;Please answer yes&lt;/b&gt;. If you do not, wireless connections will not work.</string>
<horstretch>0</horstretch> </property>
<verstretch>100</verstretch> <property name="wordWrap">
</sizepolicy> <bool>true</bool>
</property> </property>
</widget> </widget>
</item> </item>
@ -51,23 +54,6 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="3" column="1">
<widget class="QCheckBox" name="run_box">
<property name="text">
<string>&amp;Allow connections</string>
</property>
</widget>
</item>
<item row="0" column="0" colspan="2">
<widget class="QLabel" name="msg">
<property name="text">
<string>TextLabel</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0"> <item row="1" column="0">
<widget class="QLabel" name="label_2"> <widget class="QLabel" name="label_2">
<property name="text"> <property name="text">
@ -85,7 +71,7 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="6" column="0" colspan="3"> <item row="5" column="0" colspan="3">
<widget class="QDialogButtonBox" name="buttonBox"> <widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation"> <property name="orientation">
<enum>Qt::Horizontal</enum> <enum>Qt::Horizontal</enum>
@ -95,6 +81,20 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="4" column="1" colspan="2">
<widget class="QCheckBox" name="autostart_box">
<property name="text">
<string>&amp;Automatically allow connections at startup</string>
</property>
</widget>
</item>
<item row="3" column="1" colspan="2">
<widget class="QCheckBox" name="run_box">
<property name="text">
<string>&amp;Allow connections</string>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
<resources> <resources>

View File

@ -339,14 +339,14 @@ class Main(MainWindow, MainWindowMixin, DeviceMixin, EmailMixin, # {{{
if config['autolaunch_server']: if config['autolaunch_server']:
self.start_content_server() self.start_content_server()
smartdevice_actions = self.iactions['Connect Share'] smartdevice_action = self.iactions['Connect Share']
smartdevice_actions.check_smartdevice_menus() smartdevice_action.check_smartdevice_menus()
if self.device_manager.get_option('smartdevice', 'autostart'): if self.device_manager.get_option('smartdevice', 'autostart'):
try: try:
self.device_manager.start_plugin('smartdevice') self.device_manager.start_plugin('smartdevice')
except: except:
pass pass
smartdevice_actions.set_smartdevice_icon() smartdevice_action.set_smartdevice_action_state()
self.keyboard_interrupt.connect(self.quit, type=Qt.QueuedConnection) self.keyboard_interrupt.connect(self.quit, type=Qt.QueuedConnection)