|
| 1 | +// SPDX-FileCopyrightText: 2022 Jonah Brüchert <[email protected]> |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 | + |
| 5 | +#include "QXmppStickerPackItem.h" |
| 6 | + |
| 7 | +#include "QXmppConstants_p.h" |
| 8 | +#include "QXmppEncryptedFileSource.h" |
| 9 | +#include "QXmppFileMetadata.h" |
| 10 | + |
| 11 | +#include <QXmlStreamWriter> |
| 12 | + |
| 13 | +class QXmppStickerItemPrivate : public QSharedData |
| 14 | +{ |
| 15 | +public: |
| 16 | + QXmppFileMetadata metadata; |
| 17 | + QVector<QXmppHttpFileSource> httpSources; |
| 18 | + QVector<QXmppEncryptedFileSource> encryptedSources; |
| 19 | +}; |
| 20 | + |
| 21 | +QXmppStickerItem::QXmppStickerItem() |
| 22 | + : d(new QXmppStickerItemPrivate()) |
| 23 | +{ |
| 24 | +} |
| 25 | + |
| 26 | +/// |
| 27 | +/// \class QXmppStickerItem |
| 28 | +/// |
| 29 | +/// This class represents a single sticker when publishing or retrieving it. |
| 30 | +/// |
| 31 | + |
| 32 | +/// |
| 33 | +/// \brief Returns metadata about the sticker file |
| 34 | +/// |
| 35 | +const QXmppFileMetadata &QXmppStickerItem::metadata() const |
| 36 | +{ |
| 37 | + return d->metadata; |
| 38 | +} |
| 39 | + |
| 40 | +/// |
| 41 | +/// \brief Sets metadata of this sticker file |
| 42 | +/// |
| 43 | +void QXmppStickerItem::setMetadata(const QXmppFileMetadata &metadata) |
| 44 | +{ |
| 45 | + d->metadata = metadata; |
| 46 | +} |
| 47 | + |
| 48 | +/// |
| 49 | +/// \brief Returns HTTP sources for the sticker file |
| 50 | +/// |
| 51 | +const QVector<QXmppHttpFileSource> &QXmppStickerItem::httpSource() const |
| 52 | +{ |
| 53 | + return d->httpSources; |
| 54 | +} |
| 55 | + |
| 56 | +/// |
| 57 | +/// \brief Sets the list of HTTP sources for this sticker file |
| 58 | +/// |
| 59 | +void QXmppStickerItem::setHttpSources(const QVector<QXmppHttpFileSource> &httpSources) |
| 60 | +{ |
| 61 | + d->httpSources = httpSources; |
| 62 | +} |
| 63 | + |
| 64 | +/// |
| 65 | +/// \brief Returns the list of encrypted sources for this sticker file |
| 66 | +/// |
| 67 | +const QVector<QXmppEncryptedFileSource> &QXmppStickerItem::encryptedSources() const |
| 68 | +{ |
| 69 | + return d->encryptedSources; |
| 70 | +} |
| 71 | + |
| 72 | +/// |
| 73 | +/// \brief Set the list of encrypted sources for this sticker file |
| 74 | +/// |
| 75 | +void QXmppStickerItem::setEncryptedSources(const QVector<QXmppEncryptedFileSource> &encryptedSources) |
| 76 | +{ |
| 77 | + d->encryptedSources = encryptedSources; |
| 78 | +} |
| 79 | + |
| 80 | +QXMPP_PRIVATE_DEFINE_RULE_OF_SIX(QXmppStickerItem) |
| 81 | + |
| 82 | +/// \cond |
| 83 | +void QXmppStickerItem::toXml(QXmlStreamWriter *writer) const |
| 84 | +{ |
| 85 | + writer->writeStartElement("item"); |
| 86 | + d->metadata.toXml(writer); |
| 87 | + for (const auto &httpSource : d->httpSources) { |
| 88 | + httpSource.toXml(writer); |
| 89 | + } |
| 90 | + for (const auto &encryptedSource : d->encryptedSources) { |
| 91 | + encryptedSource.toXml(writer); |
| 92 | + } |
| 93 | + writer->writeEndElement(); |
| 94 | +} |
| 95 | + |
| 96 | +bool QXmppStickerItem::parse(const QDomElement &element) |
| 97 | +{ |
| 98 | + auto fileElement = element.firstChildElement("file"); |
| 99 | + d->metadata.parse(fileElement); |
| 100 | + |
| 101 | + auto sources = element.firstChildElement("sources"); |
| 102 | + for (auto sourceEl = sources.firstChildElement(); |
| 103 | + !sourceEl.isNull(); |
| 104 | + sourceEl = sourceEl.nextSiblingElement()) { |
| 105 | + if (sourceEl.tagName() == QStringLiteral("url-data")) { |
| 106 | + QXmppHttpFileSource source; |
| 107 | + if (source.parse(sourceEl)) { |
| 108 | + d->httpSources.push_back(std::move(source)); |
| 109 | + } |
| 110 | + } else if (sourceEl.tagName() == QStringLiteral("encrypted")) { |
| 111 | + QXmppEncryptedFileSource source; |
| 112 | + if (source.parse(sourceEl)) { |
| 113 | + d->encryptedSources.push_back(std::move(source)); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return true; |
| 119 | +} |
| 120 | +/// \endcond |
| 121 | + |
| 122 | +class QXmppStickerPackItemPrivate : public QSharedData |
| 123 | +{ |
| 124 | +public: |
| 125 | + QString name; |
| 126 | + QString summary; |
| 127 | + QVector<QXmppStickerItem> items; |
| 128 | +}; |
| 129 | + |
| 130 | +QXmppStickerPackItem::QXmppStickerPackItem() |
| 131 | + : d(new QXmppStickerPackItemPrivate()) |
| 132 | +{ |
| 133 | +} |
| 134 | + |
| 135 | +/// |
| 136 | +/// \class QXmppStickerPackitem |
| 137 | +/// |
| 138 | +/// A pubsub item for a sticker pack. |
| 139 | +/// |
| 140 | + |
| 141 | +/// |
| 142 | +/// \brief Returns the name of the sticker pack |
| 143 | +/// |
| 144 | +const QString &QXmppStickerPackItem::name() const |
| 145 | +{ |
| 146 | + return d->name; |
| 147 | +} |
| 148 | + |
| 149 | +/// |
| 150 | +/// \brief Sets the name of the sticker pack |
| 151 | +/// |
| 152 | +void QXmppStickerPackItem::setName(const QString &name) |
| 153 | +{ |
| 154 | + d->name = name; |
| 155 | +} |
| 156 | + |
| 157 | +/// |
| 158 | +/// \brief Returns the summary of this sticker pack |
| 159 | +/// |
| 160 | +const QString &QXmppStickerPackItem::summary() const |
| 161 | +{ |
| 162 | + return d->summary; |
| 163 | +} |
| 164 | + |
| 165 | +/// |
| 166 | +/// \brief Sets the summary of the sticker pack |
| 167 | +/// |
| 168 | +void QXmppStickerPackItem::setSummary(const QString &summary) |
| 169 | +{ |
| 170 | + d->summary = summary; |
| 171 | +} |
| 172 | + |
| 173 | +/// |
| 174 | +/// \brief Returns the list of stickers of this pack |
| 175 | +/// |
| 176 | +const QVector<QXmppStickerItem> &QXmppStickerPackItem::items() const |
| 177 | +{ |
| 178 | + return d->items; |
| 179 | +} |
| 180 | + |
| 181 | +/// |
| 182 | +/// \brief Set the list of stickers for this pack |
| 183 | +/// |
| 184 | +void QXmppStickerPackItem::setItems(const QVector<QXmppStickerItem> &items) |
| 185 | +{ |
| 186 | + d->items = items; |
| 187 | +} |
| 188 | + |
| 189 | +QXMPP_PRIVATE_DEFINE_RULE_OF_SIX(QXmppStickerPackItem) |
| 190 | + |
| 191 | +void QXmppStickerPackItem::parsePayload(const QDomElement &payloadElement) |
| 192 | +{ |
| 193 | + d->name = payloadElement.firstChildElement("name").text(); |
| 194 | + d->summary = payloadElement.firstChildElement("summary").text(); |
| 195 | + |
| 196 | + for (auto firstChild = payloadElement.firstChildElement("item"); |
| 197 | + !firstChild.isNull(); |
| 198 | + firstChild.nextSibling()) { |
| 199 | + QXmppStickerItem stickerItem; |
| 200 | + stickerItem.parse(payloadElement); |
| 201 | + |
| 202 | + d->items.push_back(std::move(stickerItem)); |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +void QXmppStickerPackItem::serializePayload(QXmlStreamWriter *writer) const |
| 207 | +{ |
| 208 | + writer->writeStartElement("pack"); |
| 209 | + writer->writeDefaultNamespace(ns_stickers); |
| 210 | + |
| 211 | + writer->writeTextElement("name", d->name); |
| 212 | + writer->writeTextElement("summary", d->summary); |
| 213 | + |
| 214 | + for (const auto &item : d->items) { |
| 215 | + item.toXml(writer); |
| 216 | + } |
| 217 | + |
| 218 | + writer->writeEndElement(); |
| 219 | +} |
0 commit comments