diff --git a/ComunicMessages.pro b/ComunicMessages.pro index 5723ac6..a8089e9 100644 --- a/ComunicMessages.pro +++ b/ComunicMessages.pro @@ -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 \ diff --git a/config.h b/config.h index 6e80e0d..20565aa 100644 --- a/config.h +++ b/config.h @@ -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 diff --git a/data/newconversationmessage.cpp b/data/newconversationmessage.cpp new file mode 100644 index 0000000..ee81c1c --- /dev/null +++ b/data/newconversationmessage.cpp @@ -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; +} diff --git a/data/newconversationmessage.h b/data/newconversationmessage.h new file mode 100644 index 0000000..fd6e530 --- /dev/null +++ b/data/newconversationmessage.h @@ -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 + +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 diff --git a/helpers/conversationhelper.cpp b/helpers/conversationhelper.cpp new file mode 100644 index 0000000..870a354 --- /dev/null +++ b/helpers/conversationhelper.cpp @@ -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(sender())->deleteLater(); + + if(code != 200){ + emit sendMessageCallback(false); + return; + } + + //Success + emit sendMessageCallback(true); +} diff --git a/helpers/conversationhelper.h b/helpers/conversationhelper.h new file mode 100644 index 0000000..5536250 --- /dev/null +++ b/helpers/conversationhelper.h @@ -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 + +#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 diff --git a/widgets/conversationwidget.cpp b/widgets/conversationwidget.cpp index 148864a..00f89e0 100644 --- a/widgets/conversationwidget.cpp +++ b/widgets/conversationwidget.cpp @@ -1,6 +1,10 @@ +#include + #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(""); +} diff --git a/widgets/conversationwidget.h b/widgets/conversationwidget.h index a75209d..1dd9a6e 100644 --- a/widgets/conversationwidget.h +++ b/widgets/conversationwidget.h @@ -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; };