having a better abstraction for network stuff as manager for other methods

This commit is contained in:
talksik
2024-02-09 12:14:04 -08:00
parent 68596b75c5
commit edefbf5b13
6 changed files with 23 additions and 31 deletions
+50
View File
@@ -0,0 +1,50 @@
#include "networkmanager.h"
#include <iostream>
#include <QAbstractListModel>
#include <iwlib.h>
#include <string>
#include <vector>
#include <sstream>
#include <network.h>
std::string exec(const char* cmd) {
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
if (!pipe) {
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}
QList<Network> NetworkManager::scanForNetworks()
{
std::cout << "Called the C++ Network scanner class" << std::endl;
QList<Network> networks;
Network example1 = Network("Gita 2.4Ghz");
networks.append(example1);
std::string result = exec("nmcli -t -f ssid dev wifi");
std::vector<std::string> ssids;
std::istringstream iss(result);
std::string line;
while (std::getline(iss, line)) {
if (!line.empty() && line != "O.o")
{
ssids.push_back(line);
}
}
for (const std::string &ssid : ssids) {
Network newNetwork = Network(ssid.c_str());
networks.append(newNetwork);
}
return networks;
}