refactoring of netctlprofile

This commit is contained in:
arcan1s 2014-07-13 00:13:24 +04:00
parent b37272d875
commit 44f0b6a06a
4 changed files with 131 additions and 102 deletions

View File

@ -1046,7 +1046,7 @@ void MainWindow::profileTabCreateProfile()
ui->tabWidget->setDisabled(true); ui->tabWidget->setDisabled(true);
// read settings // read settings
QString profile = netctlProfile->getNameByString(ui->comboBox_profile->currentText()); QString profile = QFileInfo(ui->comboBox_profile->currentText()).fileName();
QMap<QString, QString> settings; QMap<QString, QString> settings;
settings = generalWid->getSettings(); settings = generalWid->getSettings();
if (generalWid->connectionType->currentText() == QString("ethernet")) { if (generalWid->connectionType->currentText() == QString("ethernet")) {
@ -1143,7 +1143,7 @@ void MainWindow::profileTabLoadProfile()
{ {
if (debug) qDebug() << "[MainWindow]" << "[profileTabLoadProfile]"; if (debug) qDebug() << "[MainWindow]" << "[profileTabLoadProfile]";
QString profile = netctlProfile->getNameByString(ui->comboBox_profile->currentText()); QString profile = QFileInfo(ui->comboBox_profile->currentText()).fileName();
QMap<QString, QString> settings = netctlProfile->getSettingsFromProfile(profile); QMap<QString, QString> settings = netctlProfile->getSettingsFromProfile(profile);
generalWid->setSettings(settings); generalWid->setSettings(settings);
@ -1196,7 +1196,7 @@ void MainWindow::profileTabRemoveProfile()
ui->tabWidget->setDisabled(true); ui->tabWidget->setDisabled(true);
// call netctlprofile // call netctlprofile
QString profile = netctlProfile->getNameByString(ui->comboBox_profile->currentText()); QString profile = QFileInfo(ui->comboBox_profile->currentText()).fileName();
if (netctlProfile->removeProfile(profile)) if (netctlProfile->removeProfile(profile))
ui->statusBar->showMessage(QApplication::translate("MainWindow", "Done")); ui->statusBar->showMessage(QApplication::translate("MainWindow", "Done"));
else else

View File

@ -32,10 +32,9 @@ public:
const QMap<QString, QString> settings = QMap<QString, QString>()); const QMap<QString, QString> settings = QMap<QString, QString>());
~NetctlProfile(); ~NetctlProfile();
bool copyProfile(const QString oldPath); bool copyProfile(const QString oldPath);
bool removeProfile(const QString profile);
QString createProfile(const QString profile, const QMap<QString, QString> settings); QString createProfile(const QString profile, const QMap<QString, QString> settings);
QString getNameByString(const QString profile);
QMap<QString, QString> getSettingsFromProfile(const QString profile); QMap<QString, QString> getSettingsFromProfile(const QString profile);
bool removeProfile(const QString profile);
private: private:
bool debug; bool debug;

View File

@ -57,12 +57,14 @@ Netctl::~Netctl()
QString Netctl::getNetctlOutput(const bool sudo, const QString commandLine, const QString profile) QString Netctl::getNetctlOutput(const bool sudo, const QString commandLine, const QString profile)
{ {
if (debug) qDebug() << "[Netctl]" << "[getNetctlOutput]"; if (debug) qDebug() << "[Netctl]" << "[getNetctlOutput]";
if (debug) qDebug() << "[Netctl]" << "[getNetctlOutput]" << ":" << "Command" << commandLine;
if (debug) qDebug() << "[Netctl]" << "[getNetctlOutput]" << ":" << "Profile" << profile;
if (netctlCommand == 0) { if (netctlCommand == 0) {
if (debug) qDebug() << "[Netctl]" << "[getNetctlOutput]" << "Could not find netctl"; if (debug) qDebug() << "[Netctl]" << "[getNetctlOutput]" << ":" << "Could not find netctl";
return QString(); return QString();
} }
if ((sudo) && (sudoCommand == 0)) { if ((sudo) && (sudoCommand == 0)) {
if (debug) qDebug() << "[Netctl]" << "[getNetctlOutput]" << "Could not find sudo"; if (debug) qDebug() << "[Netctl]" << "[getNetctlOutput]" << ":" << "Could not find sudo";
return QString(); return QString();
} }
@ -85,7 +87,7 @@ QString Netctl::getWifiInterface()
{ {
if (debug) qDebug() << "[Netctl]" << "[getInterfaceList]"; if (debug) qDebug() << "[Netctl]" << "[getInterfaceList]";
if (ifaceDirectory == 0) { if (ifaceDirectory == 0) {
if (debug) qDebug() << "[Netctl]" << "[getInterfaceList]" << "Could not find directory"; if (debug) qDebug() << "[Netctl]" << "[getInterfaceList]" << ":" << "Could not find directory";
return QString(); return QString();
} }
@ -108,12 +110,14 @@ QString Netctl::getWifiInterface()
bool Netctl::netctlCall(const bool sudo, const QString commandLine, const QString profile) bool Netctl::netctlCall(const bool sudo, const QString commandLine, const QString profile)
{ {
if (debug) qDebug() << "[Netctl]" << "[netctlCall]"; if (debug) qDebug() << "[Netctl]" << "[netctlCall]";
if (debug) qDebug() << "[Netctl]" << "[netctlCall]" << ":" << "Command" << commandLine;
if (debug) qDebug() << "[Netctl]" << "[netctlCall]" << ":" << "Profile" << profile;
if (netctlCommand == 0) { if (netctlCommand == 0) {
if (debug) qDebug() << "[Netctl]" << "[netctlCall]" << "Could not find netctl"; if (debug) qDebug() << "[Netctl]" << "[netctlCall]" << ":" << "Could not find netctl";
return false; return false;
} }
if ((sudo) && (sudoCommand == 0)) { if ((sudo) && (sudoCommand == 0)) {
if (debug) qDebug() << "[Netctl]" << "[netctlCall]" << "Could not find sudo"; if (debug) qDebug() << "[Netctl]" << "[netctlCall]" << ":" << "Could not find sudo";
return false; 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) bool Netctl::netctlAutoCall(const bool sudo, const QString commandLine, const QString profile)
{ {
if (debug) qDebug() << "[Netctl]" << "[netctlAutoCall]"; if (debug) qDebug() << "[Netctl]" << "[netctlAutoCall]";
if (debug) qDebug() << "[Netctl]" << "[netctlAutoCall]" << ":" << "Command" << commandLine;
if (debug) qDebug() << "[Netctl]" << "[netctlAutoCall]" << ":" << "Profile" << profile;
if (netctlAutoCommand == 0) { 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; return false;
} }
if ((sudo) && (sudoCommand == 0)) { if ((sudo) && (sudoCommand == 0)) {
if (debug) qDebug() << "[Netctl]" << "[netctlAutoCall]" << "Could not find sudo"; if (debug) qDebug() << "[Netctl]" << "[netctlAutoCall]" << ":" << "Could not find sudo";
return false; 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) bool Netctl::systemctlCall(const bool sudo, const QString commandLine)
{ {
if (debug) qDebug() << "[Netctl]" << "[systemctlCall]"; if (debug) qDebug() << "[Netctl]" << "[systemctlCall]";
if (debug) qDebug() << "[Netctl]" << "[systemctlCall]" << ":" << "Command" << commandLine;
if (netctlAutoService == 0) { if (netctlAutoService == 0) {
if (debug) qDebug() << "[Netctl]" << "[systemctlCall]" << "Could not find service"; if (debug) qDebug() << "[Netctl]" << "[systemctlCall]" << ":" << "Could not find service";
return false; return false;
} }
if ((sudo) && (sudoCommand == 0)) { if ((sudo) && (sudoCommand == 0)) {
if (debug) qDebug() << "[Netctl]" << "[systemctlCall]" << "Could not find sudo"; if (debug) qDebug() << "[Netctl]" << "[systemctlCall]" << ":" << "Could not find sudo";
return false; return false;
} }
if (systemctlCommand == 0) { if (systemctlCommand == 0) {
if (debug) qDebug() << "[Netctl]" << "[systemctlCall]" << "Could not find systemctl"; if (debug) qDebug() << "[Netctl]" << "[systemctlCall]" << ":" << "Could not find systemctl";
return false; return false;
} }
@ -210,7 +217,7 @@ QList<QStringList> Netctl::getProfileList()
{ {
if (debug) qDebug() << "[Netctl]" << "[getProfileList]"; if (debug) qDebug() << "[Netctl]" << "[getProfileList]";
if (profileDirectory == 0) { if (profileDirectory == 0) {
if (debug) qDebug() << "[Netctl]" << "[getProfileList]" << "Could not find directory"; if (debug) qDebug() << "[Netctl]" << "[getProfileList]" << ":" << "Could not find directory";
return QList<QStringList>(); return QList<QStringList>();
} }
@ -234,7 +241,7 @@ QList<QStringList> Netctl::getProfileListFromNetctlAuto()
{ {
if (debug) qDebug() << "[Netctl]" << "[getProfileListFromNetctlAuto]"; if (debug) qDebug() << "[Netctl]" << "[getProfileListFromNetctlAuto]";
if (netctlAutoCommand == 0) { 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<QStringList>(); return QList<QStringList>();
} }
@ -260,8 +267,9 @@ QList<QStringList> Netctl::getProfileListFromNetctlAuto()
QString Netctl::getProfileDescription(const QString profileName) QString Netctl::getProfileDescription(const QString profileName)
{ {
if (debug) qDebug() << "[Netctl]" << "[getProfileDescription]"; if (debug) qDebug() << "[Netctl]" << "[getProfileDescription]";
if (debug) qDebug() << "[Netctl]" << "[getProfileDescription]" << ":" << "Profile" << profileName;
if (profileDirectory == 0) { if (profileDirectory == 0) {
if (debug) qDebug() << "[Netctl]" << "[getProfileDescription]" << "Could not find directory"; if (debug) qDebug() << "[Netctl]" << "[getProfileDescription]" << ":" << "Could not find directory";
return QString(); return QString();
} }
@ -291,8 +299,9 @@ QString Netctl::getProfileDescription(const QString profileName)
QStringList Netctl::getProfileDescriptions(const QStringList profileList) QStringList Netctl::getProfileDescriptions(const QStringList profileList)
{ {
if (debug) qDebug() << "[Netctl]" << "[getProfileDescriptions]"; if (debug) qDebug() << "[Netctl]" << "[getProfileDescriptions]";
if (debug) qDebug() << "[Netctl]" << "[getProfileDescriptions]" << ":" << "Profile list" << profileList;
if (profileDirectory == 0) { if (profileDirectory == 0) {
if (debug) qDebug() << "[Netctl]" << "[getProfileDescriptions]" << "Could not find directory"; if (debug) qDebug() << "[Netctl]" << "[getProfileDescriptions]" << ":" << "Could not find directory";
return QStringList(); return QStringList();
} }
@ -328,6 +337,7 @@ QStringList Netctl::getProfileDescriptions(const QStringList profileList)
QString Netctl::getProfileStatus(const QString profile) QString Netctl::getProfileStatus(const QString profile)
{ {
if (debug) qDebug() << "[Netctl]" << "[getProfileStatus]"; if (debug) qDebug() << "[Netctl]" << "[getProfileStatus]";
if (debug) qDebug() << "[Netctl]" << "[getProfileStatus]" << ":" << "Profile" << profile;
QString status; QString status;
if (isProfileActive(profile)) if (isProfileActive(profile))
@ -346,6 +356,7 @@ QString Netctl::getProfileStatus(const QString profile)
QStringList Netctl::getProfileStatuses(const QStringList profileList) QStringList Netctl::getProfileStatuses(const QStringList profileList)
{ {
if (debug) qDebug() << "[Netctl]" << "[getProfileStatuses]"; if (debug) qDebug() << "[Netctl]" << "[getProfileStatuses]";
if (debug) qDebug() << "[Netctl]" << "[getProfileStatuses]" << ":" << "Profile list" << profileList;
QStringList statuses; QStringList statuses;
for (int i=0; i<profileList.count(); i++) { for (int i=0; i<profileList.count(); i++) {
@ -368,8 +379,9 @@ QStringList Netctl::getProfileStatuses(const QStringList profileList)
QString Netctl::getSsidFromProfile(const QString profile) QString Netctl::getSsidFromProfile(const QString profile)
{ {
if (debug) qDebug() << "[Netctl]" << "[getSsidFromProfile]"; if (debug) qDebug() << "[Netctl]" << "[getSsidFromProfile]";
if (debug) qDebug() << "[Netctl]" << "[getSsidFromProfile]" << ":" << "Profile" << profile;
if (profileDirectory == 0) { if (profileDirectory == 0) {
if (debug) qDebug() << "[Netctl]" << "[getSsidFromProfile]" << "Could not find directory"; if (debug) qDebug() << "[Netctl]" << "[getSsidFromProfile]" << ":" << "Could not find directory";
return QString(); return QString();
} }
@ -400,6 +412,7 @@ QString Netctl::getSsidFromProfile(const QString profile)
bool Netctl::isProfileActive(const QString profile) bool Netctl::isProfileActive(const QString profile)
{ {
if (debug) qDebug() << "[Netctl]" << "[isProfileActive]"; if (debug) qDebug() << "[Netctl]" << "[isProfileActive]";
if (debug) qDebug() << "[Netctl]" << "[isProfileActive]" << ":" << "Profile" << profile;
bool status = false; bool status = false;
QString cmdOutput = getNetctlOutput(false, QString("status"), profile); QString cmdOutput = getNetctlOutput(false, QString("status"), profile);
@ -414,6 +427,7 @@ bool Netctl::isProfileActive(const QString profile)
bool Netctl::isProfileEnabled(const QString profile) bool Netctl::isProfileEnabled(const QString profile)
{ {
if (debug) qDebug() << "[Netctl]" << "[isProfileEnabled]"; if (debug) qDebug() << "[Netctl]" << "[isProfileEnabled]";
if (debug) qDebug() << "[Netctl]" << "[isProfileEnabled]" << ":" << "Profile" << profile;
return netctlCall(false, QString("is-enabled"), profile); return netctlCall(false, QString("is-enabled"), profile);
} }
@ -422,6 +436,7 @@ bool Netctl::isProfileEnabled(const QString profile)
bool Netctl::autoIsProfileActive(const QString profile) bool Netctl::autoIsProfileActive(const QString profile)
{ {
if (debug) qDebug() << "[Netctl]" << "[autoIsProfileActive]"; if (debug) qDebug() << "[Netctl]" << "[autoIsProfileActive]";
if (debug) qDebug() << "[Netctl]" << "[autoIsProfileActive]" << ":" << "Profile" << profile;
bool status = false; bool status = false;
QList<QStringList> profiles = getProfileListFromNetctlAuto(); QList<QStringList> profiles = getProfileListFromNetctlAuto();
@ -438,6 +453,7 @@ bool Netctl::autoIsProfileActive(const QString profile)
bool Netctl::autoIsProfileEnabled(const QString profile) bool Netctl::autoIsProfileEnabled(const QString profile)
{ {
if (debug) qDebug() << "[Netctl]" << "[autoIsProfileEnabled]"; if (debug) qDebug() << "[Netctl]" << "[autoIsProfileEnabled]";
if (debug) qDebug() << "[Netctl]" << "[autoIsProfileEnabled]" << ":" << "Profile" << profile;
bool status = true; bool status = true;
QList<QStringList> profiles = getProfileListFromNetctlAuto(); QList<QStringList> profiles = getProfileListFromNetctlAuto();
@ -455,11 +471,11 @@ bool Netctl::isNetctlAutoEnabled()
{ {
if (debug) qDebug() << "[Netctl]" << "[isNetctlAutoEnabled]"; if (debug) qDebug() << "[Netctl]" << "[isNetctlAutoEnabled]";
if (netctlAutoService == 0) { if (netctlAutoService == 0) {
if (debug) qDebug() << "[Netctl]" << "[isNetctlAutoEnabled]" << "Could not find service"; if (debug) qDebug() << "[Netctl]" << "[isNetctlAutoEnabled]" << ":" << "Could not find service";
return false; return false;
} }
if (systemctlCommand == 0) { if (systemctlCommand == 0) {
if (debug) qDebug() << "[Netctl]" << "[isNetctlAutoEnabled]" << "Could not find systemctl"; if (debug) qDebug() << "[Netctl]" << "[isNetctlAutoEnabled]" << ":" << "Could not find systemctl";
return false; return false;
} }
@ -484,11 +500,11 @@ bool Netctl::isNetctlAutoRunning()
{ {
if (debug) qDebug() << "[Netctl]" << "[isNetctlAutoRunning]"; if (debug) qDebug() << "[Netctl]" << "[isNetctlAutoRunning]";
if (netctlAutoService == 0) { if (netctlAutoService == 0) {
if (debug) qDebug() << "[Netctl]" << "[isNetctlAutoRunning]" << "Could not find service"; if (debug) qDebug() << "[Netctl]" << "[isNetctlAutoRunning]" << ":" << "Could not find service";
return false; return false;
} }
if (systemctlCommand == 0) { if (systemctlCommand == 0) {
if (debug) qDebug() << "[Netctl]" << "[isNetctlAutoRunning]" << "Could not find systemctl"; if (debug) qDebug() << "[Netctl]" << "[isNetctlAutoRunning]" << ":" << "Could not find systemctl";
return false; return false;
} }
@ -513,6 +529,7 @@ bool Netctl::isNetctlAutoRunning()
bool Netctl::enableProfile(const QString profile) bool Netctl::enableProfile(const QString profile)
{ {
if (debug) qDebug() << "[Netctl]" << "[enableProfile]"; if (debug) qDebug() << "[Netctl]" << "[enableProfile]";
if (debug) qDebug() << "[Netctl]" << "[enableProfile]" << ":" << "Profile" << profile;
if (isProfileEnabled(profile)) if (isProfileEnabled(profile))
return netctlCall(true, QString("disable"), profile); return netctlCall(true, QString("disable"), profile);
@ -524,6 +541,7 @@ bool Netctl::enableProfile(const QString profile)
bool Netctl::restartProfile(const QString profile) bool Netctl::restartProfile(const QString profile)
{ {
if (debug) qDebug() << "[Netctl]" << "[restartProfile]"; if (debug) qDebug() << "[Netctl]" << "[restartProfile]";
if (debug) qDebug() << "[Netctl]" << "[restartProfile]" << ":" << "Profile" << profile;
return netctlCall(true, QString("restart"), profile); return netctlCall(true, QString("restart"), profile);
} }
@ -532,6 +550,7 @@ bool Netctl::restartProfile(const QString profile)
bool Netctl::startProfile(const QString profile) bool Netctl::startProfile(const QString profile)
{ {
if (debug) qDebug() << "[Netctl]" << "[startProfile]"; if (debug) qDebug() << "[Netctl]" << "[startProfile]";
if (debug) qDebug() << "[Netctl]" << "[startProfile]" << ":" << "Profile" << profile;
if (isProfileActive(profile)) if (isProfileActive(profile))
return netctlCall(true, QString("stop"), profile); return netctlCall(true, QString("stop"), profile);
@ -551,6 +570,7 @@ bool Netctl::autoDisableAllProfiles()
bool Netctl::autoEnableProfile(const QString profile) bool Netctl::autoEnableProfile(const QString profile)
{ {
if (debug) qDebug() << "[Netctl]" << "[autoEnableProfile]"; if (debug) qDebug() << "[Netctl]" << "[autoEnableProfile]";
if (debug) qDebug() << "[Netctl]" << "[autoEnableProfile]" << ":" << "Profile" << profile;
if (autoIsProfileEnabled(profile)) if (autoIsProfileEnabled(profile))
return netctlAutoCall(false, QString("disable"), profile); return netctlAutoCall(false, QString("disable"), profile);
@ -570,6 +590,7 @@ bool Netctl::autoEnableAllProfiles()
bool Netctl::autoStartProfile(const QString profile) bool Netctl::autoStartProfile(const QString profile)
{ {
if (debug) qDebug() << "[Netctl]" << "[autoStartProfile]"; if (debug) qDebug() << "[Netctl]" << "[autoStartProfile]";
if (debug) qDebug() << "[Netctl]" << "[autoStartProfile]" << ":" << "Profile" << profile;
if (autoIsProfileActive(profile)) if (autoIsProfileActive(profile))
return true; return true;

View File

@ -28,8 +28,10 @@
NetctlProfile::NetctlProfile(const bool debugCmd, const QMap<QString, QString> settings) NetctlProfile::NetctlProfile(const bool debugCmd, const QMap<QString, QString> settings)
: debug(debugCmd) : debug(debugCmd)
{ {
profileDirectory = new QDir(settings[QString("PROFILE_DIR")]); if (settings.contains(QString("PROFILE_DIR")))
sudoCommand = settings[QString("SUDO_PATH")]; 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) bool NetctlProfile::copyProfile(const QString oldPath)
{ {
if (debug) qDebug() << "[NetctlProfile]" << "[copyProfile]"; 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; QProcess command;
QString newPath = profileDirectory->absolutePath() + QDir::separator() + QFileInfo(oldPath).fileName();
QString commandText = sudoCommand + QString(" /usr/bin/mv ") + oldPath + QString(" ") + newPath; QString commandText = sudoCommand + QString(" /usr/bin/mv ") + oldPath + QString(" ") + newPath;
if (debug) qDebug() << "[NetctlProfile]" << "[copyProfile]" << ":" << "Run cmd" << commandText; if (debug) qDebug() << "[NetctlProfile]" << "[copyProfile]" << ":" << "Run cmd" << commandText;
command.start(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<QString, QString> settings) QString NetctlProfile::createProfile(const QString profile, const QMap<QString, QString> settings)
{ {
if (debug) qDebug() << "[NetctlProfile]" << "[createProfile]"; 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 profileTempName = QDir::homePath() + QDir::separator() +
QString(".cache") + QDir::separator() + QFileInfo(profile).fileName(); QString(".cache") + QDir::separator() + QFileInfo(profile).fileName();
QFile profileFile(profileTempName); QFile profileFile(profileTempName);
if (debug) qDebug() << "[NetctlProfile]" << "[createProfile]" << ":" << "Save to" << profileTempName; if (debug) qDebug() << "[NetctlProfile]" << "[createProfile]" << ":" << "Save to" << profileTempName;
if (!profileFile.open(QIODevice::WriteOnly | QIODevice::Text)) if (!profileFile.open(QIODevice::WriteOnly | QIODevice::Text))
return profileTempName; return profileTempName;
QTextStream out(&profileFile); QTextStream out(&profileFile);
for (int i=0; i<settings.keys().count(); i++) { for (int i=0; i<settings.keys().count(); i++) {
out << settings.keys()[i] << QString("="); out << settings.keys()[i] << QString("=");
@ -105,9 +95,9 @@ QString NetctlProfile::createProfile(const QString profile, const QMap<QString,
(settings.keys()[i] == QString("DNSOptions")) || (settings.keys()[i] == QString("DNSOptions")) ||
(settings.keys()[i] == QString("WPAConfigSection")) || (settings.keys()[i] == QString("WPAConfigSection")) ||
(settings.keys()[i] == QString("WPAConfigSection"))) (settings.keys()[i] == QString("WPAConfigSection")))
out << QString("(") + settings[settings.keys()[i]] << QString(")\n"); out << QString("(") + settings[settings.keys()[i]] << QString(")") << endl;
else else
out << settings[settings.keys()[i]] << QString("\n"); out << settings[settings.keys()[i]] << endl;
} }
profileFile.close(); profileFile.close();
@ -115,75 +105,94 @@ QString NetctlProfile::createProfile(const QString profile, const QMap<QString,
} }
QString NetctlProfile::getNameByString(const QString profile)
{
if (debug) qDebug() << "[NetctlProfile]" << "[getNameByString]";
if (debug) qDebug() << "[NetctlProfile]" << "[getNameByString]" << ":" << "Raw string" << profile;
return QFileInfo(profile).fileName();
}
QMap<QString, QString> NetctlProfile::getSettingsFromProfile(const QString profile) QMap<QString, QString> NetctlProfile::getSettingsFromProfile(const QString profile)
{ {
if (debug) qDebug() << "[NetctlProfile]" << "[getSettingsFromProfile]"; 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<QString, QString>();
}
QMap<QString, QString> settings; QMap<QString, QString> settings;
QString fileStr, profileUrl; QString fileStr, profileUrl;
profileUrl = profileDirectory->absolutePath() + QDir::separator() + QFileInfo(profile).fileName(); profileUrl = profileDirectory->absolutePath() + QDir::separator() + QFileInfo(profile).fileName();
QFile profileFile(profileUrl); QFile profileFile(profileUrl);
if (debug) qDebug() << "[NetctlProfile]" << "[getSettingsFromProfile]" << ":" << "Read from" << profileUrl; if (debug) qDebug() << "[NetctlProfile]" << "[getSettingsFromProfile]" << ":" << "Read from" << profileUrl;
if (!profileFile.open(QIODevice::ReadOnly)) if (!profileFile.open(QIODevice::ReadOnly))
return settings; return settings;
while (true) { while (true) {
fileStr = QString(profileFile.readLine()); fileStr = QString(profileFile.readLine());
if (fileStr[0] != '#') { if (fileStr[0] == QChar('#')) continue;
if (fileStr.split(QChar('='), QString::SkipEmptyParts).count() == 2) { if (fileStr.split(QChar('='), QString::SkipEmptyParts).count() != 2) continue;
if ((fileStr.split(QChar('='))[1][0] == QChar('(')) && if ((fileStr.split(QChar('='))[1][0] == QChar('(')) &&
(fileStr.split(QChar('='))[1][fileStr.split(QChar('='))[1].size()-2] == QChar(')'))) (fileStr.split(QChar('='))[1][fileStr.split(QChar('='))[1].size()-2] == QChar(')')))
settings[fileStr.split(QChar('='))[0]] = fileStr.split(QChar('='))[1] settings[fileStr.split(QChar('='))[0]] = fileStr.split(QChar('='))[1]
.remove(QString("(")) .remove(QChar('('))
.remove(QString(")")) .remove(QChar(')'))
.trimmed(); .trimmed();
else if (fileStr.split(QChar('='))[1][0] == QChar('(')) { else if (fileStr.split(QChar('='))[1][0] == QChar('(')) {
QString parameterName = fileStr.split(QChar('='))[0]; QString parameterName = fileStr.split(QChar('='))[0];
QStringList parameter; QStringList parameter;
if (!fileStr.split(QChar('='))[1] if (!fileStr.split(QChar('='))[1]
.remove(QString("(")) .remove(QChar('('))
.remove(QString(")")) .remove(QChar(')'))
.trimmed() .trimmed()
.isEmpty()) .isEmpty())
parameter.append(fileStr.split(QChar('='))[1] parameter.append(fileStr.split(QChar('='))[1]
.remove(QString("(")) .remove(QChar('('))
.remove(QString(")")) .remove(QChar(')'))
.trimmed()); .trimmed());
while(true) { while(true) {
fileStr = QString(profileFile.readLine()); fileStr = QString(profileFile.readLine());
if (fileStr[fileStr.size()-2] == QChar(')')) if (fileStr[fileStr.size()-2] == QChar(')'))
break; break;
if (!fileStr.remove(QString("(")) if (!fileStr.remove(QChar('('))
.remove(QString(")")) .remove(QChar(')'))
.trimmed() .trimmed()
.isEmpty()) .isEmpty())
parameter.append(fileStr.remove(QString("(")) parameter.append(fileStr.remove(QChar('('))
.remove(QString(")")) .remove(QChar(')'))
.trimmed()); .trimmed());
if (profileFile.atEnd()) if (profileFile.atEnd())
break; break;
}
settings[parameterName] = parameter.join(QString("\n"));
}
else
settings[fileStr.split(QChar('='))[0]] = fileStr.split(QChar('='))[1]
.trimmed();
} }
settings[parameterName] = parameter.join(QChar('\n'));
} }
else
settings[fileStr.split(QChar('='))[0]] = fileStr.split(QChar('='))[1]
.trimmed();
if (profileFile.atEnd()) if (profileFile.atEnd())
break; break;
} }
profileFile.close(); profileFile.close();
return settings; 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;
}