end of refactoring of the library

This commit is contained in:
arcan1s 2014-07-13 09:44:20 +04:00
parent 44f0b6a06a
commit 8ce4b03399
3 changed files with 142 additions and 73 deletions

View File

@ -48,7 +48,9 @@ Netctl::~Netctl()
{ {
if (debug) qDebug() << "[Netctl]" << "[~Netctl]"; if (debug) qDebug() << "[Netctl]" << "[~Netctl]";
delete ifaceDirectory; if (ifaceDirectory != 0)
delete ifaceDirectory;
if (profileDirectory != 0)
delete profileDirectory; delete profileDirectory;
} }

View File

@ -39,7 +39,8 @@ NetctlProfile::~NetctlProfile()
{ {
if (debug) qDebug() << "[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]";
if (debug) qDebug() << "[NetctlProfile]" << "[copyProfile]" << ":" << "Path" << oldPath; if (debug) qDebug() << "[NetctlProfile]" << "[copyProfile]" << ":" << "Path" << oldPath;
if (profileDirectory == 0) { if (profileDirectory == 0) {
if (debug) qDebug() << "[NetctlProfile]" << "[profileDirectory]" << "Could not find directory"; if (debug) qDebug() << "[NetctlProfile]" << "[profileDirectory]" << ":" << "Could not find directory";
return false; return false;
} }
if (sudoCommand == 0) { if (sudoCommand == 0) {
if (debug) qDebug() << "[NetctlProfile]" << "[profileDirectory]" << "Could not find sudo"; if (debug) qDebug() << "[NetctlProfile]" << "[profileDirectory]" << ":" << "Could not find sudo";
return false; return false;
} }
@ -110,7 +111,7 @@ QMap<QString, QString> NetctlProfile::getSettingsFromProfile(const QString profi
if (debug) qDebug() << "[NetctlProfile]" << "[getSettingsFromProfile]"; if (debug) qDebug() << "[NetctlProfile]" << "[getSettingsFromProfile]";
if (debug) qDebug() << "[NetctlProfile]" << "[getSettingsFromProfile]" << ":" << "Profile" << profile; if (debug) qDebug() << "[NetctlProfile]" << "[getSettingsFromProfile]" << ":" << "Profile" << profile;
if (profileDirectory == 0) { if (profileDirectory == 0) {
if (debug) qDebug() << "[NetctlProfile]" << "[getSettingsFromProfile]" << "Could not find directory"; if (debug) qDebug() << "[NetctlProfile]" << "[getSettingsFromProfile]" << ":" << "Could not find directory";
return QMap<QString, QString>(); return QMap<QString, QString>();
} }
@ -176,11 +177,11 @@ bool NetctlProfile::removeProfile(const QString profile)
if (debug) qDebug() << "[NetctlProfile]" << "[removeProfile]"; if (debug) qDebug() << "[NetctlProfile]" << "[removeProfile]";
if (debug) qDebug() << "[NetctlProfile]" << "[removeProfile]" << ":" << "Profile" << profile; if (debug) qDebug() << "[NetctlProfile]" << "[removeProfile]" << ":" << "Profile" << profile;
if (profileDirectory == 0) { if (profileDirectory == 0) {
if (debug) qDebug() << "[NetctlProfile]" << "[removeProfile]" << "Could not find directory"; if (debug) qDebug() << "[NetctlProfile]" << "[removeProfile]" << ":" << "Could not find directory";
return false; return false;
} }
if (sudoCommand == 0) { if (sudoCommand == 0) {
if (debug) qDebug() << "[NetctlProfile]" << "[removeProfile]" << "Could not find sudo"; if (debug) qDebug() << "[NetctlProfile]" << "[removeProfile]" << ":" << "Could not find sudo";
return false; return false;
} }

View File

@ -28,15 +28,24 @@ WpaSup::WpaSup(const bool debugCmd, const QMap<QString, QString> settings)
{ {
netctlCommand = new Netctl(debug, settings); netctlCommand = new Netctl(debug, settings);
ctrlDir = settings[QString("CTRL_DIR")]; if (settings.contains(QString("CTRL_DIR")))
ctrlGroup = settings[QString("CTRL_GROUP")]; ctrlDir = settings[QString("CTRL_DIR")];
ifaceDirectory = new QDir(settings[QString("IFACE_DIR")]); if (settings.contains(QString("CTRL_GROUP")))
mainInterface = settings[QString("PREFERED_IFACE")]; ctrlGroup = settings[QString("CTRL_GROUP")];
pidFile = settings[QString("PID_FILE")]; if (settings.contains(QString("IFACE_DIR")))
sudoCommand = settings[QString("SUDO_PATH")]; ifaceDirectory = new QDir(settings[QString("IFACE_DIR")]);
wpaCliPath = settings[QString("WPACLI_PATH")]; if (settings.contains(QString("PREFERED_IFACE")))
wpaDrivers = settings[QString("WPA_DRIVERS")]; mainInterface = settings[QString("PREFERED_IFACE")];
wpaSupPath = settings[QString("WPASUP_PATH")]; 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 // terminate old loaded profile
if (QFile(pidFile).exists() || QDir(ctrlDir).exists()) if (QFile(pidFile).exists() || QDir(ctrlDir).exists())
@ -49,7 +58,8 @@ WpaSup::~WpaSup()
if (debug) qDebug() << "[WpaSup]" << "[~WpaSup]"; if (debug) qDebug() << "[WpaSup]" << "[~WpaSup]";
delete netctlCommand; delete netctlCommand;
delete ifaceDirectory; if (ifaceDirectory != 0)
delete ifaceDirectory;
} }
@ -57,6 +67,7 @@ WpaSup::~WpaSup()
QString WpaSup::existentProfile(const QString profile) QString WpaSup::existentProfile(const QString profile)
{ {
if (debug) qDebug() << "[WpaSup]" << "[existentProfile]"; if (debug) qDebug() << "[WpaSup]" << "[existentProfile]";
if (debug) qDebug() << "[WpaSup]" << "[existentProfile]" << ":" << "Profile" << profile;
QString profileFile = QString(""); QString profileFile = QString("");
QList<QStringList> profileList = netctlCommand->getProfileList(); QList<QStringList> profileList = netctlCommand->getProfileList();
@ -71,9 +82,12 @@ QString WpaSup::existentProfile(const QString profile)
QStringList WpaSup::getInterfaceList() QStringList WpaSup::getInterfaceList()
{ {
if (debug) qDebug() << "[WpaSup]" << "[getInterfaceList]"; if (debug) qDebug() << "[WpaSup]" << "[getInterfaceList]";
if (ifaceDirectory == 0) {
if (debug) qDebug() << "[WpaSup]" << "[getInterfaceList]" << ":" << "Could not find directory";
return QStringList();
}
QStringList interfaces; QStringList interfaces;
if (!mainInterface.isEmpty()) if (!mainInterface.isEmpty())
interfaces.append(mainInterface); interfaces.append(mainInterface);
QStringList allInterfaces = ifaceDirectory->entryList(QDir::Dirs | QDir::NoDotAndDotDot); QStringList allInterfaces = ifaceDirectory->entryList(QDir::Dirs | QDir::NoDotAndDotDot);
@ -92,6 +106,7 @@ QStringList WpaSup::getInterfaceList()
bool WpaSup::isProfileActive(const QString profile) bool WpaSup::isProfileActive(const QString profile)
{ {
if (debug) qDebug() << "[WpaSup]" << "[isProfileActive]"; if (debug) qDebug() << "[WpaSup]" << "[isProfileActive]";
if (debug) qDebug() << "[WpaSup]" << "[isProfileActive]" << ":" << "Profile" << profile;
QString profileFile; QString profileFile;
QList<QStringList> profileList = netctlCommand->getProfileList(); QList<QStringList> profileList = netctlCommand->getProfileList();
@ -106,6 +121,7 @@ bool WpaSup::isProfileActive(const QString profile)
bool WpaSup::isProfileExists(const QString profile) bool WpaSup::isProfileExists(const QString profile)
{ {
if (debug) qDebug() << "[WpaSup]" << "[isProfileExists]"; if (debug) qDebug() << "[WpaSup]" << "[isProfileExists]";
if (debug) qDebug() << "[WpaSup]" << "[isProfileExists]" << ":" << "Profile" << profile;
bool exists = false; bool exists = false;
QList<QStringList> profileList = netctlCommand->getProfileList(); QList<QStringList> 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<QStringList> WpaSup::scanWifi() QList<QStringList> WpaSup::scanWifi()
{ {
if (debug) qDebug() << "[WpaSup]" << "[scanWifi]"; if (debug) qDebug() << "[WpaSup]" << "[scanWifi]";
@ -186,7 +165,6 @@ QList<QStringList> WpaSup::scanWifi()
for (int i=0; i<rawList.count(); i++) { for (int i=0; i<rawList.count(); i++) {
QStringList wifiPoint; QStringList wifiPoint;
// point name // point name
if (rawList[i].split(QChar('\t'), QString::SkipEmptyParts).count() > 4) if (rawList[i].split(QChar('\t'), QString::SkipEmptyParts).count() > 4)
wifiPoint.append(rawList[i].split(QChar('\t'), QString::SkipEmptyParts)[4]); wifiPoint.append(rawList[i].split(QChar('\t'), QString::SkipEmptyParts)[4]);
@ -217,11 +195,10 @@ QList<QStringList> WpaSup::scanWifi()
else else
security = QString("none"); security = QString("none");
wifiPoint.append(security); wifiPoint.append(security);
scanResults.append(wifiPoint); scanResults.append(wifiPoint);
} }
stopWpaSupplicant(); stopWpaSupplicant();
return scanResults; return scanResults;
} }
@ -229,23 +206,48 @@ QList<QStringList> WpaSup::scanWifi()
bool WpaSup::startWpaSupplicant() bool WpaSup::startWpaSupplicant()
{ {
if (debug) qDebug() << "[WpaSup]" << "[startWpaSupplicant]"; if (debug) qDebug() << "[WpaSup]" << "[startWpaSupplicant]";
if (ctrlDir == 0) {
if (!QFile(pidFile).exists()) { if (debug) qDebug() << "[WpaSup]" << "[startWpaSupplicant]" << ":" << "Could not find directory";
QString interface = getInterfaceList()[0]; return false;
QProcess command; }
QString commandText = sudoCommand + QString(" ") + wpaSupPath + QString(" -B -P ") + pidFile + if (ctrlGroup == 0) {
QString(" -i ") + interface + QString(" -D ") + wpaDrivers + if (debug) qDebug() << "[WpaSup]" << "[startWpaSupplicant]" << ":" << "Could not find group";
QString(" -C \"DIR=") + ctrlDir + QString(" GROUP=") + ctrlGroup + QString("\""); return false;
if (debug) qDebug() << "[WpaSup]" << "[startWpaSupplicant]" << ":" << "Run cmd" << commandText; }
command.start(commandText); if (pidFile == 0) {
command.waitForFinished(-1); if (debug) qDebug() << "[WpaSup]" << "[startWpaSupplicant]" << ":" << "Could not find PID file";
SleepThread::sleep(1); return false;
if (debug) qDebug() << "[WpaSup]" << "[startWpaSupplicant]" << ":" << "Cmd returns" << command.exitCode(); }
if (command.exitCode() != 0) if (sudoCommand == 0) {
return false; 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")); 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();
}