From 2175a9055e47276619ddb98ad32397b9ab2f39f1 Mon Sep 17 00:00:00 2001 From: Simon Briere Date: Thu, 28 Nov 2024 09:28:45 -0500 Subject: [PATCH] Refs #99. Refactored DashboardsComManager to use base class --- client/src/services/BaseServiceComManager.cpp | 18 +-- .../DashboardsComManager.cpp | 105 +----------------- .../DashboardsService/DashboardsComManager.h | 21 +--- .../DashboardsConfigWidget.cpp | 17 +++ .../DashboardsConfigWidget.h | 3 + .../DashboardsService/DashboardsWebAPI.h | 2 +- .../EmailService/EmailServiceConfigWidget.cpp | 16 ++- .../EmailService/EmailServiceConfigWidget.h | 2 + 8 files changed, 56 insertions(+), 128 deletions(-) diff --git a/client/src/services/BaseServiceComManager.cpp b/client/src/services/BaseServiceComManager.cpp index c033deeb..50343efd 100644 --- a/client/src/services/BaseServiceComManager.cpp +++ b/client/src/services/BaseServiceComManager.cpp @@ -20,48 +20,53 @@ BaseServiceComManager::BaseServiceComManager(ComManager *comManager, QString ser void BaseServiceComManager::doGet(const QString &path, const QUrlQuery &query_args, const bool &use_token) { + Q_UNUSED(use_token) if (!isReady()){ qWarning() << "ServiceComManager is not ready yet - ignoring GET."; return; } - BaseComManager::doGet(getServiceEndpoint(path), query_args, use_token); + BaseComManager::doGet(getServiceEndpoint(path), query_args, true); } void BaseServiceComManager::doPost(const QString &path, const QString &post_data, const bool &use_token) { + Q_UNUSED(use_token) if (!isReady()){ qWarning() << "ServiceComManager is not ready yet - ignoring POST."; return; } - BaseComManager::doPost(getServiceEndpoint(path), post_data, use_token); + BaseComManager::doPost(getServiceEndpoint(path), post_data, true); } void BaseServiceComManager::doPost(const QUrl &full_url, const QString &post_data, const bool &use_token) { + Q_UNUSED(use_token) if (!isReady()){ qWarning() << "ServiceComManager is not ready yet - ignoring POST."; return; } - BaseComManager::doPost(getServiceEndpoint(full_url.toString()), post_data, use_token); + BaseComManager::doPost(getServiceEndpoint(full_url.toString()), post_data, true); } void BaseServiceComManager::doPostWithParams(const QString &path, const QString &post_data, const QUrlQuery &query_args, const bool &use_token) { + Q_UNUSED(use_token) if (!isReady()){ qWarning() << "ServiceComManager is not ready yet - ignoring POST."; return; } - BaseComManager::doPostWithParams(getServiceEndpoint(path), post_data, query_args, use_token); + BaseComManager::doPostWithParams(getServiceEndpoint(path), post_data, query_args, true); } void BaseServiceComManager::doDelete(const QString &path, const int &id, const bool &use_token) { + Q_UNUSED(use_token) if (!isReady()){ qWarning() << "ServiceComManager is not ready yet - ignoring DELETE."; return; } - BaseComManager::doDelete(getServiceEndpoint(path), id, use_token); + BaseComManager::doDelete(getServiceEndpoint(path), id, true); } bool BaseServiceComManager::isReady() @@ -173,10 +178,9 @@ void BaseServiceComManager::processServicesReply(QList services, QUrlQ if (service.getFieldValue("service_key").toString() == m_serviceKey){ m_service = service; emit readyChanged(true); + disconnect(m_comManager, &ComManager::servicesReceived, this, &BaseServiceComManager::processServicesReply); break; } } } - - disconnect(m_comManager, &ComManager::servicesReceived, this, &BaseServiceComManager::processServicesReply); } diff --git a/client/src/services/DashboardsService/DashboardsComManager.cpp b/client/src/services/DashboardsService/DashboardsComManager.cpp index d0b09e48..8b1f930a 100644 --- a/client/src/services/DashboardsService/DashboardsComManager.cpp +++ b/client/src/services/DashboardsService/DashboardsComManager.cpp @@ -2,16 +2,9 @@ #include "DashboardsWebAPI.h" DashboardsComManager::DashboardsComManager(ComManager *comManager, QObject *parent) - : BaseComManager(comManager->getServerUrl()), - m_comManager(comManager) + : BaseServiceComManager(comManager, "DashboardsService") { - // Set initial user token - setCredentials(m_comManager->getCurrentToken()); - - // Connect signals - connectSignals(); - } DashboardsComManager::~DashboardsComManager() @@ -19,103 +12,11 @@ DashboardsComManager::~DashboardsComManager() } -bool DashboardsComManager::processNetworkReply(QNetworkReply *reply) -{ - QString reply_path = reply->url().path(); - QString reply_data = reply->readAll(); - QUrlQuery reply_query = QUrlQuery(reply->url().query()); - //qDebug() << reply_path << " ---> " << reply_data << ": " << reply->url().query(); - - bool handled = false; - - if (reply->operation()==QNetworkAccessManager::GetOperation){ - if (!handled){ - // General case - handled = handleDataReply(reply_path, reply_data, reply_query); - if (handled) emit queryResultsOK(reply_path, reply_query); - } - } - - if (reply->operation()==QNetworkAccessManager::PostOperation){ - if (!handled){ - handled=handleDataReply(reply_path, reply_data, reply_query); - if (handled) emit postResultsOK(reply_path, reply_data); - } - } - - if (reply->operation()==QNetworkAccessManager::DeleteOperation){ - // Extract id from url - int id = 0; - if (reply_query.hasQueryItem("id")){ - id = reply_query.queryItemValue("id").toInt(); - } - emit deleteResultsOK(reply_path, id); - handled=true; - } - - return handled; -} - -void DashboardsComManager::connectSignals() +void DashboardsComManager::postHandleData(const QList &items, const QString &reply_path, const QUrlQuery &reply_query) { - connect(m_comManager, &ComManager::userTokenUpdated, this, &DashboardsComManager::handleUserTokenUpdated); -} + Q_UNUSED(reply_query) -bool DashboardsComManager::handleDataReply(const QString &reply_path, const QString &reply_data, const QUrlQuery &reply_query) -{ - QJsonParseError json_error; - - // Process reply - QString data_str = filterReplyString(reply_data); - - QJsonDocument data_list = QJsonDocument::fromJson(data_str.toUtf8(), &json_error); - if (json_error.error!= QJsonParseError::NoError){ - LOG_ERROR("Received a JSON string for " + reply_path + " with " + reply_query.toString() + " with error: " + json_error.errorString(), "DashboardsComManager::handleDataReply"); - return false; - } - - // Browse each items received - QList items; - if (data_list.isArray()){ - QJsonArray data_list_array = data_list.array(); - for (const QJsonValue &data:std::as_const(data_list_array)){ - items.append(data.toObject()); - } - }else{ - items.append(data_list.object()); - } - - // Check to emit correct signals for specific data types if (reply_path.endsWith(DASHBOARDS_USER_PATH)){ emit dashboardsReceived(items, reply_query); } - - // Always emit generic signal - emit dataReceived(items, reply_query); - - return true; -} - -void DashboardsComManager::handleUserTokenUpdated() -{ - // Update token - setCredentials(m_comManager->getCurrentToken()); -} - -void DashboardsComManager::doGet(const QString &path, const QUrlQuery &query_args, const bool &use_token) -{ - Q_UNUSED(use_token) - BaseComManager::doGet(path, query_args, true); -} - -void DashboardsComManager::doPost(const QString &path, const QString &post_data, const bool &use_token) -{ - Q_UNUSED(use_token) - BaseComManager::doPost(path, post_data, true); -} - -void DashboardsComManager::doDelete(const QString &path, const int &id, const bool &use_token) -{ - Q_UNUSED(use_token) - BaseComManager::doDelete(path, id, true); } diff --git a/client/src/services/DashboardsService/DashboardsComManager.h b/client/src/services/DashboardsService/DashboardsComManager.h index 61e09674..5e07ad84 100644 --- a/client/src/services/DashboardsService/DashboardsComManager.h +++ b/client/src/services/DashboardsService/DashboardsComManager.h @@ -3,33 +3,20 @@ #include #include "managers/ComManager.h" -#include "managers/BaseComManager.h" +#include "services/BaseServiceComManager.h" -class DashboardsComManager : public BaseComManager +class DashboardsComManager : public BaseServiceComManager { Q_OBJECT public: explicit DashboardsComManager(ComManager* comManager, QObject *parent = nullptr); ~DashboardsComManager(); - bool processNetworkReply(QNetworkReply* reply); - - void doGet(const QString &path, const QUrlQuery &query_args = QUrlQuery(), const bool &use_token=true); - void doPost(const QString &path, const QString &post_data, const bool &use_token=true); - void doDelete(const QString &path, const int& id, const bool &use_token=true); - - private: - ComManager* m_comManager; - - void connectSignals(); - bool handleDataReply(const QString& reply_path, const QString &reply_data, const QUrlQuery &reply_query); - -private slots: - void handleUserTokenUpdated(); + void postHandleData(const QList& items, const QString &reply_path, const QUrlQuery &reply_query) override; signals: - void dataReceived(QList items, QUrlQuery reply_query); + void dashboardsReceived(QList dashboards, QUrlQuery reply_query); }; diff --git a/client/src/services/DashboardsService/DashboardsConfigWidget.cpp b/client/src/services/DashboardsService/DashboardsConfigWidget.cpp index adff9ac6..1d3eae47 100644 --- a/client/src/services/DashboardsService/DashboardsConfigWidget.cpp +++ b/client/src/services/DashboardsService/DashboardsConfigWidget.cpp @@ -42,6 +42,12 @@ DashboardsConfigWidget::~DashboardsConfigWidget() void DashboardsConfigWidget::refresh() { ui->frameDashboardButtons->hide(); + + if (!m_dashComManager->isReady()){ + m_refreshRequested = true; + return; + } + // Query dashboards QUrlQuery args; //args.addQueryItem(WEB_QUERY_LIST, "1"); @@ -58,6 +64,7 @@ void DashboardsConfigWidget::refresh() void DashboardsConfigWidget::connectSignals() { + connect(m_dashComManager, &DashboardsComManager::readyChanged, this, &DashboardsConfigWidget::dashComReady); connect(m_dashComManager, &DashboardsComManager::dashboardsReceived, this, &DashboardsConfigWidget::processDashboardsReply); connect(m_dashComManager, &DashboardsComManager::networkError, this, &DashboardsConfigWidget::handleNetworkError); connect(m_dashComManager, &DashboardsComManager::deleteResultsOK, this, &DashboardsConfigWidget::dashComDeleteOK); @@ -273,6 +280,16 @@ void DashboardsConfigWidget::dashComPostOK(QString path, QString data) } } +void DashboardsConfigWidget::dashComReady(bool ready) +{ + if (ready){ + if (m_refreshRequested){ + m_refreshRequested = false; + refresh(); + } + } +} + void DashboardsConfigWidget::nextMessageWasShown(Message current_message) { if (current_message.getMessageType()==Message::MESSAGE_NONE){ diff --git a/client/src/services/DashboardsService/DashboardsConfigWidget.h b/client/src/services/DashboardsService/DashboardsConfigWidget.h index 04abd49b..d0fa0dae 100644 --- a/client/src/services/DashboardsService/DashboardsConfigWidget.h +++ b/client/src/services/DashboardsService/DashboardsConfigWidget.h @@ -38,6 +38,8 @@ class DashboardsConfigWidget : public QWidget int m_idProject; int m_idSite; + bool m_refreshRequested = false; + void connectSignals(); void queryDashboard(const int& id_dashboard); void clearDetails(); @@ -48,6 +50,7 @@ private slots: void handleNetworkError(QNetworkReply::NetworkError error, QString error_msg, QNetworkAccessManager::Operation op, int status_code); void dashComDeleteOK(QString path, int id); void dashComPostOK(QString path, QString data); + void dashComReady(bool ready); void nextMessageWasShown(Message current_message); diff --git a/client/src/services/DashboardsService/DashboardsWebAPI.h b/client/src/services/DashboardsService/DashboardsWebAPI.h index 29c039ec..766d4557 100644 --- a/client/src/services/DashboardsService/DashboardsWebAPI.h +++ b/client/src/services/DashboardsService/DashboardsWebAPI.h @@ -1,7 +1,7 @@ #ifndef DASHWEBAPI_H #define DASHWEBAPI_H -#define DASHBOARDS_USER_PATH "/dashboards/api/user/dashboards" +#define DASHBOARDS_USER_PATH "api/user/dashboards" #define WEB_QUERY_ID_DASHBOARD "id_dashboard" #define WEB_QUERY_ALL_VERSIONS "all_versions" diff --git a/client/src/services/EmailService/EmailServiceConfigWidget.cpp b/client/src/services/EmailService/EmailServiceConfigWidget.cpp index 24dccb38..f69a2812 100644 --- a/client/src/services/EmailService/EmailServiceConfigWidget.cpp +++ b/client/src/services/EmailService/EmailServiceConfigWidget.cpp @@ -33,7 +33,10 @@ EmailServiceConfigWidget::~EmailServiceConfigWidget() void EmailServiceConfigWidget::refresh() { - queryTemplate(ui->cmbTemplate->currentData().toString()); + if (m_emailComManager->isReady()) + queryTemplate(ui->cmbTemplate->currentData().toString()); + else + m_refreshRequested = true; } void EmailServiceConfigWidget::setSiteId(const int &id_site) @@ -98,6 +101,16 @@ void EmailServiceConfigWidget::emailComPostOK(QString path, QString data) } +void EmailServiceConfigWidget::emailComReady(bool ready) +{ + if (ready){ + if (m_refreshRequested){ + m_refreshRequested = false; + refresh(); + } + } +} + void EmailServiceConfigWidget::nextMessageWasShown(Message current_message) { if (current_message.getMessageType()==Message::MESSAGE_NONE){ @@ -119,6 +132,7 @@ void EmailServiceConfigWidget::templateBeingEdited(bool editing) void EmailServiceConfigWidget::connectSignals() { + connect(m_emailComManager, &EmailComManager::readyChanged, this, &EmailServiceConfigWidget::emailComReady); connect(m_emailComManager, &EmailComManager::emailTemplateReceived, this, &EmailServiceConfigWidget::processTemplateReply); connect(m_emailComManager, &EmailComManager::postResultsOK, this, &EmailServiceConfigWidget::emailComPostOK); connect(m_emailComManager, &EmailComManager::networkError, this, &EmailServiceConfigWidget::handleNetworkError); diff --git a/client/src/services/EmailService/EmailServiceConfigWidget.h b/client/src/services/EmailService/EmailServiceConfigWidget.h index ff520d19..e4a281ca 100644 --- a/client/src/services/EmailService/EmailServiceConfigWidget.h +++ b/client/src/services/EmailService/EmailServiceConfigWidget.h @@ -28,6 +28,7 @@ private slots: void processTemplateReply(const QJsonObject email_template); void handleNetworkError(QNetworkReply::NetworkError error, QString error_msg, QNetworkAccessManager::Operation op, int status_code); void emailComPostOK(QString path, QString data); + void emailComReady(bool ready); void nextMessageWasShown(Message current_message); @@ -49,6 +50,7 @@ private slots: QJsonObject m_currentTemplate; bool m_loaded = false; + bool m_refreshRequested = false; void connectSignals();