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