mirror of
https://github.com/arcan1s/awesome-widgets.git
synced 2025-04-24 23:47:20 +00:00
implement script feature to dataengine
This commit is contained in:
parent
2cd04e1f7f
commit
6a41fa2b8a
@ -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)
|
||||
|
@ -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<QString, QString> settings;
|
||||
for (int i=0; i<dirs.count(); i++) {
|
||||
if (!QDir(dirs[i]).entryList().contains(name + QString(".conf"))) continue;
|
||||
if (!QDir(dirs[i]).entryList(QDir::Files).contains(name + QString(".conf"))) continue;
|
||||
QString fileName = dirs[i] + QDir::separator() + name + QString(".conf");
|
||||
QMap<QString, QString> newSettings = getConfigurationFromFile(fileName);
|
||||
for (int i=0; i<newSettings.keys().count(); i++)
|
||||
@ -177,23 +185,24 @@ void ExtScript::readConfiguration()
|
||||
}
|
||||
|
||||
|
||||
QMap<QString, QVariant> ExtScript::run(const int time)
|
||||
ExtScript::ScriptData ExtScript::run(const int time)
|
||||
{
|
||||
if (debug) qDebug() << PDEBUG;
|
||||
|
||||
QMap<QString, QVariant> 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<dirs.count(); i++) {
|
||||
if (!QDir(dirs[i]).entryList().contains(name)) continue;
|
||||
if (!QDir(dirs[i]).entryList(QDir::Files).contains(name)) continue;
|
||||
fullPath = dirs[i] + QDir::separator() + name;
|
||||
break;
|
||||
}
|
||||
@ -210,15 +219,15 @@ QMap<QString, QVariant> 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;
|
||||
}
|
||||
|
@ -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<QString, QVariant> run(const int time);
|
||||
ScriptData run(const int time);
|
||||
|
||||
private:
|
||||
// configuration
|
||||
|
@ -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
|
||||
|
||||
|
@ -29,6 +29,7 @@
|
||||
#include <QTextCodec>
|
||||
#include <QThread>
|
||||
|
||||
#include <extscript.h>
|
||||
#include <pdebug/pdebug.h>
|
||||
#include <task/taskadds.h>
|
||||
|
||||
@ -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<dirs.count(); i++) {
|
||||
QStringList files = QDir(dirs[i]).entryList(QDir::Files, QDir::Name);
|
||||
for (int j=0; j<files.count(); j++) {
|
||||
if (files[j].endsWith(QString(".conf"))) continue;
|
||||
if (names.contains(files[j])) continue;
|
||||
if (debug) qDebug() << PDEBUG << ":" << "Found file" << files[j] << "in" << dirs[i];
|
||||
names.append(files[j]);
|
||||
externalScripts.append(new ExtScript(files[j], dirs, debug));
|
||||
times.append(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
QStringList ExtendedSysMon::sources() const
|
||||
{
|
||||
if (debug) qDebug() << PDEBUG;
|
||||
@ -173,7 +200,6 @@ void ExtendedSysMon::readConfiguration()
|
||||
// pre-setup
|
||||
QMap<QString, QString> 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<QString, QString> ExtendedSysMon::updateConfiguration(const QMap<QString, Q
|
||||
key = rawConfig.keys()[i];
|
||||
value = rawConfig[key];
|
||||
key.remove(QChar(' '));
|
||||
if ((key != QString("CUSTOM")) &&
|
||||
(key != QString("DESKTOPCMD")) &&
|
||||
if ((key != QString("DESKTOPCMD")) &&
|
||||
(key != QString("HDDTEMPCMD")) &&
|
||||
(key != QString("PKGCMD")))
|
||||
value.remove(QChar(' '));
|
||||
@ -339,18 +364,6 @@ QMap<QString, QVariant> 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; i<configuration[QString("CUSTOM")].split(QString("@@"), QString::SkipEmptyParts).count(); i++) {
|
||||
setData(source, QString("custom") + QString::number(i),
|
||||
getCustomCmd(configuration[QString("CUSTOM")].split(QString("@@"), QString::SkipEmptyParts)[i]));
|
||||
for (int i=0; i<externalScripts.count(); i++) {
|
||||
ExtScript::ScriptData data = externalScripts[i]->run(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<QString, QVariant> desktop = getCurrentDesktop(configuration[QString("DESKTOPCMD")]);
|
||||
|
@ -21,6 +21,9 @@
|
||||
#include <Plasma/DataEngine>
|
||||
#include <QProcess>
|
||||
|
||||
|
||||
class ExtScript;
|
||||
|
||||
class ExtendedSysMon : public Plasma::DataEngine
|
||||
{
|
||||
Q_OBJECT
|
||||
@ -30,7 +33,6 @@ public:
|
||||
// update functions
|
||||
QMap<QString, QVariant> getBattery(const QString acpiPath);
|
||||
QMap<QString, QVariant> 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<QString, QString> configuration;
|
||||
QList<ExtScript *> externalScripts;
|
||||
QList<int> times;
|
||||
bool debug;
|
||||
// reread configuration
|
||||
QString getAllHdd();
|
||||
QString getAutoGpu();
|
||||
QString getAutoMpris();
|
||||
QStringList getDesktopNames();
|
||||
void initScripts();
|
||||
void readConfiguration();
|
||||
QMap<QString, QString> updateConfiguration(const QMap<QString, QString> rawConfig);
|
||||
};
|
||||
|
||||
|
||||
#endif /* EXTSYSMON_H */
|
||||
|
0
sources/ext-sysmon/scripts/aw-get-external-ip
Normal file → Executable file
0
sources/ext-sysmon/scripts/aw-get-external-ip
Normal file → Executable file
0
sources/ext-sysmon/scripts/aw-script-template
Normal file → Executable file
0
sources/ext-sysmon/scripts/aw-script-template
Normal file → Executable file
Loading…
Reference in New Issue
Block a user