1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
#include "pebbledinterface.h"
QString PebbledInterface::PEBBLED_SYSTEMD_UNIT("pebbled.service");
QString PebbledInterface::PEBBLED_DBUS_SERVICE("org.pebbled");
QString PebbledInterface::PEBBLED_DBUS_PATH("/");
QString PebbledInterface::PEBBLED_DBUS_IFACE("org.pebbled");
PebbledInterface::PebbledInterface(QObject *parent) :
QObject(parent), pebbled(0), systemd(0)
{
QDBusConnection::sessionBus().connect(
PEBBLED_DBUS_SERVICE, PEBBLED_DBUS_PATH, PEBBLED_DBUS_IFACE,
"connectedChanged", this, SIGNAL(connectedChanged()));
QDBusConnection::sessionBus().connect(
PEBBLED_DBUS_SERVICE, PEBBLED_DBUS_PATH, PEBBLED_DBUS_IFACE,
"pebbleChanged", this, SLOT(onPebbleChanged()));
// simulate connected change on active changed
// as the daemon might not had a chance to send connectedChanged()
connect(this, SIGNAL(activeChanged()), SIGNAL(connectedChanged()));
pebbled = new QDBusInterface(PEBBLED_DBUS_SERVICE, PEBBLED_DBUS_PATH, PEBBLED_DBUS_IFACE,
QDBusConnection::sessionBus(), this);
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_SYSTEMD_UNIT);
if (unit.isValid()) {
unitPath = unit.value();
getUnitProperties();
QDBusConnection::sessionBus().connect(
"org.freedesktop.systemd1", unitPath.path(),
"org.freedesktop.DBus.Properties", "PropertiesChanged",
this, SLOT(onPropertiesChanged(QString,QMap<QString,QVariant>,QStringList)));
} else {
qWarning() << unit.error().message();
}
}
void PebbledInterface::getUnitProperties()
{
QDBusMessage request = QDBusMessage::createMethodCall(
"org.freedesktop.systemd1", unitPath.path(),
"org.freedesktop.DBus.Properties", "GetAll");
request << "org.freedesktop.systemd1.Unit";
QDBusReply<QVariantMap> reply = QDBusConnection::sessionBus().call(request);
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()";
return properties["UnitFileState"].toString() == "enabled";
}
void PebbledInterface::setEnabled(bool enabled)
{
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()";
return properties["ActiveState"].toString() == "active";
}
void PebbledInterface::setActive(bool active)
{
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() << __FUNCTION__;
return pebbled->property(__FUNCTION__).toBool();
}
QVariantMap PebbledInterface::pebble() const
{
qDebug() << __FUNCTION__;
return pebbled->property(__FUNCTION__).toMap();
}
QString PebbledInterface::name() const
{
qDebug() << __FUNCTION__;
return pebbled->property(__FUNCTION__).toString();
}
QString PebbledInterface::address() const
{
qDebug() << __FUNCTION__;
return pebbled->property(__FUNCTION__).toString();
}
void PebbledInterface::ping()
{
pebbled->call("ping", 66);
}
void PebbledInterface::time()
{
pebbled->call("time");
}
void PebbledInterface::disconnect()
{
pebbled->call("disconnect");
}
void PebbledInterface::reconnect()
{
pebbled->call("reconnect");
}
|