summaryrefslogtreecommitdiff
path: root/rockwork/pebbles.cpp
blob: e45691e3f0bc2f576bc8b5d41705b8f08947d4e8 (plain)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "pebbles.h"
#include "pebble.h"

#include <QDBusConnection>
#include <QDBusInterface>
#include <QDebug>
#include <QDBusArgument>
#include <QDBusServiceWatcher>
#include <algorithm>

#define ROCKWORK_SERVICE QStringLiteral("org.rockwork")
#define ROCKWORK_MANAGER_PATH QStringLiteral("/org/rockwork/Manager")
#define ROCKWORK_MANAGER_INTERFACE QStringLiteral("org.rockwork.Manager")

Pebbles::Pebbles(QObject *parent):
    QAbstractListModel(parent)
{
    refresh();
    m_watcher = new QDBusServiceWatcher(ROCKWORK_SERVICE, QDBusConnection::sessionBus(), QDBusServiceWatcher::WatchForRegistration, this);
    QDBusConnection::sessionBus().connect(ROCKWORK_SERVICE, ROCKWORK_MANAGER_PATH, ROCKWORK_MANAGER_INTERFACE, "PebblesChanged", this, SLOT(refresh()));
    connect(m_watcher, &QDBusServiceWatcher::serviceRegistered, [this]() {
        refresh();
        QDBusConnection::sessionBus().connect(ROCKWORK_SERVICE, ROCKWORK_MANAGER_PATH, ROCKWORK_MANAGER_INTERFACE, "PebblesChanged", this, SLOT(refresh()));
    });
}

int Pebbles::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return m_pebbles.count();
}

QVariant Pebbles::data(const QModelIndex &index, int role) const
{
    switch (role) {
    case RoleAddress:
        return m_pebbles.at(index.row())->address();
    case RoleName:
        return m_pebbles.at(index.row())->name();
    case RoleSerialNumber:
        return m_pebbles.at(index.row())->serialNumber();
    case RoleConnected:
        return m_pebbles.at(index.row())->connected();
    }

    return QVariant();
}

QHash<int, QByteArray> Pebbles::roleNames() const
{
    QHash<int,QByteArray> roles;
    roles.insert(RoleAddress, "address");
    roles.insert(RoleName, "name");
    roles.insert(RoleSerialNumber, "serialNumber");
    roles.insert(RoleConnected, "connected");
    return roles;
}

QString Pebbles::version() const
{
    QDBusInterface iface(ROCKWORK_SERVICE, ROCKWORK_MANAGER_PATH, ROCKWORK_MANAGER_INTERFACE);
    if (!iface.isValid()) {
        qWarning() << "Could not connect to rockworkd.";
        return QString();
    }
    QDBusMessage reply = iface.call("Version");
    if (reply.type() == QDBusMessage::ErrorMessage) {
        qWarning() << "Error refreshing watches:" << reply.errorMessage();
        return QString();
    }
    if (reply.arguments().count() == 0) {
        qWarning() << "No reply from service.";
        return QString();
    }
    return reply.arguments().first().toString();
}

Pebble *Pebbles::get(int index) const
{
    return m_pebbles.at(index);
}

int Pebbles::find(const QString &address) const
{
    for (int i = 0; i < m_pebbles.count(); i++) {
        if (m_pebbles.at(i)->address() == address) {
            return i;
        }
    }
    return -1;
}

void Pebbles::refresh()
{
    qDebug() << "pebbles changed";
    QDBusInterface iface(ROCKWORK_SERVICE, ROCKWORK_MANAGER_PATH, ROCKWORK_MANAGER_INTERFACE);
    if (!iface.isValid()) {
        qWarning() << "Could not connect to rockworkd.";
        return;
    }
    QDBusMessage reply = iface.call("ListWatches");
    if (reply.type() == QDBusMessage::ErrorMessage) {
        qWarning() << "Error refreshing watches:" << reply.errorMessage();
        return;
    }
    if (reply.arguments().count() == 0) {
        qWarning() << "No reply from service.";
        return;
    }
    QDBusArgument arg = reply.arguments().first().value<QDBusArgument>();
    QStringList availableList;
    arg.beginArray();
    while (!arg.atEnd()) {
        QDBusObjectPath p;
        arg >> p;
        if (find(p) == -1) {
            Pebble *pebble = new Pebble(p, this);
            connect(pebble, &Pebble::connectedChanged, this, &Pebbles::pebbleConnectedChanged);
            beginInsertRows(QModelIndex(), m_pebbles.count(), m_pebbles.count());
            m_pebbles.append(pebble);
            endInsertRows();
            emit countChanged();
        }
        availableList << p.path();
        std::sort(m_pebbles.begin(), m_pebbles.end(), Pebbles::sortPebbles);
    }
    arg.endArray();

    QList<Pebble*> toRemove;
    foreach (Pebble *pebble, m_pebbles) {
        bool found = false;
        foreach (const QString &path, availableList) {
            if (path == pebble->path().path()) {
                found = true;
                break;
            }
        }
        if (!found) {
            toRemove << pebble;
        }
    }

    while (!toRemove.isEmpty()) {
        Pebble *pebble = toRemove.takeFirst();
        int idx = m_pebbles.indexOf(pebble);
        beginRemoveRows(QModelIndex(), idx, idx);
        m_pebbles.takeAt(idx)->deleteLater();
        endRemoveRows();
        emit countChanged();
    }
}

bool Pebbles::sortPebbles(Pebble *a, Pebble *b)
{
    if (a->connected() && !b->connected()) {
        return true;
    }
    else if (!a->connected() && b->connected()) {
        return false;
    }
    else {
        return a->name() < b->name();
    }
}

void Pebbles::pebbleConnectedChanged()
{
    Pebble *pebble = static_cast<Pebble*>(sender());
    emit dataChanged(index(find(pebble->address())), index(find(pebble->address())), {RoleConnected});
}

int Pebbles::find(const QDBusObjectPath &path) const
{
    for (int i = 0; i < m_pebbles.count(); i++) {
        if (m_pebbles.at(i)->path() == path) {
            return i;
        }
    }
    return -1;
}