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