File indexing completed on 2023-09-24 08:52:41

0001 # -*- coding: UTF-8 -*-
0002 
0003 """
0004  * SPDX-FileCopyrightText: 2018 Albert Vaca Cintora <albertvaka@gmail.com>
0005  * SPDX-FileCopyrightText: 2018 Andy Holmes <andrew.g.r.holmes@gmail.com>
0006  *
0007  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0008 """
0009 
0010 __author__ = "Albert Vaca Cintora <albertvaka@gmail.com>"
0011 __version__ = "1.0"
0012 __appname__ = "kdeconnect-share"
0013 __app_disp_name__ = "Share files to your phone via KDE Connect"
0014 __website__ = "https://community.kde.org/KDEConnect"
0015 
0016 import gettext
0017 from functools import partial
0018 
0019 from gi.repository import Nautilus, Gio, GLib, GObject
0020 
0021 try:
0022     i18n = gettext.translation('kdeconnect-nautilus-extension')
0023     _ = i18n.gettext
0024 except (IOError, OSError) as e:
0025     print('kdeconnect-nautilus: {0}'.format(e))
0026     i18n = gettext.translation('kdeconnect-nautilus-extension', fallback=True)
0027     _ = i18n.gettext
0028 
0029 class KdeConnectShareExtension(GObject.GObject, Nautilus.MenuProvider):
0030     """A context menu for sending files via KDE Connect."""
0031 
0032     def refresh_devices_list(self, *args, **kwargs):
0033         try:
0034             onlyReachable = True
0035             onlyPaired = True
0036             variant = GLib.Variant('(bb)', (onlyReachable, onlyPaired))
0037             devices = self.dbus.call_sync('deviceNames', variant, 0, -1, None)
0038             self.devices = devices.unpack()[0]
0039         except Exception as e:
0040             raise Exception('Error while getting reachable devices')
0041 
0042 
0043     def __init__(self):
0044         GObject.GObject.__init__(self)
0045 
0046         self.devices = {}
0047 
0048         self.dbus = Gio.DBusProxy.new_for_bus_sync(
0049             Gio.BusType.SESSION,
0050             Gio.DBusProxyFlags.NONE,
0051             None,
0052             'org.kde.kdeconnect',
0053             '/modules/kdeconnect',
0054             'org.kde.kdeconnect.daemon',
0055             None)
0056 
0057         connection = Gio.bus_get_sync(
0058             Gio.BusType.SESSION,
0059             None)
0060         connection.signal_subscribe(
0061             None,
0062             'org.kde.kdeconnect.daemon',
0063             'deviceListChanged',
0064             "/modules/kdeconnect",
0065             None,
0066             Gio.DBusSignalFlags.NONE,
0067             partial(self.refresh_devices_list, self),
0068         )
0069 
0070         self.refresh_devices_list()
0071 
0072     def send_files(self, menu, files, deviceId):
0073         device_proxy = Gio.DBusProxy.new_for_bus_sync(
0074             Gio.BusType.SESSION,
0075             Gio.DBusProxyFlags.NONE,
0076             None,
0077             'org.kde.kdeconnect',
0078             '/modules/kdeconnect/devices/'+deviceId+'/share',
0079             'org.kde.kdeconnect.device.share',
0080             None)
0081 
0082         for file in files:
0083             variant = GLib.Variant('(s)', (file.get_uri(),))
0084             device_proxy.call_sync('shareUrl', variant, 0, -1, None)
0085 
0086     def get_file_items(self, *args):
0087         # `args` will be `[files: List[Nautilus.FileInfo]]` in Nautilus 4.0 API,
0088         # and `[window: Gtk.Widget, files: List[Nautilus.FileInfo]]` in Nautilus 3.0 API.
0089         files = args[-1]
0090 
0091         #We can only send regular files
0092         for uri in files:
0093             if uri.get_uri_scheme() != 'file' or uri.is_directory():
0094                 return
0095 
0096         devices = self.devices
0097 
0098         if len(devices) == 0:
0099             return
0100 
0101         if len(devices) == 1:
0102             deviceId, deviceName = list(devices.items())[0]
0103             item = Nautilus.MenuItem(
0104                         name='KdeConnectShareExtension::Devices::' + deviceId,
0105                         label=_("Send to %s via KDE Connect") % deviceName,
0106                     )
0107             item.connect('activate', self.send_files, files, deviceId)
0108             return item,
0109         else:
0110             menu = Nautilus.MenuItem(
0111                 name='KdeConnectShareExtension::Devices',
0112                 label=_('Send via KDE Connect'),
0113             )
0114             submenu = Nautilus.Menu()
0115             menu.set_submenu(submenu)
0116 
0117             for deviceId, deviceName in devices.items():
0118                 item = Nautilus.MenuItem(
0119                     name='KdeConnectShareExtension::Devices::' + deviceId,
0120                     label=deviceName,
0121                 )
0122                 item.connect('activate', self.send_files, files, deviceId)
0123                 submenu.append_item(item)
0124 
0125             return menu,
0126