kdeconnect-kde/fileitemactionplugin/sendfileitemaction.cpp
Weixuan Xiao f1843cb492 Improve D-Bus implementation on macOS
Better patch to replace !218.

- Auto and quick detection of previous D-Bus instance;
- Remove private D-Bus compile definition, only use it on macOS without an existing D-Bus instance;
- Safe reboot after crashes because the indicator is not relating on the kdeconnectd to run a D-Bus session;
- Safe exit after clicking on `Quit` in the systray.


More details in commit logs:

Only enable private D-Bus on macOS because the other platforms do not
need them.
The app should be able to easily detect the session bus from the env
DBUS_LAUNCHD_SESSION_BUS_SOCKET from launchd through launchctl.
Because https://gitlab.freedesktop.org/dbus/dbus/-/blob/master/dbus/dbus-sysdeps-unix.c#L4392
shows that it is the only probing method on macOS with launchd.

The D-Bus session bus can be easily found from launchd/launchctl
with DBUS_LAUNCHD_SESSION_BUS_SOCKET env. It can be an external one
(installed from HomeBrew) or an internal one (launched by a previous
instance followed by a crash).

The indicator helper on macOS can now automatically detect whether we can use a potentially
(with launchd/launchctl env set, or KDE Connect macOS
private_bus_address set) existed and usable session bus.
If previous bus is usable, just try to launch the kdeconnectd with us.
Otherwise, launch a private D-Bus daemon, export the launchd/launchctl
env, and run a kdeconnectd instance.

Everything works better and quicker now :)
2022-04-12 05:40:03 +00:00

87 lines
2.9 KiB
C++

/*
* SPDX-FileCopyrightText: 2011 Alejandro Fiestas Olivares <afiestas@kde.org>
* SPDX-FileCopyrightText: 2014 Aleix Pol Gonzalez <aleixpol@kde.org>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "sendfileitemaction.h"
#include <QList>
#include <QMenu>
#include <QAction>
#include <QWidget>
#include <QVariantList>
#include <QUrl>
#include <QIcon>
#include <KPluginFactory>
#include <KLocalizedString>
#include <interfaces/devicesmodel.h>
#include <interfaces/dbusinterfaces.h>
#include <dbushelper.h>
#include "kdeconnect_fileitemaction_debug.h"
K_PLUGIN_CLASS_WITH_JSON(SendFileItemAction, "kdeconnectsendfile.json")
SendFileItemAction::SendFileItemAction(QObject* parent, const QVariantList& ): KAbstractFileItemActionPlugin(parent)
{
}
QList<QAction*> SendFileItemAction::actions(const KFileItemListProperties& fileItemInfos, QWidget* parentWidget)
{
QList<QAction*> actions;
DaemonDbusInterface iface;
if (!iface.isValid()) {
return actions;
}
QDBusPendingReply<QStringList> reply = iface.devices(true, true);
reply.waitForFinished();
const QStringList devices = reply.value();
for (const QString& id : devices) {
DeviceDbusInterface deviceIface(id);
if (!deviceIface.isValid()) {
continue;
}
if (!deviceIface.hasPlugin(QStringLiteral("kdeconnect_share"))) {
continue;
}
QAction* action = new QAction(QIcon::fromTheme(deviceIface.iconName()), deviceIface.name(), parentWidget);
action->setProperty("id", id);
action->setProperty("urls", QVariant::fromValue(fileItemInfos.urlList()));
action->setProperty("parentWidget", QVariant::fromValue(parentWidget));
connect(action, &QAction::triggered, this, &SendFileItemAction::sendFile);
actions += action;
}
if (actions.count() > 1) {
QAction* menuAction = new QAction(QIcon::fromTheme(QStringLiteral("kdeconnect")), i18n("Send via KDE Connect"), parentWidget);
QMenu* menu = new QMenu(parentWidget);
menu->addActions(actions);
menuAction->setMenu(menu);
return QList<QAction*>() << menuAction;
} else {
if(actions.count() == 1) {
actions.first()->setText(i18n("Send to '%1' via KDE Connect", actions.first()->text()));
}
return actions;
}
}
void SendFileItemAction::sendFile()
{
const QList<QUrl> urls = sender()->property("urls").value<QList<QUrl>>();
QString id = sender()->property("id").toString();
for (const QUrl& url : urls) {
QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect"), QStringLiteral("/modules/kdeconnect/devices/") + id + QStringLiteral("/share"), QStringLiteral("org.kde.kdeconnect.device.share"), QStringLiteral("shareUrl"));
msg.setArguments(QVariantList() << url.toString());
QDBusConnection::sessionBus().asyncCall(msg);
}
}
#include "sendfileitemaction.moc"