From 4600aae8bc1463144494696c8a6127f9de711d46 Mon Sep 17 00:00:00 2001 From: arcan1s Date: Mon, 21 Jul 2014 10:25:10 +0400 Subject: [PATCH] add netctlgui.h rewrited some functions --- PKGBUILD | 4 +- sources/gui/src/mainwindow.cpp | 8 +- sources/gui/src/netctlautowindow.cpp | 2 +- .../netctlgui/include/netctlgui/netctlgui.h | 35 ++++++ .../include/netctlgui/netctlinteract.h | 18 ++- .../include/netctlgui/netctlprofile.h | 7 ++ .../include/netctlgui/wpasupinteract.h | 20 +--- sources/netctlgui/src/netctlinteract.cpp | 113 +++++++----------- sources/netctlgui/src/netctlprofile.cpp | 20 +++- sources/netctlgui/src/wpasupinteract.cpp | 65 +++------- 10 files changed, 138 insertions(+), 154 deletions(-) create mode 100644 sources/netctlgui/include/netctlgui/netctlgui.h diff --git a/PKGBUILD b/PKGBUILD index b4a1bc8..53512e9 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -15,7 +15,7 @@ optdepends=('kdebase-runtime: sudo support' 'wpa_supplicant: wifi support') source=("https://github.com/arcan1s/netctl-gui/releases/download/V.${pkgver}/${pkgbase}-${pkgver}-src.tar.xz") install="${pkgbase}.install" -md5sums=('29542e7bd1978e5bae0a3523e59e533a') +md5sums=('45eebb959b9a760240e5b7db5bdfbf48') prepare() { @@ -56,7 +56,7 @@ build() { package_libnetctlgui() { - pkgdesc="Qt library interacts with netctl. A part of netctl-gui" + pkgdesc="Qt5 library which interacts with netctl. A part of netctl-gui" depends=('netctl' 'qt5-base') cd "${srcdir}/build-qt5/netctlgui" diff --git a/sources/gui/src/mainwindow.cpp b/sources/gui/src/mainwindow.cpp index 0743473..f13c4c7 100644 --- a/sources/gui/src/mainwindow.cpp +++ b/sources/gui/src/mainwindow.cpp @@ -23,9 +23,7 @@ #include #include -#include -#include -#include +#include #include "aboutwindow.h" #include "bridgewidget.h" @@ -1256,10 +1254,12 @@ void MainWindow::connectToUnknownEssid(const QString passwd) if (passwdWid != 0) delete passwdWid; + if (netctlCommand->getInterfaceList().isEmpty()) + return; QMap settings; settings[QString("Description")] = QString("'Automatically generated profile by Netctl GUI'"); - settings[QString("Interface")] = wpaCommand->getInterfaceList()[0]; + settings[QString("Interface")] = netctlCommand->getInterfaceList()[0]; settings[QString("Connection")] = QString("wireless"); QString security = ui->tableWidget_wifi->item(ui->tableWidget_wifi->currentItem()->row(), 3)->text(); if (checkState(QString("WPA"), security)) diff --git a/sources/gui/src/netctlautowindow.cpp b/sources/gui/src/netctlautowindow.cpp index 8ab777b..faf5c89 100644 --- a/sources/gui/src/netctlautowindow.cpp +++ b/sources/gui/src/netctlautowindow.cpp @@ -20,7 +20,7 @@ #include -#include +#include NetctlAutoWindow::NetctlAutoWindow(QWidget *parent, const bool debugCmd, const QMap settings) diff --git a/sources/netctlgui/include/netctlgui/netctlgui.h b/sources/netctlgui/include/netctlgui/netctlgui.h new file mode 100644 index 0000000..de2d69b --- /dev/null +++ b/sources/netctlgui/include/netctlgui/netctlgui.h @@ -0,0 +1,35 @@ +/*************************************************************************** + * This file is part of netctl-gui * + * * + * netctl-gui is free software: you can redistribute it and/or * + * modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * netctl-gui is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with netctl-gui. If not, see http://www.gnu.org/licenses/ * + ***************************************************************************/ +/** + * @file netctlgui.h + * Header of netctlgui library + * @author Evgeniy Alekseev + * @copyright GPLv3 + * @bug https://github.com/arcan1s/netctl-gui/issues + */ + + +#ifndef NETCTLGUI_H +#define NETCTLGUI_H + +#include "netctlinteract.h" +#include "netctlprofile.h" +#include "sleepthread.h" +#include "wpasupinteract.h" + + +#endif /* NETCTLGUI_H */ diff --git a/sources/netctlgui/include/netctlgui/netctlinteract.h b/sources/netctlgui/include/netctlgui/netctlinteract.h index 1b6338e..369b42a 100644 --- a/sources/netctlgui/include/netctlgui/netctlinteract.h +++ b/sources/netctlgui/include/netctlgui/netctlinteract.h @@ -31,6 +31,8 @@ #include +class WpaSup; + /** * @brief The Netctl class interacts with netctl */ @@ -59,6 +61,11 @@ public: */ ~Netctl(); // general information + /** + * @brief method which gets interface list from PREFERED_IFACE and IFACE_DIR + * @return interface list. If PREFERED_IFACE is not empty it will be first element + */ + QStringList getInterfaceList(); /** * @brief method which returns profile informations from netctl * @return list of profiles. Available information is [NAME, DESCRIPTION, STATUS]: @@ -101,12 +108,6 @@ public: * "inactive (enabled)", "inactive (static)" */ QStringList getProfileStatuses(const QStringList profileList); - /** - * @brief method which gets ESSID from profile - * @param profile profile name - * @return ESSID name or null string - */ - QString getSsidFromProfile(const QString profile); /** * @brief method which checks if profile is active * @param profile profile name @@ -265,11 +266,6 @@ private: * @return netctl output */ QString getNetctlOutput(const bool sudo, const QString commandLine, const QString profile = 0); - /** - * @brief method which gets interface list from PREFERED_IFACE and IFACE_DIR - * @return first element from interface list. If PREFERED_IFACE is not empty it will be first element - */ - QString getWifiInterface(); /** * @brief method which calls netctl * @param sudo set true if sudo is needed diff --git a/sources/netctlgui/include/netctlgui/netctlprofile.h b/sources/netctlgui/include/netctlgui/netctlprofile.h index 4ce4c65..3a703ba 100644 --- a/sources/netctlgui/include/netctlgui/netctlprofile.h +++ b/sources/netctlgui/include/netctlgui/netctlprofile.h @@ -72,6 +72,13 @@ public: * @return settings from profile */ QMap getSettingsFromProfile(const QString profile); + /** + * @brief method which return value from profile by key + * @param profile profile name + * @param key required key + * @return value by key + */ + QString getValueFromProfile(const QString profile, const QString key); /** * @brief method which removes profile * @param profile profile name diff --git a/sources/netctlgui/include/netctlgui/wpasupinteract.h b/sources/netctlgui/include/netctlgui/wpasupinteract.h index 2a37b25..68bd5e6 100644 --- a/sources/netctlgui/include/netctlgui/wpasupinteract.h +++ b/sources/netctlgui/include/netctlgui/wpasupinteract.h @@ -32,6 +32,7 @@ class Netctl; +class NetctlProfile; /** * @brief The WpaSup class interacts with wpa_supplicant @@ -47,8 +48,6 @@ public: * @param settings default settings. Needed keys are * CTRL_DIR (path to ctrl_directory), * CTRL_GROUP (group which is owner of CTRL_DIR), - * IFACE_DIR (path to directory with interfaces), - * PREFERED_IFACE (prefered interface for WiFi), * PID_FILE (wpa_supplicant PID file), * SUDO_PATH (path to sudo command), * WPACLI_PATH (path to wpa_cli command), @@ -68,11 +67,6 @@ public: * @return profile name */ QString existentProfile(const QString essid); - /** - * @brief method which gets interface list from PREFERED_IFACE and IFACE_DIR - * @return interface list. If PREFERED_IFACE is not empty it will be first element - */ - QStringList getInterfaceList(); /** * @brief method which checks profile status by ESSID * @param essid ESSID name @@ -117,6 +111,10 @@ private: * @brief Netctl class */ Netctl *netctlCommand; + /** + * @brief NetctlProfile class + */ + NetctlProfile *netctlProfile; /** * @brief show debug messages */ @@ -129,14 +127,6 @@ private: * @brief group which is owner of CTRL_DIR. Default is "users" */ QString ctrlGroup; - /** - * @brief directory with interfaces. Default is "/sys/class/net/" - */ - QDir *ifaceDirectory; - /** - * @brief prefered interface for WiFi. Default is "" - */ - QString mainInterface; /** * @brief wpa_supplicant PID file. Default is "/run/wpa_supplicant_netctl-gui.pid" */ diff --git a/sources/netctlgui/src/netctlinteract.cpp b/sources/netctlgui/src/netctlinteract.cpp index 380d777..fdf58b2 100644 --- a/sources/netctlgui/src/netctlinteract.cpp +++ b/sources/netctlgui/src/netctlinteract.cpp @@ -27,7 +27,7 @@ #include #include -#include +#include /** @@ -121,36 +121,6 @@ QString Netctl::getNetctlOutput(const bool sudo, const QString commandLine, cons } -/** - * @fn getWifiInterface - */ -QString Netctl::getWifiInterface() -{ - if (debug) qDebug() << "[Netctl]" << "[getWifiInterface]"; - if (ifaceDirectory == 0) { - if (debug) qDebug() << "[Netctl]" << "[getWifiInterface]" << ":" << "Could not find directory"; - return QString(); - } - - QStringList interfaces; - if (!mainInterface.isEmpty()) - interfaces.append(mainInterface); - QStringList allInterfaces = ifaceDirectory->entryList(QDir::Dirs | QDir::NoDotAndDotDot); - for (int i=0; ipath() + QDir::separator() + allInterfaces[i] + QDir::separator() + QString("wireless"); - if (QDir(ifaceDirectory->path() + QDir::separator() + allInterfaces[i] + - QDir::separator() + QString("wireless")).exists()) - interfaces.append(allInterfaces[i]); - } - - if (interfaces.isEmpty()) - return QString(""); - else - return interfaces[0]; -} - - /** * @fn netctlCall */ @@ -243,10 +213,14 @@ bool Netctl::systemctlCall(const bool sudo, const QString commandLine) if (debug) qDebug() << "[Netctl]" << "[systemctlCall]" << ":" << "Could not find systemctl"; return false; } + if (getInterfaceList().isEmpty()) { + if (debug) qDebug() << "[Netctl]" << "[systemctlCall]" << ":" << "Could not interface"; + return false; + } QProcess command; QString commandText; - QString interface = getWifiInterface(); + QString interface = getInterfaceList()[0]; if (interface.isEmpty()) return false; if (sudo) @@ -268,6 +242,33 @@ bool Netctl::systemctlCall(const bool sudo, const QString commandLine) // general information +/** + * @fn getInterfaceList + */ +QStringList Netctl::getInterfaceList() +{ + if (debug) qDebug() << "[Netctl]" << "[getInterfaceList]"; + if (ifaceDirectory == 0) { + if (debug) qDebug() << "[Netctl]" << "[getInterfaceList]" << ":" << "Could not find directory"; + return QStringList(); + } + + QStringList interfaces; + if (!mainInterface.isEmpty()) + interfaces.append(mainInterface); + QStringList allInterfaces = ifaceDirectory->entryList(QDir::Dirs | QDir::NoDotAndDotDot); + for (int i=0; ipath() + QDir::separator() + allInterfaces[i] + QDir::separator() + QString("wireless"); + if (QDir(ifaceDirectory->path() + QDir::separator() + allInterfaces[i] + + QDir::separator() + QString("wireless")).exists()) + interfaces.append(allInterfaces[i]); + } + + return interfaces; +} + + /** * @fn getProfileList */ @@ -450,42 +451,6 @@ QStringList Netctl::getProfileStatuses(const QStringList profileList) } -/** - * @fn getSsidFromProfile - */ -QString Netctl::getSsidFromProfile(const QString profile) -{ - if (debug) qDebug() << "[Netctl]" << "[getSsidFromProfile]"; - if (debug) qDebug() << "[Netctl]" << "[getSsidFromProfile]" << ":" << "Profile" << profile; - if (profileDirectory == 0) { - if (debug) qDebug() << "[Netctl]" << "[getSsidFromProfile]" << ":" << "Could not find directory"; - return QString(); - } - - QString ssidName; - QString profileUrl = profileDirectory->absolutePath() + QDir::separator() + profile; - if (debug) qDebug() << "[Netctl]" << "[getSsidFromProfile]" << ":" << "Check" << profileUrl; - QFile profileFile(profileUrl); - QString fileStr; - if (!profileFile.open(QIODevice::ReadOnly)) - return ssidName; - while (true) { - fileStr = QString(profileFile.readLine()); - if (fileStr.isEmpty()) continue; - if (fileStr[0] == QChar('#')) continue; - if (fileStr.split(QChar('='), QString::SkipEmptyParts).count() == 2) - if (fileStr.split(QChar('='), QString::SkipEmptyParts)[0] == QString("ESSID")) - ssidName = fileStr.split(QChar('='), QString::SkipEmptyParts)[1].trimmed(); - if (profileFile.atEnd()) - break; - } - profileFile.close(); - ssidName.remove(QChar('\'')).remove(QChar('"')); - - return ssidName; -} - - /** * @fn isProfileActive */ @@ -569,9 +534,13 @@ bool Netctl::isNetctlAutoEnabled() if (debug) qDebug() << "[Netctl]" << "[isNetctlAutoEnabled]" << ":" << "Could not find systemctl"; return false; } + if (getInterfaceList().isEmpty()) { + if (debug) qDebug() << "[Netctl]" << "[isNetctlAutoEnabled]" << ":" << "Could not interface"; + return false; + } QProcess command; - QString interface = getWifiInterface(); + QString interface = getInterfaceList()[0]; QString commandText = systemctlCommand + QString(" is-enabled ") + netctlAutoService + QString("@") + interface + QString(".service"); if (debug) qDebug() << "[Netctl]" << "[isNetctlAutoEnabled]" << ":" << "Run cmd" << commandText; @@ -601,8 +570,12 @@ bool Netctl::isNetctlAutoRunning() if (debug) qDebug() << "[Netctl]" << "[isNetctlAutoRunning]" << ":" << "Could not find systemctl"; return false; } + if (getInterfaceList().isEmpty()) { + if (debug) qDebug() << "[Netctl]" << "[isNetctlAutoRunning]" << ":" << "Could not interface"; + return false; + } - QString interface = getWifiInterface(); + QString interface = getInterfaceList()[0]; QProcess command; QString commandText = systemctlCommand + QString(" is-active ") + netctlAutoService + QString("@") + interface + QString(".service"); diff --git a/sources/netctlgui/src/netctlprofile.cpp b/sources/netctlgui/src/netctlprofile.cpp index f4380df..dd2fd3b 100644 --- a/sources/netctlgui/src/netctlprofile.cpp +++ b/sources/netctlgui/src/netctlprofile.cpp @@ -29,7 +29,7 @@ #include #include -#include +#include /** @@ -186,6 +186,24 @@ QMap NetctlProfile::getSettingsFromProfile(const QString profi } +/** + * @fn ValueFromProfile + */ +QString NetctlProfile::getValueFromProfile(const QString profile, const QString key) +{ + if (debug) qDebug() << "[NetctlProfile]" << "[getValueFromProfile]"; + if (debug) qDebug() << "[NetctlProfile]" << "[getValueFromProfile]" << ":" << "Profile" << profile; + if (debug) qDebug() << "[NetctlProfile]" << "[getValueFromProfile]" << ":" << "Key" << key; + + QMap settings = getSettingsFromProfile(profile); + + if (settings.contains(key)) + return settings[key]; + else + return QString(""); +} + + /** * @fn removeProfile */ diff --git a/sources/netctlgui/src/wpasupinteract.cpp b/sources/netctlgui/src/wpasupinteract.cpp index affe1d2..9c8476c 100644 --- a/sources/netctlgui/src/wpasupinteract.cpp +++ b/sources/netctlgui/src/wpasupinteract.cpp @@ -26,9 +26,7 @@ #include #include -#include -#include -#include +#include /** @@ -41,6 +39,7 @@ WpaSup::WpaSup(const bool debugCmd, const QMap settings) : debug(debugCmd) { netctlCommand = new Netctl(debug, settings); + netctlProfile = new NetctlProfile(debug, settings); if (settings.contains(QString("CTRL_DIR"))) ctrlDir = settings[QString("CTRL_DIR")]; @@ -50,14 +49,6 @@ WpaSup::WpaSup(const bool debugCmd, const QMap settings) ctrlGroup = settings[QString("CTRL_GROUP")]; else ctrlGroup = QString("users"); - if (settings.contains(QString("IFACE_DIR"))) - ifaceDirectory = new QDir(settings[QString("IFACE_DIR")]); - else - ifaceDirectory = new QDir(QString("/sys/class/net/")); - if (settings.contains(QString("PREFERED_IFACE"))) - mainInterface = settings[QString("PREFERED_IFACE")]; - else - mainInterface = QString(""); if (settings.contains(QString("PID_FILE"))) pidFile = settings[QString("PID_FILE")]; else @@ -92,9 +83,10 @@ WpaSup::~WpaSup() { if (debug) qDebug() << "[WpaSup]" << "[~WpaSup]"; - delete netctlCommand; - if (ifaceDirectory != 0) - delete ifaceDirectory; + if (netctlCommand != 0) + delete netctlCommand; + if (netctlProfile != 0) + delete netctlProfile; } @@ -110,40 +102,13 @@ QString WpaSup::existentProfile(const QString essid) QString profileFile = QString(""); QList profileList = netctlCommand->getProfileList(); for (int i=0; igetSsidFromProfile(profileList[i][0])) + if (essid == netctlProfile->getValueFromProfile(profileList[i][0], QString("ESSID"))) profileFile = profileList[i][0]; return profileFile; } -/** - * @fn getInterfaceList - */ -QStringList WpaSup::getInterfaceList() -{ - if (debug) qDebug() << "[WpaSup]" << "[getInterfaceList]"; - if (ifaceDirectory == 0) { - if (debug) qDebug() << "[WpaSup]" << "[getInterfaceList]" << ":" << "Could not find directory"; - return QStringList(); - } - - QStringList interfaces; - if (!mainInterface.isEmpty()) - interfaces.append(mainInterface); - QStringList allInterfaces = ifaceDirectory->entryList(QDir::Dirs | QDir::NoDotAndDotDot); - for (int i=0; ipath() + QDir::separator() + allInterfaces[i] + QDir::separator() + QString("wireless"); - if (QDir(ifaceDirectory->path() + QDir::separator() + allInterfaces[i] + - QDir::separator() + QString("wireless")).exists()) - interfaces.append(allInterfaces[i]); - } - - return interfaces; -} - - /** * @fn isProfileActive */ @@ -155,7 +120,7 @@ bool WpaSup::isProfileActive(const QString essid) QString profileFile; QList profileList = netctlCommand->getProfileList(); for (int i=0; igetSsidFromProfile(profileList[i][0])) { + if (essid == netctlProfile->getValueFromProfile(profileList[i][0], QString("ESSID"))) { profileFile = profileList[i][0]; break; } @@ -175,7 +140,7 @@ bool WpaSup::isProfileExists(const QString essid) bool exists = false; QList profileList = netctlCommand->getProfileList(); for (int i=0; igetSsidFromProfile(profileList[i][0])) { + if (essid == netctlProfile->getValueFromProfile(profileList[i][0], QString("ESSID"))) { exists = true; break; } @@ -287,7 +252,7 @@ bool WpaSup::startWpaSupplicant() if (debug) qDebug() << "[WpaSup]" << "[startWpaSupplicant]" << ":" << "Could not find wpa_supplicant"; return false; } - if (getInterfaceList().isEmpty()) { + if (netctlCommand->getInterfaceList().isEmpty()) { if (debug) qDebug() << "[WpaSup]" << "[startWpaSupplicant]" << ":" << "Could not find interfaces"; return false; } @@ -295,7 +260,7 @@ bool WpaSup::startWpaSupplicant() if (QFile(pidFile).exists()) return true; QProcess command; - QString interface = getInterfaceList()[0]; + QString interface = netctlCommand->getInterfaceList()[0]; QString commandText = sudoCommand + QString(" ") + wpaSupPath + QString(" -B -P ") + pidFile + QString(" -i ") + interface + QString(" -D ") + wpaDrivers + QString(" -C \"DIR=") + ctrlDir + QString(" GROUP=") + ctrlGroup + QString("\""); @@ -343,13 +308,13 @@ QString WpaSup::getWpaCliOutput(const QString commandLine) if (debug) qDebug() << "[WpaSup]" << "[getWpaCliOutput]" << ":" << "Could not find wpa_cli"; return QString(); } - if (getInterfaceList().isEmpty()) { + if (netctlCommand->getInterfaceList().isEmpty()) { if (debug) qDebug() << "[WpaSup]" << "[getWpaCliOutput]" << ":" << "Could not find interfaces"; return QString(); } QProcess command; - QString interface = getInterfaceList()[0]; + QString interface = netctlCommand->getInterfaceList()[0]; QString commandText = wpaCliPath + QString(" -i ") + interface + QString(" -p ") + ctrlDir + QString(" -P ") + pidFile + QString(" ") + commandLine; if (debug) qDebug() << "[WpaSup]" << "[getWpaCliOutput]" << ":" << "Run cmd" << commandText; @@ -379,13 +344,13 @@ bool WpaSup::wpaCliCall(const QString commandLine) if (debug) qDebug() << "[WpaSup]" << "[wpaCliCall]" << ":" << "Could not find wpa_cli"; return false; } - if (getInterfaceList().isEmpty()) { + if (netctlCommand->getInterfaceList().isEmpty()) { if (debug) qDebug() << "[WpaSup]" << "[wpaCliCall]" << ":" << "Could not find interfaces"; return false; } QProcess command; - QString interface = getInterfaceList()[0]; + QString interface = netctlCommand->getInterfaceList()[0]; QString commandText = wpaCliPath + QString(" -i ") + interface + QString(" -p ") + ctrlDir + QString(" -P ") + pidFile + QString(" ") + commandLine; if (debug) qDebug() << "[WpaSup]" << "[wpaCliCall]" << ":" << "Run cmd" << commandText;