From 12f4e2ece1113cb0e5dced788e2b90c5e46daa21 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sun, 2 Dec 2018 21:26:49 +0100 Subject: [PATCH] Can get the list of conversations --- ComunicMessages.pro | 10 +++- data/apirequest.h | 9 ++++ data/conversation.cpp | 76 +++++++++++++++++++++++++++++ data/conversation.h | 50 +++++++++++++++++++ helpers/apihelper.cpp | 5 +- helpers/conversationslisthelper.cpp | 62 +++++++++++++++++++++++ helpers/conversationslisthelper.h | 67 +++++++++++++++++++++++++ widgets/conversationslistwidget.cpp | 33 +++++++++++++ widgets/conversationslistwidget.h | 37 ++++++++++++++ widgets/mainwindow.cpp | 6 +++ widgets/mainwindow.h | 3 ++ widgets/mainwindow.ui | 22 ++++++++- 12 files changed, 376 insertions(+), 4 deletions(-) create mode 100644 data/conversation.cpp create mode 100644 data/conversation.h create mode 100644 helpers/conversationslisthelper.cpp create mode 100644 helpers/conversationslisthelper.h create mode 100644 widgets/conversationslistwidget.cpp create mode 100644 widgets/conversationslistwidget.h diff --git a/ComunicMessages.pro b/ComunicMessages.pro index a6078b0..4e3bb21 100644 --- a/ComunicMessages.pro +++ b/ComunicMessages.pro @@ -15,7 +15,10 @@ SOURCES += \ data/accountlogintokens.cpp \ widgets/mainwindow.cpp \ widgets/aboutthisappdialog.cpp \ - controllers/initcontroller.cpp + controllers/initcontroller.cpp \ + data/conversation.cpp \ + helpers/conversationslisthelper.cpp \ + widgets/conversationslistwidget.cpp HEADERS += \ helpers/accounthelper.h \ @@ -32,7 +35,10 @@ HEADERS += \ data/accountlogintokens.h \ widgets/mainwindow.h \ widgets/aboutthisappdialog.h \ - controllers/initcontroller.h + controllers/initcontroller.h \ + data/conversation.h \ + helpers/conversationslisthelper.h \ + widgets/conversationslistwidget.h FORMS += \ widgets/loginwidget.ui \ diff --git a/data/apirequest.h b/data/apirequest.h index 109b513..8ea94e9 100644 --- a/data/apirequest.h +++ b/data/apirequest.h @@ -77,6 +77,15 @@ signals: */ void success(const QJsonDocument &document); + /** + * Signal emitted once we consider the request as finished + * It is emitted both in case of error and in case of success + * + * @param code The code of the error + * @param document Data received in case of success + */ + void finished(int code, const QJsonDocument &document); + public slots: private: diff --git a/data/conversation.cpp b/data/conversation.cpp new file mode 100644 index 0000000..f64d1e0 --- /dev/null +++ b/data/conversation.cpp @@ -0,0 +1,76 @@ +#include "conversation.h" + +Conversation::Conversation() +{ + +} + +int Conversation::iD() const +{ + return mID; +} + +void Conversation::setID(int iD) +{ + mID = iD; +} + +int Conversation::iDowner() const +{ + return mIDowner; +} + +void Conversation::setIDowner(int iDowner) +{ + mIDowner = iDowner; +} + +int Conversation::lastActive() const +{ + return mLastActive; +} + +void Conversation::setLastActive(int lastActive) +{ + mLastActive = lastActive; +} + +QString Conversation::name() const +{ + return mName; +} + +void Conversation::setName(const QString &name) +{ + mName = name; +} + +bool Conversation::following() const +{ + return mFollowing; +} + +void Conversation::setFollowing(bool following) +{ + mFollowing = following; +} + +bool Conversation::sawLastMessage() const +{ + return mSawLastMessage; +} + +void Conversation::setSawLastMessage(bool sawLastMessage) +{ + mSawLastMessage = sawLastMessage; +} + +QList Conversation::members() const +{ + return mMembers; +} + +void Conversation::setMembers(const QList &members) +{ + mMembers = members; +} diff --git a/data/conversation.h b/data/conversation.h new file mode 100644 index 0000000..f8e11f3 --- /dev/null +++ b/data/conversation.h @@ -0,0 +1,50 @@ +/** + * This widget contains information about + * a single conversation + * + * @author Pierre HUBERT + */ + +#ifndef CONVERSATION_H +#define CONVERSATION_H + +#include +#include + +class Conversation +{ +public: + Conversation(); + + int iD() const; + void setID(int iD); + + int iDowner() const; + void setIDowner(int iDowner); + + int lastActive() const; + void setLastActive(int lastActive); + + QString name() const; + void setName(const QString &name); + + bool following() const; + void setFollowing(bool following); + + bool sawLastMessage() const; + void setSawLastMessage(bool sawLastMessage); + + QList members() const; + void setMembers(const QList &members); + +private: + int mID; + int mIDowner; + int mLastActive; + QString mName; + bool mFollowing; + bool mSawLastMessage; + QList mMembers; +}; + +#endif // CONVERSATION_H diff --git a/helpers/apihelper.cpp b/helpers/apihelper.cpp index a0e7a19..f728124 100644 --- a/helpers/apihelper.cpp +++ b/helpers/apihelper.cpp @@ -59,7 +59,9 @@ void APIHelper::finished() APIRequest *request = mRequestsList.findForReply(qobject_cast(sender()), true); //Process and return response - emit request->success(QJsonDocument::fromJson(request->networkReply()->readAll())); + QJsonDocument document = QJsonDocument::fromJson(request->networkReply()->readAll()); + emit request->success(document); + emit request->finished(200, document); } void APIHelper::error() @@ -72,6 +74,7 @@ void APIHelper::error() qWarning("An error occurred in an API request! (code: %d)", response_code); emit request->error(response_code); + emit request->finished(response_code, QJsonDocument()); } void APIHelper::sslErrors() diff --git a/helpers/conversationslisthelper.cpp b/helpers/conversationslisthelper.cpp new file mode 100644 index 0000000..eb464be --- /dev/null +++ b/helpers/conversationslisthelper.cpp @@ -0,0 +1,62 @@ +#include +#include + +#include "conversationslisthelper.h" +#include "apihelper.h" +#include "../data/apirequest.h" + +ConversationsListHelper::ConversationsListHelper(QObject *parent) : QObject(parent) +{ + mAPIHelper = new APIHelper(this); +} + +void ConversationsListHelper::getList() +{ + //We need to perform a request on the server + APIRequest *request = new APIRequest(this); + request->setURI("conversations/getList"); + connect(request, &APIRequest::finished, this, &ConversationsListHelper::getConvListCallback); + mAPIHelper->execute(request); +} + +void ConversationsListHelper::getConvListCallback(int code, const QJsonDocument &document) +{ + //Delete the request + qobject_cast(sender())->deleteLater(); + + if(code != 200){ + emit onGotList(false, QList()); + return; + } + + //The request successfully completed + QJsonArray arr = document.array(); + + //Process the list of conversations + QList list; + for(int i = 0; i < arr.size(); i++) + list.append(GetConversationFromJson(arr.at(i).toObject())); + + //Finished + emit onGotList(true, list); +} + +Conversation ConversationsListHelper::GetConversationFromJson(const QJsonObject &obj) +{ + Conversation conv; + conv.setID(obj.value("ID").toInt()); + conv.setIDowner(obj.value("ID_owner").toInt()); + conv.setLastActive(obj.value("last_active").toInt()); + conv.setName(obj.value("name").toString()); + conv.setFollowing(obj.value("following").toBool()); + conv.setSawLastMessage(obj.value("saw_last_message").toBool()); + + //Process the list of members of the conversation + QJsonArray members_arr = obj.value("members").toArray(); + QList members_list; + for(int i = 0; i < members_arr.size(); i++) + members_list.append(members_arr.at(i).toInt()); + conv.setMembers(members_list); + + return conv; +} diff --git a/helpers/conversationslisthelper.h b/helpers/conversationslisthelper.h new file mode 100644 index 0000000..c6386d8 --- /dev/null +++ b/helpers/conversationslisthelper.h @@ -0,0 +1,67 @@ +/** + * Conversations list helper + * + * Does all the tasks related with conversations managment + * + * @author Pierre HUBERT + */ + +#ifndef CONVERSATIONSLISTHELPER_H +#define CONVERSATIONSLISTHELPER_H + +#include + +#include "../data/conversation.h" + +class APIHelper; +class QJsonObject; + +class ConversationsListHelper : public QObject +{ + Q_OBJECT +public: + explicit ConversationsListHelper(QObject *parent = nullptr); + + /** + * Get the list of conversations + */ + void getList(); + +signals: + + /** + * This signal is emitted once we have a got a new list of conversation + * + * @param success TRUE for a success / FALSE else + * @param list The list of conversations + */ + void onGotList(bool success, const QList &list); + +public slots: + +private slots: + + /** + * This slot is triggered once the request to the server was + * completed + * + * @param code Response code from the server + * @param document Response document + */ + void getConvListCallback(int code, const QJsonDocument &document); + +private: + + /** + * Turn a QJsonObject into a Conversation one + * + * @param obj The object to convert + * @return Generated conversation object + */ + static Conversation GetConversationFromJson(const QJsonObject &obj); + + //Private fields + APIHelper *mAPIHelper; +}; + +#endif // CONVERSATIONSLISTHELPER_H diff --git a/widgets/conversationslistwidget.cpp b/widgets/conversationslistwidget.cpp new file mode 100644 index 0000000..ff06ddf --- /dev/null +++ b/widgets/conversationslistwidget.cpp @@ -0,0 +1,33 @@ +#include "conversationslistwidget.h" +#include "../helpers/conversationslisthelper.h" + +ConversationsListWidget::ConversationsListWidget(QWidget *parent) : + QWidget(parent) +{ + //Create conversations helper + mConversationsList = new ConversationsListHelper(this); + connect(mConversationsList, &ConversationsListHelper::onGotList, this, &ConversationsListWidget::onGotConversationsList); +} + +ConversationsListWidget::~ConversationsListWidget() +{ + +} + +void ConversationsListWidget::refresh() +{ + mConversationsList->getList(); +} + +void ConversationsListWidget::onGotConversationsList(bool success, const QList &list) +{ + qWarning("Got conversations list (or failure)."); + + if(!success) + qWarning("Failure."); + else { + for(Conversation conv : list){ + qWarning("Conv %d : %s", conv.iD(), conv.name().toStdString().c_str()); + } + } +} diff --git a/widgets/conversationslistwidget.h b/widgets/conversationslistwidget.h new file mode 100644 index 0000000..80b25e3 --- /dev/null +++ b/widgets/conversationslistwidget.h @@ -0,0 +1,37 @@ +#ifndef CONVERSATIONSLISTWIDGET_H +#define CONVERSATIONSLISTWIDGET_H + +#include + +#include "../data/conversation.h" + +class ConversationsListHelper; + +class ConversationsListWidget : public QWidget +{ + Q_OBJECT + +public: + explicit ConversationsListWidget(QWidget *parent = nullptr); + ~ConversationsListWidget(); + + /** + * Refresh the list of conversations of the user + */ + void refresh(); + +private slots: + + /** + * This slot is triggered once we have got a new list of conversations + * + * @param success TRUE for a success / FALSE else + * @param list The list of conversation (empty list in case of failure) + */ + void onGotConversationsList(bool success, const QList &list); + +private: + ConversationsListHelper *mConversationsList; +}; + +#endif // CONVERSATIONSLISTWIDGET_H diff --git a/widgets/mainwindow.cpp b/widgets/mainwindow.cpp index 8df3fe5..ec15ba8 100644 --- a/widgets/mainwindow.cpp +++ b/widgets/mainwindow.cpp @@ -4,6 +4,7 @@ #include "ui_mainwindow.h" #include "aboutthisappdialog.h" #include "loginwidget.h" +#include "conversationslistwidget.h" #include "../helpers/accounthelper.h" @@ -12,6 +13,11 @@ MainWindow::MainWindow(QWidget *parent) : ui(new Ui::MainWindow) { ui->setupUi(this); + + //Display the list of conversations + mConversationsListWidget = new ConversationsListWidget; + mConversationsListWidget->refresh(); + ui->conversationsListArea->setWidget(mConversationsListWidget); } MainWindow::~MainWindow() diff --git a/widgets/mainwindow.h b/widgets/mainwindow.h index 0b8b711..4d626d7 100644 --- a/widgets/mainwindow.h +++ b/widgets/mainwindow.h @@ -13,6 +13,8 @@ namespace Ui { class MainWindow; } +class ConversationsListWidget; + class MainWindow : public QMainWindow { Q_OBJECT @@ -30,6 +32,7 @@ private slots: private: Ui::MainWindow *ui; + ConversationsListWidget *mConversationsListWidget; }; #endif // MAINWINDOW_H diff --git a/widgets/mainwindow.ui b/widgets/mainwindow.ui index 76cdc25..31284c2 100644 --- a/widgets/mainwindow.ui +++ b/widgets/mainwindow.ui @@ -13,7 +13,27 @@ ComunicMessages - + + + + + + true + + + + + 0 + 0 + 780 + 536 + + + + + + +