diff --git a/sources/extsysmon/CMakeLists.txt b/sources/extsysmon/CMakeLists.txt index 5c8beed..7e9857a 100644 --- a/sources/extsysmon/CMakeLists.txt +++ b/sources/extsysmon/CMakeLists.txt @@ -13,16 +13,14 @@ include_directories( file(GLOB SUBPROJECT_DESKTOP_IN *.desktop) file(RELATIVE_PATH SUBPROJECT_DESKTOP ${CMAKE_SOURCE_DIR} ${SUBPROJECT_DESKTOP_IN}) -file(GLOB SUBPROJECT_SOURCE *.cpp sources/*.cpp ${PROJECT_TRDPARTY_DIR}/task/*.cpp ${CMAKE_SOURCE_DIR}/*.cpp) -set(TASK_HEADER ${PROJECT_TRDPARTY_DIR}/task/task.h) +file(GLOB SUBPROJECT_SOURCE *.cpp sources/*.cpp ${CMAKE_SOURCE_DIR}/*.cpp) file(GLOB SUBPROJECT_CONF *.conf) # prepare configure_file(${SUBPROJECT_DESKTOP_IN} ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT_DESKTOP}) # make -qt5_wrap_cpp(TASK_MOC_SOURCE ${TASK_HEADER}) -add_library(${SUBPROJECT} MODULE ${SUBPROJECT_SOURCE} ${TASK_MOC_SOURCE}) +add_library(${SUBPROJECT} MODULE ${SUBPROJECT_SOURCE}) target_link_libraries(${SUBPROJECT} ${PROJECT_LIBRARY} ${Qt_LIBRARIES} ${Kf5_LIBRARIES}) kcoreaddons_desktop_to_json(${SUBPROJECT} ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT_DESKTOP} SERVICE_TYPES plasma-dataengine.desktop) diff --git a/sources/extsysmon/sources/batterysource.cpp b/sources/extsysmon/sources/batterysource.cpp index af4da45..96dfae2 100644 --- a/sources/extsysmon/sources/batterysource.cpp +++ b/sources/extsysmon/sources/batterysource.cpp @@ -44,9 +44,9 @@ QVariant BatterySource::data(QString source) { qCDebug(LOG_ESM) << "Source" << source; - if (!values.contains(source)) + if (!m_values.contains(source)) run(); - QVariant value = values.take(source); + QVariant value = m_values.take(source); return value; } @@ -85,7 +85,7 @@ void BatterySource::run() // adaptor QFile acFile(QString("%1/AC/online").arg(m_acpiPath)); if (acFile.open(QIODevice::ReadOnly)) - values[QString("battery/ac")] + m_values[QString("battery/ac")] = (QString(acFile.readLine()).trimmed().toInt() == 1); acFile.close(); @@ -103,7 +103,7 @@ void BatterySource::run() = QString(currentLevelFile.readLine()).trimmed().toFloat(); float batFull = QString(fullLevelFile.readLine()).trimmed().toFloat(); - values[QString("battery/bat%1").arg(i)] + m_values[QString("battery/bat%1").arg(i)] = static_cast(100 * batCurrent / batFull); currentLevel += batCurrent; fullLevel += batFull; @@ -111,7 +111,7 @@ void BatterySource::run() currentLevelFile.close(); fullLevelFile.close(); } - values[QString("battery/bat")] + m_values[QString("battery/bat")] = static_cast(100 * currentLevel / fullLevel); } diff --git a/sources/extsysmon/sources/batterysource.h b/sources/extsysmon/sources/batterysource.h index 8a1fe3f..09403c7 100644 --- a/sources/extsysmon/sources/batterysource.h +++ b/sources/extsysmon/sources/batterysource.h @@ -39,7 +39,7 @@ private: int m_batteriesCount = 0; QString m_acpiPath; QStringList m_sources; - QVariantHash values; + QVariantHash m_values; }; diff --git a/sources/extsysmon/sources/playersource.cpp b/sources/extsysmon/sources/playersource.cpp index 4a10ccb..df832e1 100644 --- a/sources/extsysmon/sources/playersource.cpp +++ b/sources/extsysmon/sources/playersource.cpp @@ -22,10 +22,9 @@ #include #include #include +#include #include -#include - #include "awdebug.h" @@ -39,12 +38,24 @@ PlayerSource::PlayerSource(QObject *parent, const QStringList args) m_mpdAddress = QString("%1:%2").arg(args.at(1)).arg(args.at(2)); m_mpris = args.at(3); m_symbols = args.at(4).toInt(); + + m_mpdProcess = new QProcess(nullptr); + // fucking magic from http://doc.qt.io/qt-5/qprocess.html#finished + connect(m_mpdProcess, + static_cast( + &QProcess::finished), + [this](int, QProcess::ExitStatus) { return updateValue(); }); + m_mpdProcess->waitForFinished(0); + m_mpdCached = defaultInfo(); } PlayerSource::~PlayerSource() { qCDebug(LOG_ESM) << __PRETTY_FUNCTION__; + + m_mpdProcess->kill(); + m_mpdProcess->deleteLater(); } @@ -52,9 +63,9 @@ QVariant PlayerSource::data(QString source) { qCDebug(LOG_ESM) << "Source" << source; - if (!values.contains(source)) + if (!m_values.contains(source)) run(); - QVariant value = values.take(source); + QVariant value = m_values.take(source); return value; } @@ -152,33 +163,33 @@ void PlayerSource::run() // mpd QHash data = getPlayerMpdInfo(m_mpdAddress); for (auto key : data.keys()) - values[key] = data[key]; + m_values[key] = data[key]; } else if (m_player == QString("mpris")) { // players which supports mpris QString mpris = m_mpris == QString("auto") ? getAutoMpris() : m_mpris; QHash data = getPlayerMprisInfo(mpris); for (auto key : data.keys()) - values[key] = data[key]; + m_values[key] = data[key]; } // dymanic properties // solid - values[QString("player/salbum")] - = stripString(values[QString("player/album")].toString(), m_symbols); - values[QString("player/sartist")] - = stripString(values[QString("player/artist")].toString(), m_symbols); - values[QString("player/stitle")] - = stripString(values[QString("player/title")].toString(), m_symbols); + m_values[QString("player/salbum")] + = stripString(m_values[QString("player/album")].toString(), m_symbols); + m_values[QString("player/sartist")] + = stripString(m_values[QString("player/artist")].toString(), m_symbols); + m_values[QString("player/stitle")] + = stripString(m_values[QString("player/title")].toString(), m_symbols); // dynamic - values[QString("player/dalbum")] - = buildString(values[QString("player/dalbum")].toString(), - values[QString("player/album")].toString(), m_symbols); - values[QString("player/dartist")] - = buildString(values[QString("player/dartist")].toString(), - values[QString("player/artist")].toString(), m_symbols); - values[QString("player/dtitle")] - = buildString(values[QString("player/dtitle")].toString(), - values[QString("player/title")].toString(), m_symbols); + m_values[QString("player/dalbum")] + = buildString(m_values[QString("player/dalbum")].toString(), + m_values[QString("player/album")].toString(), m_symbols); + m_values[QString("player/dartist")] + = buildString(m_values[QString("player/dartist")].toString(), + m_values[QString("player/artist")].toString(), m_symbols); + m_values[QString("player/dtitle")] + = buildString(m_values[QString("player/dtitle")].toString(), + m_values[QString("player/title")].toString(), m_symbols); } @@ -201,6 +212,40 @@ QStringList PlayerSource::sources() const } +void PlayerSource::updateValue() +{ + qCInfo(LOG_LIB) << "Cmd returns" << m_mpdProcess->exitCode(); + QString qdebug = QTextCodec::codecForMib(106) + ->toUnicode(m_mpdProcess->readAllStandardError()) + .trimmed(); + qCInfo(LOG_LIB) << "Error" << qdebug; + QString qoutput = QTextCodec::codecForMib(106) + ->toUnicode(m_mpdProcess->readAllStandardOutput()) + .trimmed(); + qCInfo(LOG_LIB) << "Output" << qoutput; + + for (auto str : qoutput.split(QChar('\n'), QString::SkipEmptyParts)) { + if (str.split(QString(": "), QString::SkipEmptyParts).count() == 2) { + // "Metadata: data" + QString metadata = str.split(QString(": "), QString::SkipEmptyParts) + .first() + .toLower(); + QString data = str.split(QString(": "), QString::SkipEmptyParts) + .last() + .trimmed(); + // there are one more time... + if ((metadata == QString("time")) && (data.contains(QChar(':')))) { + QStringList times = data.split(QString(":")); + m_mpdCached[QString("player/duration")] = times.at(0).toInt(); + m_mpdCached[QString("player/progress")] = times.at(1).toInt(); + } else if (m_metadata.contains(metadata)) { + m_mpdCached[QString("player/%1").arg(metadata)] = data; + } + } + } +} + + QVariantHash PlayerSource::defaultInfo() const { QVariantHash info; @@ -239,40 +284,14 @@ QVariantHash PlayerSource::getPlayerMpdInfo(const QString mpdAddress) const { qCDebug(LOG_ESM) << "MPD" << mpdAddress; - QVariantHash info = defaultInfo(); - // build cmd QString cmd = QString("bash -c \"echo 'currentsong\nstatus\nclose' | curl " "--connect-timeout 1 -fsm 3 telnet://%1\"") .arg(mpdAddress); qCInfo(LOG_ESM) << "cmd" << cmd; - TaskResult process = runTask(cmd); - qCInfo(LOG_ESM) << "Cmd returns" << process.exitCode; - qCInfo(LOG_ESM) << "Error" << process.error; + m_mpdProcess->start(cmd); - QString qoutput - = QTextCodec::codecForMib(106)->toUnicode(process.output).trimmed(); - for (auto str : qoutput.split(QChar('\n'), QString::SkipEmptyParts)) { - if (str.split(QString(": "), QString::SkipEmptyParts).count() == 2) { - // "Metadata: data" - QString metadata = str.split(QString(": "), QString::SkipEmptyParts) - .first() - .toLower(); - QString data = str.split(QString(": "), QString::SkipEmptyParts) - .last() - .trimmed(); - // there are one more time... - if ((metadata == QString("time")) && (data.contains(QChar(':')))) { - QStringList times = data.split(QString(":")); - info[QString("player/duration")] = times.at(0).toInt(); - info[QString("player/progress")] = times.at(1).toInt(); - } else if (m_metadata.contains(metadata)) { - info[QString("player/%1").arg(metadata)] = data; - } - } - } - - return info; + return m_mpdCached; } @@ -351,7 +370,7 @@ QString PlayerSource::buildString(const QString current, const QString value, << "will be stripped after" << s; int index = value.indexOf(current); - if ((current.isEmpty()) || ((index + s + 1) > value.count()))x + if ((current.isEmpty()) || ((index + s + 1) > value.count())) return QString("%1").arg(value.left(s), s, QLatin1Char(' ')); else return QString("%1").arg(value.mid(index + 1, s), s, QLatin1Char(' ')); diff --git a/sources/extsysmon/sources/playersource.h b/sources/extsysmon/sources/playersource.h index 14904bd..0d8bbfc 100644 --- a/sources/extsysmon/sources/playersource.h +++ b/sources/extsysmon/sources/playersource.h @@ -23,6 +23,8 @@ #include "abstractextsysmonsource.h" +class QProcess; + class PlayerSource : public AbstractExtSysMonSource { public: @@ -33,6 +35,9 @@ public: void run(); QStringList sources() const; +private slots: + void updateValue(); + private: inline QVariantHash defaultInfo() const; QString getAutoMpris() const; @@ -44,13 +49,15 @@ private: QString stripString(const QString value, const int s) const; // configuration and values QString m_mpdAddress; + QVariantHash m_mpdCached; + QProcess *m_mpdProcess = nullptr; QString m_mpris; QString m_player; int m_symbols; QStringList m_metadata = QStringList() << QString("album") << QString("artist") << QString("title"); - QVariantHash values; + QVariantHash m_values; }; diff --git a/sources/extsysmon/sources/processessource.cpp b/sources/extsysmon/sources/processessource.cpp index a84687c..6054909 100644 --- a/sources/extsysmon/sources/processessource.cpp +++ b/sources/extsysmon/sources/processessource.cpp @@ -41,9 +41,9 @@ QVariant ProcessesSource::data(QString source) { qCDebug(LOG_ESM) << "Source" << source; - if (!values.contains(source)) + if (!m_values.contains(source)) run(); - QVariant value = values.take(source); + QVariant value = m_values.take(source); return value; } @@ -98,9 +98,9 @@ void ProcessesSource::run() running.append(cmdFile.readAll()); } - values[QString("ps/running/count")] = running.count(); - values[QString("ps/running/list")] = running; - values[QString("ps/total/count")] = directories.count(); + m_values[QString("ps/running/count")] = running.count(); + m_values[QString("ps/running/list")] = running; + m_values[QString("ps/total/count")] = directories.count(); } diff --git a/sources/extsysmon/sources/processessource.h b/sources/extsysmon/sources/processessource.h index e9e12df..3d7c3a3 100644 --- a/sources/extsysmon/sources/processessource.h +++ b/sources/extsysmon/sources/processessource.h @@ -35,7 +35,7 @@ public: private: // configuration and values - QVariantHash values; + QVariantHash m_values; }; diff --git a/sources/extsysmon/sources/quotessource.cpp b/sources/extsysmon/sources/quotessource.cpp index fff4473..4a44b09 100644 --- a/sources/extsysmon/sources/quotessource.cpp +++ b/sources/extsysmon/sources/quotessource.cpp @@ -47,12 +47,12 @@ QVariant QuotesSource::data(QString source) int ind = index(source); source.remove(QString("quotes/")); - if (!values.contains(source)) { + if (!m_values.contains(source)) { QVariantHash data = extQuotes->itemByTagNumber(ind)->run(); for (auto key : data.keys()) - values[key] = data[key]; + m_values[key] = data[key]; } - QVariant value = values.take(source); + QVariant value = m_values.take(source); return value; } diff --git a/sources/extsysmon/sources/quotessource.h b/sources/extsysmon/sources/quotessource.h index 2dbc4c5..ae09f0b 100644 --- a/sources/extsysmon/sources/quotessource.h +++ b/sources/extsysmon/sources/quotessource.h @@ -41,7 +41,7 @@ private: // configuration and values ExtItemAggregator *extQuotes; QStringList m_sources; - QVariantHash values; + QVariantHash m_values; }; diff --git a/sources/extsysmon/sources/weathersource.cpp b/sources/extsysmon/sources/weathersource.cpp index 13a1fc6..910ad31 100644 --- a/sources/extsysmon/sources/weathersource.cpp +++ b/sources/extsysmon/sources/weathersource.cpp @@ -47,12 +47,12 @@ QVariant WeatherSource::data(QString source) int ind = index(source); source.remove(QString("weather/")); - if (!values.contains(source)) { + if (!m_values.contains(source)) { QVariantHash data = extWeather->itemByTagNumber(ind)->run(); for (auto key : data.keys()) - values[key] = data[key]; + m_values[key] = data[key]; } - QVariant value = values.take(source); + QVariant value = m_values.take(source); return value; } diff --git a/sources/extsysmon/sources/weathersource.h b/sources/extsysmon/sources/weathersource.h index 5132fff..50b9f83 100644 --- a/sources/extsysmon/sources/weathersource.h +++ b/sources/extsysmon/sources/weathersource.h @@ -41,7 +41,7 @@ private: // configuration and values ExtItemAggregator *extWeather; QStringList m_sources; - QVariantHash values; + QVariantHash m_values; }; diff --git a/sources/version.h.in b/sources/version.h.in index 5587fe9..97bd9c7 100644 --- a/sources/version.h.in +++ b/sources/version.h.in @@ -14,7 +14,7 @@ #define EMAIL "@PROJECT_CONTACT@" #define LICENSE "@PROJECT_LICENSE@" #define TRDPARTY_LICENSE \ - "tasks,BSD,https://github.com/mhogomchungu/tasks;QReplyTimeout " \ + "QReplyTimeout " \ "wrapper,no,http://codereview.stackexchange.com/questions/30031/" \ "qnetworkreply-network-reply-timeout-helper" #define SPECIAL_THANKS \