matrixion/src/timeline/TimelineItem.cpp

855 lines
28 KiB
C++
Raw Normal View History

2017-04-06 02:06:42 +03:00
/*
* nheko Copyright (C) 2017 Konstantinos Sideris <siderisk@auth.gr>
*
* 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/>.
*/
2018-01-03 19:05:49 +03:00
#include <QContextMenuEvent>
2018-09-12 20:54:44 +03:00
#include <QDesktopServices>
#include <QFontDatabase>
2018-01-03 19:05:49 +03:00
#include <QMenu>
#include <QTimer>
2017-04-06 02:06:42 +03:00
#include "ChatPage.h"
#include "Config.h"
2018-07-17 16:37:25 +03:00
#include "Logging.h"
2018-07-20 12:02:35 +03:00
#include "MainWindow.h"
2018-07-17 16:37:25 +03:00
#include "Olm.h"
#include "ui/Avatar.h"
#include "ui/Painter.h"
2018-09-26 15:17:14 +03:00
#include "ui/TextLabel.h"
2017-11-30 14:53:28 +03:00
#include "timeline/TimelineItem.h"
#include "timeline/widgets/AudioItem.h"
2017-11-30 14:53:28 +03:00
#include "timeline/widgets/FileItem.h"
#include "timeline/widgets/ImageItem.h"
#include "timeline/widgets/VideoItem.h"
2017-04-06 02:06:42 +03:00
#include "dialogs/RawMessage.h"
2018-09-12 20:54:44 +03:00
#include "mtx/identifiers.hpp"
constexpr int MSG_RIGHT_MARGIN = 7;
constexpr int MSG_PADDING = 20;
StatusIndicator::StatusIndicator(QWidget *parent)
: QWidget(parent)
{
lockIcon_.addFile(":/icons/icons/ui/lock.png");
clockIcon_.addFile(":/icons/icons/ui/clock.png");
checkmarkIcon_.addFile(":/icons/icons/ui/checkmark.png");
doubleCheckmarkIcon_.addFile(":/icons/icons/ui/double-tick-indicator.png");
}
void
StatusIndicator::paintIcon(QPainter &p, QIcon &icon)
{
auto pixmap = icon.pixmap(width());
QPainter painter(&pixmap);
painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
painter.fillRect(pixmap.rect(), p.pen().color());
QIcon(pixmap).paint(&p, rect(), Qt::AlignCenter, QIcon::Normal);
}
void
StatusIndicator::paintEvent(QPaintEvent *)
{
if (state_ == StatusIndicatorState::Empty)
return;
Painter p(this);
PainterHighQualityEnabler hq(p);
p.setPen(iconColor_);
switch (state_) {
case StatusIndicatorState::Sent: {
paintIcon(p, clockIcon_);
break;
}
case StatusIndicatorState::Encrypted:
paintIcon(p, lockIcon_);
break;
case StatusIndicatorState::Received: {
paintIcon(p, checkmarkIcon_);
break;
}
case StatusIndicatorState::Read: {
paintIcon(p, doubleCheckmarkIcon_);
break;
}
case StatusIndicatorState::Empty:
break;
}
}
void
StatusIndicator::setState(StatusIndicatorState state)
{
state_ = state;
switch (state) {
case StatusIndicatorState::Encrypted:
setToolTip(tr("Encrypted"));
break;
case StatusIndicatorState::Received:
setToolTip(tr("Delivered"));
break;
case StatusIndicatorState::Read:
setToolTip(tr("Seen"));
break;
case StatusIndicatorState::Sent:
setToolTip(tr("Sent"));
break;
case StatusIndicatorState::Empty:
setToolTip("");
break;
}
update();
}
void
TimelineItem::adjustMessageLayoutForWidget()
{
messageLayout_->addLayout(widgetLayout_, 1);
messageLayout_->addWidget(statusIndicator_);
messageLayout_->addWidget(timestamp_);
messageLayout_->setAlignment(statusIndicator_, Qt::AlignTop);
messageLayout_->setAlignment(timestamp_, Qt::AlignTop);
mainLayout_->addLayout(messageLayout_);
}
void
TimelineItem::adjustMessageLayout()
{
messageLayout_->addWidget(body_, 1);
messageLayout_->addWidget(statusIndicator_);
messageLayout_->addWidget(timestamp_);
messageLayout_->setAlignment(statusIndicator_, Qt::AlignTop);
messageLayout_->setAlignment(timestamp_, Qt::AlignTop);
mainLayout_->addLayout(messageLayout_);
}
2017-08-20 13:47:22 +03:00
void
TimelineItem::init()
{
2017-08-26 11:33:26 +03:00
userAvatar_ = nullptr;
timestamp_ = nullptr;
userName_ = nullptr;
body_ = nullptr;
2018-03-17 22:23:46 +03:00
usernameFont_ = font_;
2018-09-30 13:24:36 +03:00
usernameFont_.setWeight(QFont::Medium);
2017-08-26 11:33:26 +03:00
QFontMetrics fm(font_);
contextMenu_ = new QMenu(this);
2018-01-03 19:05:49 +03:00
showReadReceipts_ = new QAction("Read receipts", this);
markAsRead_ = new QAction("Mark as read", this);
viewRawMessage_ = new QAction("View raw message", this);
2018-03-17 22:23:46 +03:00
redactMsg_ = new QAction("Redact message", this);
contextMenu_->addAction(showReadReceipts_);
contextMenu_->addAction(viewRawMessage_);
contextMenu_->addAction(markAsRead_);
2018-03-17 22:23:46 +03:00
contextMenu_->addAction(redactMsg_);
connect(showReadReceipts_, &QAction::triggered, this, [this]() {
2018-01-03 19:05:49 +03:00
if (!event_id_.isEmpty())
2018-08-11 13:50:56 +03:00
MainWindow::instance()->openReadReceiptsDialog(event_id_);
2018-01-03 19:05:49 +03:00
});
connect(this, &TimelineItem::eventRedacted, this, [this](const QString &event_id) {
emit ChatPage::instance()->removeTimelineEvent(room_id_, event_id);
});
connect(this, &TimelineItem::redactionFailed, this, [](const QString &msg) {
emit ChatPage::instance()->showNotification(msg);
});
2018-03-17 22:23:46 +03:00
connect(redactMsg_, &QAction::triggered, this, [this]() {
if (!event_id_.isEmpty())
http::client()->redact_event(
room_id_.toStdString(),
event_id_.toStdString(),
[this](const mtx::responses::EventId &, mtx::http::RequestErr err) {
if (err) {
emit redactionFailed(tr("Message redaction failed: %1")
.arg(QString::fromStdString(
err->matrix_error.error)));
return;
}
emit eventRedacted(event_id_);
});
2018-03-17 22:23:46 +03:00
});
connect(markAsRead_, &QAction::triggered, this, &TimelineItem::sendReadReceipt);
connect(viewRawMessage_, &QAction::triggered, this, &TimelineItem::openRawMessageViewer);
2018-01-14 13:29:54 +03:00
topLayout_ = new QHBoxLayout(this);
mainLayout_ = new QVBoxLayout;
messageLayout_ = new QHBoxLayout;
messageLayout_->setContentsMargins(0, 0, MSG_RIGHT_MARGIN, 0);
messageLayout_->setSpacing(MSG_PADDING);
topLayout_->setContentsMargins(
conf::timeline::msgLeftMargin, conf::timeline::msgTopMargin, 0, 0);
2017-08-26 11:33:26 +03:00
topLayout_->setSpacing(0);
topLayout_->addLayout(mainLayout_);
2017-08-26 11:33:26 +03:00
mainLayout_->setContentsMargins(conf::timeline::headerLeftMargin, 0, 0, 0);
mainLayout_->setSpacing(0);
2018-09-30 13:24:36 +03:00
timestampFont_.setPointSizeF(timestampFont_.pointSizeF() * 0.9);
timestampFont_.setFamily("Monospace");
timestampFont_.setStyleHint(QFont::Monospace);
QFontMetrics tsFm(timestampFont_);
2018-02-07 07:52:21 +03:00
statusIndicator_ = new StatusIndicator(this);
statusIndicator_->setFixedWidth(tsFm.height() - tsFm.leading());
statusIndicator_->setFixedHeight(tsFm.height() - tsFm.leading());
parentWidget()->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
}
2017-08-20 13:47:22 +03:00
/*
* For messages created locally.
*/
TimelineItem::TimelineItem(mtx::events::MessageType ty,
const QString &userid,
QString body,
bool withSender,
2018-04-21 16:34:50 +03:00
const QString &room_id,
QWidget *parent)
2017-08-20 13:47:22 +03:00
: QWidget(parent)
2018-04-21 16:34:50 +03:00
, room_id_{room_id}
2017-04-06 02:06:42 +03:00
{
2017-08-26 11:33:26 +03:00
init();
2018-06-30 15:13:15 +03:00
addReplyAction();
2018-04-21 16:34:50 +03:00
auto displayName = Cache::displayName(room_id_, userid);
auto timestamp = QDateTime::currentDateTime();
2017-05-11 01:28:06 +03:00
2018-09-13 11:02:54 +03:00
// Generate the html body to be rendered.
2018-09-12 21:46:33 +03:00
auto formatted_body = utils::markdownToHtml(body);
2018-09-13 11:02:54 +03:00
// Escape html if the input is not formatted.
if (formatted_body == body.trimmed().toHtmlEscaped())
formatted_body = body.toHtmlEscaped();
2018-09-12 21:46:33 +03:00
QString emptyEventId;
if (ty == mtx::events::MessageType::Emote) {
2018-09-12 21:46:33 +03:00
formatted_body = QString("<em>%1</em>").arg(formatted_body);
descriptionMsg_ = {emptyEventId,
"",
2018-09-12 21:46:33 +03:00
userid,
QString("* %1 %2").arg(displayName).arg(body),
utils::descriptiveTime(timestamp),
timestamp};
} else {
descriptionMsg_ = {emptyEventId,
"You: ",
userid,
body,
utils::descriptiveTime(timestamp),
timestamp};
}
2018-09-12 21:46:33 +03:00
formatted_body = utils::linkifyMessage(formatted_body);
generateTimestamp(timestamp);
if (withSender) {
2018-09-12 21:46:33 +03:00
generateBody(userid, displayName, formatted_body);
setupAvatarLayout(displayName);
2018-01-14 13:29:54 +03:00
2018-03-25 15:59:47 +03:00
AvatarProvider::resolve(
2018-04-21 16:34:50 +03:00
room_id_, userid, this, [this](const QImage &img) { setUserAvatar(img); });
} else {
2018-09-12 21:46:33 +03:00
generateBody(formatted_body);
setupSimpleLayout();
}
adjustMessageLayout();
}
2017-04-06 02:06:42 +03:00
2017-09-10 12:58:00 +03:00
TimelineItem::TimelineItem(ImageItem *image,
const QString &userid,
bool withSender,
2018-04-21 16:34:50 +03:00
const QString &room_id,
2017-09-10 12:58:00 +03:00
QWidget *parent)
2017-11-06 00:04:55 +03:00
: QWidget{parent}
2018-04-21 16:34:50 +03:00
, room_id_{room_id}
2017-09-10 12:58:00 +03:00
{
init();
2018-04-29 15:42:40 +03:00
setupLocalWidgetLayout<ImageItem>(image, userid, withSender);
addSaveImageAction(image);
2017-11-30 00:39:35 +03:00
}
2017-09-10 12:58:00 +03:00
2018-04-21 16:34:50 +03:00
TimelineItem::TimelineItem(FileItem *file,
const QString &userid,
bool withSender,
const QString &room_id,
QWidget *parent)
2017-11-30 00:39:35 +03:00
: QWidget{parent}
2018-04-21 16:34:50 +03:00
, room_id_{room_id}
2017-11-30 00:39:35 +03:00
{
init();
2017-09-10 12:58:00 +03:00
2018-04-29 15:42:40 +03:00
setupLocalWidgetLayout<FileItem>(file, userid, withSender);
2017-09-10 12:58:00 +03:00
}
TimelineItem::TimelineItem(AudioItem *audio,
const QString &userid,
bool withSender,
2018-04-21 16:34:50 +03:00
const QString &room_id,
QWidget *parent)
: QWidget{parent}
2018-04-21 16:34:50 +03:00
, room_id_{room_id}
{
init();
2018-04-29 15:42:40 +03:00
setupLocalWidgetLayout<AudioItem>(audio, userid, withSender);
}
TimelineItem::TimelineItem(VideoItem *video,
const QString &userid,
bool withSender,
2018-04-21 16:34:50 +03:00
const QString &room_id,
QWidget *parent)
: QWidget{parent}
2018-04-21 16:34:50 +03:00
, room_id_{room_id}
{
init();
2018-04-29 15:42:40 +03:00
setupLocalWidgetLayout<VideoItem>(video, userid, withSender);
}
TimelineItem::TimelineItem(ImageItem *image,
const mtx::events::RoomEvent<mtx::events::msg::Image> &event,
2017-08-26 11:33:26 +03:00
bool with_sender,
2018-04-21 16:34:50 +03:00
const QString &room_id,
2017-08-26 11:33:26 +03:00
QWidget *parent)
2017-08-20 13:47:22 +03:00
: QWidget(parent)
2018-04-21 16:34:50 +03:00
, room_id_{room_id}
2017-04-28 14:56:45 +03:00
{
setupWidgetLayout<mtx::events::RoomEvent<mtx::events::msg::Image>, ImageItem>(
2018-04-29 15:42:40 +03:00
image, event, with_sender);
markOwnMessagesAsReceived(event.sender);
addSaveImageAction(image);
2017-04-28 14:56:45 +03:00
}
TimelineItem::TimelineItem(StickerItem *image,
const mtx::events::Sticker &event,
bool with_sender,
const QString &room_id,
QWidget *parent)
: QWidget(parent)
, room_id_{room_id}
{
2018-04-29 15:42:40 +03:00
setupWidgetLayout<mtx::events::Sticker, StickerItem>(image, event, with_sender);
markOwnMessagesAsReceived(event.sender);
addSaveImageAction(image);
}
TimelineItem::TimelineItem(FileItem *file,
const mtx::events::RoomEvent<mtx::events::msg::File> &event,
bool with_sender,
2018-04-21 16:34:50 +03:00
const QString &room_id,
QWidget *parent)
: QWidget(parent)
2018-04-21 16:34:50 +03:00
, room_id_{room_id}
{
setupWidgetLayout<mtx::events::RoomEvent<mtx::events::msg::File>, FileItem>(
2018-04-29 15:42:40 +03:00
file, event, with_sender);
markOwnMessagesAsReceived(event.sender);
}
TimelineItem::TimelineItem(AudioItem *audio,
const mtx::events::RoomEvent<mtx::events::msg::Audio> &event,
bool with_sender,
2018-04-21 16:34:50 +03:00
const QString &room_id,
QWidget *parent)
: QWidget(parent)
2018-04-21 16:34:50 +03:00
, room_id_{room_id}
{
setupWidgetLayout<mtx::events::RoomEvent<mtx::events::msg::Audio>, AudioItem>(
2018-04-29 15:42:40 +03:00
audio, event, with_sender);
markOwnMessagesAsReceived(event.sender);
}
TimelineItem::TimelineItem(VideoItem *video,
const mtx::events::RoomEvent<mtx::events::msg::Video> &event,
bool with_sender,
2018-04-21 16:34:50 +03:00
const QString &room_id,
QWidget *parent)
: QWidget(parent)
2018-04-21 16:34:50 +03:00
, room_id_{room_id}
{
setupWidgetLayout<mtx::events::RoomEvent<mtx::events::msg::Video>, VideoItem>(
2018-04-29 15:42:40 +03:00
video, event, with_sender);
markOwnMessagesAsReceived(event.sender);
}
/*
* Used to display remote notice messages.
*/
TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Notice> &event,
2017-08-26 11:33:26 +03:00
bool with_sender,
2018-04-21 16:34:50 +03:00
const QString &room_id,
2017-08-26 11:33:26 +03:00
QWidget *parent)
2017-08-20 13:47:22 +03:00
: QWidget(parent)
2018-04-21 16:34:50 +03:00
, room_id_{room_id}
{
2017-08-26 11:33:26 +03:00
init();
2018-06-30 15:13:15 +03:00
addReplyAction();
markOwnMessagesAsReceived(event.sender);
event_id_ = QString::fromStdString(event.event_id);
const auto sender = QString::fromStdString(event.sender);
const auto timestamp = QDateTime::fromMSecsSinceEpoch(event.origin_server_ts);
auto formatted_body = utils::linkifyMessage(utils::getMessageBody(event).trimmed());
auto body = QString::fromStdString(event.content.body).trimmed().toHtmlEscaped();
descriptionMsg_ = {event_id_,
Cache::displayName(room_id_, sender),
sender,
2017-11-06 00:04:55 +03:00
" sent a notification",
utils::descriptiveTime(timestamp),
timestamp};
2017-08-26 11:33:26 +03:00
generateTimestamp(timestamp);
2017-04-06 02:06:42 +03:00
2017-08-26 11:33:26 +03:00
if (with_sender) {
2018-04-21 16:34:50 +03:00
auto displayName = Cache::displayName(room_id_, sender);
generateBody(sender, displayName, formatted_body);
2017-08-26 11:33:26 +03:00
setupAvatarLayout(displayName);
2018-03-25 15:59:47 +03:00
AvatarProvider::resolve(
2018-04-21 16:34:50 +03:00
room_id_, sender, this, [this](const QImage &img) { setUserAvatar(img); });
2017-08-26 11:33:26 +03:00
} else {
generateBody(formatted_body);
2017-08-26 11:33:26 +03:00
setupSimpleLayout();
}
adjustMessageLayout();
}
/*
* Used to display remote emote messages.
*/
TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Emote> &event,
bool with_sender,
2018-04-21 16:34:50 +03:00
const QString &room_id,
QWidget *parent)
: QWidget(parent)
2018-04-21 16:34:50 +03:00
, room_id_{room_id}
{
init();
2018-06-30 15:13:15 +03:00
addReplyAction();
markOwnMessagesAsReceived(event.sender);
event_id_ = QString::fromStdString(event.event_id);
const auto sender = QString::fromStdString(event.sender);
auto formatted_body = utils::linkifyMessage(utils::getMessageBody(event).trimmed());
auto body = QString::fromStdString(event.content.body).trimmed().toHtmlEscaped();
auto timestamp = QDateTime::fromMSecsSinceEpoch(event.origin_server_ts);
2018-04-21 16:34:50 +03:00
auto displayName = Cache::displayName(room_id_, sender);
2018-09-12 21:46:33 +03:00
formatted_body = QString("<em>%1</em>").arg(formatted_body);
descriptionMsg_ = {event_id_,
"",
2018-09-12 21:46:33 +03:00
sender,
QString("* %1 %2").arg(displayName).arg(body),
utils::descriptiveTime(timestamp),
timestamp};
generateTimestamp(timestamp);
if (with_sender) {
generateBody(sender, displayName, formatted_body);
setupAvatarLayout(displayName);
2018-01-14 13:29:54 +03:00
2018-03-25 15:59:47 +03:00
AvatarProvider::resolve(
2018-04-21 16:34:50 +03:00
room_id_, sender, this, [this](const QImage &img) { setUserAvatar(img); });
} else {
generateBody(formatted_body);
setupSimpleLayout();
}
adjustMessageLayout();
}
/*
* Used to display remote text messages.
*/
TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Text> &event,
2017-08-26 11:33:26 +03:00
bool with_sender,
2018-04-21 16:34:50 +03:00
const QString &room_id,
2017-08-26 11:33:26 +03:00
QWidget *parent)
2017-08-20 13:47:22 +03:00
: QWidget(parent)
2018-04-21 16:34:50 +03:00
, room_id_{room_id}
{
2017-08-26 11:33:26 +03:00
init();
2018-06-30 15:13:15 +03:00
addReplyAction();
markOwnMessagesAsReceived(event.sender);
event_id_ = QString::fromStdString(event.event_id);
const auto sender = QString::fromStdString(event.sender);
auto formatted_body = utils::linkifyMessage(utils::getMessageBody(event).trimmed());
auto body = QString::fromStdString(event.content.body).trimmed().toHtmlEscaped();
auto timestamp = QDateTime::fromMSecsSinceEpoch(event.origin_server_ts);
2018-04-21 16:34:50 +03:00
auto displayName = Cache::displayName(room_id_, sender);
2017-08-26 11:33:26 +03:00
QSettings settings;
descriptionMsg_ = {event_id_,
sender == settings.value("auth/user_id") ? "You" : displayName,
sender,
2017-11-06 00:04:55 +03:00
QString(": %1").arg(body),
utils::descriptiveTime(timestamp),
timestamp};
2017-08-26 11:33:26 +03:00
generateTimestamp(timestamp);
2017-08-26 11:33:26 +03:00
if (with_sender) {
generateBody(sender, displayName, formatted_body);
2017-08-26 11:33:26 +03:00
setupAvatarLayout(displayName);
2018-03-25 15:59:47 +03:00
AvatarProvider::resolve(
2018-04-21 16:34:50 +03:00
room_id_, sender, this, [this](const QImage &img) { setUserAvatar(img); });
2017-08-26 11:33:26 +03:00
} else {
generateBody(formatted_body);
2017-08-26 11:33:26 +03:00
setupSimpleLayout();
}
adjustMessageLayout();
}
void
TimelineItem::markSent()
{
statusIndicator_->setState(StatusIndicatorState::Sent);
}
void
TimelineItem::markOwnMessagesAsReceived(const std::string &sender)
{
QSettings settings;
if (sender == settings.value("auth/user_id").toString().toStdString())
statusIndicator_->setState(StatusIndicatorState::Received);
}
void
TimelineItem::markRead()
{
if (statusIndicator_->state() != StatusIndicatorState::Encrypted)
statusIndicator_->setState(StatusIndicatorState::Read);
}
void
TimelineItem::markReceived(bool isEncrypted)
{
isReceived_ = true;
if (isEncrypted)
statusIndicator_->setState(StatusIndicatorState::Encrypted);
else
statusIndicator_->setState(StatusIndicatorState::Received);
sendReadReceipt();
}
// Only the body is displayed.
2017-08-20 13:47:22 +03:00
void
TimelineItem::generateBody(const QString &body)
{
2018-09-13 11:02:54 +03:00
body_ = new TextLabel(replaceEmoji(body), this);
2017-08-26 11:33:26 +03:00
body_->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextBrowserInteraction);
2018-09-26 15:17:14 +03:00
connect(body_, &TextLabel::userProfileTriggered, this, [](const QString &user_id) {
MainWindow::instance()->openUserProfile(user_id,
ChatPage::instance()->currentRoom());
});
}
// The username/timestamp is displayed along with the message body.
2017-08-20 13:47:22 +03:00
void
TimelineItem::generateBody(const QString &user_id, const QString &displayname, const QString &body)
2018-07-20 12:02:35 +03:00
{
generateUserName(user_id, displayname);
generateBody(body);
}
void
TimelineItem::generateUserName(const QString &user_id, const QString &displayname)
{
auto sender = displayname;
2017-05-08 19:44:01 +03:00
if (displayname.startsWith("@")) {
2017-09-28 22:26:24 +03:00
// TODO: Fix this by using a UserId type.
if (displayname.split(":")[0].split("@").size() > 1)
sender = displayname.split(":")[0].split("@")[1];
2017-09-28 22:26:24 +03:00
}
2017-04-06 02:06:42 +03:00
2018-03-17 22:23:46 +03:00
QFontMetrics fm(usernameFont_);
2017-12-23 15:06:59 +03:00
2017-08-26 11:33:26 +03:00
userName_ = new QLabel(this);
2018-03-17 22:23:46 +03:00
userName_->setFont(usernameFont_);
2017-12-23 15:06:59 +03:00
userName_->setText(fm.elidedText(sender, Qt::ElideRight, 500));
userName_->setToolTip(user_id);
userName_->setToolTipDuration(1500);
userName_->setAttribute(Qt::WA_Hover);
userName_->setAlignment(Qt::AlignLeft | Qt::AlignTop);
userName_->setFixedWidth(QFontMetrics(userName_->font()).width(userName_->text()));
auto filter = new UserProfileFilter(user_id, userName_);
userName_->installEventFilter(filter);
userName_->setCursor(Qt::PointingHandCursor);
connect(filter, &UserProfileFilter::hoverOn, this, [this]() {
QFont f = userName_->font();
f.setUnderline(true);
userName_->setFont(f);
});
connect(filter, &UserProfileFilter::hoverOff, this, [this]() {
QFont f = userName_->font();
f.setUnderline(false);
userName_->setFont(f);
});
2018-07-20 12:02:35 +03:00
connect(filter, &UserProfileFilter::clicked, this, [this, user_id]() {
MainWindow::instance()->openUserProfile(user_id, room_id_);
});
}
2017-04-06 02:06:42 +03:00
2017-08-20 13:47:22 +03:00
void
TimelineItem::generateTimestamp(const QDateTime &time)
{
2017-08-26 11:33:26 +03:00
timestamp_ = new QLabel(this);
2018-09-30 13:24:36 +03:00
timestamp_->setFont(timestampFont_);
2018-01-14 13:29:54 +03:00
timestamp_->setText(
QString("<span style=\"color: #999\"> %1 </span>").arg(time.toString("HH:mm")));
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
QString
TimelineItem::replaceEmoji(const QString &body)
2017-04-19 19:38:39 +03:00
{
2017-08-26 11:33:26 +03:00
QString fmtBody = "";
QVector<uint> utf32_string = body.toUcs4();
2017-08-26 11:33:26 +03:00
for (auto &code : utf32_string) {
2017-08-26 11:33:26 +03:00
// TODO: Be more precise here.
if (code > 9000)
2018-09-30 13:24:36 +03:00
fmtBody += QString("<span style=\"font-family: Emoji One;\">") +
QString::fromUcs4(&code, 1) + "</span>";
2017-08-26 11:33:26 +03:00
else
fmtBody += QString::fromUcs4(&code, 1);
2017-08-26 11:33:26 +03:00
}
return fmtBody;
2017-04-19 19:38:39 +03:00
}
2017-08-20 13:47:22 +03:00
void
TimelineItem::setupAvatarLayout(const QString &userName)
{
topLayout_->setContentsMargins(
conf::timeline::msgLeftMargin, conf::timeline::msgAvatarTopMargin, 0, 0);
2017-08-26 11:33:26 +03:00
userAvatar_ = new Avatar(this);
userAvatar_->setLetter(QChar(userName[0]).toUpper());
userAvatar_->setSize(conf::timeline::avatarSize);
2017-08-26 11:33:26 +03:00
// TODO: The provided user name should be a UserId class
if (userName[0] == '@' && userName.size() > 1)
userAvatar_->setLetter(QChar(userName[1]).toUpper());
2018-03-17 22:23:46 +03:00
topLayout_->insertWidget(0, userAvatar_);
topLayout_->setAlignment(userAvatar_, Qt::AlignTop | Qt::AlignLeft);
if (userName_)
mainLayout_->insertWidget(0, userName_, Qt::AlignTop | Qt::AlignLeft);
}
2017-08-20 13:47:22 +03:00
void
TimelineItem::setupSimpleLayout()
{
topLayout_->setContentsMargins(conf::timeline::msgLeftMargin + conf::timeline::avatarSize +
2,
conf::timeline::msgTopMargin,
2018-01-14 13:29:54 +03:00
0,
0);
}
2017-08-20 13:47:22 +03:00
void
TimelineItem::setUserAvatar(const QImage &avatar)
{
2017-08-26 11:33:26 +03:00
if (userAvatar_ == nullptr)
return;
2017-08-26 11:33:26 +03:00
userAvatar_->setImage(avatar);
}
2018-01-03 19:05:49 +03:00
void
TimelineItem::contextMenuEvent(QContextMenuEvent *event)
{
if (contextMenu_)
contextMenu_->exec(event->globalPos());
2018-01-03 19:05:49 +03:00
}
void
TimelineItem::paintEvent(QPaintEvent *)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
void
TimelineItem::addSaveImageAction(ImageItem *image)
{
if (contextMenu_) {
auto saveImage = new QAction("Save image", this);
contextMenu_->addAction(saveImage);
connect(saveImage, &QAction::triggered, image, &ImageItem::saveAs);
}
}
2018-03-17 22:23:46 +03:00
2018-06-30 15:13:15 +03:00
void
TimelineItem::addReplyAction()
{
if (contextMenu_) {
auto replyAction = new QAction("Reply", this);
contextMenu_->addAction(replyAction);
connect(replyAction, &QAction::triggered, this, [this]() {
if (!body_)
return;
emit ChatPage::instance()->messageReply(
Cache::displayName(room_id_, descriptionMsg_.userid),
body_->toPlainText());
});
}
}
void
TimelineItem::addKeyRequestAction()
{
if (contextMenu_) {
auto requestKeys = new QAction("Request encryption keys", this);
contextMenu_->addAction(requestKeys);
connect(requestKeys, &QAction::triggered, this, [this]() {
olm::request_keys(room_id_.toStdString(), event_id_.toStdString());
});
}
}
2018-03-17 22:23:46 +03:00
void
TimelineItem::addAvatar()
{
if (userAvatar_)
return;
// TODO: should be replaced with the proper event struct.
auto userid = descriptionMsg_.userid;
2018-04-21 16:34:50 +03:00
auto displayName = Cache::displayName(room_id_, userid);
2018-03-17 22:23:46 +03:00
2018-07-20 12:02:35 +03:00
generateUserName(userid, displayName);
2018-03-17 22:23:46 +03:00
setupAvatarLayout(displayName);
2018-04-21 16:34:50 +03:00
AvatarProvider::resolve(
room_id_, userid, this, [this](const QImage &img) { setUserAvatar(img); });
2018-03-17 22:23:46 +03:00
}
void
TimelineItem::sendReadReceipt() const
{
if (!event_id_.isEmpty())
http::client()->read_event(room_id_.toStdString(),
event_id_.toStdString(),
[this](mtx::http::RequestErr err) {
if (err) {
nhlog::net()->warn(
"failed to read_event ({}, {})",
room_id_.toStdString(),
event_id_.toStdString());
}
});
}
void
TimelineItem::openRawMessageViewer() const
{
const auto event_id = event_id_.toStdString();
const auto room_id = room_id_.toStdString();
auto proxy = std::make_shared<EventProxy>();
connect(proxy.get(), &EventProxy::eventRetrieved, this, [](const nlohmann::json &obj) {
auto dialog = new dialogs::RawMessage{QString::fromStdString(obj.dump(4))};
Q_UNUSED(dialog);
});
http::client()->get_event(
room_id,
event_id,
[event_id, room_id, proxy = std::move(proxy)](
const mtx::events::collections::TimelineEvents &res, mtx::http::RequestErr err) {
using namespace mtx::events;
if (err) {
nhlog::net()->warn(
"failed to retrieve event {} from {}", event_id, room_id);
return;
}
try {
emit proxy->eventRetrieved(utils::serialize_event(res));
} catch (const nlohmann::json::exception &e) {
nhlog::net()->warn(
"failed to serialize event ({}, {})", room_id, event_id);
}
});
}