mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Add use of netifaces to show multiple IP addresses
This commit is contained in:
parent
cddf873db2
commit
ffde6a4a04
@ -237,20 +237,28 @@ class ConnectShareAction(InterfaceAction):
|
|||||||
self.share_conn_menu.hide_smartdevice_menus()
|
self.share_conn_menu.hide_smartdevice_menus()
|
||||||
|
|
||||||
def set_smartdevice_action_state(self):
|
def set_smartdevice_action_state(self):
|
||||||
from calibre.utils.mdns import get_external_ip
|
from calibre.gui2.dialogs.smartdevice import get_all_ip_addresses
|
||||||
dm = self.gui.device_manager
|
dm = self.gui.device_manager
|
||||||
|
|
||||||
|
all_ips = get_all_ip_addresses()
|
||||||
|
if len(all_ips) > 3:
|
||||||
|
formatted_addresses = _('Many IP addresses. See Start/Stop dialog.')
|
||||||
|
show_port = False
|
||||||
|
else:
|
||||||
|
formatted_addresses = ' or '.join(get_all_ip_addresses())
|
||||||
|
show_port = True
|
||||||
|
|
||||||
running = dm.is_running('smartdevice')
|
running = dm.is_running('smartdevice')
|
||||||
if not running:
|
if not running:
|
||||||
text = self.share_conn_menu.DEVICE_MSGS[0]
|
text = self.share_conn_menu.DEVICE_MSGS[0]
|
||||||
else:
|
else:
|
||||||
use_fixed_port = dm.get_option('smartdevice', 'use_fixed_port')
|
use_fixed_port = dm.get_option('smartdevice', 'use_fixed_port')
|
||||||
port_number = dm.get_option('smartdevice', 'port_number')
|
port_number = dm.get_option('smartdevice', 'port_number')
|
||||||
if use_fixed_port:
|
if show_port and use_fixed_port:
|
||||||
text = self.share_conn_menu.DEVICE_MSGS[1] + ' [%s port %s]'%(
|
text = self.share_conn_menu.DEVICE_MSGS[1] + ' [%s port %s]'%(
|
||||||
get_external_ip(), port_number)
|
formatted_addresses, port_number)
|
||||||
else:
|
else:
|
||||||
text = self.share_conn_menu.DEVICE_MSGS[1] + ' [%s]'%get_external_ip()
|
text = self.share_conn_menu.DEVICE_MSGS[1] + ' [' + formatted_addresses + ']'
|
||||||
|
|
||||||
icon = 'green' if running else 'red'
|
icon = 'green' if running else 'red'
|
||||||
ac = self.share_conn_menu.control_smartdevice_action
|
ac = self.share_conn_menu.control_smartdevice_action
|
||||||
|
@ -5,12 +5,47 @@ from __future__ import (unicode_literals, division, absolute_import,
|
|||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||||
|
|
||||||
|
import netifaces, socket
|
||||||
|
|
||||||
from PyQt4.Qt import (QDialog, QLineEdit, Qt)
|
from PyQt4.Qt import (QDialog, QLineEdit, Qt)
|
||||||
|
|
||||||
from calibre.gui2 import error_dialog
|
from calibre.gui2 import error_dialog
|
||||||
from calibre.gui2.dialogs.smartdevice_ui import Ui_Dialog
|
from calibre.gui2.dialogs.smartdevice_ui import Ui_Dialog
|
||||||
from calibre.utils.config import prefs
|
from calibre.utils.config import prefs
|
||||||
|
|
||||||
|
def _cmp_ipaddr(l, r):
|
||||||
|
lparts = ['%3s'%x for x in l.split('.')]
|
||||||
|
rparts = ['%3s'%x for x in r.split('.')]
|
||||||
|
|
||||||
|
if lparts[0] in ['192', '170', ' 10']:
|
||||||
|
if rparts[0] not in ['192', '170', '10']:
|
||||||
|
return -1
|
||||||
|
return cmp(rparts, lparts)
|
||||||
|
|
||||||
|
if rparts[0] in ['192', '170', ' 10']:
|
||||||
|
return 1
|
||||||
|
|
||||||
|
return cmp(lparts, rparts)
|
||||||
|
|
||||||
|
def get_all_ip_addresses():
|
||||||
|
ip_info = [netifaces.ifaddresses(x).get(netifaces.AF_INET, None)
|
||||||
|
for x in netifaces.interfaces()]
|
||||||
|
|
||||||
|
all_ipaddrs = list()
|
||||||
|
for iface in ip_info:
|
||||||
|
if iface is not None:
|
||||||
|
for addrs in iface:
|
||||||
|
if 'netmask' in addrs and addrs['addr'] != '127.0.0.1':
|
||||||
|
# We get VPN interfaces that were connected and then
|
||||||
|
# disconnected. Oh well. At least the 'right' IP addr
|
||||||
|
# is there.
|
||||||
|
all_ipaddrs.append(addrs['addr'])
|
||||||
|
|
||||||
|
all_ipaddrs.sort(cmp=_cmp_ipaddr)
|
||||||
|
print(all_ipaddrs)
|
||||||
|
return all_ipaddrs
|
||||||
|
|
||||||
|
|
||||||
class SmartdeviceDialog(QDialog, Ui_Dialog):
|
class SmartdeviceDialog(QDialog, Ui_Dialog):
|
||||||
|
|
||||||
def __init__(self, parent):
|
def __init__(self, parent):
|
||||||
@ -49,6 +84,15 @@ class SmartdeviceDialog(QDialog, Ui_Dialog):
|
|||||||
'choice at Preferences -> Send to device -> Metadata management')
|
'choice at Preferences -> Send to device -> Metadata management')
|
||||||
+ '</p>')
|
+ '</p>')
|
||||||
|
|
||||||
|
self.ip_addresses.setToolTip('<p>' +
|
||||||
|
_('These are the IP addresses detected by calibre for the computer '
|
||||||
|
'running calibre. If you decide to have your device connect to '
|
||||||
|
'calibre using a fixed IP address, one of these addresses should '
|
||||||
|
'be the one you use. It is unlikely but possible that the correct '
|
||||||
|
'IP address is not listed here, in which case you will need to go '
|
||||||
|
"to your computer's control panel to get a complete list of "
|
||||||
|
"your computer's network interfaces and IP addresses.") + '</p>')
|
||||||
|
|
||||||
self.show_password.stateChanged[int].connect(self.toggle_password)
|
self.show_password.stateChanged[int].connect(self.toggle_password)
|
||||||
self.use_fixed_port.stateChanged[int].connect(self.use_fixed_port_changed)
|
self.use_fixed_port.stateChanged[int].connect(self.use_fixed_port_changed)
|
||||||
|
|
||||||
@ -79,6 +123,8 @@ class SmartdeviceDialog(QDialog, Ui_Dialog):
|
|||||||
self.enable_auto_management_box.setEnabled(False)
|
self.enable_auto_management_box.setEnabled(False)
|
||||||
self.auto_management_is_set = True
|
self.auto_management_is_set = True
|
||||||
|
|
||||||
|
self.ip_addresses.setText(', '.join(get_all_ip_addresses()))
|
||||||
|
|
||||||
self.resize(self.sizeHint())
|
self.resize(self.sizeHint())
|
||||||
|
|
||||||
def use_fixed_port_changed(self, state):
|
def use_fixed_port_changed(self, state):
|
||||||
|
@ -38,7 +38,34 @@
|
|||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
<item row="1" column="0">
|
||||||
|
<widget class="QLabel" name="label_23">
|
||||||
|
<property name="text">
|
||||||
|
<string>Calibre's IP addresses:</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
<item row="1" column="1">
|
<item row="1" column="1">
|
||||||
|
<widget class="QLabel" name="ip_addresses">
|
||||||
|
<property name="text">
|
||||||
|
<string>Possibe IP addresses:</string>
|
||||||
|
</property>
|
||||||
|
<property name="wordWrap">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="2" column="0">
|
||||||
|
<widget class="QLabel" name="label_2">
|
||||||
|
<property name="text">
|
||||||
|
<string>Optional &password:</string>
|
||||||
|
</property>
|
||||||
|
<property name="buddy">
|
||||||
|
<cstring>password_box</cstring>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="2" column="1">
|
||||||
<widget class="QLineEdit" name="password_box">
|
<widget class="QLineEdit" name="password_box">
|
||||||
<property name="sizePolicy">
|
<property name="sizePolicy">
|
||||||
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
|
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
|
||||||
@ -54,24 +81,14 @@
|
|||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="1" column="0">
|
<item row="2" column="2">
|
||||||
<widget class="QLabel" name="label_2">
|
|
||||||
<property name="text">
|
|
||||||
<string>Optional &password:</string>
|
|
||||||
</property>
|
|
||||||
<property name="buddy">
|
|
||||||
<cstring>password_box</cstring>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item row="1" column="2">
|
|
||||||
<widget class="QCheckBox" name="show_password">
|
<widget class="QCheckBox" name="show_password">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>&Show password</string>
|
<string>&Show password</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="2" column="0">
|
<item row="4" column="0">
|
||||||
<widget class="QLabel" name="label_21">
|
<widget class="QLabel" name="label_21">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>Optional &fixed port:</string>
|
<string>Optional &fixed port:</string>
|
||||||
@ -81,7 +98,7 @@
|
|||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="2" column="1">
|
<item row="4" column="1">
|
||||||
<widget class="QLineEdit" name="fixed_port">
|
<widget class="QLineEdit" name="fixed_port">
|
||||||
<property name="sizePolicy">
|
<property name="sizePolicy">
|
||||||
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
|
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
|
||||||
@ -94,28 +111,28 @@
|
|||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="2" column="2">
|
<item row="4" column="2">
|
||||||
<widget class="QCheckBox" name="use_fixed_port">
|
<widget class="QCheckBox" name="use_fixed_port">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>&Use a fixed port</string>
|
<string>&Use a fixed port</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="3" column="0" colspan="3">
|
<item row="6" column="0" colspan="3">
|
||||||
<widget class="QCheckBox" name="autostart_box">
|
<widget class="QCheckBox" name="autostart_box">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>&Automatically allow connections at calibre startup</string>
|
<string>&Automatically allow connections at calibre startup</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="4" column="0" colspan="3">
|
<item row="8" column="0" colspan="3">
|
||||||
<widget class="QCheckBox" name="enable_auto_management_box">
|
<widget class="QCheckBox" name="enable_auto_management_box">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>&Enable automatic sending of book metadata when your device connects</string>
|
<string>&Enable automatic sending of book metadata when your device connects</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="5" column="0" colspan="3">
|
<item row="10" 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>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user