From 5c8b0d9b4f6ca7dc67c52040be30a0d60218cce9 Mon Sep 17 00:00:00 2001 From: Tomasz Sterna Date: Wed, 9 Jul 2014 01:55:13 +0200 Subject: Implemented daemon interfaces for QML Manager app --- app/pebbledinterface.cpp | 130 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 96 insertions(+), 34 deletions(-) (limited to 'app/pebbledinterface.cpp') diff --git a/app/pebbledinterface.cpp b/app/pebbledinterface.cpp index 4b56bed..9710d78 100644 --- a/app/pebbledinterface.cpp +++ b/app/pebbledinterface.cpp @@ -1,82 +1,144 @@ #include "pebbledinterface.h" +QString PebbledInterface::PEBBLED_SYSTEMD_UNIT("pebbled.service"); +QString PebbledInterface::SYSTEMD_UNIT_IFACE("org.freedesktop.systemd1.Unit"); + + PebbledInterface::PebbledInterface(QObject *parent) : - QObject(parent), - systemd() + QObject(parent), pebbled(0), systemd(0), unitprops(0) { - systemd = new QDBusInterface("org.freedesktop.systemd1", - "/org/freedesktop/systemd1", - "org.freedesktop.systemd1.Manager", - QDBusConnection::systemBus(), this); - pebbled = new QDBusInterface("org.pebbled", "/", "org.pebbled", QDBusConnection::sessionBus(), this); + pebbled->connection() + .connect(pebbled->service(), pebbled->path(), pebbled->interface(), + "connectedChanged", this, SIGNAL(connectedChanged())); + pebbled->connection() + .connect(pebbled->service(), pebbled->path(), pebbled->interface(), + "pebbleChanged", this, SLOT(onPebbleChanged())); + + // simulate connected change on active changed + connect(this, SIGNAL(activeChanged()), this, SIGNAL(connectedChanged())); + + systemd = new QDBusInterface("org.freedesktop.systemd1", + "/org/freedesktop/systemd1", + "org.freedesktop.systemd1.Manager", + QDBusConnection::sessionBus(), this); + + systemd->call("Subscribe"); - QDBusReply unit = systemd->call("LoadUnit", "pebbled.service"); + QDBusReply unit = systemd->call("LoadUnit", PEBBLED_SYSTEMD_UNIT); if (not unit.isValid()) { qWarning() << unit.error().message(); } else { - systemdUnit = unit.value().path(); + unitprops = new QDBusInterface("org.freedesktop.systemd1", + unit.value().path(), + "org.freedesktop.DBus.Properties", + QDBusConnection::sessionBus(), this); + getUnitProperties(); + + unitprops->connection() + .connect("org.freedesktop.systemd1", + unitprops->path(), + "org.freedesktop.DBus.Properties", + "PropertiesChanged", + this, + SLOT(onPropertiesChanged(QString,QMap,QStringList)) + ); } - qDebug() << "pebbled.service unit:" << systemdUnit; +} + +void PebbledInterface::getUnitProperties() +{ + QDBusReply reply = unitprops->call("GetAll", SYSTEMD_UNIT_IFACE); + if (reply.isValid()) { + QVariantMap newProperties = reply.value(); + bool emitEnabledChanged = (properties["UnitFileState"] != newProperties["UnitFileState"]); + bool emitActiveChanged = (properties["ActiveState"] != newProperties["ActiveState"]); + properties = newProperties; + if (emitEnabledChanged) emit enabledChanged(); + if (emitActiveChanged) emit activeChanged(); + } else { + qWarning() << reply.error().message(); + } +} + +void PebbledInterface::onPropertiesChanged(QString interface, QMap changed, QStringList invalidated) +{ + qDebug() << __FUNCTION__ << interface << changed << invalidated; + if (interface != "org.freedesktop.systemd1.Unit") return; + if (invalidated.contains("UnitFileState") or invalidated.contains("ActiveState")) + getUnitProperties(); +} + +void PebbledInterface::onPebbleChanged() +{ + qDebug() << __FUNCTION__; + emit nameChanged(); + emit addressChanged(); + emit pebbleChanged(); } bool PebbledInterface::enabled() const { qDebug() << "enabled()"; - // FIXME: implement - return true; + return properties["UnitFileState"].toString() == "enabled"; } void PebbledInterface::setEnabled(bool enabled) { - bool doEmit = (this->enabled() != enabled); - qDebug() << "setEnabled" << this->enabled() << enabled; - // FIXME: implement - if (doEmit) emit enabledChanged(); + if (systemd) { + qDebug() << "setEnabled" << enabled; + QDBusError reply; + if (enabled) systemd->call("EnableUnitFiles", QStringList() << PEBBLED_SYSTEMD_UNIT, false, true); + else systemd->call("DisableUnitFiles", QStringList() << PEBBLED_SYSTEMD_UNIT, false); + if (reply.isValid()) { + qWarning() << reply.message(); + } else { + systemd->call("Reload"); + getUnitProperties(); + } + } } bool PebbledInterface::active() const { qDebug() << "active()"; - // FIXME: implement - return true; + return properties["ActiveState"].toString() == "active"; } void PebbledInterface::setActive(bool active) { - bool doEmit = (this->active() != active); - qDebug() << "setActive" << this->active() << active; - // FIXME: implement - if (doEmit) emit activeChanged(); + if (systemd) { + qDebug() << "setActive" << active; + QDBusReply reply = systemd->call(active?"StartUnit":"StopUnit", PEBBLED_SYSTEMD_UNIT, "replace"); + if (!reply.isValid()) { + qWarning() << reply.error().message(); + } + } } bool PebbledInterface::connected() const { - qDebug() << "connected()"; - // FIXME: implement - return true; + qDebug() << __FUNCTION__; + return pebbled->property(__FUNCTION__).toBool(); } QVariantMap PebbledInterface::pebble() const { - qDebug() << "pebble()"; - // FIXME: implement - return QVariantMap(); + qDebug() << __FUNCTION__; + return pebbled->property(__FUNCTION__).toMap(); } QString PebbledInterface::name() const { - qDebug() << "name()"; - // FIXME: implement - return QString("Pebble XXX"); + qDebug() << __FUNCTION__; + return pebbled->property(__FUNCTION__).toString(); } QString PebbledInterface::address() const { - qDebug() << "address()"; - // FIXME: implement - return QString(""); + qDebug() << __FUNCTION__; + return pebbled->property(__FUNCTION__).toString(); } -- cgit v1.2.3