2019-04-08 21:40:49 +00:00
|
|
|
/*
|
2019-08-14 14:54:46 +00:00
|
|
|
* Squawk messenger.
|
2019-04-08 21:40:49 +00:00
|
|
|
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
|
|
|
*
|
|
|
|
* This program is free 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.
|
|
|
|
*
|
|
|
|
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "conversation.h"
|
|
|
|
#include "ui_conversation.h"
|
2020-03-30 21:17:10 +00:00
|
|
|
|
2019-04-09 15:04:08 +00:00
|
|
|
#include <QDebug>
|
2021-10-06 15:09:18 +00:00
|
|
|
#include <QClipboard>
|
2019-04-12 15:22:10 +00:00
|
|
|
#include <QScrollBar>
|
2019-05-15 17:36:37 +00:00
|
|
|
#include <QTimer>
|
2019-06-21 19:33:38 +00:00
|
|
|
#include <QFileDialog>
|
2019-09-19 14:31:27 +00:00
|
|
|
#include <QMimeDatabase>
|
2020-03-30 21:17:10 +00:00
|
|
|
#include <QAbstractTextDocumentLayout>
|
2020-04-03 22:28:15 +00:00
|
|
|
#include <QCoreApplication>
|
2021-10-06 15:09:18 +00:00
|
|
|
#include <QTemporaryFile>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QMenu>
|
2022-01-08 22:28:29 +00:00
|
|
|
#include <QBitmap>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
constexpr QSize avatarSize(50, 50);
|
2019-04-09 15:04:08 +00:00
|
|
|
|
2020-08-12 16:55:01 +00:00
|
|
|
Conversation::Conversation(bool muc, Models::Account* acc, Models::Element* el, const QString pJid, const QString pRes, QWidget* parent):
|
2019-04-09 15:04:08 +00:00
|
|
|
QWidget(parent),
|
2019-08-29 14:19:35 +00:00
|
|
|
isMuc(muc),
|
2019-12-20 15:41:20 +00:00
|
|
|
account(acc),
|
2021-04-23 11:53:48 +00:00
|
|
|
element(el),
|
2019-08-23 09:51:33 +00:00
|
|
|
palJid(pJid),
|
|
|
|
activePalResource(pRes),
|
2019-04-13 20:38:20 +00:00
|
|
|
m_ui(new Ui::Conversation()),
|
2019-04-12 15:22:10 +00:00
|
|
|
ker(),
|
2019-04-13 20:38:20 +00:00
|
|
|
thread(),
|
2019-08-23 09:51:33 +00:00
|
|
|
statusIcon(0),
|
|
|
|
statusLabel(0),
|
2019-09-19 14:31:27 +00:00
|
|
|
filesLayout(0),
|
2020-04-14 16:30:33 +00:00
|
|
|
overlay(new QWidget()),
|
2019-09-19 14:31:27 +00:00
|
|
|
filesToAttach(),
|
2020-08-15 21:48:28 +00:00
|
|
|
feed(new FeedView()),
|
2021-02-01 22:55:15 +00:00
|
|
|
delegate(new MessageDelegate(this)),
|
2019-05-15 17:36:37 +00:00
|
|
|
manualSliderChange(false),
|
2021-05-03 00:35:43 +00:00
|
|
|
tsb(QApplication::style()->styleHint(QStyle::SH_ScrollBar_Transient) == 1),
|
2021-10-13 12:06:13 +00:00
|
|
|
pasteImageAction(new QAction(tr("Paste Image"), this)),
|
2021-05-04 14:09:41 +00:00
|
|
|
shadow(10, 1, Qt::black, this),
|
2022-02-20 19:10:09 +00:00
|
|
|
contextMenu(new QMenu()),
|
2022-03-28 20:25:33 +00:00
|
|
|
currentAction(CurrentAction::none),
|
|
|
|
currentMessageId()
|
2019-04-09 15:04:08 +00:00
|
|
|
{
|
|
|
|
m_ui->setupUi(this);
|
2020-08-12 16:55:01 +00:00
|
|
|
|
2021-05-03 00:35:43 +00:00
|
|
|
shadow.setFrames(true, false, true, false);
|
|
|
|
|
2020-08-20 21:32:30 +00:00
|
|
|
feed->setItemDelegate(delegate);
|
2021-05-03 00:35:43 +00:00
|
|
|
feed->setFrameShape(QFrame::NoFrame);
|
2021-05-04 14:09:41 +00:00
|
|
|
feed->setContextMenuPolicy(Qt::CustomContextMenu);
|
2020-08-20 21:32:30 +00:00
|
|
|
delegate->initializeFonts(feed->getFont());
|
2020-08-15 21:48:28 +00:00
|
|
|
feed->setModel(el->feed);
|
2021-04-27 19:29:15 +00:00
|
|
|
el->feed->incrementObservers();
|
2020-08-15 21:48:28 +00:00
|
|
|
m_ui->widget->layout()->addWidget(feed);
|
2019-09-19 14:31:27 +00:00
|
|
|
|
2021-04-27 19:29:15 +00:00
|
|
|
connect(el->feed, &Models::MessageFeed::newMessage, this, &Conversation::onFeedMessage);
|
2021-05-03 00:35:43 +00:00
|
|
|
connect(feed, &FeedView::resized, this, &Conversation::positionShadow);
|
2021-05-04 14:09:41 +00:00
|
|
|
connect(feed, &FeedView::customContextMenuRequested, this, &Conversation::onFeedContext);
|
2021-04-27 19:29:15 +00:00
|
|
|
|
2020-08-07 23:33:03 +00:00
|
|
|
connect(acc, &Models::Account::childChanged, this, &Conversation::onAccountChanged);
|
|
|
|
|
2019-09-19 14:31:27 +00:00
|
|
|
filesLayout = new FlowLayout(m_ui->filesPanel, 0);
|
|
|
|
m_ui->filesPanel->setLayout(filesLayout);
|
|
|
|
|
2019-08-23 09:51:33 +00:00
|
|
|
statusIcon = m_ui->statusIcon;
|
|
|
|
statusLabel = m_ui->statusLabel;
|
2019-04-09 15:04:08 +00:00
|
|
|
|
2022-03-28 20:25:33 +00:00
|
|
|
connect(&ker, &KeyEnterReceiver::enterPressed, this, qOverload<>(&Conversation::initiateMessageSending));
|
2021-10-06 15:09:18 +00:00
|
|
|
connect(&ker, &KeyEnterReceiver::imagePasted, this, &Conversation::onImagePasted);
|
2022-03-28 20:25:33 +00:00
|
|
|
connect(m_ui->sendButton, &QPushButton::clicked, this, qOverload<>(&Conversation::initiateMessageSending));
|
2019-11-03 18:46:40 +00:00
|
|
|
connect(m_ui->attachButton, &QPushButton::clicked, this, &Conversation::onAttach);
|
2022-03-28 20:25:33 +00:00
|
|
|
connect(m_ui->clearButton, &QPushButton::clicked, this, &Conversation::clear);
|
2020-03-30 21:17:10 +00:00
|
|
|
connect(m_ui->messageEditor->document()->documentLayout(), &QAbstractTextDocumentLayout::documentSizeChanged,
|
|
|
|
this, &Conversation::onTextEditDocSizeChanged);
|
2019-04-09 22:01:25 +00:00
|
|
|
|
2019-04-10 15:22:52 +00:00
|
|
|
m_ui->messageEditor->installEventFilter(&ker);
|
2021-10-06 15:09:18 +00:00
|
|
|
m_ui->messageEditor->setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
|
2021-10-13 12:06:13 +00:00
|
|
|
connect(m_ui->messageEditor, &QTextEdit::customContextMenuRequested, this, &Conversation::onMessageEditorContext);
|
|
|
|
connect(pasteImageAction, &QAction::triggered, this, &Conversation::onImagePasted);
|
2021-10-06 15:09:18 +00:00
|
|
|
|
2022-03-28 20:25:33 +00:00
|
|
|
connect(m_ui->currentActionBadge, &Badge::close, this, &Conversation::clear);
|
|
|
|
m_ui->currentActionBadge->setVisible(false);
|
|
|
|
|
2020-08-11 22:49:51 +00:00
|
|
|
//line->setAutoFillBackground(false);
|
|
|
|
//if (testAttribute(Qt::WA_TranslucentBackground)) {
|
|
|
|
//m_ui->scrollArea->setAutoFillBackground(false);
|
|
|
|
//} else {
|
|
|
|
//m_ui->scrollArea->setBackgroundRole(QPalette::Base);
|
|
|
|
//}
|
2020-02-04 15:14:51 +00:00
|
|
|
|
2020-08-11 22:49:51 +00:00
|
|
|
//line->setMyAvatarPath(acc->getAvatarPath());
|
|
|
|
//line->setMyName(acc->getName());
|
2019-12-20 15:41:20 +00:00
|
|
|
|
2021-05-03 00:35:43 +00:00
|
|
|
initializeOverlay();
|
2019-04-09 15:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Conversation::~Conversation()
|
|
|
|
{
|
2021-05-04 14:09:41 +00:00
|
|
|
delete contextMenu;
|
|
|
|
|
2021-04-27 19:29:15 +00:00
|
|
|
element->feed->decrementObservers();
|
2019-04-09 15:04:08 +00:00
|
|
|
}
|
|
|
|
|
2020-08-07 23:33:03 +00:00
|
|
|
void Conversation::onAccountChanged(Models::Item* item, int row, int col)
|
|
|
|
{
|
|
|
|
if (item == account) {
|
2021-04-27 19:29:15 +00:00
|
|
|
if (col == 2 && account->getState() == Shared::ConnectionState::connected) { //to request the history when we're back online after reconnect
|
|
|
|
//if (!requestingHistory) {
|
|
|
|
//requestingHistory = true;
|
2020-08-11 22:49:51 +00:00
|
|
|
//line->showBusyIndicator();
|
|
|
|
//emit requestArchive("");
|
|
|
|
//scroll = down;
|
2021-04-27 19:29:15 +00:00
|
|
|
//}
|
2020-08-07 23:33:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-03 00:35:43 +00:00
|
|
|
void Conversation::initializeOverlay()
|
2019-06-19 14:15:20 +00:00
|
|
|
{
|
2021-05-03 00:35:43 +00:00
|
|
|
QGridLayout* gr = static_cast<QGridLayout*>(layout());
|
|
|
|
QLabel* progressLabel = new QLabel(tr("Drop files here to attach them to your message"));
|
|
|
|
gr->addWidget(overlay, 0, 0, 2, 1);
|
|
|
|
QVBoxLayout* nl = new QVBoxLayout();
|
|
|
|
QGraphicsOpacityEffect* opacity = new QGraphicsOpacityEffect();
|
|
|
|
opacity->setOpacity(0.8);
|
|
|
|
overlay->setLayout(nl);
|
|
|
|
overlay->setBackgroundRole(QPalette::Base);
|
|
|
|
overlay->setAutoFillBackground(true);
|
|
|
|
overlay->setGraphicsEffect(opacity);
|
|
|
|
progressLabel->setAlignment(Qt::AlignCenter);
|
|
|
|
QFont pf = progressLabel->font();
|
|
|
|
pf.setBold(true);
|
|
|
|
pf.setPointSize(26);
|
|
|
|
progressLabel->setWordWrap(true);
|
|
|
|
progressLabel->setFont(pf);
|
|
|
|
nl->addStretch();
|
|
|
|
nl->addWidget(progressLabel);
|
|
|
|
nl->addStretch();
|
|
|
|
overlay->hide();
|
2019-06-19 14:15:20 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 15:04:08 +00:00
|
|
|
void Conversation::setName(const QString& name)
|
|
|
|
{
|
2019-06-18 15:08:03 +00:00
|
|
|
m_ui->nameLabel->setText(name);
|
2019-06-21 19:33:38 +00:00
|
|
|
setWindowTitle(name);
|
2019-04-09 15:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Conversation::getAccount() const
|
|
|
|
{
|
2019-12-20 15:41:20 +00:00
|
|
|
return account->getName();
|
2019-04-09 15:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Conversation::getJid() const
|
|
|
|
{
|
2019-08-23 09:51:33 +00:00
|
|
|
return palJid;
|
2019-04-09 15:04:08 +00:00
|
|
|
}
|
2019-04-09 22:01:25 +00:00
|
|
|
|
2019-04-10 15:22:52 +00:00
|
|
|
KeyEnterReceiver::KeyEnterReceiver(QObject* parent): QObject(parent), ownEvent(false) {}
|
|
|
|
|
2019-04-09 22:01:25 +00:00
|
|
|
bool KeyEnterReceiver::eventFilter(QObject* obj, QEvent* event)
|
|
|
|
{
|
2019-04-10 15:22:52 +00:00
|
|
|
QEvent::Type type = event->type();
|
|
|
|
if (type == QEvent::KeyPress) {
|
2019-04-09 22:01:25 +00:00
|
|
|
QKeyEvent* key = static_cast<QKeyEvent*>(event);
|
2019-04-10 15:22:52 +00:00
|
|
|
int k = key->key();
|
|
|
|
if (k == Qt::Key_Enter || k == Qt::Key_Return) {
|
|
|
|
Qt::KeyboardModifiers mod = key->modifiers();
|
|
|
|
if (mod & Qt::ControlModifier) {
|
|
|
|
mod = mod & ~Qt::ControlModifier;
|
|
|
|
QKeyEvent* nEvent = new QKeyEvent(event->type(), k, mod, key->text(), key->isAutoRepeat(), key->count());
|
|
|
|
QCoreApplication::postEvent(obj, nEvent);
|
|
|
|
ownEvent = true;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
if (ownEvent) {
|
|
|
|
ownEvent = false;
|
|
|
|
} else {
|
|
|
|
emit enterPressed();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2019-04-09 22:01:25 +00:00
|
|
|
}
|
2021-10-06 15:09:18 +00:00
|
|
|
if (k == Qt::Key_V && key->modifiers() & Qt::CTRL) {
|
|
|
|
if (Conversation::checkClipboardImage()) {
|
|
|
|
emit imagePasted();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2019-04-09 22:01:25 +00:00
|
|
|
}
|
2019-04-10 15:22:52 +00:00
|
|
|
return QObject::eventFilter(obj, event);
|
2019-04-09 22:01:25 +00:00
|
|
|
}
|
|
|
|
|
2021-10-06 15:09:18 +00:00
|
|
|
bool Conversation::checkClipboardImage() {
|
|
|
|
return !QApplication::clipboard()->image().isNull();
|
|
|
|
}
|
|
|
|
|
2019-04-12 15:22:10 +00:00
|
|
|
QString Conversation::getPalResource() const
|
|
|
|
{
|
|
|
|
return activePalResource;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Conversation::setPalResource(const QString& res)
|
|
|
|
{
|
|
|
|
activePalResource = res;
|
|
|
|
}
|
|
|
|
|
2022-03-28 20:25:33 +00:00
|
|
|
void Conversation::initiateMessageSending()
|
2019-04-09 22:01:25 +00:00
|
|
|
{
|
2019-04-11 14:58:59 +00:00
|
|
|
QString body(m_ui->messageEditor->toPlainText());
|
2019-04-12 15:22:10 +00:00
|
|
|
|
2019-11-14 11:43:43 +00:00
|
|
|
if (body.size() > 0) {
|
2020-04-15 13:48:49 +00:00
|
|
|
Shared::Message msg = createMessage();
|
|
|
|
msg.setBody(body);
|
2022-03-28 20:25:33 +00:00
|
|
|
initiateMessageSending(msg);
|
2019-11-14 11:43:43 +00:00
|
|
|
}
|
2019-11-12 13:38:01 +00:00
|
|
|
if (filesToAttach.size() > 0) {
|
2021-02-07 17:02:11 +00:00
|
|
|
for (Badge* badge : filesToAttach) {
|
|
|
|
Shared::Message msg = createMessage();
|
|
|
|
msg.setAttachPath(badge->id);
|
2021-04-23 11:53:48 +00:00
|
|
|
element->feed->registerUpload(msg.getId());
|
2022-03-28 20:25:33 +00:00
|
|
|
initiateMessageSending(msg);
|
2021-02-07 17:02:11 +00:00
|
|
|
}
|
2022-03-28 20:25:33 +00:00
|
|
|
}
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Conversation::initiateMessageSending(const Shared::Message& msg)
|
|
|
|
{
|
|
|
|
if (currentAction == CurrentAction::edit) {
|
|
|
|
emit replaceMessage(currentMessageId, msg);
|
|
|
|
currentAction = CurrentAction::none;
|
|
|
|
} else {
|
|
|
|
emit sendMessage(msg);
|
2019-04-12 15:22:10 +00:00
|
|
|
}
|
2019-04-09 22:01:25 +00:00
|
|
|
}
|
2019-04-13 20:38:20 +00:00
|
|
|
|
2021-10-06 15:09:18 +00:00
|
|
|
void Conversation::onImagePasted()
|
|
|
|
{
|
|
|
|
QImage image = QApplication::clipboard()->image();
|
|
|
|
if (image.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QTemporaryFile *tempFile = new QTemporaryFile(QDir::tempPath() + QStringLiteral("/squawk_img_attach_XXXXXX.png"), QApplication::instance());
|
|
|
|
tempFile->open();
|
|
|
|
image.save(tempFile, "PNG");
|
|
|
|
tempFile->close();
|
|
|
|
qDebug() << "image on paste temp file: " << tempFile->fileName();
|
|
|
|
addAttachedFile(tempFile->fileName());
|
2021-10-15 16:20:31 +00:00
|
|
|
|
|
|
|
// The file, if successfully uploaded, will be copied to Download folder.
|
|
|
|
// On application closing, this temporary file will be automatically removed by Qt.
|
|
|
|
// See Core::NetworkAccess::onUploadFinished.
|
2021-10-06 15:09:18 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 19:33:38 +00:00
|
|
|
void Conversation::onAttach()
|
|
|
|
{
|
2019-10-23 14:49:56 +00:00
|
|
|
QFileDialog* d = new QFileDialog(this, tr("Chose a file to send"));
|
2019-06-21 19:33:38 +00:00
|
|
|
d->setFileMode(QFileDialog::ExistingFile);
|
|
|
|
|
2019-10-23 14:49:56 +00:00
|
|
|
connect(d, &QFileDialog::accepted, this, &Conversation::onFileSelected);
|
|
|
|
connect(d, &QFileDialog::rejected, d, &QFileDialog::deleteLater);
|
2019-06-21 19:33:38 +00:00
|
|
|
|
|
|
|
d->show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Conversation::onFileSelected()
|
|
|
|
{
|
|
|
|
QFileDialog* d = static_cast<QFileDialog*>(sender());
|
|
|
|
|
2019-09-19 14:31:27 +00:00
|
|
|
for (const QString& path : d->selectedFiles()) {
|
|
|
|
addAttachedFile(path);
|
|
|
|
}
|
2019-06-21 19:33:38 +00:00
|
|
|
|
|
|
|
d->deleteLater();
|
|
|
|
}
|
2019-09-03 20:28:58 +00:00
|
|
|
|
|
|
|
void Conversation::setStatus(const QString& status)
|
|
|
|
{
|
2020-04-12 15:55:05 +00:00
|
|
|
statusLabel->setText(Shared::processMessageBody(status));
|
2019-09-03 20:28:58 +00:00
|
|
|
}
|
2019-09-10 14:33:39 +00:00
|
|
|
|
2020-04-11 20:00:15 +00:00
|
|
|
Models::Roster::ElId Conversation::getId() const
|
|
|
|
{
|
|
|
|
return {getAccount(), getJid()};
|
|
|
|
}
|
|
|
|
|
2019-09-19 14:31:27 +00:00
|
|
|
void Conversation::addAttachedFile(const QString& path)
|
|
|
|
{
|
|
|
|
QMimeDatabase db;
|
|
|
|
QMimeType type = db.mimeTypeForFile(path);
|
|
|
|
QFileInfo info(path);
|
2021-10-06 14:45:10 +00:00
|
|
|
|
|
|
|
QIcon fileIcon = QIcon::fromTheme(type.iconName());
|
|
|
|
if (fileIcon.isNull()) {
|
|
|
|
fileIcon.addFile(QString::fromUtf8(":/images/fallback/dark/big/mail-attachment.svg"), QSize(), QIcon::Normal, QIcon::Off);
|
|
|
|
}
|
|
|
|
Badge* badge = new Badge(path, info.fileName(), fileIcon);
|
2019-09-19 14:31:27 +00:00
|
|
|
|
2019-11-03 18:46:40 +00:00
|
|
|
connect(badge, &Badge::close, this, &Conversation::onBadgeClose);
|
2020-04-14 16:30:33 +00:00
|
|
|
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;
|
2019-09-19 14:31:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Conversation::removeAttachedFile(Badge* badge)
|
|
|
|
{
|
|
|
|
W::Order<Badge*, Badge::Comparator>::const_iterator itr = filesToAttach.find(badge);
|
|
|
|
if (itr != filesToAttach.end()) {
|
|
|
|
filesToAttach.erase(badge);
|
|
|
|
if (filesLayout->count() == 1) {
|
|
|
|
filesLayout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
badge->deleteLater();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Conversation::onBadgeClose()
|
|
|
|
{
|
|
|
|
Badge* badge = static_cast<Badge*>(sender());
|
|
|
|
removeAttachedFile(badge);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Conversation::clearAttachedFiles()
|
|
|
|
{
|
|
|
|
for (Badge* badge : filesToAttach) {
|
|
|
|
badge->deleteLater();
|
|
|
|
}
|
|
|
|
filesToAttach.clear();
|
|
|
|
filesLayout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
2022-03-28 20:25:33 +00:00
|
|
|
void Conversation::clear()
|
2019-11-14 11:43:43 +00:00
|
|
|
{
|
2022-03-28 20:25:33 +00:00
|
|
|
currentMessageId.clear();
|
|
|
|
currentAction = CurrentAction::none;
|
|
|
|
m_ui->currentActionBadge->setVisible(false);
|
2019-11-14 11:43:43 +00:00
|
|
|
clearAttachedFiles();
|
|
|
|
m_ui->messageEditor->clear();
|
|
|
|
}
|
|
|
|
|
2019-12-23 06:28:23 +00:00
|
|
|
void Conversation::setAvatar(const QString& path)
|
|
|
|
{
|
2022-01-08 22:28:29 +00:00
|
|
|
QPixmap pixmap;
|
2019-12-24 08:42:46 +00:00
|
|
|
if (path.size() == 0) {
|
2022-01-08 22:28:29 +00:00
|
|
|
pixmap = Shared::icon("user", true).pixmap(avatarSize);
|
2019-12-24 08:42:46 +00:00
|
|
|
} else {
|
2022-01-08 22:28:29 +00:00
|
|
|
pixmap = QPixmap(path).scaled(avatarSize);
|
2019-12-24 08:42:46 +00:00
|
|
|
}
|
2022-01-08 22:28:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
QPixmap result(avatarSize);
|
|
|
|
result.fill(Qt::transparent);
|
|
|
|
QPainter painter(&result);
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
|
|
|
painter.setRenderHint(QPainter::SmoothPixmapTransform);
|
|
|
|
QPainterPath maskPath;
|
|
|
|
maskPath.addEllipse(0, 0, avatarSize.width(), avatarSize.height());
|
|
|
|
painter.setClipPath(maskPath);
|
|
|
|
painter.drawPixmap(0, 0, pixmap);
|
|
|
|
|
|
|
|
m_ui->avatar->setPixmap(result);
|
2019-12-23 06:28:23 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 21:17:10 +00:00
|
|
|
void Conversation::onTextEditDocSizeChanged(const QSizeF& size)
|
2019-11-14 11:43:43 +00:00
|
|
|
{
|
2020-03-30 21:17:10 +00:00
|
|
|
m_ui->messageEditor->setMaximumHeight(int(size.height()));
|
2019-11-14 11:43:43 +00:00
|
|
|
}
|
|
|
|
|
2020-04-11 20:00:15 +00:00
|
|
|
void Conversation::setFeedFrames(bool top, bool right, bool bottom, bool left)
|
|
|
|
{
|
2021-05-03 00:35:43 +00:00
|
|
|
shadow.setFrames(top, right, bottom, left);
|
2020-04-11 20:00:15 +00:00
|
|
|
}
|
2019-11-14 11:43:43 +00:00
|
|
|
|
2020-04-14 16:30:33 +00:00
|
|
|
void Conversation::dragEnterEvent(QDragEnterEvent* event)
|
|
|
|
{
|
|
|
|
bool accept = false;
|
|
|
|
if (event->mimeData()->hasUrls()) {
|
|
|
|
QList<QUrl> list = event->mimeData()->urls();
|
|
|
|
for (const QUrl& url : list) {
|
|
|
|
if (url.isLocalFile()) {
|
|
|
|
QFileInfo info(url.toLocalFile());
|
|
|
|
if (info.isReadable() && info.isFile()) {
|
|
|
|
accept = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (accept) {
|
|
|
|
event->acceptProposedAction();
|
|
|
|
overlay->show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Conversation::dragLeaveEvent(QDragLeaveEvent* event)
|
|
|
|
{
|
|
|
|
overlay->hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Conversation::dropEvent(QDropEvent* event)
|
|
|
|
{
|
|
|
|
bool accept = false;
|
|
|
|
if (event->mimeData()->hasUrls()) {
|
|
|
|
QList<QUrl> list = event->mimeData()->urls();
|
|
|
|
for (const QUrl& url : list) {
|
|
|
|
if (url.isLocalFile()) {
|
|
|
|
QFileInfo info(url.toLocalFile());
|
|
|
|
if (info.isReadable() && info.isFile()) {
|
|
|
|
addAttachedFile(info.canonicalFilePath());
|
|
|
|
accept = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (accept) {
|
|
|
|
event->acceptProposedAction();
|
|
|
|
}
|
|
|
|
overlay->hide();
|
|
|
|
}
|
|
|
|
|
2020-04-15 13:48:49 +00:00
|
|
|
Shared::Message Conversation::createMessage() const
|
|
|
|
{
|
|
|
|
Shared::Message msg;
|
|
|
|
msg.setOutgoing(true);
|
|
|
|
msg.generateRandomId();
|
|
|
|
msg.setCurrentTime();
|
|
|
|
msg.setState(Shared::Message::State::pending);
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2021-04-27 19:29:15 +00:00
|
|
|
void Conversation::onFeedMessage(const Shared::Message& msg)
|
|
|
|
{
|
|
|
|
this->onMessage(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Conversation::onMessage(const Shared::Message& msg)
|
|
|
|
{
|
|
|
|
if (!msg.getForwarded()) {
|
|
|
|
QApplication::alert(this);
|
|
|
|
if (window()->windowState().testFlag(Qt::WindowMinimized)) {
|
|
|
|
emit notifyableMessage(getAccount(), msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-03 00:35:43 +00:00
|
|
|
|
|
|
|
void Conversation::positionShadow()
|
|
|
|
{
|
|
|
|
int w = width();
|
|
|
|
int h = feed->height();
|
|
|
|
|
|
|
|
shadow.resize(w, h);
|
|
|
|
shadow.move(feed->pos());
|
|
|
|
shadow.raise();
|
|
|
|
}
|
2021-05-04 14:09:41 +00:00
|
|
|
|
|
|
|
void Conversation::onFeedContext(const QPoint& pos)
|
|
|
|
{
|
|
|
|
QModelIndex index = feed->indexAt(pos);
|
|
|
|
if (index.isValid()) {
|
|
|
|
Shared::Message* item = static_cast<Shared::Message*>(index.internalPointer());
|
|
|
|
|
|
|
|
contextMenu->clear();
|
2022-03-27 19:05:31 +00:00
|
|
|
QString id = item->getId();
|
2021-05-04 14:09:41 +00:00
|
|
|
bool showMenu = false;
|
2021-05-22 22:03:14 +00:00
|
|
|
if (item->getState() == Shared::Message::State::error) {
|
|
|
|
showMenu = true;
|
|
|
|
QAction* resend = contextMenu->addAction(Shared::icon("view-refresh"), tr("Try sending again"));
|
|
|
|
connect(resend, &QAction::triggered, [this, id]() {
|
|
|
|
element->feed->registerUpload(id);
|
|
|
|
emit resendMessage(id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-18 21:27:09 +00:00
|
|
|
QString path = Shared::resolvePath(item->getAttachPath());
|
2021-05-04 14:09:41 +00:00
|
|
|
if (path.size() > 0) {
|
|
|
|
showMenu = true;
|
2021-05-06 14:44:43 +00:00
|
|
|
QAction* open = contextMenu->addAction(Shared::icon("document-preview"), tr("Open"));
|
2021-05-04 14:09:41 +00:00
|
|
|
connect(open, &QAction::triggered, [path]() {
|
|
|
|
QDesktopServices::openUrl(QUrl::fromLocalFile(path));
|
|
|
|
});
|
|
|
|
|
2021-05-06 14:44:43 +00:00
|
|
|
QAction* show = contextMenu->addAction(Shared::icon("folder"), tr("Show in folder"));
|
2021-05-04 14:09:41 +00:00
|
|
|
connect(show, &QAction::triggered, [path]() {
|
2021-05-06 14:44:43 +00:00
|
|
|
Shared::Global::highlightInFileManager(path);
|
2021-05-04 14:09:41 +00:00
|
|
|
});
|
|
|
|
}
|
2022-03-27 19:05:31 +00:00
|
|
|
|
|
|
|
if (item->getOutgoing()) {
|
|
|
|
showMenu = true;
|
|
|
|
QAction* edit = contextMenu->addAction(Shared::icon("edit-rename"), tr("Edit"));
|
|
|
|
connect(edit, &QAction::triggered, this, std::bind(&Conversation::onMessageEditRequested, this, id));
|
|
|
|
}
|
2021-05-04 14:09:41 +00:00
|
|
|
|
|
|
|
if (showMenu) {
|
|
|
|
contextMenu->popup(feed->viewport()->mapToGlobal(pos));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-13 12:06:13 +00:00
|
|
|
|
|
|
|
void Conversation::onMessageEditorContext(const QPoint& pos)
|
|
|
|
{
|
|
|
|
pasteImageAction->setEnabled(Conversation::checkClipboardImage());
|
|
|
|
|
|
|
|
QMenu *editorMenu = m_ui->messageEditor->createStandardContextMenu();
|
|
|
|
editorMenu->addSeparator();
|
|
|
|
editorMenu->addAction(pasteImageAction);
|
|
|
|
|
|
|
|
editorMenu->exec(this->m_ui->messageEditor->mapToGlobal(pos));
|
|
|
|
}
|
2022-03-27 19:05:31 +00:00
|
|
|
|
|
|
|
void Conversation::onMessageEditRequested(const QString& id)
|
|
|
|
{
|
2022-03-28 20:25:33 +00:00
|
|
|
clear();
|
2022-03-27 19:05:31 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
Shared::Message msg = element->feed->getMessage(id);
|
|
|
|
|
2022-03-28 20:25:33 +00:00
|
|
|
currentMessageId = id;
|
2022-03-27 19:05:31 +00:00
|
|
|
m_ui->currentActionBadge->setVisible(true);
|
|
|
|
m_ui->currentActionBadge->setText(tr("Editing message..."));
|
|
|
|
currentAction = CurrentAction::edit;
|
|
|
|
m_ui->messageEditor->setText(msg.getBody());
|
|
|
|
|
|
|
|
} catch (const Models::MessageFeed::NotFound& e) {
|
|
|
|
qDebug() << "The message requested to be edited was not found" << e.getMessage().c_str();
|
|
|
|
qDebug() << "Ignoring";
|
|
|
|
}
|
|
|
|
}
|