Skip to content

Commit

Permalink
Allow to show window over dbus
Browse files Browse the repository at this point in the history
  • Loading branch information
neochapay committed Sep 21, 2023
1 parent e61b87f commit 431b92f
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 4 deletions.
1 change: 1 addition & 0 deletions config.h.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <QtCore/QCoreApplication>
#include <QtCore/QDir>
#include <QDebug>

inline bool isRunningInstalled()
{
Expand Down
8 changes: 4 additions & 4 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
set(SRC glacierapp.cpp)
set(SRC glacierapp.cpp dbusadaptor.cpp)
set(HEADERS glacierapp.h)

set(PRIVATE_HEADERS dbusadaptor.h)

set(PACKAGE glacierapp${LIBGLACIER_PKG_VER})
add_library(${PACKAGE} SHARED ${SRC} ${HEADERS})
add_library(${PACKAGE} SHARED ${SRC} ${HEADERS} ${PRIVATE_HEADERS})
add_library(Glacier${LIBGLACIER_PKG_VER}::App ALIAS ${PACKAGE})

set(QT_LIB_LIST Core Gui Qml Quick)
set(QT_LIB_LIST Core Gui Qml Quick DBus)
find_qt_package("${QT_LIB_LIST}" REQUIRED )
add_qt_library(QT_DEPLIBS "${QT_LIB_LIST}")

Expand Down
24 changes: 24 additions & 0 deletions src/dbusadaptor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "dbusadaptor.h"
#include "qcoreapplication.h"

#include <QDBusConnection>

DBusAdaptor::DBusAdaptor(QQuickWindow* window)
: QDBusAbstractAdaptor(window)
, m_window(window)
{
QDBusConnection sessionbus = QDBusConnection::sessionBus();
QString serviceName = QString("org.glacier.%1").arg(qApp->applicationName().replace("-", "_"));

QDBusConnection::sessionBus().registerService(serviceName);
if (!QDBusConnection::sessionBus().registerObject("/", m_window)) {
qWarning() << Q_FUNC_INFO << "Cannot register DBus object!";
};
}

void DBusAdaptor::show(QStringList args)
{
Q_UNUSED(args);
qDebug() << "show window from dbus call";
m_window->show();
}
21 changes: 21 additions & 0 deletions src/dbusadaptor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef DBUSADAPTOR_H
#define DBUSADAPTOR_H

#include <QDBusAbstractAdaptor>
#include <QQuickWindow>

class DBusAdaptor : public QDBusAbstractAdaptor {
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "glacier.app")

public:
explicit DBusAdaptor(QQuickWindow* window);

public slots:
void show(QStringList args = QStringList());

private:
QQuickWindow* m_window;
};

#endif // DBUSADAPTOR_H
15 changes: 15 additions & 0 deletions src/glacierapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@

#include "glacierapp.h"
#include "config.h"
#include "dbusadaptor.h"

#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <QDebug>
#include <QFileInfo>
#include <QGuiApplication>
Expand Down Expand Up @@ -126,9 +129,21 @@ QQuickWindow* GlacierApp::showWindow()
window->setHeight(settings.value("size/h", 640).toInt());

#endif
QString serviceName = QString("org.glacier.%1").arg(qApp->applicationName().replace("-", "_"));
QDBusConnection sessionBus = QDBusConnection::sessionBus();
if (sessionBus.interface()->isServiceRegistered(serviceName)) {
qWarning() << "Current application run shadow mode.";
QDBusMessage message = QDBusMessage::createMethodCall(serviceName,
"/",
"glacier.app",
"show");
sessionBus.call(message);
return nullptr;
}

if (QCoreApplication::arguments().contains("--prestart") || QCoreApplication::arguments().contains("-p")) {
qDebug() << "Application run in shadow mode";
new DBusAdaptor(window);
} else {
window->show();
}
Expand Down

0 comments on commit 431b92f

Please sign in to comment.