Skip to content

Commit

Permalink
Refs #99. Refactored DanceComManager 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 2175a90 commit 1dad8a9
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 127 deletions.
106 changes: 3 additions & 103 deletions client/src/services/DanceService/DanceComManager.cpp
Original file line number Diff line number Diff line change
@@ -1,124 +1,24 @@
#include "DanceComManager.h"
#include "DanceWebAPI.h"

DanceComManager::DanceComManager(ComManager *comManager, QObject *parent)
: BaseComManager(comManager->getServerUrl()),
m_comManager(comManager)
: BaseServiceComManager(comManager, "DanceService")
{

// Set initial user token
setCredentials(m_comManager->getCurrentToken());

// Connect signals
connectSignals();

}

DanceComManager::~DanceComManager()
{

}

bool DanceComManager::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 DanceComManager::connectSignals()
void DanceComManager::postHandleData(const QList<QJsonObject> &items, const QString &reply_path, const QUrlQuery &reply_query)
{
connect(m_comManager, &ComManager::userTokenUpdated, this, &DanceComManager::handleUserTokenUpdated);
}

bool DanceComManager::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(), "DanceComManager::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(DANCE_LIBRARY_PATH) || reply_path.endsWith(DANCE_LIBRARY_VIDEOS_PATH)){
emit videosReceived(items, reply_query);
}

if (reply_path.endsWith(DANCE_PLAYLIST_PATH)){
emit playlistReceived(items, reply_query);
}

// Always emit generic signal
emit dataReceived(items, reply_query);

return true;
}

void DanceComManager::handleUserTokenUpdated()
{
// Update token
setCredentials(m_comManager->getCurrentToken());
}

void DanceComManager::doGet(const QString &path, const QUrlQuery &query_args, const bool &use_token)
{
Q_UNUSED(use_token)
BaseComManager::doGet(path, query_args, true); // Always use token!
}

void DanceComManager::doPost(const QString &path, const QString &post_data, const bool &use_token)
{
Q_UNUSED(use_token)
BaseComManager::doPost(path, post_data, true); // Always use token!
}

void DanceComManager::doDelete(const QString &path, const int &id, const bool &use_token)
{
Q_UNUSED(use_token)
BaseComManager::doDelete(path, id, true); // Always use token!
}
23 changes: 3 additions & 20 deletions client/src/services/DanceService/DanceComManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,19 @@

#include <QObject>
#include "managers/ComManager.h"
#include "managers/BaseComManager.h"
#include "Utils.h"
#include "DanceWebAPI.h"
#include "services/BaseServiceComManager.h"

class DanceComManager : public BaseComManager
class DanceComManager : public BaseServiceComManager
{
Q_OBJECT
public:
explicit DanceComManager(ComManager* comManager, QObject *parent = nullptr);
~DanceComManager();

bool processNetworkReply(QNetworkReply* reply);

void doGet(const QString &path, const QUrlQuery &query_args = QUrlQuery(), const bool &use_token=false);
void doPost(const QString &path, const QString &post_data, const bool &use_token=false);
void doDelete(const QString &path, const int& id, const bool &use_token=false);


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 videosReceived(QList<QJsonObject> videos, QUrlQuery reply_query);
void playlistReceived(QList<QJsonObject> playlist, QUrlQuery reply_query);
Expand Down
18 changes: 18 additions & 0 deletions client/src/services/DanceService/DanceConfigWidget.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "DanceConfigWidget.h"
#include "ui_DanceConfigWidget.h"

#include "DanceWebAPI.h"
#include "WebAPI.h"

DanceConfigWidget::DanceConfigWidget(ComManager *comManager, int projectId, QString participantUuid, QWidget *parent) :
QWidget(parent),
ui(new Ui::DanceConfigWidget),
Expand Down Expand Up @@ -38,6 +41,10 @@ DanceConfigWidget::~DanceConfigWidget()

void DanceConfigWidget::refresh()
{
if (!m_danceComManager->isReady()){
m_refreshRequested = true;
return;
}
// Setup widget according to setted values
if (m_uuidParticipant.isEmpty()){
// Hide the playlist tab if no participant specified
Expand Down Expand Up @@ -245,6 +252,16 @@ void DanceConfigWidget::danceComTransferAborted(TransferringFile *file)
}
}

void DanceConfigWidget::danceComReady(bool ready)
{
if (ready){
if (m_refreshRequested){
m_refreshRequested = false;
refresh();
}
}
}

void DanceConfigWidget::transferDialogCompleted()
{
if (m_transferDialog){
Expand Down Expand Up @@ -304,6 +321,7 @@ void DanceConfigWidget::nextMessageWasShown(Message current_message)

void DanceConfigWidget::connectSignals()
{
connect(m_danceComManager, &DanceComManager::readyChanged, this, &DanceConfigWidget::danceComReady);
connect(m_danceComManager, &DanceComManager::videosReceived, this, &DanceConfigWidget::processVideosReply);
connect(m_danceComManager, &DanceComManager::playlistReceived, this, &DanceConfigWidget::processPlaylistReply);

Expand Down
4 changes: 3 additions & 1 deletion client/src/services/DanceService/DanceConfigWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include "GlobalMessageBox.h"

#include "DanceComManager.h"
#include "WebAPI.h"

namespace Ui {
class DanceConfigWidget;
Expand All @@ -39,6 +38,8 @@ class DanceConfigWidget : public QWidget
int m_idProject;
QString m_uuidParticipant;

bool m_refreshRequested = false;

QList<int> m_playlistIds;

DanceComManager* m_danceComManager;
Expand Down Expand Up @@ -68,6 +69,7 @@ private slots:
void danceComUploadProgress(UploadingFile* file);
void danceComUploadCompleted(UploadingFile* file);
void danceComTransferAborted(TransferringFile *file);
void danceComReady(bool ready);

void transferDialogCompleted();
void transferDialogAbortRequested();
Expand Down
6 changes: 3 additions & 3 deletions client/src/services/DanceService/DanceWebAPI.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef DANCEWEBAPI_H
#define DANCEWEBAPI_H

#define DANCE_LIBRARY_PATH "/dance/api/library"
#define DANCE_LIBRARY_VIDEOS_PATH "/dance/api/library/videos"
#define DANCE_PLAYLIST_PATH "/dance/api/playlist"
#define DANCE_LIBRARY_PATH "api/library"
#define DANCE_LIBRARY_VIDEOS_PATH "api/library/videos"
#define DANCE_PLAYLIST_PATH "api/playlist"

#endif // DANCEWEBAPI_H

0 comments on commit 1dad8a9

Please sign in to comment.