2018-01-05 01:27:32 +03:00
|
|
|
#include "Utils.h"
|
|
|
|
|
2018-05-11 16:00:14 +03:00
|
|
|
#include <QApplication>
|
2020-04-26 12:26:51 +03:00
|
|
|
#include <QBuffer>
|
2018-10-01 17:56:46 +03:00
|
|
|
#include <QComboBox>
|
2018-05-11 16:00:14 +03:00
|
|
|
#include <QDesktopWidget>
|
2019-07-05 04:20:19 +03:00
|
|
|
#include <QGuiApplication>
|
2020-04-26 12:26:51 +03:00
|
|
|
#include <QImageReader>
|
2019-08-10 20:14:37 +03:00
|
|
|
#include <QProcessEnvironment>
|
2019-07-05 04:20:19 +03:00
|
|
|
#include <QScreen>
|
2018-07-20 16:15:50 +03:00
|
|
|
#include <QSettings>
|
2018-09-07 20:05:30 +03:00
|
|
|
#include <QTextDocument>
|
2018-09-07 14:52:29 +03:00
|
|
|
#include <QXmlStreamReader>
|
2019-12-14 19:08:36 +03:00
|
|
|
|
2018-07-30 12:35:15 +03:00
|
|
|
#include <cmath>
|
2019-12-14 19:08:36 +03:00
|
|
|
#include <variant>
|
2018-05-11 16:00:14 +03:00
|
|
|
|
2018-09-11 14:56:09 +03:00
|
|
|
#include <cmark.h>
|
2018-01-05 01:27:32 +03:00
|
|
|
|
2019-12-15 05:34:17 +03:00
|
|
|
#include "Cache.h"
|
2018-09-07 14:52:29 +03:00
|
|
|
#include "Config.h"
|
2020-02-24 03:07:25 +03:00
|
|
|
#include "MatrixClient.h"
|
2018-09-07 14:52:29 +03:00
|
|
|
|
2018-01-05 01:27:32 +03:00
|
|
|
using TimelineEvent = mtx::events::collections::TimelineEvents;
|
|
|
|
|
2019-01-20 07:43:48 +03:00
|
|
|
QHash<QString, QString> authorColors_;
|
|
|
|
|
2020-01-31 08:12:02 +03:00
|
|
|
template<class T, class Event>
|
|
|
|
static DescInfo
|
|
|
|
createDescriptionInfo(const Event &event, const QString &localUser, const QString &room_id)
|
|
|
|
{
|
|
|
|
const auto msg = std::get<T>(event);
|
|
|
|
const auto sender = QString::fromStdString(msg.sender);
|
|
|
|
|
|
|
|
const auto username = cache::displayName(room_id, sender);
|
|
|
|
const auto ts = QDateTime::fromMSecsSinceEpoch(msg.origin_server_ts);
|
|
|
|
|
2020-07-11 02:19:48 +03:00
|
|
|
return DescInfo{QString::fromStdString(msg.event_id),
|
|
|
|
sender,
|
|
|
|
utils::messageDescription<T>(
|
|
|
|
username, utils::event_body(event).trimmed(), sender == localUser),
|
|
|
|
utils::descriptiveTime(ts),
|
|
|
|
msg.origin_server_ts,
|
|
|
|
ts};
|
2020-01-31 08:12:02 +03:00
|
|
|
}
|
|
|
|
|
2018-07-20 16:15:50 +03:00
|
|
|
QString
|
|
|
|
utils::localUser()
|
|
|
|
{
|
2020-02-24 03:07:25 +03:00
|
|
|
return QString::fromStdString(http::client()->user_id().to_string());
|
2018-07-20 16:15:50 +03:00
|
|
|
}
|
|
|
|
|
2020-05-19 22:04:38 +03:00
|
|
|
bool
|
|
|
|
utils::codepointIsEmoji(uint code)
|
|
|
|
{
|
|
|
|
// TODO: Be more precise here.
|
|
|
|
return (code >= 0x2600 && code <= 0x27bf) || (code >= 0x1f300 && code <= 0x1f3ff) ||
|
|
|
|
(code >= 0x1f000 && code <= 0x1faff);
|
|
|
|
}
|
|
|
|
|
2019-07-27 00:44:44 +03:00
|
|
|
QString
|
|
|
|
utils::replaceEmoji(const QString &body)
|
|
|
|
{
|
|
|
|
QString fmtBody = "";
|
|
|
|
|
|
|
|
QVector<uint> utf32_string = body.toUcs4();
|
|
|
|
|
|
|
|
QSettings settings;
|
|
|
|
QString userFontFamily = settings.value("user/emoji_font_family", "emoji").toString();
|
|
|
|
|
2020-01-21 22:41:09 +03:00
|
|
|
bool insideFontBlock = false;
|
2019-07-27 00:44:44 +03:00
|
|
|
for (auto &code : utf32_string) {
|
2020-05-19 22:04:38 +03:00
|
|
|
if (utils::codepointIsEmoji(code)) {
|
2020-01-21 06:18:17 +03:00
|
|
|
if (!insideFontBlock) {
|
|
|
|
fmtBody += QString("<font face=\"" + userFontFamily + "\">");
|
|
|
|
insideFontBlock = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (insideFontBlock) {
|
|
|
|
fmtBody += "</font>";
|
|
|
|
insideFontBlock = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmtBody += QString::fromUcs4(&code, 1);
|
2019-07-27 00:44:44 +03:00
|
|
|
}
|
2020-01-21 22:41:09 +03:00
|
|
|
if (insideFontBlock) {
|
|
|
|
fmtBody += "</font>";
|
|
|
|
}
|
2019-07-27 00:44:44 +03:00
|
|
|
|
|
|
|
return fmtBody;
|
|
|
|
}
|
|
|
|
|
2018-07-22 19:48:58 +03:00
|
|
|
void
|
|
|
|
utils::setScaleFactor(float factor)
|
|
|
|
{
|
|
|
|
if (factor < 1 || factor > 3)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QSettings settings;
|
|
|
|
settings.setValue("settings/scale_factor", factor);
|
|
|
|
}
|
|
|
|
|
|
|
|
float
|
|
|
|
utils::scaleFactor()
|
|
|
|
{
|
2019-09-02 01:12:01 +03:00
|
|
|
QSettings settings;
|
2018-07-22 19:48:58 +03:00
|
|
|
return settings.value("settings/scale_factor", -1).toFloat();
|
|
|
|
}
|
|
|
|
|
2018-07-22 16:36:25 +03:00
|
|
|
bool
|
|
|
|
utils::respondsToKeyRequests(const std::string &roomId)
|
|
|
|
{
|
|
|
|
return respondsToKeyRequests(QString::fromStdString(roomId));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
utils::respondsToKeyRequests(const QString &roomId)
|
|
|
|
{
|
|
|
|
if (roomId.isEmpty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
QSettings settings;
|
|
|
|
return settings.value("rooms/respond_to_key_requests/" + roomId, false).toBool();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
utils::setKeyRequestsPreference(QString roomId, bool value)
|
|
|
|
{
|
|
|
|
if (roomId.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
QSettings settings;
|
|
|
|
settings.setValue("rooms/respond_to_key_requests/" + roomId, value);
|
|
|
|
}
|
|
|
|
|
2018-01-05 01:27:32 +03:00
|
|
|
QString
|
|
|
|
utils::descriptiveTime(const QDateTime &then)
|
|
|
|
{
|
|
|
|
const auto now = QDateTime::currentDateTime();
|
|
|
|
const auto days = then.daysTo(now);
|
|
|
|
|
|
|
|
if (days == 0)
|
2020-06-06 00:34:00 +03:00
|
|
|
return QLocale::system().toString(then.time(), QLocale::ShortFormat);
|
2018-01-05 01:27:32 +03:00
|
|
|
else if (days < 2)
|
2019-07-28 13:50:10 +03:00
|
|
|
return QString(QCoreApplication::translate("descriptiveTime", "Yesterday"));
|
|
|
|
else if (days < 7)
|
|
|
|
return then.toString("dddd");
|
2018-01-05 01:27:32 +03:00
|
|
|
|
2020-06-06 00:34:00 +03:00
|
|
|
return QLocale::system().toString(then.date(), QLocale::ShortFormat);
|
2018-01-05 01:27:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
DescInfo
|
2018-04-21 16:34:50 +03:00
|
|
|
utils::getMessageDescription(const TimelineEvent &event,
|
|
|
|
const QString &localUser,
|
|
|
|
const QString &room_id)
|
2018-01-05 01:27:32 +03:00
|
|
|
{
|
2020-07-11 02:19:48 +03:00
|
|
|
using Audio = mtx::events::RoomEvent<mtx::events::msg::Audio>;
|
|
|
|
using Emote = mtx::events::RoomEvent<mtx::events::msg::Emote>;
|
|
|
|
using File = mtx::events::RoomEvent<mtx::events::msg::File>;
|
|
|
|
using Image = mtx::events::RoomEvent<mtx::events::msg::Image>;
|
|
|
|
using Notice = mtx::events::RoomEvent<mtx::events::msg::Notice>;
|
|
|
|
using Text = mtx::events::RoomEvent<mtx::events::msg::Text>;
|
|
|
|
using Video = mtx::events::RoomEvent<mtx::events::msg::Video>;
|
|
|
|
using CallInvite = mtx::events::RoomEvent<mtx::events::msg::CallInvite>;
|
|
|
|
using CallAnswer = mtx::events::RoomEvent<mtx::events::msg::CallAnswer>;
|
|
|
|
using CallHangUp = mtx::events::RoomEvent<mtx::events::msg::CallHangUp>;
|
|
|
|
using Encrypted = mtx::events::EncryptedEvent<mtx::events::msg::Encrypted>;
|
2018-01-05 01:27:32 +03:00
|
|
|
|
2019-12-14 19:08:36 +03:00
|
|
|
if (std::holds_alternative<Audio>(event)) {
|
2018-04-29 15:42:40 +03:00
|
|
|
return createDescriptionInfo<Audio>(event, localUser, room_id);
|
2019-12-14 19:08:36 +03:00
|
|
|
} else if (std::holds_alternative<Emote>(event)) {
|
2018-04-29 15:42:40 +03:00
|
|
|
return createDescriptionInfo<Emote>(event, localUser, room_id);
|
2019-12-14 19:08:36 +03:00
|
|
|
} else if (std::holds_alternative<File>(event)) {
|
2018-04-29 15:42:40 +03:00
|
|
|
return createDescriptionInfo<File>(event, localUser, room_id);
|
2019-12-14 19:08:36 +03:00
|
|
|
} else if (std::holds_alternative<Image>(event)) {
|
2018-04-29 15:42:40 +03:00
|
|
|
return createDescriptionInfo<Image>(event, localUser, room_id);
|
2019-12-14 19:08:36 +03:00
|
|
|
} else if (std::holds_alternative<Notice>(event)) {
|
2018-04-29 15:42:40 +03:00
|
|
|
return createDescriptionInfo<Notice>(event, localUser, room_id);
|
2019-12-14 19:08:36 +03:00
|
|
|
} else if (std::holds_alternative<Text>(event)) {
|
2018-04-29 15:42:40 +03:00
|
|
|
return createDescriptionInfo<Text>(event, localUser, room_id);
|
2019-12-14 19:08:36 +03:00
|
|
|
} else if (std::holds_alternative<Video>(event)) {
|
2018-04-29 15:42:40 +03:00
|
|
|
return createDescriptionInfo<Video>(event, localUser, room_id);
|
2020-07-11 02:19:48 +03:00
|
|
|
} else if (std::holds_alternative<CallInvite>(event)) {
|
|
|
|
return createDescriptionInfo<CallInvite>(event, localUser, room_id);
|
|
|
|
} else if (std::holds_alternative<CallAnswer>(event)) {
|
|
|
|
return createDescriptionInfo<CallAnswer>(event, localUser, room_id);
|
|
|
|
} else if (std::holds_alternative<CallHangUp>(event)) {
|
|
|
|
return createDescriptionInfo<CallHangUp>(event, localUser, room_id);
|
2019-12-14 19:08:36 +03:00
|
|
|
} else if (std::holds_alternative<mtx::events::Sticker>(event)) {
|
2018-04-29 15:42:40 +03:00
|
|
|
return createDescriptionInfo<mtx::events::Sticker>(event, localUser, room_id);
|
2019-12-14 19:08:36 +03:00
|
|
|
} else if (auto msg = std::get_if<Encrypted>(&event); msg != nullptr) {
|
|
|
|
const auto sender = QString::fromStdString(msg->sender);
|
2018-06-28 16:16:43 +03:00
|
|
|
|
2019-12-15 04:56:04 +03:00
|
|
|
const auto username = cache::displayName(room_id, sender);
|
2019-12-14 19:08:36 +03:00
|
|
|
const auto ts = QDateTime::fromMSecsSinceEpoch(msg->origin_server_ts);
|
2018-06-28 16:16:43 +03:00
|
|
|
|
|
|
|
DescInfo info;
|
2020-04-23 03:29:41 +03:00
|
|
|
info.userid = sender;
|
|
|
|
info.body = QString(" %1").arg(
|
|
|
|
messageDescription<Encrypted>(username, "", sender == localUser));
|
2020-05-01 00:59:17 +03:00
|
|
|
info.timestamp = msg->origin_server_ts;
|
|
|
|
info.descriptiveTime = utils::descriptiveTime(ts);
|
|
|
|
info.event_id = QString::fromStdString(msg->event_id);
|
|
|
|
info.datetime = ts;
|
2018-06-28 16:16:43 +03:00
|
|
|
|
|
|
|
return info;
|
2018-01-05 01:27:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return DescInfo{};
|
|
|
|
}
|
2018-01-12 11:21:53 +03:00
|
|
|
|
|
|
|
QString
|
|
|
|
utils::firstChar(const QString &input)
|
|
|
|
{
|
2018-01-25 19:10:05 +03:00
|
|
|
if (input.isEmpty())
|
|
|
|
return input;
|
2018-01-12 11:21:53 +03:00
|
|
|
|
2018-01-25 19:10:05 +03:00
|
|
|
for (auto const &c : input.toUcs4()) {
|
|
|
|
if (QString::fromUcs4(&c, 1) != QString("#"))
|
|
|
|
return QString::fromUcs4(&c, 1).toUpper();
|
|
|
|
}
|
|
|
|
|
|
|
|
return QString::fromUcs4(&input.toUcs4().at(0), 1).toUpper();
|
2018-01-12 11:21:53 +03:00
|
|
|
}
|
2018-02-18 23:52:31 +03:00
|
|
|
|
|
|
|
QString
|
2018-02-19 23:09:21 +03:00
|
|
|
utils::humanReadableFileSize(uint64_t bytes)
|
2018-02-18 23:52:31 +03:00
|
|
|
{
|
|
|
|
constexpr static const char *units[] = {"B", "KiB", "MiB", "GiB", "TiB"};
|
|
|
|
constexpr static const int length = sizeof(units) / sizeof(units[0]);
|
|
|
|
|
|
|
|
int u = 0;
|
|
|
|
double size = static_cast<double>(bytes);
|
|
|
|
while (size >= 1024.0 && u < length) {
|
|
|
|
++u;
|
|
|
|
size /= 1024.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return QString::number(size, 'g', 4) + ' ' + units[u];
|
|
|
|
}
|
2018-03-25 00:16:15 +03:00
|
|
|
|
|
|
|
int
|
|
|
|
utils::levenshtein_distance(const std::string &s1, const std::string &s2)
|
|
|
|
{
|
|
|
|
const int nlen = s1.size();
|
|
|
|
const int hlen = s2.size();
|
|
|
|
|
|
|
|
if (hlen == 0)
|
|
|
|
return -1;
|
|
|
|
if (nlen == 1)
|
|
|
|
return s2.find(s1);
|
|
|
|
|
|
|
|
std::vector<int> row1(hlen + 1, 0);
|
|
|
|
|
|
|
|
for (int i = 0; i < nlen; ++i) {
|
|
|
|
std::vector<int> row2(1, i + 1);
|
|
|
|
|
|
|
|
for (int j = 0; j < hlen; ++j) {
|
|
|
|
const int cost = s1[i] != s2[j];
|
|
|
|
row2.push_back(
|
|
|
|
std::min(row1[j + 1] + 1, std::min(row2[j] + 1, row1[j] + cost)));
|
|
|
|
}
|
|
|
|
|
|
|
|
row1.swap(row2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return *std::min_element(row1.begin(), row1.end());
|
|
|
|
}
|
2018-05-05 16:38:41 +03:00
|
|
|
|
|
|
|
QString
|
2019-12-14 19:08:36 +03:00
|
|
|
utils::event_body(const mtx::events::collections::TimelineEvents &e)
|
2018-05-05 16:38:41 +03:00
|
|
|
{
|
|
|
|
using namespace mtx::events;
|
2019-12-14 19:08:36 +03:00
|
|
|
if (auto ev = std::get_if<RoomEvent<msg::Audio>>(&e); ev != nullptr)
|
|
|
|
return QString::fromStdString(ev->content.body);
|
|
|
|
if (auto ev = std::get_if<RoomEvent<msg::Emote>>(&e); ev != nullptr)
|
|
|
|
return QString::fromStdString(ev->content.body);
|
|
|
|
if (auto ev = std::get_if<RoomEvent<msg::File>>(&e); ev != nullptr)
|
|
|
|
return QString::fromStdString(ev->content.body);
|
|
|
|
if (auto ev = std::get_if<RoomEvent<msg::Image>>(&e); ev != nullptr)
|
|
|
|
return QString::fromStdString(ev->content.body);
|
|
|
|
if (auto ev = std::get_if<RoomEvent<msg::Notice>>(&e); ev != nullptr)
|
|
|
|
return QString::fromStdString(ev->content.body);
|
|
|
|
if (auto ev = std::get_if<RoomEvent<msg::Text>>(&e); ev != nullptr)
|
|
|
|
return QString::fromStdString(ev->content.body);
|
|
|
|
if (auto ev = std::get_if<RoomEvent<msg::Video>>(&e); ev != nullptr)
|
|
|
|
return QString::fromStdString(ev->content.body);
|
|
|
|
|
|
|
|
return "";
|
2018-05-05 16:38:41 +03:00
|
|
|
}
|
2018-05-11 16:00:14 +03:00
|
|
|
|
|
|
|
QPixmap
|
|
|
|
utils::scaleImageToPixmap(const QImage &img, int size)
|
|
|
|
{
|
2018-05-18 21:27:44 +03:00
|
|
|
if (img.isNull())
|
|
|
|
return QPixmap();
|
|
|
|
|
2019-07-05 04:20:19 +03:00
|
|
|
// Deprecated in 5.13: const double sz =
|
|
|
|
// std::ceil(QApplication::desktop()->screen()->devicePixelRatioF() * (double)size);
|
2018-07-30 12:35:15 +03:00
|
|
|
const double sz =
|
2019-07-05 04:20:19 +03:00
|
|
|
std::ceil(QGuiApplication::primaryScreen()->devicePixelRatio() * (double)size);
|
2018-05-11 16:00:14 +03:00
|
|
|
return QPixmap::fromImage(
|
|
|
|
img.scaled(sz, sz, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
|
|
|
|
}
|
2018-07-15 20:05:31 +03:00
|
|
|
|
2018-08-01 21:10:03 +03:00
|
|
|
QPixmap
|
|
|
|
utils::scaleDown(uint64_t maxWidth, uint64_t maxHeight, const QPixmap &source)
|
|
|
|
{
|
|
|
|
if (source.isNull())
|
|
|
|
return QPixmap();
|
|
|
|
|
|
|
|
const double widthRatio = (double)maxWidth / (double)source.width();
|
|
|
|
const double heightRatio = (double)maxHeight / (double)source.height();
|
|
|
|
const double minAspectRatio = std::min(widthRatio, heightRatio);
|
|
|
|
|
|
|
|
// Size of the output image.
|
|
|
|
int w, h = 0;
|
|
|
|
|
|
|
|
if (minAspectRatio > 1) {
|
|
|
|
w = source.width();
|
|
|
|
h = source.height();
|
|
|
|
} else {
|
|
|
|
w = source.width() * minAspectRatio;
|
|
|
|
h = source.height() * minAspectRatio;
|
|
|
|
}
|
|
|
|
|
|
|
|
return source.scaled(w, h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
|
|
|
|
}
|
|
|
|
|
2018-07-15 20:05:31 +03:00
|
|
|
QString
|
|
|
|
utils::mxcToHttp(const QUrl &url, const QString &server, int port)
|
|
|
|
{
|
|
|
|
auto mxcParts = mtx::client::utils::parse_mxc_url(url.toString().toStdString());
|
|
|
|
|
|
|
|
return QString("https://%1:%2/_matrix/media/r0/download/%3/%4")
|
|
|
|
.arg(server)
|
|
|
|
.arg(port)
|
|
|
|
.arg(QString::fromStdString(mxcParts.server))
|
|
|
|
.arg(QString::fromStdString(mxcParts.media_id));
|
|
|
|
}
|
2018-08-21 09:22:51 +03:00
|
|
|
|
|
|
|
QString
|
|
|
|
utils::humanReadableFingerprint(const std::string &ed25519)
|
|
|
|
{
|
|
|
|
return humanReadableFingerprint(QString::fromStdString(ed25519));
|
|
|
|
}
|
|
|
|
QString
|
|
|
|
utils::humanReadableFingerprint(const QString &ed25519)
|
|
|
|
{
|
|
|
|
QStringList fingerprintList;
|
|
|
|
for (int i = 0; i < ed25519.length(); i = i + 4) {
|
|
|
|
fingerprintList << ed25519.mid(i, 4);
|
|
|
|
}
|
|
|
|
return fingerprintList.join(" ");
|
2018-08-30 13:39:09 +03:00
|
|
|
}
|
2018-09-07 14:52:29 +03:00
|
|
|
|
|
|
|
QString
|
|
|
|
utils::linkifyMessage(const QString &body)
|
|
|
|
{
|
2018-09-13 16:10:45 +03:00
|
|
|
// Convert to valid XML.
|
2020-01-21 05:36:26 +03:00
|
|
|
auto doc = body;
|
2019-05-01 13:11:19 +03:00
|
|
|
doc.replace(conf::strings::url_regex, conf::strings::url_html);
|
2018-09-13 16:10:45 +03:00
|
|
|
|
2019-05-01 13:11:19 +03:00
|
|
|
return doc;
|
2018-09-07 14:52:29 +03:00
|
|
|
}
|
2018-09-07 20:05:30 +03:00
|
|
|
|
2020-01-14 19:47:30 +03:00
|
|
|
QString
|
|
|
|
utils::escapeBlacklistedHtml(const QString &rawStr)
|
2019-10-17 10:36:16 +03:00
|
|
|
{
|
2020-01-16 22:36:44 +03:00
|
|
|
static const std::array allowedTags = {
|
2020-01-14 19:47:30 +03:00
|
|
|
"font", "/font", "del", "/del", "h1", "/h1", "h2", "/h2",
|
|
|
|
"h3", "/h3", "h4", "/h4", "h5", "/h5", "h6", "/h6",
|
|
|
|
"blockquote", "/blockquote", "p", "/p", "a", "/a", "ul", "/ul",
|
|
|
|
"ol", "/ol", "sup", "/sup", "sub", "/sub", "li", "/li",
|
|
|
|
"b", "/b", "i", "/i", "u", "/u", "strong", "/strong",
|
|
|
|
"em", "/em", "strike", "/strike", "code", "/code", "hr", "/hr",
|
|
|
|
"br", "br/", "div", "/div", "table", "/table", "thead", "/thead",
|
|
|
|
"tbody", "/tbody", "tr", "/tr", "th", "/th", "td", "/td",
|
|
|
|
"caption", "/caption", "pre", "/pre", "span", "/span", "img", "/img"};
|
|
|
|
QByteArray data = rawStr.toUtf8();
|
2019-10-17 10:36:16 +03:00
|
|
|
QByteArray buffer;
|
|
|
|
const size_t length = data.size();
|
|
|
|
buffer.reserve(length);
|
2020-01-14 19:47:30 +03:00
|
|
|
bool escapingTag = false;
|
2019-10-17 10:36:16 +03:00
|
|
|
for (size_t pos = 0; pos != length; ++pos) {
|
|
|
|
switch (data.at(pos)) {
|
2020-01-14 19:47:30 +03:00
|
|
|
case '<': {
|
|
|
|
bool oneTagMatched = false;
|
|
|
|
size_t endPos = std::min(static_cast<size_t>(data.indexOf('>', pos)),
|
|
|
|
static_cast<size_t>(data.indexOf(' ', pos)));
|
|
|
|
|
|
|
|
auto mid = data.mid(pos + 1, endPos - pos - 1);
|
|
|
|
for (const auto &tag : allowedTags) {
|
|
|
|
// TODO: Check src and href attribute
|
2020-01-16 22:36:44 +03:00
|
|
|
if (mid.toLower() == tag) {
|
2020-01-14 19:47:30 +03:00
|
|
|
oneTagMatched = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (oneTagMatched)
|
|
|
|
buffer.append('<');
|
|
|
|
else {
|
|
|
|
escapingTag = true;
|
|
|
|
buffer.append("<");
|
|
|
|
}
|
2019-10-17 10:36:16 +03:00
|
|
|
break;
|
2020-01-14 19:47:30 +03:00
|
|
|
}
|
2019-10-17 10:36:16 +03:00
|
|
|
case '>':
|
2020-02-07 00:04:55 +03:00
|
|
|
if (escapingTag) {
|
2020-01-14 19:47:30 +03:00
|
|
|
buffer.append(">");
|
|
|
|
escapingTag = false;
|
2020-02-07 00:04:55 +03:00
|
|
|
} else
|
2020-01-14 19:47:30 +03:00
|
|
|
buffer.append('>');
|
2019-10-17 10:36:16 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
buffer.append(data.at(pos));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-01-14 19:47:30 +03:00
|
|
|
return QString::fromUtf8(buffer);
|
2019-09-21 02:38:17 +03:00
|
|
|
}
|
|
|
|
|
2018-09-12 14:20:12 +03:00
|
|
|
QString
|
|
|
|
utils::markdownToHtml(const QString &text)
|
2018-09-07 20:05:30 +03:00
|
|
|
{
|
2020-01-14 19:47:30 +03:00
|
|
|
const auto str = text.toUtf8();
|
|
|
|
const char *tmp_buf = cmark_markdown_to_html(str.constData(), str.size(), CMARK_OPT_UNSAFE);
|
2018-09-07 20:05:30 +03:00
|
|
|
|
2018-09-11 14:56:09 +03:00
|
|
|
// Copy the null terminated output buffer.
|
|
|
|
std::string html(tmp_buf);
|
|
|
|
|
|
|
|
// The buffer is no longer needed.
|
|
|
|
free((char *)tmp_buf);
|
|
|
|
|
2020-01-21 05:36:26 +03:00
|
|
|
auto result = linkifyMessage(escapeBlacklistedHtml(QString::fromStdString(html))).trimmed();
|
2018-09-07 20:05:30 +03:00
|
|
|
|
2020-01-27 19:25:09 +03:00
|
|
|
if (result.count("<p>") == 1 && result.startsWith("<p>") && result.endsWith("</p>")) {
|
|
|
|
result = result.mid(3, result.size() - 3 - 4);
|
|
|
|
}
|
|
|
|
|
2018-09-13 11:02:54 +03:00
|
|
|
return result;
|
2018-09-07 20:05:30 +03:00
|
|
|
}
|
2018-09-19 22:42:26 +03:00
|
|
|
|
2019-06-14 05:33:04 +03:00
|
|
|
QString
|
|
|
|
utils::getFormattedQuoteBody(const RelatedInfo &related, const QString &html)
|
|
|
|
{
|
2020-01-24 03:07:25 +03:00
|
|
|
auto getFormattedBody = [related]() -> QString {
|
2020-01-21 05:36:26 +03:00
|
|
|
using MsgType = mtx::events::MessageType;
|
|
|
|
|
|
|
|
switch (related.type) {
|
|
|
|
case MsgType::File: {
|
2020-01-24 03:07:25 +03:00
|
|
|
return "sent a file.";
|
2020-01-21 05:36:26 +03:00
|
|
|
}
|
|
|
|
case MsgType::Image: {
|
2020-01-24 03:07:25 +03:00
|
|
|
return "sent an image.";
|
2020-01-21 05:36:26 +03:00
|
|
|
}
|
|
|
|
case MsgType::Audio: {
|
2020-01-24 03:07:25 +03:00
|
|
|
return "sent an audio file.";
|
2020-01-21 05:36:26 +03:00
|
|
|
}
|
|
|
|
case MsgType::Video: {
|
2020-01-24 03:07:25 +03:00
|
|
|
return "sent a video";
|
2020-01-21 05:36:26 +03:00
|
|
|
}
|
|
|
|
default: {
|
|
|
|
return related.quoted_formatted_body;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-06-14 05:33:04 +03:00
|
|
|
return QString("<mx-reply><blockquote><a "
|
2019-07-05 04:20:19 +03:00
|
|
|
"href=\"https://matrix.to/#/%1/%2\">In reply "
|
2019-11-05 19:16:04 +03:00
|
|
|
"to</a> <a href=\"https://matrix.to/#/%3\">%4</a><br"
|
2019-07-05 04:20:19 +03:00
|
|
|
"/>%5</blockquote></mx-reply>")
|
|
|
|
.arg(related.room,
|
|
|
|
QString::fromStdString(related.related_event),
|
2019-06-14 05:33:04 +03:00
|
|
|
related.quoted_user,
|
|
|
|
related.quoted_user,
|
2020-01-21 05:36:26 +03:00
|
|
|
getFormattedBody()) +
|
2019-06-14 05:33:04 +03:00
|
|
|
html;
|
|
|
|
}
|
|
|
|
|
2019-07-05 04:20:19 +03:00
|
|
|
QString
|
|
|
|
utils::getQuoteBody(const RelatedInfo &related)
|
|
|
|
{
|
|
|
|
using MsgType = mtx::events::MessageType;
|
|
|
|
|
|
|
|
switch (related.type) {
|
|
|
|
case MsgType::File: {
|
2020-01-24 03:07:25 +03:00
|
|
|
return "sent a file.";
|
2019-07-05 04:20:19 +03:00
|
|
|
}
|
|
|
|
case MsgType::Image: {
|
2020-01-24 03:07:25 +03:00
|
|
|
return "sent an image.";
|
2019-07-05 04:20:19 +03:00
|
|
|
}
|
|
|
|
case MsgType::Audio: {
|
2020-01-24 03:07:25 +03:00
|
|
|
return "sent an audio file.";
|
2019-07-05 04:20:19 +03:00
|
|
|
}
|
|
|
|
case MsgType::Video: {
|
2020-01-24 03:07:25 +03:00
|
|
|
return "sent a video";
|
2019-07-05 04:20:19 +03:00
|
|
|
}
|
|
|
|
default: {
|
|
|
|
return related.quoted_body;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-19 22:42:26 +03:00
|
|
|
QString
|
|
|
|
utils::linkColor()
|
|
|
|
{
|
|
|
|
QSettings settings;
|
2019-08-10 20:14:37 +03:00
|
|
|
// Default to system theme if QT_QPA_PLATFORMTHEME var is set.
|
|
|
|
QString defaultTheme =
|
|
|
|
QProcessEnvironment::systemEnvironment().value("QT_QPA_PLATFORMTHEME", "").isEmpty()
|
|
|
|
? "light"
|
|
|
|
: "system";
|
|
|
|
const auto theme = settings.value("user/theme", defaultTheme).toString();
|
|
|
|
|
|
|
|
if (theme == "light") {
|
2018-09-19 22:42:26 +03:00
|
|
|
return "#0077b5";
|
2019-08-10 20:14:37 +03:00
|
|
|
} else if (theme == "dark") {
|
2018-09-19 22:42:26 +03:00
|
|
|
return "#38A3D8";
|
2019-08-10 20:14:37 +03:00
|
|
|
} else {
|
|
|
|
return QPalette().color(QPalette::Link).name();
|
|
|
|
}
|
2018-09-19 22:42:26 +03:00
|
|
|
}
|
2018-09-21 10:55:24 +03:00
|
|
|
|
2019-06-27 15:11:02 +03:00
|
|
|
uint32_t
|
2019-01-18 20:17:25 +03:00
|
|
|
utils::hashQString(const QString &input)
|
2019-01-18 07:09:42 +03:00
|
|
|
{
|
2019-06-27 15:11:02 +03:00
|
|
|
uint32_t hash = 0;
|
2019-01-18 07:09:42 +03:00
|
|
|
|
|
|
|
for (int i = 0; i < input.length(); i++) {
|
|
|
|
hash = input.at(i).digitValue() + ((hash << 5) - hash);
|
|
|
|
}
|
2019-01-18 20:17:25 +03:00
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
|
|
|
utils::generateContrastingHexColor(const QString &input, const QString &background)
|
|
|
|
{
|
|
|
|
const QColor backgroundCol(background);
|
|
|
|
const qreal backgroundLum = luminance(background);
|
|
|
|
|
|
|
|
// Create a color for the input
|
2019-01-20 03:12:57 +03:00
|
|
|
auto hash = hashQString(input);
|
|
|
|
// create a hue value based on the hash of the input.
|
2019-06-27 15:11:02 +03:00
|
|
|
auto userHue = static_cast<int>(qAbs(hash % 360));
|
2019-01-20 03:12:57 +03:00
|
|
|
// start with moderate saturation and lightness values.
|
|
|
|
auto sat = 220;
|
|
|
|
auto lightness = 125;
|
2019-01-18 20:17:25 +03:00
|
|
|
|
|
|
|
// converting to a QColor makes the luminance calc easier.
|
2019-01-20 03:12:57 +03:00
|
|
|
QColor inputColor = QColor::fromHsl(userHue, sat, lightness);
|
|
|
|
|
|
|
|
// calculate the initial luminance and contrast of the
|
|
|
|
// generated color. It's possible that no additional
|
|
|
|
// work will be necessary.
|
|
|
|
auto lum = luminance(inputColor);
|
|
|
|
auto contrast = computeContrast(lum, backgroundLum);
|
|
|
|
|
|
|
|
// If the contrast doesn't meet our criteria,
|
|
|
|
// try again and again until they do by modifying first
|
|
|
|
// the lightness and then the saturation of the color.
|
2020-04-23 00:31:50 +03:00
|
|
|
int iterationCount = 9;
|
2019-01-20 03:12:57 +03:00
|
|
|
while (contrast < 5) {
|
|
|
|
// if our lightness is at it's bounds, try changing
|
|
|
|
// saturation instead.
|
|
|
|
if (lightness == 242 || lightness == 13) {
|
|
|
|
qreal newSat = qBound(26.0, sat * 1.25, 242.0);
|
|
|
|
|
|
|
|
inputColor.setHsl(userHue, qFloor(newSat), lightness);
|
|
|
|
auto tmpLum = luminance(inputColor);
|
|
|
|
auto higherContrast = computeContrast(tmpLum, backgroundLum);
|
|
|
|
if (higherContrast > contrast) {
|
|
|
|
contrast = higherContrast;
|
|
|
|
sat = newSat;
|
|
|
|
} else {
|
|
|
|
newSat = qBound(26.0, sat / 1.25, 242.0);
|
|
|
|
inputColor.setHsl(userHue, qFloor(newSat), lightness);
|
|
|
|
tmpLum = luminance(inputColor);
|
|
|
|
auto lowerContrast = computeContrast(tmpLum, backgroundLum);
|
|
|
|
if (lowerContrast > contrast) {
|
|
|
|
contrast = lowerContrast;
|
|
|
|
sat = newSat;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qreal newLightness = qBound(13.0, lightness * 1.25, 242.0);
|
|
|
|
|
|
|
|
inputColor.setHsl(userHue, sat, qFloor(newLightness));
|
|
|
|
|
|
|
|
auto tmpLum = luminance(inputColor);
|
|
|
|
auto higherContrast = computeContrast(tmpLum, backgroundLum);
|
|
|
|
|
|
|
|
// Check to make sure we have actually improved contrast
|
|
|
|
if (higherContrast > contrast) {
|
|
|
|
contrast = higherContrast;
|
|
|
|
lightness = newLightness;
|
|
|
|
// otherwise, try going the other way instead.
|
|
|
|
} else {
|
|
|
|
newLightness = qBound(13.0, lightness / 1.25, 242.0);
|
|
|
|
inputColor.setHsl(userHue, sat, qFloor(newLightness));
|
|
|
|
tmpLum = luminance(inputColor);
|
|
|
|
auto lowerContrast = computeContrast(tmpLum, backgroundLum);
|
|
|
|
if (lowerContrast > contrast) {
|
|
|
|
contrast = lowerContrast;
|
|
|
|
lightness = newLightness;
|
|
|
|
}
|
|
|
|
}
|
2019-01-18 20:17:25 +03:00
|
|
|
}
|
2020-04-23 00:31:50 +03:00
|
|
|
|
|
|
|
// don't loop forever, just give up at some point!
|
|
|
|
// Someone smart may find a better solution
|
|
|
|
if (--iterationCount < 0)
|
|
|
|
break;
|
2019-01-18 20:17:25 +03:00
|
|
|
}
|
|
|
|
|
2019-01-20 03:12:57 +03:00
|
|
|
// get the hex value of the generated color.
|
|
|
|
auto colorHex = inputColor.name();
|
|
|
|
|
2019-01-18 20:17:25 +03:00
|
|
|
return colorHex;
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal
|
|
|
|
utils::computeContrast(const qreal &one, const qreal &two)
|
|
|
|
{
|
|
|
|
auto ratio = (one + 0.05) / (two + 0.05);
|
|
|
|
|
|
|
|
if (two > one) {
|
|
|
|
ratio = 1 / ratio;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ratio;
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal
|
|
|
|
utils::luminance(const QColor &col)
|
|
|
|
{
|
|
|
|
int colRgb[3] = {col.red(), col.green(), col.blue()};
|
|
|
|
qreal lumRgb[3];
|
|
|
|
|
2019-01-18 07:09:42 +03:00
|
|
|
for (int i = 0; i < 3; i++) {
|
2019-01-18 20:17:25 +03:00
|
|
|
qreal v = colRgb[i] / 255.0;
|
|
|
|
v <= 0.03928 ? lumRgb[i] = v / 12.92 : lumRgb[i] = qPow((v + 0.055) / 1.055, 2.4);
|
2019-01-18 07:09:42 +03:00
|
|
|
}
|
2019-01-18 20:17:25 +03:00
|
|
|
|
2019-01-20 03:12:57 +03:00
|
|
|
auto lum = lumRgb[0] * 0.2126 + lumRgb[1] * 0.7152 + lumRgb[2] * 0.0722;
|
|
|
|
|
|
|
|
return lum;
|
2019-01-18 07:09:42 +03:00
|
|
|
}
|
|
|
|
|
2018-09-21 10:55:24 +03:00
|
|
|
void
|
|
|
|
utils::centerWidget(QWidget *widget, QWidget *parent)
|
|
|
|
{
|
2018-09-21 16:44:45 +03:00
|
|
|
auto findCenter = [childRect = widget->rect()](QRect hostRect) -> QPoint {
|
|
|
|
return QPoint(hostRect.center().x() - (childRect.width() * 0.5),
|
|
|
|
hostRect.center().y() - (childRect.height() * 0.5));
|
|
|
|
};
|
|
|
|
|
2018-09-21 10:55:24 +03:00
|
|
|
if (parent) {
|
2020-01-30 05:45:27 +03:00
|
|
|
widget->move(parent->window()->frameGeometry().topLeft() +
|
|
|
|
parent->window()->rect().center() - widget->rect().center());
|
2018-09-21 10:55:24 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-05 04:20:19 +03:00
|
|
|
// Deprecated in 5.13: widget->move(findCenter(QApplication::desktop()->screenGeometry()));
|
|
|
|
widget->move(findCenter(QGuiApplication::primaryScreen()->geometry()));
|
2018-09-21 10:55:24 +03:00
|
|
|
}
|
2018-10-01 17:56:46 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
utils::restoreCombobox(QComboBox *combo, const QString &value)
|
|
|
|
{
|
|
|
|
for (auto i = 0; i < combo->count(); ++i) {
|
|
|
|
if (value == combo->itemText(i)) {
|
|
|
|
combo->setCurrentIndex(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-26 12:26:51 +03:00
|
|
|
|
|
|
|
QImage
|
|
|
|
utils::readImage(QByteArray *data)
|
|
|
|
{
|
|
|
|
QBuffer buf(data);
|
|
|
|
QImageReader reader(&buf);
|
|
|
|
reader.setAutoTransform(true);
|
|
|
|
return reader.read();
|
|
|
|
}
|