add task definitions to dataengine

This commit is contained in:
arcan1s 2014-08-05 16:55:23 +04:00
parent 7ed3e4e01b
commit 1abc4a5ac9
5 changed files with 485 additions and 200 deletions

View File

@ -41,8 +41,7 @@ endif ()
# flags # flags
if (CMAKE_COMPILER_IS_GNUCXX) if (CMAKE_COMPILER_IS_GNUCXX)
set (ADD_CXX_FLAGS "-Wall") set (CMAKE_CXX_FLAGS "-Wall -std=c++11")
set (CMAKE_CXX_FLAGS "-O0 ${ADD_CXX_FLAGS}")
set (CMAKE_CXX_FLAGS_DEBUG "-g -O0") set (CMAKE_CXX_FLAGS_DEBUG "-g -O0")
set (CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG") set (CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG")
else () else ()

View File

@ -13,13 +13,15 @@ set (PLUGIN_NAME ${SUBPROJECT})
file (GLOB SUBPROJECT_DESKTOP_IN *.desktop) file (GLOB SUBPROJECT_DESKTOP_IN *.desktop)
file (RELATIVE_PATH SUBPROJECT_DESKTOP ${CMAKE_SOURCE_DIR} ${SUBPROJECT_DESKTOP_IN}) file (RELATIVE_PATH SUBPROJECT_DESKTOP ${CMAKE_SOURCE_DIR} ${SUBPROJECT_DESKTOP_IN})
file (GLOB SUBPROJECT_SOURCE *.cpp) file (GLOB SUBPROJECT_SOURCE *.cpp)
set (TASK_HEADER ${CMAKE_SOURCE_DIR}/task.h)
file (GLOB SUBPROJECT_CONF *.conf) file (GLOB SUBPROJECT_CONF *.conf)
# prepare # prepare
configure_file (${SUBPROJECT_DESKTOP_IN} ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT_DESKTOP}) configure_file (${SUBPROJECT_DESKTOP_IN} ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT_DESKTOP})
# make # make
kde4_add_plugin (${PLUGIN_NAME} ${SUBPROJECT_SOURCE}) qt4_wrap_cpp (TASK_MOC_SOURCE ${TASK_HEADER})
kde4_add_plugin (${PLUGIN_NAME} ${SUBPROJECT_SOURCE} ${TASK_MOC_SOURCE})
target_link_libraries (${PLUGIN_NAME} ${KDE4_KDECORE_LIBS} ${KDE4_PLASMA_LIBS} ${QT_QTNETWORK_LIBRARY}) target_link_libraries (${PLUGIN_NAME} ${KDE4_KDECORE_LIBS} ${KDE4_PLASMA_LIBS} ${QT_QTNETWORK_LIBRARY})
# install # install

View File

@ -16,6 +16,7 @@
***************************************************************************/ ***************************************************************************/
#include "netctl.h" #include "netctl.h"
#include "task.h"
#include <KGlobal> #include <KGlobal>
#include <KStandardDirs> #include <KStandardDirs>
@ -28,6 +29,29 @@
#include <QTextCodec> #include <QTextCodec>
struct TaskResult
{
int exitCode;
QByteArray output;
};
TaskResult runTask(const QString cmd)
{
return Task::await<TaskResult>( [ & ]() {
QProcess command;
command.start(cmd);
command.waitForFinished(-1);
TaskResult r;
r.exitCode = command.exitCode();
r.output = command.readAllStandardOutput();
return r;
});
}
Netctl::Netctl(QObject *parent, const QVariantList &args) Netctl::Netctl(QObject *parent, const QVariantList &args)
: Plasma::DataEngine(parent, args) : Plasma::DataEngine(parent, args)
{ {
@ -43,7 +67,6 @@ Netctl::Netctl(QObject *parent, const QVariantList &args)
setMinimumPollingInterval(333); setMinimumPollingInterval(333);
readConfiguration(); readConfiguration();
setProcesses();
setKeys(); setKeys();
} }
@ -51,12 +74,6 @@ Netctl::Netctl(QObject *parent, const QVariantList &args)
Netctl::~Netctl() Netctl::~Netctl()
{ {
if (debug) qDebug() << "[DE]" << "[~Netctl]"; if (debug) qDebug() << "[DE]" << "[~Netctl]";
QStringList processesKeys = processes.keys();
for (int i=0; i<processesKeys.count(); i++) {
processes[processesKeys[i]]->terminate();
delete processes[processesKeys[i]];
}
} }
@ -103,31 +120,6 @@ void Netctl::setKeys()
} }
void Netctl::setProcesses()
{
if (debug) qDebug() << "[DE]" << "[setProcesses]";
processes[QString("currentProfile")] = new QProcess();
connect(processes[QString("currentProfile")], SIGNAL(finished(int, QProcess::ExitStatus)),
this, SLOT(setCurrentProfile(int, QProcess::ExitStatus)));
processes[QString("extIp")] = new QProcess();
connect(processes[QString("extIp")], SIGNAL(finished(int, QProcess::ExitStatus)),
this, SLOT(setExtIp(int, QProcess::ExitStatus)));
processes[QString("extIp6")] = new QProcess();
connect(processes[QString("extIp6")], SIGNAL(finished(int, QProcess::ExitStatus)),
this, SLOT(setExtIp6(int, QProcess::ExitStatus)));
processes[QString("netctlAuto")] = new QProcess();
connect(processes[QString("netctlAuto")], SIGNAL(finished(int, QProcess::ExitStatus)),
this, SLOT(setNetctlAutoStatus(int, QProcess::ExitStatus)));
processes[QString("profiles")] = new QProcess();
connect(processes[QString("profiles")], SIGNAL(finished(int, QProcess::ExitStatus)),
this, SLOT(setProfileList(int, QProcess::ExitStatus)));
processes[QString("statusString")] = new QProcess();
connect(processes[QString("statusString")], SIGNAL(finished(int, QProcess::ExitStatus)),
this, SLOT(setProfileStringStatus(int, QProcess::ExitStatus)));
}
void Netctl::readConfiguration() void Netctl::readConfiguration()
{ {
if (debug) qDebug() << "[DE]" << "[readConfiguration]"; if (debug) qDebug() << "[DE]" << "[readConfiguration]";
@ -206,33 +198,22 @@ bool Netctl::sourceRequestEvent(const QString &name)
} }
void Netctl::getCurrentProfile(const QString cmdNetctl, const QString cmdNetctlAuto) QString Netctl::getCurrentProfile(const QString cmdNetctl, const QString cmdNetctlAuto)
{ {
if (debug) qDebug() << "[DE]" << "[getCurrentProfile]"; if (debug) qDebug() << "[DE]" << "[getCurrentProfile]";
getNetctlAutoStatus(cmdNetctlAuto); getNetctlAutoStatus(cmdNetctlAuto);
if (processes[QString("currentProfile")]->state() != QProcess::NotRunning) return;
QString cmd; QString cmd;
if (netctlAutoStatus) if (netctlAutoStatus)
cmd = cmdNetctlAuto; cmd = cmdNetctlAuto;
else else
cmd = cmdNetctl; cmd = cmdNetctl;
if (debug) qDebug() << "[DE]" << "[getCurrentProfile]" << ":" << "Cmd" << cmd; if (debug) qDebug() << "[DE]" << "[getCurrentProfile]" << ":" << "Cmd" << cmd;
TaskResult process = runTask(cmd + QString(" list"));
processes[QString("currentProfile")]->start(cmd + QString(" list")); if (debug) qDebug() << "[DE]" << "[getCurrentProfile]" << ":" << "Cmd returns" << process.exitCode;
}
void Netctl::setCurrentProfile(int exitCode, QProcess::ExitStatus exitStatus)
{
Q_UNUSED(exitStatus)
if (debug) qDebug() << "[DE]" << "[setCurrentProfile]";
if (debug) qDebug() << "[DE]" << "[setCurrentProfile]" << ":" << "Cmd returns" << exitCode;
currentProfile = QString(""); currentProfile = QString("");
QString status = QString("false"); QString cmdOutput = QTextCodec::codecForMib(106)->toUnicode(process.output);
QString cmdOutput = QTextCodec::codecForMib(106)
->toUnicode(processes[QString("currentProfile")]->readAllStandardOutput());
QStringList profileList = cmdOutput.split(QChar('\n'), QString::SkipEmptyParts); QStringList profileList = cmdOutput.split(QChar('\n'), QString::SkipEmptyParts);
for (int i=0; i<profileList.count(); i++) for (int i=0; i<profileList.count(); i++)
if (profileList[i][0] == QChar('*')) { if (profileList[i][0] == QChar('*')) {
@ -240,65 +221,22 @@ void Netctl::setCurrentProfile(int exitCode, QProcess::ExitStatus exitStatus)
break; break;
} }
currentProfile.remove(0, 2); currentProfile.remove(0, 2);
if (!currentProfile.isEmpty())
status = QString("true");
setData(QString("currentProfile"), QString("value"), currentProfile); return currentProfile;
setData(QString("statusBool"), QString("value"), status);
} }
void Netctl::getExtIp(const QString cmd) QString Netctl::getExtIp(const QString cmd)
{ {
if (debug) qDebug() << "[DE]" << "[getExtIp]"; if (debug) qDebug() << "[DE]" << "[getExtIp]";
if (debug) qDebug() << "[DE]" << "[getExtIp]" << ":" << "Cmd" << cmd; if (debug) qDebug() << "[DE]" << "[getExtIp]" << ":" << "Cmd" << cmd;
if (processes[QString("extIp")]->state() != QProcess::NotRunning) return; TaskResult process = runTask(cmd);
if (debug) qDebug() << "[DE]" << "[getExtIp]" << ":" << "Cmd returns" << process.exitCode;
processes[QString("extIp")]->start(cmd); QString extIp = QTextCodec::codecForMib(106)->toUnicode(process.output).trimmed();
}
return extIp;
void Netctl::setExtIp(int exitCode, QProcess::ExitStatus exitStatus)
{
Q_UNUSED(exitStatus)
if (debug) qDebug() << "[DE]" << "[setExtIp]";
if (debug) qDebug() << "[DE]" << "[setExtIp]" << ":" << "Cmd returns" << exitCode;
QString extIp = QString("");
QString cmdOutput = QTextCodec::codecForMib(106)
->toUnicode(processes[QString("extIp")]->readAllStandardOutput())
.trimmed();
extIp = cmdOutput;
setData(QString("extIp"), QString("value"), extIp);
}
void Netctl::getExtIp6(const QString cmd)
{
if (debug) qDebug() << "[DE]" << "[getExtIp6]";
if (debug) qDebug() << "[DE]" << "[getExtIp6]" << ":" << "Cmd" << cmd;
if (processes[QString("extIp6")]->state() != QProcess::NotRunning) return;
processes[QString("extIp6")]->start(cmd);
}
void Netctl::setExtIp6(int exitCode, QProcess::ExitStatus exitStatus)
{
Q_UNUSED(exitStatus)
if (debug) qDebug() << "[DE]" << "[setExtIp6]";
if (debug) qDebug() << "[DE]" << "[setExtIp6]" << ":" << "Cmd returns" << exitCode;
QString extIp = QString("");
QString cmdOutput = QTextCodec::codecForMib(106)
->toUnicode(processes[QString("extIp6")]->readAllStandardOutput())
.trimmed();
extIp = cmdOutput;
setData(QString("extIp6"), QString("value"), extIp);
} }
@ -315,59 +253,37 @@ QStringList Netctl::getInterfaceList()
} }
QString Netctl::getIntIp() QString Netctl::getIntIp(const QAbstractSocket::NetworkLayerProtocol protocol)
{ {
if (debug) qDebug() << "[DE]" << "[getIntIp]"; if (debug) qDebug() << "[DE]" << "[getIntIp]";
QString intIp = QString("127.0.0.1/8"); QString intIp = QString("");
if (protocol == QAbstractSocket::IPv4Protocol)
intIp = QString("127.0.0.1/8");
else if (protocol == QAbstractSocket::IPv6Protocol)
intIp = QString("::1/128");
QList<QHostAddress> rawList = QNetworkInterface::allAddresses(); QList<QHostAddress> rawList = QNetworkInterface::allAddresses();
for (int i=0; i<rawList.count(); i++) { for (int i=0; i<rawList.count(); i++) {
if(rawList[i] == QHostAddress(QHostAddress::LocalHost)) continue; if(rawList[i] == QHostAddress(QHostAddress::LocalHost)) continue;
if (rawList[i].protocol() != QAbstractSocket::IPv4Protocol) continue; if (rawList[i].protocol() == protocol) {
intIp = rawList[i].toString(); intIp = rawList[i].toString();
break; break;
}
} }
return intIp; return intIp;
} }
QString Netctl::getIntIp6() QString Netctl::getNetctlAutoStatus(const QString cmdNetctlAuto)
{
if (debug) qDebug() << "[DE]" << "[getIntIp6]";
QString intIp = QString("::1/128");
QList<QHostAddress> rawList = QNetworkInterface::allAddresses();
for (int i=0; i<rawList.count(); i++) {
if(rawList[i] == QHostAddress(QHostAddress::LocalHost)) continue;
if (rawList[i].protocol() != QAbstractSocket::IPv6Protocol) continue;
intIp = rawList[i].toString();
break;
}
return intIp;
}
void Netctl::getNetctlAutoStatus(const QString cmdNetctlAuto)
{ {
if (debug) qDebug() << "[DE]" << "[getNetctlAutoStatus]"; if (debug) qDebug() << "[DE]" << "[getNetctlAutoStatus]";
if (processes[QString("netctlAuto")]->state() != QProcess::NotRunning) return; TaskResult process = runTask(cmdNetctlAuto + QString(" list"));
if (debug) qDebug() << "[DE]" << "[getNetctlAutoStatus]" << ":" << "Cmd returns" << process.exitCode;
processes[QString("netctlAuto")]->start(cmdNetctlAuto + QString(" list"));
}
void Netctl::setNetctlAutoStatus(int exitCode, QProcess::ExitStatus exitStatus)
{
Q_UNUSED(exitStatus)
if (debug) qDebug() << "[DE]" << "[setNetctlAutoStatus]";
if (debug) qDebug() << "[DE]" << "[setNetctlAutoStatus]" << ":" << "Cmd returns" << exitCode;
QString status; QString status;
QString cmdOutput = QTextCodec::codecForMib(106) QString cmdOutput = QTextCodec::codecForMib(106)->toUnicode(process.output);
->toUnicode(processes[QString("netctlAuto")]->readAllStandardOutput());
if (cmdOutput.isEmpty()) { if (cmdOutput.isEmpty()) {
netctlAutoStatus = false; netctlAutoStatus = false;
status = QString("false"); status = QString("false");
@ -377,69 +293,64 @@ void Netctl::setNetctlAutoStatus(int exitCode, QProcess::ExitStatus exitStatus)
status = QString("true"); status = QString("true");
} }
setData(QString("netctlAuto"), QString("value"), status); return status;
} }
void Netctl::getProfileList(const QString cmdNetctl, const QString cmdNetctlAuto) QStringList Netctl::getProfileList(const QString cmdNetctl, const QString cmdNetctlAuto)
{ {
if (debug) qDebug() << "[DE]" << "[getProfileList]"; if (debug) qDebug() << "[DE]" << "[getProfileList]";
getNetctlAutoStatus(cmdNetctlAuto); getNetctlAutoStatus(cmdNetctlAuto);
if (processes[QString("profiles")]->state() != QProcess::NotRunning) return;
QString cmd; QString cmd;
if (netctlAutoStatus) if (netctlAutoStatus)
cmd = cmdNetctlAuto; cmd = cmdNetctlAuto;
else else
cmd = cmdNetctl; cmd = cmdNetctl;
if (debug) qDebug() << "[DE]" << "[getCurrentProfile]" << ":" << "Cmd" << cmd; if (debug) qDebug() << "[DE]" << "[getCurrentProfile]" << ":" << "Cmd" << cmd;
TaskResult process = runTask(cmd + QString(" list"));
if (debug) qDebug() << "[DE]" << "[getCurrentProfile]" << ":" << "Cmd returns" << process.exitCode;
processes[QString("profiles")]->start(cmd + QString(" list")); QString cmdOutput = QTextCodec::codecForMib(106)->toUnicode(process.output);
}
void Netctl::setProfileList(int exitCode, QProcess::ExitStatus exitStatus)
{
Q_UNUSED(exitStatus)
if (debug) qDebug() << "[DE]" << "[setProfileList]";
if (debug) qDebug() << "[DE]" << "[setProfileList]" << ":" << "Cmd returns" << exitCode;
QString cmdOutput = QTextCodec::codecForMib(106)
->toUnicode(processes[QString("profiles")]->readAllStandardOutput());
QStringList profileList = cmdOutput.split(QChar('\n'), QString::SkipEmptyParts); QStringList profileList = cmdOutput.split(QChar('\n'), QString::SkipEmptyParts);
for (int i=0; i<profileList.count(); i++) for (int i=0; i<profileList.count(); i++)
profileList[i].remove(0, 2); profileList[i].remove(0, 2);
setData(QString("profiles"), QString("value"), profileList.join(QChar(','))); return profileList;
} }
void Netctl::getProfileStringStatus(const QString cmdNetctl, const QString cmdNetctlAuto) QString Netctl::getProfileStringStatus(const QString cmdNetctl, const QString cmdNetctlAuto)
{ {
if (debug) qDebug() << "[DE]" << "[getProfileStringStatus]"; if (debug) qDebug() << "[DE]" << "[getProfileStringStatus]";
getNetctlAutoStatus(cmdNetctlAuto); getNetctlAutoStatus(cmdNetctlAuto);
if (netctlAutoStatus) { QString status = QString("static");
setData(QString("statusString"), QString("value"), QString("netctl-auto")); if (netctlAutoStatus)
return; status = QString("netctl-auto");
} else {
if (processes[QString("statusString")]->state() != QProcess::NotRunning) return; TaskResult process = runTask(cmdNetctl + QString(" is-enabled ") + getCurrentProfile(cmdNetctl, cmdNetctlAuto));
if (debug) qDebug() << "[DE]" << "[getProfileStringStatus]" << ":" << "Cmd returns" << process.exitCode;
processes[QString("statusString")]->start(cmdNetctl + QString(" is-enabled ") + currentProfile); if (process.exitCode == 0)
status = QString("enabled");
}
return status;
} }
void Netctl::setProfileStringStatus(int exitCode, QProcess::ExitStatus exitStatus) QString Netctl::getStatus(const QString cmdNetctl, const QString cmdNetctlAuto)
{ {
Q_UNUSED(exitStatus) if (debug) qDebug() << "[DE]" << "[getStatus]";
if (debug) qDebug() << "[DE]" << "[setProfileStringStatus]"; getNetctlAutoStatus(cmdNetctlAuto);
if (debug) qDebug() << "[DE]" << "[setProfileStringStatus]" << ":" << "Cmd returns" << exitCode;
QString status = QString("static"); QString status = QString("false");
if (exitCode == 0) QString currentProfile = getCurrentProfile(cmdNetctl, cmdNetctlAuto);
status = QString("enabled"); if (!currentProfile.isEmpty())
status = QString("true");
setData(QString("statusString"), QString("value"), status); return status;
} }
@ -448,45 +359,45 @@ bool Netctl::updateSourceEvent(const QString &source)
if (debug) qDebug() << "[DE]" << "[updateSourceEvent]"; if (debug) qDebug() << "[DE]" << "[updateSourceEvent]";
if (debug) qDebug() << "[DE]" << "[updateSourceEvent]" << ":" << "Source name" << source; if (debug) qDebug() << "[DE]" << "[updateSourceEvent]" << ":" << "Source name" << source;
QString value = QString("N\\A");
if (source == QString("currentProfile")) { if (source == QString("currentProfile")) {
getCurrentProfile(configuration[QString("CMD")], value = getCurrentProfile(configuration[QString("CMD")],
configuration[QString("NETCTLAUTOCMD")]); configuration[QString("NETCTLAUTOCMD")]);
} }
else if (source == QString("extIp")) { else if (source == QString("extIp")) {
if (configuration[QString("EXTIP")] == QString("true")) if (configuration[QString("EXTIP")] == QString("true"))
getExtIp(configuration[QString("EXTIPCMD")]); value = getExtIp(configuration[QString("EXTIPCMD")]);
} }
else if (source == QString("extIp6")) { else if (source == QString("extIp6")) {
if (configuration[QString("EXTIP6")] == QString("true")) if (configuration[QString("EXTIP6")] == QString("true"))
getExtIp(configuration[QString("EXTIP6CMD")]); value = getExtIp(configuration[QString("EXTIP6CMD")]);
} }
else if (source == QString("interfaces")) { else if (source == QString("interfaces")) {
QString value = getInterfaceList().join(QChar(',')); value = getInterfaceList().join(QChar(','));
setData(source, QString("value"), value);
} }
else if (source == QString("intIp")) { else if (source == QString("intIp")) {
QString value = getIntIp(); value = getIntIp(QAbstractSocket::IPv4Protocol);
setData(source, QString("value"), value);
} }
else if (source == QString("intIp6")) { else if (source == QString("intIp6")) {
QString value = getIntIp6(); value = getIntIp(QAbstractSocket::IPv6Protocol);
setData(source, QString("value"), value);
} }
else if (source == QString("netctlAuto")) { else if (source == QString("netctlAuto")) {
getNetctlAutoStatus(configuration[QString("NETCTLAUTOCMD")]); value = getNetctlAutoStatus(configuration[QString("NETCTLAUTOCMD")]);
} }
else if (source == QString("profiles")) { else if (source == QString("profiles")) {
getProfileList(configuration[QString("CMD")], value = getProfileList(configuration[QString("CMD")],
configuration[QString("NETCTLAUTOCMD")]); configuration[QString("NETCTLAUTOCMD")])
.join(QChar(','));
} }
else if (source == QString("statusBool")) { else if (source == QString("statusBool")) {
getCurrentProfile(configuration[QString("CMD")], value = getCurrentProfile(configuration[QString("CMD")],
configuration[QString("NETCTLAUTOCMD")]); configuration[QString("NETCTLAUTOCMD")]);
} }
else if (source == QString("statusString")) { else if (source == QString("statusString")) {
getProfileStringStatus(configuration[QString("CMD")], value = getStatus(configuration[QString("CMD")],
configuration[QString("NETCTLAUTOCMD")]); configuration[QString("NETCTLAUTOCMD")]);
} }
setData(source, QString("value"), value);
return true; return true;
} }

View File

@ -19,7 +19,7 @@
#define NETCTL_DE_H #define NETCTL_DE_H
#include <Plasma/DataEngine> #include <Plasma/DataEngine>
#include <QProcess> #include <QAbstractSocket>
class Netctl : public Plasma::DataEngine class Netctl : public Plasma::DataEngine
@ -29,40 +29,28 @@ class Netctl : public Plasma::DataEngine
public: public:
Netctl(QObject *parent, const QVariantList &args); Netctl(QObject *parent, const QVariantList &args);
~Netctl(); ~Netctl();
void getCurrentProfile(const QString cmdNetctl, const QString cmdNetctlAuto); QString getCurrentProfile(const QString cmdNetctl, const QString cmdNetctlAuto);
void getExtIp(const QString cmd); QString getExtIp(const QString cmd);
void getExtIp6(const QString cmd);
QStringList getInterfaceList(); QStringList getInterfaceList();
QString getIntIp(); QString getIntIp(const QAbstractSocket::NetworkLayerProtocol protocol);
QString getIntIp6(); QString getNetctlAutoStatus(const QString cmdNetctlAuto);
void getNetctlAutoStatus(const QString cmdNetctlAuto); QStringList getProfileList(const QString cmdNetctl, const QString cmdNetctlAuto);
void getProfileList(const QString cmdNetctl, const QString cmdNetctlAuto); QString getProfileStringStatus(const QString cmdNetctl, const QString cmdNetctlAuto);
void getProfileStringStatus(const QString cmdNetctl, const QString cmdNetctlAuto); QString getStatus(const QString cmdNetctl, const QString cmdNetctlAuto);
protected: protected:
bool sourceRequestEvent(const QString &name); bool sourceRequestEvent(const QString &name);
bool updateSourceEvent(const QString &source); bool updateSourceEvent(const QString &source);
QStringList sources() const; QStringList sources() const;
private slots:
void setCurrentProfile(int exitCode, QProcess::ExitStatus exitStatus);
void setExtIp(int exitCode, QProcess::ExitStatus exitStatus);
void setExtIp6(int exitCode, QProcess::ExitStatus exitStatus);
void setNetctlAutoStatus(int exitCode, QProcess::ExitStatus exitStatus);
void setProfileList(int exitCode, QProcess::ExitStatus exitStatus);
void setProfileStringStatus(int exitCode, QProcess::ExitStatus exitStatus);
private: private:
bool netctlAutoStatus; bool netctlAutoStatus;
QString currentProfile; QString currentProfile;
// processes
QMap<QString, QProcess *> processes;
// configuration // configuration
bool debug; bool debug;
QMap<QString, QString> configuration; QMap<QString, QString> configuration;
void initValues(); void initValues();
void setKeys(); void setKeys();
void setProcesses();
void readConfiguration(); void readConfiguration();
QMap<QString, QString> updateConfiguration(const QMap<QString, QString> rawConfig); QMap<QString, QString> updateConfiguration(const QMap<QString, QString> rawConfig);
}; };

385
sources/task.h Normal file
View File

@ -0,0 +1,385 @@
/*
* copyright: 2014
* name : mhogo mchungu
* email: mhogomchungu@gmail.com
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef __TASK_H_INCLUDED__
#define __TASK_H_INCLUDED__
#include <utility>
#include <future>
#include <functional>
#include <QThread>
#include <QEventLoop>
/*
*
* Examples on how to use the library are at the end of this file.
*
*/
namespace Task
{
class Thread : public QThread
{
Q_OBJECT
public:
Thread()
{
connect( this,SIGNAL( finished() ),this,SLOT( deleteLater() ) ) ;
}
protected:
virtual ~Thread()
{
}
private:
virtual void run( void )
{
}
};
template< typename T >
class future
{
public:
future() : m_function( []( T t ){ Q_UNUSED( t ) ; } )
{
}
void setActions( std::function< void( void ) > start,
std::function< void( void ) > cancel,
std::function< T ( void ) > get )
{
m_start = std::move( start ) ;
m_cancel = std::move( cancel ) ;
m_get = std::move( get ) ;
}
void then( std::function< void( T ) > function )
{
m_function = std::move( function ) ;
m_start() ;
}
T get()
{
return m_get() ;
}
T await()
{
QEventLoop p ;
T q ;
m_function = [ & ]( T r ){ q = std::move( r ) ; p.exit() ; } ;
m_start() ;
p.exec() ;
return q ;
}
void start()
{
m_start() ;
}
void cancel()
{
m_cancel() ;
}
void run( T r )
{
m_function( std::move( r ) ) ;
}
private:
std::function< void( T ) > m_function ;
std::function< void( void ) > m_start ;
std::function< void( void ) > m_cancel ;
std::function< T ( void ) > m_get ;
};
template< typename T >
class ThreadHelper : public Thread
{
public:
ThreadHelper( std::function< T ( void ) >&& function ) : m_function( std::move( function ) )
{
}
future<T>& Future( void )
{
m_future.setActions( [ this ](){ this->start() ; },
[ this ](){ this->deleteLater() ; },
[ this ](){ T r = m_function() ; this->deleteLater() ; return r ; } ) ;
return m_future ;
}
private:
~ThreadHelper()
{
m_future.run( std::move( m_cargo ) ) ;
}
void run( void )
{
m_cargo = m_function() ;
}
std::function< T ( void ) > m_function ;
future<T> m_future ;
T m_cargo ;
};
class future_1
{
public:
future_1() : m_function( [](){} )
{
}
void setActions( std::function< void( void ) > start,
std::function< void( void ) > cancel,
std::function< void( void ) > get )
{
m_start = std::move( start ) ;
m_cancel = std::move( cancel ) ;
m_get = std::move( get ) ;
}
void then( std::function< void( void ) > function )
{
m_function = std::move( function ) ;
m_start() ;
}
void get()
{
m_get() ;
}
void await()
{
QEventLoop p ;
m_function = [ & ](){ p.exit() ; } ;
m_start() ;
p.exec() ;
}
void start()
{
m_start() ;
}
void run()
{
m_function() ;
}
void cancel()
{
m_cancel() ;
}
private:
std::function< void( void ) > m_function ;
std::function< void( void ) > m_start ;
std::function< void( void ) > m_cancel ;
std::function< void( void ) > m_get ;
};
class ThreadHelper_1 : public Thread
{
public:
ThreadHelper_1( std::function< void ( void ) >&& function ) : m_function( std::move( function ) )
{
}
future_1& Future( void )
{
m_future.setActions( [ this ](){ this->start() ; },
[ this ](){ this->deleteLater() ; },
[ this ](){ m_function() ; this->deleteLater() ; } ) ;
return m_future ;
}
private:
~ThreadHelper_1()
{
m_future.run() ;
}
void run( void )
{
m_function() ;
}
std::function< void ( void ) > m_function ;
future_1 m_future ;
};
/*
* Below APIs runs two tasks,the first one will run in a different thread and
* the second one will be run on the original thread after the completion of the
* first one.
*/
template< typename T >
future<T>& run( std::function< T ( void ) > function )
{
auto t = new ThreadHelper<T>( std::move( function ) ) ;
return t->Future() ;
}
static inline future_1& run( std::function< void( void ) > function )
{
auto t = new ThreadHelper_1( std::move( function ) ) ;
return t->Future() ;
}
static inline void exec( std::function< void( void ) > function )
{
Task::run( std::move( function ) ).start() ;
}
/*
* Below APIs implements resumable functions where a function will be "blocked"
* waiting for the function to return without "hanging" the current thread.
*
* recommending reading up on C#'s await keyword to get a sense of what is being
* discussed below.
*/
static inline void await( Task::future_1& e )
{
e.await() ;
}
static inline void await( std::function< void( void ) > function )
{
Task::run( std::move( function ) ).await() ;
}
template< typename T >
T await( std::function< T ( void ) > function )
{
return Task::run<T>( std::move( function ) ).await() ;
}
template< typename T >
T await( Task::future<T>& e )
{
return e.await() ;
}
template< typename T >
T await( std::future<T>&& t )
{
return Task::await<T>( [ & ](){ return t.get() ; } ) ;
}
}
#if 0
/*
* Examples on how to use the library
*/
/*
* templated version that passes a return value of one function to another function
*/
auto _a = [](){
/*
* task _a does what task _a does here.
*
* This function body will run on a different thread
*/
return 0 ;
}
auto _b = []( int r ){
/*
*
* task _b does what task _b does here.
*
* r is a const reference to a value returned by _a
*
* This function body will run on the original thread
*/
}
Task::run<int>( _a ).then( _b ) ;
alternatively,
Task::future<int>& e = Task::run( _a ) ;
e.then( _b ) ;
/*
* Non templated version that does not pass around return value
*/
auto _c = [](){
/*
* task _a does what task _a does here.
*
* This function body will run on a different thread
*/
}
auto _d = [](){
/*
* task _b does what task _b does here.
*
* r is a const reference to a value returned by _a
*
* This function body will run on the original thread
*/
}
Task::run( _c ).then( _d ) ;
/*
* if no continuation
*/
Task::exec( _c ) ;
/*
* Task::await() is used to "block" without "hanging" the calling thread until the function returns.
*
* Its use case is to do sync programming without hanging the calling thread.
*
* example use case for it is to "block" on function in a GUI thread withough blocking the GUI thread
* hanging the application.
*/
/*
* await example when the called function return no result
*/
Task::await( _c ) ;
/*
* await example when the called function return a result
*/
int r = Task::await<int>( _a ) ;
alternatively,
Task::future<int>& e = Task::run<int>( _a ) ;
int r = e.await() ;
alternatively,
int r = Task::run<int>( _a ).await() ;
#endif
#endif //__TASK_H_INCLUDED__