/*************************************************************************** * This file is part of netctl-gui * * * * netctl-gui is free software: you can redistribute it and/or * * modify it under the terms of the GNU General Public License as * * published by the Free Software Foundation, either version 3 of the * * License, or (at your option) any later version. * * * * netctl-gui is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with netctl-gui. If not, see http://www.gnu.org/licenses/ * ***************************************************************************/ /** * @file netctlprofile.cpp * Source code of netctlgui library * @author Evgeniy Alekseev * @copyright GPLv3 * @bug https://github.com/arcan1s/netctl-gui/issues */ #include #include #include #include #include #include #include #include /** * @class NetctlProfile */ /** * @fn NetctlProfile */ NetctlProfile::NetctlProfile(const bool debugCmd, const QMap settings) : debug(debugCmd) { if (settings.contains(QString("PROFILE_DIR"))) profileDirectory = new QDir(settings[QString("PROFILE_DIR")]); else profileDirectory = new QDir(QString("/etc/netctl/")); if (settings.contains(QString("SUDO_PATH"))) sudoCommand = settings[QString("SUDO_PATH")]; if (settings.contains(QString("FORCE_SUDO"))) if (settings[QString("FORCE_SUDO")] == QString("true")) useSuid = false; if (useSuid) sudoCommand = QString(""); } /** * @fn ~NetctlProfile */ NetctlProfile::~NetctlProfile() { if (debug) qDebug() << PDEBUG; if (profileDirectory != nullptr) delete profileDirectory; } /** * @fn copyProfile */ bool NetctlProfile::copyProfile(const QString oldPath) { if (debug) qDebug() << PDEBUG; if (debug) qDebug() << PDEBUG << ":" << "Path" << oldPath; if (profileDirectory == 0) { if (debug) qDebug() << PDEBUG << ":" << "Could not find directory"; return false; } QString newPath = profileDirectory->absolutePath() + QDir::separator() + QFileInfo(oldPath).fileName(); QString cmd = sudoCommand + QString(" /usr/bin/mv \"") + oldPath + QString("\" \"") + newPath + QString("\""); if (debug) qDebug() << PDEBUG << ":" << "Run cmd" << cmd; TaskResult process = runTask(cmd, useSuid); if (debug) qDebug() << PDEBUG << ":" << "Cmd returns" << process.exitCode; if (process.exitCode != 0) if (debug) qDebug() << PDEBUG << ":" << "Error" << process.error; if (process.exitCode == 0) return true; else return false; } /** * @fn createProfile */ QString NetctlProfile::createProfile(const QString profile, const QMap settings) { if (debug) qDebug() << PDEBUG; if (debug) qDebug() << PDEBUG << ":" << "Profile" << profile; QString profileTempName = QDir::tempPath() + QDir::separator() + QFileInfo(profile).fileName(); QFile profileFile(profileTempName); if (debug) qDebug() << PDEBUG << ":" << "Save to" << profileTempName; if (!profileFile.open(QIODevice::WriteOnly | QIODevice::Text)) return profileTempName; QTextStream out(&profileFile); for (int i=0; i NetctlProfile::getRecommendedConfiguration() { QMap settings; QString cmd; TaskResult process; QStringList recommended; // force sudo // find out helper exe settings[QString("FORCE_SUDO")] = QString("true"); recommended.clear(); recommended.append(QString("netctlgui-helper")); recommended.append(QString("netctlgui-helper-suid")); for (int i=0; i NetctlProfile::getSettingsFromProfile(const QString profile) { if (debug) qDebug() << PDEBUG; if (debug) qDebug() << PDEBUG << ":" << "Profile" << profile; if (profileDirectory == 0) { if (debug) qDebug() << PDEBUG << ":" << "Could not find directory"; return QMap(); } // getting variables list // system variables QString cmd = QString("env -i bash -c \"set\""); if (debug) qDebug() << PDEBUG << ":" << "Run cmd" << cmd; TaskResult process = runTask(cmd, false); if (debug) qDebug() << PDEBUG << ":" << "Cmd returns" << process.exitCode; if (process.exitCode != 0) if (debug) qDebug() << PDEBUG << ":" << "Error" << process.error; QStringList output = QString(process.output).trimmed().split(QChar('\n')); QStringList systemVariables; systemVariables.append(QString("PIPESTATUS")); for (int i=0; i settings = getSettingsFromProfile(profile); if (settings.contains(key)) return settings[key]; else return QString(); } /** * @fn removeProfile */ bool NetctlProfile::removeProfile(const QString profile) { if (debug) qDebug() << PDEBUG; if (debug) qDebug() << PDEBUG << ":" << "Profile" << profile; if (profileDirectory == 0) { if (debug) qDebug() << PDEBUG << ":" << "Could not find directory"; return false; } QString profilePath = profileDirectory->absolutePath() + QDir::separator() + QFileInfo(profile).fileName(); QString cmd = sudoCommand + QString(" /usr/bin/rm \"") + profilePath + QString("\""); if (debug) qDebug() << PDEBUG << ":" << "Run cmd" << cmd; TaskResult process = runTask(cmd, useSuid); if (debug) qDebug() << PDEBUG << ":" << "Cmd returns" << process.exitCode; if (process.exitCode != 0) if (debug) qDebug() << PDEBUG << ":" << "Error" << process.error; if (process.exitCode == 0) return true; else return false; }