aboutsummaryrefslogtreecommitdiff
path: root/src/base/QXmppFileMetadata.cpp
blob: 08b9f0858ed3675e05983f5373c19c9e753c1b8f (plain) (blame)
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// SPDX-FileCopyrightText: 2022 Jonah Brüchert <jbb@kaidan.im>
//
// SPDX-License-Identifier: LGPL-2.1-or-later

#include "QXmppFileMetadata.h"

#include "QXmppConstants_p.h"
#include "QXmppHash.h"
#include "QXmppThumbnail.h"
#include "QXmppUtils.h"

#include <utility>

#include <QDateTime>
#include <QDomElement>
#include <QFileInfo>
#include <QMimeDatabase>

class QXmppFileMetadataPrivate : public QSharedData
{
public:
    std::optional<QDateTime> date;
    std::optional<QString> desc;
    QVector<QXmppHash> hashes;
    std::optional<uint32_t> height;
    std::optional<uint32_t> length;
    std::optional<QMimeType> mediaType;
    std::optional<QString> name;
    std::optional<uint64_t> size;
    QVector<QXmppThumbnail> thumbnails;
    std::optional<uint32_t> width;
};

///
/// \class QXmppFileMetadata
///
/// File metadata from \xep{0446, File metadata element}.
///
/// \since QXmpp 1.5
///

///
/// \brief Creates a QXmppFileMetadata object from information from QFileInfo.
///
/// Sets the filename, file size, media type and the last modification date.
///
QXmppFileMetadata QXmppFileMetadata::fromFileInfo(const QFileInfo &info)
{
    QXmppFileMetadata metadata;
    metadata.setFilename(info.fileName());
    metadata.setSize(info.size());
    metadata.setMediaType(QMimeDatabase().mimeTypeForFile(info));
    metadata.setLastModified(info.lastModified());
    return metadata;
}

QXmppFileMetadata::QXmppFileMetadata()
    : d(new QXmppFileMetadataPrivate())
{
}

QXMPP_PRIVATE_DEFINE_RULE_OF_SIX(QXmppFileMetadata)

/// \cond
QVector<QDomElement> allChildElements(const QDomElement &el, const QString &name)
{
    QVector<QDomElement> out;

    for (auto childEl = el.firstChildElement(name); !childEl.isNull(); childEl = childEl.nextSiblingElement(name)) {
        out.push_back(childEl);
    }

    return out;
}

template<typename Func>
QVector<std::invoke_result_t<Func, QDomElement>> forAllChildElements(const QDomElement &el, const QString &name, Func func)
{
    auto elements = allChildElements(el, name);
    QVector<std::invoke_result_t<Func, QDomElement>> out;
    std::transform(elements.begin(), elements.end(), std::back_inserter(out), func);
    return out;
}

bool QXmppFileMetadata::parse(const QDomElement &el)
{
    if (el.isNull()) {
        return false;
    }

    if (auto dateEl = el.firstChildElement("date"); !dateEl.isNull()) {
        d->date = QXmppUtils::datetimeFromString(dateEl.text());
    }

    if (auto descEl = el.firstChildElement("desc"); !descEl.isNull()) {
        d->desc = descEl.text();
    }

    d->hashes = forAllChildElements(el, "hash", [](const auto &el) {
        QXmppHash hash;
        hash.parse(el);
        return hash;
    });

    if (auto heightEl = el.firstChildElement("height"); !heightEl.isNull()) {
        d->height = el.firstChildElement("height").text().toUInt();
    }
    if (auto lengthEl = el.firstChildElement("length"); !lengthEl.isNull()) {
        d->length = lengthEl.text().toUInt();
    }
    if (auto mediaTypeEl = el.firstChildElement("media-type"); !mediaTypeEl.isNull()) {
        d->mediaType = QMimeDatabase().mimeTypeForName(mediaTypeEl.text());
    }
    if (auto nameEl = el.firstChildElement("name"); !nameEl.isNull()) {
        d->name = nameEl.text();
    }
    if (auto sizeEl = el.firstChildElement("size"); !sizeEl.isNull()) {
        d->size = sizeEl.text().toULong();
    }
    for (auto thumbEl = el.firstChildElement("thumbnail");
         !thumbEl.isNull();
         thumbEl = thumbEl.nextSiblingElement("thumbnail")) {
        QXmppThumbnail thumbnail;
        if (thumbnail.parse(thumbEl)) {
            d->thumbnails.append(std::move(thumbnail));
        }
    }
    if (auto widthEl = el.firstChildElement("width"); !widthEl.isNull()) {
        d->width = widthEl.text().toUInt();
    }
    return true;
}

void QXmppFileMetadata::toXml(QXmlStreamWriter *writer) const
{
    writer->writeStartElement("file");
    writer->writeDefaultNamespace(ns_file_metadata);
    if (d->date) {
        writer->writeTextElement("date", QXmppUtils::datetimeToString(*d->date));
    }

    if (d->desc) {
        writer->writeTextElement("desc", *d->desc);
    }

    for (const auto &hash : d->hashes) {
        hash.toXml(writer);
    }

    if (d->height) {
        writer->writeTextElement("height", QString::number(*d->height));
    }
    if (d->length) {
        writer->writeTextElement("length", QString::number(*d->length));
    }
    if (d->mediaType) {
        writer->writeTextElement("media-type", d->mediaType->name());
    }
    if (d->name) {
        writer->writeTextElement("name", *d->name);
    }
    if (d->size) {
        writer->writeTextElement("size", QString::number(*d->size));
    }
    for (const auto &thumbnail : d->thumbnails) {
        thumbnail.toXml(writer);
    }
    if (d->width) {
        writer->writeTextElement("width", QString::number(*d->width));
    }
    writer->writeEndElement();
}
/// \endcond

/// Returns when the file was last modified
const std::optional<QDateTime> &QXmppFileMetadata::lastModified() const
{
    return d->date;
}

/// Sets when the file was last modified
void QXmppFileMetadata::setLastModified(const std::optional<QDateTime> &date)
{
    d->date = date;
}

/// Returns the description of the file
const std::optional<QString> &QXmppFileMetadata::description() const
{
    return d->desc;
}

/// Sets the description of the file
void QXmppFileMetadata::setDescription(const std::optional<QString> &description)
{
    d->desc = description;
}

/// Returns the hashes of the file
const QVector<QXmppHash> &QXmppFileMetadata::hashes() const
{
    return d->hashes;
}

/// Sets the hashes of the file
void QXmppFileMetadata::setHashes(const QVector<QXmppHash> &hashes)
{
    d->hashes = hashes;
}

/// Returns the height of the image
std::optional<uint32_t> QXmppFileMetadata::height() const
{
    return d->height;
}

/// Sets the height of the image
void QXmppFileMetadata::setHeight(std::optional<uint32_t> height)
{
    d->height = height;
}

/// Returns the length of a video or audio file
std::optional<uint32_t> QXmppFileMetadata::length() const
{
    return d->length;
}

/// Sets the length of a video or audio file
void QXmppFileMetadata::setLength(std::optional<uint32_t> length)
{
    d->length = length;
}

/// Returns the media type of the file
const std::optional<QMimeType> &QXmppFileMetadata::mediaType() const
{
    return d->mediaType;
}

/// Sets the media type of the file
void QXmppFileMetadata::setMediaType(std::optional<QMimeType> mediaType)
{
    d->mediaType = std::move(mediaType);
}

/// Returns the filename
std::optional<QString> QXmppFileMetadata::filename() const
{
    return d->name;
}

/// Sets the filename
void QXmppFileMetadata::setFilename(std::optional<QString> name)
{
    d->name = std::move(name);
}

/// Returns the size of the file in bytes
std::optional<uint64_t> QXmppFileMetadata::size() const
{
    return d->size;
}

/// Sets the size of the file in bytes
void QXmppFileMetadata::setSize(std::optional<uint64_t> size)
{
    d->size = size;
}

/// Returns the thumbnail references.
const QVector<QXmppThumbnail> &QXmppFileMetadata::thumbnails() const
{
    return d->thumbnails;
}

/// Sets the thumbnail references.
void QXmppFileMetadata::setThumbnails(const QVector<QXmppThumbnail> &thumbnail)
{
    d->thumbnails = thumbnail;
}

/// Returns the width of the image or video.
std::optional<uint32_t> QXmppFileMetadata::width() const
{
    return d->width;
}

/// Sets the width of the image or video.
void QXmppFileMetadata::setWidth(std::optional<uint32_t> width)
{
    d->width = width;
}