Debian Codegraph @

src/Utils.h

raw symbols SCIP
1 // SPDX-FileCopyrightText: Nheko Contributors
2 //
3 // SPDX-License-Identifier: GPL-3.0-or-later
4
5 #pragma once
6
7 #include <CacheStructs.h>
8 #include <QDateTime>
9 #include <QPixmap>
10 #include <mtx/events.hpp>
11
12 namespace mtx::events::collections {
13 struct TimelineEvents;
14 struct StateEvents;
15 }
16
17 struct DescInfo;
18
19 namespace cache {
20 // Forward declarations to prevent dependency on Cache.h, since this header is included often!
21 QString
22 displayName(const QString &room_id, const QString &user_id);
23 }
24
25 class QComboBox;
26
27 // Contains information about related events for
28 // outgoing messages
29 struct RelatedInfo
30 {
31 using MsgType = mtx::events::MessageType;
32 MsgType type;
33 QString room;
34 QString quoted_body, quoted_formatted_body;
35 std::string related_event;
36 QString quoted_user;
37 };
38
39 namespace utils {
40 //! Helper function to remove reply fallback from body
41 std::string
42 stripReplyFromBody(const std::string &body);
43
44 //! Helper function to remove reply fallback from formatted body
45 std::string
46 stripReplyFromFormattedBody(const std::string &formatted_body);
47
48 RelatedInfo
49 stripReplyFallbacks(const mtx::events::collections::TimelineEvents &event,
50 std::string id,
51 QString room_id_);
52
53 bool
54 codepointIsEmoji(uint code);
55
56 QString
57 replaceEmoji(const QString &body);
58
59 QString
60 localUser();
61
62 float
63 scaleFactor();
64
65 void
66 setScaleFactor(float factor);
67
68 //! Human friendly timestamp representation.
69 QString
70 descriptiveTime(const QDateTime &then);
71
72 //! Generate a message description from the event to be displayed
73 //! in the RoomList.
74 DescInfo
75 getMessageDescription(const mtx::events::collections::TimelineEvents &event,
76 const QString &localUser,
77 const QString &displayName);
78
79 //! Get the first character of a string, taking into account that
80 //! surrogate pairs might be in use.
81 QString
82 firstChar(const QString &input);
83
84 //! Get a human readable file size with the appropriate units attached.
85 QString
86 humanReadableFileSize(uint64_t bytes);
87
88 //! Scale down an image to fit to the given width & height limitations.
89 QPixmap
90 scaleDown(uint64_t maxWidth, uint64_t maxHeight, const QPixmap &source);
91
92 //! Delete items in a container based on a predicate.
93 template<typename ContainerT, typename PredicateT>
94 void
95 erase_if(ContainerT &items, const PredicateT &predicate)
96 {
97 for (auto it = items.begin(); it != items.end();) {
98 if (predicate(*it))
99 it = items.erase(it);
100 else
101 ++it;
102 }
103 }
104
105 //! Calculate the Levenshtein distance between two strings with character skipping.
106 int
107 levenshtein_distance(const std::string &s1, const std::string &s2);
108
109 QPixmap
110 scaleImageToPixmap(const QImage &img, int size);
111
112 //! Convert a Content Matrix URI to an HTTP link.
113 QString
114 mxcToHttp(const QUrl &url, const QString &server, int port);
115
116 //! Convert a ed25519 fingerprint into a human readable form
117 QString
118 humanReadableFingerprint(const std::string &ed25519);
119
120 //! Retrieve the message body taking into account the `formatted_body` field.
121 //! If the `format` of the message is not supported we fallback to `body`.
122 template<typename RoomMessageT>
123 QString
124 getMessageBody(const RoomMessageT &event)
125 {
126 if (event.content.format.empty())
127 return QString::fromStdString(event.content.body).toHtmlEscaped();
128
129 if (event.content.format != mtx::common::FORMAT_MSG_TYPE)
130 return QString::fromStdString(event.content.body).toHtmlEscaped();
131
132 return QString::fromStdString(event.content.formatted_body);
133 }
134
135 //! Replace raw URLs in text with HTML link tags.
136 QString
137 linkifyMessage(const QString &body);
138
139 //! Convert the input markdown text to html.
140 QString
141 markdownToHtml(const QString &text, bool rainbowify = false, bool noExtensions = false);
142
143 QString
144 escapeMentionMarkdown(QString input);
145
146 //! Escape every html tag, that was not whitelisted
147 QString
148 escapeBlacklistedHtml(const QString &data);
149
150 //! Retrieve the color of the links based on the current theme.
151 QString
152 linkColor();
153
154 //! Returns the hash code of the input QString
155 uint32_t
156 hashQString(const QString &input);
157
158 //! Generate a color (matching #RRGGBB) that has an acceptable contrast to background that is based
159 //! on the input string.
160 QColor
161 generateContrastingHexColor(const QString &input, const QColor &background);
162
163 //! Given two luminance values, compute the contrast ratio between them.
164 qreal
165 computeContrast(const qreal &one, const qreal &two);
166
167 //! Compute the luminance of a single color. Based on https://stackoverflow.com/a/9733420
168 qreal
169 luminance(const QColor &col);
170
171 //! Center a widget in relation to another widget.
172 void
173 centerWidget(QWidget *widget, QWindow *parent);
174
175 void
176 restoreCombobox(QComboBox *combo, const QString &value);
177
178 //! Read image respecting exif orientation
179 QImage
180 readImageFromFile(const QString &filename);
181
182 //! Read image respecting exif orientation
183 QImage
184 readImage(const QByteArray &data);
185
186 bool
187 isReply(const mtx::events::collections::TimelineEvents &e);
188
189 void
190 removeDirectFromRoom(QString roomid);
191
192 void
193 markRoomAsDirect(QString roomid, std::vector<RoomMember> members);
194
195 std::vector<std::string>
196 roomVias(const std::string &roomid);
197
198 void
199 updateSpaceVias();
200
201 void
202 removeExpiredEvents();
203
204 QString
205 glitchText(const QString &text);
206
207 QString
208 graduallyGlitchText(const QString &text);
209
210 struct MatrixUriParseResult
211 {
212 QString sigil1, mxid1, sigil2, mxid2, action;
213 std::vector<std::string> vias;
214 };
215
216 std::optional<MatrixUriParseResult>
217 parseMatrixUri(QString uri);
218
219 void
220 markFileAsFromWeb(const QString &file);
221 }