File indexing completed on 2023-10-01 08:39:15

0001 /*
0002  * SPDX-FileCopyrightText: 2011 Alejandro Fiestas Olivares <afiestas@kde.org>
0003  * SPDX-FileCopyrightText: 2014 Aleix Pol Gonzalez <aleixpol@kde.org>
0004  *
0005  * SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 
0008 #include "sendfileitemaction.h"
0009 
0010 #include <QAction>
0011 #include <QIcon>
0012 #include <QList>
0013 #include <QMenu>
0014 #include <QUrl>
0015 #include <QVariantList>
0016 #include <QWidget>
0017 
0018 #include <KLocalizedString>
0019 #include <KPluginFactory>
0020 
0021 #include <interfaces/dbusinterfaces.h>
0022 #include <interfaces/devicesmodel.h>
0023 
0024 #include <dbushelper.h>
0025 
0026 #include "kdeconnect_fileitemaction_debug.h"
0027 
0028 K_PLUGIN_CLASS_WITH_JSON(SendFileItemAction, "kdeconnectsendfile.json")
0029 
0030 SendFileItemAction::SendFileItemAction(QObject *parent, const QVariantList &)
0031     : KAbstractFileItemActionPlugin(parent)
0032 {
0033 }
0034 
0035 QList<QAction *> SendFileItemAction::actions(const KFileItemListProperties &fileItemInfos, QWidget *parentWidget)
0036 {
0037     QList<QAction *> actions;
0038 
0039     DaemonDbusInterface iface;
0040     if (!iface.isValid()) {
0041         return actions;
0042     }
0043 
0044     QDBusPendingReply<QStringList> reply = iface.devices(true, true);
0045     reply.waitForFinished();
0046     const QStringList devices = reply.value();
0047     for (const QString &id : devices) {
0048         DeviceDbusInterface deviceIface(id);
0049         if (!deviceIface.isValid()) {
0050             continue;
0051         }
0052         if (!deviceIface.hasPlugin(QStringLiteral("kdeconnect_share"))) {
0053             continue;
0054         }
0055         QAction *action = new QAction(QIcon::fromTheme(deviceIface.iconName()), deviceIface.name(), parentWidget);
0056         action->setProperty("id", id);
0057         action->setProperty("urls", QVariant::fromValue(fileItemInfos.urlList()));
0058         action->setProperty("parentWidget", QVariant::fromValue(parentWidget));
0059         connect(action, &QAction::triggered, this, &SendFileItemAction::sendFile);
0060         actions += action;
0061     }
0062 
0063     if (actions.count() > 1) {
0064         QAction *menuAction = new QAction(QIcon::fromTheme(QStringLiteral("kdeconnect")), i18n("Send via KDE Connect"), parentWidget);
0065         QMenu *menu = new QMenu(parentWidget);
0066         menu->addActions(actions);
0067         menuAction->setMenu(menu);
0068         return QList<QAction *>() << menuAction;
0069     } else {
0070         if (actions.count() == 1) {
0071             actions.first()->setText(i18n("Send to '%1' via KDE Connect", actions.first()->text()));
0072         }
0073         return actions;
0074     }
0075 }
0076 
0077 void SendFileItemAction::sendFile()
0078 {
0079     const QList<QUrl> urls = sender()->property("urls").value<QList<QUrl>>();
0080     QString id = sender()->property("id").toString();
0081     for (const QUrl &url : urls) {
0082         QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect"),
0083                                                           QStringLiteral("/modules/kdeconnect/devices/") + id + QStringLiteral("/share"),
0084                                                           QStringLiteral("org.kde.kdeconnect.device.share"),
0085                                                           QStringLiteral("shareUrl"));
0086         msg.setArguments(QVariantList() << url.toString());
0087         QDBusConnection::sessionBus().asyncCall(msg);
0088     }
0089 }
0090 
0091 #include "sendfileitemaction.moc"