summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/pebbledinterface.cpp130
-rw-r--r--app/pebbledinterface.h18
-rw-r--r--app/qml/pages/ManagerPage.qml10
-rw-r--r--daemon/watchconnector.cpp7
-rw-r--r--daemon/watchconnector.h1
5 files changed, 121 insertions, 45 deletions
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<QDBusObjectPath> unit = systemd->call("LoadUnit", "pebbled.service");
+ QDBusReply<QDBusObjectPath> 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<QString,QVariant>,QStringList))
+ );
}
- qDebug() << "pebbled.service unit:" << systemdUnit;
+}
+
+void PebbledInterface::getUnitProperties()
+{
+ QDBusReply<QVariantMap> 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<QString,QVariant> 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<QDBusObjectPath> 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();
}
diff --git a/app/pebbledinterface.h b/app/pebbledinterface.h
index 110cecc..2afc6c0 100644
--- a/app/pebbledinterface.h
+++ b/app/pebbledinterface.h
@@ -3,18 +3,20 @@
#include <QObject>
#include <QtDBus/QtDBus>
+#include <QDBusArgument>
class PebbledInterface : public QObject
{
Q_OBJECT
+ static QString PEBBLED_SYSTEMD_UNIT;
+ static QString SYSTEMD_UNIT_IFACE;
+
Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
bool enabled() const;
- void setEnabled(bool);
Q_PROPERTY(bool active READ active WRITE setActive NOTIFY activeChanged)
bool active() const;
- void setActive(bool);
Q_PROPERTY(bool connected READ connected NOTIFY connectedChanged)
bool connected() const;
@@ -42,12 +44,20 @@ signals:
void addressChanged();
public slots:
+ void setEnabled(bool);
+ void setActive(bool);
+
+private slots:
+ void getUnitProperties();
+ void onPropertiesChanged(QString interface, QMap<QString, QVariant> changed, QStringList invalidated);
+ void onPebbleChanged();
private:
+ QDBusInterface *pebbled;
QDBusInterface *systemd;
- QString systemdUnit;
+ QDBusInterface *unitprops;
- QDBusInterface *pebbled;
+ QVariantMap properties;
};
#endif // PEBBLEDINTERFACE_H
diff --git a/app/qml/pages/ManagerPage.qml b/app/qml/pages/ManagerPage.qml
index c042adc..f24fae7 100644
--- a/app/qml/pages/ManagerPage.qml
+++ b/app/qml/pages/ManagerPage.qml
@@ -53,7 +53,7 @@ Page {
Label {
color: Theme.highlightColor
font.pixelSize: Theme.fontSizeSmall
- visible: !pebbled.connected
+ visible: pebbled.active && !pebbled.connected
text: qsTr("Waiting for watch...\nIf it can't be found plase check it's available and paired in Bluetooth settings.")
wrapMode: Text.Wrap
anchors {
@@ -89,18 +89,14 @@ Page {
description: pebbled.enabled ? qsTr("Automatic startup") : qsTr("Manual startup")
checked: pebbled.enabled
automaticCheck: false
- onClicked: {
- console.log('pebbled.(dis|en)able()');
- }
+ onClicked: pebbled.setEnabled(!checked)
}
TextSwitch {
text: qsTr("Active")
description: pebbled.active ? qsTr("Running") : qsTr("Dead")
checked: pebbled.active
automaticCheck: false
- onClicked: {
- console.log('pebbled.start|stop()');
- }
+ onClicked: pebbled.setActive(!checked)
}
TextSwitch {
text: qsTr("Connection")
diff --git a/daemon/watchconnector.cpp b/daemon/watchconnector.cpp
index 18d287e..dbcd831 100644
--- a/daemon/watchconnector.cpp
+++ b/daemon/watchconnector.cpp
@@ -38,6 +38,13 @@ void WatchConnector::reconnect()
}
}
+void WatchConnector::disconnect()
+{
+ qDebug() << __FUNCTION__;
+ socket->close();
+ socket->deleteLater();
+}
+
void WatchConnector::handleWatch(const QString &name, const QString &address)
{
qDebug() << "handleWatch" << name << address;
diff --git a/daemon/watchconnector.h b/daemon/watchconnector.h
index 493856b..eaeccdc 100644
--- a/daemon/watchconnector.h
+++ b/daemon/watchconnector.h
@@ -111,6 +111,7 @@ public slots:
void endPhoneCall(unsigned int cookie=0);
void deviceConnect(const QString &name, const QString &address);
+ void disconnect();
void deviceDiscovered(const QBluetoothDeviceInfo&);
void handleWatch(const QString &name, const QString &address);
void onReadSocket();