1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

Merge pull request #302 from apprb/dev

Split message
This commit is contained in:
Tux3 / Mlkj / !Lev.uXFMLA 2014-09-22 00:01:36 +02:00
commit 94f9a6d431
4 changed files with 63 additions and 8 deletions

View File

@ -34,6 +34,7 @@
#include <QTimer>
#include <QCoreApplication>
#include <QDateTime>
#include <QList>
const QString Core::CONFIG_FILE_NAME = "data";
QList<ToxFile> 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<CString> 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<CString> 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<CString> Core::splitMessage(const QString &message)
{
QList<CString> 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;
}

3
core.h
View File

@ -28,6 +28,7 @@ template <typename T> class QList;
class Camera;
class QTimer;
class QString;
class CString;
class Core : public QObject
{
@ -210,6 +211,8 @@ private:
void checkLastOnline(int friendId);
QList<CString> splitMessage(const QString &message);
private slots:
void onFileTransferFinished(ToxFile file);

View File

@ -17,10 +17,23 @@
#include "cstring.h"
#include <QString>
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<const uint8_t*>(ba_string.data()), cStringSize);
}
CString::CString(const CString &cstr)
{
cStringSize = cstr.cStringSize;
cString = new uint8_t[cStringSize]();
memcpy(cString, cstr.cString, cStringSize);
}
CString::~CString()

View File

@ -20,11 +20,14 @@
#include <cstdint>
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();