Can get the list of conversations

This commit is contained in:
Pierre HUBERT 2018-12-02 21:26:49 +01:00
parent d405b0e8e1
commit 12f4e2ece1
12 changed files with 376 additions and 4 deletions

View File

@ -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 \

View File

@ -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:

76
data/conversation.cpp Normal file
View File

@ -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<int> Conversation::members() const
{
return mMembers;
}
void Conversation::setMembers(const QList<int> &members)
{
mMembers = members;
}

50
data/conversation.h Normal file
View File

@ -0,0 +1,50 @@
/**
* This widget contains information about
* a single conversation
*
* @author Pierre HUBERT
*/
#ifndef CONVERSATION_H
#define CONVERSATION_H
#include <QList>
#include <QString>
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<int> members() const;
void setMembers(const QList<int> &members);
private:
int mID;
int mIDowner;
int mLastActive;
QString mName;
bool mFollowing;
bool mSawLastMessage;
QList<int> mMembers;
};
#endif // CONVERSATION_H

View File

@ -59,7 +59,9 @@ void APIHelper::finished()
APIRequest *request = mRequestsList.findForReply(qobject_cast<QNetworkReply *>(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()

View File

@ -0,0 +1,62 @@
#include <QJsonArray>
#include <QJsonObject>
#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<APIRequest *>(sender())->deleteLater();
if(code != 200){
emit onGotList(false, QList<Conversation>());
return;
}
//The request successfully completed
QJsonArray arr = document.array();
//Process the list of conversations
QList<Conversation> 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<int> 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;
}

View File

@ -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 <QObject>
#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<Conversation> &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

View File

@ -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<Conversation> &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());
}
}
}

View File

@ -0,0 +1,37 @@
#ifndef CONVERSATIONSLISTWIDGET_H
#define CONVERSATIONSLISTWIDGET_H
#include <QWidget>
#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<Conversation> &list);
private:
ConversationsListHelper *mConversationsList;
};
#endif // CONVERSATIONSLISTWIDGET_H

View File

@ -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()

View File

@ -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

View File

@ -13,7 +13,27 @@
<property name="windowTitle">
<string>ComunicMessages</string>
</property>
<widget class="QWidget" name="centralwidget"/>
<widget class="QWidget" name="centralwidget">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QScrollArea" name="conversationsListArea">
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>780</width>
<height>536</height>
</rect>
</property>
</widget>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>