/*************************************************************************** * 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/ * ***************************************************************************/ #include "netctl.h" #include #include #include #include #include #include #include #include #include #include Netctl::Netctl(QObject *parent, const QVariantList &args) : Plasma::DataEngine(parent, args) { Q_UNUSED(args) // debug QProcessEnvironment environment = QProcessEnvironment::systemEnvironment(); QString debugEnv = environment.value(QString("NETCTLGUI_DEBUG"), QString("no")); if (debugEnv == QString("yes")) debug = true; else debug = false; setMinimumPollingInterval(333); readConfiguration(); } Netctl::~Netctl() { if (debug) qDebug() << PDEBUG; } QStringList Netctl::sources() const { if (debug) qDebug() << PDEBUG; QStringList sources; sources.append(QString("active")); sources.append(QString("current")); sources.append(QString("extip4")); sources.append(QString("extip6")); sources.append(QString("interfaces")); sources.append(QString("intip4")); sources.append(QString("intip6")); sources.append(QString("netctlauto")); sources.append(QString("profiles")); sources.append(QString("status")); return sources; } void Netctl::readConfiguration() { if (debug) qDebug() << PDEBUG; // default configuration QMap rawConfig; rawConfig[QString("EXTIP4")] = QString("false"); rawConfig[QString("EXTIP4CMD")] = QString("curl ip4.telize.com"); rawConfig[QString("EXTIP6")] = QString("false"); rawConfig[QString("EXTIP6CMD")] = QString("curl ip6.telize.com"); rawConfig[QString("NETCTLCMD")] = QString("/usr/bin/netctl"); rawConfig[QString("NETCTLAUTOCMD")] = QString("/usr/bin/netctl-auto"); QString fileName = KGlobal::dirs()->findResource("config", "netctl.conf"); if (debug) qDebug() << PDEBUG << ":" << "Configuration file" << fileName; QFile configFile(fileName); if (!configFile.open(QIODevice::ReadOnly)) { configuration = updateConfiguration(rawConfig); return; } QString fileStr; QStringList value; while (true) { fileStr = QString(configFile.readLine()).trimmed(); if ((fileStr.isEmpty()) && (!configFile.atEnd())) continue; if ((fileStr[0] == QChar('#')) && (!configFile.atEnd())) continue; if ((fileStr[0] == QChar(';')) && (!configFile.atEnd())) continue; if (fileStr.contains(QChar('='))) { value.clear(); for (int i=1; i Netctl::updateConfiguration(const QMap rawConfig) { if (debug) qDebug() << PDEBUG; QMap config; QString key, value; // remove spaces and copy source map for (int i=0; itoUnicode(process.output); QStringList profileList = cmdOutput.split(QChar('\n'), QString::SkipEmptyParts); for (int i=0; itoUnicode(process.output).trimmed(); return extIp; } QStringList Netctl::getInterfaceList() { if (debug) qDebug() << PDEBUG; QList rawList = QNetworkInterface::allInterfaces(); QStringList interfacesList; for (int i=0; i rawList = QNetworkInterface::allAddresses(); for (int i=0; itoUnicode(process.output); if (cmdOutput.isEmpty()) { netctlAutoStatus = false; status = QString("false"); } else { netctlAutoStatus = true; status = QString("true"); } return status; } QStringList Netctl::getProfileList(const QString cmdNetctl, const QString cmdNetctlAuto) { if (debug) qDebug() << PDEBUG; getNetctlAutoStatus(cmdNetctlAuto); QString cmd; if (netctlAutoStatus) cmd = cmdNetctlAuto; else cmd = cmdNetctl; if (debug) qDebug() << PDEBUG << ":" << "Cmd" << cmd; TaskResult process = runTask(cmd + QString(" list")); if (debug) qDebug() << PDEBUG << ":" << "Cmd returns" << process.exitCode; if (process.exitCode != 0) if (debug) qDebug() << PDEBUG << ":" << "Error" << process.error; QString cmdOutput = QTextCodec::codecForMib(106)->toUnicode(process.output); QStringList profileList = cmdOutput.split(QChar('\n'), QString::SkipEmptyParts); for (int i=0; i