Do not do a dbus call every time get_file_items is called

This commit is contained in:
Albert Vaca 2018-05-29 20:52:16 +02:00
parent bf879a8579
commit 18e799612d

View file

@ -30,6 +30,7 @@ __website__ = "https://community.kde.org/KDEConnect"
import gettext
import locale
from functools import partial
from gi.repository import Nautilus, Gio, GLib, GObject
@ -38,9 +39,22 @@ _ = gettext.gettext
class KdeConnectShareExtension(GObject.GObject, Nautilus.MenuProvider):
"""A context menu for sending files via KDE Connect."""
def refresh_devices_list(self, *args, **kwargs):
try:
onlyReachable = True
onlyPaired = True
variant = GLib.Variant('(bb)', (onlyReachable, onlyPaired))
devices = self.dbus.call_sync('deviceNames', variant, 0, -1, None)
self.devices = devices.unpack()[0]
except Exception as e:
raise Exception('Error while getting reachable devices')
def __init__(self):
GObject.GObject.__init__(self)
self.devices = {}
try:
locale.setlocale(locale.LC_ALL, '')
gettext.bindtextdomain('kdeconnect-nautilus-extension')
@ -57,6 +71,21 @@ class KdeConnectShareExtension(GObject.GObject, Nautilus.MenuProvider):
'org.kde.kdeconnect.daemon',
None)
connection = Gio.bus_get_sync(
Gio.BusType.SESSION,
None)
connection.signal_subscribe(
None,
'org.kde.kdeconnect.daemon',
'deviceListChanged',
"/modules/kdeconnect",
None,
Gio.DBusSignalFlags.NONE,
partial(self.refresh_devices_list, self),
)
self.refresh_devices_list()
def send_files(self, menu, files, deviceId):
device_proxy = Gio.DBusProxy.new_for_bus_sync(
Gio.BusType.SESSION,
@ -78,20 +107,13 @@ class KdeConnectShareExtension(GObject.GObject, Nautilus.MenuProvider):
if uri.get_uri_scheme() != 'file' or uri.is_directory():
return
try:
onlyReachable = True
onlyPaired = True
variant = GLib.Variant('(bb)', (onlyReachable, onlyPaired))
devices = self.dbus.call_sync('deviceNames', variant, 0, -1, None)
devices = devices.unpack()[0]
except Exception as e:
raise Exception('Error while getting reachable devices')
devices = self.devices
if len(devices) == 0:
return
if len(devices) == 1:
deviceId, deviceName = devices.items()[0]
deviceId, deviceName = devices[0], devices[0]
item = Nautilus.MenuItem(
name='KdeConnectShareExtension::Devices::' + deviceId,
label=_("Send to %s via KDE Connect") % deviceName,