diff --git a/daemon/CMakeLists.txt b/daemon/CMakeLists.txt index 34fb74e70..5d486073f 100644 --- a/daemon/CMakeLists.txt +++ b/daemon/CMakeLists.txt @@ -1,6 +1,6 @@ add_definitions(-DTRANSLATION_DOMAIN="kdeconnect-kded") -add_executable(kdeconnectd kdeconnectd.cpp ${CMAKE_SOURCE_DIR}/icons/custom_icons.qrc) +add_executable(kdeconnectd kdeconnectd.cpp desktop_daemon.cpp ${CMAKE_SOURCE_DIR}/icons/custom_icons.qrc) ecm_qt_declare_logging_category(kdeconnectd HEADER kdeconnectd_debug.h IDENTIFIER KDECONNECT_DAEMON CATEGORY_NAME kdeconnect.daemon diff --git a/daemon/desktop_daemon.cpp b/daemon/desktop_daemon.cpp new file mode 100644 index 000000000..1b997995b --- /dev/null +++ b/daemon/desktop_daemon.cpp @@ -0,0 +1,83 @@ +/** + * SPDX-FileCopyrightText: 2024 Albert Vaca + * + * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL + */ + +#include "desktop_daemon.h" + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "core/backends/pairinghandler.h" +#include "core/device.h" +#include "core/openconfig.h" + +DesktopDaemon::DesktopDaemon(QObject *parent) + : Daemon(parent) +{ + qApp->setWindowIcon(QIcon(QStringLiteral(":/icons/kdeconnect/kdeconnect.png"))); +} + +void DesktopDaemon::askPairingConfirmation(Device *device) +{ + KNotification *notification = new KNotification(QStringLiteral("pairingRequest"), KNotification::NotificationFlag::Persistent); + QTimer::singleShot(PairingHandler::pairingTimeoutMsec, notification, &KNotification::close); + notification->setIconName(QStringLiteral("dialog-information")); + notification->setComponentName(QStringLiteral("kdeconnect")); + notification->setTitle(QStringLiteral("KDE Connect")); + notification->setText(i18n("Pairing request from %1\nKey: %2", device->name().toHtmlEscaped(), device->verificationKey())); + QString deviceId = device->id(); + auto openSettings = [deviceId, notification] { + OpenConfig oc; + oc.setXdgActivationToken(notification->xdgActivationToken()); + oc.openConfiguration(deviceId); + }; + + 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); + notification->sendEvent(); +} + +void DesktopDaemon::reportError(const QString &title, const QString &description) +{ + qWarning() << title << ":" << description; + KNotification::event(KNotification::Error, title, description); +} + +KJobTrackerInterface *DesktopDaemon::jobTracker() +{ + return KIO::getJobTracker(); +} + +Q_SCRIPTABLE void DesktopDaemon::sendSimpleNotification(const QString &eventId, const QString &title, const QString &text, const QString &iconName) +{ + KNotification *notification = new KNotification(eventId); // KNotification::Persistent + notification->setIconName(iconName); + notification->setComponentName(QStringLiteral("kdeconnect")); + notification->setTitle(title); + notification->setText(text); + notification->sendEvent(); +} + +void DesktopDaemon::quit() +{ + QApplication::quit(); +} diff --git a/daemon/desktop_daemon.h b/daemon/desktop_daemon.h new file mode 100644 index 000000000..d2f3fdcf2 --- /dev/null +++ b/daemon/desktop_daemon.h @@ -0,0 +1,29 @@ +/** + * SPDX-FileCopyrightText: 2024 Albert Vaca Cintora + * + * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL + */ + +#pragma once + +#include +#include + +#include "daemon.h" + +class Device; +class KJobTrackerInterface; + +class DesktopDaemon : public Daemon +{ + Q_OBJECT + Q_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.daemon") +public: + DesktopDaemon(QObject *parent = nullptr); + + void askPairingConfirmation(Device *device) override; + void reportError(const QString &title, const QString &description) override; + KJobTrackerInterface *jobTracker() override; + Q_SCRIPTABLE void sendSimpleNotification(const QString &eventId, const QString &title, const QString &text, const QString &iconName) override; + void quit() override; +}; diff --git a/daemon/kdeconnectd.cpp b/daemon/kdeconnectd.cpp index 4f8723bdf..adcdbf096 100644 --- a/daemon/kdeconnectd.cpp +++ b/daemon/kdeconnectd.cpp @@ -30,79 +30,12 @@ #include #include "core/backends/pairinghandler.h" -#include "core/daemon.h" +#include "desktop_daemon.h" #include "core/device.h" #include "core/openconfig.h" #include "kdeconnect-version.h" #include "kdeconnectd_debug.h" -class DesktopDaemon : public Daemon -{ - Q_OBJECT - Q_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.daemon") -public: - DesktopDaemon(QObject *parent = nullptr) - : Daemon(parent) - { - qApp->setWindowIcon(QIcon(QStringLiteral(":/icons/kdeconnect/kdeconnect.png"))); - } - - void askPairingConfirmation(Device *device) override - { - KNotification *notification = new KNotification(QStringLiteral("pairingRequest"), KNotification::NotificationFlag::Persistent); - QTimer::singleShot(PairingHandler::pairingTimeoutMsec, notification, &KNotification::close); - notification->setIconName(QStringLiteral("dialog-information")); - notification->setComponentName(QStringLiteral("kdeconnect")); - notification->setTitle(QStringLiteral("KDE Connect")); - notification->setText(i18n("Pairing request from %1\nKey: %2", device->name().toHtmlEscaped(), device->verificationKey())); - QString deviceId = device->id(); - auto openSettings = [deviceId, notification] { - OpenConfig oc; - oc.setXdgActivationToken(notification->xdgActivationToken()); - oc.openConfiguration(deviceId); - }; - - 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); - notification->sendEvent(); - } - - void reportError(const QString &title, const QString &description) override - { - qCWarning(KDECONNECT_DAEMON) << title << ":" << description; - KNotification::event(KNotification::Error, title, description); - } - - KJobTrackerInterface *jobTracker() override - { - return KIO::getJobTracker(); - } - - Q_SCRIPTABLE void sendSimpleNotification(const QString &eventId, const QString &title, const QString &text, const QString &iconName) override - { - KNotification *notification = new KNotification(eventId); // KNotification::Persistent - notification->setIconName(iconName); - notification->setComponentName(QStringLiteral("kdeconnect")); - notification->setTitle(title); - notification->setText(text); - notification->sendEvent(); - } - - void quit() override - { - QApplication::quit(); - } -}; - // Copied from plasma-workspace/libkworkspace/kworkspace.cpp static void detectPlatform(int argc, char **argv) { @@ -195,5 +128,3 @@ int main(int argc, char *argv[]) return app.exec(); } - -#include "kdeconnectd.moc"