decently working showing available network interfaces

This commit is contained in:
talksik
2024-02-08 16:42:33 -08:00
parent dc74c0ede7
commit 4324abd5ea
+32
View File
@@ -1,6 +1,23 @@
#include "networkscanner.h"
#include <iostream>
#include <QAbstractListModel>
#include <iwlib.h>
#include <string>
#include <vector>
#include <sstream>
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> NetworkScanner::scanForNetworks()
{
@@ -11,5 +28,20 @@ QList<Network> NetworkScanner::scanForNetworks()
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)) {
ssids.push_back(line);
}
for (const std::string &ssid : ssids) {
std::cout << ssid << std::endl;
Network newNetwork = Network(ssid.c_str());
networks.append(newNetwork);
}
return networks;
}