File indexing completed on 2024-03-24 15:26:12

0001 /*
0002     This file is part of the KContacts framework.
0003     SPDX-FileCopyrightText: 2008 Tobias Koenig <tokoe@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "contactgroup.h"
0009 
0010 #include <QMap>
0011 #include <QSharedData>
0012 #include <QString>
0013 #include <QUuid>
0014 
0015 using namespace KContacts;
0016 
0017 class Q_DECL_HIDDEN ContactGroup::ContactReference::ContactReferencePrivate : public QSharedData
0018 {
0019 public:
0020     ContactReferencePrivate()
0021         : QSharedData()
0022     {
0023     }
0024 
0025     ContactReferencePrivate(const ContactReferencePrivate &other)
0026         : QSharedData(other)
0027     {
0028         mUid = other.mUid;
0029         mPreferredEmail = other.mPreferredEmail;
0030         mCustoms = other.mCustoms;
0031     }
0032 
0033     QString mUid;
0034     QString mGid;
0035     QString mPreferredEmail;
0036     QMap<QString, QString> mCustoms;
0037 };
0038 
0039 ContactGroup::ContactReference::ContactReference()
0040     : d(new ContactReferencePrivate)
0041 {
0042 }
0043 
0044 ContactGroup::ContactReference::ContactReference(const ContactReference &other)
0045     : d(other.d)
0046 {
0047 }
0048 
0049 ContactGroup::ContactReference::ContactReference(const QString &uid)
0050     : d(new ContactReferencePrivate)
0051 {
0052     d->mUid = uid;
0053 }
0054 
0055 ContactGroup::ContactReference::~ContactReference()
0056 {
0057 }
0058 
0059 void ContactGroup::ContactReference::setUid(const QString &uid)
0060 {
0061     d->mUid = uid;
0062 }
0063 
0064 QString ContactGroup::ContactReference::uid() const
0065 {
0066     return d->mUid;
0067 }
0068 
0069 void ContactGroup::ContactReference::setGid(const QString &gid)
0070 {
0071     d->mGid = gid;
0072 }
0073 
0074 QString ContactGroup::ContactReference::gid() const
0075 {
0076     return d->mGid;
0077 }
0078 
0079 void ContactGroup::ContactReference::setPreferredEmail(const QString &email)
0080 {
0081     d->mPreferredEmail = email;
0082 }
0083 
0084 QString ContactGroup::ContactReference::preferredEmail() const
0085 {
0086     return d->mPreferredEmail;
0087 }
0088 
0089 void ContactGroup::ContactReference::insertCustom(const QString &key, const QString &value)
0090 {
0091     d->mCustoms.insert(key, value);
0092 }
0093 
0094 void ContactGroup::ContactReference::removeCustom(const QString &key)
0095 {
0096     d->mCustoms.remove(key);
0097 }
0098 
0099 QString ContactGroup::ContactReference::custom(const QString &key) const
0100 {
0101     return d->mCustoms.value(key);
0102 }
0103 
0104 ContactGroup::ContactReference &ContactGroup::ContactReference::operator=(const ContactGroup::ContactReference &other)
0105 {
0106     if (this != &other) {
0107         d = other.d;
0108     }
0109 
0110     return *this;
0111 }
0112 
0113 bool ContactGroup::ContactReference::operator==(const ContactReference &other) const
0114 {
0115     return d->mUid == other.d->mUid && d->mPreferredEmail == other.d->mPreferredEmail && d->mCustoms == other.d->mCustoms;
0116 }
0117 
0118 class Q_DECL_HIDDEN ContactGroup::ContactGroupReference::ContactGroupReferencePrivate : public QSharedData
0119 {
0120 public:
0121     ContactGroupReferencePrivate()
0122         : QSharedData()
0123     {
0124     }
0125 
0126     ContactGroupReferencePrivate(const ContactGroupReferencePrivate &other)
0127         : QSharedData(other)
0128     {
0129         mUid = other.mUid;
0130         mCustoms = other.mCustoms;
0131     }
0132 
0133     QString mUid;
0134     QMap<QString, QString> mCustoms;
0135 };
0136 
0137 ContactGroup::ContactGroupReference::ContactGroupReference()
0138     : d(new ContactGroupReferencePrivate)
0139 {
0140 }
0141 
0142 ContactGroup::ContactGroupReference::ContactGroupReference(const ContactGroupReference &other)
0143     : d(other.d)
0144 {
0145 }
0146 
0147 ContactGroup::ContactGroupReference::ContactGroupReference(const QString &uid)
0148     : d(new ContactGroupReferencePrivate)
0149 {
0150     d->mUid = uid;
0151 }
0152 
0153 ContactGroup::ContactGroupReference::~ContactGroupReference()
0154 {
0155 }
0156 
0157 void ContactGroup::ContactGroupReference::setUid(const QString &uid)
0158 {
0159     d->mUid = uid;
0160 }
0161 
0162 QString ContactGroup::ContactGroupReference::uid() const
0163 {
0164     return d->mUid;
0165 }
0166 
0167 void ContactGroup::ContactGroupReference::insertCustom(const QString &key, const QString &value)
0168 {
0169     d->mCustoms.insert(key, value);
0170 }
0171 
0172 void ContactGroup::ContactGroupReference::removeCustom(const QString &key)
0173 {
0174     d->mCustoms.remove(key);
0175 }
0176 
0177 QString ContactGroup::ContactGroupReference::custom(const QString &key) const
0178 {
0179     return d->mCustoms.value(key);
0180 }
0181 
0182 ContactGroup::ContactGroupReference &ContactGroup::ContactGroupReference::operator=(const ContactGroup::ContactGroupReference &other)
0183 {
0184     if (this != &other) {
0185         d = other.d;
0186     }
0187 
0188     return *this;
0189 }
0190 
0191 bool ContactGroup::ContactGroupReference::operator==(const ContactGroupReference &other) const
0192 {
0193     return d->mUid == other.d->mUid && d->mCustoms == other.d->mCustoms;
0194 }
0195 
0196 class Q_DECL_HIDDEN ContactGroup::Data::DataPrivate : public QSharedData
0197 {
0198 public:
0199     DataPrivate()
0200         : QSharedData()
0201     {
0202     }
0203 
0204     DataPrivate(const DataPrivate &other)
0205         : QSharedData(other)
0206     {
0207         mName = other.mName;
0208         mEmail = other.mEmail;
0209         mCustoms = other.mCustoms;
0210     }
0211 
0212     QString mName;
0213     QString mEmail;
0214     QMap<QString, QString> mCustoms;
0215 };
0216 
0217 ContactGroup::Data::Data()
0218     : d(new DataPrivate)
0219 {
0220 }
0221 
0222 ContactGroup::Data::Data(const Data &other)
0223     : d(other.d)
0224 {
0225 }
0226 
0227 ContactGroup::Data::Data(const QString &name, const QString &email)
0228     : d(new DataPrivate)
0229 {
0230     d->mName = name;
0231     d->mEmail = email;
0232 }
0233 
0234 ContactGroup::Data::~Data()
0235 {
0236 }
0237 
0238 void ContactGroup::Data::setName(const QString &name)
0239 {
0240     d->mName = name;
0241 }
0242 
0243 QString ContactGroup::Data::name() const
0244 {
0245     return d->mName;
0246 }
0247 
0248 void ContactGroup::Data::setEmail(const QString &email)
0249 {
0250     d->mEmail = email;
0251 }
0252 
0253 QString ContactGroup::Data::email() const
0254 {
0255     return d->mEmail;
0256 }
0257 
0258 void ContactGroup::Data::insertCustom(const QString &key, const QString &value)
0259 {
0260     d->mCustoms.insert(key, value);
0261 }
0262 
0263 void ContactGroup::Data::removeCustom(const QString &key)
0264 {
0265     d->mCustoms.remove(key);
0266 }
0267 
0268 QString ContactGroup::Data::custom(const QString &key) const
0269 {
0270     return d->mCustoms.value(key);
0271 }
0272 
0273 ContactGroup::Data &ContactGroup::Data::operator=(const ContactGroup::Data &other)
0274 {
0275     if (this != &other) {
0276         d = other.d;
0277     }
0278 
0279     return *this;
0280 }
0281 
0282 bool ContactGroup::Data::operator==(const Data &other) const
0283 {
0284     return d->mName == other.d->mName //
0285         && d->mEmail == other.d->mEmail //
0286         && d->mCustoms == other.d->mCustoms;
0287 }
0288 
0289 class Q_DECL_HIDDEN ContactGroup::Private : public QSharedData
0290 {
0291 public:
0292     Private()
0293         : QSharedData()
0294         , mIdentifier(QUuid::createUuid().toString().mid(1, 36)) // We avoid the curly braces so the string is RFC4122 compliant and can be used as urn
0295     {
0296     }
0297 
0298     Private(const Private &other)
0299         : QSharedData(other)
0300     {
0301         mIdentifier = other.mIdentifier;
0302         mName = other.mName;
0303         mContactReferences = other.mContactReferences;
0304         mContactGroupReferences = other.mContactGroupReferences;
0305         mDataObjects = other.mDataObjects;
0306     }
0307 
0308     QString mIdentifier;
0309     QString mName;
0310     ContactGroup::ContactReference::List mContactReferences;
0311     ContactGroup::ContactGroupReference::List mContactGroupReferences;
0312     ContactGroup::Data::List mDataObjects;
0313 };
0314 
0315 ContactGroup::ContactGroup()
0316     : d(new Private)
0317 {
0318 }
0319 
0320 ContactGroup::ContactGroup(const ContactGroup &other)
0321     : d(other.d)
0322 {
0323 }
0324 
0325 ContactGroup::ContactGroup(const QString &name)
0326     : d(new Private)
0327 {
0328     d->mName = name;
0329 }
0330 
0331 ContactGroup::~ContactGroup()
0332 {
0333 }
0334 
0335 void ContactGroup::setName(const QString &name)
0336 {
0337     d->mName = name;
0338 }
0339 
0340 QString ContactGroup::name() const
0341 {
0342     return d->mName;
0343 }
0344 
0345 void ContactGroup::setId(const QString &id)
0346 {
0347     d->mIdentifier = id;
0348 }
0349 
0350 QString ContactGroup::id() const
0351 {
0352     return d->mIdentifier;
0353 }
0354 
0355 int ContactGroup::count() const
0356 {
0357     return d->mContactReferences.count() + d->mDataObjects.count();
0358 }
0359 
0360 int ContactGroup::contactReferenceCount() const
0361 {
0362     return d->mContactReferences.count();
0363 }
0364 
0365 int ContactGroup::contactGroupReferenceCount() const
0366 {
0367     return d->mContactGroupReferences.count();
0368 }
0369 
0370 int ContactGroup::dataCount() const
0371 {
0372     return d->mDataObjects.count();
0373 }
0374 
0375 ContactGroup::ContactReference &ContactGroup::contactReference(int index)
0376 {
0377     Q_ASSERT_X(index < d->mContactReferences.count(), "contactReference()", "index out of range");
0378 
0379     return d->mContactReferences[index];
0380 }
0381 
0382 const ContactGroup::ContactReference &ContactGroup::contactReference(int index) const
0383 {
0384     Q_ASSERT_X(index < d->mContactReferences.count(), "contactReference()", "index out of range");
0385 
0386     return d->mContactReferences[index];
0387 }
0388 
0389 ContactGroup::ContactGroupReference &ContactGroup::contactGroupReference(int index)
0390 {
0391     Q_ASSERT_X(index < d->mContactGroupReferences.count(), "contactGroupReference()", "index out of range");
0392 
0393     return d->mContactGroupReferences[index];
0394 }
0395 
0396 const ContactGroup::ContactGroupReference &ContactGroup::contactGroupReference(int index) const
0397 {
0398     Q_ASSERT_X(index < d->mContactGroupReferences.count(), "contactGroupReference()", "index out of range");
0399 
0400     return d->mContactGroupReferences[index];
0401 }
0402 
0403 ContactGroup::Data &ContactGroup::data(int index)
0404 {
0405     Q_ASSERT_X(index < d->mDataObjects.count(), "data()", "index out of range");
0406 
0407     return d->mDataObjects[index];
0408 }
0409 
0410 const ContactGroup::Data &ContactGroup::data(int index) const
0411 {
0412     Q_ASSERT_X(index < d->mDataObjects.count(), "data()", "index out of range");
0413 
0414     return d->mDataObjects[index];
0415 }
0416 
0417 void ContactGroup::append(const ContactReference &reference)
0418 {
0419     d->mContactReferences.append(reference);
0420 }
0421 
0422 void ContactGroup::append(const ContactGroupReference &reference)
0423 {
0424     d->mContactGroupReferences.append(reference);
0425 }
0426 
0427 void ContactGroup::append(const Data &data)
0428 {
0429     d->mDataObjects.append(data);
0430 }
0431 
0432 void ContactGroup::remove(const ContactReference &reference)
0433 {
0434     d->mContactReferences.removeOne(reference);
0435 }
0436 
0437 void ContactGroup::remove(const ContactGroupReference &reference)
0438 {
0439     d->mContactGroupReferences.removeOne(reference);
0440 }
0441 
0442 void ContactGroup::remove(const Data &data)
0443 {
0444     d->mDataObjects.removeOne(data);
0445 }
0446 
0447 void ContactGroup::removeAllContactReferences()
0448 {
0449     d->mContactReferences.clear();
0450 }
0451 
0452 void ContactGroup::removeAllContactGroupReferences()
0453 {
0454     d->mContactGroupReferences.clear();
0455 }
0456 
0457 void ContactGroup::removeAllContactData()
0458 {
0459     d->mDataObjects.clear();
0460 }
0461 
0462 ContactGroup &ContactGroup::operator=(const ContactGroup &other)
0463 {
0464     if (this != &other) {
0465         d = other.d;
0466     }
0467 
0468     return *this;
0469 }
0470 
0471 bool ContactGroup::operator==(const ContactGroup &other) const
0472 {
0473     return d->mIdentifier == other.d->mIdentifier //
0474         && d->mName == other.d->mName //
0475         && d->mContactReferences == other.d->mContactReferences //
0476         && d->mContactGroupReferences == other.d->mContactGroupReferences //
0477         && d->mDataObjects == other.d->mDataObjects;
0478 }
0479 
0480 QString ContactGroup::mimeType()
0481 {
0482     return QStringLiteral("application/x-vnd.kde.contactgroup");
0483 }