2019-09-10 14:33:39 +00:00
|
|
|
/*
|
|
|
|
* Squawk messenger.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2020-04-03 22:28:15 +00:00
|
|
|
#include "message.h"
|
2019-09-10 14:33:39 +00:00
|
|
|
#include <QDebug>
|
2019-09-17 22:05:32 +00:00
|
|
|
#include <QMimeDatabase>
|
|
|
|
#include <QPixmap>
|
|
|
|
#include <QFileInfo>
|
2019-12-06 08:00:09 +00:00
|
|
|
#include <QRegularExpression>
|
2019-09-10 14:33:39 +00:00
|
|
|
|
2020-02-08 11:44:15 +00:00
|
|
|
Message::Message(const Shared::Message& source, bool p_outgoing, const QString& p_sender, const QString& avatarPath, QWidget* parent):
|
2020-02-04 15:14:51 +00:00
|
|
|
QWidget(parent),
|
2020-02-08 11:44:15 +00:00
|
|
|
outgoing(p_outgoing),
|
2019-09-10 14:33:39 +00:00
|
|
|
msg(source),
|
|
|
|
body(new QWidget()),
|
2020-02-04 15:14:51 +00:00
|
|
|
statusBar(new QWidget()),
|
2019-09-10 14:33:39 +00:00
|
|
|
bodyLayout(new QVBoxLayout(body)),
|
2020-02-04 15:14:51 +00:00
|
|
|
layout(new QHBoxLayout(this)),
|
2019-09-10 14:33:39 +00:00
|
|
|
date(new QLabel(msg.getTime().toLocalTime().toString())),
|
|
|
|
sender(new QLabel(p_sender)),
|
|
|
|
text(new QLabel()),
|
2019-09-12 20:54:44 +00:00
|
|
|
shadow(new QGraphicsDropShadowEffect()),
|
2019-11-12 13:38:01 +00:00
|
|
|
button(0),
|
2019-09-12 20:54:44 +00:00
|
|
|
file(0),
|
|
|
|
progress(0),
|
2019-09-17 22:05:32 +00:00
|
|
|
fileComment(new QLabel()),
|
2020-02-08 11:44:15 +00:00
|
|
|
statusIcon(0),
|
|
|
|
editedLabel(0),
|
2019-12-20 15:41:20 +00:00
|
|
|
avatar(new Image(avatarPath.size() == 0 ? Shared::iconPath("user", true) : avatarPath, 60)),
|
2019-11-12 13:38:01 +00:00
|
|
|
hasButton(false),
|
2019-09-12 20:54:44 +00:00
|
|
|
hasProgress(false),
|
2019-09-17 22:05:32 +00:00
|
|
|
hasFile(false),
|
2020-02-08 11:44:15 +00:00
|
|
|
commentAdded(false),
|
|
|
|
hasStatusIcon(false),
|
|
|
|
hasEditedLabel(false)
|
2019-09-10 14:33:39 +00:00
|
|
|
{
|
2020-02-04 15:14:51 +00:00
|
|
|
setContentsMargins(0, 0, 0, 0);
|
|
|
|
layout->setContentsMargins(10, 5, 10, 5);
|
2019-09-10 14:33:39 +00:00
|
|
|
body->setBackgroundRole(QPalette::AlternateBase);
|
|
|
|
body->setAutoFillBackground(true);
|
|
|
|
|
2020-04-12 15:55:05 +00:00
|
|
|
QString bd = Shared::processMessageBody(msg.getBody());
|
2019-12-06 08:00:09 +00:00
|
|
|
text->setTextFormat(Qt::RichText);
|
2020-04-12 15:55:05 +00:00
|
|
|
text->setText(bd);;
|
2019-09-10 14:33:39 +00:00
|
|
|
text->setTextInteractionFlags(text->textInteractionFlags() | Qt::TextSelectableByMouse | Qt::LinksAccessibleByMouse);
|
|
|
|
text->setWordWrap(true);
|
|
|
|
text->setOpenExternalLinks(true);
|
2019-09-12 20:54:44 +00:00
|
|
|
if (bd.size() == 0) {
|
|
|
|
text->hide();
|
|
|
|
}
|
2019-09-10 14:33:39 +00:00
|
|
|
|
|
|
|
QFont dFont = date->font();
|
|
|
|
dFont.setItalic(true);
|
|
|
|
dFont.setPointSize(dFont.pointSize() - 2);
|
|
|
|
date->setFont(dFont);
|
|
|
|
|
|
|
|
QFont f;
|
|
|
|
f.setBold(true);
|
|
|
|
sender->setFont(f);
|
|
|
|
|
|
|
|
bodyLayout->addWidget(sender);
|
|
|
|
bodyLayout->addWidget(text);
|
|
|
|
|
|
|
|
shadow->setBlurRadius(10);
|
|
|
|
shadow->setXOffset(1);
|
|
|
|
shadow->setYOffset(1);
|
|
|
|
shadow->setColor(Qt::black);
|
|
|
|
body->setGraphicsEffect(shadow);
|
2019-12-20 15:41:20 +00:00
|
|
|
avatar->setMaximumHeight(60);
|
|
|
|
avatar->setMaximumWidth(60);
|
|
|
|
|
2020-02-04 15:14:51 +00:00
|
|
|
statusBar->setContentsMargins(0, 0, 0, 0);
|
|
|
|
QHBoxLayout* statusLay = new QHBoxLayout();
|
|
|
|
statusLay->setContentsMargins(0, 0, 0, 0);
|
|
|
|
statusBar->setLayout(statusLay);
|
2019-09-10 14:33:39 +00:00
|
|
|
|
|
|
|
if (outgoing) {
|
|
|
|
sender->setAlignment(Qt::AlignRight);
|
|
|
|
date->setAlignment(Qt::AlignRight);
|
2020-02-08 11:44:15 +00:00
|
|
|
statusIcon = new QLabel();
|
2020-03-26 15:08:44 +00:00
|
|
|
setState();
|
2020-02-04 15:14:51 +00:00
|
|
|
statusLay->addWidget(statusIcon);
|
|
|
|
statusLay->addWidget(date);
|
|
|
|
layout->addStretch();
|
|
|
|
layout->addWidget(body);
|
|
|
|
layout->addWidget(avatar);
|
2020-02-08 11:44:15 +00:00
|
|
|
hasStatusIcon = true;
|
2019-11-12 13:38:01 +00:00
|
|
|
} else {
|
2020-02-04 15:14:51 +00:00
|
|
|
layout->addWidget(avatar);
|
|
|
|
layout->addWidget(body);
|
|
|
|
layout->addStretch();
|
|
|
|
statusLay->addWidget(date);
|
2019-09-10 14:33:39 +00:00
|
|
|
}
|
2020-03-28 14:05:49 +00:00
|
|
|
if (msg.getEdited()) {
|
|
|
|
setEdited();
|
|
|
|
}
|
2020-02-04 15:14:51 +00:00
|
|
|
|
|
|
|
bodyLayout->addWidget(statusBar);
|
|
|
|
layout->setAlignment(avatar, Qt::AlignTop);
|
2019-09-10 14:33:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Message::~Message()
|
|
|
|
{
|
2019-09-17 22:05:32 +00:00
|
|
|
if (!commentAdded) {
|
|
|
|
delete fileComment;
|
|
|
|
}
|
2020-02-08 11:44:15 +00:00
|
|
|
//delete body; //not sure if I should delete it here, it's probably already owned by the infrastructure and gonna die with the rest of the widget
|
|
|
|
//delete avatar;
|
2019-09-10 14:33:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Message::getId() const
|
|
|
|
{
|
|
|
|
return msg.getId();
|
|
|
|
}
|
|
|
|
|
2020-02-08 11:44:15 +00:00
|
|
|
QString Message::getSenderJid() const
|
|
|
|
{
|
|
|
|
return msg.getFromJid();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Message::getSenderResource() const
|
|
|
|
{
|
|
|
|
return msg.getFromResource();
|
|
|
|
}
|
|
|
|
|
2019-11-11 15:19:54 +00:00
|
|
|
QString Message::getFileUrl() const
|
|
|
|
{
|
|
|
|
return msg.getOutOfBandUrl();
|
|
|
|
}
|
|
|
|
|
2019-09-10 14:33:39 +00:00
|
|
|
void Message::setSender(const QString& p_sender)
|
|
|
|
{
|
|
|
|
sender->setText(p_sender);
|
|
|
|
}
|
2019-09-12 20:54:44 +00:00
|
|
|
|
2019-11-14 11:43:43 +00:00
|
|
|
void Message::addButton(const QIcon& icon, const QString& buttonText, const QString& tooltip)
|
2019-09-12 20:54:44 +00:00
|
|
|
{
|
2019-09-17 22:05:32 +00:00
|
|
|
hideFile();
|
|
|
|
hideProgress();
|
2019-11-12 13:38:01 +00:00
|
|
|
if (!hasButton) {
|
2019-09-17 22:05:32 +00:00
|
|
|
hideComment();
|
2019-09-17 15:16:09 +00:00
|
|
|
if (msg.getBody() == msg.getOutOfBandUrl()) {
|
|
|
|
text->setText("");
|
|
|
|
text->hide();
|
|
|
|
}
|
2019-11-12 13:38:01 +00:00
|
|
|
button = new QPushButton(icon, buttonText);
|
2019-11-14 11:43:43 +00:00
|
|
|
button->setToolTip(tooltip);
|
2019-11-12 13:38:01 +00:00
|
|
|
connect(button, &QPushButton::clicked, this, &Message::buttonClicked);
|
|
|
|
bodyLayout->insertWidget(2, button);
|
|
|
|
hasButton = true;
|
2019-09-12 20:54:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 13:38:01 +00:00
|
|
|
void Message::setProgress(qreal value)
|
2019-09-12 20:54:44 +00:00
|
|
|
{
|
2019-09-17 22:05:32 +00:00
|
|
|
hideFile();
|
2019-11-12 13:38:01 +00:00
|
|
|
hideButton();
|
2019-09-12 20:54:44 +00:00
|
|
|
if (!hasProgress) {
|
2019-09-17 22:05:32 +00:00
|
|
|
hideComment();
|
2019-09-17 15:16:09 +00:00
|
|
|
if (msg.getBody() == msg.getOutOfBandUrl()) {
|
|
|
|
text->setText("");
|
|
|
|
text->hide();
|
|
|
|
}
|
2019-09-17 22:05:32 +00:00
|
|
|
progress = new QProgressBar();
|
|
|
|
progress->setRange(0, 100);
|
2019-09-12 20:54:44 +00:00
|
|
|
bodyLayout->insertWidget(2, progress);
|
|
|
|
hasProgress = true;
|
|
|
|
}
|
2019-09-17 22:05:32 +00:00
|
|
|
progress->setValue(value * 100);
|
2019-09-12 20:54:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Message::showFile(const QString& path)
|
2019-09-17 22:05:32 +00:00
|
|
|
{
|
2019-11-12 13:38:01 +00:00
|
|
|
hideButton();
|
2019-09-17 22:05:32 +00:00
|
|
|
hideProgress();
|
|
|
|
if (!hasFile) {
|
|
|
|
hideComment();
|
|
|
|
if (msg.getBody() == msg.getOutOfBandUrl()) {
|
|
|
|
text->setText("");
|
|
|
|
text->hide();
|
|
|
|
}
|
|
|
|
QMimeDatabase db;
|
|
|
|
QMimeType type = db.mimeTypeForFile(path);
|
|
|
|
QStringList parts = type.name().split("/");
|
|
|
|
QString big = parts.front();
|
|
|
|
QFileInfo info(path);
|
|
|
|
if (big == "image") {
|
|
|
|
file = new Image(path);
|
|
|
|
} else {
|
|
|
|
file = new QLabel();
|
2021-07-07 13:45:08 +00:00
|
|
|
QIcon qIcon = QIcon::fromTheme(type.iconName());
|
|
|
|
if (qIcon.isNull()) {
|
|
|
|
qIcon.addFile(QString::fromUtf8(":/images/fallback/dark/big/mail-attachment.svg"), QSize(), QIcon::Normal, QIcon::Off);
|
|
|
|
}
|
|
|
|
file->setPixmap(qIcon.pixmap(50));
|
2019-09-17 22:05:32 +00:00
|
|
|
file->setAlignment(Qt::AlignCenter);
|
2019-11-12 13:38:01 +00:00
|
|
|
showComment(info.fileName(), true);
|
2019-09-17 22:05:32 +00:00
|
|
|
}
|
|
|
|
file->setContextMenuPolicy(Qt::ActionsContextMenu);
|
2021-07-07 13:45:08 +00:00
|
|
|
QAction* openAction = new QAction(QIcon::fromTheme("document-new-from-template"), tr("Open"), this);
|
2019-09-17 22:05:32 +00:00
|
|
|
connect(openAction, &QAction::triggered, [path]() { //TODO need to get rid of this shame
|
|
|
|
QDesktopServices::openUrl(QUrl::fromLocalFile(path));
|
|
|
|
});
|
|
|
|
file->addAction(openAction);
|
2021-07-07 13:45:08 +00:00
|
|
|
fileComment->setContextMenuPolicy(Qt::ActionsContextMenu);
|
|
|
|
fileComment->addAction(openAction);
|
|
|
|
|
2019-09-17 22:05:32 +00:00
|
|
|
bodyLayout->insertWidget(2, file);
|
|
|
|
hasFile = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Message::hideComment()
|
|
|
|
{
|
|
|
|
if (commentAdded) {
|
|
|
|
bodyLayout->removeWidget(fileComment);
|
|
|
|
fileComment->hide();
|
|
|
|
fileComment->setWordWrap(false);
|
2020-01-16 08:52:54 +00:00
|
|
|
commentAdded = false;
|
2019-09-17 22:05:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 13:38:01 +00:00
|
|
|
void Message::hideButton()
|
2019-09-12 20:54:44 +00:00
|
|
|
{
|
2019-11-12 13:38:01 +00:00
|
|
|
if (hasButton) {
|
|
|
|
button->deleteLater();
|
|
|
|
button = 0;
|
|
|
|
hasButton = false;
|
2019-09-12 20:54:44 +00:00
|
|
|
}
|
2019-09-17 22:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Message::hideFile()
|
|
|
|
{
|
|
|
|
if (hasFile) {
|
|
|
|
file->deleteLater();
|
|
|
|
file = 0;
|
|
|
|
hasFile = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Message::hideProgress()
|
|
|
|
{
|
2019-09-12 20:54:44 +00:00
|
|
|
if (hasProgress) {
|
|
|
|
progress->deleteLater();
|
|
|
|
progress = 0;
|
2019-09-17 22:05:32 +00:00
|
|
|
hasProgress = false;;
|
2019-09-12 20:54:44 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-12 13:38:01 +00:00
|
|
|
void Message::showComment(const QString& comment, bool wordWrap)
|
|
|
|
{
|
|
|
|
if (!commentAdded) {
|
|
|
|
int index = 2;
|
|
|
|
if (hasFile) {
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
if (hasButton) {
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
if (hasProgress) {
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
bodyLayout->insertWidget(index, fileComment);
|
|
|
|
fileComment->show();
|
|
|
|
commentAdded = true;
|
|
|
|
}
|
|
|
|
fileComment->setWordWrap(wordWrap);
|
|
|
|
fileComment->setText(comment);
|
|
|
|
}
|
|
|
|
|
|
|
|
const Shared::Message & Message::getMessage() const
|
|
|
|
{
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2019-12-20 15:41:20 +00:00
|
|
|
void Message::setAvatarPath(const QString& p_path)
|
|
|
|
{
|
2019-12-23 06:28:23 +00:00
|
|
|
if (p_path.size() == 0) {
|
|
|
|
avatar->setPath(Shared::iconPath("user", true));
|
|
|
|
} else {
|
|
|
|
avatar->setPath(p_path);
|
|
|
|
}
|
2019-12-20 15:41:20 +00:00
|
|
|
}
|
2020-02-08 11:44:15 +00:00
|
|
|
|
|
|
|
bool Message::change(const QMap<QString, QVariant>& data)
|
|
|
|
{
|
|
|
|
bool idChanged = msg.change(data);
|
|
|
|
|
2020-04-12 15:55:05 +00:00
|
|
|
QString body = msg.getBody();
|
|
|
|
QString bd = Shared::processMessageBody(body);
|
|
|
|
if (body.size() > 0) {
|
|
|
|
text->setText(bd);
|
2020-02-08 11:44:15 +00:00
|
|
|
text->show();
|
|
|
|
} else {
|
2020-04-12 15:55:05 +00:00
|
|
|
text->setText(body);
|
2020-02-08 11:44:15 +00:00
|
|
|
text->hide();
|
|
|
|
}
|
|
|
|
if (msg.getEdited()) {
|
2020-03-28 14:05:49 +00:00
|
|
|
setEdited();
|
2020-02-08 11:44:15 +00:00
|
|
|
}
|
|
|
|
if (hasStatusIcon) {
|
2020-03-26 15:08:44 +00:00
|
|
|
setState();
|
2020-02-08 11:44:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return idChanged;
|
|
|
|
}
|
2020-03-26 15:08:44 +00:00
|
|
|
|
2020-03-28 14:05:49 +00:00
|
|
|
void Message::setEdited()
|
|
|
|
{
|
|
|
|
if (!hasEditedLabel) {
|
|
|
|
editedLabel = new QLabel();
|
|
|
|
hasEditedLabel = true;
|
|
|
|
QIcon q(Shared::icon("edit-rename"));
|
|
|
|
editedLabel->setPixmap(q.pixmap(12, 12));
|
|
|
|
QHBoxLayout* statusLay = static_cast<QHBoxLayout*>(statusBar->layout());
|
|
|
|
statusLay->insertWidget(1, editedLabel);
|
|
|
|
}
|
|
|
|
editedLabel->setToolTip("Last time edited: " + msg.getLastModified().toLocalTime().toString()
|
|
|
|
+ "\nOriginal message: " + msg.getOriginalBody());
|
|
|
|
}
|
|
|
|
|
2020-03-26 15:08:44 +00:00
|
|
|
void Message::setState()
|
|
|
|
{
|
|
|
|
Shared::Message::State state = msg.getState();
|
|
|
|
QIcon q(Shared::icon(Shared::messageStateThemeIcons[static_cast<uint8_t>(state)]));
|
2020-04-03 22:28:15 +00:00
|
|
|
QString tt = Shared::Global::getName(state);
|
2020-03-26 15:08:44 +00:00
|
|
|
if (state == Shared::Message::State::error) {
|
|
|
|
QString errText = msg.getErrorText();
|
|
|
|
if (errText.size() > 0) {
|
|
|
|
tt += ": " + errText;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
statusIcon->setToolTip(tt);
|
|
|
|
statusIcon->setPixmap(q.pixmap(12, 12));
|
|
|
|
}
|
|
|
|
|