From 8f777040f7044d72bc491ce0e34fa6ef9fad2b03 Mon Sep 17 00:00:00 2001 From: Aleix Pol Date: Tue, 24 Mar 2015 12:26:37 +0100 Subject: [PATCH] Remove KNotifications dependency from libkdeconnectcore Moves the implementation into the actual daemon. This opens the possibility for different ways to expose these notifications depending on where the libkdeconnect will be deployed. REVIEW: 123076 --- CMakeLists.txt | 2 +- core/CMakeLists.txt | 1 - core/daemon.cpp | 10 +++++++ core/daemon.h | 10 +++++++ core/device.cpp | 14 ++------- core/kdeconnectconfig.cpp | 11 ++++--- daemon/CMakeLists.txt | 4 ++- daemon/kdeconnectd.cpp | 34 +++++++++++++++++++++- plugins/battery/CMakeLists.txt | 2 ++ plugins/notifications/CMakeLists.txt | 2 ++ plugins/ping/CMakeLists.txt | 2 ++ plugins/screensaver-inhibit/CMakeLists.txt | 2 ++ plugins/sftp/CMakeLists.txt | 2 ++ plugins/share/CMakeLists.txt | 3 ++ plugins/telephony/CMakeLists.txt | 2 ++ 15 files changed, 79 insertions(+), 22 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 05a1b2cff..516f5179b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ find_package(ECM 0.0.9 REQUIRED NO_MODULE) set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH} ${ECM_KDE_MODULE_DIR} ${CMAKE_SOURCE_DIR}/cmake) find_package(Qt5 5.2 REQUIRED COMPONENTS Quick Test) -find_package(KF5 REQUIRED COMPONENTS I18n KIO Notifications ConfigWidgets DBusAddons KCMUtils IconThemes) +find_package(KF5 REQUIRED COMPONENTS I18n KIO ConfigWidgets DBusAddons KCMUtils IconThemes) find_package(Qca-qt5 2.1.0 REQUIRED) include_directories(${CMAKE_SOURCE_DIR}) diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 20263179d..623c63708 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -41,7 +41,6 @@ PRIVATE Qt5::Gui KF5::I18n KF5::ConfigCore - KF5::Notifications ) set_target_properties(kdeconnectcore PROPERTIES diff --git a/core/daemon.cpp b/core/daemon.cpp index 4e9cec4c5..ec1fee7ab 100644 --- a/core/daemon.cpp +++ b/core/daemon.cpp @@ -36,6 +36,8 @@ #include "backends/devicelink.h" #include "backends/linkprovider.h" +Q_GLOBAL_STATIC(Daemon*, s_instance) + struct DaemonPrivate { //Different ways to find devices and connect to them @@ -45,10 +47,18 @@ struct DaemonPrivate QMap mDevices; }; +Daemon* Daemon::instance() +{ + Q_ASSERT(s_instance.exists()); + return *s_instance; +} + Daemon::Daemon(QObject *parent) : QObject(parent) , d(new DaemonPrivate) { + Q_ASSERT(!s_instance.exists()); + *s_instance = this; qCDebug(KDECONNECT_CORE) << "KdeConnect daemon starting"; //Load backends diff --git a/core/daemon.h b/core/daemon.h index 1817bdf31..654441fb3 100644 --- a/core/daemon.h +++ b/core/daemon.h @@ -30,6 +30,7 @@ class DaemonPrivate; class NetworkPackage; class DeviceLink; +class Device; class KDECONNECTCORE_EXPORT Daemon : public QObject @@ -42,6 +43,12 @@ public: ~Daemon(); public Q_SLOTS: + /** + * Returns the daemon. + * + * Note this can't be called before constructing the Daemon. + */ + static Daemon* instance(); //After calling this, signal deviceDiscovered will be triggered for each device Q_SCRIPTABLE void setDiscoveryEnabled(bool b); @@ -54,6 +61,9 @@ public Q_SLOTS: //Returns a list of ids. The respective devices can be manipulated using the dbus path: "/modules/kdeconnect/Devices/"+id Q_SCRIPTABLE QStringList devices(bool onlyReachable = false, bool onlyVisible = false) const; + virtual void requestPairing(Device *d) = 0; + virtual void reportError(const QString &title, const QString &description) = 0; + Q_SIGNALS: Q_SCRIPTABLE void deviceAdded(const QString& id); Q_SCRIPTABLE void deviceRemoved(const QString& id); //Note that paired devices will never be removed diff --git a/core/device.cpp b/core/device.cpp index 96c62b5d6..c8a28bdb3 100644 --- a/core/device.cpp +++ b/core/device.cpp @@ -30,7 +30,6 @@ #include #include -#include #include #include #include @@ -43,6 +42,7 @@ #include "backends/linkprovider.h" #include "networkpackage.h" #include "kdeconnectconfig.h" +#include "daemon.h" Q_LOGGING_CATEGORY(KDECONNECT_CORE, "kdeconnect.core") @@ -352,21 +352,11 @@ void Device::privateReceivedPackage(const NetworkPackage& np) setAsPaired(); } else { - qCDebug(KDECONNECT_CORE) << "Pair request"; - KNotification* notification = new KNotification("pairingRequest"); - notification->setIconName(QStringLiteral("dialog-information")); - notification->setComponentName("kdeconnect"); - notification->setText(i18n("Pairing request from %1", m_deviceName)); - notification->setActions(QStringList() << i18n("Accept") << i18n("Reject")); - connect(notification, &KNotification::ignored, this, &Device::rejectPairing); - connect(notification, &KNotification::action1Activated, this, &Device::acceptPairing); - connect(notification, &KNotification::action2Activated, this, &Device::rejectPairing); - notification->sendEvent(); + Daemon::instance()->requestPairing(this); m_pairStatus = Device::RequestedByPeer; - } } else { diff --git a/core/kdeconnectconfig.cpp b/core/kdeconnectconfig.cpp index 1356f43fd..d72fe9554 100644 --- a/core/kdeconnectconfig.cpp +++ b/core/kdeconnectconfig.cpp @@ -20,7 +20,6 @@ #include "kdeconnectconfig.h" -#include #include #include @@ -37,6 +36,7 @@ #include "core_debug.h" #include "dbushelper.h" +#include "daemon.h" struct KdeConnectConfigPrivate { @@ -61,9 +61,9 @@ KdeConnectConfig::KdeConnectConfig() { //qCDebug(KDECONNECT_CORE) << "QCA supported capabilities:" << QCA::supportedFeatures().join(","); if(!QCA::isSupported("rsa")) { - KNotification::event(KNotification::Error, - QLatin1String("KDE Connect failed to start"), //Should not happen, not worth i18n - QLatin1String("Could not find support for RSA in your QCA installation. If your" + Daemon::instance()->reportError( + i18n("KDE Connect failed to start"), + i18n("Could not find support for RSA in your QCA installation. If your" "distribution provides separate packages for QCA-ossl and QCA-gnupg," "make sure you have them installed and try again.")); return; @@ -99,8 +99,7 @@ KdeConnectConfig::KdeConnectConfig() d->privateKey = QCA::KeyGenerator().createRSA(2048); if (!privKey.open(QIODevice::ReadWrite | QIODevice::Truncate)) { - KNotification::event(KNotification::StandardEvent::Error, QLatin1String("KDE Connect"), - i18n("Could not store private key file: %1", keyPath)); + Daemon::instance()->reportError(QLatin1String("KDE Connect"), i18n("Could not store private key file: %1", keyPath)); } else { privKey.setPermissions(strict); privKey.write(d->privateKey.toPEM().toLatin1()); diff --git a/daemon/CMakeLists.txt b/daemon/CMakeLists.txt index 8847c32ae..d3133a3ef 100644 --- a/daemon/CMakeLists.txt +++ b/daemon/CMakeLists.txt @@ -1,9 +1,11 @@ project(kdeconnectd) +find_package(KF5 REQUIRED COMPONENTS Notifications) + add_definitions(-DTRANSLATION_DOMAIN="kdeconnect-daemon") add_executable(kdeconnectd kdeconnectd.cpp) -target_link_libraries(kdeconnectd kdeconnectcore KF5::DBusAddons Qt5::Widgets) +target_link_libraries(kdeconnectd kdeconnectcore KF5::DBusAddons KF5::Notifications KF5::I18n Qt5::Widgets) configure_file(kdeconnectd.desktop.cmake ${CMAKE_CURRENT_BINARY_DIR}/kdeconnectd.desktop) configure_file(org.kde.kdeconnect.service.in ${CMAKE_CURRENT_BINARY_DIR}/org.kde.kdeconnect.service) diff --git a/daemon/kdeconnectd.cpp b/daemon/kdeconnectd.cpp index cb7de79f0..25632fd0f 100644 --- a/daemon/kdeconnectd.cpp +++ b/daemon/kdeconnectd.cpp @@ -27,8 +27,11 @@ #include #include +#include +#include #include "core/daemon.h" +#include "core/device.h" #include "kdeconnect-version.h" static int sigtermfd[2]; @@ -58,6 +61,33 @@ void initializeTermHandlers(QCoreApplication* app, Daemon* daemon) sigaction(SIGINT, &action, NULL); } +class DesktopDaemon : public Daemon +{ + Q_OBJECT +public: + DesktopDaemon(QObject* parent = Q_NULLPTR) + : Daemon(parent) + {} + + void requestPairing(Device* d) Q_DECL_OVERRIDE + { + KNotification* notification = new KNotification("pairingRequest"); + notification->setIconName(QStringLiteral("dialog-information")); + notification->setComponentName("kdeconnect"); + notification->setText(i18n("Pairing request from %1", d->name())); + notification->setActions(QStringList() << i18n("Accept") << i18n("Reject")); + connect(notification, &KNotification::ignored, d, &Device::rejectPairing); + connect(notification, &KNotification::action1Activated, d, &Device::acceptPairing); + connect(notification, &KNotification::action2Activated, d, &Device::rejectPairing); + notification->sendEvent(); + } + + void reportError(const QString & title, const QString & description) Q_DECL_OVERRIDE + { + KNotification::event(KNotification::Error, title, description); + } +}; + int main(int argc, char* argv[]) { QApplication app(argc, argv); @@ -68,9 +98,11 @@ int main(int argc, char* argv[]) KDBusService dbusService(KDBusService::Unique); - Daemon* daemon = new Daemon(0); + Daemon* daemon = new DesktopDaemon; QObject::connect(daemon, SIGNAL(destroyed(QObject*)), &app, SLOT(quit())); initializeTermHandlers(&app, daemon); return app.exec(); } + +#include "kdeconnectd.moc" diff --git a/plugins/battery/CMakeLists.txt b/plugins/battery/CMakeLists.txt index f6a6c5b32..15b697a87 100644 --- a/plugins/battery/CMakeLists.txt +++ b/plugins/battery/CMakeLists.txt @@ -1,3 +1,5 @@ +find_package(KF5 REQUIRED COMPONENTS Notifications) + set(kdeconnect_battery_SRCS batteryplugin.cpp batterydbusinterface.cpp diff --git a/plugins/notifications/CMakeLists.txt b/plugins/notifications/CMakeLists.txt index 565bf7fd5..341381a1a 100644 --- a/plugins/notifications/CMakeLists.txt +++ b/plugins/notifications/CMakeLists.txt @@ -1,3 +1,5 @@ +find_package(KF5 REQUIRED COMPONENTS Notifications) + set(kdeconnect_notifications_SRCS notification.cpp notificationsplugin.cpp diff --git a/plugins/ping/CMakeLists.txt b/plugins/ping/CMakeLists.txt index 15daa70f9..e50f88f62 100644 --- a/plugins/ping/CMakeLists.txt +++ b/plugins/ping/CMakeLists.txt @@ -1,3 +1,5 @@ +find_package(KF5 REQUIRED COMPONENTS Notifications) + set(kdeconnect_ping_SRCS pingplugin.cpp ) diff --git a/plugins/screensaver-inhibit/CMakeLists.txt b/plugins/screensaver-inhibit/CMakeLists.txt index 893693086..10a0928d6 100644 --- a/plugins/screensaver-inhibit/CMakeLists.txt +++ b/plugins/screensaver-inhibit/CMakeLists.txt @@ -1,3 +1,5 @@ +find_package(KF5 REQUIRED COMPONENTS Notifications) + set(kdeconnect_screensaver_inhibit_SRCS screensaverinhibitplugin.cpp ) diff --git a/plugins/sftp/CMakeLists.txt b/plugins/sftp/CMakeLists.txt index cecedf8c1..34070315c 100644 --- a/plugins/sftp/CMakeLists.txt +++ b/plugins/sftp/CMakeLists.txt @@ -1,3 +1,5 @@ +find_package(KF5 REQUIRED COMPONENTS Notifications) + set(kdeconnect_sftp_SRCS mounter.cpp mountloop.cpp diff --git a/plugins/share/CMakeLists.txt b/plugins/share/CMakeLists.txt index fed160c08..fa7080060 100644 --- a/plugins/share/CMakeLists.txt +++ b/plugins/share/CMakeLists.txt @@ -1,3 +1,5 @@ +find_package(KF5 REQUIRED COMPONENTS Notifications) + set(kdeconnect_share_SRCS shareplugin.cpp autoclosingqfile.cpp @@ -27,6 +29,7 @@ target_link_libraries( kdeconnect_share_config KF5::CoreAddons KF5::ConfigWidgets KF5::KIOWidgets + KF5::Notifications ) install(TARGETS kdeconnect_share_config DESTINATION ${PLUGIN_INSTALL_DIR} ) diff --git a/plugins/telephony/CMakeLists.txt b/plugins/telephony/CMakeLists.txt index 9ec302759..c6ef82420 100644 --- a/plugins/telephony/CMakeLists.txt +++ b/plugins/telephony/CMakeLists.txt @@ -1,3 +1,5 @@ +find_package(KF5 REQUIRED COMPONENTS Notifications) + set(kdeconnect_telephony_SRCS telephonyplugin.cpp )