From d4325e4c9c74e1c9f8bf6379a34b99123fd8fb04 Mon Sep 17 00:00:00 2001 From: PedroHLC Date: Sat, 5 Oct 2019 11:50:34 -0300 Subject: [PATCH] Finished network details --- qml/MainContainer.qml | 1 + qml/NetworkUp.qml | 11 +++++- qml/WifiMenu.qml | 49 +++++++++++++++++++++-- src/lib/network.cpp | 90 +++++++++++++++++++++++++++++++++++++++++-- src/lib/network.hpp | 36 +++++++++++++---- 5 files changed, 173 insertions(+), 14 deletions(-) diff --git a/qml/MainContainer.qml b/qml/MainContainer.qml index 15fc282..48efb56 100644 --- a/qml/MainContainer.qml +++ b/qml/MainContainer.qml @@ -79,4 +79,5 @@ Rectangle { KeyboardPicker { id: keyboardPicker } NetworkUp { id: networkUp } WifiMenu { id: wifiMenu } + EthernetMenu { id: ethMenu } } \ No newline at end of file diff --git a/qml/NetworkUp.qml b/qml/NetworkUp.qml index 23d4152..ae7187a 100644 --- a/qml/NetworkUp.qml +++ b/qml/NetworkUp.qml @@ -51,9 +51,18 @@ Component { anchors.horizontalCenter: parent.horizontalCenter Button { - text: qsTr('Configure Internet Address') + text: qsTr('Configure Ethernet') + onClicked: contentStack.push(ethMenu) } } + + Row { + anchors.horizontalCenter: parent.horizontalCenter + + Text { + text: qsTr('* In both options you\'ll find a form that will be used for generating a netctl profile,\nthis profile can be manually edited before starting it.') + } + } } } diff --git a/qml/WifiMenu.qml b/qml/WifiMenu.qml index d6065a4..a8497e7 100644 --- a/qml/WifiMenu.qml +++ b/qml/WifiMenu.qml @@ -19,7 +19,7 @@ Component { apPicker.currentIndex = 0; } - onWifiCardsChanged: { + onCardsChanged: { wifiIfacePicker.currentIndex = 0; } } @@ -78,7 +78,46 @@ Component { TextField { placeholderText: qsTr('WPA2 personal password only') passwordCharacter: "*" - echoMode: TextInput.Password + echoMode: TextInput.PasswordEchoOnEdit + onEditingFinished: net.setWifiPassword(text) + text: net.wifiPassword + } + } + } + + Row { + Button { + text: qsTr('Generate profile') + onClicked: net.genWifiProfile(); + } + } + + Row { + id: wifiProfileTextView + width: 480 + height: 90 + ScrollView { + anchors.top: parent.top + width: parent.width + height: parent.height + clip: true + + contentWidth: wifiProfileText.width + contentHeight: wifiProfileText.height + + ScrollBar.vertical: ScrollBar { + policy: ScrollBar.AlwaysOn; + parent: wifiProfileTextView + anchors.top: parent.top + anchors.right: parent.right + anchors.bottom: parent.bottom + } + + TextArea { + id: wifiProfileText + text: net.wifiProfile + width: parent.width + onEditingFinished: net.setWifiProfile(text) } } } @@ -103,8 +142,12 @@ Component { Column { Button { - text: qsTr('View script && Apply') + text: qsTr('Apply') highlighted: true + onClicked: { + net.applyWifiProfile(); + contentStack.pop(); + } } } } diff --git a/src/lib/network.cpp b/src/lib/network.cpp index dc72362..41e01b5 100644 --- a/src/lib/network.cpp +++ b/src/lib/network.cpp @@ -9,7 +9,7 @@ Network::Network(QObject *parent) : QObject (parent) { } bool Network::getAvailability() { - return false; + return QProcess::execute("wget -q --spider http://google.com") == 0; } bool Network::getWifiCardsExists() { @@ -32,6 +32,18 @@ QString Network::getWifiIface() { return wifiIface; } +QString Network::getWifiProfile() { + return wifiProfile; +} + +QString Network::getEthProfile() { + return ethProfile; +} + +QString Network::getWifiPassword() { + return wifiPsw; +} + QDir Network::interfacesDir = QDir(QStringLiteral("/sys/class/net")); void Network::loadCards() { @@ -52,8 +64,11 @@ void Network::loadCards() { if(wifiCardsCache.size() >= 1) setWifiIface(0); + + if(cardsCache.size() >= 1) + setEthIface(0); - emit wifiCardsChanged(); + emit cardsChanged(); } void Network::scanWifi() { @@ -89,4 +104,73 @@ void Network::setWifiIface(int iface_i) { void Network::setWifiAP(int ap_i) { wifiSSID = wifiAPs.at(ap_i); qDebug() << "User selected " << wifiSSID; -} \ No newline at end of file +} + +void Network::setWifiPassword(QString psw) { + wifiPsw = psw; + qDebug() << QString("%1 characters password was entered.").arg(psw.size()); + + emit wifiPasswordChanged(); +} + +void Network::setWifiProfile(QString profile) { + wifiProfile = profile; + + emit wifiProfileChanged(); +} + +void Network::genWifiProfile() { + wifiProfile = QString("Interface=%1\n" + "Connection=wireless\n" + "Security=wpa\n" + "IP=dhcp\n" + "ESSID='%2'\n" + "Key=\"%3\"\n" + ).arg(wifiIface).arg(wifiSSID).arg(wifiPsw); + + emit wifiProfileChanged(); +} + +void Network::applyWifiProfile() { + if (wifiProfile.size() < 1) + genWifiProfile(); + applyProfile(wifiProfile); +} + +void Network::applyProfile(QString profileData) { + QProcess::execute(QStringLiteral("netctl stop-all")); + + QFile proFile("/etc/netctl/chaotic-setup"); + proFile.write(profileData.toUtf8()); + proFile.close(); + + QProcess::execute(QStringLiteral("netctl start chaotic-setup")); + + emit availabilityChanged(); +} + +void Network::setEthIface(int iface_i) { + ethIface = cardsCache.at(iface_i); +} + +void Network::setEthProfile(QString profile) { + ethProfile = profile; + + emit ethProfileChanged(); +} + +void Network::genEthProfile() { + ethProfile = QString("Interface=%1\n" + "Connection=ethernet\n" + "IP=dhcp\n" + ).arg(ethIface); + + emit ethProfileChanged(); +} + +void Network::applyEthProfile() { + if (ethProfile.size() < 1) + genEthProfile(); + applyProfile(ethProfile); +} + diff --git a/src/lib/network.hpp b/src/lib/network.hpp index a5b4c0d..5371cde 100644 --- a/src/lib/network.hpp +++ b/src/lib/network.hpp @@ -7,11 +7,13 @@ class Network : public QObject { Q_OBJECT Q_PROPERTY(bool available READ getAvailability NOTIFY availabilityChanged) - Q_PROPERTY(bool wifiCardExists READ getWifiCardsExists NOTIFY wifiCardsChanged) - Q_PROPERTY(QStringList wifiCards READ getWifiCards NOTIFY wifiCardsChanged) + Q_PROPERTY(bool wifiCardExists READ getWifiCardsExists NOTIFY cardsChanged) + Q_PROPERTY(QStringList cards READ getCards NOTIFY cardsChanged) + Q_PROPERTY(QStringList wifiCards READ getWifiCards NOTIFY cardsChanged) Q_PROPERTY(QStringList wifiAPs READ getWifiAPs NOTIFY wifiAPsChanged) - //Q_PROPERTY(QString selectedWifiIface READ getWifiIface WRITE setWifiIface NOTIFY wifiCardsChanged) - //Q_PROPERTY(QString selectedWifiAP READ getWifiSSID WRITE setWifiAP NOTIFY wifiAPsChanged) + Q_PROPERTY(QString wifiPassword READ getWifiPassword WRITE setWifiPassword NOTIFY wifiPasswordChanged) + Q_PROPERTY(QString wifiProfile READ getWifiProfile WRITE setWifiProfile NOTIFY wifiProfileChanged) + Q_PROPERTY(QString ethProfile READ getEthProfile WRITE setEthProfile NOTIFY ethProfileChanged) private: static QDir interfacesDir; @@ -20,6 +22,12 @@ class Network : public QObject { QStringList wifiAPs; QString wifiIface; QString wifiSSID; + QString wifiPsw; + QString wifiProfile; + QString ethIface; + QString ethProfile; + + void applyProfile(QString); public: explicit Network(QObject *parent = nullptr); @@ -31,17 +39,31 @@ class Network : public QObject { QStringList getWifiCards(); QStringList getCards(); QString getWifiIface(); + QString getWifiPassword(); + QString getWifiProfile(); + QString getEthIface(); + QString getEthProfile(); - void loadCards(); - + Q_INVOKABLE void loadCards(); Q_INVOKABLE void scanWifi(); Q_INVOKABLE void setWifiAP(int ap_i); Q_INVOKABLE void setWifiIface(int iface_i); + Q_INVOKABLE void setWifiPassword(QString password); + Q_INVOKABLE void setWifiProfile(QString script); + Q_INVOKABLE void genWifiProfile(); + Q_INVOKABLE void applyWifiProfile(); + Q_INVOKABLE void setEthIface(int iface_i); + Q_INVOKABLE void setEthProfile(QString script); + Q_INVOKABLE void genEthProfile(); + Q_INVOKABLE void applyEthProfile(); signals: void availabilityChanged(); + void cardsChanged(); void wifiAPsChanged(); - void wifiCardsChanged(); + void wifiPasswordChanged(); + void wifiProfileChanged(); + void ethProfileChanged(); }; #endif // NETWORK_H \ No newline at end of file