mirror of
https://github.com/arcan1s/netctl-gui.git
synced 2025-07-10 04:15:52 +00:00
add debug information
fix reading files
This commit is contained in:
@ -20,9 +20,11 @@
|
||||
#include <KGlobal>
|
||||
#include <KStandardDirs>
|
||||
#include <Plasma/DataContainer>
|
||||
#include <QDebug>
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QProcess>
|
||||
#include <QProcessEnvironment>
|
||||
#include <QTextCodec>
|
||||
|
||||
|
||||
@ -31,6 +33,14 @@ Netctl::Netctl(QObject *parent, const QVariantList &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();
|
||||
}
|
||||
@ -38,8 +48,9 @@ Netctl::Netctl(QObject *parent, const QVariantList &args)
|
||||
|
||||
QStringList Netctl::sources() const
|
||||
{
|
||||
QStringList sources;
|
||||
if (debug) qDebug() << "[DE]" << "[sources]";
|
||||
|
||||
QStringList sources;
|
||||
sources.append(QString("currentProfile"));
|
||||
sources.append(QString("extIp"));
|
||||
sources.append(QString("interfaces"));
|
||||
@ -54,6 +65,8 @@ QStringList Netctl::sources() const
|
||||
|
||||
void Netctl::readConfiguration()
|
||||
{
|
||||
if (debug) qDebug() << "[DE]" << "[readConfiguration]";
|
||||
|
||||
// default configuration
|
||||
QMap<QString, QString> rawConfig;
|
||||
rawConfig[QString("CMD")] = QString("/usr/bin/netctl");
|
||||
@ -64,6 +77,7 @@ void Netctl::readConfiguration()
|
||||
rawConfig[QString("NETCTLAUTOCMD")] = QString("/usr/bin/netctl-auto");
|
||||
|
||||
QString fileName = KGlobal::dirs()->findResource("config", "netctl.conf");
|
||||
if (debug) qDebug() << "[DE]" << "[readConfiguration]" << ":" << "Configuration file" << fileName;
|
||||
QFile confFile(fileName);
|
||||
if (!confFile.open(QIODevice::ReadOnly)) {
|
||||
configuration = updateConfiguration(rawConfig);
|
||||
@ -73,24 +87,27 @@ void Netctl::readConfiguration()
|
||||
QStringList value;
|
||||
while (true) {
|
||||
fileStr = QString(confFile.readLine()).trimmed();
|
||||
if (fileStr[0] == QChar('#')) continue;
|
||||
if (fileStr[0] == QChar(';')) continue;
|
||||
if (!fileStr.contains(QChar('='))) continue;
|
||||
if ((fileStr.isEmpty()) && (!confFile.atEnd())) continue;
|
||||
if ((fileStr[0] == QChar('#')) && (!confFile.atEnd())) continue;
|
||||
if ((fileStr[0] == QChar(';')) && (!confFile.atEnd())) continue;
|
||||
if ((!fileStr.contains(QChar('='))) && (!confFile.atEnd())) continue;
|
||||
value.clear();
|
||||
for (int i=1; i<fileStr.split(QChar('=')).count(); i++)
|
||||
value.append(fileStr.split(QChar('='))[i]);
|
||||
rawConfig[fileStr.split(QChar('='))[0]] = value.join(QChar('='));
|
||||
if (confFile.atEnd())
|
||||
break;
|
||||
if (confFile.atEnd()) break;
|
||||
}
|
||||
confFile.close();
|
||||
configuration = updateConfiguration(rawConfig);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
QMap<QString, QString> Netctl::updateConfiguration(const QMap<QString, QString> rawConfig)
|
||||
{
|
||||
if (debug) qDebug() << "[DE]" << "[updateConfiguration]";
|
||||
|
||||
QMap<QString, QString> config;
|
||||
QString key, value;
|
||||
// remove spaces and copy source map
|
||||
@ -105,18 +122,29 @@ QMap<QString, QString> Netctl::updateConfiguration(const QMap<QString, QString>
|
||||
value.remove(QChar(' '));
|
||||
config[key] = value;
|
||||
}
|
||||
|
||||
for (int i=0; i<config.keys().count(); i++)
|
||||
if (debug) qDebug() << "[DE]" << "[updateConfiguration]" << ":" <<
|
||||
config.keys()[i] + QString("=") + config[config.keys()[i]];
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
|
||||
bool Netctl::sourceRequestEvent(const QString &name)
|
||||
{
|
||||
if (debug) qDebug() << "[DE]" << "[sourceRequestEvent]";
|
||||
if (debug) qDebug() << "[DE]" << "[sourceRequestEvent]" << ":" << "Source name" << name;
|
||||
|
||||
return updateSourceEvent(name);
|
||||
}
|
||||
|
||||
|
||||
QString Netctl::getCurrentProfile(const QString cmd)
|
||||
{
|
||||
if (debug) qDebug() << "[DE]" << "[getCurrentProfile]";
|
||||
if (debug) qDebug() << "[DE]" << "[getCurrentProfile]" << ":" << "Cmd" << cmd;
|
||||
|
||||
QProcess command;
|
||||
QString profile = QString("");
|
||||
command.start(cmd + QString(" list"));
|
||||
@ -129,33 +157,46 @@ QString Netctl::getCurrentProfile(const QString cmd)
|
||||
break;
|
||||
}
|
||||
profile.remove(0, 1);
|
||||
|
||||
return profile;
|
||||
}
|
||||
|
||||
|
||||
QString Netctl::getExtIp(const QString cmd)
|
||||
{
|
||||
if (debug) qDebug() << "[DE]" << "[getExtIp]";
|
||||
if (debug) qDebug() << "[DE]" << "[getExtIp]" << ":" << "Cmd" << cmd;
|
||||
|
||||
QProcess command;
|
||||
QString extIp = QString("");
|
||||
command.start(cmd);
|
||||
command.waitForFinished(-1);
|
||||
QString cmdOutput = QTextCodec::codecForMib(106)->toUnicode(command.readAllStandardOutput());
|
||||
extIp = cmdOutput.trimmed();
|
||||
|
||||
return extIp;
|
||||
}
|
||||
|
||||
|
||||
QStringList Netctl::getInterfaceList(const QString dir)
|
||||
{
|
||||
if (debug) qDebug() << "[DE]" << "[getInterfaceList]";
|
||||
if (debug) qDebug() << "[DE]" << "[getInterfaceList]" << ":" << "Directory" << dir;
|
||||
|
||||
QStringList interfaceList;
|
||||
if (QDir(dir).exists())
|
||||
interfaceList = QDir(dir).entryList(QDir::Dirs | QDir::NoDotAndDotDot);
|
||||
|
||||
return interfaceList;
|
||||
}
|
||||
|
||||
|
||||
QString Netctl::getIntIp(const QString cmd, const QString dir)
|
||||
{
|
||||
if (debug) qDebug() << "[DE]" << "[getIntIp]";
|
||||
if (debug) qDebug() << "[DE]" << "[getIntIp]" << ":" << "Cmd" << cmd;
|
||||
if (debug) qDebug() << "[DE]" << "[getIntIp]" << ":" << "Directory" << dir;
|
||||
|
||||
QProcess command;
|
||||
QString intIp = QString("127.0.0.1/8");
|
||||
QStringList interfaceList = getInterfaceList(dir);
|
||||
@ -169,12 +210,16 @@ QString Netctl::getIntIp(const QString cmd, const QString dir)
|
||||
if (deviceInfo[j].split(QChar(' '), QString::SkipEmptyParts)[0] == QString("inet"))
|
||||
intIp = deviceInfo[j].split(QChar(' '), QString::SkipEmptyParts)[1];
|
||||
}
|
||||
|
||||
return intIp;
|
||||
}
|
||||
|
||||
|
||||
QStringList Netctl::getProfileList(const QString cmd)
|
||||
{
|
||||
if (debug) qDebug() << "[DE]" << "[getProfileList]";
|
||||
if (debug) qDebug() << "[DE]" << "[getProfileList]" << ":" << "Cmd" << cmd;
|
||||
|
||||
QProcess command;
|
||||
command.start(cmd + QString(" list"));
|
||||
command.waitForFinished(-1);
|
||||
@ -182,22 +227,31 @@ QStringList Netctl::getProfileList(const QString cmd)
|
||||
QStringList profileList = cmdOutput.split(QChar('\n'), QString::SkipEmptyParts);
|
||||
for (int i=0; i<profileList.count(); i++)
|
||||
profileList[i].remove(0, 1);
|
||||
|
||||
return profileList;
|
||||
}
|
||||
|
||||
|
||||
bool Netctl::getProfileStatus(const QString cmd)
|
||||
{
|
||||
if (debug) qDebug() << "[DE]" << "[getProfileStatus]";
|
||||
if (debug) qDebug() << "[DE]" << "[getProfileStatus]" << ":" << "Cmd" << cmd;
|
||||
|
||||
bool status = false;
|
||||
QString cmdOutput = getCurrentProfile(cmd);
|
||||
if (!cmdOutput.isEmpty())
|
||||
status = true;
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
QString Netctl::getProfileStringStatus(const QString cmdNetctl, const QString cmdNetctlAuto)
|
||||
{
|
||||
if (debug) qDebug() << "[DE]" << "[getProfileStringStatus]";
|
||||
if (debug) qDebug() << "[DE]" << "[getProfileStringStatus]" << ":" << "Cmd" << cmdNetctl;
|
||||
if (debug) qDebug() << "[DE]" << "[getProfileStringStatus]" << ":" << "Cmd" << cmdNetctlAuto;
|
||||
|
||||
QProcess command;
|
||||
QString status = QString("static");
|
||||
// check netctl-auto
|
||||
@ -210,12 +264,16 @@ QString Netctl::getProfileStringStatus(const QString cmdNetctl, const QString cm
|
||||
if (command.exitCode() == 0)
|
||||
status = QString("enabled");
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
bool Netctl::updateSourceEvent(const QString &source)
|
||||
{
|
||||
if (debug) qDebug() << "[DE]" << "[updateSourceEvent]";
|
||||
if (debug) qDebug() << "[DE]" << "[updateSourceEvent]" << ":" << "Source name" << source;
|
||||
|
||||
QString key = QString("value");
|
||||
QString value = QString("");
|
||||
if (source == QString("currentProfile")) {
|
||||
@ -251,6 +309,7 @@ bool Netctl::updateSourceEvent(const QString &source)
|
||||
configuration[QString("NETCTLAUTOCMD")]);
|
||||
}
|
||||
setData(source, key, value);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user