From be8f6c46fd345479744f88b4698cc0c5c56575c7 Mon Sep 17 00:00:00 2001 From: Sergei Ilinykh Date: Tue, 11 Jun 2024 23:38:47 +0300 Subject: [PATCH] minor improvements to vcard4 api --- src/xmpp/xmpp-im/xmpp_vcard4.cpp | 20 +++++++++++++------- src/xmpp/xmpp-im/xmpp_vcard4.h | 28 +++++++++++++++++++++++----- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/xmpp/xmpp-im/xmpp_vcard4.cpp b/src/xmpp/xmpp-im/xmpp_vcard4.cpp index da8f2c85..120c39aa 100644 --- a/src/xmpp/xmpp-im/xmpp_vcard4.cpp +++ b/src/xmpp/xmpp-im/xmpp_vcard4.cpp @@ -115,7 +115,7 @@ namespace { } template - static void serializeList(QDomElement &parent, const TaggedList &list, const QString &tagName, + static void serializeList(QDomElement &parent, const TaggedList> &list, const QString &tagName, const QString &innerTagName = QLatin1String("text")) { auto document = parent.ownerDocument(); @@ -619,6 +619,14 @@ class VCard::VCardData : public QSharedData { VCard::VCard() : d(nullptr) { } +VCard::VCard(const VCard &other) : d(other.d) { } + +VCard &VCard::operator=(const VCard &other) +{ + d = other.d; + return *this; +} + VCard::VCard(const QDomElement &element) : d(new VCardData(element)) { } VCard::~VCard() = default; @@ -630,8 +638,6 @@ bool VCard::isEmpty() const return d->isEmpty(); } -VCard::operator bool() const { return d != nullptr; } - QDomElement VCard::toXmlElement(QDomDocument &document) const { if (!d) { @@ -799,7 +805,7 @@ void VCard::fromVCardTemp(const XMPP::VCard &tempVCard) setNames(names); // Nickname - setNickname({ { PStringList { Parameters(), { tempVCard.nickName() } } } }); + setNickName({ { { PStringList { Parameters(), { tempVCard.nickName() } } } } }); // Photo if (!tempVCard.photo().isEmpty()) { @@ -902,7 +908,7 @@ void VCard::fromVCardTemp(const XMPP::VCard &tempVCard) setOrg(org); // Categories - setCategories({ { PStringList { Parameters(), tempVCard.categories() } } }); + setCategories({ { { PStringList { Parameters(), tempVCard.categories() } } } }); // Note setNote({ { PString { Parameters(), tempVCard.note() } } }); @@ -1069,9 +1075,9 @@ void VCard::setNames(const PNames &names) d->names = names; } -PStringLists VCard::nickname() const { return d ? d->nickname : PStringLists(); } +PStringLists VCard::nickName() const { return d ? d->nickname : PStringLists(); } -void VCard::setNickname(const PStringLists &nickname) +void VCard::setNickName(const PStringLists &nickname) { INIT_D(); d->nickname = nickname; diff --git a/src/xmpp/xmpp-im/xmpp_vcard4.h b/src/xmpp/xmpp-im/xmpp_vcard4.h index b75ca659..b95c6670 100644 --- a/src/xmpp/xmpp-im/xmpp_vcard4.h +++ b/src/xmpp/xmpp-im/xmpp_vcard4.h @@ -133,6 +133,11 @@ template <> struct Item : public ItemBase { operator QDate() const { return data.date(); } }; +template <> struct Item : public ItemBase { + QStringList data; + operator QString() const { return data.value(0); } +}; + using UriOrText = std::variant; using TimeZone = std::variant; using Historical = std::variant; @@ -193,6 +198,8 @@ template class TaggedList : public QList { return *std::ranges::max_element( *this, [](auto const &a, auto const &b) { return a.parameters.pref > b.parameters.pref; }); } + + operator QString() const { return preferred().data; } }; template <> class TaggedList : public QList { @@ -214,7 +221,12 @@ template <> class TaggedList : public QList { } }; -using PStringLists = TaggedList; +class TaggedListStringList : public TaggedList { +public: + operator QString() const { return preferred().data.value(0); } +}; + +using PStringLists = TaggedListStringList; using PStrings = TaggedList; using PUris = TaggedList; using PAdvUris = TaggedList; @@ -226,10 +238,16 @@ class VCard { public: VCard(); VCard(const QDomElement &element); + VCard(const VCard &other); + ~VCard(); - bool isEmpty() const; - explicit operator bool() const; + VCard &operator=(const VCard &); + + bool isEmpty() const; + + inline bool isNull() const { return d != nullptr; } + inline explicit operator bool() const { return isNull(); } QDomElement toXmlElement(QDomDocument &document) const; @@ -247,8 +265,8 @@ class VCard { const PNames &names() const; void setNames(const PNames &names); - PStringLists nickname() const; - void setNickname(const PStringLists &nickname); + PStringLists nickName() const; + void setNickName(const PStringLists &nickname); PStrings emails() const; void setEmails(const PStrings &emails);