mirror of
https://github.com/arcan1s/awesome-widgets.git
synced 2025-07-23 02:29:56 +00:00
system source, some code improvements
This commit is contained in:
@ -30,13 +30,13 @@ class AbstractExtSysMonSource : public QObject
|
||||
public:
|
||||
explicit AbstractExtSysMonSource(QObject *_parent, const QStringList &)
|
||||
: QObject(_parent){};
|
||||
virtual ~AbstractExtSysMonSource(){};
|
||||
~AbstractExtSysMonSource() override = default;
|
||||
virtual QVariant data(const QString &_source) = 0;
|
||||
virtual QVariantMap initialData(const QString &_source) const = 0;
|
||||
virtual void run() = 0;
|
||||
virtual QStringList sources() const = 0;
|
||||
// used by extensions
|
||||
int index(const QString &_source) const
|
||||
static int index(const QString &_source)
|
||||
{
|
||||
QRegExp rx("\\d+");
|
||||
rx.indexIn(_source);
|
||||
|
@ -32,15 +32,15 @@ public:
|
||||
const int TREND_LIMIT = 20;
|
||||
|
||||
explicit BatterySource(QObject *_parent, const QStringList &_args);
|
||||
virtual ~BatterySource();
|
||||
~BatterySource() override;
|
||||
QStringList getSources();
|
||||
QVariant data(const QString &_source);
|
||||
QVariantMap initialData(const QString &_source) const;
|
||||
void run();
|
||||
QStringList sources() const;
|
||||
QVariant data(const QString &_source) override;
|
||||
QVariantMap initialData(const QString &_source) const override;
|
||||
void run() override;
|
||||
QStringList sources() const override;
|
||||
|
||||
private:
|
||||
double approximate(const QList<int> &_trend);
|
||||
static double approximate(const QList<int> &_trend);
|
||||
void calculateRates();
|
||||
// configuration and values
|
||||
int m_batteriesCount = 0;
|
||||
|
@ -32,11 +32,11 @@ class CustomSource : public AbstractExtSysMonSource
|
||||
|
||||
public:
|
||||
explicit CustomSource(QObject *_parent, const QStringList &_args);
|
||||
virtual ~CustomSource();
|
||||
QVariant data(const QString &_source);
|
||||
QVariantMap initialData(const QString &_source) const;
|
||||
void run(){};
|
||||
QStringList sources() const;
|
||||
~CustomSource() override;
|
||||
QVariant data(const QString &_source) override;
|
||||
QVariantMap initialData(const QString &_source) const override;
|
||||
void run() override{};
|
||||
QStringList sources() const override;
|
||||
|
||||
private:
|
||||
QStringList getSources();
|
||||
|
@ -29,11 +29,11 @@ class DesktopSource : public AbstractExtSysMonSource
|
||||
|
||||
public:
|
||||
explicit DesktopSource(QObject *_parent, const QStringList &_args);
|
||||
virtual ~DesktopSource();
|
||||
QVariant data(const QString &_source);
|
||||
QVariantMap initialData(const QString &_source) const;
|
||||
void run(){};
|
||||
QStringList sources() const;
|
||||
~DesktopSource() override;
|
||||
QVariant data(const QString &_source) override;
|
||||
QVariantMap initialData(const QString &_source) const override;
|
||||
void run() override{};
|
||||
QStringList sources() const override;
|
||||
};
|
||||
|
||||
|
||||
|
@ -35,9 +35,8 @@ GPULoadSource::GPULoadSource(QObject *_parent, const QStringList &_args)
|
||||
|
||||
m_process = new QProcess(nullptr);
|
||||
// fucking magic from http://doc.qt.io/qt-5/qprocess.html#finished
|
||||
connect(m_process,
|
||||
static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
|
||||
[this](int, QProcess::ExitStatus) { return updateValue(); });
|
||||
connect(m_process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
|
||||
[=](int, QProcess::ExitStatus) { return updateValue(); });
|
||||
m_process->waitForFinished(0);
|
||||
}
|
||||
|
||||
|
@ -31,12 +31,12 @@ class GPULoadSource : public AbstractExtSysMonSource
|
||||
|
||||
public:
|
||||
explicit GPULoadSource(QObject *_parent, const QStringList &_args);
|
||||
virtual ~GPULoadSource();
|
||||
~GPULoadSource() override;
|
||||
static QString autoGpu();
|
||||
QVariant data(const QString &_source);
|
||||
QVariantMap initialData(const QString &_source) const;
|
||||
void run();
|
||||
QStringList sources() const;
|
||||
QVariant data(const QString &_source) override;
|
||||
QVariantMap initialData(const QString &_source) const override;
|
||||
void run() override;
|
||||
QStringList sources() const override;
|
||||
|
||||
private slots:
|
||||
void updateValue();
|
||||
|
@ -35,9 +35,8 @@ GPUTemperatureSource::GPUTemperatureSource(QObject *_parent, const QStringList &
|
||||
|
||||
m_process = new QProcess(nullptr);
|
||||
// fucking magic from http://doc.qt.io/qt-5/qprocess.html#finished
|
||||
connect(m_process,
|
||||
static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
|
||||
[this](int, QProcess::ExitStatus) { return updateValue(); });
|
||||
connect(m_process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
|
||||
[=](int, QProcess::ExitStatus) { return updateValue(); });
|
||||
m_process->waitForFinished(0);
|
||||
}
|
||||
|
||||
|
@ -31,11 +31,11 @@ class GPUTemperatureSource : public AbstractExtSysMonSource
|
||||
|
||||
public:
|
||||
explicit GPUTemperatureSource(QObject *_parent, const QStringList &_args);
|
||||
virtual ~GPUTemperatureSource();
|
||||
QVariant data(const QString &_source);
|
||||
QVariantMap initialData(const QString &_source) const;
|
||||
void run();
|
||||
QStringList sources() const;
|
||||
~GPUTemperatureSource() override;
|
||||
QVariant data(const QString &_source) override;
|
||||
QVariantMap initialData(const QString &_source) const override;
|
||||
void run() override;
|
||||
QStringList sources() const override;
|
||||
|
||||
private slots:
|
||||
void updateValue();
|
||||
|
@ -40,9 +40,8 @@ HDDTemperatureSource::HDDTemperatureSource(QObject *_parent, const QStringList &
|
||||
for (auto &device : m_devices) {
|
||||
m_processes[device] = new QProcess(nullptr);
|
||||
// fucking magic from http://doc.qt.io/qt-5/qprocess.html#finished
|
||||
connect(m_processes[device],
|
||||
static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
|
||||
[this, device](int, QProcess::ExitStatus) { return updateValue(device); });
|
||||
connect(m_processes[device], QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
|
||||
[=](int, QProcess::ExitStatus) { return updateValue(device); });
|
||||
m_processes[device]->waitForFinished(0);
|
||||
}
|
||||
}
|
||||
|
@ -31,12 +31,12 @@ class HDDTemperatureSource : public AbstractExtSysMonSource
|
||||
|
||||
public:
|
||||
explicit HDDTemperatureSource(QObject *_parent, const QStringList &_args);
|
||||
virtual ~HDDTemperatureSource();
|
||||
~HDDTemperatureSource() override;
|
||||
static QStringList allHdd();
|
||||
QVariant data(const QString &_source);
|
||||
QVariantMap initialData(const QString &_source) const;
|
||||
void run(){};
|
||||
QStringList sources() const;
|
||||
QVariant data(const QString &_source) override;
|
||||
QVariantMap initialData(const QString &_source) const override;
|
||||
void run() override{};
|
||||
QStringList sources() const override;
|
||||
|
||||
private slots:
|
||||
void updateValue(const QString &_device);
|
||||
|
@ -29,11 +29,11 @@ class LoadSource : public AbstractExtSysMonSource
|
||||
|
||||
public:
|
||||
explicit LoadSource(QObject *_parent, const QStringList &_args);
|
||||
virtual ~LoadSource();
|
||||
QVariant data(const QString &_source);
|
||||
QVariantMap initialData(const QString &_source) const;
|
||||
void run(){};
|
||||
QStringList sources() const;
|
||||
~LoadSource() override;
|
||||
QVariant data(const QString &_source) override;
|
||||
QVariantMap initialData(const QString &_source) const override;
|
||||
void run() override{};
|
||||
QStringList sources() const override;
|
||||
};
|
||||
|
||||
|
||||
|
@ -19,6 +19,8 @@
|
||||
#include "networksource.h"
|
||||
|
||||
#include <QNetworkInterface>
|
||||
#include <QProcess>
|
||||
#include <QTextCodec>
|
||||
|
||||
#include "awdebug.h"
|
||||
|
||||
@ -28,6 +30,12 @@ NetworkSource::NetworkSource(QObject *_parent, const QStringList &_args)
|
||||
{
|
||||
Q_ASSERT(_args.count() == 0);
|
||||
qCDebug(LOG_ESS) << __PRETTY_FUNCTION__;
|
||||
|
||||
m_process = new QProcess(nullptr);
|
||||
// fucking magic from http://doc.qt.io/qt-5/qprocess.html#finished
|
||||
connect(m_process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
|
||||
[=](int, QProcess::ExitStatus) { return updateSsid(); });
|
||||
m_process->waitForFinished(0);
|
||||
}
|
||||
|
||||
|
||||
@ -41,23 +49,9 @@ QVariant NetworkSource::data(const QString &_source)
|
||||
{
|
||||
qCDebug(LOG_ESS) << "Source" << _source;
|
||||
|
||||
if (_source == "network/current/name") {
|
||||
QString device = "lo";
|
||||
QList<QNetworkInterface> rawInterfaceList = QNetworkInterface::allInterfaces();
|
||||
qCInfo(LOG_ESS) << "Devices" << rawInterfaceList;
|
||||
for (auto &interface : rawInterfaceList) {
|
||||
if ((interface.flags().testFlag(QNetworkInterface::IsLoopBack))
|
||||
|| (interface.flags().testFlag(QNetworkInterface::IsPointToPoint)))
|
||||
continue;
|
||||
if (interface.addressEntries().isEmpty())
|
||||
continue;
|
||||
device = interface.name();
|
||||
break;
|
||||
}
|
||||
return device;
|
||||
}
|
||||
|
||||
return QVariant();
|
||||
if (!m_values.contains(_source))
|
||||
run();
|
||||
return m_values.take(_source);
|
||||
}
|
||||
|
||||
|
||||
@ -72,16 +66,66 @@ QVariantMap NetworkSource::initialData(const QString &_source) const
|
||||
data["name"] = "Current network device name";
|
||||
data["type"] = "QString";
|
||||
data["units"] = "";
|
||||
} else if (_source == "network/current/ssid") {
|
||||
data["min"] = "";
|
||||
data["max"] = "";
|
||||
data["name"] = "Current SSID name";
|
||||
data["type"] = "QString";
|
||||
data["units"] = "";
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
void NetworkSource::run()
|
||||
{
|
||||
m_values["network/current/name"] = NetworkSource::getCurrentDevice();
|
||||
m_process->start("iwgetid -r");
|
||||
}
|
||||
|
||||
|
||||
QStringList NetworkSource::sources() const
|
||||
{
|
||||
QStringList sources;
|
||||
sources.append("network/current/name");
|
||||
sources.append("network/current/ssid");
|
||||
|
||||
return sources;
|
||||
}
|
||||
|
||||
|
||||
void NetworkSource::updateSsid()
|
||||
{
|
||||
qCInfo(LOG_ESS) << "Cmd returns" << m_process->exitCode();
|
||||
QString qdebug
|
||||
= QTextCodec::codecForMib(106)->toUnicode(m_process->readAllStandardError()).trimmed();
|
||||
qCInfo(LOG_ESS) << "Error" << qdebug;
|
||||
QString qoutput
|
||||
= QTextCodec::codecForMib(106)->toUnicode(m_process->readAllStandardOutput()).trimmed();
|
||||
qCInfo(LOG_ESS) << "Output" << qoutput;
|
||||
|
||||
m_values["network/current/ssid"] = qoutput;
|
||||
}
|
||||
|
||||
|
||||
QString NetworkSource::getCurrentDevice()
|
||||
{
|
||||
qCDebug(LOG_ESS) << "Get current devices";
|
||||
|
||||
QString device = "lo";
|
||||
auto rawInterfaceList = QNetworkInterface::allInterfaces();
|
||||
qCInfo(LOG_ESS) << "Devices" << rawInterfaceList;
|
||||
|
||||
for (auto &interface : rawInterfaceList) {
|
||||
if ((interface.flags().testFlag(QNetworkInterface::IsLoopBack))
|
||||
|| (interface.flags().testFlag(QNetworkInterface::IsPointToPoint)))
|
||||
continue;
|
||||
if (interface.addressEntries().isEmpty())
|
||||
continue;
|
||||
device = interface.name();
|
||||
break;
|
||||
}
|
||||
|
||||
return device;
|
||||
}
|
||||
|
@ -23,17 +23,27 @@
|
||||
#include "abstractextsysmonsource.h"
|
||||
|
||||
|
||||
class QProcess;
|
||||
|
||||
class NetworkSource : public AbstractExtSysMonSource
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit NetworkSource(QObject *_parent, const QStringList &_args);
|
||||
virtual ~NetworkSource();
|
||||
QVariant data(const QString &_source);
|
||||
QVariantMap initialData(const QString &_source) const;
|
||||
void run(){};
|
||||
QStringList sources() const;
|
||||
~NetworkSource() override;
|
||||
QVariant data(const QString &_source) override;
|
||||
QVariantMap initialData(const QString &_source) const override;
|
||||
void run() override;
|
||||
QStringList sources() const override;
|
||||
|
||||
private slots:
|
||||
void updateSsid();
|
||||
|
||||
private:
|
||||
QVariantHash m_values;
|
||||
QProcess *m_process = nullptr;
|
||||
static QString getCurrentDevice();
|
||||
};
|
||||
|
||||
|
||||
|
@ -66,7 +66,7 @@ QVariant PlayerSource::data(const QString &_source)
|
||||
}
|
||||
|
||||
|
||||
QString PlayerSource::getAutoMpris() const
|
||||
QString PlayerSource::getAutoMpris()
|
||||
{
|
||||
QDBusMessage listServices
|
||||
= QDBusConnection::sessionBus().interface()->call(QDBus::BlockWithGui, "ListNames");
|
||||
@ -282,7 +282,7 @@ void PlayerSource::mpdSocketWritten(const qint64 _bytes)
|
||||
}
|
||||
|
||||
|
||||
QVariantHash PlayerSource::defaultInfo() const
|
||||
QVariantHash PlayerSource::defaultInfo()
|
||||
{
|
||||
QVariantHash info;
|
||||
info["player/album"] = "unknown";
|
||||
@ -300,7 +300,7 @@ QVariantHash PlayerSource::getPlayerMpdInfo()
|
||||
if (m_mpdSocket.state() == QAbstractSocket::UnconnectedState) {
|
||||
// connect to host
|
||||
qCInfo(LOG_ESS) << "Connect to" << m_mpdAddress << m_mpdPort;
|
||||
m_mpdSocket.connectToHost(m_mpdAddress, m_mpdPort);
|
||||
m_mpdSocket.connectToHost(m_mpdAddress, static_cast<quint16>(m_mpdPort));
|
||||
} else if (m_mpdSocket.state() == QAbstractSocket::ConnectedState) {
|
||||
// send request
|
||||
if (m_mpdSocket.write(MPD_STATUS_REQUEST) == -1)
|
||||
@ -315,11 +315,11 @@ QVariantHash PlayerSource::getPlayerMprisInfo(const QString &_mpris) const
|
||||
{
|
||||
qCDebug(LOG_ESS) << "MPRIS" << _mpris;
|
||||
|
||||
QVariantHash info = defaultInfo();
|
||||
auto info = defaultInfo();
|
||||
if (_mpris.isEmpty())
|
||||
return info;
|
||||
|
||||
QDBusConnection bus = QDBusConnection::sessionBus();
|
||||
auto bus = QDBusConnection::sessionBus();
|
||||
// comes from the following request:
|
||||
// qdbus org.mpris.MediaPlayer2.vlc /org/mpris/MediaPlayer2
|
||||
// org.freedesktop.DBus.Properties.Get org.mpris.MediaPlayer2.Player
|
||||
@ -328,12 +328,12 @@ QVariantHash PlayerSource::getPlayerMprisInfo(const QString &_mpris) const
|
||||
// dbus-send --print-reply --session --dest=org.mpris.MediaPlayer2.vlc
|
||||
// /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get
|
||||
// string:'org.mpris.MediaPlayer2.Player' string:'Metadata'
|
||||
QVariantList args = QVariantList({"org.mpris.MediaPlayer2.Player", "Metadata"});
|
||||
auto args = QVariantList({"org.mpris.MediaPlayer2.Player", "Metadata"});
|
||||
QDBusMessage request = QDBusMessage::createMethodCall(
|
||||
QString("org.mpris.MediaPlayer2.%1").arg(_mpris), "/org/mpris/MediaPlayer2",
|
||||
"org.freedesktop.DBus.Properties", "Get");
|
||||
request.setArguments(args);
|
||||
QDBusMessage response = bus.call(request, QDBus::BlockWithGui, REQUEST_TIMEOUT);
|
||||
auto response = bus.call(request, QDBus::BlockWithGui, REQUEST_TIMEOUT);
|
||||
if ((response.type() != QDBusMessage::ReplyMessage) || (response.arguments().isEmpty())) {
|
||||
qCWarning(LOG_ESS) << "Error message" << response.errorMessage();
|
||||
} else {
|
||||
|
@ -35,12 +35,12 @@ public:
|
||||
const char *MPD_STATUS_REQUEST = "currentsong\nstatus\n";
|
||||
|
||||
explicit PlayerSource(QObject *_parent, const QStringList &_args);
|
||||
virtual ~PlayerSource();
|
||||
QVariant data(const QString &_source);
|
||||
QString getAutoMpris() const;
|
||||
QVariantMap initialData(const QString &_source) const;
|
||||
void run();
|
||||
QStringList sources() const;
|
||||
~PlayerSource() override;
|
||||
QVariant data(const QString &_source) override;
|
||||
static QString getAutoMpris();
|
||||
QVariantMap initialData(const QString &_source) const override;
|
||||
void run() override;
|
||||
QStringList sources() const override;
|
||||
// additional method to build dynamic tags
|
||||
static QString buildString(const QString &_current, const QString &_value, const int _s);
|
||||
static QString stripString(const QString &_value, const int _s);
|
||||
@ -53,7 +53,7 @@ private slots:
|
||||
void mpdSocketWritten(const qint64 _bytes);
|
||||
|
||||
private:
|
||||
inline QVariantHash defaultInfo() const;
|
||||
static inline QVariantHash defaultInfo();
|
||||
QVariantHash getPlayerMpdInfo();
|
||||
QVariantHash getPlayerMprisInfo(const QString &_mpris) const;
|
||||
QTcpSocket m_mpdSocket;
|
||||
|
@ -29,11 +29,11 @@ class ProcessesSource : public AbstractExtSysMonSource
|
||||
|
||||
public:
|
||||
explicit ProcessesSource(QObject *_parent, const QStringList &_args);
|
||||
virtual ~ProcessesSource();
|
||||
QVariant data(const QString &_source);
|
||||
QVariantMap initialData(const QString &_source) const;
|
||||
void run();
|
||||
QStringList sources() const;
|
||||
~ProcessesSource() override;
|
||||
QVariant data(const QString &_source) override;
|
||||
QVariantMap initialData(const QString &_source) const override;
|
||||
void run() override;
|
||||
QStringList sources() const override;
|
||||
|
||||
private:
|
||||
// configuration and values
|
||||
|
@ -32,11 +32,11 @@ class QuotesSource : public AbstractExtSysMonSource
|
||||
|
||||
public:
|
||||
explicit QuotesSource(QObject *_parent, const QStringList &_args);
|
||||
virtual ~QuotesSource();
|
||||
QVariant data(const QString &_source);
|
||||
QVariantMap initialData(const QString &_source) const;
|
||||
void run(){};
|
||||
QStringList sources() const;
|
||||
~QuotesSource() override;
|
||||
QVariant data(const QString &_source) override;
|
||||
QVariantMap initialData(const QString &_source) const override;
|
||||
void run() override{};
|
||||
QStringList sources() const override;
|
||||
|
||||
private:
|
||||
QStringList getSources();
|
||||
|
@ -32,11 +32,11 @@ class RequestSource : public AbstractExtSysMonSource
|
||||
|
||||
public:
|
||||
explicit RequestSource(QObject *_parent, const QStringList &_args);
|
||||
virtual ~RequestSource();
|
||||
QVariant data(const QString &_source);
|
||||
QVariantMap initialData(const QString &_source) const;
|
||||
void run(){};
|
||||
QStringList sources() const;
|
||||
~RequestSource() override;
|
||||
QVariant data(const QString &_source) override;
|
||||
QVariantMap initialData(const QString &_source) const override;
|
||||
void run() override{};
|
||||
QStringList sources() const override;
|
||||
|
||||
private:
|
||||
QStringList getSources();
|
||||
|
174
sources/extsysmonsources/systeminfosource.cpp
Normal file
174
sources/extsysmonsources/systeminfosource.cpp
Normal file
@ -0,0 +1,174 @@
|
||||
/***************************************************************************
|
||||
* This file is part of awesome-widgets *
|
||||
* *
|
||||
* awesome-widgets 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. *
|
||||
* *
|
||||
* awesome-widgets 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 awesome-widgets. If not, see http://www.gnu.org/licenses/ *
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#include "systeminfosource.h"
|
||||
|
||||
#include <QDBusConnection>
|
||||
#include <QDBusMessage>
|
||||
#include <QDBusVariant>
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include "awdebug.h"
|
||||
|
||||
|
||||
SystemInfoSource::SystemInfoSource(QObject *_parent, const QStringList &_args)
|
||||
: AbstractExtSysMonSource(_parent, _args)
|
||||
{
|
||||
Q_ASSERT(_args.count() == 0);
|
||||
qCDebug(LOG_ESS) << __PRETTY_FUNCTION__;
|
||||
}
|
||||
|
||||
|
||||
SystemInfoSource::~SystemInfoSource()
|
||||
{
|
||||
qCDebug(LOG_ESS) << __PRETTY_FUNCTION__;
|
||||
}
|
||||
|
||||
|
||||
QVariant SystemInfoSource::data(const QString &_source)
|
||||
{
|
||||
qCDebug(LOG_ESS) << "Source" << _source;
|
||||
|
||||
if (!m_values.contains(_source))
|
||||
run();
|
||||
return m_values.take(_source);
|
||||
}
|
||||
|
||||
|
||||
QVariantMap SystemInfoSource::initialData(const QString &_source) const
|
||||
{
|
||||
qCDebug(LOG_ESS) << "Source" << _source;
|
||||
|
||||
QVariantMap data;
|
||||
if (_source == "system/brightness") {
|
||||
data["min"] = 0.0;
|
||||
data["max"] = 100.0;
|
||||
data["name"] = "Screen brightness";
|
||||
data["type"] = "float";
|
||||
data["units"] = "%";
|
||||
} else if (_source == "system/volume") {
|
||||
data["min"] = 0.0;
|
||||
data["max"] = 100.0;
|
||||
data["name"] = "Master volume";
|
||||
data["type"] = "float";
|
||||
data["units"] = "%";
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
void SystemInfoSource::run()
|
||||
{
|
||||
m_values["system/brightness"] = SystemInfoSource::getCurrentBrightness();
|
||||
m_values["system/volume"] = SystemInfoSource::getCurrentVolume();
|
||||
}
|
||||
|
||||
|
||||
QStringList SystemInfoSource::sources() const
|
||||
{
|
||||
QStringList sources;
|
||||
sources.append("system/brightness");
|
||||
sources.append("system/volume");
|
||||
|
||||
return sources;
|
||||
}
|
||||
|
||||
|
||||
QVariant SystemInfoSource::fromDBusVariant(const QVariant &value)
|
||||
{
|
||||
return value.value<QDBusVariant>().variant();
|
||||
}
|
||||
|
||||
|
||||
float SystemInfoSource::getCurrentBrightness()
|
||||
{
|
||||
qCDebug(LOG_ESS) << "Get current brightness";
|
||||
|
||||
auto maxBrightness = sendDBusRequest("org.kde.Solid.PowerManagement",
|
||||
"/org/kde/Solid/PowerManagement/Actions/BrightnessControl",
|
||||
"org.kde.Solid.PowerManagement.Actions.BrightnessControl",
|
||||
"brightnessMax")
|
||||
.toFloat();
|
||||
auto brightness
|
||||
= sendDBusRequest("org.kde.Solid.PowerManagement",
|
||||
"/org/kde/Solid/PowerManagement/Actions/BrightnessControl",
|
||||
"org.kde.Solid.PowerManagement.Actions.BrightnessControl", "brightness")
|
||||
.toFloat();
|
||||
|
||||
return std::round(100.0f * brightness / maxBrightness);
|
||||
}
|
||||
|
||||
|
||||
float SystemInfoSource::getCurrentVolume()
|
||||
{
|
||||
qCDebug(LOG_ESS) << "Get current volume";
|
||||
|
||||
// current device first
|
||||
auto currentMixer
|
||||
= fromDBusVariant(
|
||||
sendDBusRequest("org.kde.kmix", "/Mixers", "org.freedesktop.DBus.Properties", "Get",
|
||||
QVariantList({"org.kde.KMix.MixSet", "currentMasterMixer"})))
|
||||
.toString();
|
||||
|
||||
if (currentMixer.isEmpty()) {
|
||||
qCWarning(LOG_ESS) << "Mixer is empty";
|
||||
return std::numeric_limits<float>::quiet_NaN();
|
||||
}
|
||||
currentMixer.replace(":", "_").replace(".", "_").replace("-", "_");
|
||||
|
||||
// get capture device
|
||||
auto currentControl
|
||||
= fromDBusVariant(
|
||||
sendDBusRequest("org.kde.kmix", "/Mixers", "org.freedesktop.DBus.Properties", "Get",
|
||||
QVariantList({"org.kde.KMix.MixSet", "currentMasterControl"})))
|
||||
.toString();
|
||||
if (currentControl.isEmpty()) {
|
||||
qCWarning(LOG_ESS) << "Control is empty";
|
||||
return std::numeric_limits<float>::quiet_NaN();
|
||||
}
|
||||
currentControl.replace(":", "_").replace(".", "_").replace("-", "_");
|
||||
|
||||
auto path = QString("/Mixers/%1/%2").arg(currentMixer).arg(currentControl);
|
||||
return fromDBusVariant(sendDBusRequest("org.kde.kmix", path, "org.freedesktop.DBus.Properties",
|
||||
"Get", QVariantList({"org.kde.KMix.Control", "volume"})))
|
||||
.toFloat();
|
||||
}
|
||||
|
||||
|
||||
QVariant SystemInfoSource::sendDBusRequest(const QString &destination, const QString &path,
|
||||
const QString &interface, const QString &method,
|
||||
const QVariantList &args)
|
||||
{
|
||||
qCDebug(LOG_ESS) << "Send dbus request" << destination << path << interface << method << args;
|
||||
|
||||
auto bus = QDBusConnection::sessionBus();
|
||||
auto request = QDBusMessage::createMethodCall(destination, path, interface, method);
|
||||
if (!args.isEmpty())
|
||||
request.setArguments(args);
|
||||
|
||||
auto response = bus.call(request, QDBus::BlockWithGui, REQUEST_TIMEOUT);
|
||||
|
||||
if ((response.type() != QDBusMessage::ReplyMessage) || (response.arguments().isEmpty())) {
|
||||
qCWarning(LOG_ESS) << "Error message" << response.errorMessage();
|
||||
return QVariant();
|
||||
} else {
|
||||
return response.arguments().first();
|
||||
}
|
||||
}
|
50
sources/extsysmonsources/systeminfosource.h
Normal file
50
sources/extsysmonsources/systeminfosource.h
Normal file
@ -0,0 +1,50 @@
|
||||
/***************************************************************************
|
||||
* This file is part of awesome-widgets *
|
||||
* *
|
||||
* awesome-widgets 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. *
|
||||
* *
|
||||
* awesome-widgets 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 awesome-widgets. If not, see http://www.gnu.org/licenses/ *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef SYSTEMINFOSOURCE_H
|
||||
#define SYSTEMINFOSOURCE_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
#include "abstractextsysmonsource.h"
|
||||
|
||||
|
||||
class SystemInfoSource : public AbstractExtSysMonSource
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit SystemInfoSource(QObject *_parent, const QStringList &_args);
|
||||
~SystemInfoSource() override;
|
||||
QVariant data(const QString &_source) override;
|
||||
QVariantMap initialData(const QString &_source) const override;
|
||||
void run() override;
|
||||
QStringList sources() const override;
|
||||
|
||||
private:
|
||||
// configuration and values
|
||||
QVariantHash m_values;
|
||||
static QVariant fromDBusVariant(const QVariant &value);
|
||||
static float getCurrentBrightness();
|
||||
static float getCurrentVolume();
|
||||
static QVariant sendDBusRequest(const QString &destination, const QString &path,
|
||||
const QString &interface, const QString &method,
|
||||
const QVariantList &args = QVariantList());
|
||||
};
|
||||
|
||||
|
||||
#endif /* SYSTEMINFOSOURCE_H */
|
@ -32,11 +32,11 @@ class UpgradeSource : public AbstractExtSysMonSource
|
||||
|
||||
public:
|
||||
explicit UpgradeSource(QObject *_parent, const QStringList &_args);
|
||||
virtual ~UpgradeSource();
|
||||
QVariant data(const QString &_source);
|
||||
QVariantMap initialData(const QString &_source) const;
|
||||
void run(){};
|
||||
QStringList sources() const;
|
||||
~UpgradeSource() override;
|
||||
QVariant data(const QString &_source) override;
|
||||
QVariantMap initialData(const QString &_source) const override;
|
||||
void run() override{};
|
||||
QStringList sources() const override;
|
||||
|
||||
private:
|
||||
QStringList getSources();
|
||||
|
@ -32,11 +32,11 @@ class WeatherSource : public AbstractExtSysMonSource
|
||||
|
||||
public:
|
||||
explicit WeatherSource(QObject *_parent, const QStringList &_args);
|
||||
virtual ~WeatherSource();
|
||||
QVariant data(const QString &_source);
|
||||
QVariantMap initialData(const QString &_source) const;
|
||||
void run(){};
|
||||
QStringList sources() const;
|
||||
~WeatherSource() override;
|
||||
QVariant data(const QString &_source) override;
|
||||
QVariantMap initialData(const QString &_source) const override;
|
||||
void run() override{};
|
||||
QStringList sources() const override;
|
||||
|
||||
private:
|
||||
QStringList getSources();
|
||||
|
Reference in New Issue
Block a user