From 6a41fa2b8ade18c64081c32b241b779031921512 Mon Sep 17 00:00:00 2001 From: arcan1s Date: Fri, 31 Oct 2014 06:13:49 +0300 Subject: [PATCH] implement script feature to dataengine --- sources/ext-sysmon/CMakeLists.txt | 2 +- sources/ext-sysmon/extscript.cpp | 33 +++++++----- sources/ext-sysmon/extscript.h | 11 +++- sources/ext-sysmon/extsysmon.conf | 3 -- sources/ext-sysmon/extsysmon.cpp | 54 ++++++++++++------- sources/ext-sysmon/extsysmon.h | 8 ++- sources/ext-sysmon/scripts/aw-get-external-ip | 0 sources/ext-sysmon/scripts/aw-script-template | 0 8 files changed, 74 insertions(+), 37 deletions(-) mode change 100644 => 100755 sources/ext-sysmon/scripts/aw-get-external-ip mode change 100644 => 100755 sources/ext-sysmon/scripts/aw-script-template diff --git a/sources/ext-sysmon/CMakeLists.txt b/sources/ext-sysmon/CMakeLists.txt index 3878a3c..2bdb51e 100644 --- a/sources/ext-sysmon/CMakeLists.txt +++ b/sources/ext-sysmon/CMakeLists.txt @@ -33,4 +33,4 @@ target_link_libraries (${PLUGIN_NAME} ${KDE4_KDECORE_LIBS} ${KDE4_PLASMA_LIBS}) install (TARGETS ${PLUGIN_NAME} DESTINATION ${PLUGIN_INSTALL_DIR}) install (FILES ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT_DESKTOP} DESTINATION ${SERVICES_INSTALL_DIR}) install (FILES ${SUBPROJECT_CONF} DESTINATION ${CONFIG_INSTALL_DIR}) -install (DIRECTORY ${SUBPROJECT_SCRIPTS} DESTINATION ${DATA_INSTALL_DIR}/${PLUGIN_NAME}) +install (DIRECTORY ${SUBPROJECT_SCRIPTS} DESTINATION ${DATA_INSTALL_DIR}/${PLUGIN_NAME} USE_SOURCE_PERMISSIONS) diff --git a/sources/ext-sysmon/extscript.cpp b/sources/ext-sysmon/extscript.cpp index d1d357c..39604da 100644 --- a/sources/ext-sysmon/extscript.cpp +++ b/sources/ext-sysmon/extscript.cpp @@ -71,6 +71,14 @@ int ExtScript::getInterval() } +QString ExtScript::getName() +{ + if (debug) qDebug() << PDEBUG; + + return name; +} + + QString ExtScript::getPrefix() { if (debug) qDebug() << PDEBUG; @@ -166,7 +174,7 @@ void ExtScript::readConfiguration() QMap settings; for (int i=0; i newSettings = getConfigurationFromFile(fileName); for (int i=0; i ExtScript::run(const int time) +ExtScript::ScriptData ExtScript::run(const int time) { if (debug) qDebug() << PDEBUG; - QMap response; - if (time != interval) { - response[QString("refresh")] = false; - return response; - } - response[QString("refresh")] = true; + ScriptData response; + response.active = active; + response.name = name; + response.refresh = false; + if (!active) return response; + if (time != interval) return response; + response.refresh = true; QStringList cmdList; if (!prefix.isEmpty()) cmdList.append(prefix); QString fullPath = name; for (int i=0; i ExtScript::run(const int time) if (debug) qDebug() << PDEBUG << ":" << "Output" << qoutput; break; case stderr2stdout: - response[QString("output")] = info + QString("\t") + qoutput; + response.output = info + QString("\t") + qoutput; break; default: if (debug) qDebug() << PDEBUG << ":" << "Debug" << info; - response[QString("output")] = qoutput; + response.output = qoutput; break; } if (!output) - response[QString("output")] = QString::number(process.exitCode); + response.output = QString::number(process.exitCode); return response; } diff --git a/sources/ext-sysmon/extscript.h b/sources/ext-sysmon/extscript.h index 8819c4c..8c563f0 100644 --- a/sources/ext-sysmon/extscript.h +++ b/sources/ext-sysmon/extscript.h @@ -24,19 +24,26 @@ class ExtScript : public QObject { +public: enum Redirect { stdout2stderr = -1, nothing, stderr2stdout }; + typedef struct { + bool active; + QString name; + QString output; + bool refresh; + } ScriptData; -public: ExtScript(const QString scriptName, const QStringList directories, const bool debugCmd = false); ~ExtScript(); // configuration void addDirectory(const QString dir); QStringList directories(); int getInterval(); + QString getName(); QString getPrefix(); Redirect getRedirect(); bool hasOutput(); @@ -50,7 +57,7 @@ public: public slots: void readConfiguration(); - QMap run(const int time); + ScriptData run(const int time); private: // configuration diff --git a/sources/ext-sysmon/extsysmon.conf b/sources/ext-sysmon/extsysmon.conf index a66fa17..9379b5a 100644 --- a/sources/ext-sysmon/extsysmon.conf +++ b/sources/ext-sysmon/extsysmon.conf @@ -4,9 +4,6 @@ # ACPI devices #ACPIPATH=/sys/class/power_supply/ -# Custom command, separator is '@@' -#CUSTOM=curl ip4.telize.com - # Command which returns number of the current desktop #DESKTOPCMD=qdbus org.kde.kwin /KWin currentDesktop diff --git a/sources/ext-sysmon/extsysmon.cpp b/sources/ext-sysmon/extsysmon.cpp index de5246b..7da13e1 100644 --- a/sources/ext-sysmon/extsysmon.cpp +++ b/sources/ext-sysmon/extsysmon.cpp @@ -29,6 +29,7 @@ #include #include +#include #include #include @@ -48,6 +49,7 @@ ExtendedSysMon::ExtendedSysMon(QObject* parent, const QVariantList& args) setMinimumPollingInterval(333); readConfiguration(); + initScripts(); } @@ -146,6 +148,31 @@ QStringList ExtendedSysMon::getDesktopNames() } +void ExtendedSysMon::initScripts() +{ + if (debug) qDebug() << PDEBUG; + + // create directory at $HOME + QString localDir = KStandardDirs::locateLocal("data", "plasma_engine_extsysmon/scripts"); + if (KStandardDirs::makeDir(localDir)) + if (debug) qDebug() << PDEBUG << ":" << "Created directory" << localDir; + + QStringList dirs = KGlobal::dirs()->findDirs("data", "plasma_engine_extsysmon/scripts"); + QStringList names; + for (int i=0; i rawConfig; rawConfig[QString("ACPIPATH")] = QString("/sys/class/power_supply/"); - rawConfig[QString("CUSTOM")] = QString("curl ip4.telize.com"); rawConfig[QString("DESKTOP")] = QString(""); rawConfig[QString("DESKTOPCMD")] = QString("qdbus org.kde.kwin /KWin currentDesktop"); rawConfig[QString("GPUDEV")] = QString("auto"); @@ -225,8 +251,7 @@ QMap ExtendedSysMon::updateConfiguration(const QMap ExtendedSysMon::getCurrentDesktop(const QString cmd) } -QString ExtendedSysMon::getCustomCmd(const QString cmd) -{ - if (debug) qDebug() << PDEBUG; - if (debug) qDebug() << PDEBUG << ":" << "cmd" << cmd; - - TaskResult process = runTask(QString("bash -c \"") + cmd + QString("\"")); - if (debug) qDebug() << PDEBUG << ":" << "Cmd returns" << process.exitCode; - - return QTextCodec::codecForMib(106)->toUnicode(process.output).trimmed(); -} - - float ExtendedSysMon::getGpu(const QString device) { if (debug) qDebug() << PDEBUG; @@ -595,9 +608,14 @@ bool ExtendedSysMon::updateSourceEvent(const QString &source) setData(source, battery.keys()[i], battery[battery.keys()[i]].toInt()); } } else if (source == QString("custom")) { - for (int i=0; irun(times[i]); + if (!data.active) return true; + if (data.refresh) { + times[i] = 1; + setData(source, QString("custom") + QString::number(i), data.output); + } else + times[i]++; } } else if (source == QString("desktop")) { QMap desktop = getCurrentDesktop(configuration[QString("DESKTOPCMD")]); diff --git a/sources/ext-sysmon/extsysmon.h b/sources/ext-sysmon/extsysmon.h index 9a867a9..4fbea9f 100644 --- a/sources/ext-sysmon/extsysmon.h +++ b/sources/ext-sysmon/extsysmon.h @@ -21,6 +21,9 @@ #include #include + +class ExtScript; + class ExtendedSysMon : public Plasma::DataEngine { Q_OBJECT @@ -30,7 +33,6 @@ public: // update functions QMap getBattery(const QString acpiPath); QMap getCurrentDesktop(const QString cmd); - QString getCustomCmd(const QString cmd); float getGpu(const QString device); float getGpuTemp(const QString device); float getHddTemp(const QString cmd, const QString device); @@ -49,14 +51,18 @@ protected: private: // configuration QMap configuration; + QList externalScripts; + QList times; bool debug; // reread configuration QString getAllHdd(); QString getAutoGpu(); QString getAutoMpris(); QStringList getDesktopNames(); + void initScripts(); void readConfiguration(); QMap updateConfiguration(const QMap rawConfig); }; + #endif /* EXTSYSMON_H */ diff --git a/sources/ext-sysmon/scripts/aw-get-external-ip b/sources/ext-sysmon/scripts/aw-get-external-ip old mode 100644 new mode 100755 diff --git a/sources/ext-sysmon/scripts/aw-script-template b/sources/ext-sysmon/scripts/aw-script-template old mode 100644 new mode 100755