refactor: review delete and new operators

This commit is contained in:
2024-04-17 17:43:33 +03:00
parent 397b523180
commit 95d5bb99c0
46 changed files with 175 additions and 311 deletions

View File

@ -58,6 +58,12 @@ AWAbstractPairConfig::~AWAbstractPairConfig()
}
void AWAbstractPairConfig::setHelper(std::unique_ptr<AWAbstractPairHelper> _helper)
{
m_helper = std::move(_helper);
}
void AWAbstractPairConfig::showDialog()
{
// update dialog
@ -133,7 +139,7 @@ void AWAbstractPairConfig::execDialog()
auto ret = exec();
QHash<QString, QString> data;
for (auto selector : m_selectors) {
QPair<QString, QString> select = selector->current();
auto select = selector->current();
if (select.first.isEmpty())
continue;
data[select.first] = select.second;

View File

@ -35,12 +35,7 @@ class AWAbstractPairConfig : public QDialog
public:
explicit AWAbstractPairConfig(QWidget *_parent = nullptr, bool _hasEdit = false, QStringList _keys = {});
~AWAbstractPairConfig() override;
template <class T> void initHelper()
{
delete m_helper;
m_helper = new T(this);
}
void setHelper(std::unique_ptr<AWAbstractPairHelper> _helper);
void showDialog();
// properties
void setEditable(bool _first, bool _second);
@ -52,7 +47,7 @@ private slots:
private:
QPushButton *m_editButton = nullptr;
Ui::AWAbstractPairConfig *ui = nullptr;
AWAbstractPairHelper *m_helper = nullptr;
std::unique_ptr<AWAbstractPairHelper> m_helper;
QList<AWAbstractSelector *> m_selectors;
// properties
QPair<bool, bool> m_editable = {false, false};

View File

@ -33,12 +33,17 @@ AWBugReporter::AWBugReporter(QObject *_parent)
: QObject(_parent)
{
qCDebug(LOG_AW) << __PRETTY_FUNCTION__;
m_manager = new QNetworkAccessManager(nullptr);
connect(m_manager, &QNetworkAccessManager::finished, this, &AWBugReporter::issueReplyReceived);
}
AWBugReporter::~AWBugReporter()
{
qCDebug(LOG_AW) << __PRETTY_FUNCTION__;
m_manager->deleteLater();
}
@ -72,8 +77,6 @@ void AWBugReporter::sendBugReport(const QString &_title, const QString &_body)
{
qCDebug(LOG_AW) << "Send bug report with title" << _title << "and body" << _body;
auto manager = new QNetworkAccessManager(nullptr);
connect(manager, &QNetworkAccessManager::finished, this, &AWBugReporter::issueReplyReceived);
auto request = QNetworkRequest(QUrl(BUGTRACKER_API));
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
@ -87,7 +90,7 @@ void AWBugReporter::sendBugReport(const QString &_title, const QString &_body)
auto data = QJsonDocument::fromVariant(payload).toJson(QJsonDocument::Compact);
qCInfo(LOG_AW) << "Send request with _body" << data.data() << "and size" << data.size();
manager->post(request, data);
m_manager->post(request, data);
}

View File

@ -20,6 +20,7 @@
#include <QObject>
class QNetworkAccessManager;
class QNetworkReply;
class AWBugReporter : public QObject
@ -44,4 +45,5 @@ private slots:
private:
QString m_lastBugUrl;
QNetworkAccessManager *m_manager = nullptr;
};

View File

@ -27,5 +27,5 @@ AWCustomKeysConfig::AWCustomKeysConfig(QWidget *_parent, const QStringList &_key
qCDebug(LOG_AW) << __PRETTY_FUNCTION__;
setEditable(true, false);
initHelper<AWCustomKeysHelper>();
setHelper(std::make_unique<AWCustomKeysHelper>());
}

View File

@ -50,7 +50,8 @@ AWDataAggregator::~AWDataAggregator()
{
qCDebug(LOG_AW) << __PRETTY_FUNCTION__;
delete m_toolTipScene;
m_toolTipView->deleteLater();
m_toolTipScene->deleteLater();
}

View File

@ -27,5 +27,5 @@ AWFormatterConfig::AWFormatterConfig(QWidget *_parent, const QStringList &_keys)
qCDebug(LOG_AW) << __PRETTY_FUNCTION__;
setEditable(false, false);
initHelper<AWFormatterHelper>();
setHelper(std::make_unique<AWFormatterHelper>());
}

View File

@ -33,7 +33,7 @@
#include "awstringformatter.h"
AWFormatterHelper::AWFormatterHelper(QWidget *_parent)
AWFormatterHelper::AWFormatterHelper(QObject *_parent)
: AbstractExtItemAggregator(_parent, "formatters")
, AWAbstractPairHelper("awesomewidgets/formatters/formatters.ini", "Formatters")
{

View File

@ -27,7 +27,7 @@ class AWFormatterHelper : public AbstractExtItemAggregator, public AWAbstractPai
Q_OBJECT
public:
explicit AWFormatterHelper(QWidget *_parent = nullptr);
explicit AWFormatterHelper(QObject *_parent = nullptr);
~AWFormatterHelper() override;
// read-write methods
void initItems() override;

View File

@ -40,12 +40,12 @@ AWKeyOperations::AWKeyOperations(QObject *_parent)
qCDebug(LOG_AW) << __PRETTY_FUNCTION__;
m_customKeys = new AWCustomKeysHelper(this);
m_graphicalItems = new ExtItemAggregator<GraphicalItem>(nullptr, "desktops");
m_extNetRequest = new ExtItemAggregator<ExtNetworkRequest>(nullptr, "requests");
m_extQuotes = new ExtItemAggregator<ExtQuotes>(nullptr, "quotes");
m_extScripts = new ExtItemAggregator<ExtScript>(nullptr, "scripts");
m_extUpgrade = new ExtItemAggregator<ExtUpgrade>(nullptr, "upgrade");
m_extWeather = new ExtItemAggregator<ExtWeather>(nullptr, "weather");
m_graphicalItems = new ExtItemAggregator<GraphicalItem>(this, "desktops");
m_extNetRequest = new ExtItemAggregator<ExtNetworkRequest>(this, "requests");
m_extQuotes = new ExtItemAggregator<ExtQuotes>(this, "quotes");
m_extScripts = new ExtItemAggregator<ExtScript>(this, "scripts");
m_extUpgrade = new ExtItemAggregator<ExtUpgrade>(this, "upgrade");
m_extWeather = new ExtItemAggregator<ExtWeather>(this, "weather");
}

View File

@ -189,8 +189,10 @@ void AWKeys::dataUpdated(const QHash<QString, KSysGuard::SensorInfo> &_sensors,
if (!_sensors.contains(data.sensorProperty))
continue;
auto sensor = _sensors[data.sensorProperty];
std::ignore = QtConcurrent::run(m_threadPool, &AWKeys::setDataBySource, this, data.sensorProperty, sensor,
data.payload);
setDataBySource(data.sensorProperty, sensor, data.payload);
// std::ignore = QtConcurrent::run(m_threadPool, &AWKeys::setDataBySource, this, data.sensorProperty,
// sensor,
// data.payload);
}
}

View File

@ -33,7 +33,7 @@ AWKeysAggregator::AWKeysAggregator(QObject *_parent)
{
qCDebug(LOG_AW) << __PRETTY_FUNCTION__;
m_customFormatters = new AWFormatterHelper(nullptr);
m_customFormatters = new AWFormatterHelper(this);
m_mapper = new AWDataEngineMapper(this, m_customFormatters);
// sort time keys

View File

@ -18,8 +18,6 @@
#include "awtelemetryhandler.h"
#include <QJsonDocument>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QSettings>
#include <QStandardPaths>
#include <QUuid>
@ -65,13 +63,11 @@ QString AWTelemetryHandler::getLast(const QString &_group) const
}
void AWTelemetryHandler::init(const int _count, const bool _enableRemote, const QString &_clientId)
void AWTelemetryHandler::init(const int _count, const QString &_clientId)
{
qCDebug(LOG_AW) << "Init telemetry with count" << _count << "enable remote" << _enableRemote << "client ID"
<< _clientId;
qCDebug(LOG_AW) << "Init telemetry with count" << _count << "client ID" << _clientId;
m_storeCount = _count;
m_uploadEnabled = _enableRemote;
m_clientId = _clientId;
}
@ -112,59 +108,6 @@ bool AWTelemetryHandler::put(const QString &_group, const QString &_value) const
}
void AWTelemetryHandler::uploadTelemetry(const QString &_group, const QString &_value)
{
qCDebug(LOG_AW) << "Upload data with group" << _group << "and value" << _value;
if (!m_uploadEnabled) {
qCInfo(LOG_AW) << "Upload disabled by configuration";
return;
}
auto manager = new QNetworkAccessManager(nullptr);
connect(manager, &QNetworkAccessManager::finished, this, &AWTelemetryHandler::telemetryReplyReceived);
QUrl url(REMOTE_TELEMETRY_URL);
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
// generate payload
QVariantMap payload;
payload["api"] = AW_TELEMETRY_API;
payload["client_id"] = m_clientId;
payload["metadata"] = _value;
payload["type"] = _group;
// convert to QByteArray to send request
auto data = QJsonDocument::fromVariant(payload).toJson(QJsonDocument::Compact);
qCInfo(LOG_AW) << "Send request with body" << data.data() << "and size" << data.size();
manager->post(request, data);
}
void AWTelemetryHandler::telemetryReplyReceived(QNetworkReply *_reply)
{
if (_reply->error() != QNetworkReply::NoError) {
qCWarning(LOG_AW) << "An error occurs" << _reply->error() << "with message" << _reply->errorString();
return;
}
QJsonParseError error{};
auto jsonDoc = QJsonDocument::fromJson(_reply->readAll(), &error);
if (error.error != QJsonParseError::NoError) {
qCWarning(LOG_AW) << "Parse error" << error.errorString();
return;
}
_reply->deleteLater();
// convert to map
auto response = jsonDoc.toVariant().toMap();
auto message = response["message"].toString();
qCInfo(LOG_AW) << "Server reply on telemetry" << message;
return emit(replyReceived(message));
}
QString AWTelemetryHandler::getKey(const int _count)
{
qCDebug(LOG_AW) << "Get key for keys count" << _count;

View File

@ -20,33 +20,21 @@
#include <QObject>
class QNetworkReply;
class AWTelemetryHandler : public QObject
{
Q_OBJECT
public:
const char *REMOTE_TELEMETRY_URL = "https://arcanis.me/telemetry";
explicit AWTelemetryHandler(QObject *_parent = nullptr, const QString &_clientId = "");
~AWTelemetryHandler() override = default;
Q_INVOKABLE [[nodiscard]] QStringList get(const QString &_group) const;
Q_INVOKABLE [[nodiscard]] QString getLast(const QString &_group) const;
Q_INVOKABLE void init(int _count, bool _enableRemote, const QString &_clientId);
Q_INVOKABLE void init(int _count, const QString &_clientId);
Q_INVOKABLE [[nodiscard]] bool put(const QString &_group, const QString &_value) const;
Q_INVOKABLE void uploadTelemetry(const QString &_group, const QString &_value);
signals:
void replyReceived(const QString &_message);
private slots:
void telemetryReplyReceived(QNetworkReply *_reply);
private:
static QString getKey(int _count);
QString m_clientId;
QString m_localFile;
int m_storeCount = 0;
bool m_uploadEnabled = false;
};

View File

@ -45,7 +45,7 @@ void AWUpdateHelper::checkUpdates(const bool _showAnyway)
{
qCDebug(LOG_AW) << "Show anyway" << _showAnyway;
// showAnyway options requires to show message if no updates found on direct
// showAnyway options required to show message if no updates found on direct
// request. In case of automatic check no message will be shown
auto manager = new QNetworkAccessManager(nullptr);
connect(manager, &QNetworkAccessManager::finished,
@ -126,6 +126,7 @@ void AWUpdateHelper::versionReplyReceived(QNetworkReply *_reply, const bool _sho
return;
}
_reply->deleteLater();
_reply->manager()->deleteLater(); // remember to delete manager too
// convert to map
auto firstRelease = jsonDoc.toVariant().toList().first().toMap();