Can send messages from conversations

This commit is contained in:
Pierre HUBERT 2018-12-12 21:28:48 +01:00 committed by Pierre HUBERT
parent a0eaf69c0e
commit 58b4afe653
8 changed files with 241 additions and 3 deletions

View File

@ -26,7 +26,9 @@ SOURCES += \
utils/uiutils.cpp \
data/userslist.cpp \
utils/timeutils.cpp \
widgets/conversationwidget.cpp
widgets/conversationwidget.cpp \
data/newconversationmessage.cpp \
helpers/conversationhelper.cpp
HEADERS += \
helpers/accounthelper.h \
@ -54,7 +56,9 @@ HEADERS += \
utils/uiutils.h \
data/userslist.h \
utils/timeutils.h \
widgets/conversationwidget.h
widgets/conversationwidget.h \
data/newconversationmessage.h \
helpers/conversationhelper.h
FORMS += \
widgets/loginwidget.ui \

View File

@ -29,4 +29,9 @@
#define SETTINGS_ACCOUNT_LOGIN_TOKEN_1 "account_login_token_1"
#define SETTINGS_ACCOUNT_LOGIN_TOKEN_2 "account_login_token_2"
/**
* Conversations information
*/
#define CONVERSATION_MESSAGE_MIN_LENGTH 3
#endif // CONFIG_H

View File

@ -0,0 +1,26 @@
#include "newconversationmessage.h"
NewConversationMessage::NewConversationMessage()
{
}
int NewConversationMessage::iDConversation() const
{
return mIDConversation;
}
void NewConversationMessage::setIDConversation(int iDConversation)
{
mIDConversation = iDConversation;
}
QString NewConversationMessage::message() const
{
return mMessage;
}
void NewConversationMessage::setMessage(const QString &message)
{
mMessage = message;
}

View File

@ -0,0 +1,30 @@
/**
* New conversation message
*
* Contains information about a message to send
* to the server
*
* @author Pierre HUBERT
*/
#ifndef NEWCONVERSATIONMESSAGE_H
#define NEWCONVERSATIONMESSAGE_H
#include <QString>
class NewConversationMessage
{
public:
NewConversationMessage();
int iDConversation() const;
void setIDConversation(int iDConversation);
QString message() const;
void setMessage(const QString &message);
private:
int mIDConversation;
QString mMessage;
};
#endif // NEWCONVERSATIONMESSAGE_H

View File

@ -0,0 +1,33 @@
#include "apihelper.h"
#include "conversationhelper.h"
#include "../data/apirequest.h"
ConversationHelper::ConversationHelper(QObject *parent) : QObject(parent)
{
mAPIHelper = new APIHelper(this);
}
void ConversationHelper::sendMessage(const NewConversationMessage &message)
{
APIRequest *request = new APIRequest;
request->setURI("conversations/sendMessage");
request->addInt("conversationID", message.iDConversation());
request->addString("message", message.message());
connect(request, &APIRequest::finished, this, &ConversationHelper::sendMessageFinished);
mAPIHelper->execute(request);
}
void ConversationHelper::sendMessageFinished(int code)
{
//Delete sender
qobject_cast<APIRequest *>(sender())->deleteLater();
if(code != 200){
emit sendMessageCallback(false);
return;
}
//Success
emit sendMessageCallback(true);
}

View File

@ -0,0 +1,61 @@
/**
* Conversation helper
*
* This class is an interface between the server
* and the conversations widgets
*
* @author Pierre HUBERT
*/
#ifndef CONVERSATIONHELPER_H
#define CONVERSATIONHELPER_H
#include <QObject>
#include "../data/newconversationmessage.h"
#include "../data/apirequest.h"
class APIHelper;
class ConversationHelper : public QObject
{
Q_OBJECT
public:
explicit ConversationHelper(QObject *parent = nullptr);
/**
* Send a message to the server
*
* @param message Information about the message to send
*/
void sendMessage(const NewConversationMessage &message);
signals:
/**
* This signal is emitted when we have got the result of a create message
* attempt
*
* @param success TRUE if the operation is a success / FALSE
*/
void sendMessageCallback(bool success);
public slots:
private slots:
/**
* Send message callback
*
* @param code The code of the error
* @param & Server response (useless in this case)
*/
void sendMessageFinished(int code);
private:
//Private fields
APIHelper *mAPIHelper;
};
#endif // CONVERSATIONHELPER_H

View File

@ -1,6 +1,10 @@
#include <QMessageBox>
#include "conversationwidget.h"
#include "ui_conversationwidget.h"
#include "../helpers/conversationslisthelper.h"
#include "../helpers/conversationhelper.h"
#include "../config.h"
ConversationWidget::ConversationWidget(const Conversation &conversation, const UsersList &list, QWidget *parent) :
QWidget(parent),
@ -13,6 +17,10 @@ ConversationWidget::ConversationWidget(const Conversation &conversation, const U
//Initialize UI
QString convTitle = ConversationsListHelper::getConversationDisplayName(conversation, list);
ui->convName->setText(convTitle);
//Initalize helpers
mConversationHelper = new ConversationHelper(this);
connect(mConversationHelper, &ConversationHelper::sendMessageCallback, this, &ConversationWidget::sendMessageCallback);
}
ConversationWidget::~ConversationWidget()
@ -22,7 +30,42 @@ ConversationWidget::~ConversationWidget()
void ConversationWidget::sendMessage()
{
qWarning("Send a new message");
if(isSendMessageFormLocked()){
qInfo("sendMessage cancelled because another send message request is running...");
return;
}
QString content = ui->messageContentInput->text();
//Check message length
if(content.length() < CONVERSATION_MESSAGE_MIN_LENGTH){
QMessageBox::warning(this, tr("Invalid message!"), tr("Specified message is too short!"));
return;
}
//Lock send form
setSendMessageFormLocked(true);
//Send a request to send message
NewConversationMessage newMessage;
newMessage.setIDConversation(mConversation.iD());
newMessage.setMessage(content);
//Request the message to be sent
mConversationHelper->sendMessage(newMessage);
}
void ConversationWidget::sendMessageCallback(bool success)
{
setSendMessageFormLocked(false);
if(!success){
QMessageBox::warning(this, tr("Error"), tr("Could not send your message! Please check it and your Internet connection..."));
return;
}
//Reset message form
resetSendMessageForm();
}
void ConversationWidget::on_sendMessageButton_clicked()
@ -34,3 +77,19 @@ void ConversationWidget::on_messageContentInput_returnPressed()
{
sendMessage();
}
void ConversationWidget::setSendMessageFormLocked(bool lock)
{
ui->sendMessageButton->setEnabled(!lock);
ui->messageContentInput->setEnabled(!lock);
}
bool ConversationWidget::isSendMessageFormLocked()
{
return !ui->sendMessageButton->isEnabled();
}
void ConversationWidget::resetSendMessageForm()
{
ui->messageContentInput->setText("");
}

View File

@ -18,6 +18,8 @@ namespace Ui {
class ConversationWidget;
}
class ConversationHelper;
class ConversationWidget : public QWidget
{
Q_OBJECT
@ -36,14 +38,32 @@ public slots:
private slots:
/**
* Method called once the server replied to a send conversation
* message request
*
* @param success TRUE in case of success / FALSE else
*/
void sendMessageCallback(bool success);
void on_sendMessageButton_clicked();
void on_messageContentInput_returnPressed();
private:
/**
* Methods to get and set send message form
* lock state
*/
void setSendMessageFormLocked(bool lock);
bool isSendMessageFormLocked();
void resetSendMessageForm();
//Private fields
Ui::ConversationWidget *ui;
ConversationHelper *mConversationHelper;
Conversation mConversation;
UsersList mUsersList;
};