Split DesktopDaemon into its own files

This commit is contained in:
Albert Vaca Cintora 2024-09-13 00:44:38 +02:00
parent 5af9a33710
commit da07bce897
No known key found for this signature in database
4 changed files with 114 additions and 71 deletions

View file

@ -1,6 +1,6 @@
add_definitions(-DTRANSLATION_DOMAIN="kdeconnect-kded") 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 ecm_qt_declare_logging_category(kdeconnectd
HEADER kdeconnectd_debug.h HEADER kdeconnectd_debug.h
IDENTIFIER KDECONNECT_DAEMON CATEGORY_NAME kdeconnect.daemon IDENTIFIER KDECONNECT_DAEMON CATEGORY_NAME kdeconnect.daemon

83
daemon/desktop_daemon.cpp Normal file
View file

@ -0,0 +1,83 @@
/**
* SPDX-FileCopyrightText: 2024 Albert Vaca <albertvaka@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include "desktop_daemon.h"
#include <QApplication>
#include <QIcon>
#include <QStandardPaths>
#include <QTimer>
#include <KDBusService>
#include <KIO/Global>
#include <KIO/JobTracker>
#include <KLocalizedString>
#include <KNotification>
#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();
}

29
daemon/desktop_daemon.h Normal file
View file

@ -0,0 +1,29 @@
/**
* SPDX-FileCopyrightText: 2024 Albert Vaca Cintora <albertvaka@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#pragma once
#include <QString>
#include <QObject>
#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;
};

View file

@ -30,79 +30,12 @@
#include <dbushelper.h> #include <dbushelper.h>
#include "core/backends/pairinghandler.h" #include "core/backends/pairinghandler.h"
#include "core/daemon.h" #include "desktop_daemon.h"
#include "core/device.h" #include "core/device.h"
#include "core/openconfig.h" #include "core/openconfig.h"
#include "kdeconnect-version.h" #include "kdeconnect-version.h"
#include "kdeconnectd_debug.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 // Copied from plasma-workspace/libkworkspace/kworkspace.cpp
static void detectPlatform(int argc, char **argv) static void detectPlatform(int argc, char **argv)
{ {
@ -195,5 +128,3 @@ int main(int argc, char *argv[])
return app.exec(); return app.exec();
} }
#include "kdeconnectd.moc"