From 44f0b6a06a09be5116d56d4e15c6e9fc870b85ae Mon Sep 17 00:00:00 2001 From: arcan1s Date: Sun, 13 Jul 2014 00:13:24 +0400 Subject: [PATCH] refactoring of netctlprofile --- sources/gui/src/mainwindow.cpp | 6 +- .../include/netctlgui/netctlprofile.h | 3 +- sources/netctlgui/src/netctlinteract.cpp | 59 +++++-- sources/netctlgui/src/netctlprofile.cpp | 165 +++++++++--------- 4 files changed, 131 insertions(+), 102 deletions(-) diff --git a/sources/gui/src/mainwindow.cpp b/sources/gui/src/mainwindow.cpp index c695ec9..fd7f561 100644 --- a/sources/gui/src/mainwindow.cpp +++ b/sources/gui/src/mainwindow.cpp @@ -1046,7 +1046,7 @@ void MainWindow::profileTabCreateProfile() ui->tabWidget->setDisabled(true); // read settings - QString profile = netctlProfile->getNameByString(ui->comboBox_profile->currentText()); + QString profile = QFileInfo(ui->comboBox_profile->currentText()).fileName(); QMap settings; settings = generalWid->getSettings(); if (generalWid->connectionType->currentText() == QString("ethernet")) { @@ -1143,7 +1143,7 @@ void MainWindow::profileTabLoadProfile() { if (debug) qDebug() << "[MainWindow]" << "[profileTabLoadProfile]"; - QString profile = netctlProfile->getNameByString(ui->comboBox_profile->currentText()); + QString profile = QFileInfo(ui->comboBox_profile->currentText()).fileName(); QMap settings = netctlProfile->getSettingsFromProfile(profile); generalWid->setSettings(settings); @@ -1196,7 +1196,7 @@ void MainWindow::profileTabRemoveProfile() ui->tabWidget->setDisabled(true); // call netctlprofile - QString profile = netctlProfile->getNameByString(ui->comboBox_profile->currentText()); + QString profile = QFileInfo(ui->comboBox_profile->currentText()).fileName(); if (netctlProfile->removeProfile(profile)) ui->statusBar->showMessage(QApplication::translate("MainWindow", "Done")); else diff --git a/sources/netctlgui/include/netctlgui/netctlprofile.h b/sources/netctlgui/include/netctlgui/netctlprofile.h index 4d830e8..e3fd81b 100644 --- a/sources/netctlgui/include/netctlgui/netctlprofile.h +++ b/sources/netctlgui/include/netctlgui/netctlprofile.h @@ -32,10 +32,9 @@ public: const QMap settings = QMap()); ~NetctlProfile(); bool copyProfile(const QString oldPath); - bool removeProfile(const QString profile); QString createProfile(const QString profile, const QMap settings); - QString getNameByString(const QString profile); QMap getSettingsFromProfile(const QString profile); + bool removeProfile(const QString profile); private: bool debug; diff --git a/sources/netctlgui/src/netctlinteract.cpp b/sources/netctlgui/src/netctlinteract.cpp index 4d055e5..db20623 100644 --- a/sources/netctlgui/src/netctlinteract.cpp +++ b/sources/netctlgui/src/netctlinteract.cpp @@ -57,12 +57,14 @@ Netctl::~Netctl() QString Netctl::getNetctlOutput(const bool sudo, const QString commandLine, const QString profile) { if (debug) qDebug() << "[Netctl]" << "[getNetctlOutput]"; + if (debug) qDebug() << "[Netctl]" << "[getNetctlOutput]" << ":" << "Command" << commandLine; + if (debug) qDebug() << "[Netctl]" << "[getNetctlOutput]" << ":" << "Profile" << profile; if (netctlCommand == 0) { - if (debug) qDebug() << "[Netctl]" << "[getNetctlOutput]" << "Could not find netctl"; + if (debug) qDebug() << "[Netctl]" << "[getNetctlOutput]" << ":" << "Could not find netctl"; return QString(); } if ((sudo) && (sudoCommand == 0)) { - if (debug) qDebug() << "[Netctl]" << "[getNetctlOutput]" << "Could not find sudo"; + if (debug) qDebug() << "[Netctl]" << "[getNetctlOutput]" << ":" << "Could not find sudo"; return QString(); } @@ -85,7 +87,7 @@ QString Netctl::getWifiInterface() { if (debug) qDebug() << "[Netctl]" << "[getInterfaceList]"; if (ifaceDirectory == 0) { - if (debug) qDebug() << "[Netctl]" << "[getInterfaceList]" << "Could not find directory"; + if (debug) qDebug() << "[Netctl]" << "[getInterfaceList]" << ":" << "Could not find directory"; return QString(); } @@ -108,12 +110,14 @@ QString Netctl::getWifiInterface() bool Netctl::netctlCall(const bool sudo, const QString commandLine, const QString profile) { if (debug) qDebug() << "[Netctl]" << "[netctlCall]"; + if (debug) qDebug() << "[Netctl]" << "[netctlCall]" << ":" << "Command" << commandLine; + if (debug) qDebug() << "[Netctl]" << "[netctlCall]" << ":" << "Profile" << profile; if (netctlCommand == 0) { - if (debug) qDebug() << "[Netctl]" << "[netctlCall]" << "Could not find netctl"; + if (debug) qDebug() << "[Netctl]" << "[netctlCall]" << ":" << "Could not find netctl"; return false; } if ((sudo) && (sudoCommand == 0)) { - if (debug) qDebug() << "[Netctl]" << "[netctlCall]" << "Could not find sudo"; + if (debug) qDebug() << "[Netctl]" << "[netctlCall]" << ":" << "Could not find sudo"; return false; } @@ -139,12 +143,14 @@ bool Netctl::netctlCall(const bool sudo, const QString commandLine, const QStrin bool Netctl::netctlAutoCall(const bool sudo, const QString commandLine, const QString profile) { if (debug) qDebug() << "[Netctl]" << "[netctlAutoCall]"; + if (debug) qDebug() << "[Netctl]" << "[netctlAutoCall]" << ":" << "Command" << commandLine; + if (debug) qDebug() << "[Netctl]" << "[netctlAutoCall]" << ":" << "Profile" << profile; if (netctlAutoCommand == 0) { - if (debug) qDebug() << "[Netctl]" << "[netctlAutoCall]" << "Could not find netctl-auto"; + if (debug) qDebug() << "[Netctl]" << "[netctlAutoCall]" << ":" << "Could not find netctl-auto"; return false; } if ((sudo) && (sudoCommand == 0)) { - if (debug) qDebug() << "[Netctl]" << "[netctlAutoCall]" << "Could not find sudo"; + if (debug) qDebug() << "[Netctl]" << "[netctlAutoCall]" << ":" << "Could not find sudo"; return false; } @@ -171,16 +177,17 @@ bool Netctl::netctlAutoCall(const bool sudo, const QString commandLine, const QS bool Netctl::systemctlCall(const bool sudo, const QString commandLine) { if (debug) qDebug() << "[Netctl]" << "[systemctlCall]"; + if (debug) qDebug() << "[Netctl]" << "[systemctlCall]" << ":" << "Command" << commandLine; if (netctlAutoService == 0) { - if (debug) qDebug() << "[Netctl]" << "[systemctlCall]" << "Could not find service"; + if (debug) qDebug() << "[Netctl]" << "[systemctlCall]" << ":" << "Could not find service"; return false; } if ((sudo) && (sudoCommand == 0)) { - if (debug) qDebug() << "[Netctl]" << "[systemctlCall]" << "Could not find sudo"; + if (debug) qDebug() << "[Netctl]" << "[systemctlCall]" << ":" << "Could not find sudo"; return false; } if (systemctlCommand == 0) { - if (debug) qDebug() << "[Netctl]" << "[systemctlCall]" << "Could not find systemctl"; + if (debug) qDebug() << "[Netctl]" << "[systemctlCall]" << ":" << "Could not find systemctl"; return false; } @@ -210,7 +217,7 @@ QList Netctl::getProfileList() { if (debug) qDebug() << "[Netctl]" << "[getProfileList]"; if (profileDirectory == 0) { - if (debug) qDebug() << "[Netctl]" << "[getProfileList]" << "Could not find directory"; + if (debug) qDebug() << "[Netctl]" << "[getProfileList]" << ":" << "Could not find directory"; return QList(); } @@ -234,7 +241,7 @@ QList Netctl::getProfileListFromNetctlAuto() { if (debug) qDebug() << "[Netctl]" << "[getProfileListFromNetctlAuto]"; if (netctlAutoCommand == 0) { - if (debug) qDebug() << "[Netctl]" << "[getProfileListFromNetctlAuto]" << "Could not find netctl-auto"; + if (debug) qDebug() << "[Netctl]" << "[getProfileListFromNetctlAuto]" << ":" << "Could not find netctl-auto"; return QList(); } @@ -260,8 +267,9 @@ QList Netctl::getProfileListFromNetctlAuto() QString Netctl::getProfileDescription(const QString profileName) { if (debug) qDebug() << "[Netctl]" << "[getProfileDescription]"; + if (debug) qDebug() << "[Netctl]" << "[getProfileDescription]" << ":" << "Profile" << profileName; if (profileDirectory == 0) { - if (debug) qDebug() << "[Netctl]" << "[getProfileDescription]" << "Could not find directory"; + if (debug) qDebug() << "[Netctl]" << "[getProfileDescription]" << ":" << "Could not find directory"; return QString(); } @@ -291,8 +299,9 @@ QString Netctl::getProfileDescription(const QString profileName) QStringList Netctl::getProfileDescriptions(const QStringList profileList) { if (debug) qDebug() << "[Netctl]" << "[getProfileDescriptions]"; + if (debug) qDebug() << "[Netctl]" << "[getProfileDescriptions]" << ":" << "Profile list" << profileList; if (profileDirectory == 0) { - if (debug) qDebug() << "[Netctl]" << "[getProfileDescriptions]" << "Could not find directory"; + if (debug) qDebug() << "[Netctl]" << "[getProfileDescriptions]" << ":" << "Could not find directory"; return QStringList(); } @@ -328,6 +337,7 @@ QStringList Netctl::getProfileDescriptions(const QStringList profileList) QString Netctl::getProfileStatus(const QString profile) { if (debug) qDebug() << "[Netctl]" << "[getProfileStatus]"; + if (debug) qDebug() << "[Netctl]" << "[getProfileStatus]" << ":" << "Profile" << profile; QString status; if (isProfileActive(profile)) @@ -346,6 +356,7 @@ QString Netctl::getProfileStatus(const QString profile) QStringList Netctl::getProfileStatuses(const QStringList profileList) { if (debug) qDebug() << "[Netctl]" << "[getProfileStatuses]"; + if (debug) qDebug() << "[Netctl]" << "[getProfileStatuses]" << ":" << "Profile list" << profileList; QStringList statuses; for (int i=0; i profiles = getProfileListFromNetctlAuto(); @@ -438,6 +453,7 @@ bool Netctl::autoIsProfileActive(const QString profile) bool Netctl::autoIsProfileEnabled(const QString profile) { if (debug) qDebug() << "[Netctl]" << "[autoIsProfileEnabled]"; + if (debug) qDebug() << "[Netctl]" << "[autoIsProfileEnabled]" << ":" << "Profile" << profile; bool status = true; QList profiles = getProfileListFromNetctlAuto(); @@ -455,11 +471,11 @@ bool Netctl::isNetctlAutoEnabled() { if (debug) qDebug() << "[Netctl]" << "[isNetctlAutoEnabled]"; if (netctlAutoService == 0) { - if (debug) qDebug() << "[Netctl]" << "[isNetctlAutoEnabled]" << "Could not find service"; + if (debug) qDebug() << "[Netctl]" << "[isNetctlAutoEnabled]" << ":" << "Could not find service"; return false; } if (systemctlCommand == 0) { - if (debug) qDebug() << "[Netctl]" << "[isNetctlAutoEnabled]" << "Could not find systemctl"; + if (debug) qDebug() << "[Netctl]" << "[isNetctlAutoEnabled]" << ":" << "Could not find systemctl"; return false; } @@ -484,11 +500,11 @@ bool Netctl::isNetctlAutoRunning() { if (debug) qDebug() << "[Netctl]" << "[isNetctlAutoRunning]"; if (netctlAutoService == 0) { - if (debug) qDebug() << "[Netctl]" << "[isNetctlAutoRunning]" << "Could not find service"; + if (debug) qDebug() << "[Netctl]" << "[isNetctlAutoRunning]" << ":" << "Could not find service"; return false; } if (systemctlCommand == 0) { - if (debug) qDebug() << "[Netctl]" << "[isNetctlAutoRunning]" << "Could not find systemctl"; + if (debug) qDebug() << "[Netctl]" << "[isNetctlAutoRunning]" << ":" << "Could not find systemctl"; return false; } @@ -513,6 +529,7 @@ bool Netctl::isNetctlAutoRunning() bool Netctl::enableProfile(const QString profile) { if (debug) qDebug() << "[Netctl]" << "[enableProfile]"; + if (debug) qDebug() << "[Netctl]" << "[enableProfile]" << ":" << "Profile" << profile; if (isProfileEnabled(profile)) return netctlCall(true, QString("disable"), profile); @@ -524,6 +541,7 @@ bool Netctl::enableProfile(const QString profile) bool Netctl::restartProfile(const QString profile) { if (debug) qDebug() << "[Netctl]" << "[restartProfile]"; + if (debug) qDebug() << "[Netctl]" << "[restartProfile]" << ":" << "Profile" << profile; return netctlCall(true, QString("restart"), profile); } @@ -532,6 +550,7 @@ bool Netctl::restartProfile(const QString profile) bool Netctl::startProfile(const QString profile) { if (debug) qDebug() << "[Netctl]" << "[startProfile]"; + if (debug) qDebug() << "[Netctl]" << "[startProfile]" << ":" << "Profile" << profile; if (isProfileActive(profile)) return netctlCall(true, QString("stop"), profile); @@ -551,6 +570,7 @@ bool Netctl::autoDisableAllProfiles() bool Netctl::autoEnableProfile(const QString profile) { if (debug) qDebug() << "[Netctl]" << "[autoEnableProfile]"; + if (debug) qDebug() << "[Netctl]" << "[autoEnableProfile]" << ":" << "Profile" << profile; if (autoIsProfileEnabled(profile)) return netctlAutoCall(false, QString("disable"), profile); @@ -570,6 +590,7 @@ bool Netctl::autoEnableAllProfiles() bool Netctl::autoStartProfile(const QString profile) { if (debug) qDebug() << "[Netctl]" << "[autoStartProfile]"; + if (debug) qDebug() << "[Netctl]" << "[autoStartProfile]" << ":" << "Profile" << profile; if (autoIsProfileActive(profile)) return true; diff --git a/sources/netctlgui/src/netctlprofile.cpp b/sources/netctlgui/src/netctlprofile.cpp index 56977a1..0e0cba2 100644 --- a/sources/netctlgui/src/netctlprofile.cpp +++ b/sources/netctlgui/src/netctlprofile.cpp @@ -28,8 +28,10 @@ NetctlProfile::NetctlProfile(const bool debugCmd, const QMap settings) : debug(debugCmd) { - profileDirectory = new QDir(settings[QString("PROFILE_DIR")]); - sudoCommand = settings[QString("SUDO_PATH")]; + if (settings.contains(QString("PROFILE_DIR"))) + profileDirectory = new QDir(settings[QString("PROFILE_DIR")]); + if (settings.contains(QString("SUDO_PATH"))) + sudoCommand = settings[QString("SUDO_PATH")]; } @@ -44,9 +46,18 @@ NetctlProfile::~NetctlProfile() 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"; + return false; + } + if (sudoCommand == 0) { + if (debug) qDebug() << "[NetctlProfile]" << "[profileDirectory]" << "Could not find sudo"; + return false; + } - QString newPath = profileDirectory->absolutePath() + QDir::separator() + QFileInfo(oldPath).fileName(); QProcess command; + QString newPath = profileDirectory->absolutePath() + QDir::separator() + QFileInfo(oldPath).fileName(); QString commandText = sudoCommand + QString(" /usr/bin/mv ") + oldPath + QString(" ") + newPath; if (debug) qDebug() << "[NetctlProfile]" << "[copyProfile]" << ":" << "Run cmd" << commandText; command.start(commandText); @@ -59,38 +70,17 @@ bool NetctlProfile::copyProfile(const QString oldPath) } -bool NetctlProfile::removeProfile(const QString profile) -{ - if (debug) qDebug() << "[NetctlProfile]" << "[removeProfile]"; - - QString profilePath = profileDirectory->absolutePath() + QDir::separator() + QFileInfo(profile).fileName(); - QProcess command; - QString commandText = sudoCommand + QString(" /usr/bin/rm ") + profilePath; - if (debug) qDebug() << "[NetctlProfile]" << "[removeProfile]" << ":" << "Run cmd" << commandText; - command.start(commandText); - command.waitForFinished(-1); - - if (command.exitCode() == 0) - return true; - else - return false; -} - - - QString NetctlProfile::createProfile(const QString profile, const QMap settings) { if (debug) qDebug() << "[NetctlProfile]" << "[createProfile]"; - if (debug) qDebug() << "[NetctlProfile]" << "[createProfile]" << ":" << "Profile name" << profile; + if (debug) qDebug() << "[NetctlProfile]" << "[createProfile]" << ":" << "Profile" << profile; QString profileTempName = QDir::homePath() + QDir::separator() + QString(".cache") + QDir::separator() + QFileInfo(profile).fileName(); QFile profileFile(profileTempName); if (debug) qDebug() << "[NetctlProfile]" << "[createProfile]" << ":" << "Save to" << profileTempName; - if (!profileFile.open(QIODevice::WriteOnly | QIODevice::Text)) return profileTempName; - QTextStream out(&profileFile); for (int i=0; i NetctlProfile::getSettingsFromProfile(const QString profile) { if (debug) qDebug() << "[NetctlProfile]" << "[getSettingsFromProfile]"; + if (debug) qDebug() << "[NetctlProfile]" << "[getSettingsFromProfile]" << ":" << "Profile" << profile; + if (profileDirectory == 0) { + if (debug) qDebug() << "[NetctlProfile]" << "[getSettingsFromProfile]" << "Could not find directory"; + return QMap(); + } QMap settings; QString fileStr, profileUrl; profileUrl = profileDirectory->absolutePath() + QDir::separator() + QFileInfo(profile).fileName(); QFile profileFile(profileUrl); if (debug) qDebug() << "[NetctlProfile]" << "[getSettingsFromProfile]" << ":" << "Read from" << profileUrl; - if (!profileFile.open(QIODevice::ReadOnly)) return settings; while (true) { fileStr = QString(profileFile.readLine()); - if (fileStr[0] != '#') { - if (fileStr.split(QChar('='), QString::SkipEmptyParts).count() == 2) { - if ((fileStr.split(QChar('='))[1][0] == QChar('(')) && - (fileStr.split(QChar('='))[1][fileStr.split(QChar('='))[1].size()-2] == QChar(')'))) - settings[fileStr.split(QChar('='))[0]] = fileStr.split(QChar('='))[1] - .remove(QString("(")) - .remove(QString(")")) - .trimmed(); - else if (fileStr.split(QChar('='))[1][0] == QChar('(')) { - QString parameterName = fileStr.split(QChar('='))[0]; - QStringList parameter; - if (!fileStr.split(QChar('='))[1] - .remove(QString("(")) - .remove(QString(")")) - .trimmed() - .isEmpty()) - parameter.append(fileStr.split(QChar('='))[1] - .remove(QString("(")) - .remove(QString(")")) - .trimmed()); - while(true) { - fileStr = QString(profileFile.readLine()); - if (fileStr[fileStr.size()-2] == QChar(')')) - break; - if (!fileStr.remove(QString("(")) - .remove(QString(")")) - .trimmed() - .isEmpty()) - parameter.append(fileStr.remove(QString("(")) - .remove(QString(")")) - .trimmed()); - if (profileFile.atEnd()) - break; - } - settings[parameterName] = parameter.join(QString("\n")); - } - else - settings[fileStr.split(QChar('='))[0]] = fileStr.split(QChar('='))[1] - .trimmed(); + if (fileStr[0] == QChar('#')) continue; + if (fileStr.split(QChar('='), QString::SkipEmptyParts).count() != 2) continue; + if ((fileStr.split(QChar('='))[1][0] == QChar('(')) && + (fileStr.split(QChar('='))[1][fileStr.split(QChar('='))[1].size()-2] == QChar(')'))) + settings[fileStr.split(QChar('='))[0]] = fileStr.split(QChar('='))[1] + .remove(QChar('(')) + .remove(QChar(')')) + .trimmed(); + else if (fileStr.split(QChar('='))[1][0] == QChar('(')) { + QString parameterName = fileStr.split(QChar('='))[0]; + QStringList parameter; + if (!fileStr.split(QChar('='))[1] + .remove(QChar('(')) + .remove(QChar(')')) + .trimmed() + .isEmpty()) + parameter.append(fileStr.split(QChar('='))[1] + .remove(QChar('(')) + .remove(QChar(')')) + .trimmed()); + while(true) { + fileStr = QString(profileFile.readLine()); + if (fileStr[fileStr.size()-2] == QChar(')')) + break; + if (!fileStr.remove(QChar('(')) + .remove(QChar(')')) + .trimmed() + .isEmpty()) + parameter.append(fileStr.remove(QChar('(')) + .remove(QChar(')')) + .trimmed()); + if (profileFile.atEnd()) + break; } - + settings[parameterName] = parameter.join(QChar('\n')); } + else + settings[fileStr.split(QChar('='))[0]] = fileStr.split(QChar('='))[1] + .trimmed(); if (profileFile.atEnd()) break; } - profileFile.close(); + return settings; } + + +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"; + return false; + } + if (sudoCommand == 0) { + if (debug) qDebug() << "[NetctlProfile]" << "[removeProfile]" << "Could not find sudo"; + return false; + } + + QProcess command; + QString profilePath = profileDirectory->absolutePath() + QDir::separator() + QFileInfo(profile).fileName(); + QString commandText = sudoCommand + QString(" /usr/bin/rm ") + profilePath; + if (debug) qDebug() << "[NetctlProfile]" << "[removeProfile]" << ":" << "Run cmd" << commandText; + command.start(commandText); + command.waitForFinished(-1); + + if (command.exitCode() == 0) + return true; + else + return false; +}