2018-01-05 01:27:32 +03:00
|
|
|
#include "Utils.h"
|
|
|
|
|
2018-05-11 16:00:14 +03:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QDesktopWidget>
|
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>
|
2018-07-30 12:35:15 +03:00
|
|
|
#include <cmath>
|
2018-05-11 16:00:14 +03:00
|
|
|
|
2018-09-01 13:35:10 +03:00
|
|
|
#include <boost/variant.hpp>
|
2018-09-11 14:56:09 +03:00
|
|
|
#include <cmark.h>
|
2018-01-05 01:27:32 +03:00
|
|
|
|
2018-09-07 14:52:29 +03:00
|
|
|
#include "Config.h"
|
|
|
|
|
2018-01-05 01:27:32 +03:00
|
|
|
using TimelineEvent = mtx::events::collections::TimelineEvents;
|
|
|
|
|
2018-07-20 16:15:50 +03:00
|
|
|
QString
|
|
|
|
utils::localUser()
|
|
|
|
{
|
|
|
|
QSettings settings;
|
|
|
|
return settings.value("auth/user_id").toString();
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
QSettings settings("nheko", "nheko");
|
|
|
|
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)
|
|
|
|
return then.toString("HH:mm");
|
|
|
|
else if (days < 2)
|
|
|
|
return QString("Yesterday");
|
|
|
|
else if (days < 365)
|
|
|
|
return then.toString("dd/MM");
|
|
|
|
|
|
|
|
return then.toString("dd/MM/yy");
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2018-06-28 16:16:43 +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 Encrypted = mtx::events::EncryptedEvent<mtx::events::msg::Encrypted>;
|
2018-01-05 01:27:32 +03:00
|
|
|
|
2018-09-01 13:35:10 +03:00
|
|
|
if (boost::get<Audio>(&event) != nullptr) {
|
2018-04-29 15:42:40 +03:00
|
|
|
return createDescriptionInfo<Audio>(event, localUser, room_id);
|
2018-09-01 13:35:10 +03:00
|
|
|
} else if (boost::get<Emote>(&event) != nullptr) {
|
2018-04-29 15:42:40 +03:00
|
|
|
return createDescriptionInfo<Emote>(event, localUser, room_id);
|
2018-09-01 13:35:10 +03:00
|
|
|
} else if (boost::get<File>(&event) != nullptr) {
|
2018-04-29 15:42:40 +03:00
|
|
|
return createDescriptionInfo<File>(event, localUser, room_id);
|
2018-09-01 13:35:10 +03:00
|
|
|
} else if (boost::get<Image>(&event) != nullptr) {
|
2018-04-29 15:42:40 +03:00
|
|
|
return createDescriptionInfo<Image>(event, localUser, room_id);
|
2018-09-01 13:35:10 +03:00
|
|
|
} else if (boost::get<Notice>(&event) != nullptr) {
|
2018-04-29 15:42:40 +03:00
|
|
|
return createDescriptionInfo<Notice>(event, localUser, room_id);
|
2018-09-01 13:35:10 +03:00
|
|
|
} else if (boost::get<Text>(&event) != nullptr) {
|
2018-04-29 15:42:40 +03:00
|
|
|
return createDescriptionInfo<Text>(event, localUser, room_id);
|
2018-09-01 13:35:10 +03:00
|
|
|
} else if (boost::get<Video>(&event) != nullptr) {
|
2018-04-29 15:42:40 +03:00
|
|
|
return createDescriptionInfo<Video>(event, localUser, room_id);
|
2018-09-01 13:35:10 +03:00
|
|
|
} else if (boost::get<mtx::events::Sticker>(&event) != nullptr) {
|
2018-04-29 15:42:40 +03:00
|
|
|
return createDescriptionInfo<mtx::events::Sticker>(event, localUser, room_id);
|
2018-09-01 13:35:10 +03:00
|
|
|
} else if (boost::get<Encrypted>(&event) != nullptr) {
|
|
|
|
const auto msg = boost::get<Encrypted>(event);
|
2018-06-28 16:16:43 +03:00
|
|
|
const auto sender = QString::fromStdString(msg.sender);
|
|
|
|
|
|
|
|
const auto username = Cache::displayName(room_id, sender);
|
|
|
|
const auto ts = QDateTime::fromMSecsSinceEpoch(msg.origin_server_ts);
|
|
|
|
|
|
|
|
DescInfo info;
|
|
|
|
if (sender == localUser)
|
|
|
|
info.username = "You";
|
|
|
|
else
|
|
|
|
info.username = username;
|
|
|
|
|
|
|
|
info.userid = sender;
|
|
|
|
info.body = QString(" %1").arg(messageDescription<Encrypted>());
|
|
|
|
info.timestamp = utils::descriptiveTime(ts);
|
|
|
|
info.datetime = ts;
|
|
|
|
|
|
|
|
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
|
|
|
|
utils::event_body(const mtx::events::collections::TimelineEvents &event)
|
|
|
|
{
|
|
|
|
using namespace mtx::events;
|
|
|
|
using namespace mtx::events::msg;
|
|
|
|
|
2018-09-01 13:35:10 +03:00
|
|
|
if (boost::get<RoomEvent<Audio>>(&event) != nullptr) {
|
2018-05-05 16:38:41 +03:00
|
|
|
return message_body<RoomEvent<Audio>>(event);
|
2018-09-01 13:35:10 +03:00
|
|
|
} else if (boost::get<RoomEvent<Emote>>(&event) != nullptr) {
|
2018-05-05 16:38:41 +03:00
|
|
|
return message_body<RoomEvent<Emote>>(event);
|
2018-09-01 13:35:10 +03:00
|
|
|
} else if (boost::get<RoomEvent<File>>(&event) != nullptr) {
|
2018-05-05 16:38:41 +03:00
|
|
|
return message_body<RoomEvent<File>>(event);
|
2018-09-01 13:35:10 +03:00
|
|
|
} else if (boost::get<RoomEvent<Image>>(&event) != nullptr) {
|
2018-05-05 16:38:41 +03:00
|
|
|
return message_body<RoomEvent<Image>>(event);
|
2018-09-01 13:35:10 +03:00
|
|
|
} else if (boost::get<RoomEvent<Notice>>(&event) != nullptr) {
|
2018-05-05 16:38:41 +03:00
|
|
|
return message_body<RoomEvent<Notice>>(event);
|
2018-09-01 13:35:10 +03:00
|
|
|
} else if (boost::get<Sticker>(&event) != nullptr) {
|
2018-05-05 16:38:41 +03:00
|
|
|
return message_body<Sticker>(event);
|
2018-09-01 13:35:10 +03:00
|
|
|
} else if (boost::get<RoomEvent<Text>>(&event) != nullptr) {
|
2018-05-05 16:38:41 +03:00
|
|
|
return message_body<RoomEvent<Text>>(event);
|
2018-09-01 13:35:10 +03:00
|
|
|
} else if (boost::get<RoomEvent<Video>>(&event) != nullptr) {
|
2018-05-05 16:38:41 +03:00
|
|
|
return message_body<RoomEvent<Video>>(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
return QString();
|
|
|
|
}
|
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();
|
|
|
|
|
2018-07-30 12:35:15 +03:00
|
|
|
const double sz =
|
2018-08-01 21:10:03 +03:00
|
|
|
std::ceil(QApplication::desktop()->screen()->devicePixelRatioF() * (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.
|
|
|
|
auto doc = QString("<html>%1</html>").arg(body);
|
|
|
|
|
|
|
|
doc.replace("<mx-reply>", "");
|
|
|
|
doc.replace("</mx-reply>", "");
|
|
|
|
doc.replace("<br>", "<br></br>");
|
|
|
|
|
|
|
|
QXmlStreamReader xml{doc};
|
2018-09-07 14:52:29 +03:00
|
|
|
|
|
|
|
QString textString;
|
|
|
|
while (!xml.atEnd() && !xml.hasError()) {
|
|
|
|
auto t = xml.readNext();
|
|
|
|
|
|
|
|
switch (t) {
|
|
|
|
case QXmlStreamReader::Characters: {
|
|
|
|
auto text = xml.text().toString();
|
|
|
|
text.replace(conf::strings::url_regex, conf::strings::url_html);
|
|
|
|
|
|
|
|
textString += text;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QXmlStreamReader::StartDocument:
|
|
|
|
case QXmlStreamReader::EndDocument:
|
|
|
|
break;
|
|
|
|
case QXmlStreamReader::StartElement: {
|
|
|
|
if (xml.name() == "html")
|
|
|
|
break;
|
|
|
|
|
2018-09-12 13:40:42 +03:00
|
|
|
textString += "<" + xml.name();
|
|
|
|
|
|
|
|
const auto attrs = xml.attributes();
|
|
|
|
for (const auto &e : attrs)
|
|
|
|
textString += QString(" %1=\"%2\"").arg(e.name()).arg(e.value());
|
|
|
|
|
|
|
|
textString += ">";
|
|
|
|
|
2018-09-07 14:52:29 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QXmlStreamReader::EndElement: {
|
|
|
|
if (xml.name() == "html")
|
|
|
|
break;
|
|
|
|
|
|
|
|
textString += "</" + xml.name() + ">";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xml.hasError()) {
|
2018-09-13 16:10:45 +03:00
|
|
|
qWarning() << "error while parsing xml" << xml.errorString() << doc;
|
|
|
|
doc.replace("<html>", "");
|
|
|
|
doc.replace("</html>", "");
|
|
|
|
return doc;
|
2018-09-07 14:52:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return textString;
|
|
|
|
}
|
2018-09-07 20:05:30 +03:00
|
|
|
|
2018-09-12 14:20:12 +03:00
|
|
|
QString
|
|
|
|
utils::markdownToHtml(const QString &text)
|
2018-09-07 20:05:30 +03:00
|
|
|
{
|
2018-09-12 14:20:12 +03:00
|
|
|
const auto str = text.toUtf8();
|
|
|
|
const char *tmp_buf =
|
|
|
|
cmark_markdown_to_html(str.constData(), str.size(), CMARK_OPT_DEFAULT);
|
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);
|
|
|
|
|
2018-09-13 11:02:54 +03:00
|
|
|
auto result = QString::fromStdString(html).trimmed();
|
2018-09-07 20:05:30 +03:00
|
|
|
|
2018-09-13 11:02:54 +03:00
|
|
|
// Strip paragraph tags.
|
|
|
|
result.replace("<p>", "");
|
|
|
|
result.replace("</p>", "");
|
|
|
|
|
|
|
|
return result;
|
2018-09-07 20:05:30 +03:00
|
|
|
}
|