From 2e3b9a2599fe824ec8d7d35705da651f0efaf265 Mon Sep 17 00:00:00 2001 From: Fabio Bas Date: Mon, 8 Apr 2024 15:30:11 +0200 Subject: [PATCH 1/6] Move KViDbusAdaptor from kvilib to kvirc (so that it can access the g_pApp object) and add a method to register itself --- src/kvilib/CMakeLists.txt | 1 - src/kvirc/CMakeLists.txt | 1 + src/{kvilib/ext => kvirc/kernel}/KviDbusAdaptor.cpp | 13 +++++++++++++ src/{kvilib/ext => kvirc/kernel}/KviDbusAdaptor.h | 7 ++++++- 4 files changed, 20 insertions(+), 2 deletions(-) rename src/{kvilib/ext => kvirc/kernel}/KviDbusAdaptor.cpp (72%) rename src/{kvilib/ext => kvirc/kernel}/KviDbusAdaptor.h (86%) diff --git a/src/kvilib/CMakeLists.txt b/src/kvilib/CMakeLists.txt index 577fc6fd26..f34beea6b0 100644 --- a/src/kvilib/CMakeLists.txt +++ b/src/kvilib/CMakeLists.txt @@ -26,7 +26,6 @@ set(kvilib_SRCS ext/KviCryptEngine.cpp ext/KviCryptEngineManager.cpp ext/KviDataBuffer.cpp - ext/KviDbusAdaptor.cpp ext/KviDebugContext.cpp ext/KviMediaManager.cpp ext/KviMiscUtils.cpp diff --git a/src/kvirc/CMakeLists.txt b/src/kvirc/CMakeLists.txt index e624156265..f4bb1c1524 100644 --- a/src/kvirc/CMakeLists.txt +++ b/src/kvirc/CMakeLists.txt @@ -210,6 +210,7 @@ set(kvirc_SRCS kernel/KviCoreActions.cpp kernel/KviCustomToolBarDescriptor.cpp kernel/KviCustomToolBarManager.cpp + kernel/KviDbusAdaptor.cpp kernel/KviDefaultScript.cpp kernel/KviFileTransfer.cpp kernel/KviHtmlGenerator.cpp diff --git a/src/kvilib/ext/KviDbusAdaptor.cpp b/src/kvirc/kernel/KviDbusAdaptor.cpp similarity index 72% rename from src/kvilib/ext/KviDbusAdaptor.cpp rename to src/kvirc/kernel/KviDbusAdaptor.cpp index 4c242a3651..64c8cf083c 100644 --- a/src/kvilib/ext/KviDbusAdaptor.cpp +++ b/src/kvirc/kernel/KviDbusAdaptor.cpp @@ -24,11 +24,24 @@ #include "KviDbusAdaptor.h" +#include "KviApplication.h" + #ifdef COMPILE_DBUS_SUPPORT KviDbusAdaptor::KviDbusAdaptor(QObject * pObj) : QDBusAbstractAdaptor(pObj) { + setAutoRelaySignals(false); +} + +void KviDbusAdaptor::registerToSessionBus() { + auto connection = QDBusConnection::sessionBus(); + if(!connection.registerService(KVI_DBUS_SERVICENAME)) { + qWarning() << "D-Bus service registration failed:" << connection.lastError().message(); + } + if(!connection.registerObject("/", g_pApp)) { + qWarning() << "D-Bus object registration failed:" << connection.lastError().message(); + } } #endif diff --git a/src/kvilib/ext/KviDbusAdaptor.h b/src/kvirc/kernel/KviDbusAdaptor.h similarity index 86% rename from src/kvilib/ext/KviDbusAdaptor.h rename to src/kvirc/kernel/KviDbusAdaptor.h index 3a71ad2034..ad736ea413 100644 --- a/src/kvilib/ext/KviDbusAdaptor.h +++ b/src/kvirc/kernel/KviDbusAdaptor.h @@ -31,13 +31,18 @@ #include #include +#define KVI_DBUS_SERVICENAME "net.kvirc.KVIrc" + class KVILIB_API KviDbusAdaptor : public QDBusAbstractAdaptor { Q_OBJECT - Q_CLASSINFO("KVIrc D-Bus Interface", "org.kvirc.KVIrc") + Q_CLASSINFO("D-Bus Interface", KVI_DBUS_SERVICENAME) + // don't change "D-Bus Interface", it needs to be exactly that string public: KviDbusAdaptor(QObject * pObj); + virtual ~KviDbusAdaptor() = default; + void registerToSessionBus(); }; #endif // COMPILE_DBUS_SUPPORT From 6835471042b990a1e6983ffa8f0966f10b5a5726 Mon Sep 17 00:00:00 2001 From: Fabio Bas Date: Mon, 8 Apr 2024 15:31:14 +0200 Subject: [PATCH 2/6] KviMain: register a dbus session even when kdelibs are enabled --- src/kvirc/kernel/KviMain.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/kvirc/kernel/KviMain.cpp b/src/kvirc/kernel/KviMain.cpp index 53376a62a8..3064d39023 100644 --- a/src/kvirc/kernel/KviMain.cpp +++ b/src/kvirc/kernel/KviMain.cpp @@ -31,10 +31,8 @@ #include "KviMessageBox.h" #include "KviBuildInfo.h" #ifdef COMPILE_DBUS_SUPPORT -#ifndef COMPILE_KDE_SUPPORT // 'cause kde adds an interface itself #include "KviDbusAdaptor.h" #endif -#endif #ifndef COMPILE_NO_IPC extern bool kvi_sendIpcMessage(const char * message); // KviIpcSentinel.cpp #endif @@ -382,10 +380,9 @@ int main(int argc, char ** argv) #endif #ifdef COMPILE_DBUS_SUPPORT -#ifndef COMPILE_KDE_SUPPORT - new KviDbusAdaptor(pTheApp); // FIXME: shouldn't this be deleted by someone ? - QDBusConnection::sessionBus().registerObject("/MainApplication", pTheApp); -#endif + // deleted automatically when pTheApp gets destroyed + KviDbusAdaptor * pDbusAdaptor = new KviDbusAdaptor(pTheApp); + pDbusAdaptor->registerToSessionBus(); #endif QString szRemoteCommand = a.szExecCommand; From 3729eece1f577981ed3195f44111228557b3289b Mon Sep 17 00:00:00 2001 From: Fabio Bas Date: Mon, 8 Apr 2024 15:31:56 +0200 Subject: [PATCH 3/6] KviTalMainWindow: when kdelibs are enabled, avoid KMainWindow to expose all of its signal/lots via dbus --- src/kvilib/tal/KviTalMainWindow.cpp | 17 +++++++++++++++++ src/kvilib/tal/KviTalMainWindow.h | 7 ++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/kvilib/tal/KviTalMainWindow.cpp b/src/kvilib/tal/KviTalMainWindow.cpp index 4011d280d2..efa0d48060 100644 --- a/src/kvilib/tal/KviTalMainWindow.cpp +++ b/src/kvilib/tal/KviTalMainWindow.cpp @@ -26,6 +26,8 @@ #ifdef COMPILE_KDE_SUPPORT +#include + KviTalMainWindow::KviTalMainWindow(QWidget * pParent, const char * pcName) : KMainWindow(pParent) { @@ -44,3 +46,18 @@ KviTalMainWindow::KviTalMainWindow(QWidget * pParent, const char * pcName) KviTalMainWindow::~KviTalMainWindow() = default; + +#ifdef COMPILE_KDE_SUPPORT +bool KviTalMainWindow::event(QEvent *ev) +{ + /** + * KMainWindow uses this event to ensure the object as an objectname and + * also register the mainwindow as an exported object on d-bus. + * We already ensure an object name is set and want to avoid the dbus registration + */ + if(ev->type() == QEvent::Polish) + return QMainWindow::event(ev); + + return KMainWindow::event(ev); +} +#endif \ No newline at end of file diff --git a/src/kvilib/tal/KviTalMainWindow.h b/src/kvilib/tal/KviTalMainWindow.h index 35c1500044..2c29af2fa7 100644 --- a/src/kvilib/tal/KviTalMainWindow.h +++ b/src/kvilib/tal/KviTalMainWindow.h @@ -39,7 +39,7 @@ #ifdef COMPILE_KDE_SUPPORT -#include +#include class KVILIB_API KviTalMainWindow : public KMainWindow @@ -66,6 +66,11 @@ class KVILIB_API KviTalMainWindow : public QMainWindow * \return KviTalMainWindow */ ~KviTalMainWindow(); + +#ifdef COMPILE_KDE_SUPPORT +protected: + bool event(QEvent *event) override; +#endif }; #endif // _KVI_TAL_MAINWINDOW_H_ From 05ffab0cfd75b3135536ef474707da27afacedc7 Mon Sep 17 00:00:00 2001 From: Fabio Bas Date: Mon, 8 Apr 2024 22:45:09 +0200 Subject: [PATCH 4/6] Implement a simple IPC using d-bus --- src/kvirc/kernel/KviDbusAdaptor.cpp | 16 ++++++++++++++-- src/kvirc/kernel/KviDbusAdaptor.h | 7 +++++++ src/kvirc/kernel/KviIpcSentinel.cpp | 19 ++++++++++++++++--- src/kvirc/kernel/KviMain.cpp | 15 +++++++++------ 4 files changed, 46 insertions(+), 11 deletions(-) diff --git a/src/kvirc/kernel/KviDbusAdaptor.cpp b/src/kvirc/kernel/KviDbusAdaptor.cpp index 64c8cf083c..af6864c33d 100644 --- a/src/kvirc/kernel/KviDbusAdaptor.cpp +++ b/src/kvirc/kernel/KviDbusAdaptor.cpp @@ -28,6 +28,8 @@ #ifdef COMPILE_DBUS_SUPPORT +#include + KviDbusAdaptor::KviDbusAdaptor(QObject * pObj) : QDBusAbstractAdaptor(pObj) { @@ -36,12 +38,22 @@ KviDbusAdaptor::KviDbusAdaptor(QObject * pObj) void KviDbusAdaptor::registerToSessionBus() { auto connection = QDBusConnection::sessionBus(); - if(!connection.registerService(KVI_DBUS_SERVICENAME)) { + if(!connection.registerService(KVI_DBUS_INTERFACENAME)) { qWarning() << "D-Bus service registration failed:" << connection.lastError().message(); } - if(!connection.registerObject("/", g_pApp)) { + if(!connection.registerObject(KVI_DBUS_PATH, this, QDBusConnection::ExportAllSlots)) { qWarning() << "D-Bus object registration failed:" << connection.lastError().message(); } } +#ifndef COMPILE_NO_IPC +void KviDbusAdaptor::ipcMessage(const QString &message) +{ + if(!g_pApp) + return; + + g_pApp->ipcMessage(message.toUtf8().data()); +} +#endif + #endif diff --git a/src/kvirc/kernel/KviDbusAdaptor.h b/src/kvirc/kernel/KviDbusAdaptor.h index ad736ea413..76f5425117 100644 --- a/src/kvirc/kernel/KviDbusAdaptor.h +++ b/src/kvirc/kernel/KviDbusAdaptor.h @@ -32,6 +32,8 @@ #include #define KVI_DBUS_SERVICENAME "net.kvirc.KVIrc" +#define KVI_DBUS_INTERFACENAME "net.kvirc.KVIrc" +#define KVI_DBUS_PATH "/kvirc" class KVILIB_API KviDbusAdaptor : public QDBusAbstractAdaptor { @@ -43,6 +45,11 @@ class KVILIB_API KviDbusAdaptor : public QDBusAbstractAdaptor KviDbusAdaptor(QObject * pObj); virtual ~KviDbusAdaptor() = default; void registerToSessionBus(); + +public slots: +#ifndef COMPILE_NO_IPC + void ipcMessage(const QString &message); +#endif }; #endif // COMPILE_DBUS_SUPPORT diff --git a/src/kvirc/kernel/KviIpcSentinel.cpp b/src/kvirc/kernel/KviIpcSentinel.cpp index a564064025..01f631c902 100644 --- a/src/kvirc/kernel/KviIpcSentinel.cpp +++ b/src/kvirc/kernel/KviIpcSentinel.cpp @@ -131,11 +131,17 @@ static Window kvi_x11_findIpcSentinel(Window win) return found; } -#endif //!COMPILE_NO_X - +#elif defined(COMPILE_ON_WINDOWS) || defined(COMPILE_ON_MINGW) #define KVI_WINDOWS_IPC_MESSAGE 0x2FACE5 +#elif defined(COMPILE_DBUS_SUPPORT) + +#include +#include "KviDbusAdaptor.h" + +#endif + bool kvi_sendIpcMessage(const char * message) { #if defined(COMPILE_ON_WINDOWS) || defined(COMPILE_ON_MINGW) @@ -184,7 +190,14 @@ bool kvi_sendIpcMessage(const char * message) kvi_ipcSetRemoteCommand(sentinel, message); return true; } -#endif //!COMPILE_NO_X && COMPILE_ON_WINDOWS +#elif defined(COMPILE_DBUS_SUPPORT) + QDBusInterface remoteApp(KVI_DBUS_SERVICENAME, KVI_DBUS_PATH, KVI_DBUS_INTERFACENAME, QDBusConnection::sessionBus()); + if(remoteApp.isValid()) + { + remoteApp.call( "ipcMessage", message ); + return true; + } +#endif return false; } diff --git a/src/kvirc/kernel/KviMain.cpp b/src/kvirc/kernel/KviMain.cpp index 3064d39023..c11dbd775c 100644 --- a/src/kvirc/kernel/KviMain.cpp +++ b/src/kvirc/kernel/KviMain.cpp @@ -379,12 +379,6 @@ int main(int argc, char ** argv) delete pAboutData; #endif -#ifdef COMPILE_DBUS_SUPPORT - // deleted automatically when pTheApp gets destroyed - KviDbusAdaptor * pDbusAdaptor = new KviDbusAdaptor(pTheApp); - pDbusAdaptor->registerToSessionBus(); -#endif - QString szRemoteCommand = a.szExecCommand; if(!a.szExecRemoteCommand.isEmpty()) { @@ -447,6 +441,15 @@ int main(int argc, char ** argv) } #endif +#ifdef COMPILE_DBUS_SUPPORT + /* + * D-Bus initialization must happen after IPC session handling + * The object itsef is deleted automatically when pTheApp gets destroyed + */ + KviDbusAdaptor * pDbusAdaptor = new KviDbusAdaptor(pTheApp); + pDbusAdaptor->registerToSessionBus(); +#endif + pTheApp->m_bCreateConfig = a.createFile; pTheApp->m_szConfigFile = a.configFile; pTheApp->m_szExecAfterStartup = a.szExecCommand; From 96f993782cc5b6d920abc669864912ef598bd3d3 Mon Sep 17 00:00:00 2001 From: Fabio Bas Date: Mon, 8 Apr 2024 23:08:28 +0200 Subject: [PATCH 5/6] Add missing include --- src/kvirc/kernel/KviDbusAdaptor.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/kvirc/kernel/KviDbusAdaptor.cpp b/src/kvirc/kernel/KviDbusAdaptor.cpp index af6864c33d..8f2e69916e 100644 --- a/src/kvirc/kernel/KviDbusAdaptor.cpp +++ b/src/kvirc/kernel/KviDbusAdaptor.cpp @@ -28,6 +28,7 @@ #ifdef COMPILE_DBUS_SUPPORT +#include //for qWarning() #include KviDbusAdaptor::KviDbusAdaptor(QObject * pObj) From ef89b6b00ddcd24fc5d2507d78177a554aa3480a Mon Sep 17 00:00:00 2001 From: Fabio Bas Date: Mon, 8 Apr 2024 23:14:59 +0200 Subject: [PATCH 6/6] Fix include --- src/kvirc/kernel/KviDbusAdaptor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/kvirc/kernel/KviDbusAdaptor.cpp b/src/kvirc/kernel/KviDbusAdaptor.cpp index 8f2e69916e..16c167249d 100644 --- a/src/kvirc/kernel/KviDbusAdaptor.cpp +++ b/src/kvirc/kernel/KviDbusAdaptor.cpp @@ -28,7 +28,7 @@ #ifdef COMPILE_DBUS_SUPPORT -#include //for qWarning() +#include //for qWarning() #include KviDbusAdaptor::KviDbusAdaptor(QObject * pObj)