working connecting and entering password
This commit is contained in:
@@ -10,6 +10,7 @@ Window {
|
|||||||
title: "WIFI Picker"
|
title: "WIFI Picker"
|
||||||
|
|
||||||
signal scanNetworks()
|
signal scanNetworks()
|
||||||
|
signal connectToNetwork(networkName: string, password: string)
|
||||||
|
|
||||||
ColumnLayout {
|
ColumnLayout {
|
||||||
id: layout
|
id: layout
|
||||||
@@ -27,9 +28,17 @@ Window {
|
|||||||
|
|
||||||
Text {
|
Text {
|
||||||
text: "Loading"
|
text: "Loading"
|
||||||
|
width: 100
|
||||||
visible: layout.isLoading
|
visible: layout.isLoading
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TextInput {
|
||||||
|
id: passwordInput
|
||||||
|
text: "enter password"
|
||||||
|
selectionColor: "blue"
|
||||||
|
width: 500
|
||||||
|
}
|
||||||
|
|
||||||
ListView {
|
ListView {
|
||||||
id: listview
|
id: listview
|
||||||
visible: !layout.isLoading
|
visible: !layout.isLoading
|
||||||
@@ -75,7 +84,10 @@ Window {
|
|||||||
Button {
|
Button {
|
||||||
visible: !listItem.isConnected && listItem.hovered
|
visible: !listItem.isConnected && listItem.hovered
|
||||||
text: "Connect"
|
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 <QtQuick/qquickview.h>
|
||||||
#include <networksmodel.h>
|
#include <networksmodel.h>
|
||||||
#include <QQmlContext>
|
#include <QQmlContext>
|
||||||
|
#include <QString>
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
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(scanNetworks()), &model, SLOT(scan()));
|
||||||
|
QObject::connect(rootObject, SIGNAL(connectToNetwork(QString, QString)), &model, SLOT(connectToNetwork(QString, QString)));
|
||||||
|
|
||||||
return app.exec();
|
return app.exec();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,3 +48,25 @@ QList<Network> NetworkManager::scanForNetworks()
|
|||||||
|
|
||||||
return networks;
|
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 <QObject>
|
||||||
#include <QAbstractListModel>
|
#include <QAbstractListModel>
|
||||||
#include <network.h>
|
#include <network.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
class NetworkManager : public QObject
|
class NetworkManager : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
QList<Network> scanForNetworks();
|
QList<Network> scanForNetworks();
|
||||||
|
bool isWifiEnabled();
|
||||||
|
bool connect(std::string ssid, std::string password);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // NETWORKMANAGER_H
|
#endif // NETWORKMANAGER_H
|
||||||
|
|||||||
@@ -48,3 +48,8 @@ QHash<int, QByteArray> NetworksModel::roleNames() const
|
|||||||
roles[NetworksModel::NetworkRoles::NameRole] = "name";
|
roles[NetworksModel::NetworkRoles::NameRole] = "name";
|
||||||
return roles;
|
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:
|
public slots:
|
||||||
void scan();
|
void scan();
|
||||||
|
void connectToNetwork(const QString &ssid, const QString &password);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
QHash<int, QByteArray> roleNames() const;
|
QHash<int, QByteArray> roleNames() const;
|
||||||
|
|||||||
Reference in New Issue
Block a user