11 #include <QtCore/QJsonObject>
12 #include <QtCore/QMetaType>
13 #include <QtCore/QMimeType>
14 #include <QtCore/QSize>
15 #include <QtCore/QUrl>
20 constexpr inline auto BodyKey =
"body"_L1;
21 constexpr inline auto FormatKey =
"format"_L1;
22 constexpr inline auto FormattedBodyKey =
"formatted_body"_L1;
23 constexpr inline auto HtmlContentTypeId =
"org.matrix.custom.html"_L1;
24 constexpr inline auto InfoKey =
"info"_L1;
27 namespace Quotient::EventContent {
37 explicit Base(QJsonObject o = {}) : originalJson(std::move(o)) {}
38 virtual ~Base() =
default;
40 QJsonObject toJson()
const;
42 virtual QMimeType type()
const = 0;
45 QJsonObject originalJson;
48 Base& operator=(
const Base&) =
delete;
49 Base& operator=(Base&&) =
delete;
52 Base(
const Base&) =
default;
53 Base(Base&&)
noexcept =
default;
55 virtual void fillJson(QJsonObject&)
const = 0;
96 explicit FileInfo(
const QFileInfo& fi);
97 explicit FileInfo(FileSourceInfo sourceInfo, qint64 payloadSize = -1,
98 const QMimeType& mimeType = {},
99 QString originalFilename = {});
104 FileInfo(FileSourceInfo sourceInfo,
const QJsonObject& infoJson,
105 QString originalFilename = {});
107 bool isValid()
const;
115 QString mediaId()
const {
return url().authority() + url().path(); }
117 FileSourceInfo source;
118 QJsonObject originalInfoJson;
120 qint64 payloadSize = 0;
121 QString originalName;
128 ImageInfo() =
default;
129 explicit ImageInfo(
const QFileInfo& fi, QSize imageSize = {});
130 explicit ImageInfo(FileSourceInfo sourceInfo, qint64 fileSize = -1,
131 const QMimeType& type = {}, QSize imageSize = {},
132 const QString& originalFilename = {});
133 ImageInfo(FileSourceInfo sourceInfo,
const QJsonObject& infoJson,
134 const QString& originalFilename = {});
148 using ImageInfo::ImageInfo;
149 explicit Thumbnail(
const QJsonObject& infoJson);
152 void dumpTo(QJsonObject& infoJson)
const;
158 explicit FileContentBase(
const QJsonObject& contentJson = {})
159 : Base(contentJson), thumbnail(contentJson[InfoKey].toObject())
161 virtual QUrl url()
const = 0;
162 virtual FileInfo commonInfo()
const = 0;
167 virtual QJsonObject makeInfoJson()
const = 0;
169 void fillJson(QJsonObject& json)
const override
171 const auto fileInfo = commonInfo();
172 Quotient::fillJson(json, {
"url"_L1,
"file"_L1 }, fileInfo.source);
173 addParam<IfNotEmpty>(json,
"filename"_L1, fileInfo.originalName);
174 auto infoJson = makeInfoJson();
175 if (thumbnail.isValid())
176 thumbnail.dumpTo(infoJson);
177 json.insert(InfoKey, infoJson);
187 TextContent(QString text,
const QString& contentType);
188 explicit TextContent(
const QJsonObject& json);
190 QMimeType type()
const override {
return mimeType; }
196 void fillJson(QJsonObject& json)
const override;
212 LocationContent(
const QString& geoUri,
const Thumbnail& thumbnail = {});
213 explicit LocationContent(
const QJsonObject& json);
215 QMimeType type()
const override;
222 void fillJson(QJsonObject& o)
const override;
232 template <std::derived_from<FileInfo> InfoT>
233 class UrlBasedContent :
public FileContentBase,
public InfoT {
236 explicit UrlBasedContent(
const QJsonObject& json)
237 : FileContentBase(json)
238 , InfoT(fileSourceInfoFromJson(json, {
"url"_L1,
"file"_L1 }), json[InfoKey].toObject(),
239 json[
"filename"_L1].toString())
242 originalJson.insert(
"mediaId"_L1, InfoT::mediaId());
243 originalJson.insert(
"thumbnailMediaId"_L1, thumbnail.mediaId());
246 QMimeType type()
const override {
return InfoT::mimeType; }
247 QUrl url()
const override {
return InfoT::url(); }
248 FileInfo commonInfo()
const override {
return SLICE(*
this, FileInfo); }
251 QJsonObject makeInfoJson()
const override {
return toInfoJson(*
this); }
270 using ImageContent = UrlBasedContent<ImageInfo>;
286 using FileContent = UrlBasedContent<FileInfo>;
289 template <
typename InfoT>
290 class PlayableContent :
public UrlBasedContent<InfoT> {
292 using UrlBasedContent<InfoT>::UrlBasedContent;
293 explicit PlayableContent(
const QJsonObject& json)
294 : UrlBasedContent<InfoT>(json), duration(FileInfo::originalInfoJson[
"duration"_L1].toInt())
298 QJsonObject makeInfoJson()
const override
300 auto infoJson = UrlBasedContent<InfoT>::makeInfoJson();
301 infoJson.insert(
"duration"_L1, duration);
326 using VideoContent = PlayableContent<ImageInfo>;
344 using AudioContent = PlayableContent<FileInfo>;
346 Q_DECLARE_METATYPE(
const Quotient::EventContent::Base*)