matrixion/src/TimelineItem.cc

479 lines
15 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/>.
*/
#include <QDateTime>
#include <QFontDatabase>
2017-05-11 01:28:06 +03:00
#include <QRegExp>
2017-10-28 15:46:39 +03:00
#include <QSettings>
#include <QTextEdit>
2017-04-06 02:06:42 +03:00
2017-10-28 15:46:39 +03:00
#include "Avatar.h"
#include "AvatarProvider.h"
#include "Config.h"
2017-04-28 14:56:45 +03:00
#include "ImageItem.h"
2017-10-28 15:46:39 +03:00
#include "Sync.h"
#include "TimelineItem.h"
2017-05-08 19:44:01 +03:00
#include "TimelineViewManager.h"
2017-04-06 02:06:42 +03:00
2017-05-11 01:28:06 +03:00
static const QRegExp URL_REGEX("((?:https?|ftp)://\\S+)");
static const QString URL_HTML = "<a href=\"\\1\">\\1</a>";
2017-05-11 01:28:06 +03:00
namespace events = matrix::events;
2017-08-26 11:33:26 +03:00
namespace msgs = matrix::events::messages;
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;
2017-08-26 11:33:26 +03:00
font_.setPixelSize(conf::fontSize);
2017-08-26 11:33:26 +03:00
QFontMetrics fm(font_);
2017-08-26 11:33:26 +03:00
topLayout_ = new QHBoxLayout(this);
sideLayout_ = new QVBoxLayout();
mainLayout_ = new QVBoxLayout();
headerLayout_ = new QHBoxLayout();
2017-08-26 11:33:26 +03:00
topLayout_->setContentsMargins(conf::timeline::msgMargin, conf::timeline::msgMargin, 0, 0);
topLayout_->setSpacing(0);
2017-08-26 11:33:26 +03:00
topLayout_->addLayout(sideLayout_);
topLayout_->addLayout(mainLayout_, 1);
2017-08-26 11:33:26 +03:00
sideLayout_->setMargin(0);
sideLayout_->setSpacing(0);
2017-08-26 11:33:26 +03:00
mainLayout_->setContentsMargins(conf::timeline::headerLeftMargin, 0, 0, 0);
mainLayout_->setSpacing(0);
2017-08-26 11:33:26 +03:00
headerLayout_->setMargin(0);
headerLayout_->setSpacing(conf::timeline::headerSpacing);
}
2017-08-20 13:47:22 +03:00
/*
* For messages created locally.
*/
TimelineItem::TimelineItem(events::MessageEventType ty,
const QString &userid,
QString body,
bool withSender,
QWidget *parent)
2017-08-20 13:47:22 +03:00
: QWidget(parent)
2017-04-06 02:06:42 +03:00
{
2017-08-26 11:33:26 +03:00
init();
2017-08-26 11:33:26 +03:00
auto displayName = TimelineViewManager::displayName(userid);
auto timestamp = QDateTime::currentDateTime();
2017-05-11 01:28:06 +03:00
if (ty == events::MessageEventType::Emote) {
body = QString("* %1 %2").arg(displayName).arg(body);
2017-11-06 00:04:55 +03:00
descriptionMsg_ = {"", userid, body, descriptiveTime(timestamp)};
} else {
descriptionMsg_ = {
2017-11-06 00:04:55 +03:00
"You: ", userid, body, descriptiveTime(QDateTime::currentDateTime())};
}
2017-09-22 22:12:36 +03:00
body = body.toHtmlEscaped();
2017-08-26 11:33:26 +03:00
body.replace(URL_REGEX, URL_HTML);
body.replace("\n", "<br/>");
generateTimestamp(timestamp);
if (withSender) {
generateBody(displayName, body);
setupAvatarLayout(displayName);
mainLayout_->addLayout(headerLayout_);
AvatarProvider::resolve(userid, this);
} else {
generateBody(body);
setupSimpleLayout();
}
2017-08-26 11:33:26 +03:00
mainLayout_->addWidget(body_);
}
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,
QWidget *parent)
2017-11-06 00:04:55 +03:00
: QWidget{parent}
2017-09-10 12:58:00 +03:00
{
init();
auto displayName = TimelineViewManager::displayName(userid);
auto timestamp = QDateTime::currentDateTime();
2017-11-06 00:04:55 +03:00
descriptionMsg_ = {"You", userid, " sent an image", descriptiveTime(timestamp)};
2017-09-10 12:58:00 +03:00
generateTimestamp(timestamp);
auto imageLayout = new QHBoxLayout();
imageLayout->setMargin(0);
imageLayout->addWidget(image);
imageLayout->addStretch(1);
if (withSender) {
generateBody(displayName, "");
setupAvatarLayout(displayName);
mainLayout_->addLayout(headerLayout_);
AvatarProvider::resolve(userid, this);
} else {
setupSimpleLayout();
}
mainLayout_->addLayout(imageLayout);
}
/*
* Used to display images. The avatar and the username are displayed.
*/
TimelineItem::TimelineItem(ImageItem *image,
2017-08-26 11:33:26 +03:00
const events::MessageEvent<msgs::Image> &event,
bool with_sender,
QWidget *parent)
2017-08-20 13:47:22 +03:00
: QWidget(parent)
2017-04-28 14:56:45 +03:00
{
2017-08-26 11:33:26 +03:00
init();
2017-08-26 11:33:26 +03:00
auto timestamp = QDateTime::fromMSecsSinceEpoch(event.timestamp());
auto displayName = TimelineViewManager::displayName(event.sender());
2017-08-26 11:33:26 +03:00
QSettings settings;
2017-11-06 00:04:55 +03:00
descriptionMsg_ = {event.sender() == settings.value("auth/user_id") ? "You" : displayName,
event.sender(),
" sent an image",
descriptiveTime(QDateTime::fromMSecsSinceEpoch(event.timestamp()))};
2017-08-26 11:33:26 +03:00
generateTimestamp(timestamp);
2017-04-28 14:56:45 +03:00
2017-08-26 11:33:26 +03:00
auto imageLayout = new QHBoxLayout();
imageLayout->setMargin(0);
imageLayout->addWidget(image);
imageLayout->addStretch(1);
2017-04-28 14:56:45 +03:00
2017-08-26 11:33:26 +03:00
if (with_sender) {
generateBody(displayName, "");
setupAvatarLayout(displayName);
2017-04-28 14:56:45 +03:00
2017-08-26 11:33:26 +03:00
mainLayout_->addLayout(headerLayout_);
2017-04-28 14:56:45 +03:00
2017-08-26 11:33:26 +03:00
AvatarProvider::resolve(event.sender(), this);
} else {
setupSimpleLayout();
}
2017-08-06 22:28:54 +03:00
2017-08-26 11:33:26 +03:00
mainLayout_->addLayout(imageLayout);
2017-04-28 14:56:45 +03:00
}
/*
* Used to display remote notice messages.
*/
2017-08-20 13:47:22 +03:00
TimelineItem::TimelineItem(const events::MessageEvent<msgs::Notice> &event,
2017-08-26 11:33:26 +03:00
bool with_sender,
QWidget *parent)
2017-08-20 13:47:22 +03:00
: QWidget(parent)
{
2017-08-26 11:33:26 +03:00
init();
2017-11-06 00:04:55 +03:00
descriptionMsg_ = {TimelineViewManager::displayName(event.sender()),
event.sender(),
" sent a notification",
descriptiveTime(QDateTime::fromMSecsSinceEpoch(event.timestamp()))};
2017-11-07 10:16:39 +03:00
auto body = event.content().body().trimmed().toHtmlEscaped();
2017-08-26 11:33:26 +03:00
auto timestamp = QDateTime::fromMSecsSinceEpoch(event.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
body.replace(URL_REGEX, URL_HTML);
body.replace("\n", "<br/>");
body = "<i>" + body + "</i>";
2017-08-26 11:33:26 +03:00
if (with_sender) {
auto displayName = TimelineViewManager::displayName(event.sender());
2017-08-26 11:33:26 +03:00
generateBody(displayName, body);
setupAvatarLayout(displayName);
2017-08-26 11:33:26 +03:00
mainLayout_->addLayout(headerLayout_);
2017-08-26 11:33:26 +03:00
AvatarProvider::resolve(event.sender(), this);
} else {
generateBody(body);
setupSimpleLayout();
}
2017-08-26 11:33:26 +03:00
mainLayout_->addWidget(body_);
}
/*
* Used to display remote emote messages.
*/
TimelineItem::TimelineItem(const events::MessageEvent<msgs::Emote> &event,
bool with_sender,
QWidget *parent)
: QWidget(parent)
{
init();
2017-09-22 22:12:36 +03:00
auto body = event.content().body().trimmed();
auto timestamp = QDateTime::fromMSecsSinceEpoch(event.timestamp());
auto displayName = TimelineViewManager::displayName(event.sender());
auto emoteMsg = QString("* %1 %2").arg(displayName).arg(body);
2017-11-06 00:04:55 +03:00
descriptionMsg_ = {"",
event.sender(),
emoteMsg,
descriptiveTime(QDateTime::fromMSecsSinceEpoch(event.timestamp()))};
generateTimestamp(timestamp);
2017-09-22 22:12:36 +03:00
emoteMsg = emoteMsg.toHtmlEscaped();
emoteMsg.replace(URL_REGEX, URL_HTML);
emoteMsg.replace("\n", "<br/>");
if (with_sender) {
generateBody(displayName, emoteMsg);
setupAvatarLayout(displayName);
mainLayout_->addLayout(headerLayout_);
AvatarProvider::resolve(event.sender(), this);
} else {
generateBody(emoteMsg);
setupSimpleLayout();
}
mainLayout_->addWidget(body_);
}
/*
* Used to display remote text messages.
*/
2017-08-20 13:47:22 +03:00
TimelineItem::TimelineItem(const events::MessageEvent<msgs::Text> &event,
2017-08-26 11:33:26 +03:00
bool with_sender,
QWidget *parent)
2017-08-20 13:47:22 +03:00
: QWidget(parent)
{
2017-08-26 11:33:26 +03:00
init();
2017-09-22 22:12:36 +03:00
auto body = event.content().body().trimmed();
2017-08-26 11:33:26 +03:00
auto timestamp = QDateTime::fromMSecsSinceEpoch(event.timestamp());
auto displayName = TimelineViewManager::displayName(event.sender());
2017-08-26 11:33:26 +03:00
QSettings settings;
2017-11-06 00:04:55 +03:00
descriptionMsg_ = {event.sender() == settings.value("auth/user_id") ? "You" : displayName,
event.sender(),
QString(": %1").arg(body),
descriptiveTime(QDateTime::fromMSecsSinceEpoch(event.timestamp()))};
2017-08-26 11:33:26 +03:00
generateTimestamp(timestamp);
2017-09-22 22:12:36 +03:00
body = body.toHtmlEscaped();
2017-08-26 11:33:26 +03:00
body.replace(URL_REGEX, URL_HTML);
body.replace("\n", "<br/>");
2017-05-11 01:28:06 +03:00
2017-08-26 11:33:26 +03:00
if (with_sender) {
generateBody(displayName, body);
setupAvatarLayout(displayName);
2017-08-26 11:33:26 +03:00
mainLayout_->addLayout(headerLayout_);
2017-08-26 11:33:26 +03:00
AvatarProvider::resolve(event.sender(), this);
} else {
generateBody(body);
setupSimpleLayout();
}
2017-08-26 11:33:26 +03:00
mainLayout_->addWidget(body_);
}
// Only the body is displayed.
2017-08-20 13:47:22 +03:00
void
TimelineItem::generateBody(const QString &body)
{
QString content("<span> %1 </span>");
2017-08-26 11:33:26 +03:00
body_ = new QLabel(this);
body_->setFont(font_);
body_->setWordWrap(true);
body_->setText(content.arg(replaceEmoji(body)));
body_->setMargin(0);
2017-08-26 11:33:26 +03:00
body_->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextBrowserInteraction);
body_->setOpenExternalLinks(true);
}
// The username/timestamp is displayed along with the message body.
2017-08-20 13:47:22 +03:00
void
2017-08-26 11:33:26 +03:00
TimelineItem::generateBody(const QString &userid, const QString &body)
{
2017-08-26 11:33:26 +03:00
auto sender = userid;
2017-05-08 19:44:01 +03:00
2017-09-28 22:26:24 +03:00
if (userid.startsWith("@")) {
// TODO: Fix this by using a UserId type.
if (userid.split(":")[0].split("@").size() > 1)
sender = userid.split(":")[0].split("@")[1];
}
2017-04-06 02:06:42 +03:00
QString userContent("%1");
QString bodyContent("%1");
2017-08-26 11:33:26 +03:00
QFont usernameFont = font_;
usernameFont.setBold(true);
2017-08-26 11:33:26 +03:00
userName_ = new QLabel(this);
userName_->setFont(usernameFont);
userName_->setText(userContent.arg(sender));
2017-08-26 11:33:26 +03:00
if (body.isEmpty())
return;
2017-08-26 11:33:26 +03:00
body_ = new QLabel(this);
body_->setFont(font_);
body_->setWordWrap(true);
body_->setText(bodyContent.arg(replaceEmoji(body)));
body_->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextBrowserInteraction);
body_->setOpenExternalLinks(true);
body_->setMargin(0);
}
2017-04-06 02:06:42 +03:00
2017-08-20 13:47:22 +03:00
void
TimelineItem::generateTimestamp(const QDateTime &time)
{
QString msg("%1");
2017-04-06 02:06:42 +03:00
2017-08-26 11:33:26 +03:00
QFont timestampFont;
timestampFont.setPixelSize(conf::timeline::fonts::timestamp);
2017-08-26 11:33:26 +03:00
QFontMetrics fm(timestampFont);
int topMargin = QFontMetrics(font_).ascent() - fm.ascent();
2017-08-26 11:33:26 +03:00
timestamp_ = new QLabel(this);
timestamp_->setFont(timestampFont);
timestamp_->setText(msg.arg(time.toString("HH:mm")));
timestamp_->setContentsMargins(0, topMargin, 0, 0);
timestamp_->setStyleSheet(
QString("font-size: %1px;").arg(conf::timeline::fonts::timestamp));
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)
fmtBody += QString("<span style=\"font-family: Emoji "
"One; font-size: %1px\">")
.arg(conf::emojiSize) +
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)
{
2017-08-26 11:33:26 +03:00
topLayout_->setContentsMargins(conf::timeline::msgMargin, conf::timeline::msgMargin, 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());
2017-08-26 11:33:26 +03:00
sideLayout_->addWidget(userAvatar_);
sideLayout_->addStretch(1);
2017-08-26 11:33:26 +03:00
headerLayout_->addWidget(userName_);
headerLayout_->addWidget(timestamp_, 1);
}
2017-08-20 13:47:22 +03:00
void
TimelineItem::setupSimpleLayout()
{
2017-08-26 11:33:26 +03:00
sideLayout_->addWidget(timestamp_);
2017-08-26 11:33:26 +03:00
// Keep only the time in plain text.
QTextEdit htmlText(timestamp_->text());
QString plainText = htmlText.toPlainText();
2017-08-26 11:33:26 +03:00
timestamp_->adjustSize();
2017-07-15 19:18:34 +03:00
2017-08-26 11:33:26 +03:00
// Align the end of the avatar bubble with the end of the timestamp for
// messages with and without avatar. Otherwise their bodies would not be
// aligned.
int tsWidth = timestamp_->fontMetrics().width(plainText);
int offset = std::max(0, conf::timeline::avatarSize - tsWidth);
2017-08-26 11:33:26 +03:00
int defaultFontHeight = QFontMetrics(font_).ascent();
2017-08-26 11:33:26 +03:00
timestamp_->setAlignment(Qt::AlignTop);
timestamp_->setContentsMargins(
offset + 1, defaultFontHeight - timestamp_->fontMetrics().ascent(), 0, 0);
topLayout_->setContentsMargins(
conf::timeline::msgMargin, conf::timeline::msgMargin / 3, 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);
}
2017-08-20 13:47:22 +03:00
QString
TimelineItem::descriptiveTime(const QDateTime &then)
{
2017-08-26 11:33:26 +03:00
auto now = QDateTime::currentDateTime();
2017-08-26 11:33:26 +03:00
auto days = then.daysTo(now);
2017-08-26 11:33:26 +03:00
if (days == 0)
return then.toString("HH:mm");
else if (days < 2)
return QString("Yesterday");
else if (days < 365)
return then.toString("dd/MM");
2017-08-26 11:33:26 +03:00
return then.toString("dd/MM/yy");
}
2017-10-01 12:11:33 +03:00
TimelineItem::~TimelineItem() {}
void
TimelineItem::paintEvent(QPaintEvent *)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}