add netctlgui.h

rewrited some functions
This commit is contained in:
arcan1s
2014-07-21 10:25:10 +04:00
parent 79e7aa1926
commit 4600aae8bc
10 changed files with 138 additions and 154 deletions

View File

@ -27,7 +27,7 @@
#include <QFile>
#include <QProcess>
#include <netctlgui/netctlinteract.h>
#include <netctlgui/netctlgui.h>
/**
@ -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; i<allInterfaces.count(); i++) {
if (debug) qDebug() << "[Netctl]" << "[getWifiInterface]" << ":" << "Check directory"
<< ifaceDirectory->path() + 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; i<allInterfaces.count(); i++) {
if (debug) qDebug() << "[Netctl]" << "[getInterfaceList]" << ":" << "Check directory"
<< ifaceDirectory->path() + 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");

View File

@ -29,7 +29,7 @@
#include <QProcess>
#include <QTextStream>
#include <netctlgui/netctlprofile.h>
#include <netctlgui/netctlgui.h>
/**
@ -186,6 +186,24 @@ QMap<QString, QString> 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<QString, QString> settings = getSettingsFromProfile(profile);
if (settings.contains(key))
return settings[key];
else
return QString("");
}
/**
* @fn removeProfile
*/

View File

@ -26,9 +26,7 @@
#include <QDebug>
#include <QProcess>
#include <netctlgui/netctlinteract.h>
#include <netctlgui/sleepthread.h>
#include <netctlgui/wpasupinteract.h>
#include <netctlgui/netctlgui.h>
/**
@ -41,6 +39,7 @@ WpaSup::WpaSup(const bool debugCmd, const QMap<QString, QString> 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<QString, QString> 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<QStringList> profileList = netctlCommand->getProfileList();
for (int i=0; i<profileList.count(); i++)
if (essid == netctlCommand->getSsidFromProfile(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; i<allInterfaces.count(); i++) {
if (debug) qDebug() << "[WpaSup]" << "[getInterfaceList]" << ":" << "Check directory"
<< ifaceDirectory->path() + 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<QStringList> profileList = netctlCommand->getProfileList();
for (int i=0; i<profileList.count(); i++)
if (essid == netctlCommand->getSsidFromProfile(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<QStringList> profileList = netctlCommand->getProfileList();
for (int i=0; i<profileList.count(); i++)
if (essid == netctlCommand->getSsidFromProfile(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;