From 87b747cd360524db82f6597aaead1f19e8320498 Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee <elv1313@gmail.com> Date: Thu, 1 Oct 2015 10:25:37 -0400 Subject: [PATCH] person: Allow copies to be created Useful for migration between collections without using vCard serialization. Issue: #81136 Change-Id: If52e19692c2a5022e484f855ee475ed192cf068a --- src/person.cpp | 34 ++++++++++++++++++++++++++++++++++ src/person.h | 1 + src/private/person_p.h | 12 ++++++++++-- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/person.cpp b/src/person.cpp index 73a70277..ee205f0e 100644 --- a/src/person.cpp +++ b/src/person.cpp @@ -224,6 +224,40 @@ Person::Person(const QByteArray& content, Person::Encoding encoding, CollectionI }; } +/** + * Copy constructor, useful when transferring a contact between collections + * + * For example, converting a trust request to GMail contact without forcing + * a slow vCard conversion. + * + * This create a COPY of the person details, using shared attributes between + * multiple person with multiple collection is currently not supported (but + * would be easy to enable if the need arise). + */ +Person::Person(const Person& other) noexcept : ItemBase(nullptr), +d_ptr(new PersonPrivate(this)) +{ + d_ptr->m_FirstName = other.d_ptr->m_FirstName ; + d_ptr->m_SecondName = other.d_ptr->m_SecondName ; + d_ptr->m_NickName = other.d_ptr->m_NickName ; + d_ptr->m_vPhoto = other.d_ptr->m_vPhoto ; + d_ptr->m_FormattedName = other.d_ptr->m_FormattedName ; + d_ptr->m_PreferredEmail = other.d_ptr->m_PreferredEmail ; + d_ptr->m_Organization = other.d_ptr->m_Organization ; + d_ptr->m_Uid = other.d_ptr->m_Uid ; + d_ptr->m_Group = other.d_ptr->m_Group ; + d_ptr->m_Department = other.d_ptr->m_Department ; + d_ptr->m_DisplayPhoto = other.d_ptr->m_DisplayPhoto ; + d_ptr->m_Numbers = other.d_ptr->m_Numbers ; + d_ptr->m_Active = other.d_ptr->m_Active ; + d_ptr->m_isPlaceHolder = other.d_ptr->m_isPlaceHolder ; + d_ptr->m_lAddresses = other.d_ptr->m_lAddresses ; + d_ptr->m_lCustomAttributes = other.d_ptr->m_lCustomAttributes ; + d_ptr->m_LastUsed = other.d_ptr->m_LastUsed ; + d_ptr->m_LastUsedInit = other.d_ptr->m_LastUsedInit ; + d_ptr->m_HiddenContactMethods = other.d_ptr->m_HiddenContactMethods; +} + ///Updates an existing contact from vCard info void Person::updateFromVCard(const QByteArray& content) { diff --git a/src/person.h b/src/person.h index eeb89c5f..cdc26aec 100644 --- a/src/person.h +++ b/src/person.h @@ -122,6 +122,7 @@ public: //Constructors & Destructors explicit Person(CollectionInterface* parent = nullptr, const QByteArray& uid = QByteArray()); Person(const QByteArray& content, Person::Encoding encoding = Encoding::UID, CollectionInterface* parent = nullptr); + Person(const Person& other) noexcept; virtual ~Person(); //Getters diff --git a/src/private/person_p.h b/src/private/person_p.h index 69d35303..ac28b137 100644 --- a/src/private/person_p.h +++ b/src/private/person_p.h @@ -27,12 +27,12 @@ #include "person.h" class ContactMethod; -class PersonPrivate : public QObject +class PersonPrivate final : public QObject { Q_OBJECT friend class ContactMethod; public: - PersonPrivate(Person* contact); + explicit PersonPrivate(Person* contact); ~PersonPrivate(); QString m_FirstName ; QString m_SecondName ; @@ -54,6 +54,14 @@ public: bool m_LastUsedInit ; QList<ContactMethod*> m_HiddenContactMethods; + /* + * NOTE If new attributes are added, please update the explicit Person copy + * constructor as Qt force QObject copy via serialization (to force developers + * to use references, copy-on-write based containers and smart pointers + * instead), which is overkill for this scenario and would detach all the + * containers causing useless increase in memory usage. + */ + //Cache QString m_CachedFilterString; -- GitLab