// Qt // Project #include "VNetworkModel.h" #include "WifiInterface.h" #include "Logger.h" using namespace View; /*! * \brief VNetworkModel::initConnections * Makes the necessary connections. Called inside VIEW_DEF_CLASS */ void VNetworkModel::initConnections() { // incoming connect(&_WifiInterface, SIGNAL(didAddNetwork(const Network)), this, SLOT(doAddNetwork(const Network))); connect(&_WifiInterface, SIGNAL(didScanStatusChanged(const bool)), this, SLOT(onScanStatusChanged(const bool))); // outgoing connect(this, SIGNAL(didScan()), &_WifiInterface, SLOT(doScan())); // outgoing connect(this, SIGNAL(didJoinNetwork(const Network, const QString)), &_WifiInterface, SLOT(doJoinNetwork(const Network, const QString))); } /*! * \brief VNetworkModel::addNetwork * Adds a network to the network model * \param network (Network) - the network to add to the model */ void VNetworkModel::addNetwork(const Network &vNetwork) { LOG_DEBUG(QString("Adding network with SSID: %1").arg(vNetwork.ssid())); beginInsertRows(QModelIndex(), rowCount(), rowCount()); _networks << vNetwork; endInsertRows(); } /*! * \brief VNetworkModel::rowCount * Gets the number of networks * \param parent - (QModelIndex) the parent QModelIndex * \return (int) - the number of networks */ int VNetworkModel::rowCount(const QModelIndex & parent) const { Q_UNUSED(parent); return _networks.count(); } /*! * \brief VNetworkModel::data * Returns the network properties at the specified index * \param index (QModelIndex) contains the row of data to lookup * \param role - (int) the property index to return. See NetworkDataRole * \return (QVariant) - the value for the specified network property */ QVariant VNetworkModel::data(const QModelIndex & index, int role) const { if (index.row() < 0 || index.row() >= _networks.count()) return QVariant(); const Network &network = _networks[index.row()]; switch (role) { case MacAddressRole: return network.macAddress(); case SSIDRole: return network.ssid(); case SecurityLevelRole: return network.security(); case StatusRole: return network.status(); case SignalLevelRole: return network.signalLevel(); } return QVariant(); } /*! * \brief VNetworkModel::roleNames * Translates how to access specific properties of the data for QML from the NetworkDataRole enum * \return (QHash) - maps enums to property names */ QHash VNetworkModel::roleNames() const { QHash roles; roles[MacAddressRole] = "macAddress"; roles[SSIDRole] = "ssid"; roles[SecurityLevelRole] = "securityLevel"; roles[StatusRole] = "status"; roles[SignalLevelRole] = "signalLevel"; return roles; } /*! * \brief VNetworkModel::doScan * Handles when a user clicks the Scan button */ void VNetworkModel::doScan() { status(tr("Scanning...")); emit didScan(); } /*! * \brief VNetworkModel::onAddNetwork * Slot that receives a request to add a new network * \param vNetwork - (Network) the new network to add */ void VNetworkModel::doAddNetwork(const Network &vNetwork) { if (!_networks.contains(vNetwork)) { qDebug() << QString("Adding network with SSID: %1.").arg(vNetwork.ssid()); LOG_DEBUG(QString("Adding network with SSID: %1.").arg(vNetwork.ssid())); addNetwork(vNetwork); } else { LOG_DEBUG(QString("Skipping adding network with SSID: %1. It already exists").arg(vNetwork.ssid())); } } /*! * \brief VNetworkModel::onScanStatusChanged * Called when the scan status changes. Updates QML with the current state * \param vScanning - (bool) true if scanning, false otherwise */ void VNetworkModel::onScanStatusChanged(const bool &vScanning) { if (!vScanning) status(tr("Scan Finished")); scanInProgress(vScanning); } /*! * \brief VNetworkModel::doCheckIfConnected * Checks whether the network specified by the mac address is connected or not * \param vMacAddress - (QString) the unique mac address for the network */ bool VNetworkModel::doCheckIfConnected(const QString &vMacAddress) { for (const Network &network : _networks) { if (network.macAddress() == vMacAddress && network.status() == Network::CONNECTED) return true; } return false; } /*! * \brief VNetworkModel::doJoinNetwork * Handles request from qml to join a network * \param vMacAddress - (QString) the mac address of the network to join * \param vPassword - (QString) the password for the network provided by the user */ void VNetworkModel::doJoinNetwork(const QString &vMacAddress, const QString &vPassword) { for (const Network &network : _networks) { if (network.macAddress() == vMacAddress) { status(tr("Connecting to %1...").arg(network.ssid())); emit didJoinNetwork(network, vPassword); return; } } }