Index: sources/device/DeviceView.cpp =================================================================== diff -u -ra5760947d3ed0d2748ba023a1c25e3c6aa0b1de1 -r71cd3700a59ff4b0e363aae8e3487c559c0a0eec --- sources/device/DeviceView.cpp (.../DeviceView.cpp) (revision a5760947d3ed0d2748ba023a1c25e3c6aa0b1de1) +++ sources/device/DeviceView.cpp (.../DeviceView.cpp) (revision 71cd3700a59ff4b0e363aae8e3487c559c0a0eec) @@ -352,8 +352,8 @@ // the Freq, Rate units QString mSSID; -const QString mFREQ_Unit = " MHz"; -const QString mRATE_Unit = " Mbit/s"; + const QString mFREQ_Unit = " MHz"; + const QString mRATE_Unit = " Mbit/s"; // Removing the units once from the result to accelerate parsing. (will be added when done) mResult.remove(mFREQ_Unit); mResult.remove(mRATE_Unit); @@ -364,7 +364,7 @@ QString mRSN_FLAGS; QStringList fields = lines[row].split(','); - qDebug() << fields.join("-"); + // DEBUG: qDebug() << fields.join("-"); // this will never fail since even an empty string in split at least has index 0=eSSID; mSSID = fields[eSSID].trimmed(); if ( mSSID.isEmpty() ) continue; //hidden networks, or an incorrect entry @@ -467,9 +467,160 @@ case eRole_WifiSignalLevel : return dataList.mWifiSignalLevel ; case eRole_WifiSupported : return dataList.mWifiSupported ; case eRole_WifiConnected : return dataList.mWifiConnected ; + // ----- Bluetooth case eRole_BLE_UNUSED : return ""; ; } return QString("Wifi %1").arg(vIndex.row()); } + +bool View::VDevice::setData(const QModelIndex &vIndex, const QVariant& vValue, int vRole) { + if (! vIndex.isValid() || vIndex.row() >= _dataList.count()) { + return false; + } + + DataModel &dataItem = _dataList[vIndex.row()]; + + switch (vRole) { + case eRole_WifiConnected: { + if (dataItem.mWifiConnected != vValue.toBool()) { + dataItem.mWifiConnected = vValue.toBool(); + } + break; + } + default: + return false; + } + + // explicitly emit a dataChanged signal to notify anybody bound to this property (vRole) + emit dataChanged(vIndex, vIndex, QVector(1, vRole)); + return true; +} + +QModelIndex View::VDevice::index(int vRow, int vColumn, const QModelIndex &vParent) const { + return hasIndex(vRow, vColumn, vParent) + ? createIndex(vRow, vColumn) + : QModelIndex(); +} + +// ================================================= WifiInfo +void VDevice::doInitWifiInfo() { + wifiInfoRequest({}); +} + +void VDevice::wifiInfoRequest(const QStringList &) { + DeviceWifiInfoRequestData data; + wifiInfo({}); + emit didAttributeRequest(data); +} + +void VDevice::onAttributeResponse(const DeviceWifiInfoResponseData &vData) +{ + if ( vData.mCompleted ) { + if ( vData.mAccepted ) { + parseWifiInfoResult(vData.mWifiInfo); + } + else { + wifiInfo({}); + } + } + + accepted(vData.mAccepted); + reason (vData.mReason ); + status (vData.mMessage ); + + // has to be the last one + response(true); +} + +/*! + * \brief Network::parseWifiInfoResult + * \details Extract desired information from the WiFi information output. + * \param vResult - (QString) output collected from QProcess execution + */ +void View::VDevice::parseWifiInfoResult(const QString &vResult) +{ + enum WifiInfo_Enum { + eSSID , + eIPADDRESS , + eSUBNETMASK , + eGATEWAY , + eDNS , + eCount , + }; + + QString mResult = vResult; + QStringList fields = mResult.split(','); + + if (fields.size() < eCount) { + status (tr("The WiFi info response error")); + LOG_DEBUG(QStringLiteral("The WiFi info response length is not correct [%1 of %2]").arg(fields.size()).arg(eCount)); + return; + } + + if (! fields[eSSID].trimmed().isEmpty()) { + ssid(fields[eSSID].trimmed()); + ipAddress(fields[eIPADDRESS].trimmed()); + subnetMask(fields[eSUBNETMASK].trimmed()); + gateway(fields[eGATEWAY].trimmed()); + QStringList dnsList = fields.mid(eDNS); + dns(dnsList.join('\n')); + } + else { + ssid(""); + ipAddress(""); + gateway(""); + subnetMask(""); + dns(""); + } + + updateWifiList(); +} + +void VDevice::updateWifiList() { + for (int row = 0; row < rowCount(); row++) { + setData(index(row,0), _dataList[row].mWifiSSID == ssid(), eRole_WifiConnected); + } +} + +// ================================================= WifiConnect +void VDevice::doInitWifiConnect() { + // Nothing for now. +} + +void VDevice::wifiConnectRequest(const bool &) { + // Nothing for now. +} + +/*! + * \brief Network::doWifiConnect + * \details Method to connect/disconnect to desired Wi-Fi with SSID and password + * \param vConnect - (bool) determine if user wants to connect or disconnect Wi-Fi + * \param vSsid - (QString) SSID to connect or disconnect + * \param vPassword - (QString) SSID password + */ +void VDevice::doWifiConnect(bool vConnect, const QString &vSsid, const QString &vPassword) { + DeviceWifiConnectRequestData data; + data.mConnect = vConnect; + data.mSsid = vSsid; + data.mPassword = vPassword; + + emit didAttributeRequest(data); +} + +void VDevice::onAttributeResponse(const DeviceWifiConnectResponseData &vData) { + if ( vData.mCompleted ) { + if ( vData.mAccepted ) { + wifiInfoRequest({}); + } + } + + status (vData.mMessage ); + accepted(vData.mAccepted); + reason (vData.mReason ); + + // has to be the last one + response(true); +} +