From 8ce4b03399670f26c205b0a43bb00fb7b4987533 Mon Sep 17 00:00:00 2001 From: arcan1s Date: Sun, 13 Jul 2014 09:44:20 +0400 Subject: [PATCH] end of refactoring of the library --- sources/netctlgui/src/netctlinteract.cpp | 4 +- sources/netctlgui/src/netctlprofile.cpp | 13 +- sources/netctlgui/src/wpasupinteract.cpp | 198 +++++++++++++++-------- 3 files changed, 142 insertions(+), 73 deletions(-) diff --git a/sources/netctlgui/src/netctlinteract.cpp b/sources/netctlgui/src/netctlinteract.cpp index db20623..8941426 100644 --- a/sources/netctlgui/src/netctlinteract.cpp +++ b/sources/netctlgui/src/netctlinteract.cpp @@ -48,7 +48,9 @@ Netctl::~Netctl() { if (debug) qDebug() << "[Netctl]" << "[~Netctl]"; - delete ifaceDirectory; + if (ifaceDirectory != 0) + delete ifaceDirectory; + if (profileDirectory != 0) delete profileDirectory; } diff --git a/sources/netctlgui/src/netctlprofile.cpp b/sources/netctlgui/src/netctlprofile.cpp index 0e0cba2..77a0504 100644 --- a/sources/netctlgui/src/netctlprofile.cpp +++ b/sources/netctlgui/src/netctlprofile.cpp @@ -39,7 +39,8 @@ NetctlProfile::~NetctlProfile() { if (debug) qDebug() << "[NetctlProfile]" << "[~NetctlProfile]"; - delete profileDirectory; + if (profileDirectory != 0) + delete profileDirectory; } @@ -48,11 +49,11 @@ bool NetctlProfile::copyProfile(const QString oldPath) if (debug) qDebug() << "[NetctlProfile]" << "[copyProfile]"; if (debug) qDebug() << "[NetctlProfile]" << "[copyProfile]" << ":" << "Path" << oldPath; if (profileDirectory == 0) { - if (debug) qDebug() << "[NetctlProfile]" << "[profileDirectory]" << "Could not find directory"; + if (debug) qDebug() << "[NetctlProfile]" << "[profileDirectory]" << ":" << "Could not find directory"; return false; } if (sudoCommand == 0) { - if (debug) qDebug() << "[NetctlProfile]" << "[profileDirectory]" << "Could not find sudo"; + if (debug) qDebug() << "[NetctlProfile]" << "[profileDirectory]" << ":" << "Could not find sudo"; return false; } @@ -110,7 +111,7 @@ QMap NetctlProfile::getSettingsFromProfile(const QString profi if (debug) qDebug() << "[NetctlProfile]" << "[getSettingsFromProfile]"; if (debug) qDebug() << "[NetctlProfile]" << "[getSettingsFromProfile]" << ":" << "Profile" << profile; if (profileDirectory == 0) { - if (debug) qDebug() << "[NetctlProfile]" << "[getSettingsFromProfile]" << "Could not find directory"; + if (debug) qDebug() << "[NetctlProfile]" << "[getSettingsFromProfile]" << ":" << "Could not find directory"; return QMap(); } @@ -176,11 +177,11 @@ bool NetctlProfile::removeProfile(const QString profile) if (debug) qDebug() << "[NetctlProfile]" << "[removeProfile]"; if (debug) qDebug() << "[NetctlProfile]" << "[removeProfile]" << ":" << "Profile" << profile; if (profileDirectory == 0) { - if (debug) qDebug() << "[NetctlProfile]" << "[removeProfile]" << "Could not find directory"; + if (debug) qDebug() << "[NetctlProfile]" << "[removeProfile]" << ":" << "Could not find directory"; return false; } if (sudoCommand == 0) { - if (debug) qDebug() << "[NetctlProfile]" << "[removeProfile]" << "Could not find sudo"; + if (debug) qDebug() << "[NetctlProfile]" << "[removeProfile]" << ":" << "Could not find sudo"; return false; } diff --git a/sources/netctlgui/src/wpasupinteract.cpp b/sources/netctlgui/src/wpasupinteract.cpp index 1565cd2..27b39fb 100644 --- a/sources/netctlgui/src/wpasupinteract.cpp +++ b/sources/netctlgui/src/wpasupinteract.cpp @@ -28,15 +28,24 @@ WpaSup::WpaSup(const bool debugCmd, const QMap settings) { netctlCommand = new Netctl(debug, settings); - ctrlDir = settings[QString("CTRL_DIR")]; - ctrlGroup = settings[QString("CTRL_GROUP")]; - ifaceDirectory = new QDir(settings[QString("IFACE_DIR")]); - mainInterface = settings[QString("PREFERED_IFACE")]; - pidFile = settings[QString("PID_FILE")]; - sudoCommand = settings[QString("SUDO_PATH")]; - wpaCliPath = settings[QString("WPACLI_PATH")]; - wpaDrivers = settings[QString("WPA_DRIVERS")]; - wpaSupPath = settings[QString("WPASUP_PATH")]; + if (settings.contains(QString("CTRL_DIR"))) + ctrlDir = settings[QString("CTRL_DIR")]; + if (settings.contains(QString("CTRL_GROUP"))) + ctrlGroup = settings[QString("CTRL_GROUP")]; + if (settings.contains(QString("IFACE_DIR"))) + ifaceDirectory = new QDir(settings[QString("IFACE_DIR")]); + if (settings.contains(QString("PREFERED_IFACE"))) + mainInterface = settings[QString("PREFERED_IFACE")]; + if (settings.contains(QString("PID_FILE"))) + pidFile = settings[QString("PID_FILE")]; + if (settings.contains(QString("SUDO_PATH"))) + sudoCommand = settings[QString("SUDO_PATH")]; + if (settings.contains(QString("WPACLI_PATH"))) + wpaCliPath = settings[QString("WPACLI_PATH")]; + if (settings.contains(QString("WPA_DRIVERS"))) + wpaDrivers = settings[QString("WPA_DRIVERS")]; + if (settings.contains(QString("WPASUP_PATH"))) + wpaSupPath = settings[QString("WPASUP_PATH")]; // terminate old loaded profile if (QFile(pidFile).exists() || QDir(ctrlDir).exists()) @@ -49,7 +58,8 @@ WpaSup::~WpaSup() if (debug) qDebug() << "[WpaSup]" << "[~WpaSup]"; delete netctlCommand; - delete ifaceDirectory; + if (ifaceDirectory != 0) + delete ifaceDirectory; } @@ -57,6 +67,7 @@ WpaSup::~WpaSup() QString WpaSup::existentProfile(const QString profile) { if (debug) qDebug() << "[WpaSup]" << "[existentProfile]"; + if (debug) qDebug() << "[WpaSup]" << "[existentProfile]" << ":" << "Profile" << profile; QString profileFile = QString(""); QList profileList = netctlCommand->getProfileList(); @@ -71,9 +82,12 @@ QString WpaSup::existentProfile(const QString profile) 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); @@ -92,6 +106,7 @@ QStringList WpaSup::getInterfaceList() bool WpaSup::isProfileActive(const QString profile) { if (debug) qDebug() << "[WpaSup]" << "[isProfileActive]"; + if (debug) qDebug() << "[WpaSup]" << "[isProfileActive]" << ":" << "Profile" << profile; QString profileFile; QList profileList = netctlCommand->getProfileList(); @@ -106,6 +121,7 @@ bool WpaSup::isProfileActive(const QString profile) bool WpaSup::isProfileExists(const QString profile) { if (debug) qDebug() << "[WpaSup]" << "[isProfileExists]"; + if (debug) qDebug() << "[WpaSup]" << "[isProfileExists]" << ":" << "Profile" << profile; bool exists = false; QList profileList = netctlCommand->getProfileList(); @@ -117,43 +133,6 @@ bool WpaSup::isProfileExists(const QString profile) } -// functions -bool WpaSup::wpaCliCall(const QString commandLine) -{ - if (debug) qDebug() << "[WpaSup]" << "[getInterfaceList]"; - - QString interface = getInterfaceList()[0]; - QProcess command; - QString commandText = wpaCliPath + QString(" -i ") + interface + QString(" -p ") + ctrlDir + - QString(" -P ") + pidFile + QString(" ") + commandLine; - if (debug) qDebug() << "[WpaSup]" << "[wpaCliCall]" << ":" << "Run cmd" << commandText; - command.start(commandText); - command.waitForFinished(-1); - SleepThread::sleep(1); - if (debug) qDebug() << "[WpaSup]" << "[wpaCliCall]" << ":" << "Cmd returns" << command.exitCode(); - if (command.exitCode() == 0) - return true; - else - return false; -} - - -QString WpaSup::getWpaCliOutput(const QString commandLine) -{ - if (debug) qDebug() << "[WpaSup]" << "[getWpaCliOutput]"; - - QString interface = getInterfaceList()[0]; - QProcess command; - QString commandText = wpaCliPath + QString(" -i ") + interface + QString(" -p ") + ctrlDir + - QString(" -P ") + pidFile + QString(" ") + commandLine; - if (debug) qDebug() << "[WpaSup]" << "[getWpaCliOutput]" << ":" << "Run cmd" << commandText; - command.start(commandText); - command.waitForFinished(-1); - - return command.readAllStandardOutput(); -} - - QList WpaSup::scanWifi() { if (debug) qDebug() << "[WpaSup]" << "[scanWifi]"; @@ -186,7 +165,6 @@ QList WpaSup::scanWifi() for (int i=0; i 4) wifiPoint.append(rawList[i].split(QChar('\t'), QString::SkipEmptyParts)[4]); @@ -217,11 +195,10 @@ QList WpaSup::scanWifi() else security = QString("none"); wifiPoint.append(security); - scanResults.append(wifiPoint); } - stopWpaSupplicant(); + return scanResults; } @@ -229,23 +206,48 @@ QList WpaSup::scanWifi() bool WpaSup::startWpaSupplicant() { if (debug) qDebug() << "[WpaSup]" << "[startWpaSupplicant]"; - - if (!QFile(pidFile).exists()) { - QString interface = getInterfaceList()[0]; - QProcess command; - QString commandText = sudoCommand + QString(" ") + wpaSupPath + QString(" -B -P ") + pidFile + - QString(" -i ") + interface + QString(" -D ") + wpaDrivers + - QString(" -C \"DIR=") + ctrlDir + QString(" GROUP=") + ctrlGroup + QString("\""); - if (debug) qDebug() << "[WpaSup]" << "[startWpaSupplicant]" << ":" << "Run cmd" << commandText; - command.start(commandText); - command.waitForFinished(-1); - SleepThread::sleep(1); - if (debug) qDebug() << "[WpaSup]" << "[startWpaSupplicant]" << ":" << "Cmd returns" << command.exitCode(); - if (command.exitCode() != 0) - return false; + if (ctrlDir == 0) { + if (debug) qDebug() << "[WpaSup]" << "[startWpaSupplicant]" << ":" << "Could not find directory"; + return false; + } + if (ctrlGroup == 0) { + if (debug) qDebug() << "[WpaSup]" << "[startWpaSupplicant]" << ":" << "Could not find group"; + return false; + } + if (pidFile == 0) { + if (debug) qDebug() << "[WpaSup]" << "[startWpaSupplicant]" << ":" << "Could not find PID file"; + return false; + } + if (sudoCommand == 0) { + if (debug) qDebug() << "[WpaSup]" << "[startWpaSupplicant]" << ":" << "Could not find sudo"; + return false; + } + if (wpaDrivers == 0) { + if (debug) qDebug() << "[WpaSup]" << "[startWpaSupplicant]" << ":" << "Could not find drivers"; + return false; + } + if (wpaSupPath == 0) { + if (debug) qDebug() << "[WpaSup]" << "[startWpaSupplicant]" << ":" << "Could not find wpa_supplicant"; + return false; } - return true; + if (QFile(pidFile).exists()) + return true; + QProcess command; + QString interface = 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("\""); + if (debug) qDebug() << "[WpaSup]" << "[startWpaSupplicant]" << ":" << "Run cmd" << commandText; + command.start(commandText); + command.waitForFinished(-1); + SleepThread::sleep(1); + if (debug) qDebug() << "[WpaSup]" << "[startWpaSupplicant]" << ":" << "Cmd returns" << command.exitCode(); + + if (command.exitCode() == 0) + return true; + else + return false; } @@ -255,3 +257,67 @@ bool WpaSup::stopWpaSupplicant() return wpaCliCall(QString("terminate")); } + + +// functions +bool WpaSup::wpaCliCall(const QString commandLine) +{ + if (debug) qDebug() << "[WpaSup]" << "[wpaCliCall]"; + if (debug) qDebug() << "[WpaSup]" << "[wpaCliCall]" << ":" << "Command" << commandLine; + if (ctrlDir == 0) { + if (debug) qDebug() << "[WpaSup]" << "[wpaCliCall]" << ":" << "Could not find directory"; + return false; + } + if (pidFile == 0) { + if (debug) qDebug() << "[WpaSup]" << "[wpaCliCall]" << ":" << "Could not find PID file"; + return false; + } + if (wpaCliPath == 0) { + if (debug) qDebug() << "[WpaSup]" << "[wpaCliCall]" << ":" << "Could not find wpa_cli"; + return false; + } + + QProcess command; + QString interface = getInterfaceList()[0]; + QString commandText = wpaCliPath + QString(" -i ") + interface + QString(" -p ") + ctrlDir + + QString(" -P ") + pidFile + QString(" ") + commandLine; + if (debug) qDebug() << "[WpaSup]" << "[wpaCliCall]" << ":" << "Run cmd" << commandText; + command.start(commandText); + command.waitForFinished(-1); + SleepThread::sleep(1); + if (debug) qDebug() << "[WpaSup]" << "[wpaCliCall]" << ":" << "Cmd returns" << command.exitCode(); + + if (command.exitCode() == 0) + return true; + else + return false; +} + + +QString WpaSup::getWpaCliOutput(const QString commandLine) +{ + if (debug) qDebug() << "[WpaSup]" << "[getWpaCliOutput]"; + if (debug) qDebug() << "[WpaSup]" << "[getWpaCliOutput]" << ":" << "Command" << commandLine; + if (ctrlDir == 0) { + if (debug) qDebug() << "[WpaSup]" << "[getWpaCliOutput]" << ":" << "Could not find directory"; + return QString(); + } + if (pidFile == 0) { + if (debug) qDebug() << "[WpaSup]" << "[getWpaCliOutput]" << ":" << "Could not find PID file"; + return QString(); + } + if (wpaCliPath == 0) { + if (debug) qDebug() << "[WpaSup]" << "[getWpaCliOutput]" << ":" << "Could not find wpa_cli"; + return QString(); + } + + QProcess command; + QString interface = getInterfaceList()[0]; + QString commandText = wpaCliPath + QString(" -i ") + interface + QString(" -p ") + ctrlDir + + QString(" -P ") + pidFile + QString(" ") + commandLine; + if (debug) qDebug() << "[WpaSup]" << "[getWpaCliOutput]" << ":" << "Run cmd" << commandText; + command.start(commandText); + command.waitForFinished(-1); + + return command.readAllStandardOutput(); +}