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 _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

View File

@ -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])

View File

@ -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. <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>' +
_('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.') + '</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>' +
_('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.') + '</p>')
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)

View File

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

View File

@ -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)