From 68654179e99645198b24e78b00676ff0ada7dc63 Mon Sep 17 00:00:00 2001 From: apprb Date: Sun, 21 Sep 2014 17:30:10 +0700 Subject: [PATCH] Split message --- core.cpp | 46 +++++++++++++++++++++++++++++++++++++++++----- core.h | 3 +++ cstring.cpp | 19 ++++++++++++++++--- cstring.h | 3 +++ 4 files changed, 63 insertions(+), 8 deletions(-) diff --git a/core.cpp b/core.cpp index fe1e3187b..c37c78146 100644 --- a/core.cpp +++ b/core.cpp @@ -34,6 +34,7 @@ #include #include #include +#include const QString Core::CONFIG_FILE_NAME = "data"; QList Core::fileSendQueue; @@ -473,10 +474,13 @@ void Core::requestFriendship(const QString& friendAddress, const QString& messag void Core::sendMessage(int friendId, const QString& message) { - CString cMessage(message); + QList cMessages = splitMessage(message); - int messageId = tox_send_message(tox, friendId, cMessage.data(), cMessage.size()); - emit messageSentResult(friendId, message, messageId); + for (auto &cMsg :cMessages) + { + int messageId = tox_send_message(tox, friendId, cMsg.data(), cMsg.size()); + emit messageSentResult(friendId, message, messageId); + } } void Core::sendAction(int friendId, const QString &action) @@ -495,9 +499,12 @@ void Core::sendTyping(int friendId, bool typing) void Core::sendGroupMessage(int groupId, const QString& message) { - CString cMessage(message); + QList cMessages = splitMessage(message); - tox_group_message_send(tox, groupId, cMessage.data(), cMessage.size()); + for (auto &cMsg :cMessages) + { + tox_group_message_send(tox, groupId, cMsg.data(), cMsg.size()); + } } void Core::sendFile(int32_t friendId, QString Filename, QString FilePath, long long filesize) @@ -1159,3 +1166,32 @@ QString Core::getFriendAddress(int friendNumber) const return id; } + +QList Core::splitMessage(const QString &message) +{ + QList splittedMsgs; + QByteArray ba_message(message.toUtf8()); + + while (ba_message.size() > TOX_MAX_MESSAGE_LENGTH) + { + int splitPos = ba_message.lastIndexOf(' ', TOX_MAX_MESSAGE_LENGTH - 1); + if (splitPos <= 0) + { + splitPos = TOX_MAX_MESSAGE_LENGTH; + if (ba_message[splitPos] & 0x80) + { + do { + splitPos--; + } while (!(ba_message[splitPos] & 0x40)); + } + splitPos--; + } + + splittedMsgs.push_back(CString(ba_message.left(splitPos + 1))); + ba_message = ba_message.mid(splitPos + 1); + } + + splittedMsgs.push_back(CString(ba_message)); + + return splittedMsgs; +} diff --git a/core.h b/core.h index 13d051017..706f448a3 100644 --- a/core.h +++ b/core.h @@ -28,6 +28,7 @@ template class QList; class Camera; class QTimer; class QString; +class CString; class Core : public QObject { @@ -210,6 +211,8 @@ private: void checkLastOnline(int friendId); + QList splitMessage(const QString &message); + private slots: void onFileTransferFinished(ToxFile file); diff --git a/cstring.cpp b/cstring.cpp index 6d7815e9c..b9217fbc8 100644 --- a/cstring.cpp +++ b/cstring.cpp @@ -17,10 +17,23 @@ #include "cstring.h" #include -CString::CString(const QString& string) +CString::CString(const QString& string) : + CString(string.toUtf8()) { - cString = new uint8_t[string.length() * MAX_SIZE_OF_UTF8_ENCODED_CHARACTER](); - cStringSize = fromString(string, cString); +} + +CString::CString(const QByteArray& ba_string) +{ + cString = new uint8_t[ba_string.size()](); + cStringSize = ba_string.size(); + memcpy(cString, reinterpret_cast(ba_string.data()), cStringSize); +} + +CString::CString(const CString &cstr) +{ + cStringSize = cstr.cStringSize; + cString = new uint8_t[cStringSize](); + memcpy(cString, cstr.cString, cStringSize); } CString::~CString() diff --git a/cstring.h b/cstring.h index a75ed6468..818c79bc8 100644 --- a/cstring.h +++ b/cstring.h @@ -20,11 +20,14 @@ #include class QString; +class QByteArray; class CString { public: explicit CString(const QString& string); + explicit CString(const QByteArray& ba_string); + explicit CString(const CString& cstr); ~CString(); uint8_t* data();