Skip to content

Commit

Permalink
Merge pull request qTox#5703
Browse files Browse the repository at this point in the history
Mick Sayson (8):
      refactor(messages): Create class to manage sending/receiving friend messages from core
      refactor(core): Remove length parameter from splitMessages
      refactor(messages): Create class to manage sending/receiving group messages from core
      fix(messages): Fix broken sanitized name for notifications/alert messages
      refactor(messages): Replace QRegExp with QRegularExpression
      refactor(chatlog): Add class to manage underlying chatlog state
      refactor(chatlog): Add a class to manage history through the IChatLog interface
      refactor(chatform): Remove message handling logic from gui path
  • Loading branch information
sudden6 committed Jun 21, 2019
2 parents 678fc51 + ed514d7 commit 033f28e
Show file tree
Hide file tree
Showing 51 changed files with 3,821 additions and 1,477 deletions.
16 changes: 16 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,19 @@ set(${PROJECT_NAME}_SOURCES
src/model/chatroom/groupchatroom.h
src/model/contact.cpp
src/model/contact.h
src/model/chatlogitem.cpp
src/model/chatlogitem.h
src/model/friend.cpp
src/model/friend.h
src/model/message.h
src/model/message.cpp
src/model/imessagedispatcher.h
src/model/friendmessagedispatcher.h
src/model/friendmessagedispatcher.cpp
src/model/groupmessagedispatcher.h
src/model/groupmessagedispatcher.cpp
src/model/message.h
src/model/message.cpp
src/model/groupinvite.cpp
src/model/groupinvite.h
src/model/group.cpp
Expand All @@ -337,6 +348,11 @@ set(${PROJECT_NAME}_SOURCES
src/model/profile/profileinfo.cpp
src/model/profile/profileinfo.h
src/model/dialogs/idialogs.h
src/model/ichatlog.h
src/model/sessionchatlog.h
src/model/sessionchatlog.cpp
src/model/chathistory.h
src/model/chathistory.cpp
src/net/bootstrapnodeupdater.cpp
src/net/bootstrapnodeupdater.h
src/net/avatarbroadcaster.cpp
Expand Down
4 changes: 4 additions & 0 deletions cmake/Testing.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ auto_test(net bsu)
auto_test(persistence paths)
auto_test(persistence dbschema)
auto_test(persistence offlinemsgengine)
auto_test(model friendmessagedispatcher)
auto_test(model groupmessagedispatcher)
auto_test(model messageprocessor)
auto_test(model sessionchatlog)

if (UNIX)
auto_test(platform posixsignalnotifier)
Expand Down
2 changes: 1 addition & 1 deletion src/chatlog/chatmessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ ChatMessage::Ptr ChatMessage::createChatMessage(const QString& sender, const QSt
authorFont.setBold(true);

QColor color = Style::getColor(Style::MainText);
if (colorizeName && Settings::getInstance().getEnableGroupChatsColor()) {
if (colorizeName) {
QByteArray hash = QCryptographicHash::hash((sender.toUtf8()), QCryptographicHash::Sha256);
quint8 *data = (quint8*)hash.data();

Expand Down
90 changes: 3 additions & 87 deletions src/chatlog/content/filetransferwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,6 @@ FileTransferWidget::FileTransferWidget(QWidget* parent, ToxFile file)

CoreFile* coreFile = Core::getInstance()->getCoreFile();

connect(coreFile, &CoreFile::fileTransferInfo, this,
&FileTransferWidget::onFileTransferInfo);
connect(coreFile, &CoreFile::fileTransferAccepted, this,
&FileTransferWidget::onFileTransferAccepted);
connect(coreFile, &CoreFile::fileTransferCancelled, this,
&FileTransferWidget::onFileTransferCancelled);
connect(coreFile, &CoreFile::fileTransferPaused, this,
&FileTransferWidget::onFileTransferPaused);
connect(coreFile, &CoreFile::fileTransferFinished, this,
&FileTransferWidget::onFileTransferFinished);
connect(coreFile, &CoreFile::fileTransferRemotePausedUnpaused, this,
&FileTransferWidget::fileTransferRemotePausedUnpaused);
connect(coreFile, &CoreFile::fileTransferBrokenUnbroken, this,
&FileTransferWidget::fileTransferBrokenUnbroken);
connect(ui->leftButton, &QPushButton::clicked, this, &FileTransferWidget::onLeftButtonClicked);
connect(ui->rightButton, &QPushButton::clicked, this, &FileTransferWidget::onRightButtonClicked);
connect(ui->previewButton, &QPushButton::clicked, this,
Expand Down Expand Up @@ -133,30 +119,9 @@ bool FileTransferWidget::tryRemoveFile(const QString& filepath)
return writable;
}

void FileTransferWidget::autoAcceptTransfer(const QString& path)
void FileTransferWidget::onFileTransferUpdate(ToxFile file)
{
QString filepath;
int number = 0;

QString suffix = QFileInfo(fileInfo.fileName).completeSuffix();
QString base = QFileInfo(fileInfo.fileName).baseName();

do {
filepath = QString("%1/%2%3.%4")
.arg(path, base,
number > 0 ? QString(" (%1)").arg(QString::number(number)) : QString(),
suffix);
++number;
} while (QFileInfo(filepath).exists());

// Do not automatically accept the file-transfer if the path is not writable.
// The user can still accept it manually.
if (tryRemoveFile(filepath)) {
CoreFile* coreFile = Core::getInstance()->getCoreFile();
coreFile->acceptFileRecvRequest(fileInfo.friendId, fileInfo.fileNum, filepath);
} else {
qWarning() << "Cannot write to " << filepath;
}
updateWidget(file);
}

bool FileTransferWidget::isActive() const
Expand Down Expand Up @@ -265,53 +230,6 @@ void FileTransferWidget::paintEvent(QPaintEvent*)
}
}

void FileTransferWidget::onFileTransferInfo(ToxFile file)
{
updateWidget(file);
}

void FileTransferWidget::onFileTransferAccepted(ToxFile file)
{
updateWidget(file);
}

void FileTransferWidget::onFileTransferCancelled(ToxFile file)
{
updateWidget(file);
}

void FileTransferWidget::onFileTransferPaused(ToxFile file)
{
updateWidget(file);
}

void FileTransferWidget::onFileTransferResumed(ToxFile file)
{
updateWidget(file);
}

void FileTransferWidget::onFileTransferFinished(ToxFile file)
{
updateWidget(file);
}

void FileTransferWidget::fileTransferRemotePausedUnpaused(ToxFile file, bool paused)
{
if (paused) {
onFileTransferPaused(file);
} else {
onFileTransferResumed(file);
}
}

void FileTransferWidget::fileTransferBrokenUnbroken(ToxFile file, bool broken)
{
// TODO: Handle broken transfer differently once we have resuming code
if (broken) {
onFileTransferCancelled(file);
}
}

QString FileTransferWidget::getHumanReadableSize(qint64 size)
{
static const char* suffix[] = {"B", "kiB", "MiB", "GiB", "TiB"};
Expand Down Expand Up @@ -737,9 +655,7 @@ void FileTransferWidget::applyTransformation(const int orientation, QImage& imag

void FileTransferWidget::updateWidget(ToxFile const& file)
{
if (fileInfo != file) {
return;
}
assert(file == fileInfo);

fileInfo = file;

Expand Down
11 changes: 1 addition & 10 deletions src/chatlog/content/filetransferwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,10 @@ class FileTransferWidget : public QWidget
public:
explicit FileTransferWidget(QWidget* parent, ToxFile file);
virtual ~FileTransferWidget();
void autoAcceptTransfer(const QString& path);
bool isActive() const;
static QString getHumanReadableSize(qint64 size);

protected slots:
void onFileTransferInfo(ToxFile file);
void onFileTransferAccepted(ToxFile file);
void onFileTransferCancelled(ToxFile file);
void onFileTransferPaused(ToxFile file);
void onFileTransferResumed(ToxFile file);
void onFileTransferFinished(ToxFile file);
void fileTransferRemotePausedUnpaused(ToxFile file, bool paused);
void fileTransferBrokenUnbroken(ToxFile file, bool broken);
void onFileTransferUpdate(ToxFile file);

protected:
void updateWidgetColor(ToxFile const& file);
Expand Down
30 changes: 17 additions & 13 deletions src/core/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@

const QString Core::TOX_EXT = ".tox";

#define MAX_GROUP_MESSAGE_LEN 1024

#define ASSERT_CORE_THREAD assert(QThread::currentThread() == coreThread.get())

namespace {
Expand Down Expand Up @@ -715,17 +713,21 @@ void Core::sendGroupMessageWithType(int groupId, const QString& message, Tox_Mes
{
QMutexLocker ml{&coreLoopLock};

QStringList cMessages = splitMessage(message, MAX_GROUP_MESSAGE_LEN);
int size = message.toUtf8().size();
auto maxSize = tox_max_message_length();
if (size > maxSize) {
qCritical() << "Core::sendMessageWithType called with message of size:" << size
<< "when max is:" << maxSize << ". Ignoring.";
return;
}

for (auto& part : cMessages) {
ToxString cMsg(part);
Tox_Err_Conference_Send_Message error;
bool ok =
tox_conference_send_message(tox.get(), groupId, type, cMsg.data(), cMsg.size(), &error);
if (!ok || !parseConferenceSendMessageError(error)) {
emit groupSentFailed(groupId);
return;
}
ToxString cMsg(message);
Tox_Err_Conference_Send_Message error;
bool ok =
tox_conference_send_message(tox.get(), groupId, type, cMsg.data(), cMsg.size(), &error);
if (!ok || !parseConferenceSendMessageError(error)) {
emit groupSentFailed(groupId);
return;
}
}

Expand Down Expand Up @@ -1434,11 +1436,13 @@ QString Core::getFriendUsername(uint32_t friendnumber) const
return sname.getQString();
}

QStringList Core::splitMessage(const QString& message, int maxLen)
QStringList Core::splitMessage(const QString& message)
{
QStringList splittedMsgs;
QByteArray ba_message{message.toUtf8()};

const auto maxLen = tox_max_message_length();

while (ba_message.size() > maxLen) {
int splitPos = ba_message.lastIndexOf('\n', maxLen - 1);

Expand Down
33 changes: 20 additions & 13 deletions src/core/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

#include "groupid.h"
#include "icorefriendmessagesender.h"
#include "icoregroupmessagesender.h"
#include "icoregroupquery.h"
#include "icoreidhandler.h"
#include "receiptnum.h"
#include "toxfile.h"
#include "toxid.h"
Expand Down Expand Up @@ -50,7 +53,11 @@ class Core;

using ToxCorePtr = std::unique_ptr<Core>;

class Core : public QObject, public ICoreFriendMessageSender
class Core : public QObject,
public ICoreFriendMessageSender,
public ICoreIdHandler,
public ICoreGroupMessageSender,
public ICoreGroupQuery
{
Q_OBJECT
public:
Expand All @@ -71,15 +78,15 @@ class Core : public QObject, public ICoreFriendMessageSender
~Core();

static const QString TOX_EXT;
static QStringList splitMessage(const QString& message, int maxLen);
static QStringList splitMessage(const QString& message);
QString getPeerName(const ToxPk& id) const;
QVector<uint32_t> getFriendList() const;
GroupId getGroupPersistentId(uint32_t groupNumber) const;
uint32_t getGroupNumberPeers(int groupId) const;
QString getGroupPeerName(int groupId, int peerId) const;
ToxPk getGroupPeerPk(int groupId, int peerId) const;
QStringList getGroupPeerNames(int groupId) const;
bool getGroupAvEnabled(int groupId) const;
GroupId getGroupPersistentId(uint32_t groupNumber) const override;
uint32_t getGroupNumberPeers(int groupId) const override;
QString getGroupPeerName(int groupId, int peerId) const override;
ToxPk getGroupPeerPk(int groupId, int peerId) const override;
QStringList getGroupPeerNames(int groupId) const override;
bool getGroupAvEnabled(int groupId) const override;
ToxPk getFriendPublicKey(uint32_t friendNumber) const;
QString getFriendUsername(uint32_t friendNumber) const;

Expand All @@ -88,11 +95,11 @@ class Core : public QObject, public ICoreFriendMessageSender
uint32_t joinGroupchat(const GroupInvite& inviteInfo);
void quitGroupChat(int groupId) const;

QString getUsername() const;
QString getUsername() const override;
Status::Status getStatus() const;
QString getStatusMessage() const;
ToxId getSelfId() const;
ToxPk getSelfPublicKey() const;
ToxId getSelfId() const override;
ToxPk getSelfPublicKey() const override;
QPair<QByteArray, QByteArray> getKeypair() const;

void sendFile(uint32_t friendId, QString filename, QString filePath, long long filesize);
Expand All @@ -115,8 +122,8 @@ public slots:
void setStatusMessage(const QString& message);

bool sendMessage(uint32_t friendId, const QString& message, ReceiptNum& receipt) override;
void sendGroupMessage(int groupId, const QString& message);
void sendGroupAction(int groupId, const QString& message);
void sendGroupMessage(int groupId, const QString& message) override;
void sendGroupAction(int groupId, const QString& message) override;
void changeGroupTitle(int groupId, const QString& title);
bool sendAction(uint32_t friendId, const QString& action, ReceiptNum& receipt) override;
void sendTyping(uint32_t friendId, bool typing);
Expand Down
1 change: 1 addition & 0 deletions src/core/icorefriendmessagesender.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
class ICoreFriendMessageSender
{
public:
virtual ~ICoreFriendMessageSender() = default;
virtual bool sendAction(uint32_t friendId, const QString& action, ReceiptNum& receipt) = 0;
virtual bool sendMessage(uint32_t friendId, const QString& message, ReceiptNum& receipt) = 0;
};
Expand Down
33 changes: 33 additions & 0 deletions src/core/icoregroupmessagesender.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copyright © 2019 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox.
qTox is libre software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
qTox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with qTox. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef ICORE_GROUP_MESSAGE_SENDER_H
#define ICORE_GROUP_MESSAGE_SENDER_H

#include <QString>

class ICoreGroupMessageSender
{
public:
virtual ~ICoreGroupMessageSender() = default;
virtual void sendGroupAction(int groupId, const QString& message) = 0;
virtual void sendGroupMessage(int groupId, const QString& message) = 0;
};

#endif /*ICORE_GROUP_MESSAGE_SENDER_H*/
Loading

0 comments on commit 033f28e

Please sign in to comment.