summaryrefslogblamecommitdiffstats
path: root/src/bus.cpp
blob: 1a722c1d66ecad64d05d9e6299381922d30978e9 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15














                                                                












                                                                                                                
         


                    




                                                                         

                           











                                                                                                    
         
                  
 
#include "bus.h"

#include <QtDBus/QtDBus>

static const QString SERVICE_NAME("de.bwlehrpool.beamergui");

Bus* Bus::_instance = nullptr;

Bus::Bus(QObject *parent) : QObject(parent), _hasListener(false)
{

}

bool Bus::registerListener()
{
	QList<QDBusConnection> cons = {QDBusConnection::systemBus(), QDBusConnection::sessionBus()};
	int index = 0; // 0 == system, 1 == session
	for (auto con : cons) {
		if (!con.isConnected()) {
			qDebug() << "Cannot connect to" << con.name();
		} else {
			QDBusServiceWatcher *w = new QDBusServiceWatcher(SERVICE_NAME, con);
			w->setParent(this);
			connect(w, &QDBusServiceWatcher::serviceRegistered, [=](const QString & /* service */) {
				emit serviceConnected(index == 1);
			});
		}
		++index;
	}
	return true;
}

/**
 * Try to grab the service name on either the system or the session
 * bus. This signals the listening beamergui (if any) that it should show
 * up again.
 */
bool Bus::registerService()
{
	bool ok = false;
	QList<QDBusConnection> cons = {QDBusConnection::systemBus(), QDBusConnection::sessionBus()};
	for (auto con : cons) {
		if (!con.isConnected()) {
			qDebug() << "Cannot connect to" << con.name();
		} else {
			if (con.registerService(SERVICE_NAME)) {
				ok = true;
			} else {
				qDebug() << con.lastError().message();
			}
		}
	}
	return ok;
}