diff --git a/src/base/QXmppVCardIq.h b/src/base/QXmppVCardIq.h index c8be6e365..1a4878284 100644 --- a/src/base/QXmppVCardIq.h +++ b/src/base/QXmppVCardIq.h @@ -11,6 +11,10 @@ #include #include +namespace QXmpp::Private { +struct VCardData; +} + class QXmppVCardAddressPrivate; class QXmppVCardEmailPrivate; class QXmppVCardPhonePrivate; @@ -266,6 +270,8 @@ class QXMPP_EXPORT QXmppVCardIq : public QXmppIq /// \endcond protected: + friend struct QXmpp::Private::VCardData; + /// \cond void parseElementFromChild(const QDomElement &) override; void toXmlElementFromChild(QXmlStreamWriter *writer) const override; diff --git a/src/client/QXmppVCardManager.cpp b/src/client/QXmppVCardManager.cpp index 35af3d4d5..24f79448d 100644 --- a/src/client/QXmppVCardManager.cpp +++ b/src/client/QXmppVCardManager.cpp @@ -4,27 +4,60 @@ #include "QXmppVCardManager.h" +#include "QXmppAccountMigrationManager.h" #include "QXmppClient.h" #include "QXmppConstants_p.h" #include "QXmppError.h" #include "QXmppFutureUtils_p.h" #include "QXmppTask.h" #include "QXmppUtils.h" +#include "QXmppUtils_p.h" #include "QXmppVCardIq.h" using namespace QXmpp::Private; +namespace QXmpp::Private { + +struct VCardData { + QXmppVCardIq vCard; + + static std::variant fromDom(const QDomElement &el) + { + Q_ASSERT(el.tagName() == u"vcard"); + Q_ASSERT(el.namespaceURI() == ns_qxmpp_export); + + auto vCardEl = firstChildElement(el, u"vCard", ns_vcard); + if (vCardEl.isNull()) { + return QXmppError { QStringLiteral("Missing required element."), {} }; + } + + VCardData d; + d.vCard.parseElementFromChild(el.firstChildElement()); + return d; + } + + void toXml(QXmlStreamWriter &writer) const + { + writer.writeStartElement(QSL65("vcard")); + vCard.toXmlElementFromChild(&writer); + writer.writeEndElement(); + } +}; + +} // namespace QXmpp::Private + class QXmppVCardManagerPrivate { public: QXmppVCardIq clientVCard; - bool isClientVCardReceived; + bool isClientVCardReceived = false; }; QXmppVCardManager::QXmppVCardManager() : d(std::make_unique()) { - d->isClientVCardReceived = false; + const auto serializeVCardData = [](const VCardData &data, QXmlStreamWriter &writer) { data.toXml(writer); }; + QXmppExportData::registerExtension(u"vCard", ns_vcard); } QXmppVCardManager::~QXmppVCardManager() = default; @@ -129,4 +162,30 @@ bool QXmppVCardManager::handleStanza(const QDomElement &element) return false; } + +void QXmppVCardManager::onRegistered(QXmppClient *client) +{ + if (auto manager = client->findExtension()) { + using DataResult = std::variant; + + auto importData = [this](VCardData data) { + return setVCard(data.vCard); + }; + + auto exportData = [this]() { + return chain(fetchVCard(this->client()->configuration().jidBare()), this, [](auto &&result) { + return mapSuccess(std::move(result), [](QXmppVCardIq &&iq) -> VCardData { return { iq }; }); + }); + }; + + manager->registerExportData(importData, exportData); + } +} + +void QXmppVCardManager::onUnregistered(QXmppClient *client) +{ + if (auto manager = client->findExtension()) { + manager->unregisterExportData(); + } +} /// \endcond diff --git a/src/client/QXmppVCardManager.h b/src/client/QXmppVCardManager.h index baaf6639f..e89e434e1 100644 --- a/src/client/QXmppVCardManager.h +++ b/src/client/QXmppVCardManager.h @@ -75,6 +75,10 @@ class QXMPP_EXPORT QXmppVCardManager : public QXmppClientExtension /// after calling the requestClientVCard() function. void clientVCardReceived(); +protected: + void onRegistered(QXmppClient *client) override; + void onUnregistered(QXmppClient *client) override; + private: const std::unique_ptr d; };