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 <QDebug>
|
2017-05-11 01:28:06 +03:00
|
|
|
#include <QRegExp>
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-04-28 14:56:45 +03:00
|
|
|
#include "ImageItem.h"
|
2017-04-25 14:37:54 +03:00
|
|
|
#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\" style=\"color: #333333\">\\1</a>";
|
|
|
|
|
2017-05-07 17:15:38 +03:00
|
|
|
namespace events = matrix::events;
|
|
|
|
namespace msgs = matrix::events::messages;
|
|
|
|
|
2017-05-11 01:28:06 +03:00
|
|
|
TimelineItem::TimelineItem(const QString &userid, const QString &color, QString body, QWidget *parent)
|
2017-04-06 02:06:42 +03:00
|
|
|
: QWidget(parent)
|
|
|
|
{
|
2017-05-11 01:28:06 +03:00
|
|
|
body.replace(URL_REGEX, URL_HTML);
|
|
|
|
|
2017-04-13 04:11:22 +03:00
|
|
|
generateTimestamp(QDateTime::currentDateTime());
|
2017-05-08 19:44:01 +03:00
|
|
|
generateBody(TimelineViewManager::displayName(userid), color, body);
|
2017-04-13 04:11:22 +03:00
|
|
|
setupLayout();
|
|
|
|
}
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-05-11 01:28:06 +03:00
|
|
|
TimelineItem::TimelineItem(QString body, QWidget *parent)
|
2017-04-13 04:11:22 +03:00
|
|
|
: QWidget(parent)
|
|
|
|
{
|
2017-05-11 01:28:06 +03:00
|
|
|
body.replace(URL_REGEX, URL_HTML);
|
|
|
|
|
2017-04-13 04:11:22 +03:00
|
|
|
generateTimestamp(QDateTime::currentDateTime());
|
|
|
|
generateBody(body);
|
|
|
|
setupLayout();
|
|
|
|
}
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-05-07 17:15:38 +03:00
|
|
|
TimelineItem::TimelineItem(ImageItem *image, const events::MessageEvent<msgs::Image> &event, const QString &color, QWidget *parent)
|
2017-04-28 14:56:45 +03:00
|
|
|
: QWidget(parent)
|
|
|
|
{
|
|
|
|
auto timestamp = QDateTime::fromMSecsSinceEpoch(event.timestamp());
|
|
|
|
generateTimestamp(timestamp);
|
2017-05-08 19:44:01 +03:00
|
|
|
generateBody(TimelineViewManager::displayName(event.sender()), color, "");
|
2017-04-28 14:56:45 +03:00
|
|
|
|
|
|
|
top_layout_ = new QHBoxLayout();
|
|
|
|
top_layout_->setMargin(0);
|
|
|
|
top_layout_->addWidget(time_label_);
|
|
|
|
|
|
|
|
auto right_layout = new QVBoxLayout();
|
|
|
|
right_layout->addWidget(content_label_);
|
|
|
|
right_layout->addWidget(image);
|
|
|
|
|
|
|
|
top_layout_->addLayout(right_layout);
|
|
|
|
top_layout_->addStretch(1);
|
|
|
|
|
|
|
|
setLayout(top_layout_);
|
|
|
|
}
|
|
|
|
|
2017-05-07 17:15:38 +03:00
|
|
|
TimelineItem::TimelineItem(ImageItem *image, const events::MessageEvent<msgs::Image> &event, QWidget *parent)
|
2017-04-28 14:56:45 +03:00
|
|
|
: QWidget(parent)
|
|
|
|
{
|
|
|
|
auto timestamp = QDateTime::fromMSecsSinceEpoch(event.timestamp());
|
|
|
|
generateTimestamp(timestamp);
|
|
|
|
|
|
|
|
top_layout_ = new QHBoxLayout();
|
|
|
|
top_layout_->setMargin(0);
|
|
|
|
top_layout_->addWidget(time_label_);
|
|
|
|
top_layout_->addWidget(image, 1);
|
|
|
|
top_layout_->addStretch(1);
|
|
|
|
|
|
|
|
setLayout(top_layout_);
|
|
|
|
}
|
|
|
|
|
2017-05-07 17:15:38 +03:00
|
|
|
TimelineItem::TimelineItem(const events::MessageEvent<msgs::Notice> &event, bool with_sender, const QString &color, QWidget *parent)
|
2017-04-13 04:11:22 +03:00
|
|
|
: QWidget(parent)
|
|
|
|
{
|
2017-05-07 17:15:38 +03:00
|
|
|
auto body = event.content().body().trimmed().toHtmlEscaped();
|
2017-04-06 02:06:42 +03:00
|
|
|
auto timestamp = QDateTime::fromMSecsSinceEpoch(event.timestamp());
|
2017-04-13 04:11:22 +03:00
|
|
|
|
|
|
|
generateTimestamp(timestamp);
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-05-11 01:28:06 +03:00
|
|
|
body.replace(URL_REGEX, URL_HTML);
|
2017-05-07 17:15:38 +03:00
|
|
|
body = "<i style=\"color: #565E5E\">" + body + "</i>";
|
|
|
|
|
|
|
|
if (with_sender)
|
2017-05-08 19:44:01 +03:00
|
|
|
generateBody(TimelineViewManager::displayName(event.sender()), color, body);
|
2017-05-07 17:15:38 +03:00
|
|
|
else
|
|
|
|
generateBody(body);
|
|
|
|
|
|
|
|
setupLayout();
|
|
|
|
}
|
|
|
|
|
|
|
|
TimelineItem::TimelineItem(const events::MessageEvent<msgs::Text> &event, bool with_sender, const QString &color, QWidget *parent)
|
|
|
|
: QWidget(parent)
|
|
|
|
{
|
|
|
|
auto body = event.content().body().trimmed().toHtmlEscaped();
|
|
|
|
auto timestamp = QDateTime::fromMSecsSinceEpoch(event.timestamp());
|
|
|
|
|
|
|
|
generateTimestamp(timestamp);
|
2017-04-12 00:52:56 +03:00
|
|
|
|
2017-05-11 01:28:06 +03:00
|
|
|
body.replace(URL_REGEX, URL_HTML);
|
|
|
|
|
2017-04-13 04:11:22 +03:00
|
|
|
if (with_sender)
|
2017-05-08 19:44:01 +03:00
|
|
|
generateBody(TimelineViewManager::displayName(event.sender()), color, body);
|
2017-04-13 04:11:22 +03:00
|
|
|
else
|
|
|
|
generateBody(body);
|
|
|
|
|
|
|
|
setupLayout();
|
|
|
|
}
|
|
|
|
|
2017-04-25 14:37:54 +03:00
|
|
|
void TimelineItem::generateBody(const QString &body)
|
2017-04-13 04:11:22 +03:00
|
|
|
{
|
|
|
|
content_label_ = new QLabel(this);
|
|
|
|
content_label_->setWordWrap(true);
|
|
|
|
content_label_->setAlignment(Qt::AlignTop);
|
|
|
|
content_label_->setStyleSheet("margin: 0;");
|
|
|
|
QString content(
|
2017-04-06 02:06:42 +03:00
|
|
|
"<html>"
|
|
|
|
"<head/>"
|
|
|
|
"<body>"
|
2017-04-14 15:13:09 +03:00
|
|
|
" <span style=\"font-size: 10pt; color: #171919;\">"
|
2017-04-06 02:06:42 +03:00
|
|
|
" %1"
|
|
|
|
" </span>"
|
|
|
|
"</body>"
|
|
|
|
"</html>");
|
2017-04-19 19:38:39 +03:00
|
|
|
content_label_->setText(content.arg(replaceEmoji(body)));
|
2017-05-11 01:28:06 +03:00
|
|
|
content_label_->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextBrowserInteraction);
|
|
|
|
content_label_->setOpenExternalLinks(true);
|
2017-04-13 04:11:22 +03:00
|
|
|
}
|
|
|
|
|
2017-04-25 14:37:54 +03:00
|
|
|
void TimelineItem::generateBody(const QString &userid, const QString &color, const QString &body)
|
2017-04-13 04:11:22 +03:00
|
|
|
{
|
2017-05-08 19:44:01 +03:00
|
|
|
auto sender = userid;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
content_label_ = new QLabel(this);
|
|
|
|
content_label_->setWordWrap(true);
|
|
|
|
content_label_->setAlignment(Qt::AlignTop);
|
|
|
|
content_label_->setStyleSheet("margin: 0;");
|
|
|
|
QString content(
|
|
|
|
"<html>"
|
|
|
|
"<head/>"
|
|
|
|
"<body>"
|
|
|
|
" <span style=\"font-size: 10pt; font-weight: 600; color: %1\">"
|
|
|
|
" %2"
|
|
|
|
" </span>"
|
2017-04-14 15:13:09 +03:00
|
|
|
" <span style=\"font-size: 10pt; color: #171919;\">"
|
2017-04-06 02:06:42 +03:00
|
|
|
" %3"
|
|
|
|
" </span>"
|
|
|
|
"</body>"
|
|
|
|
"</html>");
|
2017-04-19 19:38:39 +03:00
|
|
|
content_label_->setText(content.arg(color).arg(sender).arg(replaceEmoji(body)));
|
2017-05-11 01:28:06 +03:00
|
|
|
content_label_->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextBrowserInteraction);
|
|
|
|
content_label_->setOpenExternalLinks(true);
|
2017-04-13 04:11:22 +03:00
|
|
|
}
|
|
|
|
|
2017-04-25 14:37:54 +03:00
|
|
|
void TimelineItem::generateTimestamp(const QDateTime &time)
|
2017-04-13 04:11:22 +03:00
|
|
|
{
|
|
|
|
auto local_time = time.toString("HH:mm");
|
|
|
|
|
|
|
|
time_label_ = new QLabel(this);
|
|
|
|
QString msg(
|
|
|
|
"<html>"
|
|
|
|
"<head/>"
|
|
|
|
"<body>"
|
|
|
|
" <span style=\"font-size: 7pt; color: #5d6565;\">"
|
|
|
|
" %1"
|
|
|
|
" </span>"
|
|
|
|
"</body>"
|
|
|
|
"</html>");
|
|
|
|
time_label_->setText(msg.arg(local_time));
|
|
|
|
time_label_->setStyleSheet("margin-left: 7px; margin-right: 7px; margin-top: 0;");
|
|
|
|
time_label_->setAlignment(Qt::AlignTop);
|
|
|
|
}
|
|
|
|
|
2017-04-25 14:37:54 +03:00
|
|
|
void TimelineItem::setupLayout()
|
2017-04-13 04:11:22 +03:00
|
|
|
{
|
|
|
|
if (time_label_ == nullptr) {
|
2017-04-25 14:37:54 +03:00
|
|
|
qWarning() << "TimelineItem: Time label is not initialized";
|
2017-04-13 04:11:22 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (content_label_ == nullptr) {
|
2017-04-25 14:37:54 +03:00
|
|
|
qWarning() << "TimelineItem: Content label is not initialized";
|
2017-04-13 04:11:22 +03:00
|
|
|
return;
|
|
|
|
}
|
2017-04-06 02:06:42 +03:00
|
|
|
|
|
|
|
top_layout_ = new QHBoxLayout();
|
|
|
|
top_layout_->setMargin(0);
|
|
|
|
top_layout_->addWidget(time_label_);
|
|
|
|
top_layout_->addWidget(content_label_, 1);
|
|
|
|
|
|
|
|
setLayout(top_layout_);
|
|
|
|
}
|
|
|
|
|
2017-04-25 14:37:54 +03:00
|
|
|
QString TimelineItem::replaceEmoji(const QString &body)
|
2017-04-19 19:38:39 +03:00
|
|
|
{
|
|
|
|
QString fmtBody = "";
|
|
|
|
|
|
|
|
for (auto &c : body) {
|
2017-04-23 21:31:08 +03:00
|
|
|
int code = c.unicode();
|
2017-04-19 19:38:39 +03:00
|
|
|
|
2017-04-23 21:31:08 +03:00
|
|
|
// TODO: Be more precise here.
|
|
|
|
if (code > 9000)
|
2017-04-19 19:38:39 +03:00
|
|
|
fmtBody += "<span style=\"font-family: Emoji One; font-size: 16px\">" + QString(c) + "</span>";
|
|
|
|
else
|
|
|
|
fmtBody += c;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmtBody;
|
|
|
|
}
|
|
|
|
|
2017-04-25 14:37:54 +03:00
|
|
|
TimelineItem::~TimelineItem()
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
|
|
|
}
|