| 1 | /* |
| 2 | This file is part of the KContacts framework. |
| 3 | SPDX-FileCopyrightText: 2008 Tobias Koenig <tokoe@kde.org> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #include "contactgroup.h" |
| 9 | |
| 10 | #include <QMap> |
| 11 | #include <QSharedData> |
| 12 | #include <QString> |
| 13 | #include <QUuid> |
| 14 | |
| 15 | using namespace KContacts; |
| 16 | |
| 17 | class Q_DECL_HIDDEN ContactGroup::ContactReference::ContactReferencePrivate : public QSharedData |
| 18 | { |
| 19 | public: |
| 20 | ContactReferencePrivate() |
| 21 | : QSharedData() |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | ContactReferencePrivate(const ContactReferencePrivate &other) |
| 26 | : QSharedData(other) |
| 27 | { |
| 28 | mUid = other.mUid; |
| 29 | mPreferredEmail = other.mPreferredEmail; |
| 30 | mCustoms = other.mCustoms; |
| 31 | } |
| 32 | |
| 33 | QString mUid; |
| 34 | QString mGid; |
| 35 | QString mPreferredEmail; |
| 36 | QMap<QString, QString> mCustoms; |
| 37 | }; |
| 38 | |
| 39 | ContactGroup::ContactReference::ContactReference() |
| 40 | : d(new ContactReferencePrivate) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | ContactGroup::ContactReference::ContactReference(const ContactReference &other) |
| 45 | : d(other.d) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | ContactGroup::ContactReference::ContactReference(const QString &uid) |
| 50 | : d(new ContactReferencePrivate) |
| 51 | { |
| 52 | d->mUid = uid; |
| 53 | } |
| 54 | |
| 55 | ContactGroup::ContactReference::~ContactReference() |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | void ContactGroup::ContactReference::setUid(const QString &uid) |
| 60 | { |
| 61 | d->mUid = uid; |
| 62 | } |
| 63 | |
| 64 | QString ContactGroup::ContactReference::uid() const |
| 65 | { |
| 66 | return d->mUid; |
| 67 | } |
| 68 | |
| 69 | void ContactGroup::ContactReference::setGid(const QString &gid) |
| 70 | { |
| 71 | d->mGid = gid; |
| 72 | } |
| 73 | |
| 74 | QString ContactGroup::ContactReference::gid() const |
| 75 | { |
| 76 | return d->mGid; |
| 77 | } |
| 78 | |
| 79 | void ContactGroup::ContactReference::setPreferredEmail(const QString &email) |
| 80 | { |
| 81 | d->mPreferredEmail = email; |
| 82 | } |
| 83 | |
| 84 | QString ContactGroup::ContactReference::preferredEmail() const |
| 85 | { |
| 86 | return d->mPreferredEmail; |
| 87 | } |
| 88 | |
| 89 | void ContactGroup::ContactReference::insertCustom(const QString &key, const QString &value) |
| 90 | { |
| 91 | d->mCustoms.insert(key, value); |
| 92 | } |
| 93 | |
| 94 | void ContactGroup::ContactReference::removeCustom(const QString &key) |
| 95 | { |
| 96 | d->mCustoms.remove(key); |
| 97 | } |
| 98 | |
| 99 | QString ContactGroup::ContactReference::custom(const QString &key) const |
| 100 | { |
| 101 | return d->mCustoms.value(key); |
| 102 | } |
| 103 | |
| 104 | ContactGroup::ContactReference &ContactGroup::ContactReference::operator=(const ContactGroup::ContactReference &other) |
| 105 | { |
| 106 | if (this != &other) { |
| 107 | d = other.d; |
| 108 | } |
| 109 | |
| 110 | return *this; |
| 111 | } |
| 112 | |
| 113 | bool ContactGroup::ContactReference::operator==(const ContactReference &other) const |
| 114 | { |
| 115 | return d->mUid == other.d->mUid && d->mPreferredEmail == other.d->mPreferredEmail && d->mCustoms == other.d->mCustoms; |
| 116 | } |
| 117 | |
| 118 | class Q_DECL_HIDDEN ContactGroup::ContactGroupReference::ContactGroupReferencePrivate : public QSharedData |
| 119 | { |
| 120 | public: |
| 121 | ContactGroupReferencePrivate() |
| 122 | : QSharedData() |
| 123 | { |
| 124 | } |
| 125 | |
| 126 | ContactGroupReferencePrivate(const ContactGroupReferencePrivate &other) |
| 127 | : QSharedData(other) |
| 128 | { |
| 129 | mUid = other.mUid; |
| 130 | mCustoms = other.mCustoms; |
| 131 | } |
| 132 | |
| 133 | QString mUid; |
| 134 | QMap<QString, QString> mCustoms; |
| 135 | }; |
| 136 | |
| 137 | ContactGroup::ContactGroupReference::ContactGroupReference() |
| 138 | : d(new ContactGroupReferencePrivate) |
| 139 | { |
| 140 | } |
| 141 | |
| 142 | ContactGroup::ContactGroupReference::ContactGroupReference(const ContactGroupReference &other) |
| 143 | : d(other.d) |
| 144 | { |
| 145 | } |
| 146 | |
| 147 | ContactGroup::ContactGroupReference::ContactGroupReference(const QString &uid) |
| 148 | : d(new ContactGroupReferencePrivate) |
| 149 | { |
| 150 | d->mUid = uid; |
| 151 | } |
| 152 | |
| 153 | ContactGroup::ContactGroupReference::~ContactGroupReference() |
| 154 | { |
| 155 | } |
| 156 | |
| 157 | void ContactGroup::ContactGroupReference::setUid(const QString &uid) |
| 158 | { |
| 159 | d->mUid = uid; |
| 160 | } |
| 161 | |
| 162 | QString ContactGroup::ContactGroupReference::uid() const |
| 163 | { |
| 164 | return d->mUid; |
| 165 | } |
| 166 | |
| 167 | void ContactGroup::ContactGroupReference::insertCustom(const QString &key, const QString &value) |
| 168 | { |
| 169 | d->mCustoms.insert(key, value); |
| 170 | } |
| 171 | |
| 172 | void ContactGroup::ContactGroupReference::removeCustom(const QString &key) |
| 173 | { |
| 174 | d->mCustoms.remove(key); |
| 175 | } |
| 176 | |
| 177 | QString ContactGroup::ContactGroupReference::custom(const QString &key) const |
| 178 | { |
| 179 | return d->mCustoms.value(key); |
| 180 | } |
| 181 | |
| 182 | ContactGroup::ContactGroupReference &ContactGroup::ContactGroupReference::operator=(const ContactGroup::ContactGroupReference &other) |
| 183 | { |
| 184 | if (this != &other) { |
| 185 | d = other.d; |
| 186 | } |
| 187 | |
| 188 | return *this; |
| 189 | } |
| 190 | |
| 191 | bool ContactGroup::ContactGroupReference::operator==(const ContactGroupReference &other) const |
| 192 | { |
| 193 | return d->mUid == other.d->mUid && d->mCustoms == other.d->mCustoms; |
| 194 | } |
| 195 | |
| 196 | class Q_DECL_HIDDEN ContactGroup::Data::DataPrivate : public QSharedData |
| 197 | { |
| 198 | public: |
| 199 | DataPrivate() |
| 200 | : QSharedData() |
| 201 | { |
| 202 | } |
| 203 | |
| 204 | DataPrivate(const DataPrivate &other) |
| 205 | : QSharedData(other) |
| 206 | { |
| 207 | mName = other.mName; |
| 208 | mEmail = other.mEmail; |
| 209 | mCustoms = other.mCustoms; |
| 210 | } |
| 211 | |
| 212 | QString mName; |
| 213 | QString mEmail; |
| 214 | QMap<QString, QString> mCustoms; |
| 215 | }; |
| 216 | |
| 217 | ContactGroup::Data::Data() |
| 218 | : d(new DataPrivate) |
| 219 | { |
| 220 | } |
| 221 | |
| 222 | ContactGroup::Data::Data(const Data &other) |
| 223 | : d(other.d) |
| 224 | { |
| 225 | } |
| 226 | |
| 227 | ContactGroup::Data::Data(const QString &name, const QString &email) |
| 228 | : d(new DataPrivate) |
| 229 | { |
| 230 | d->mName = name; |
| 231 | d->mEmail = email; |
| 232 | } |
| 233 | |
| 234 | ContactGroup::Data::~Data() |
| 235 | { |
| 236 | } |
| 237 | |
| 238 | void ContactGroup::Data::setName(const QString &name) |
| 239 | { |
| 240 | d->mName = name; |
| 241 | } |
| 242 | |
| 243 | QString ContactGroup::Data::name() const |
| 244 | { |
| 245 | return d->mName; |
| 246 | } |
| 247 | |
| 248 | void ContactGroup::Data::setEmail(const QString &email) |
| 249 | { |
| 250 | d->mEmail = email; |
| 251 | } |
| 252 | |
| 253 | QString ContactGroup::Data::email() const |
| 254 | { |
| 255 | return d->mEmail; |
| 256 | } |
| 257 | |
| 258 | void ContactGroup::Data::insertCustom(const QString &key, const QString &value) |
| 259 | { |
| 260 | d->mCustoms.insert(key, value); |
| 261 | } |
| 262 | |
| 263 | void ContactGroup::Data::removeCustom(const QString &key) |
| 264 | { |
| 265 | d->mCustoms.remove(key); |
| 266 | } |
| 267 | |
| 268 | QString ContactGroup::Data::custom(const QString &key) const |
| 269 | { |
| 270 | return d->mCustoms.value(key); |
| 271 | } |
| 272 | |
| 273 | ContactGroup::Data &ContactGroup::Data::operator=(const ContactGroup::Data &other) |
| 274 | { |
| 275 | if (this != &other) { |
| 276 | d = other.d; |
| 277 | } |
| 278 | |
| 279 | return *this; |
| 280 | } |
| 281 | |
| 282 | bool ContactGroup::Data::operator==(const Data &other) const |
| 283 | { |
| 284 | return d->mName == other.d->mName // |
| 285 | && d->mEmail == other.d->mEmail // |
| 286 | && d->mCustoms == other.d->mCustoms; |
| 287 | } |
| 288 | |
| 289 | class Q_DECL_HIDDEN ContactGroup::Private : public QSharedData |
| 290 | { |
| 291 | public: |
| 292 | Private() |
| 293 | : QSharedData() |
| 294 | , mIdentifier(QUuid::createUuid().toString().mid(position: 1, n: 36)) // We avoid the curly braces so the string is RFC4122 compliant and can be used as urn |
| 295 | { |
| 296 | } |
| 297 | |
| 298 | Private(const Private &other) |
| 299 | : QSharedData(other) |
| 300 | { |
| 301 | mIdentifier = other.mIdentifier; |
| 302 | mName = other.mName; |
| 303 | mContactReferences = other.mContactReferences; |
| 304 | mContactGroupReferences = other.mContactGroupReferences; |
| 305 | mDataObjects = other.mDataObjects; |
| 306 | } |
| 307 | |
| 308 | QString mIdentifier; |
| 309 | QString mName; |
| 310 | ContactGroup::ContactReference::List mContactReferences; |
| 311 | ContactGroup::ContactGroupReference::List mContactGroupReferences; |
| 312 | ContactGroup::Data::List mDataObjects; |
| 313 | }; |
| 314 | |
| 315 | ContactGroup::ContactGroup() |
| 316 | : d(new Private) |
| 317 | { |
| 318 | } |
| 319 | |
| 320 | ContactGroup::ContactGroup(const ContactGroup &other) |
| 321 | : d(other.d) |
| 322 | { |
| 323 | } |
| 324 | |
| 325 | ContactGroup::ContactGroup(const QString &name) |
| 326 | : d(new Private) |
| 327 | { |
| 328 | d->mName = name; |
| 329 | } |
| 330 | |
| 331 | ContactGroup::~ContactGroup() |
| 332 | { |
| 333 | } |
| 334 | |
| 335 | void ContactGroup::setName(const QString &name) |
| 336 | { |
| 337 | d->mName = name; |
| 338 | } |
| 339 | |
| 340 | QString ContactGroup::name() const |
| 341 | { |
| 342 | return d->mName; |
| 343 | } |
| 344 | |
| 345 | void ContactGroup::setId(const QString &id) |
| 346 | { |
| 347 | d->mIdentifier = id; |
| 348 | } |
| 349 | |
| 350 | QString ContactGroup::id() const |
| 351 | { |
| 352 | return d->mIdentifier; |
| 353 | } |
| 354 | |
| 355 | int ContactGroup::count() const |
| 356 | { |
| 357 | return d->mContactReferences.count() + d->mDataObjects.count(); |
| 358 | } |
| 359 | |
| 360 | int ContactGroup::contactReferenceCount() const |
| 361 | { |
| 362 | return d->mContactReferences.count(); |
| 363 | } |
| 364 | |
| 365 | int ContactGroup::contactGroupReferenceCount() const |
| 366 | { |
| 367 | return d->mContactGroupReferences.count(); |
| 368 | } |
| 369 | |
| 370 | int ContactGroup::dataCount() const |
| 371 | { |
| 372 | return d->mDataObjects.count(); |
| 373 | } |
| 374 | |
| 375 | ContactGroup::ContactReference &ContactGroup::contactReference(int index) |
| 376 | { |
| 377 | Q_ASSERT_X(index < d->mContactReferences.count(), "contactReference()" , "index out of range" ); |
| 378 | |
| 379 | return d->mContactReferences[index]; |
| 380 | } |
| 381 | |
| 382 | const ContactGroup::ContactReference &ContactGroup::contactReference(int index) const |
| 383 | { |
| 384 | Q_ASSERT_X(index < d->mContactReferences.count(), "contactReference()" , "index out of range" ); |
| 385 | |
| 386 | return d->mContactReferences[index]; |
| 387 | } |
| 388 | |
| 389 | ContactGroup::ContactGroupReference &ContactGroup::contactGroupReference(int index) |
| 390 | { |
| 391 | Q_ASSERT_X(index < d->mContactGroupReferences.count(), "contactGroupReference()" , "index out of range" ); |
| 392 | |
| 393 | return d->mContactGroupReferences[index]; |
| 394 | } |
| 395 | |
| 396 | const ContactGroup::ContactGroupReference &ContactGroup::contactGroupReference(int index) const |
| 397 | { |
| 398 | Q_ASSERT_X(index < d->mContactGroupReferences.count(), "contactGroupReference()" , "index out of range" ); |
| 399 | |
| 400 | return d->mContactGroupReferences[index]; |
| 401 | } |
| 402 | |
| 403 | ContactGroup::Data &ContactGroup::data(int index) |
| 404 | { |
| 405 | Q_ASSERT_X(index < d->mDataObjects.count(), "data()" , "index out of range" ); |
| 406 | |
| 407 | return d->mDataObjects[index]; |
| 408 | } |
| 409 | |
| 410 | const ContactGroup::Data &ContactGroup::data(int index) const |
| 411 | { |
| 412 | Q_ASSERT_X(index < d->mDataObjects.count(), "data()" , "index out of range" ); |
| 413 | |
| 414 | return d->mDataObjects[index]; |
| 415 | } |
| 416 | |
| 417 | void ContactGroup::append(const ContactReference &reference) |
| 418 | { |
| 419 | d->mContactReferences.append(t: reference); |
| 420 | } |
| 421 | |
| 422 | void ContactGroup::append(const ContactGroupReference &reference) |
| 423 | { |
| 424 | d->mContactGroupReferences.append(t: reference); |
| 425 | } |
| 426 | |
| 427 | void ContactGroup::append(const Data &data) |
| 428 | { |
| 429 | d->mDataObjects.append(t: data); |
| 430 | } |
| 431 | |
| 432 | void ContactGroup::remove(const ContactReference &reference) |
| 433 | { |
| 434 | d->mContactReferences.removeOne(t: reference); |
| 435 | } |
| 436 | |
| 437 | void ContactGroup::remove(const ContactGroupReference &reference) |
| 438 | { |
| 439 | d->mContactGroupReferences.removeOne(t: reference); |
| 440 | } |
| 441 | |
| 442 | void ContactGroup::remove(const Data &data) |
| 443 | { |
| 444 | d->mDataObjects.removeOne(t: data); |
| 445 | } |
| 446 | |
| 447 | void ContactGroup::removeAllContactReferences() |
| 448 | { |
| 449 | d->mContactReferences.clear(); |
| 450 | } |
| 451 | |
| 452 | void ContactGroup::removeAllContactGroupReferences() |
| 453 | { |
| 454 | d->mContactGroupReferences.clear(); |
| 455 | } |
| 456 | |
| 457 | void ContactGroup::removeAllContactData() |
| 458 | { |
| 459 | d->mDataObjects.clear(); |
| 460 | } |
| 461 | |
| 462 | ContactGroup &ContactGroup::operator=(const ContactGroup &other) |
| 463 | { |
| 464 | if (this != &other) { |
| 465 | d = other.d; |
| 466 | } |
| 467 | |
| 468 | return *this; |
| 469 | } |
| 470 | |
| 471 | bool ContactGroup::operator==(const ContactGroup &other) const |
| 472 | { |
| 473 | return d->mIdentifier == other.d->mIdentifier // |
| 474 | && d->mName == other.d->mName // |
| 475 | && d->mContactReferences == other.d->mContactReferences // |
| 476 | && d->mContactGroupReferences == other.d->mContactGroupReferences // |
| 477 | && d->mDataObjects == other.d->mDataObjects; |
| 478 | } |
| 479 | |
| 480 | QString ContactGroup::mimeType() |
| 481 | { |
| 482 | return QStringLiteral("application/x-vnd.kde.contactgroup" ); |
| 483 | } |
| 484 | |