/*************************************************************************** * This file is part of netctl-plasmoid * * * * netctl-plasmoid 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-plasmoid 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-plasmoid. If not, see http://www.gnu.org/licenses/ * ***************************************************************************/ #include "netctl.h" #include #include #include #include Netctl::Netctl(QObject *parent, const QVariantList &args) : Plasma::DataEngine(parent, args) { Q_UNUSED(args) setMinimumPollingInterval(333); readConfiguration(); } QStringList Netctl::sources() const { QStringList sources; sources.append(QString("currentProfile")); sources.append(QString("extIp")); sources.append(QString("interfaces")); sources.append(QString("intIp")); sources.append(QString("profiles")); sources.append(QString("statusBool")); sources.append(QString("statusString")); return sources; } bool Netctl::readConfiguration() { // default configuration checkExtIP = QString("false"); cmd = QString("/usr/bin/netctl"); extIpCmd = QString("wget -qO- http://ifconfig.me/ip"); ipCmd = QString("/usr/bin/ip"); netDir = QString("/sys/class/net/"); QString fileStr; // FIXME: define configuration file QFile confFile(QString(getenv("HOME")) + QString("/.kde4/share/config/netctl.conf")); bool exists = confFile.open(QIODevice::ReadOnly); if (!exists) { confFile.setFileName("/usr/share/config/netctl.conf"); exists = confFile.open(QIODevice::ReadOnly); if (!exists) return false; } while (true) { fileStr = QString(confFile.readLine()); if (fileStr[0] != '#') { if (fileStr.split(QString("="), QString::SkipEmptyParts).count() == 2) { if (fileStr.split(QString("="), QString::SkipEmptyParts)[0] == QString("EXTIP")) checkExtIP = fileStr.split(QString("="), QString::SkipEmptyParts)[1].split(QString("\n"), QString::SkipEmptyParts)[0]; else if (fileStr.split(QString("="), QString::SkipEmptyParts)[0] == QString("CMD")) cmd = fileStr.split(QString("="), QString::SkipEmptyParts)[1].split(QString("\n"), QString::SkipEmptyParts)[0]; else if (fileStr.split(QString("="), QString::SkipEmptyParts)[0] == QString("EXTIPCMD")) extIpCmd = fileStr.split(QString("="), QString::SkipEmptyParts)[1].split(QString("\n"), QString::SkipEmptyParts)[0]; else if (fileStr.split(QString("="), QString::SkipEmptyParts)[0] == QString("IPCMD")) ipCmd = fileStr.split(QString("="), QString::SkipEmptyParts)[1].split(QString("\n"), QString::SkipEmptyParts)[0]; else if (fileStr.split(QString("="), QString::SkipEmptyParts)[0] == QString("NETDIR")) netDir = fileStr.split(QString("="), QString::SkipEmptyParts)[1].split(QString("\n"), QString::SkipEmptyParts)[0]; } } if (confFile.atEnd()) break; } confFile.close(); return true; } bool Netctl::sourceRequestEvent(const QString &name) { return updateSourceEvent(name); } bool Netctl::updateSourceEvent(const QString &source) { QProcess command; QString cmdOutput = QString(""); QString value = QString(""); if (source == QString("currentProfile")) { command.start(cmd + QString(" list")); command.waitForFinished(-1); cmdOutput = command.readAllStandardOutput(); if (!cmdOutput.isEmpty()) { QStringList profileList = cmdOutput.split(QString("\n"), QString::SkipEmptyParts); for (int i=0; i