Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start implementing XEP-0449: Stickers #490

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/doap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,14 @@ SPDX-License-Identifier: CC0-1.0
<xmpp:since>1.5</xmpp:since>
</xmpp:SupportedXep>
</implements>
<implements>
<xmpp:SupportedXep>
<xmpp:xep rdf:resource='https://xmpp.org/extensions/xep-0449.html'/>
<xmpp:status>complete</xmpp:status>
<xmpp:version>0.1</xmpp:version>
<xmpp:since>1.5</xmpp:since>
</xmpp:SupportedXep>
</implements>
<release>
<Version>
<revision>1.4.0</revision>
Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ set(INSTALL_HEADER_FILES
base/QXmppSocks.h
base/QXmppStanza.h
base/QXmppStartTlsPacket.h
base/QXmppStickerPackItem.h
base/QXmppStream.h
base/QXmppStreamFeatures.h
base/QXmppStun.h
Expand Down Expand Up @@ -212,6 +213,7 @@ set(SOURCE_FILES
base/QXmppRpcIq.cpp
base/QXmppSasl.cpp
base/QXmppSessionIq.cpp
base/QXmppStickerPackItem.cpp
base/QXmppSocks.cpp
base/QXmppStanza.cpp
base/QXmppStartTlsPacket.cpp
Expand Down
2 changes: 2 additions & 0 deletions src/base/QXmppConstants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,5 +204,7 @@ const char *ns_file_metadata = "urn:xmpp:file:metadata:0";
const char *ns_sfs = "urn:xmpp:sfs:0";
// XEP-0448: Encryption for stateless file sharing
const char *ns_esfs = "urn:xmpp:esfs:0";
// XEP-0449: Stickers
const char *ns_stickers = "urn:xmpp:stickers:0";
// XEP-0450: Automatic Trust Management (ATM)
const char *ns_atm = "urn:xmpp:atm:1";
2 changes: 2 additions & 0 deletions src/base/QXmppConstants_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ extern const char *ns_file_metadata;
extern const char *ns_sfs;
// XEP-0448: Encryption for stateless file sharing
extern const char *ns_esfs;
// XEP-0449: Stickers
extern const char *ns_stickers;
// XEP-0450: Automatic Trust Management (ATM)
extern const char *ns_atm;

Expand Down
40 changes: 40 additions & 0 deletions src/base/QXmppMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ class QXmppMessagePrivate : public QSharedData

// XEP-0448: Encryption for stateless file sharing
QVector<QXmppFileShare> sharedFiles;

// XEP-0449: Stickers
std::optional<QString> stickerPackId;
};

QXmppMessagePrivate::QXmppMessagePrivate()
Expand Down Expand Up @@ -1258,6 +1261,30 @@ void QXmppMessage::setSharedFiles(const QVector<QXmppFileShare> &sharedFiles)
d->sharedFiles = sharedFiles;
}

///
/// \brief Returns the sticker pack id for the sticker contained in this message
///
/// This is used for \xep{0449, Stickers}.
///
/// \since QXmpp 1.5
///
const std::optional<QString> &QXmppMessage::stickerPackId() const
{
return d->stickerPackId;
}

///
/// \brief Sets the sticker pack id for the sticker contained in this message
///
/// This is used for \xep{0449, Stickers}.
///
/// \since QXmpp 1.5
///
void QXmppMessage::setStickerPackId(const std::optional<QString> &stickerPackId)
{
d->stickerPackId = stickerPackId;
}

/// \cond
void QXmppMessage::parse(const QDomElement &element)
{
Expand Down Expand Up @@ -1552,6 +1579,12 @@ bool QXmppMessage::parseExtension(const QDomElement &element, QXmpp::SceMode sce
}
return true;
}

// XEP-0449: Stickers
if (checkElement(element, QStringLiteral("sticker"), ns_stickers)) {
d->stickerPackId = element.attribute("pack");
return true;
}
}
return false;
}
Expand Down Expand Up @@ -1804,5 +1837,12 @@ void QXmppMessage::serializeExtensions(QXmlStreamWriter *writer, QXmpp::SceMode
for (const auto &fileShare : d->sharedFiles) {
fileShare.toXml(writer);
}

// XEP-0449: Sticker
if (d->stickerPackId) {
writer->writeStartElement("sticker");
writer->writeDefaultNamespace(ns_stickers);
writer->writeAttribute("pack", *d->stickerPackId);
}
}
}
4 changes: 4 additions & 0 deletions src/base/QXmppMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@ class QXMPP_EXPORT QXmppMessage : public QXmppStanza
const QVector<QXmppFileShare> &sharedFiles() const;
void setSharedFiles(const QVector<QXmppFileShare> &sharedFiles);

// XEP-0449: Stickers
const std::optional<QString> &stickerPackId() const;
void setStickerPackId(const std::optional<QString> &stickerPackId);
jbruechert marked this conversation as resolved.
Show resolved Hide resolved

/// \cond
#ifdef BUILD_OMEMO
// XEP-0384: OMEMO Encryption
Expand Down
219 changes: 219 additions & 0 deletions src/base/QXmppStickerPackItem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
// SPDX-FileCopyrightText: 2022 Jonah Brüchert <[email protected]>
//
// SPDX-License-Identifier: LGPL-2.1-or-later

#include "QXmppStickerPackItem.h"

#include "QXmppConstants_p.h"
#include "QXmppEncryptedFileSource.h"
#include "QXmppFileMetadata.h"

#include <QXmlStreamWriter>

class QXmppStickerItemPrivate : public QSharedData
{
public:
QXmppFileMetadata metadata;
QVector<QXmppHttpFileSource> httpSources;
QVector<QXmppEncryptedFileSource> encryptedSources;
};

QXmppStickerItem::QXmppStickerItem()
: d(new QXmppStickerItemPrivate())
{
}

///
/// \class QXmppStickerItem
///
/// This class represents a single sticker when publishing or retrieving it.
///

///
/// \brief Returns metadata about the sticker file
///
const QXmppFileMetadata &QXmppStickerItem::metadata() const
{
return d->metadata;
}

///
/// \brief Sets metadata of this sticker file
///
void QXmppStickerItem::setMetadata(const QXmppFileMetadata &metadata)
{
d->metadata = metadata;
}

///
/// \brief Returns HTTP sources for the sticker file
///
const QVector<QXmppHttpFileSource> &QXmppStickerItem::httpSource() const
{
return d->httpSources;
}

///
/// \brief Sets the list of HTTP sources for this sticker file
///
void QXmppStickerItem::setHttpSources(const QVector<QXmppHttpFileSource> &httpSources)
{
d->httpSources = httpSources;
}

///
/// \brief Returns the list of encrypted sources for this sticker file
///
const QVector<QXmppEncryptedFileSource> &QXmppStickerItem::encryptedSources() const
{
return d->encryptedSources;
}

///
/// \brief Set the list of encrypted sources for this sticker file
///
void QXmppStickerItem::setEncryptedSources(const QVector<QXmppEncryptedFileSource> &encryptedSources)
{
d->encryptedSources = encryptedSources;
}

QXMPP_PRIVATE_DEFINE_RULE_OF_SIX(QXmppStickerItem)

/// \cond
void QXmppStickerItem::toXml(QXmlStreamWriter *writer) const
{
writer->writeStartElement("item");
d->metadata.toXml(writer);
for (const auto &httpSource : d->httpSources) {
httpSource.toXml(writer);
}
for (const auto &encryptedSource : d->encryptedSources) {
encryptedSource.toXml(writer);
}
writer->writeEndElement();
}

bool QXmppStickerItem::parse(const QDomElement &element)
{
auto fileElement = element.firstChildElement("file");
d->metadata.parse(fileElement);

auto sources = element.firstChildElement("sources");
for (auto sourceEl = sources.firstChildElement();
!sourceEl.isNull();
sourceEl = sourceEl.nextSiblingElement()) {
if (sourceEl.tagName() == QStringLiteral("url-data")) {
QXmppHttpFileSource source;
if (source.parse(sourceEl)) {
d->httpSources.push_back(std::move(source));
}
} else if (sourceEl.tagName() == QStringLiteral("encrypted")) {
QXmppEncryptedFileSource source;
if (source.parse(sourceEl)) {
d->encryptedSources.push_back(std::move(source));
}
}
}

return true;
}
/// \endcond

class QXmppStickerPackItemPrivate : public QSharedData
{
public:
QString name;
QString summary;
QVector<QXmppStickerItem> items;
};

QXmppStickerPackItem::QXmppStickerPackItem()
: d(new QXmppStickerPackItemPrivate())
{
}

///
/// \class QXmppStickerPackitem
///
/// A pubsub item for a sticker pack.
///

///
/// \brief Returns the name of the sticker pack
///
const QString &QXmppStickerPackItem::name() const
{
return d->name;
}

///
/// \brief Sets the name of the sticker pack
///
void QXmppStickerPackItem::setName(const QString &name)
{
d->name = name;
}

///
/// \brief Returns the summary of this sticker pack
///
const QString &QXmppStickerPackItem::summary() const
{
return d->summary;
}

///
/// \brief Sets the summary of the sticker pack
///
void QXmppStickerPackItem::setSummary(const QString &summary)
{
d->summary = summary;
}

///
/// \brief Returns the list of stickers of this pack
///
const QVector<QXmppStickerItem> &QXmppStickerPackItem::items() const
{
return d->items;
}

///
/// \brief Set the list of stickers for this pack
///
void QXmppStickerPackItem::setItems(const QVector<QXmppStickerItem> &items)
{
d->items = items;
}

QXMPP_PRIVATE_DEFINE_RULE_OF_SIX(QXmppStickerPackItem)

void QXmppStickerPackItem::parsePayload(const QDomElement &payloadElement)
{
d->name = payloadElement.firstChildElement("name").text();
d->summary = payloadElement.firstChildElement("summary").text();

for (auto firstChild = payloadElement.firstChildElement("item");
!firstChild.isNull();
firstChild.nextSibling()) {
QXmppStickerItem stickerItem;
stickerItem.parse(payloadElement);

d->items.push_back(std::move(stickerItem));
}
}

void QXmppStickerPackItem::serializePayload(QXmlStreamWriter *writer) const
{
writer->writeStartElement("pack");
writer->writeDefaultNamespace(ns_stickers);

writer->writeTextElement("name", d->name);
writer->writeTextElement("summary", d->summary);

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

writer->writeEndElement();
}
Loading