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/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_ 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 62% rename from src/kvilib/ext/KviDbusAdaptor.cpp rename to src/kvirc/kernel/KviDbusAdaptor.cpp index 4c242a3651..16c167249d 100644 --- a/src/kvilib/ext/KviDbusAdaptor.cpp +++ b/src/kvirc/kernel/KviDbusAdaptor.cpp @@ -24,11 +24,37 @@ #include "KviDbusAdaptor.h" +#include "KviApplication.h" + #ifdef COMPILE_DBUS_SUPPORT +#include //for qWarning() +#include + KviDbusAdaptor::KviDbusAdaptor(QObject * pObj) : QDBusAbstractAdaptor(pObj) { + setAutoRelaySignals(false); } +void KviDbusAdaptor::registerToSessionBus() { + auto connection = QDBusConnection::sessionBus(); + if(!connection.registerService(KVI_DBUS_INTERFACENAME)) { + qWarning() << "D-Bus service registration failed:" << connection.lastError().message(); + } + 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/kvilib/ext/KviDbusAdaptor.h b/src/kvirc/kernel/KviDbusAdaptor.h similarity index 78% rename from src/kvilib/ext/KviDbusAdaptor.h rename to src/kvirc/kernel/KviDbusAdaptor.h index 3a71ad2034..76f5425117 100644 --- a/src/kvilib/ext/KviDbusAdaptor.h +++ b/src/kvirc/kernel/KviDbusAdaptor.h @@ -31,13 +31,25 @@ #include #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 { 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(); + +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 53376a62a8..c11dbd775c 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 @@ -381,13 +379,6 @@ int main(int argc, char ** argv) delete pAboutData; #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 -#endif - QString szRemoteCommand = a.szExecCommand; if(!a.szExecRemoteCommand.isEmpty()) { @@ -450,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;