Skip to content

Commit

Permalink
Refs #99. Refactored DashboardsComManager to use base class
Browse files Browse the repository at this point in the history
  • Loading branch information
SBriere committed Nov 28, 2024
1 parent 269de52 commit 2175a90
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 128 deletions.
18 changes: 11 additions & 7 deletions client/src/services/BaseServiceComManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -173,10 +178,9 @@ void BaseServiceComManager::processServicesReply(QList<TeraData> 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);
}
105 changes: 3 additions & 102 deletions client/src/services/DashboardsService/DashboardsComManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,120 +2,21 @@
#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()
{

}

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<QJsonObject> &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<QJsonObject> 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);
}
21 changes: 4 additions & 17 deletions client/src/services/DashboardsService/DashboardsComManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,20 @@

#include <QObject>
#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<QJsonObject>& items, const QString &reply_path, const QUrlQuery &reply_query) override;

signals:
void dataReceived(QList<QJsonObject> items, QUrlQuery reply_query);

void dashboardsReceived(QList<QJsonObject> dashboards, QUrlQuery reply_query);

};
Expand Down
17 changes: 17 additions & 0 deletions client/src/services/DashboardsService/DashboardsConfigWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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);
Expand Down Expand Up @@ -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){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion client/src/services/DashboardsService/DashboardsWebAPI.h
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
16 changes: 15 additions & 1 deletion client/src/services/EmailService/EmailServiceConfigWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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){
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions client/src/services/EmailService/EmailServiceConfigWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -49,6 +50,7 @@ private slots:
QJsonObject m_currentTemplate;

bool m_loaded = false;
bool m_refreshRequested = false;

void connectSignals();

Expand Down

0 comments on commit 2175a90

Please sign in to comment.