working connecting and entering password
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ public:
|
||||
|
||||
public slots:
|
||||
void scan();
|
||||
void connectToNetwork(const QString &ssid, const QString &password);
|
||||
|
||||
protected:
|
||||
QHash<int, QByteArray> roleNames() const;
|
||||
|
||||
Reference in New Issue
Block a user