From 2008490ad94bb7d08ca0c4ca6910dd0d35589b8d Mon Sep 17 00:00:00 2001 From: Albert Vaca Date: Sat, 24 Mar 2018 17:32:32 +0100 Subject: [PATCH] Adds a nautilus-python extension to share files Reviewed by Aleix Pol --- CMakeLists.txt | 1 + nautilus-extension/CMakeLists.txt | 4 + nautilus-extension/Messages.sh | 3 + nautilus-extension/kdeconnect-share.py | 118 +++++++++++++++++++++++++ 4 files changed, 126 insertions(+) create mode 100644 nautilus-extension/CMakeLists.txt create mode 100644 nautilus-extension/Messages.sh create mode 100644 nautilus-extension/kdeconnect-share.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 82821070d..4d5d0229b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,6 +58,7 @@ add_subdirectory(cli) add_subdirectory(indicator) add_subdirectory(fileitemactionplugin) add_subdirectory(urlhandler) +add_subdirectory(nautilus-extension) if(KF5DocTools_FOUND) add_subdirectory(doc) endif() diff --git a/nautilus-extension/CMakeLists.txt b/nautilus-extension/CMakeLists.txt new file mode 100644 index 000000000..775940701 --- /dev/null +++ b/nautilus-extension/CMakeLists.txt @@ -0,0 +1,4 @@ + +set(NAUTILUS_PYTHON_EXTENSIONS_INSTALL_DIR "share/nautilus-python/extensions" CACHE PATH "Path for python-nautilus extensions") + +install(FILES kdeconnect-share.py DESTINATION ${NAUTILUS_PYTHON_EXTENSIONS_INSTALL_DIR}) diff --git a/nautilus-extension/Messages.sh b/nautilus-extension/Messages.sh new file mode 100644 index 000000000..d6e61be79 --- /dev/null +++ b/nautilus-extension/Messages.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +$XGETTEXT `find . -name '*.py'` -o $podir/kdeconnect-nautilus-extension.pot diff --git a/nautilus-extension/kdeconnect-share.py b/nautilus-extension/kdeconnect-share.py new file mode 100644 index 000000000..c88222621 --- /dev/null +++ b/nautilus-extension/kdeconnect-share.py @@ -0,0 +1,118 @@ +#!/usr/bin/python +# -*- coding: UTF-8 -*- + +""" + * Copyright 2018 Albert Vaca Cintora + * Copyright 2018 Andy Holmes + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +""" + +__author__ = "Albert Vaca Cintora " +__version__ = "1.0" +__appname__ = "kdeconnect-share" +__app_disp_name__ = "Share files to your phone via KDE Connect" +__website__ = "https://community.kde.org/KDEConnect" + +import gettext +import locale + +from gi.repository import Nautilus, Gio, GLib, GObject + +_ = gettext.gettext + +class KdeConnectShareExtension(GObject.GObject, Nautilus.MenuProvider): + """A context menu for sending files via KDE Connect.""" + + def __init__(self): + GObject.GObject.__init__(self) + + try: + locale.setlocale(locale.LC_ALL, '') + gettext.bindtextdomain('kdeconnect-nautilus-extension') + except Exception as e: + print(e) + pass + + self.dbus = Gio.DBusProxy.new_for_bus_sync( + Gio.BusType.SESSION, + Gio.DBusProxyFlags.NONE, + None, + 'org.kde.kdeconnect', + '/modules/kdeconnect', + 'org.kde.kdeconnect.daemon', + None) + + def send_files(self, menu, files, deviceId): + device_proxy = Gio.DBusProxy.new_for_bus_sync( + Gio.BusType.SESSION, + Gio.DBusProxyFlags.NONE, + None, + 'org.kde.kdeconnect', + '/modules/kdeconnect/devices/'+deviceId+'/share', + 'org.kde.kdeconnect.device.share', + None) + + for file in files: + variant = GLib.Variant('(s)', (file.get_uri(),)) + device_proxy.call_sync('shareUrl', variant, 0, -1, None) + + def get_file_items(self, window, files): + + #We can only send regular files + for uri in files: + 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') + + if len(devices) == 0: + return + + if len(devices) == 1: + deviceId, deviceName = devices.items()[0] + item = Nautilus.MenuItem( + name='KdeConnectShareExtension::Devices::' + deviceId, + label=_("Send to %s via KDE Connect") % deviceName, + ) + item.connect('activate', self.send_files, files, deviceId) + return item, + else: + menu = Nautilus.MenuItem( + name='KdeConnectShareExtension::Devices', + label=_('Send via KDE Connect'), + ) + submenu = Nautilus.Menu() + menu.set_submenu(submenu) + + for deviceId, deviceName in devices.items(): + item = Nautilus.MenuItem( + name='KdeConnectShareExtension::Devices::' + deviceId, + label=deviceName, + ) + item.connect('activate', self.send_files, files, deviceId) + submenu.append_item(item) + + return menu, +