Skip to content

Commit

Permalink
minor improvements to vcard4 api
Browse files Browse the repository at this point in the history
  • Loading branch information
Ri0n committed Jun 11, 2024
1 parent bfc0d9c commit be8f6c4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
20 changes: 13 additions & 7 deletions src/xmpp/xmpp-im/xmpp_vcard4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ namespace {
}

template <typename T>
static void serializeList(QDomElement &parent, const TaggedList<T> &list, const QString &tagName,
static void serializeList(QDomElement &parent, const TaggedList<Item<T>> &list, const QString &tagName,
const QString &innerTagName = QLatin1String("text"))
{
auto document = parent.ownerDocument();
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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() } } });
Expand Down Expand Up @@ -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;
Expand Down
28 changes: 23 additions & 5 deletions src/xmpp/xmpp-im/xmpp_vcard4.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ template <> struct Item<QDateTime> : public ItemBase {
operator QDate() const { return data.date(); }
};

template <> struct Item<QStringList> : public ItemBase {
QStringList data;
operator QString() const { return data.value(0); }
};

using UriOrText = std::variant<QUrl, QString>;
using TimeZone = std::variant<QUrl, QString, int>;
using Historical = std::variant<QDateTime, QDate, QTime, QString>;
Expand Down Expand Up @@ -193,6 +198,8 @@ template <typename T> class TaggedList : public QList<T> {
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<PAdvUri> : public QList<PAdvUri> {
Expand All @@ -214,7 +221,12 @@ template <> class TaggedList<PAdvUri> : public QList<PAdvUri> {
}
};

using PStringLists = TaggedList<PStringList>;
class TaggedListStringList : public TaggedList<PStringList> {
public:
operator QString() const { return preferred().data.value(0); }
};

using PStringLists = TaggedListStringList;
using PStrings = TaggedList<PString>;
using PUris = TaggedList<PUri>;
using PAdvUris = TaggedList<PAdvUri>;
Expand All @@ -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;

Expand All @@ -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);
Expand Down

0 comments on commit be8f6c4

Please sign in to comment.