working connecting and entering password

This commit is contained in:
talksik
2024-02-09 12:50:06 -08:00
parent edefbf5b13
commit 13bf908ab9
6 changed files with 46 additions and 1 deletions
+13 -1
View File
@@ -10,6 +10,7 @@ Window {
title: "WIFI Picker"
signal scanNetworks()
signal connectToNetwork(networkName: string, password: string)
ColumnLayout {
id: layout
@@ -27,9 +28,17 @@ Window {
Text {
text: "Loading"
width: 100
visible: layout.isLoading
}
TextInput {
id: passwordInput
text: "enter password"
selectionColor: "blue"
width: 500
}
ListView {
id: listview
visible: !layout.isLoading
@@ -75,7 +84,10 @@ Window {
Button {
visible: !listItem.isConnected && listItem.hovered
text: "Connect"
onClicked: console.log("connecting to " + listItem.name)
onClicked: {
console.log("connecting to " + listItem.name)
window.connectToNetwork(listItem.name, passwordInput.text)
}
}
}
}
+2
View File
@@ -5,6 +5,7 @@
#include <QtQuick/qquickview.h>
#include <networksmodel.h>
#include <QQmlContext>
#include <QString>
int main(int argc, char *argv[])
{
@@ -31,6 +32,7 @@ int main(int argc, char *argv[])
}
QObject::connect(rootObject, SIGNAL(scanNetworks()), &model, SLOT(scan()));
QObject::connect(rootObject, SIGNAL(connectToNetwork(QString, QString)), &model, SLOT(connectToNetwork(QString, QString)));
return app.exec();
}
+22
View File
@@ -48,3 +48,25 @@ QList<Network> NetworkManager::scanForNetworks()
return networks;
}
bool NetworkManager::isWifiEnabled()
{
std::string result = exec("nmcli radio wifi");
if (result.find("enabled") != std::string::npos)
{
return true;
}
return false;
}
bool NetworkManager::connect(std::string ssid, std::string password)
{
try {
std::string command = "nmcli device wifi connect \"" + ssid + "\" password \"" + password + "\"";
std::string result = exec(command.c_str());
return true;
} catch(...) {
return false;
}
}
+3
View File
@@ -4,12 +4,15 @@
#include <QObject>
#include <QAbstractListModel>
#include <network.h>
#include <string>
class NetworkManager : public QObject
{
Q_OBJECT
public:
QList<Network> scanForNetworks();
bool isWifiEnabled();
bool connect(std::string ssid, std::string password);
};
#endif // NETWORKMANAGER_H
+5
View File
@@ -48,3 +48,8 @@ QHash<int, QByteArray> NetworksModel::roleNames() const
roles[NetworksModel::NetworkRoles::NameRole] = "name";
return roles;
}
void NetworksModel::connectToNetwork(const QString &ssid, const QString &password)
{
bool success = this->m_networkmanager.connect(ssid.toStdString(), password.toStdString());
}
+1
View File
@@ -19,6 +19,7 @@ public:
public slots:
void scan();
void connectToNetwork(const QString &ssid, const QString &password);
protected:
QHash<int, QByteArray> roleNames() const;