aboutsummaryrefslogtreecommitdiff
path: root/src/base/QXmppThumbnail.cpp
blob: f5ccc89c4d238d1d88e666facc7dd206610be4fd (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
// SPDX-FileCopyrightText: 2022 Linus Jahn <lnj@kaidan.im>
//
// SPDX-License-Identifier: LGPL-2.1-or-later

#include "QXmppThumbnail.h"

#include "QXmppConstants_p.h"

#include <QDomElement>
#include <QMimeDatabase>
#include <QMimeType>
#include <QUrl>
#include <QXmlStreamWriter>

class QXmppThumbnailPrivate : public QSharedData
{
public:
    QString uri;
    QMimeType mediaType;
    std::optional<uint32_t> width;
    std::optional<uint32_t> height;
};

///
/// \class QXmppThumbnail
///
/// Thumbnail from \xep{0264, Jingle Content Thumbnails}.
///
/// \since QXmpp 1.5
///

/// Default constructor
QXmppThumbnail::QXmppThumbnail()
    : d(new QXmppThumbnailPrivate)
{
}
/// Default copy-constructor
QXmppThumbnail::QXmppThumbnail(const QXmppThumbnail &) = default;
/// Default move-constructor
QXmppThumbnail::QXmppThumbnail(QXmppThumbnail &&) noexcept = default;
QXmppThumbnail::~QXmppThumbnail() = default;
/// Default assignment operator
QXmppThumbnail &QXmppThumbnail::operator=(const QXmppThumbnail &) = default;
/// Default move-assignment operator
QXmppThumbnail &QXmppThumbnail::operator=(QXmppThumbnail &&) noexcept = default;

/// Returns the URI with the location for the data (usually a \xep{0231, Bits of Binary} content ID)
const QString &QXmppThumbnail::uri() const
{
    return d->uri;
}

/// Sets the URI with the location for the data (usually a \xep{0231, Bits of Binary} content ID)
void QXmppThumbnail::setUri(const QString &newUri)
{
    d->uri = newUri;
}

/// Returns the MIME type of the thumbnail data.
const QMimeType &QXmppThumbnail::mediaType() const
{
    return d->mediaType;
}

/// Sets the MIME type of the thumbnail data.
void QXmppThumbnail::setMediaType(const QMimeType &newMediaType)
{
    d->mediaType = newMediaType;
}

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

/// Sets the width of the thumbnail image.
void QXmppThumbnail::setWidth(std::optional<uint32_t> newWidth)
{
    d->width = newWidth;
}

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

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

/// \cond
bool QXmppThumbnail::parse(const QDomElement &el)
{
    if (el.tagName() == "thumbnail" && el.namespaceURI() == ns_thumbs) {
        if (!el.hasAttribute("uri")) {
            return false;
        }

        d->uri = el.attribute("uri");
        if (el.hasAttribute("media-type")) {
            d->mediaType = QMimeDatabase().mimeTypeForName(el.attribute(QStringLiteral("media-type")));
        }

        bool success = false;
        if (auto string = el.attribute("width"); !string.isEmpty()) {
            if (auto parsedInt = string.toUInt(&success); success) {
                d->width = parsedInt;
            } else {
                return false;
            }
        }
        if (auto string = el.attribute("height"); !string.isEmpty()) {
            if (auto parsedInt = string.toUInt(&success); success) {
                d->height = parsedInt;
            } else {
                return false;
            }
        }
        return true;
    }
    return false;
}

void QXmppThumbnail::toXml(QXmlStreamWriter *writer) const
{
    writer->writeStartElement(QStringLiteral("thumbnail"));
    writer->writeDefaultNamespace(ns_thumbs);
    writer->writeAttribute(QStringLiteral("uri"), d->uri);
    if (d->mediaType.isValid()) {
        writer->writeAttribute("media-type", d->mediaType.name());
    }
    if (d->width) {
        writer->writeAttribute("width", QString::number(*d->width));
    }
    if (d->height) {
        writer->writeAttribute("height", QString::number(*d->height));
    }
    writer->writeEndElement();
}
/// \endcond