summaryrefslogtreecommitdiffstats
path: root/network/wifinetworkmodel.cpp
blob: 2691d6ebb5cfa929466377c28fbedd3e86363018 (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
#include "wifinetworkmodel.h"
#include <QDebug>

WifiNetwork::WifiNetwork(const QString &address,
                         const QString &security,
                         const QString &service,
                         const QString &ssid,
                         const QString &state,
                         const int &strength)
    : m_address(address), m_security(security), m_service(service),
      m_ssid(ssid), m_state(state), m_strength(strength)
{
}

QString WifiNetwork::address() const
{
    return m_address;
}

QString WifiNetwork::security() const
{
    return m_security;
}

QString WifiNetwork::service() const
{
    return m_service;
}

QString WifiNetwork::ssid() const
{
    return m_ssid;
}

QString WifiNetwork::state() const
{
    return m_state;
}

int WifiNetwork::strength() const
{
    return m_strength;
}

void WifiNetwork::setAddress(const QString address)
{
    m_address = address;
}

void WifiNetwork::setState(const QString state)
{
    m_state = state;
}

void WifiNetwork::setStrength(const int strength)
{
    m_strength = strength;
}

WifiNetworkModel::WifiNetworkModel(QObject *parent)
    : QAbstractListModel(parent)
{
}

void WifiNetworkModel::addNetwork(WifiNetwork *network)
{
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_networks << network;
    endInsertRows();
}

void WifiNetworkModel::removeNetwork(WifiNetwork *network)
{
    int row = m_networks.indexOf(network);
    beginRemoveRows(QModelIndex(), row, row);
    m_networks.removeAt(row);
    endRemoveRows();
    delete network;
}

void WifiNetworkModel::removeAllNetworks()
{
    beginRemoveRows(QModelIndex(), 0, m_networks.count() - 1);
    qDeleteAll(m_networks.begin(), m_networks.end());
    m_networks.clear();
    endRemoveRows();
}

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

QVariant WifiNetworkModel::data(const QModelIndex &index, int role) const
{
    if (index.row() < 0 || index.row() >= m_networks.count())
        return QVariant();

    const WifiNetwork *network = m_networks[index.row()];

    switch (role) {
        case AddressRole:
            return network->address();
        case SecurityRole:
            return network->security();
        case ServiceRole:
            return network->service();
        case SsidRole:
            return network->ssid();
        case StateRole:
            return network->state();
        case StrengthRole:
            return network->strength();
    }

    return QVariant();
}

QHash<int, QByteArray> WifiNetworkModel::roleNames() const {
    QHash<int, QByteArray> roles;
    roles[AddressRole] = "address";
    roles[SecurityRole] = "security";
    roles[ServiceRole] = "service";
    roles[SsidRole] = "ssid";
    roles[StateRole] = "sstate";
    roles[StrengthRole] = "strength";

    return roles;
}

QModelIndex WifiNetworkModel::indexOf(WifiNetwork *network)
{
    int row = m_networks.indexOf(network);

    return index(row);
}

WifiNetwork *WifiNetworkModel::getNetwork(QString service)
{
    for (auto network : m_networks) {
        if (network->service() == service)
            return network;
    }

    return nullptr;
}

void WifiNetworkModel::updateProperties(QString service, QJsonObject properties)
{
    WifiNetwork *network;

    // FIXME: add role parameter to emits
    if ((network = getNetwork(service))) {
        if (properties.contains("ipv4")) {
            QString address = properties.value("ipv4").toObject().value("address").toString();
            network->setAddress(address);
            emit dataChanged(indexOf(network), indexOf(network));
        }
        if (properties.contains("state")) {
            network->setState(properties.value("state").toString());
            emit dataChanged(indexOf(network), indexOf(network));
        }
        if (properties.contains("strength")) {
            network->setStrength(properties.value("strength").toInt());
            emit dataChanged(indexOf(network), indexOf(network));
            if ((network->state() == "ready") ||
                (network->state() == "online"))
                    emit strengthChanged(network->strength());
        }
    }
}