removed Order, resolved a crash on several files being uploaded simultaniuosly

This commit is contained in:
Blue 2023-11-09 19:36:30 -03:00
parent 0a530bfa93
commit be466fbad1
Signed by untrusted user: blue
GPG key ID: 9B203B252A63EE38
12 changed files with 122 additions and 304 deletions

View file

@ -33,6 +33,12 @@
#include <QBitmap>
#include <unistd.h>
#include <algorithm>
#include "shared/icons.h"
#include "shared/utils.h"
#include "shared/pathcheck.h"
#include "shared/defines.h"
constexpr QSize avatarSize(50, 50);
@ -61,58 +67,9 @@ Conversation::Conversation(bool muc, Models::Account* acc, Models::Element* el,
currentAction(CurrentAction::none),
currentMessageId()
{
m_ui->setupUi(this);
shadow.setFrames(true, false, true, false);
feed->setItemDelegate(delegate);
feed->setFrameShape(QFrame::NoFrame);
feed->setContextMenuPolicy(Qt::CustomContextMenu);
feed->setModel(el->feed);
el->feed->incrementObservers();
m_ui->widget->layout()->addWidget(feed);
connect(el->feed, &Models::MessageFeed::newMessage, this, &Conversation::onFeedMessage);
connect(feed, &FeedView::resized, this, &Conversation::positionShadow);
connect(feed, &FeedView::customContextMenuRequested, this, &Conversation::onFeedContext);
connect(acc, &Models::Account::childChanged, this, &Conversation::onAccountChanged);
filesLayout = new FlowLayout(m_ui->filesPanel, 0);
m_ui->filesPanel->setLayout(filesLayout);
statusIcon = m_ui->statusIcon;
statusLabel = m_ui->statusLabel;
connect(&ker, &KeyEnterReceiver::enterPressed, this, qOverload<>(&Conversation::initiateMessageSending));
connect(&ker, &KeyEnterReceiver::imagePasted, this, &Conversation::onImagePasted);
connect(m_ui->sendButton, &QPushButton::clicked, this, qOverload<>(&Conversation::initiateMessageSending));
connect(m_ui->attachButton, &QPushButton::clicked, this, &Conversation::onAttach);
connect(m_ui->clearButton, &QPushButton::clicked, this, &Conversation::clear);
connect(m_ui->messageEditor->document()->documentLayout(), &QAbstractTextDocumentLayout::documentSizeChanged,
this, &Conversation::onTextEditDocSizeChanged);
connect(m_ui->encryptionButton, &QPushButton::clicked, this, &Conversation::onEncryptionButtonClicked);
m_ui->messageEditor->installEventFilter(&ker);
connect(m_ui->messageEditor, &QTextEdit::customContextMenuRequested, this, &Conversation::onMessageEditorContext);
connect(pasteImageAction, &QAction::triggered, this, &Conversation::onImagePasted);
connect(m_ui->currentActionBadge, &Badge::close, this, &Conversation::clear);
m_ui->currentActionBadge->setVisible(false);
m_ui->encryptionButton->setVisible(false);
//line->setAutoFillBackground(false);
//if (testAttribute(Qt::WA_TranslucentBackground)) {
//m_ui->scrollArea->setAutoFillBackground(false);
//} else {
//m_ui->scrollArea->setBackgroundRole(QPalette::Base);
//}
//line->setMyAvatarPath(acc->getAvatarPath());
//line->setMyName(acc->getName());
createUI();
createFeed();
subscribeEvents();
initializeOverlay();
}
@ -123,6 +80,52 @@ Conversation::~Conversation() {
delete m_ui;
}
void Conversation::createFeed() {
feed->setItemDelegate(delegate);
feed->setFrameShape(QFrame::NoFrame);
feed->setContextMenuPolicy(Qt::CustomContextMenu);
feed->setModel(element->feed);
element->feed->incrementObservers();
m_ui->widget->layout()->addWidget(feed);
connect(element->feed, &Models::MessageFeed::newMessage, this, &Conversation::onFeedMessage);
connect(feed, &FeedView::resized, this, &Conversation::positionShadow);
connect(feed, &FeedView::customContextMenuRequested, this, &Conversation::onFeedContext);
}
void Conversation::createUI() {
m_ui->setupUi(this);
statusIcon = m_ui->statusIcon;
statusLabel = m_ui->statusLabel;
filesLayout = new FlowLayout(m_ui->filesPanel, 0);
m_ui->filesPanel->setLayout(filesLayout);
m_ui->currentActionBadge->setVisible(false);
m_ui->encryptionButton->setVisible(false);
shadow.setFrames(true, false, true, false);
}
void Conversation::subscribeEvents() {
connect(account, &Models::Account::childChanged, this, &Conversation::onAccountChanged);
connect(&ker, &KeyEnterReceiver::enterPressed, this, qOverload<>(&Conversation::initiateMessageSending));
connect(&ker, &KeyEnterReceiver::imagePasted, this, &Conversation::onImagePasted);
connect(m_ui->sendButton, &QPushButton::clicked, this, qOverload<>(&Conversation::initiateMessageSending));
connect(m_ui->attachButton, &QPushButton::clicked, this, &Conversation::onAttach);
connect(m_ui->clearButton, &QPushButton::clicked, this, &Conversation::clear);
connect(m_ui->messageEditor->document()->documentLayout(), &QAbstractTextDocumentLayout::documentSizeChanged,
this, &Conversation::onTextEditDocSizeChanged);
connect(m_ui->encryptionButton, &QPushButton::clicked, this, &Conversation::onEncryptionButtonClicked);
m_ui->messageEditor->installEventFilter(&ker);
connect(m_ui->messageEditor, &QTextEdit::customContextMenuRequested, this, &Conversation::onMessageEditorContext);
connect(pasteImageAction, &QAction::triggered, this, &Conversation::onImagePasted);
connect(m_ui->currentActionBadge, &Badge::closeClicked, this, &Conversation::clear);
}
void Conversation::onAccountChanged(Models::Item* item, int row, int col) {
SHARED_UNUSED(row);
if (item == account) {
@ -228,7 +231,7 @@ void Conversation::initiateMessageSending() {
initiateMessageSending(msg);
}
if (filesToAttach.size() > 0) {
for (Badge* badge : filesToAttach) {
for (const Badge* badge : filesToAttach) {
Shared::Message msg = createMessage();
msg.setAttachPath(badge->id);
element->feed->registerUpload(msg.getId());
@ -291,6 +294,14 @@ Models::Roster::ElId Conversation::getId() const {
}
void Conversation::addAttachedFile(const QString& path) {
std::vector<Badge*>::const_iterator itr = std::find_if(
filesToAttach.begin(),
filesToAttach.end(),
[&path] (const Badge* badge) {return badge->id == path;}
);
if (itr != filesToAttach.end())
return;
QMimeDatabase db;
QMimeType type = db.mimeTypeForFile(path);
QFileInfo info(path);
@ -300,25 +311,23 @@ void Conversation::addAttachedFile(const QString& path) {
fileIcon.addFile(QString::fromUtf8(":/images/fallback/dark/big/mail-attachment.svg"), QSize(), QIcon::Normal, QIcon::Off);
Badge* badge = new Badge(path, info.fileName(), fileIcon);
filesToAttach.push_back(badge);
connect(badge, &Badge::close, this, &Conversation::onBadgeClose);
try {
filesToAttach.push_back(badge);
filesLayout->addWidget(badge);
if (filesLayout->count() == 1) {
filesLayout->setContentsMargins(3, 3, 3, 3);
}
} catch (const W::Order<Badge*, Badge::Comparator>::Duplicates& e) {
delete badge;
} catch (...) {
throw;
}
connect(badge, &Badge::closeClicked, this, &Conversation::onBadgeClose);
filesLayout->addWidget(badge);
if (filesLayout->count() == 1)
filesLayout->setContentsMargins(3, 3, 3, 3);
}
void Conversation::removeAttachedFile(Badge* badge) {
W::Order<Badge*, Badge::Comparator>::const_iterator itr = filesToAttach.find(badge);
void Conversation::removeAttachedFile(const QString& id) {
std::vector<Badge*>::const_iterator itr = std::find_if(
filesToAttach.begin(),
filesToAttach.end(),
[&id] (const Badge* badge) {return badge->id == id;}
);
if (itr != filesToAttach.end()) {
filesToAttach.erase(badge);
Badge* badge = *itr;
filesToAttach.erase(itr);
if (filesLayout->count() == 1)
filesLayout->setContentsMargins(0, 0, 0, 0);
@ -328,7 +337,7 @@ void Conversation::removeAttachedFile(Badge* badge) {
void Conversation::onBadgeClose() {
Badge* badge = static_cast<Badge*>(sender());
removeAttachedFile(badge);
removeAttachedFile(badge->id);
}
void Conversation::clearAttachedFiles() {
@ -351,12 +360,10 @@ void Conversation::onEncryptionButtonClicked() {}
void Conversation::setAvatar(const QString& path) {
QPixmap pixmap;
if (path.size() == 0) {
if (path.size() == 0)
pixmap = Shared::icon("user", true).pixmap(avatarSize);
} else {
else
pixmap = QPixmap(path).scaled(avatarSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
QPixmap result(avatarSize);
result.fill(Qt::transparent);
@ -379,8 +386,7 @@ void Conversation::setFeedFrames(bool top, bool right, bool bottom, bool left) {
shadow.setFrames(top, right, bottom, left);
}
void Conversation::dragEnterEvent(QDragEnterEvent* event)
{
void Conversation::dragEnterEvent(QDragEnterEvent* event) {
bool accept = false;
if (event->mimeData()->hasUrls()) {
QList<QUrl> list = event->mimeData()->urls();
@ -542,9 +548,8 @@ void Conversation::onMessageEditRequested(const QString& id) {
currentAction = CurrentAction::edit;
m_ui->messageEditor->setText(msg.getBody());
QString path = msg.getAttachPath();
if (path.size() > 0) {
if (path.size() > 0)
addAttachedFile(path);
}
} catch (const Models::MessageFeed::NotFound& e) {
qDebug() << "The message requested to be edited was not found" << e.getMessage().c_str();

View file

@ -16,8 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CONVERSATION_H
#define CONVERSATION_H
#pragma once
#include <QWidget>
#include <QObject>
@ -30,12 +29,9 @@
#include <QAction>
#include <QDesktopServices>
#include <vector>
#include "shared/message.h"
#include "shared/order.h"
#include "shared/icons.h"
#include "shared/utils.h"
#include "shared/pathcheck.h"
#include "shared/defines.h"
#include "ui/models/account.h"
#include "ui/models/roster.h"
@ -96,7 +92,7 @@ protected:
virtual Shared::Message createMessage() const;
void setStatus(const QString& status);
void addAttachedFile(const QString& path);
void removeAttachedFile(Badge* badge);
void removeAttachedFile(const QString& id);
void clearAttachedFiles();
void dragEnterEvent(QDragEnterEvent* event) override;
void dragLeaveEvent(QDragLeaveEvent* event) override;
@ -104,6 +100,10 @@ protected:
void initializeOverlay();
virtual void onMessage(const Shared::Message& msg);
virtual void showEvent(QShowEvent * event) override;
void createFeed();
void createUI();
void subscribeEvents();
protected slots:
void initiateMessageSending();
@ -142,7 +142,7 @@ protected:
QLabel* statusLabel;
FlowLayout* filesLayout;
QWidget* overlay;
W::Order<Badge*, Badge::Comparator> filesToAttach;
std::vector<Badge*> filesToAttach;
FeedView* feed;
MessageDelegate* delegate;
bool manualSliderChange;
@ -161,5 +161,3 @@ private:
static QPixmap* avatarPixmap;
static QPainter* avatarPainter;
};
#endif // CONVERSATION_H

View file

@ -18,6 +18,8 @@
#include "room.h"
#include "shared/defines.h"
Room::Room(Models::Account* acc, Models::Room* p_room, QWidget* parent):
Conversation(true, acc, p_room, p_room->getJid(), "", parent),
room(p_room)