refactoring of dataengine

This commit is contained in:
arcan1s 2014-04-12 16:31:19 +04:00
parent 92ec04cae1
commit 2778f654ef
4 changed files with 130 additions and 102 deletions

View File

@ -7,8 +7,8 @@ cmake_policy (SET CMP0015 NEW)
project (netctl-gui) project (netctl-gui)
set (PROJECT_VERSION_MAJOR 1) set (PROJECT_VERSION_MAJOR 1)
set (PROJECT_VERSION_MINOR 0) set (PROJECT_VERSION_MINOR 1)
set (PROJECT_VERSION_PATCH 4) set (PROJECT_VERSION_PATCH 0)
set (PROJECT_VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}) set (PROJECT_VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH})
configure_file (${CMAKE_SOURCE_DIR}/version.h.in ${CMAKE_CURRENT_BINARY_DIR}/version.h) configure_file (${CMAKE_SOURCE_DIR}/version.h.in ${CMAKE_CURRENT_BINARY_DIR}/version.h)

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="1">
<libraries>
<library>qt</library>
</libraries>
</project>

View File

@ -23,6 +23,7 @@
#include <QDir> #include <QDir>
#include <QFile> #include <QFile>
#include <QProcess> #include <QProcess>
#include <QTextCodec>
Netctl::Netctl(QObject *parent, const QVariantList &args) Netctl::Netctl(QObject *parent, const QVariantList &args)
@ -114,122 +115,136 @@ bool Netctl::sourceRequestEvent(const QString &name)
} }
bool Netctl::updateSourceEvent(const QString &source) QString Netctl::getCurrentProfile(const QString cmd)
{ {
QProcess command; QProcess command;
QString cmdOutput = QString(""); QString profile = QString("");
QString value = QString(""); command.start(cmd + QString(" list"));
command.waitForFinished(-1);
if (source == QString("currentProfile")) { QString cmdOutput = QTextCodec::codecForMib(106)->toUnicode(command.readAllStandardOutput());
command.start(configuration[QString("CMD")] + QString(" list")); QStringList profileList = cmdOutput.split(QChar('\n'), QString::SkipEmptyParts);
command.waitForFinished(-1); for (int i=0; i<profileList.count(); i++)
cmdOutput = command.readAllStandardOutput(); if (profileList[i][0] == QChar('*')) {
if (!cmdOutput.isEmpty()) { profile = profileList[i];
QStringList profileList = cmdOutput.split(QString("\n"), QString::SkipEmptyParts); break;
for (int i=0; i<profileList.count(); i++)
if (profileList[i].split(QString(" "), QString::SkipEmptyParts).count() == 2) {
value = profileList[i].split(QString(" "), QString::SkipEmptyParts)[1];
break;
}
} }
setData(source, QString("value"), value); profile.remove(0, 1);
return profile;
}
QString Netctl::getExtIp(const QString 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)
{
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)
{
QProcess command;
QString intIp = QString("127.0.0.1/8");
QStringList interfaceList = getInterfaceList(dir);
for (int i=0; i<interfaceList.count(); i++)
if (interfaceList[i] != QString("lo")) {
command.start(cmd + QString(" addr show ") + interfaceList[i]);
command.waitForFinished(-1);
QString cmdOutput = QTextCodec::codecForMib(106)->toUnicode(command.readAllStandardOutput());
QStringList deviceInfo = cmdOutput.split(QChar('\n'), QString::SkipEmptyParts);
for (int j=0; j<deviceInfo.count(); j++)
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)
{
QProcess command;
command.start(cmd + QString(" list"));
command.waitForFinished(-1);
QString cmdOutput = QTextCodec::codecForMib(106)->toUnicode(command.readAllStandardOutput());
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)
{
bool status = false;
QString cmdOutput = getCurrentProfile(cmd);
if (!cmdOutput.isEmpty())
status = true;
return status;
}
QString Netctl::getProfileStringStatus(const QString cmd)
{
QProcess command;
QString status = QString("static");
QString profile = getCurrentProfile(cmd);
command.start(cmd + QString(" status ") + profile);
command.waitForFinished(-1);
QString cmdOutput = QTextCodec::codecForMib(106)->toUnicode(command.readAllStandardOutput());
QStringList profileStatus = cmdOutput.split(QChar('\n'), QString::SkipEmptyParts);
for (int i=0; i<profileStatus.count(); i++)
if (profileStatus[i].split(QChar(' '), QString::SkipEmptyParts)[0] == QString("Loaded:")) {
if (profileStatus[i].contains(QString("enabled")))
status = QString("enabled");
break;
}
return status;
}
bool Netctl::updateSourceEvent(const QString &source)
{
QString key = QString("value");
QString value = QString("");
if (source == QString("currentProfile")) {
value = getCurrentProfile(configuration[QString("CMD")]);
} }
else if (source == QString("extIp")) { else if (source == QString("extIp")) {
if (configuration[QString("EXTIP")] == QString("true")) { if (configuration[QString("EXTIP")] == QString("true"))
command.start(configuration[QString("EXTIPCMD")]); value = getExtIp(configuration[QString("EXTIPCMD")]);
command.waitForFinished(-1);
cmdOutput = command.readAllStandardOutput();
if (!cmdOutput.isEmpty())
value = cmdOutput.split(QString("\n"), QString::SkipEmptyParts)[0];
}
setData(source, QString("value"), value);
} }
else if (source == QString("interfaces")) { else if (source == QString("interfaces")) {
if (QDir(configuration[QString("NETDIR")]).exists()) value = getInterfaceList(configuration[QString("NETDIR")]).join(QChar(','));
value = QDir(configuration[QString("NETDIR")]).entryList(QDir::Dirs | QDir::NoDotAndDotDot).join(QString(","));
setData(source, QString("value"), value);
} }
else if (source == QString("intIp")) { else if (source == QString("intIp")) {
if (QDir(configuration[QString("NETDIR")]).exists()) { value = getIntIp(configuration[QString("IPCMD")], configuration[QString("NETDIR")]);
value = QString("127.0.0.1/8");
QStringList netDevices = QDir(configuration[QString("NETDIR")]).entryList(QDir::Dirs | QDir::NoDotAndDotDot);
for (int i=0; i<netDevices.count(); i++)
if (netDevices[i] != QString("lo")) {
cmdOutput = QString("");
command.start(configuration[QString("IPCMD")] + QString(" addr show ") + netDevices[i]);
command.waitForFinished(-1);
cmdOutput = command.readAllStandardOutput();
if (!cmdOutput.isEmpty()) {
QStringList deviceInfo = cmdOutput.split(QString("\n"), QString::SkipEmptyParts);
for (int j=0; j<deviceInfo.count(); j++)
if (deviceInfo[j].split(QString(" "), QString::SkipEmptyParts)[0] == QString("inet"))
value = deviceInfo[j].split(QString(" "), QString::SkipEmptyParts)[1];
}
}
}
setData(source, QString("value"), value);
} }
else if (source == QString("profiles")) { else if (source == QString("profiles")) {
command.start(configuration[QString("CMD")] + QString(" list")); value = getProfileList(configuration[QString("CMD")]).join(QChar(','));
command.waitForFinished(-1);
cmdOutput = command.readAllStandardOutput();
QStringList list;
if (!cmdOutput.isEmpty()) {
QStringList profileList = cmdOutput.split(QString("\n"), QString::SkipEmptyParts);
for (int i=0; i<profileList.count(); i++)
if (profileList[i].split(QString(" "), QString::SkipEmptyParts).count() == 1)
list.append(profileList[i].split(QString(" "), QString::SkipEmptyParts)[0]);
else if (profileList[i].split(QString(" "), QString::SkipEmptyParts).count() == 2)
list.append(profileList[i].split(QString(" "), QString::SkipEmptyParts)[1]);
}
value = list.join(QString(","));
setData(source, QString("value"), value);
} }
else if (source == QString("statusBool")) { else if (source == QString("statusBool")) {
command.start(configuration[QString("CMD")] + QString(" list")); if (getProfileStatus(configuration[QString("CMD")]))
command.waitForFinished(-1); value = QString("true");
cmdOutput = command.readAllStandardOutput(); else
value = QString("false"); value = QString("false");
if (!cmdOutput.isEmpty()) {
QStringList profileList = cmdOutput.split(QString("\n"), QString::SkipEmptyParts);
for (int i=0; i<profileList.count(); i++)
if (profileList[i].split(QString(" "), QString::SkipEmptyParts).count() == 2) {
value = QString("true");
break;
}
}
setData(source, QString("value"), value);
} }
else if (source == QString("statusString")) { else if (source == QString("statusString")) {
command.start(configuration[QString("CMD")] + QString(" list")); value = getProfileStringStatus(configuration[QString("CMD")]);
command.waitForFinished(-1);
cmdOutput = command.readAllStandardOutput();
QString currentProfile;
if (!cmdOutput.isEmpty()) {
QStringList profileList = cmdOutput.split(QString("\n"), QString::SkipEmptyParts);
for (int i=0; i<profileList.count(); i++)
if (profileList[i].split(QString(" "), QString::SkipEmptyParts).count() == 2) {
currentProfile = profileList[i].split(QString(" "), QString::SkipEmptyParts)[1];
break;
}
}
command.start(configuration[QString("CMD")] + QString(" status ") + currentProfile);
command.waitForFinished(-1);
cmdOutput = command.readAllStandardOutput();
if (!cmdOutput.isEmpty()) {
QStringList profile = cmdOutput.split(QString("\n"), QString::SkipEmptyParts);
for (int i=0; i<profile.count(); i++)
if (profile[i].split(QString(" "), QString::SkipEmptyParts)[0] == QString("Loaded:")) {
if (profile[i].contains(QString("enabled")))
value = QString("enabled");
else if (profile[i].contains(QString("static")))
value = QString("static");
break;
}
}
setData(source, QString("value"), value);
} }
setData(source, key, value);
return true; return true;
} }

View File

@ -27,6 +27,13 @@ class Netctl : public Plasma::DataEngine
public: public:
Netctl(QObject *parent, const QVariantList &args); Netctl(QObject *parent, const QVariantList &args);
QString getCurrentProfile(const QString cmd);
QString getExtIp(const QString cmd);
QStringList getInterfaceList(const QString dir);
QString getIntIp(const QString cmd, const QString dir);
QStringList getProfileList(const QString cmd);
bool getProfileStatus(const QString cmd);
QString getProfileStringStatus(const QString cmd);
protected: protected:
bool sourceRequestEvent(const QString &name); bool sourceRequestEvent(const QString &name);