Commit 9dd7f015 authored by Albert Vaca Cintora's avatar Albert Vaca Cintora

Do not do a dbus call every time get_file_items is called

parent 3c82c358
......@@ -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,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment