summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/plugin/src/cntsymbiandatabase.cpp
blob: b2e821f6e4353cdf44f21aff361ac6e9f4f66a4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
//system includes
#include <e32base.h>
#include <s32mem.h>
#include <cntitem.h>

//user includes
#include "cntsymbiandatabase.h"
#include "cntsymbiantransformerror.h"
#include "qcontactchangeset.h"
#include "qcontactmanagerengine.h"

// Constant
typedef QPair<QContactLocalId, QContactLocalId> QOwnCardPair;

CntSymbianDatabase::CntSymbianDatabase(QContactManagerEngine *engine, QContactManager::Error* error) :
    m_engine(engine),
    m_contactDatabase(0),
    m_currentOwnCardId(0),
    m_restoreStarted(false)
{
    TRAPD(err, initializeL());
    CntSymbianTransformError::transformError(err, error);
}

void CntSymbianDatabase::initializeL()
{
    User::LeaveIfNull(m_engine);

#ifdef SYMBIAN_BACKEND_USE_CNTMODEL_V2
    // 10.x platforms do not need some of CContactDatabase's concepts, so
    // they use the optimized OpenV2 and CreateV2.
    TRAPD(err, m_contactDatabase = CContactDatabase::OpenV2L());
    // Database not found, create it
    if (err == KErrNotFound) {
        m_contactDatabase = CContactDatabase::CreateV2L();
    } else {
        User::LeaveIfError(err);
    }
#else
    TRAPD(err, m_contactDatabase = CContactDatabase::OpenL());

    // Database not found, create it
    if (err == KErrNotFound) {
        m_contactDatabase = CContactDatabase::CreateL();
    } else {
        User::LeaveIfError(err);
    }
#endif

#ifndef SYMBIAN_BACKEND_USE_CNTMODEL_V2
    // In pre 10.1 platforms the AddObserverL & RemoveObserver functions are not
    // exported so we need to use CContactChangeNotifier.
    TRAP(err, m_contactChangeNotifier = CContactChangeNotifier::NewL(*m_contactDatabase, this));
#else
    TRAP(err, m_contactDatabase->AddObserverL(*this));
    TRAP(err, m_contactDatabase->AddObserverV2L(*this));
#endif

    // Read current own card id (self contact id)
    TContactItemId myCard = m_contactDatabase->OwnCardId();
    if (myCard > 0)
        m_currentOwnCardId = QContactLocalId(myCard);

    // Currently the group membership check is only used in pre-10.1
    // platforms. In 10.1 we need to check the performance penalty
    // caused in the instantiation of QContactManager. If the
    // performance is too bad, then the MContactDbObserver API needs to
    // be changed in 10.1 so that we don't need the group membership
    // buffer in the engine level. In other words events like
    // EContactDbObserverEventGroupMembersAdded and 
    // EContactDbObserverEventGroupMembersRemoved need to be added to
    // MContactDbObserver.
#ifndef SYMBIAN_BACKEND_USE_CNTMODEL_V2
    updateGroupMembershipsL();
#endif
}

CntSymbianDatabase::~CntSymbianDatabase()
{
    m_engine = NULL;
#ifndef SYMBIAN_BACKEND_USE_CNTMODEL_V2
    delete m_contactChangeNotifier;
#else
    if (m_contactDatabase != 0) {
        m_contactDatabase->RemoveObserver(*this);
        m_contactDatabase->RemoveObserverV2(*this);
    }
#endif
    delete m_contactDatabase;
}

CContactDatabase* CntSymbianDatabase::contactDatabase()
{
    return m_contactDatabase;
}

void CntSymbianDatabase::appendContactsEmitted(const QList<QContactLocalId> &contactList)
{
    m_contactsEmitted += contactList;
}

void CntSymbianDatabase::appendContactEmitted(QContactLocalId id)
{
    m_contactsEmitted.append(id);
}

/*!
 * Respond to a contacts database event, delegating this event to
 * an appropriate signal as required.
 *
 * \param aEvent Contacts database event describing the change to the
 *  database.
 */
void CntSymbianDatabase::HandleDatabaseEventL(TContactDbObserverEvent aEvent)
{
    QContactChangeSet changeSet;
    TContactItemId id = aEvent.iContactId;

#ifdef SYMBIAN_BACKEND_SIGNAL_EMISSION_TWEAK
    switch (aEvent.iType)
    {
    case EContactDbObserverEventContactAdded:
        if(m_contactsEmitted.contains(id))
            m_contactsEmitted.removeOne(id);
        else
            changeSet.insertAddedContact(id);
        break;
    case EContactDbObserverEventOwnCardDeleted:
        if (m_contactsEmitted.contains(id)) {
            m_contactsEmitted.removeOne(id);
        } else {
            // signal selfContactIdChanged (from id to zero)
            QOwnCardPair ownCard(m_currentOwnCardId, QContactLocalId(0));
            changeSet.setOldAndNewSelfContactId(ownCard);
            // signal contactsRemoved (the self contact was deleted)
            changeSet.insertRemovedContact(id);
        }
        // reset own card id
        m_currentOwnCardId = QContactLocalId(0);
        break;
    case EContactDbObserverEventContactDeleted:
        if(m_contactsEmitted.contains(id))
            m_contactsEmitted.removeOne(id);
        else
            changeSet.insertRemovedContact(id);
        break;
    case EContactDbObserverEventContactChanged:
        if(m_contactsEmitted.contains(id))
            m_contactsEmitted.removeOne(id);
        else
            changeSet.insertChangedContact(id);
        break;
    case EContactDbObserverEventGroupAdded:
        if(m_contactsEmitted.contains(id)) {
            // adding a group triggers also a "changed" event. The work-around
            // is to leave the id to m_contactsEmitted
        } else {
            changeSet.insertAddedContact(id);
            m_contactsEmitted.append(id);
        }
        break;
    case EContactDbObserverEventGroupDeleted:
        if(m_contactsEmitted.contains(id))
            m_contactsEmitted.removeOne(id);
        else
            changeSet.insertRemovedContact(id);
        break;
    case EContactDbObserverEventGroupChanged:
				//handled in HandleDatabaseEventV2L
        break;
    case EContactDbObserverEventOwnCardChanged:
        if (m_contactsEmitted.contains(id)) {
            m_contactsEmitted.removeOne(id);
        }
        else {
            if (m_currentOwnCardId == QContactLocalId(id)) {
                //own card content was changed
                changeSet.insertChangedContact(m_currentOwnCardId);
            }
        
            QOwnCardPair ownCard(m_currentOwnCardId, QContactLocalId(id));
            changeSet.setOldAndNewSelfContactId(ownCard);
            m_currentOwnCardId = QContactLocalId(id);
        }
        break;
    case EContactDbObserverEventRestoreBeginning:    
        m_restoreStarted = true;
        break;
    case EContactDbObserverEventBackupRestoreCompleted:    
        if (m_restoreStarted) {
            changeSet.setDataChanged(true);
            m_restoreStarted = false;
        }
        break;
    default:
        break; // ignore other events
    }
#else // SYMBIAN_BACKEND_SIGNAL_EMISSION_TWEAK
    switch (aEvent.iType)
    {
    case EContactDbObserverEventContactAdded:
        changeSet.insertAddedContact(id);
        break;
    case EContactDbObserverEventOwnCardDeleted:
        {
            // signal selfContactIdChanged (from id to zero)
            QOwnCardPair ownCard(m_currentOwnCardId, QContactLocalId(0));
            changeSet.setOldAndNewSelfContactId(ownCard);
            // signal contactsRemoved (the self contact was deleted)
            changeSet.insertRemovedContact(id);
            // reset own card id
            m_currentOwnCardId = QContactLocalId(0);
        }
        break;
    case EContactDbObserverEventContactDeleted:
        {
            changeSet.insertRemovedContact(id);

            // Check if contact was part of some group. 
            // This check is needed because CContactDatabase will NOT
            // provide EContactDbObserverEventGroupChanged event in this case!!!
            QMap<QContactLocalId, QSet<QContactLocalId> >::iterator i;
            for (i=m_groupContents.begin(); i!=m_groupContents.end(); i++ ) {
                if (i->contains(id)) {
                    changeSet.insertRemovedRelationshipsContact(i.key());
                    changeSet.insertRemovedRelationshipsContacts(i->toList());
                    i->remove(id);
                }
            }
        }
        break;
    case EContactDbObserverEventContactChanged:
        changeSet.insertChangedContact(id);
        break;
    case EContactDbObserverEventGroupAdded:
        // Creating a group will cause two events.
        // Emitting addedContact from EContactDbObserverEventGroupChanged.
        changeSet.insertAddedContact(id);
        break;
    case EContactDbObserverEventGroupDeleted:
        {
            changeSet.insertRemovedContact(id);
            
            // Check if there was any contacts in the group
            if (m_groupContents.value(id).count()) {
                changeSet.insertRemovedRelationshipsContact(id);
                changeSet.insertRemovedRelationshipsContacts(m_groupContents.value(id).toList());
            }
            m_groupContents.remove(id);
        }
        break;
    case EContactDbObserverEventGroupChanged:
        {
            bool isOldGroup = m_groupContents.contains(id);

            // Contact DB observer API does not give information of contacts
            // possibly added to or removed from the group
            QSet<QContactLocalId> added;
            QSet<QContactLocalId> removed;
            TRAPD(err, updateGroupMembershipsL(id, added, removed));        
            if(err != KErrNone)
                changeSet.setDataChanged(true);

            if (removed.count()) {
                // The group changed event was caused by removing contacts
                // from the group
                changeSet.insertRemovedRelationshipsContact(id);
                changeSet.insertRemovedRelationshipsContacts(removed.toList());
            }
            if (added.count()) {
                // The group changed event was caused by adding contacts
                // to the group
                changeSet.insertAddedRelationshipsContact(id);
                changeSet.insertAddedRelationshipsContacts(added.toList());
            }
            if (added.count() == 0 && removed.count() == 0) {
                // The group changed event was caused by modifying the group
                // NOTE: Do not emit this for a new group. Creating a group
                // through the backend causes two events GroupAdded and 
                // GroupChanged.
                if (isOldGroup)
                    changeSet.insertChangedContact(id);
            }
        }
        break;
    case EContactDbObserverEventOwnCardChanged:
        if (m_currentOwnCardId == QContactLocalId(id))
            changeSet.insertChangedContact(m_currentOwnCardId);
        else
            changeSet.setOldAndNewSelfContactId(QOwnCardPair(m_currentOwnCardId, QContactLocalId(id)));
        m_currentOwnCardId = QContactLocalId(id);
        break;
    case EContactDbObserverEventRestoreBeginning:    
        m_restoreStarted = true;
        break;
    case EContactDbObserverEventBackupRestoreCompleted:    
        if (m_restoreStarted) {
            changeSet.setDataChanged(true);
            m_restoreStarted = false;
        }
        break;        
    default:
        break; // ignore other events
    }
#endif // SYMBIAN_BACKEND_SIGNAL_EMISSION_TWEAK
    
    changeSet.emitSignals(m_engine);
}

#ifdef SYMBIAN_BACKEND_USE_CNTMODEL_V2  
/*!
 * Respond to a contacts database extended event, delegating this event to
 * an appropriate signal as required.
 *
 * \param aEvent Contacts database extended event describing the change to the
 *  database.
 */
void CntSymbianDatabase::HandleDatabaseEventV2L(TContactDbObserverEventV2 aEvent)
{
    QContactChangeSet changeSet;
    switch (aEvent.iTypeV2)
    {
    case EContactDbObserverEventV2ContactsAddedToGroup:
        if (aEvent.iAdditionalContactIds != NULL) {
            if (m_contactsEmitted.contains(aEvent.iContactId))
                {
                // Group id is in the list of emitted events, so remove 
                // group id and all affected contacts from this list.
                m_contactsEmitted.removeOne(aEvent.iContactId);
                for (int i = 0; i < aEvent.iAdditionalContactIds->Count(); i++) {
                    int id = aEvent.iAdditionalContactIds->operator[](i);
                    m_contactsEmitted.removeOne(id);
                    }
                }
            else {
                // No signal was sent yet, update changeSet
                QList<QContactLocalId> affectedContactIds;
                affectedContactIds.append(aEvent.iContactId);
                for (int i = 0; i < aEvent.iAdditionalContactIds->Count(); i++) {
                    int id = aEvent.iAdditionalContactIds->operator[](i);
                    affectedContactIds.append(id);
                }
                changeSet.insertAddedRelationshipsContacts(affectedContactIds);
            }
        }
        break;
    case EContactDbObserverEventV2ContactsRemovedFromGroup:
        if (aEvent.iAdditionalContactIds != NULL) {
            if (m_contactsEmitted.contains(aEvent.iContactId))
                {
                // Group id is in the list of emitted events, so remove 
                // group id and all affected contacts from this list.
                m_contactsEmitted.removeOne(aEvent.iContactId);
                for (int i = 0; i < aEvent.iAdditionalContactIds->Count(); i++) {
                    int id = aEvent.iAdditionalContactIds->operator[](i);
                    m_contactsEmitted.removeOne(id);
                    }
                }
            else {
                // No signal was sent yet, update changeSet
                QList<QContactLocalId> affectedContactIds;
                affectedContactIds.append(aEvent.iContactId);
                for (int i = 0; i < aEvent.iAdditionalContactIds->Count(); i++) {
                    int id = aEvent.iAdditionalContactIds->operator[](i);
                    affectedContactIds.append(id);
                }
                changeSet.insertRemovedRelationshipsContacts(affectedContactIds);
            }
        }
        break;
    case EContactDbObserverEventV2GroupChanged:
        if(m_contactsEmitted.contains(aEvent.iContactId)) {
            m_contactsEmitted.removeOne(aEvent.iContactId);
        }
        else {
            changeSet.insertChangedContact(aEvent.iContactId);
        }
        break;
    default:
        break; // ignore other events   
    }
    changeSet.emitSignals(m_engine);
}
#endif

/*
 * Private implementation for updating the buffer containing the members of all
 * groups.
 */
void CntSymbianDatabase::updateGroupMembershipsL()
{
    CContactIdArray *groupIds = m_contactDatabase->GetGroupIdListL();
    for (TInt i(0); i < groupIds->Count(); ++i) {
        QContactLocalId id = (*groupIds)[i];
        QSet<QContactLocalId> dummySet;
        updateGroupMembershipsL(id, dummySet, dummySet);
    }
    delete groupIds;
}

/*
 * Private implementation for updating the buffer containing the members of a
 * group.
 */
void CntSymbianDatabase::updateGroupMembershipsL(
    QContactLocalId groupId,
    QSet<QContactLocalId> &added,
    QSet<QContactLocalId> &removed)
{
    QSet<QContactLocalId> groupMembersNew = groupMembersL(groupId);
    QSet<QContactLocalId> groupMembersOld = m_groupContents.value(groupId);

    if(groupMembersOld.count() < groupMembersNew.count())
        added = groupMembersNew - groupMembersOld;
    else if(groupMembersOld.count() > groupMembersNew.count())
        removed = groupMembersOld - groupMembersNew;

    m_groupContents.insert(groupId, groupMembersNew);
}

/*
 * Private implementation for fetching the members of a group.
 */
QSet<QContactLocalId> CntSymbianDatabase::groupMembersL(QContactLocalId groupId)
{
    QSet<QContactLocalId> groupMembers;

    CContactItem *contactItem = m_contactDatabase->ReadContactLC(TContactItemId(groupId));
    Q_ASSERT(contactItem && contactItem->Type() == KUidContactGroup);
    CContactGroup *group = static_cast<CContactGroup*>(contactItem);
    
    const CContactIdArray *idArray = group->ItemsContained();
    
    //loop through all the contacts and add them to the list
    for (int i(0); i < idArray->Count(); i++) {
        groupMembers.insert((*idArray)[i]);
    }
    CleanupStack::PopAndDestroy(contactItem);

    return groupMembers;
}