Skip to content

Commit

Permalink
Make D-Bus adaptor actually useful and use it as an IPC backend (#2630)
Browse files Browse the repository at this point in the history
* Move KViDbusAdaptor from kvilib to kvirc (so that it can access the g_pApp object) and add a method to register itself

* KviMain: register a dbus session even when kdelibs are enabled

* KviTalMainWindow: when kdelibs are enabled, avoid KMainWindow to expose all of its signal/lots via dbus

* Implement a simple IPC using d-bus
  • Loading branch information
ctrlaltca authored Apr 10, 2024
1 parent 4f0e6a5 commit c2ae7a4
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 15 deletions.
1 change: 0 additions & 1 deletion src/kvilib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions src/kvilib/tal/KviTalMainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

#ifdef COMPILE_KDE_SUPPORT

#include <QEvent>

KviTalMainWindow::KviTalMainWindow(QWidget * pParent, const char * pcName)
: KMainWindow(pParent)
{
Expand All @@ -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
7 changes: 6 additions & 1 deletion src/kvilib/tal/KviTalMainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

#ifdef COMPILE_KDE_SUPPORT

#include <kmainwindow.h>
#include <KMainWindow>

class KVILIB_API KviTalMainWindow : public KMainWindow

Expand All @@ -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_
1 change: 1 addition & 0 deletions src/kvirc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,37 @@

#include "KviDbusAdaptor.h"

#include "KviApplication.h"

#ifdef COMPILE_DBUS_SUPPORT

#include <QtDebug> //for qWarning()
#include <QDBusConnection>

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
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,25 @@
#include <QDBusInterface>
#include <QObject>

#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

Expand Down
19 changes: 16 additions & 3 deletions src/kvirc/kernel/KviIpcSentinel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <QDBusInterface>
#include "KviDbusAdaptor.h"

#endif

bool kvi_sendIpcMessage(const char * message)
{
#if defined(COMPILE_ON_WINDOWS) || defined(COMPILE_ON_MINGW)
Expand Down Expand Up @@ -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;
}

Expand Down
18 changes: 9 additions & 9 deletions src/kvirc/kernel/KviMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
{
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit c2ae7a4

Please sign in to comment.