summaryrefslogtreecommitdiffstats
path: root/network
diff options
context:
space:
mode:
Diffstat (limited to 'network')
-rw-r--r--network/CMakeLists.txt4
-rw-r--r--network/wifinetworkmodel.cpp168
-rw-r--r--network/wifinetworkmodel.h68
3 files changed, 238 insertions, 2 deletions
diff --git a/network/CMakeLists.txt b/network/CMakeLists.txt
index 519d28f..e3d4812 100644
--- a/network/CMakeLists.txt
+++ b/network/CMakeLists.txt
@@ -1,2 +1,2 @@
-add_headers(networkmessage.h)
-add_sources(networkmessage.cpp)
+add_headers(networkmessage.h wifinetworkmodel.h)
+add_sources(networkmessage.cpp wifinetworkmodel.cpp)
diff --git a/network/wifinetworkmodel.cpp b/network/wifinetworkmodel.cpp
new file mode 100644
index 0000000..8a83919
--- /dev/null
+++ b/network/wifinetworkmodel.cpp
@@ -0,0 +1,168 @@
+#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());
+ 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));
+ }
+ }
+}
diff --git a/network/wifinetworkmodel.h b/network/wifinetworkmodel.h
new file mode 100644
index 0000000..27e635a
--- /dev/null
+++ b/network/wifinetworkmodel.h
@@ -0,0 +1,68 @@
+#ifndef WIFI_NETWORK_MODEL_H
+#define WIFI_NETWORK_MODEL_H
+
+#include <QAbstractListModel>
+#include <QStringList>
+#include <QtQml/QQmlContext>
+#include <QJsonObject>
+
+class WifiNetwork
+{
+ public:
+ WifiNetwork(const QString &address,
+ const QString &security,
+ const QString &service,
+ const QString &ssid,
+ const QString &state,
+ const int &strength);
+ QString address() const;
+ QString service() const;
+ QString ssid() const;
+ QString security() const;
+ QString state() const;
+ int strength() const;
+ void setAddress(const QString address);
+ void setState(const QString state);
+ void setStrength(const int strength);
+
+ private:
+ QString m_address;
+ QString m_security;
+ QString m_service;
+ QString m_ssid;
+ QString m_state;
+ int m_strength;
+};
+
+class WifiNetworkModel : public QAbstractListModel
+{
+ Q_OBJECT
+
+ public:
+ enum WifiNetworkRoles {
+ AddressRole = Qt::UserRole + 1,
+ SecurityRole,
+ ServiceRole,
+ SsidRole,
+ StateRole,
+ StrengthRole
+ };
+
+ WifiNetworkModel(QObject *parent = Q_NULLPTR);
+
+ void addNetwork(WifiNetwork *network);
+ void removeNetwork(WifiNetwork *network);
+ void removeAllNetworks();
+ WifiNetwork *getNetwork(QString service);
+ int rowCount(const QModelIndex &parent = QModelIndex()) const;
+ QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
+ void updateProperties(QString service, QJsonObject properties);
+
+ protected:
+ QHash<int, QByteArray> roleNames() const;
+
+ private:
+ QList<WifiNetwork *> m_networks;
+ QModelIndex indexOf(WifiNetwork *network);
+};
+#endif // WIFI_NETWORK_MODEL_H