1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
#include "Utils.h"
#include "Cache.h"
#include <variant.hpp>
using TimelineEvent = mtx::events::collections::TimelineEvents;
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
utils::getMessageDescription(const TimelineEvent &event,
const QString &localUser,
const QString &room_id)
{
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>;
if (mpark::holds_alternative<Audio>(event)) {
const auto msg = mpark::get<Audio>(event);
QString sender = QString::fromStdString(msg.sender);
const auto username = Cache::displayName(room_id, sender);
const auto ts = QDateTime::fromMSecsSinceEpoch(msg.origin_server_ts);
return DescInfo{sender == localUser ? "You" : username,
sender,
" sent an audio clip",
utils::descriptiveTime(ts),
ts};
} else if (mpark::holds_alternative<Emote>(event)) {
auto msg = mpark::get<Emote>(event);
QString sender = QString::fromStdString(msg.sender);
const auto username = Cache::displayName(room_id, sender);
const auto ts = QDateTime::fromMSecsSinceEpoch(msg.origin_server_ts);
const auto body = QString::fromStdString(msg.content.body).trimmed();
return DescInfo{"",
sender,
QString("* %1 %2").arg(username).arg(body),
utils::descriptiveTime(ts),
ts};
} else if (mpark::holds_alternative<File>(event)) {
const auto msg = mpark::get<File>(event);
QString sender = QString::fromStdString(msg.sender);
const auto username = Cache::displayName(room_id, sender);
const auto ts = QDateTime::fromMSecsSinceEpoch(msg.origin_server_ts);
return DescInfo{sender == localUser ? "You" : username,
sender,
" sent a file",
utils::descriptiveTime(ts),
ts};
} else if (mpark::holds_alternative<Image>(event)) {
const auto msg = mpark::get<Image>(event);
QString sender = QString::fromStdString(msg.sender);
const auto username = Cache::displayName(room_id, sender);
const auto ts = QDateTime::fromMSecsSinceEpoch(msg.origin_server_ts);
return DescInfo{sender == localUser ? "You" : username,
sender,
" sent an image",
utils::descriptiveTime(ts),
ts};
} else if (mpark::holds_alternative<Notice>(event)) {
const auto msg = mpark::get<Notice>(event);
QString sender = QString::fromStdString(msg.sender);
const auto username = Cache::displayName(room_id, sender);
const auto ts = QDateTime::fromMSecsSinceEpoch(msg.origin_server_ts);
return DescInfo{
username, sender, " sent a notification", utils::descriptiveTime(ts), ts};
} else if (mpark::holds_alternative<Text>(event)) {
const auto msg = mpark::get<Text>(event);
QString sender = QString::fromStdString(msg.sender);
const auto username = Cache::displayName(room_id, sender);
const auto ts = QDateTime::fromMSecsSinceEpoch(msg.origin_server_ts);
const auto body = QString::fromStdString(msg.content.body).trimmed();
return DescInfo{sender == localUser ? "You" : username,
sender,
QString(": %1").arg(body),
utils::descriptiveTime(ts),
ts};
} else if (mpark::holds_alternative<Video>(event)) {
const auto msg = mpark::get<Video>(event);
QString sender = QString::fromStdString(msg.sender);
const auto username = Cache::displayName(room_id, sender);
const auto ts = QDateTime::fromMSecsSinceEpoch(msg.origin_server_ts);
return DescInfo{sender == localUser ? "You" : username,
sender,
" sent a video clip",
utils::descriptiveTime(ts),
ts};
} else if (mpark::holds_alternative<mtx::events::Sticker>(event)) {
const auto msg = mpark::get<mtx::events::Sticker>(event);
QString sender = QString::fromStdString(msg.sender);
const auto username = Cache::displayName(room_id, sender);
const auto ts = QDateTime::fromMSecsSinceEpoch(msg.origin_server_ts);
return DescInfo{sender == localUser ? "You" : username,
sender,
" sent a sticker",
utils::descriptiveTime(ts),
ts};
}
return DescInfo{};
}
QString
utils::firstChar(const QString &input)
{
if (input.isEmpty())
return input;
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();
}
QString
utils::humanReadableFileSize(uint64_t bytes)
{
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];
}
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());
}
|