From 69ff191c83e5277cfe22d3735e9ac2f0256be3c5 Mon Sep 17 00:00:00 2001 From: link2xt Date: Sat, 29 May 2021 23:40:34 +0300 Subject: Implement message drafts --- context.cpp | 24 ++++++++++++++++++++++++ context.h | 3 +++ message.cpp | 7 +++++++ message.h | 7 ++++++- qml/ComposePane.qml | 15 +++++++++++++++ qml/Message.qml | 5 ++++- 6 files changed, 59 insertions(+), 2 deletions(-) diff --git a/context.cpp b/context.cpp index e7742f6..a92c0c2 100644 --- a/context.cpp +++ b/context.cpp @@ -189,6 +189,23 @@ Context::sendTextMessage(uint32_t chatId, QString textToSend) return dc_send_text_msg(m_context, chatId, utf8Text.constData()); } +void +Context::setDraft(uint32_t chatId, DcMessage *message) +{ + dc_set_draft(m_context, chatId, message->m_message); +} + +DcMessage * +Context::getDraft(uint32_t chatId) +{ + dc_msg_t *draft = dc_get_draft(m_context, chatId); + if (draft) { + return new DcMessage{draft}; + } else { + return NULL; + } +} + bool Context::setChatMuteDuration(uint32_t chatId, int64_t duration) { @@ -200,3 +217,10 @@ Context::decideOnContactRequest(uint32_t msgId, int decision) { return dc_decide_on_contact_request(m_context, msgId, decision); } + +DcMessage * +Context::newMessage(int viewtype) +{ + dc_msg_t *message = dc_msg_new(m_context, viewtype); + return new DcMessage{message}; +} diff --git a/context.h b/context.h index df3fbfb..2d31f71 100644 --- a/context.h +++ b/context.h @@ -39,6 +39,8 @@ public: Q_INVOKABLE DcMessage *getMessage(uint32_t msgId); Q_INVOKABLE DcContact *getContact(uint32_t contactId); Q_INVOKABLE uint32_t sendTextMessage(uint32_t chatId, QString textToSend); + Q_INVOKABLE void setDraft(uint32_t chatId, DcMessage *message); + Q_INVOKABLE DcMessage *getDraft(uint32_t chatId); QString getBlobdir(); Q_INVOKABLE bool setConfig(QString key, QString value); Q_INVOKABLE QString getConfig(QString key); @@ -46,6 +48,7 @@ public: Q_INVOKABLE QString getMessageInfo(uint32_t msgId); Q_INVOKABLE QString getMessageHtml(uint32_t msgId); Q_INVOKABLE uint32_t decideOnContactRequest(uint32_t msgId, int decision); + Q_INVOKABLE DcMessage *newMessage(int viewtype); private: dc_context_t *m_context{nullptr}; diff --git a/message.cpp b/message.cpp index cf29dae..90723d9 100644 --- a/message.cpp +++ b/message.cpp @@ -46,6 +46,13 @@ DcMessage::getState() return dc_msg_get_state(m_message); } +void +DcMessage::setText(QString text) +{ + QByteArray utf8Text = text.toUtf8(); + return dc_msg_set_text(m_message, utf8Text.constData()); +} + QString DcMessage::getText() { diff --git a/message.h b/message.h index 8f7ec9c..2ce81e2 100644 --- a/message.h +++ b/message.h @@ -4,6 +4,8 @@ #include +class Context; + class DcMessage : public QObject { Q_OBJECT Q_PROPERTY(uint32_t id READ getId CONSTANT) @@ -11,7 +13,7 @@ class DcMessage : public QObject { Q_PROPERTY(uint32_t chatId READ getChatId CONSTANT) Q_PROPERTY(int viewtype READ getViewtype CONSTANT) Q_PROPERTY(int state READ getState CONSTANT) - Q_PROPERTY(QString text READ getText CONSTANT) + Q_PROPERTY(QString text READ getText WRITE setText) Q_PROPERTY(QString subject READ getSubject CONSTANT) Q_PROPERTY(QString file READ getFile CONSTANT) Q_PROPERTY(QString filename READ getFilename CONSTANT) @@ -23,6 +25,8 @@ class DcMessage : public QObject { Q_PROPERTY(bool hasHtml READ hasHtml CONSTANT) dc_msg_t *m_message{nullptr}; + + friend class Context; public: explicit DcMessage(QObject *parent = nullptr); explicit DcMessage(dc_msg_t *msg); @@ -36,6 +40,7 @@ public: //Q_INVOKABLE int64_t getTimestamp(); //Q_INVOKABLE int64_t getReceivedTimestamp(); //Q_INVOKABLE int64_t getSortTimestamp(); + Q_INVOKABLE void setText(QString); Q_INVOKABLE QString getText(); Q_INVOKABLE QString getSubject(); QString getFile(); diff --git a/qml/ComposePane.qml b/qml/ComposePane.qml index 54480d5..44cbd7c 100644 --- a/qml/ComposePane.qml +++ b/qml/ComposePane.qml @@ -20,6 +20,21 @@ Pane { placeholderText: qsTr("Message") wrapMode: TextArea.Wrap selectByMouse: true + + Component.onCompleted: { + let draft = context.getDraft(chatId) + if (draft) { + messageField.text = draft.text + } + } + + Connections { + function onEditingFinished() { + var msg = context.newMessage(10) + msg.setText(messageField.text) + context.setDraft(chatId, msg) + } + } } Button { diff --git a/qml/Message.qml b/qml/Message.qml index fdd895a..68b2979 100644 --- a/qml/Message.qml +++ b/qml/Message.qml @@ -161,13 +161,16 @@ RowLayout { // Message TextEdit { Layout.maximumWidth: messageObject.width > 30 ? messageObject.width - 30 : messageObject.width - text: messageObject.message.text textFormat: Text.PlainText selectByMouse: true readOnly: true color: "black" wrapMode: Text.Wrap font.pixelSize: 14 + + Component.onCompleted: { + text = messageObject.message.text + } } Row { HtmlViewSheet { -- cgit v1.2.3-70-g09d2