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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
// SPDX-FileCopyrightText: 2017 Kitsune Ral <kitsune-ral@users.sf.net>
// SPDX-License-Identifier: LGPL-2.1-or-later
#pragma once
// This file contains generic event content definitions, applicable to room
// message events as well as other events (e.g., avatars).
#include "filesourceinfo.h"
#include "quotient_export.h"
#include <QtCore/QJsonObject>
#include <QtCore/QMetaType>
#include <QtCore/QMimeType>
#include <QtCore/QSize>
#include <QtCore/QUrl>
class QFileInfo;
namespace Quotient {
namespace EventContent {
//! \brief Base for all content types that can be stored in RoomMessageEvent
//!
//! Each content type class should have a constructor taking
//! a QJsonObject and override fillJson() with an implementation
//! that will fill the target QJsonObject with stored values. It is
//! assumed but not required that a content object can also be created
//! from plain data.
class QUOTIENT_API Base {
public:
explicit Base(QJsonObject o = {}) : originalJson(std::move(o)) {}
virtual ~Base() = default;
QJsonObject toJson() const;
public:
QJsonObject originalJson;
// You can't assign those classes
Base& operator=(const Base&) = delete;
Base& operator=(Base&&) = delete;
protected:
Base(const Base&) = default;
Base(Base&&) = default;
virtual void fillJson(QJsonObject&) const = 0;
};
// The below structures fairly follow CS spec 11.2.1.6. The overall
// set of attributes for each content types is a superset of the spec
// but specific aggregation structure is altered. See doc comments to
// each type for the list of available attributes.
// A quick classes inheritance structure follows (the definitions are
// spread across eventcontent.h and roommessageevent.h):
// UrlBasedContent<InfoT> : InfoT + thumbnail data
// PlayableContent<InfoT> : + duration attribute
// FileInfo
// FileContent = UrlBasedContent<FileInfo>
// AudioContent = PlayableContent<FileInfo>
// ImageInfo : FileInfo + imageSize attribute
// ImageContent = UrlBasedContent<ImageInfo>
// VideoContent = PlayableContent<ImageInfo>
//! \brief Mix-in class representing `info` subobject in content JSON
//!
//! This is one of base classes for content types that deal with files or
//! URLs. It stores the file metadata attributes, such as size, MIME type
//! etc. found in the `content/info` subobject of event JSON payloads.
//! Actual content classes derive from this class _and_ TypedBase that
//! provides a polymorphic interface to access data in the mix-in. FileInfo
//! (as well as ImageInfo, that adds image size to the metadata) is NOT
//! polymorphic and is used in a non-polymorphic way to store thumbnail
//! metadata (in a separate instance), next to the metadata on the file
//! itself.
//!
//! If you need to make a new _content_ (not info) class based on files/URLs
//! take UrlBasedContent as the example, i.e.:
//! 1. Double-inherit from this class (or ImageInfo) and TypedBase.
//! 2. Provide a constructor from QJsonObject that will pass the `info`
//! subobject (not the whole content JSON) down to FileInfo/ImageInfo.
//! 3. Override fillJson() to customise the JSON export logic. Make sure
//! to call toInfoJson() from it to produce the payload for the `info`
//! subobject in the JSON payload.
//!
//! \sa ImageInfo, FileContent, ImageContent, AudioContent, VideoContent,
//! UrlBasedContent
class QUOTIENT_API FileInfo {
public:
FileInfo() = default;
//! \brief Construct from a QFileInfo object
//!
//! \param fi a QFileInfo object referring to an existing file
explicit FileInfo(const QFileInfo& fi);
explicit FileInfo(FileSourceInfo sourceInfo, qint64 payloadSize = -1,
const QMimeType& mimeType = {},
QString originalFilename = {});
//! \brief Construct from a JSON `info` payload
//!
//! Make sure to pass the `info` subobject of content JSON, not the
//! whole JSON content.
FileInfo(FileSourceInfo sourceInfo, const QJsonObject& infoJson,
QString originalFilename = {});
bool isValid() const;
QUrl url() const;
//! \brief Extract media id from the URL
//!
//! This can be used, e.g., to construct a QML-facing image://
//! URI as follows:
//! \code "image://provider/" + info.mediaId() \endcode
QString mediaId() const { return url().authority() + url().path(); }
public:
FileSourceInfo source;
QJsonObject originalInfoJson;
QMimeType mimeType;
qint64 payloadSize = 0;
QString originalName;
};
QUOTIENT_API QJsonObject toInfoJson(const FileInfo& info);
//! \brief A content info class for image/video content types and thumbnails
class QUOTIENT_API ImageInfo : public FileInfo {
public:
ImageInfo() = default;
explicit ImageInfo(const QFileInfo& fi, QSize imageSize = {});
explicit ImageInfo(FileSourceInfo sourceInfo, qint64 fileSize = -1,
const QMimeType& type = {}, QSize imageSize = {},
const QString& originalFilename = {});
ImageInfo(FileSourceInfo sourceInfo, const QJsonObject& infoJson,
const QString& originalFilename = {});
public:
QSize imageSize;
};
QUOTIENT_API QJsonObject toInfoJson(const ImageInfo& info);
//! \brief An auxiliary class for an info type that carries a thumbnail
//!
//! This class saves/loads a thumbnail to/from `info` subobject of
//! the JSON representation of event content; namely, `info/thumbnail_url`
//! (or, in case of an encrypted thumbnail, `info/thumbnail_file`) and
//! `info/thumbnail_info` fields are used.
class QUOTIENT_API Thumbnail : public ImageInfo {
public:
using ImageInfo::ImageInfo;
explicit Thumbnail(const QJsonObject& infoJson,
const Omittable<EncryptedFileMetadata>& efm = none);
//! \brief Add thumbnail information to the passed `info` JSON object
void dumpTo(QJsonObject& infoJson) const;
};
class QUOTIENT_API TypedBase : public Base {
public:
virtual QMimeType type() const = 0;
virtual const FileInfo* fileInfo() const { return nullptr; }
virtual FileInfo* fileInfo() { return nullptr; }
virtual const Thumbnail* thumbnailInfo() const { return nullptr; }
protected:
explicit TypedBase(QJsonObject o = {}) : Base(std::move(o)) {}
using Base::Base;
};
//! \brief A template class for content types with a URL and additional info
//!
//! Types that derive from this class template take `url` (or, if the file
//! is encrypted, `file`) and, optionally, `filename` values from
//! the top-level JSON object and the rest of information from the `info`
//! subobject, as defined by the parameter type.
//! \tparam InfoT base info class - FileInfo or ImageInfo
template <class InfoT>
class UrlBasedContent : public TypedBase, public InfoT {
public:
using InfoT::InfoT;
explicit UrlBasedContent(const QJsonObject& json)
: TypedBase(json)
, InfoT(QUrl(json["url"].toString()), json["info"].toObject(),
json["filename"].toString())
, thumbnail(FileInfo::originalInfoJson)
{
if (const auto efmJson = json.value("file"_ls).toObject();
!efmJson.isEmpty())
InfoT::source = fromJson<EncryptedFileMetadata>(efmJson);
// Two small hacks on originalJson to expose mediaIds to QML
originalJson.insert("mediaId", InfoT::mediaId());
originalJson.insert("thumbnailMediaId", thumbnail.mediaId());
}
QMimeType type() const override { return InfoT::mimeType; }
const FileInfo* fileInfo() const override { return this; }
FileInfo* fileInfo() override { return this; }
const Thumbnail* thumbnailInfo() const override { return &thumbnail; }
public:
Thumbnail thumbnail;
protected:
virtual void fillInfoJson(QJsonObject& infoJson [[maybe_unused]]) const
{}
void fillJson(QJsonObject& json) const override
{
Quotient::fillJson(json, { "url"_ls, "file"_ls }, InfoT::source);
if (!InfoT::originalName.isEmpty())
json.insert("filename", InfoT::originalName);
auto infoJson = toInfoJson(*this);
if (thumbnail.isValid())
thumbnail.dumpTo(infoJson);
fillInfoJson(infoJson);
json.insert("info", infoJson);
}
};
//! \brief Content class for m.image
//!
//! Available fields:
//! - corresponding to the top-level JSON:
//! - source (corresponding to `url` or `file` in JSON)
//! - filename (extension to the spec)
//! - corresponding to the `info` subobject:
//! - payloadSize (`size` in JSON)
//! - mimeType (`mimetype` in JSON)
//! - imageSize (QSize for a combination of `h` and `w` in JSON)
//! - thumbnail.url (`thumbnail_url` in JSON)
//! - corresponding to the `info/thumbnail_info` subobject: contents of
//! thumbnail field, in the same vein as for the main image:
//! - payloadSize
//! - mimeType
//! - imageSize
using ImageContent = UrlBasedContent<ImageInfo>;
//! \brief Content class for m.file
//!
//! Available fields:
//! - corresponding to the top-level JSON:
//! - source (corresponding to `url` or `file` in JSON)
//! - filename
//! - corresponding to the `info` subobject:
//! - payloadSize (`size` in JSON)
//! - mimeType (`mimetype` in JSON)
//! - thumbnail.source (`thumbnail_url` or `thumbnail_file` in JSON)
//! - corresponding to the `info/thumbnail_info` subobject:
//! - thumbnail.payloadSize
//! - thumbnail.mimeType
//! - thumbnail.imageSize (QSize for `h` and `w` in JSON)
using FileContent = UrlBasedContent<FileInfo>;
} // namespace EventContent
} // namespace Quotient
Q_DECLARE_METATYPE(const Quotient::EventContent::TypedBase*)
|