Skip to content

Commit

Permalink
VCardManager: Implement import and export functionalities
Browse files Browse the repository at this point in the history
  • Loading branch information
pasnox authored and lnjX committed May 8, 2024
1 parent f8ebd77 commit 66fb144
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/base/QXmppVCardIq.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
#include <QDomElement>
#include <QMap>

namespace QXmpp::Private {
struct VCardData;
}

class QXmppVCardAddressPrivate;
class QXmppVCardEmailPrivate;
class QXmppVCardPhonePrivate;
Expand Down Expand Up @@ -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;
Expand Down
63 changes: 61 additions & 2 deletions src/client/QXmppVCardManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<VCardData, QXmppError> 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 <vCard/> 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<QXmppVCardManagerPrivate>())
{
d->isClientVCardReceived = false;
const auto serializeVCardData = [](const VCardData &data, QXmlStreamWriter &writer) { data.toXml(writer); };
QXmppExportData::registerExtension<VCardData, VCardData::fromDom, serializeVCardData>(u"vCard", ns_vcard);
}

QXmppVCardManager::~QXmppVCardManager() = default;
Expand Down Expand Up @@ -129,4 +162,30 @@ bool QXmppVCardManager::handleStanza(const QDomElement &element)

return false;
}

void QXmppVCardManager::onRegistered(QXmppClient *client)
{
if (auto manager = client->findExtension<QXmppAccountMigrationManager>()) {
using DataResult = std::variant<VCardData, QXmppError>;

auto importData = [this](VCardData data) {
return setVCard(data.vCard);
};

auto exportData = [this]() {
return chain<DataResult>(fetchVCard(this->client()->configuration().jidBare()), this, [](auto &&result) {
return mapSuccess(std::move(result), [](QXmppVCardIq &&iq) -> VCardData { return { iq }; });
});
};

manager->registerExportData<VCardData>(importData, exportData);
}
}

void QXmppVCardManager::onUnregistered(QXmppClient *client)
{
if (auto manager = client->findExtension<QXmppAccountMigrationManager>()) {
manager->unregisterExportData<QXmppVCardIq>();
}
}
/// \endcond
4 changes: 4 additions & 0 deletions src/client/QXmppVCardManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<QXmppVCardManagerPrivate> d;
};
Expand Down

0 comments on commit 66fb144

Please sign in to comment.