2015-02-24 06:12:45 +00:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2014 Yuri Samoilenko <kinnalru@gmail.com>
|
2015-02-24 06:12:45 +00:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2015-02-24 06:12:45 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QApplication>
|
2019-04-27 22:03:24 +01:00
|
|
|
#include <QCommandLineOption>
|
|
|
|
#include <QCommandLineParser>
|
|
|
|
#include <QDBusMessage>
|
2019-08-19 19:57:45 +01:00
|
|
|
#include <QIcon>
|
2020-01-17 00:33:03 +00:00
|
|
|
#include <QProcess>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QSessionManager>
|
|
|
|
#include <QStandardPaths>
|
|
|
|
#include <QTimer>
|
2015-02-24 06:12:45 +00:00
|
|
|
|
2023-06-27 22:50:47 +01:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
#include <Windows.h>
|
|
|
|
#endif
|
|
|
|
|
2019-01-25 21:32:34 +00:00
|
|
|
#include <KAboutData>
|
2015-02-24 06:12:45 +00:00
|
|
|
#include <KDBusService>
|
2023-07-16 15:20:34 +01:00
|
|
|
#include <KIO/Global>
|
2023-12-27 13:37:29 +00:00
|
|
|
#include <KIO/JobTracker>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <KLocalizedString>
|
|
|
|
#include <KNotification>
|
2022-05-12 02:59:56 +01:00
|
|
|
#include <KWindowSystem>
|
2015-02-24 06:12:45 +00:00
|
|
|
|
2019-06-09 16:28:49 +01:00
|
|
|
#include <dbushelper.h>
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
#include "core/backends/pairinghandler.h"
|
2015-02-24 06:12:45 +00:00
|
|
|
#include "core/daemon.h"
|
2015-03-24 11:26:37 +00:00
|
|
|
#include "core/device.h"
|
2022-05-12 02:59:56 +01:00
|
|
|
#include "core/openconfig.h"
|
2015-03-10 04:59:36 +00:00
|
|
|
#include "kdeconnect-version.h"
|
2020-05-26 17:55:47 +01:00
|
|
|
#include "kdeconnectd_debug.h"
|
2019-03-11 12:37:15 +00:00
|
|
|
|
2015-03-24 11:26:37 +00:00
|
|
|
class DesktopDaemon : public Daemon
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
2015-04-29 01:25:49 +01:00
|
|
|
Q_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.daemon")
|
2015-03-24 11:26:37 +00:00
|
|
|
public:
|
2022-09-10 22:23:52 +01:00
|
|
|
DesktopDaemon(QObject *parent = nullptr)
|
2015-03-24 11:26:37 +00:00
|
|
|
: Daemon(parent)
|
2019-08-19 19:57:45 +01:00
|
|
|
{
|
2023-06-27 23:03:08 +01:00
|
|
|
qApp->setWindowIcon(QIcon(QStringLiteral(":/icons/kdeconnect/kdeconnect.png")));
|
2019-08-19 19:57:45 +01:00
|
|
|
}
|
2015-03-24 11:26:37 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void askPairingConfirmation(Device *device) override
|
2015-03-24 11:26:37 +00:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
KNotification *notification = new KNotification(QStringLiteral("pairingRequest"), KNotification::NotificationFlag::Persistent);
|
2023-06-01 01:25:37 +01:00
|
|
|
QTimer::singleShot(PairingHandler::pairingTimeoutMsec, notification, &KNotification::close);
|
2015-03-24 11:26:37 +00:00
|
|
|
notification->setIconName(QStringLiteral("dialog-information"));
|
2016-11-26 14:38:08 +00:00
|
|
|
notification->setComponentName(QStringLiteral("kdeconnect"));
|
2019-09-30 21:53:46 +01:00
|
|
|
notification->setTitle(QStringLiteral("KDE Connect"));
|
2022-09-10 22:23:52 +01:00
|
|
|
notification->setText(
|
|
|
|
i18n("Pairing request from %1\nKey: %2...", device->name().toHtmlEscaped(), QString::fromUtf8(device->verificationKey().left(8))));
|
2020-11-26 10:28:32 +00:00
|
|
|
QString deviceId = device->id();
|
2022-05-12 02:59:56 +01:00
|
|
|
auto openSettings = [deviceId, notification] {
|
|
|
|
OpenConfig oc;
|
|
|
|
oc.setXdgActivationToken(notification->xdgActivationToken());
|
|
|
|
oc.openConfiguration(deviceId);
|
2020-11-26 10:28:32 +00:00
|
|
|
};
|
2023-07-23 21:52:52 +01:00
|
|
|
|
|
|
|
KNotificationAction *openSettingsAction = notification->addDefaultAction(i18n("Open"));
|
|
|
|
connect(openSettingsAction, &KNotificationAction::activated, openSettings);
|
|
|
|
|
|
|
|
KNotificationAction *acceptAction = notification->addAction(i18n("Accept"));
|
|
|
|
connect(acceptAction, &KNotificationAction::activated, device, &Device::acceptPairing);
|
|
|
|
|
|
|
|
KNotificationAction *rejectAction = notification->addAction(i18n("Reject"));
|
|
|
|
connect(rejectAction, &KNotificationAction::activated, device, &Device::cancelPairing);
|
|
|
|
|
|
|
|
KNotificationAction *viewKeyAction = notification->addAction(i18n("View key"));
|
|
|
|
connect(viewKeyAction, &KNotificationAction::activated, openSettings);
|
2015-03-24 11:26:37 +00:00
|
|
|
notification->sendEvent();
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void reportError(const QString &title, const QString &description) override
|
2015-03-24 11:26:37 +00:00
|
|
|
{
|
2019-05-08 22:05:38 +01:00
|
|
|
qCWarning(KDECONNECT_DAEMON) << title << ":" << description;
|
2015-03-24 11:26:37 +00:00
|
|
|
KNotification::event(KNotification::Error, title, description);
|
|
|
|
}
|
2015-05-04 23:41:39 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
KJobTrackerInterface *jobTracker() override
|
2019-10-27 19:35:18 +00:00
|
|
|
{
|
|
|
|
return KIO::getJobTracker();
|
|
|
|
}
|
|
|
|
|
2018-03-18 11:42:15 +00:00
|
|
|
Q_SCRIPTABLE void sendSimpleNotification(const QString &eventId, const QString &title, const QString &text, const QString &iconName) override
|
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
KNotification *notification = new KNotification(eventId); // KNotification::Persistent
|
2018-03-18 11:42:15 +00:00
|
|
|
notification->setIconName(iconName);
|
|
|
|
notification->setComponentName(QStringLiteral("kdeconnect"));
|
|
|
|
notification->setTitle(title);
|
|
|
|
notification->setText(text);
|
|
|
|
notification->sendEvent();
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void quit() override
|
|
|
|
{
|
2019-03-11 12:37:15 +00:00
|
|
|
QApplication::quit();
|
|
|
|
}
|
2015-03-24 11:26:37 +00:00
|
|
|
};
|
|
|
|
|
2019-07-19 16:12:49 +01:00
|
|
|
// Copied from plasma-workspace/libkworkspace/kworkspace.cpp
|
|
|
|
static void detectPlatform(int argc, char **argv)
|
|
|
|
{
|
|
|
|
if (qEnvironmentVariableIsSet("QT_QPA_PLATFORM")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < argc; i++) {
|
2022-09-10 22:23:52 +01:00
|
|
|
if (qstrcmp(argv[i], "-platform") == 0 || qstrcmp(argv[i], "--platform") == 0 || QByteArray(argv[i]).startsWith("-platform=")
|
|
|
|
|| QByteArray(argv[i]).startsWith("--platform=")) {
|
2019-07-19 16:12:49 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const QByteArray sessionType = qgetenv("XDG_SESSION_TYPE");
|
|
|
|
if (sessionType.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
2023-07-21 10:22:47 +01:00
|
|
|
if (qstrcmp(sessionType.constData(), "wayland") == 0) {
|
2019-07-19 16:12:49 +01:00
|
|
|
qputenv("QT_QPA_PLATFORM", "wayland");
|
2023-07-21 10:22:47 +01:00
|
|
|
} else if (qstrcmp(sessionType.constData(), "x11") == 0) {
|
2019-07-19 16:12:49 +01:00
|
|
|
qputenv("QT_QPA_PLATFORM", "xcb");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
int main(int argc, char *argv[])
|
2015-02-24 06:12:45 +00:00
|
|
|
{
|
2023-06-27 22:50:47 +01:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
// If ran from a console, redirect the output there
|
|
|
|
if (AttachConsole(ATTACH_PARENT_PROCESS)) {
|
|
|
|
freopen("CONOUT$", "w", stdout);
|
|
|
|
freopen("CONOUT$", "w", stderr);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-08-18 16:26:16 +01:00
|
|
|
detectPlatform(argc, argv);
|
2023-07-21 09:16:47 +01:00
|
|
|
QGuiApplication::setQuitLockEnabled(false);
|
2019-07-19 16:12:49 +01:00
|
|
|
|
2015-02-24 06:12:45 +00:00
|
|
|
QApplication app(argc, argv);
|
2022-09-10 22:23:52 +01:00
|
|
|
KAboutData aboutData(QStringLiteral("kdeconnect.daemon"),
|
|
|
|
i18n("KDE Connect Daemon"),
|
|
|
|
QStringLiteral(KDECONNECT_VERSION_STRING),
|
|
|
|
i18n("KDE Connect Daemon"),
|
|
|
|
KAboutLicense::GPL);
|
2019-01-25 21:32:34 +00:00
|
|
|
KAboutData::setApplicationData(aboutData);
|
2015-02-24 06:12:45 +00:00
|
|
|
app.setQuitOnLastWindowClosed(false);
|
|
|
|
|
2019-04-27 22:03:24 +01:00
|
|
|
QCommandLineParser parser;
|
|
|
|
QCommandLineOption replaceOption({QStringLiteral("replace")}, i18n("Replace an existing instance"));
|
|
|
|
parser.addOption(replaceOption);
|
2023-06-07 17:25:34 +01:00
|
|
|
#ifdef Q_OS_MAC
|
2022-04-12 06:40:03 +01:00
|
|
|
QCommandLineOption macosPrivateDBusOption({QStringLiteral("use-private-dbus")},
|
2022-09-10 22:23:52 +01:00
|
|
|
i18n("Launch a private D-Bus daemon with kdeconnectd (macOS test-purpose only)"));
|
2022-04-12 06:40:03 +01:00
|
|
|
parser.addOption(macosPrivateDBusOption);
|
2023-06-07 17:25:34 +01:00
|
|
|
#endif
|
2019-04-27 22:03:24 +01:00
|
|
|
aboutData.setupCommandLine(&parser);
|
|
|
|
|
|
|
|
parser.process(app);
|
2022-04-12 06:40:03 +01:00
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
if (parser.isSet(macosPrivateDBusOption)) {
|
|
|
|
DBusHelper::launchDBusDaemon();
|
|
|
|
}
|
|
|
|
#endif
|
2019-04-27 22:03:24 +01:00
|
|
|
aboutData.processCommandLine(&parser);
|
|
|
|
if (parser.isSet(replaceOption)) {
|
2019-07-20 18:53:17 +01:00
|
|
|
auto message = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect"),
|
2022-09-10 22:23:52 +01:00
|
|
|
QStringLiteral("/MainApplication"),
|
|
|
|
QStringLiteral("org.qtproject.Qt.QCoreApplication"),
|
|
|
|
QStringLiteral("quit"));
|
|
|
|
QDBusConnection::sessionBus().call(message); // deliberately block until it's done, so we register the name after the app quits
|
2019-04-27 22:03:24 +01:00
|
|
|
}
|
|
|
|
|
2015-02-24 06:12:45 +00:00
|
|
|
KDBusService dbusService(KDBusService::Unique);
|
|
|
|
|
2019-05-05 13:58:11 +01:00
|
|
|
DesktopDaemon daemon;
|
2016-06-15 19:37:42 +01:00
|
|
|
|
2023-07-11 19:41:36 +01:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
// make sure indicator shows up in the tray whenever daemon is spawned
|
2023-07-21 13:53:23 +01:00
|
|
|
QProcess::startDetached(QStringLiteral("kdeconnect-indicator.exe"), QStringList());
|
2023-07-11 19:41:36 +01:00
|
|
|
#endif
|
|
|
|
|
2019-06-02 15:01:42 +01:00
|
|
|
// kdeconnectd is autostarted, so disable session management to speed up startup
|
|
|
|
auto disableSessionManagement = [](QSessionManager &sm) {
|
|
|
|
sm.setRestartHint(QSessionManager::RestartNever);
|
|
|
|
};
|
|
|
|
QObject::connect(&app, &QGuiApplication::commitDataRequest, disableSessionManagement);
|
|
|
|
QObject::connect(&app, &QGuiApplication::saveStateRequest, disableSessionManagement);
|
|
|
|
|
2023-11-20 18:09:13 +00:00
|
|
|
qSetMessagePattern(QStringLiteral("%{time} %{category}: %{message}"));
|
|
|
|
|
2015-02-24 06:12:45 +00:00
|
|
|
return app.exec();
|
|
|
|
}
|
2015-03-24 11:26:37 +00:00
|
|
|
|
|
|
|
#include "kdeconnectd.moc"
|