Skip to content

Commit

Permalink
refactor: split formatters and matchers into separated singleton clas…
Browse files Browse the repository at this point in the history
…ses (#166)

* split formatter into separated singleton classes

* split data engine matchers to classes

* nodiscard attribute for formatter methods

* small refactoring in matchers

* fix codefactor warnings

* fix test building
  • Loading branch information
arcan1s committed Apr 30, 2024
1 parent 9557236 commit 64b4618
Show file tree
Hide file tree
Showing 182 changed files with 4,928 additions and 766 deletions.
2 changes: 1 addition & 1 deletion sources/awesome-widget/plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ include_directories(
${Kf6_INCLUDE}
)

file(GLOB SUBPROJECT_SOURCE *.cpp ${PROJECT_TRDPARTY_DIR}/fontdialog/*.cpp ${CMAKE_SOURCE_DIR}/*.cpp)
file(GLOB SUBPROJECT_SOURCE *.cpp formatters/*.cpp matchers/*.cpp ${PROJECT_TRDPARTY_DIR}/fontdialog/*.cpp ${CMAKE_SOURCE_DIR}/*.cpp)
file(GLOB SUBPROJECT_UI *.ui)
file(GLOB SUBPROJECT_NOTIFY *.notifyrc)

Expand Down
2 changes: 1 addition & 1 deletion sources/awesome-widget/plugin/awbugreporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
class QNetworkAccessManager;
class QNetworkReply;

class AWBugReporter : public QObject
class __attribute__((visibility("default"))) AWBugReporter : public QObject
{
Q_OBJECT

Expand Down
2 changes: 1 addition & 1 deletion sources/awesome-widget/plugin/awconfighelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

class QSettings;

class AWConfigHelper : public QObject
class __attribute__((visibility("default"))) AWConfigHelper : public QObject
{
Q_OBJECT

Expand Down
414 changes: 38 additions & 376 deletions sources/awesome-widget/plugin/awdataenginemapper.cpp

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions sources/awesome-widget/plugin/awdataenginemapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
#include <QMultiHash>
#include <QObject>

#include "awkeysaggregator.h"
#include "formatters/awpluginformatter.h"
#include "matchers/awpluginmatchersettings.h"


class AWFormatterHelper;
Expand All @@ -35,16 +36,17 @@ class AWDataEngineMapper : public QObject
explicit AWDataEngineMapper(QObject *_parent = nullptr, AWFormatterHelper *_custom = nullptr);
~AWDataEngineMapper() override = default;
// get methods
[[nodiscard]] AWKeysAggregator::FormatterType formatter(const QString &_key) const;
[[nodiscard]] AWPluginFormaterInterface *formatter(const QString &_key) const;
[[nodiscard]] QStringList keysFromSource(const QString &_source) const;
// set methods
QStringList registerSource(const QString &_source, KSysGuard::Unit _units, const QStringList &_keys);
void setDevices(const QHash<QString, QStringList> &_devices);
void setDevices(const AWPluginMatcherSettings &_settings);

private:
AWFormatterHelper *m_customFormatters = nullptr;
AWPluginMatcherSettings m_settings;
// variables
QHash<QString, QStringList> m_devices;
QHash<QString, AWKeysAggregator::FormatterType> m_formatter;
QHash<QString, AWPluginFormaterInterface *> m_formatter;
QMultiHash<QString, QString> m_map;
};
55 changes: 10 additions & 45 deletions sources/awesome-widget/plugin/awkeycache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,47 +82,7 @@ QStringList AWKeyCache::getRequiredKeys(const QStringList &_keys, const QStringL
}
}

// insert depending keys, refer to AWKeys::calculateValues()
// hddtotmb*
for (auto &key : _allKeys.filter(QRegularExpression("^hddtotmb"))) {
if (!used.contains(key))
continue;
key.remove("hddtotmb");
auto index = key.toInt();
used << QString("hddfreemb%1").arg(index) << QString("hddmb%1").arg(index);
}
// hddtotgb*
for (auto &key : _allKeys.filter(QRegularExpression("^hddtotgb"))) {
if (!used.contains(key))
continue;
key.remove("hddtotgb");
auto index = key.toInt();
used << QString("hddfreegb%1").arg(index) << QString("hddgb%1").arg(index);
}
// mem
if (used.contains("mem"))
used << "memmb"
<< "memtotmb";
// memtotmb
if (used.contains("memtotmb"))
used << "memusedmb"
<< "memfreemb";
// memtotgb
if (used.contains("memtotgb"))
used << "memusedgb"
<< "memfreegb";
// swap
if (used.contains("swap"))
used << "swapmb"
<< "swaptotmb";
// swaptotmb
if (used.contains("swaptotmb"))
used << "swapmb"
<< "swapfreemb";
// memtotgb
if (used.contains("swaptotgb"))
used << "swapgb"
<< "swapfreegb";
// insert keys which depend on others, refer to AWKeys::calculateValues()
// network keys
QStringList netKeys(
{"up", "upkb", "uptot", "uptotkb", "upunits", "down", "downkb", "downtot", "downtotkb", "downunits"});
Expand All @@ -146,18 +106,23 @@ QStringList AWKeyCache::getRequiredKeys(const QStringList &_keys, const QStringL
}


QHash<QString, QStringList> AWKeyCache::loadKeysFromCache()
AWPluginMatcherSettings AWKeyCache::loadKeysFromCache()
{
auto fileName
= QString("%1/awesomewidgets.ndx").arg(QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation));
qCInfo(LOG_AW) << "Cache file" << fileName;
QSettings cache(fileName, QSettings::IniFormat);

QHash<QString, QStringList> devices;
for (auto &group : cache.childGroups()) {
AWPluginMatcherSettings devices;
QHash<QString, QStringList *> groups = {
{"disk", &devices.disk}, {"gpu", &devices.gpu}, {"mount", &devices.mount},
{"net", &devices.network}, {"temp", &devices.sensors},
};

for (auto [group, list] : groups.asKeyValueRange()) {
cache.beginGroup(group);
for (auto &key : cache.allKeys())
devices[group].append(cache.value(key).toString());
list->append(cache.value(key).toString());
cache.endGroup();
}

Expand Down
5 changes: 3 additions & 2 deletions sources/awesome-widget/plugin/awkeycache.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@

#pragma once

#include <QHash>
#include <QString>
#include <QVariant>

#include <matchers/awpluginmatchersettings.h>


namespace AWKeyCache
{
bool addKeyToCache(const QString &_type, const QString &_key = "");
QStringList getRequiredKeys(const QStringList &_keys, const QStringList &_bars, const QVariantMap &_tooltip,
const QStringList &_userKeys, const QStringList &_allKeys);
QHash<QString, QStringList> loadKeysFromCache();
AWPluginMatcherSettings loadKeysFromCache();
} // namespace AWKeyCache
28 changes: 10 additions & 18 deletions sources/awesome-widget/plugin/awkeyoperations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,7 @@ AWKeyOperations::AWKeyOperations(QObject *_parent)
}


QStringList AWKeyOperations::devices(const QString &_type) const
{
qCDebug(LOG_AW) << "Looking for type" << _type;

return m_devices[_type];
}


QHash<QString, QStringList> AWKeyOperations::devices() const
AWPluginMatcherSettings AWKeyOperations::devices() const
{
return m_devices;
}
Expand Down Expand Up @@ -88,15 +80,15 @@ QStringList AWKeyOperations::dictKeys() const
allKeys.append(QString("cpu%1").arg(i));
}
// temperature
for (auto i = 0; i < m_devices["temp"].count(); ++i)
for (auto i = 0; i < m_devices.sensors.count(); ++i)
allKeys.append(QString("temp%1").arg(i));
// gpu
for (auto i = 0; i < m_devices["gpu"].count(); ++i) {
for (auto i = 0; i < m_devices.gpu.count(); ++i) {
allKeys.append(QString("gpu%1").arg(i));
allKeys.append(QString("gputemp%1").arg(i));
}
// hdd
for (auto i = 0; i < m_devices["mount"].count(); ++i) {
for (auto i = 0; i < m_devices.mount.count(); ++i) {
allKeys.append(QString("hddmb%1").arg(i));
allKeys.append(QString("hddgb%1").arg(i));
allKeys.append(QString("hddfreemb%1").arg(i));
Expand All @@ -106,12 +98,12 @@ QStringList AWKeyOperations::dictKeys() const
allKeys.append(QString("hdd%1").arg(i));
}
// hdd speed
for (auto i = 0; i < m_devices["disk"].count(); ++i) {
for (auto i = 0; i < m_devices.disk.count(); ++i) {
allKeys.append(QString("hddr%1").arg(i));
allKeys.append(QString("hddw%1").arg(i));
}
// network
for (auto i = 0; i < m_devices["net"].count(); ++i) {
for (auto i = 0; i < m_devices.network.count(); ++i) {
allKeys.append(QString("downunits%1").arg(i));
allKeys.append(QString("upunits%1").arg(i));
allKeys.append(QString("downtotkb%1").arg(i));
Expand Down Expand Up @@ -224,15 +216,15 @@ QString AWKeyOperations::infoByKey(const QString &_key) const
} else if (_key.contains(hddrwRegExp)) {
auto index = _key;
index.remove(hddrwRegExp);
output = m_devices["disk"][index.toInt()];
output = m_devices.disk[index.toInt()];
} else if (_key.contains(hddMatchRegExp)) {
auto index = _key;
index.remove(hddRegExp);
output = m_devices["mount"][index.toInt()];
output = m_devices.mount[index.toInt()];
} else if (_key.contains(netMatchRegExp)) {
auto index = _key;
index.remove(netRegExp);
output = m_devices["net"][index.toInt()];
output = m_devices.network[index.toInt()];
} else if (_key.startsWith("pkgcount")) {
auto item = m_extUpgrade->itemByTag(_key, stripped);
if (item)
Expand All @@ -248,7 +240,7 @@ QString AWKeyOperations::infoByKey(const QString &_key) const
} else if (_key.startsWith("temp")) {
auto index = _key;
index.remove("temp");
output = m_devices["temp"][index.toInt()];
output = m_devices.sensors[index.toInt()];
} else if (_key.startsWith("response")) {
auto item = m_extNetRequest->itemByTag(_key, stripped);
if (item)
Expand Down
6 changes: 3 additions & 3 deletions sources/awesome-widget/plugin/awkeyoperations.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <QObject>

#include "extitemaggregator.h"
#include "matchers/awpluginmatchersettings.h"


class AWCustomKeysHelper;
Expand All @@ -38,8 +39,7 @@ class AWKeyOperations : public QObject
public:
explicit AWKeyOperations(QObject *_parent = nullptr);
~AWKeyOperations() override = default;
[[nodiscard]] QStringList devices(const QString &_type) const;
[[nodiscard]] QHash<QString, QStringList> devices() const;
[[nodiscard]] AWPluginMatcherSettings devices() const;
void updateCache();
// keys
[[nodiscard]] QStringList dictKeys() const;
Expand Down Expand Up @@ -73,6 +73,6 @@ public slots:
ExtItemAggregator<ExtUpgrade> *m_extUpgrade = nullptr;
ExtItemAggregator<ExtWeather> *m_extWeather = nullptr;
// variables
QHash<QString, QStringList> m_devices;
AWPluginMatcherSettings m_devices;
QString m_pattern;
};
24 changes: 2 additions & 22 deletions sources/awesome-widget/plugin/awkeys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,24 +231,10 @@ void AWKeys::updateTextData()
// specified pattern. Usually they are values which depend on several others
void AWKeys::calculateValues()
{
// hddtot*
auto mountDevices = m_keyOperator->devices("mount");
for (auto &device : mountDevices) {
auto index = mountDevices.indexOf(device);
m_values[QString("hddtotmb%1").arg(index)] = m_values[QString("hddfreemb%1").arg(index)].toDouble()
+ m_values[QString("hddmb%1").arg(index)].toDouble();
m_values[QString("hddtotgb%1").arg(index)] = m_values[QString("hddfreegb%1").arg(index)].toDouble()
+ m_values[QString("hddgb%1").arg(index)].toDouble();
}

// memtot*
m_values["memtotmb"] = m_values["memusedmb"].toLongLong() + m_values["memfreemb"].toLongLong();
m_values["memtotgb"] = m_values["memusedgb"].toDouble() + m_values["memfreegb"].toDouble();
// mem
m_values["mem"] = 100.0 * m_values["memmb"].toDouble() / m_values["memtotmb"].toDouble();
auto devices = m_keyOperator->devices();

// up, down, upkb, downkb, upunits, downunits
auto netIndex = m_keyOperator->devices("net").indexOf(m_values["netdev"].toString());
auto netIndex = devices.network.indexOf(m_values["netdev"].toString());
m_values["down"] = m_values[QString("down%1").arg(netIndex)];
m_values["downkb"] = m_values[QString("downkb%1").arg(netIndex)];
m_values["downtot"] = m_values[QString("downtot%1").arg(netIndex)];
Expand All @@ -260,12 +246,6 @@ void AWKeys::calculateValues()
m_values["uptotkb"] = m_values[QString("uptotkb%1").arg(netIndex)];
m_values["upunits"] = m_values[QString("upunits%1").arg(netIndex)];

// swaptot*
m_values["swaptotmb"] = m_values["swapmb"].toLongLong() + m_values["swapfreemb"].toLongLong();
m_values["swaptotgb"] = m_values["swapgb"].toDouble() + m_values["swapfreegb"].toDouble();
// swap
m_values["swap"] = 100.0 * m_values["swapmb"].toDouble() / m_values["swaptotmb"].toDouble();

// user defined keys
for (auto &key : m_keyOperator->userKeys())
m_values[key] = m_values[m_keyOperator->userKeySource(key)];
Expand Down
2 changes: 1 addition & 1 deletion sources/awesome-widget/plugin/awkeys.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class AWKeyOperations;
class AWKeysAggregator;
class QTimer;

class AWKeys : public QObject
class __attribute__((visibility("default"))) AWKeys : public QObject
{
Q_OBJECT

Expand Down
Loading

0 comments on commit 64b4618

Please sign in to comment.