/*************************************************************************** * This file is part of pytextmonitor * * * * pytextmonitor is free software: you can redistribute it and/or * * modify it under the terms of the GNU General Public License as * * published by the Free Software Foundation, either version 3 of the * * License, or (at your option) any later version. * * * * pytextmonitor is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with pytextmonitor. If not, see http://www.gnu.org/licenses/ * ***************************************************************************/ #include "extsysmon.h" #include #include #include #include #include #include #include #include ExtendedSysMon::ExtendedSysMon(QObject* parent, const QVariantList& args) : Plasma::DataEngine(parent, args) { Q_UNUSED(args) setMinimumPollingInterval(333); debug = true; readConfiguration(); } QString ExtendedSysMon::getAllHdd() { if (debug) qDebug() << "[getAllHdd]"; QProcess command; QStringList devices; QString cmd = QString("find /dev -name [hms]d[a-z]"); QString qoutput = QString(""); if (debug) qDebug() << "[getAllHdd]" << ":" << "Run cmd" << cmd; command.start(cmd); command.waitForFinished(-1); if (debug) qDebug() << "[getAllHdd]" << ":" << "Cmd returns" << command.exitCode(); qoutput = QTextCodec::codecForMib(106)->toUnicode(command.readAllStandardOutput()); for (int i=0; itoUnicode(command.readAllStandardOutput()); if (qoutput.toLower().contains("nvidia")) gpu = QString("nvidia"); else if (qoutput.toLower().contains("radeon")) gpu = QString("ati"); if (debug) qDebug() << "[getAutoGpu]" << ":" << "Device" << gpu; return gpu; } QStringList ExtendedSysMon::sources() const { if (debug) qDebug() << "[sources]"; QStringList source; source.append(QString("custom")); source.append(QString("gpu")); source.append(QString("gputemp")); source.append(QString("hddtemp")); source.append(QString("pkg")); source.append(QString("player")); source.append(QString("ps")); if (debug) qDebug() << "[sources]" << ":" << "Sources" << source; return source; } void ExtendedSysMon::readConfiguration() { if (debug) qDebug() << "[readConfiguration]"; // pre-setup QMap rawConfig; rawConfig[QString("CUSTOM")] = QString("wget -qO- http://ifconfig.me/ip"); rawConfig[QString("DEBUG")] = QString("no"); rawConfig[QString("GPUDEV")] = QString("auto"); rawConfig[QString("HDDDEV")] = QString("all"); rawConfig[QString("HDDTEMPCMD")] = QString("sudo hddtemp"); rawConfig[QString("MPDADDRESS")] = QString("localhost"); rawConfig[QString("MPDPORT")] = QString("6600"); rawConfig[QString("PKGCMD")] = QString("pacman -Qu"); rawConfig[QString("PKGNULL")] = QString("0"); rawConfig[QString("PLAYER")] = QString("amarok"); QString fileName = KGlobal::dirs()->findResource("config", "extsysmon.conf"); if (debug) qDebug() << "[readConfiguration]" << ":" << "Configuration file" << fileName; QFile confFile(fileName); bool ok = confFile.open(QIODevice::ReadOnly); if (!ok) { configuration = updateConfiguration(rawConfig); return; } QString fileStr; QStringList value; while (true) { fileStr = QString(confFile.readLine()).trimmed(); if (fileStr[0] == QChar('#')) continue; if (fileStr[0] == QChar(';')) continue; if (fileStr.contains(QChar('='))) { value.clear(); for (int i=1; i ExtendedSysMon::updateConfiguration(const QMap rawConfig) { if (debug) qDebug() << "[updateConfiguration]"; QMap config; QString key, value; // remove spaces and copy source map for (int i=0; i -1)) devices.append(deviceList[i]); if (devices.isEmpty()) config[QString("HDDDEV")] = getAllHdd(); else config[QString("HDDDEV")] = devices.join(QChar(',')); } // pkgcmd for (int i=config[QString("PKGNULL")].split(QString(","), QString::SkipEmptyParts).count(); itoUnicode(command.readAllStandardOutput()).trimmed(); if (debug) qDebug() << "[getCustomCmd]" << ":" << "Return" << qoutput; return qoutput; } float ExtendedSysMon::getGpu(const QString device) { if (debug) qDebug() << "[getGpu]"; if (debug) qDebug() << "[getGpu]" << ":" << "Run function with device" << device; float gpu = 0.0; if ((device != QString("nvidia")) && (device != QString("ati"))) return gpu; QProcess command; QString qoutput; if (device == QString("nvidia")) { QString cmd = QString("nvidia-smi -q -d UTILIZATION"); if (debug) qDebug() << "[getGpu]" << ":" << "Run cmd" << cmd; command.start(cmd); command.waitForFinished(-1); if (debug) qDebug() << "[getGpu]" << ":" << "Cmd returns" << command.exitCode(); qoutput = QTextCodec::codecForMib(106)->toUnicode(command.readAllStandardOutput()); for (int i=0; itoUnicode(command.readAllStandardOutput()); for (int i=0; itoUnicode(command.readAllStandardOutput()); for (int i=0; itoUnicode(command.readAllStandardOutput()); for (int i=0; itoUnicode(command.readAllStandardOutput()).trimmed(); if (qoutput.split(QChar(':'), QString::SkipEmptyParts).count() >= 3) { QString temp = qoutput.split(QChar(':'), QString::SkipEmptyParts)[2]; temp.remove(QChar(0260)).remove(QChar('C')); hddTemp = temp.toFloat(); } if (debug) qDebug() << "[getHddTemp]" << ":" << "Return" << hddTemp; return hddTemp; } QStringList ExtendedSysMon::getPlayerInfo(const QString playerName, const QString mpdAddress, const QString mpdPort) { if (debug) qDebug() << "[getPlayerInfo]"; if (debug) qDebug() << "[getPlayerInfo]" << ":" << "Run function with player" << playerName; if (debug) qDebug() << "[getPlayerInfo]" << ":" << "Run function with MPD parameters" << mpdAddress + QString(":") + mpdPort; QStringList info; // album info.append(QString("unknown")); // artist info.append(QString("unknown")); // progress info.append(QString("0")); // duration info.append(QString("0")); // title info.append(QString("unknown")); QProcess command; QString cmd; QString qoutput = QString(""); QString qstr; if (playerName == QString("amarok")) { // amarok cmd = QString("qdbus org.kde.amarok /Player GetMetadata"); if (debug) qDebug() << "[getPlayerInfo]" << ":" << "Run cmd" << cmd; command.start(cmd); command.waitForFinished(-1); if (debug) qDebug() << "[getPlayerInfo]" << ":" << "Cmd returns" << command.exitCode(); qoutput = QTextCodec::codecForMib(106)->toUnicode(command.readAllStandardOutput()); for (int i=0; i 1) { if (qstr.split(QString(": "), QString::SkipEmptyParts)[0] == QString("album")) info[0] = qstr.split(QString(": "), QString::SkipEmptyParts)[1].trimmed(); else if (qstr.split(QString(": "), QString::SkipEmptyParts)[0] == QString("artist")) info[1] = qstr.split(QString(": "), QString::SkipEmptyParts)[1].trimmed(); else if (qstr.split(QString(": "), QString::SkipEmptyParts)[0] == QString("time")) info[3] = qstr.split(QString(": "), QString::SkipEmptyParts)[1].trimmed(); else if (qstr.split(QString(": "), QString::SkipEmptyParts)[0] == QString("title")) info[4] = qstr.split(QString(": "), QString::SkipEmptyParts)[1].trimmed(); } } cmd = QString("qdbus org.kde.amarok /Player PositionGet"); if (debug) qDebug() << "[getPlayerInfo]" << ":" << "Run cmd" << cmd; command.start(cmd); command.waitForFinished(-1); if (debug) qDebug() << "[getPlayerInfo]" << ":" << "Cmd returns" << command.exitCode(); qoutput = QTextCodec::codecForMib(106)->toUnicode(command.readAllStandardOutput()); for (int i=0; itoUnicode(command.readAllStandardOutput()); for (int i=0; i 1) { if (qstr.split(QString(": "), QString::SkipEmptyParts)[0] == QString("album")) info[0] = qstr.split(QString(": "), QString::SkipEmptyParts)[1].trimmed(); else if (qstr.split(QString(": "), QString::SkipEmptyParts)[0] == QString("artist")) info[1] = qstr.split(QString(": "), QString::SkipEmptyParts)[1].trimmed(); else if (qstr.split(QString(": "), QString::SkipEmptyParts)[0] == QString("time")) info[3] = qstr.split(QString(": "), QString::SkipEmptyParts)[1].trimmed(); else if (qstr.split(QString(": "), QString::SkipEmptyParts)[0] == QString("title")) info[4] = qstr.split(QString(": "), QString::SkipEmptyParts)[1].trimmed(); } } cmd = QString("qdbus org.mpris.clementine /Player PositionGet"); if (debug) qDebug() << "[getPlayerInfo]" << ":" << "Run cmd" << cmd; command.start(cmd); command.waitForFinished(-1); if (debug) qDebug() << "[getPlayerInfo]" << ":" << "Cmd returns" << command.exitCode(); qoutput = QTextCodec::codecForMib(106)->toUnicode(command.readAllStandardOutput()); for (int i=0; itoUnicode(command.readAllStandardOutput()); for (int i=0; i 1) { if (qstr.split(QString(": "), QString::SkipEmptyParts)[0] == QString("Album")) info[0] = qstr.split(QString(": "), QString::SkipEmptyParts)[1].trimmed(); else if (qstr.split(QString(": "), QString::SkipEmptyParts)[0] == QString("Artist")) info[1] = qstr.split(QString(": "), QString::SkipEmptyParts)[1].trimmed(); else if (qstr.split(QString(": "), QString::SkipEmptyParts)[0] == QString("time")) { info[3] = qstr.split(QString(": "), QString::SkipEmptyParts)[1].trimmed().split(QString(":"))[0]; info[2] = qstr.split(QString(": "), QString::SkipEmptyParts)[1].trimmed().split(QString(":"))[1]; } else if (qstr.split(QString(": "), QString::SkipEmptyParts)[0] == QString("Title")) info[4] = qstr.split(QString(": "), QString::SkipEmptyParts)[1].trimmed(); } } } else if (playerName == QString("qmmp")) { // qmmp cmd = QString("qmmp --status"); if (debug) qDebug() << "[getPlayerInfo]" << ":" << "Run cmd" << cmd; command.start(cmd); command.waitForFinished(-1); if (debug) qDebug() << "[getPlayerInfo]" << ":" << "Cmd returns" << command.exitCode(); qoutput = QTextCodec::codecForMib(106)->toUnicode(command.readAllStandardOutput()); for (int i=0; itoUnicode(command.readAllStandardOutput()).trimmed(); for (int i=0; itoUnicode(command.readAllStandardOutput()).trimmed(); int psTotal = qoutput.split(QChar('\n'), QString::SkipEmptyParts).count(); psStats.append(QString::number(psTotal)); if (debug) qDebug() << "[getPsStats]" << ":" << "Return" << psStats; return psStats; } int ExtendedSysMon::getUpgradeInfo(const QString pkgCommand, const int pkgNull) { if (debug) qDebug() << "[getUpgradeInfo]"; if (debug) qDebug() << "[getUpgradeInfo]" << ":" << "Run function with cmd" << pkgCommand; if (debug) qDebug() << "[getUpgradeInfo]" << ":" << "Run function with number of null lines" << pkgNull; int count = 0; QString cmd = QString("bash -c \"") + pkgCommand + QString("\""); QProcess command; QString qoutput = QString(""); if (debug) qDebug() << "[getUpgradeInfo]" << ":" << "Run cmd" << cmd; command.start(cmd); command.waitForFinished(-1); if (debug) qDebug() << "[getUpgradeInfo]" << ":" << "Cmd returns" << command.exitCode(); qoutput = QTextCodec::codecForMib(106)->toUnicode(command.readAllStandardOutput()).trimmed(); count = qoutput.split(QChar('\n'), QString::SkipEmptyParts).count(); if (debug) qDebug() << "[getUpgradeInfo]" << ":" << "Return" << (count - pkgNull); return (count - pkgNull); } bool ExtendedSysMon::sourceRequestEvent(const QString &name) { if (debug) qDebug() << "[sourceRequestEvent]"; if (debug) qDebug() << "[sourceRequestEvent]" << ":" << "Run function with source name" << name; return updateSourceEvent(name); } bool ExtendedSysMon::updateSourceEvent(const QString &source) { if (debug) qDebug() << "[updateSourceEvent]"; if (debug) qDebug() << "[updateSourceEvent]" << ":" << "Run function with source name" << source; QString key; if (source == QString("custom")) { for (int i=0; i