summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/plugin/src/cntrelationship.cpp
blob: dbb51bd102d0531420ced4a408f0f665c3fe3c45 (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
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** 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$
**
****************************************************************************/

#include <cntitem.h>

#include "cntrelationship.h"
#include "cntsymbiantransformerror.h"

/* ... The macros changed names */
#if QT_VERSION < QT_VERSION_CHECK(4, 6, 0)
#define QT_TRAP_THROWING QT_TRANSLATE_SYMBIAN_LEAVE_TO_EXCEPTION
#define QT_TRYCATCH_LEAVING QT_TRANSLATE_EXCEPTION_TO_SYMBIAN_LEAVE
#endif

/*!
 * Constructor
 *
 * \a contactDatabase CContactDatabase with established connection to the database
 * \a managerUri current manager uri
 */
CntRelationship::CntRelationship(CContactDatabase* contactDatabase, const QString &managerUri)
    :m_contactDatabase(contactDatabase),
    m_managerUri(managerUri)
{
    CntAbstractRelationship *relationshipGroup = new CntRelationshipGroup(contactDatabase, managerUri);
    m_relationshipMap.insert(relationshipGroup->relationshipType(), relationshipGroup);
}

/*!
 * Destructor
 */
CntRelationship::~CntRelationship()
{
    QMap<QString, CntAbstractRelationship *>::iterator itr;

    /* XXX maybe use qDeleteAll? */
    for (itr = m_relationshipMap.begin(); itr != m_relationshipMap.end(); ++itr)
    {
        CntAbstractRelationship* value = itr.value();
        delete value;
        value = 0;
    }
}

/*!
 * \return whether relationships of type \a relationshipType is supported by contacts of \a contactType
 */
bool CntRelationship::isRelationshipTypeSupported(const QString &relationshipType, const QString &contactType) const
{
    Q_UNUSED(contactType);
    return m_relationshipMap.contains(relationshipType);

    /* XXX Old code:
    QStringList supportedTypes;

    foreach(const QString& type, m_relationshipMap.keys()) {
        supportedTypes.append(type);
    }
    return supportedTypes;
    */
}

/* !
 * Retrive the contacts relationships
 *
 * \a relationshipType The type of the relationship
 * \a participantId The contact id
 * \a role The contact role
 * \a error Error returned
 */
QList<QContactRelationship> CntRelationship::relationships(const QString& relationshipType, const QContactId& participantId, QContactRelationship::Role role, QContactManager::Error* error) const
{
    QList<QContactRelationship> returnValue;
    *error = QContactManager::NoError;

    // if relationshipType is empty, relationships of any type are returned.
    if (relationshipType.isEmpty())
    {
        foreach (const QString& type, m_relationshipMap.keys())
        {
            // get the relationship
            CntAbstractRelationship *abstractRelationship = m_relationshipMap.value(type);

            // retrieve the relationships
            TRAPD(symbianError, returnValue.append(abstractRelationship->relationshipsL(participantId, role, error)));

            // if error translate it into a qt error
            if (symbianError != KErrNone){
                CntSymbianTransformError::transformError(symbianError, error);
            }
            
            // return empty list if there was an error
            if (*error != QContactManager::NoError && *error != QContactManager::DoesNotExistError) {
                return QList<QContactRelationship>();
            }
        }
        // if relationships found, update error
        if (!returnValue.isEmpty() && *error == QContactManager::DoesNotExistError) {
            // this can be the case if nothing is found for last relationship type
            *error = QContactManager::NoError;
        }
    }
    //check if we support the relationship
    else if (m_relationshipMap.contains(relationshipType))
    {
        //get the relationship
        CntAbstractRelationship *abstractRelationship = m_relationshipMap.value(relationshipType);

        //retrieve the relationships
        TRAPD(symbianError, returnValue = abstractRelationship->relationshipsL(participantId, role, error));

        //if error translate it into a qt error
        if (symbianError != KErrNone){
            CntSymbianTransformError::transformError(symbianError, error);
        }
    }
    else{
        *error = QContactManager::NotSupportedError;
    }
    
    // No relationships found?
    if (*error == QContactManager::NoError && returnValue.count() == 0 ) {
        *error = QContactManager::DoesNotExistError;
    }

    return returnValue;
}

/* !
 * Save the relationship
 *
 * \a affectedContactIds will include the affected contact ids
 * \a relationship to be saved
 * \a error Error returned
 */
bool CntRelationship::saveRelationship(QSet<QContactLocalId> *affectedContactIds, QContactRelationship* relationship, QContactManager::Error* error)
{
    bool returnValue(false);
    *error = QContactManager::NoError;
    if (validateRelationship(*relationship, error))
    {
        // Update manager uri to this manager if it is empty
        if (relationship->second().managerUri().isEmpty()) {
            QContactId second = relationship->second();
            second.setManagerUri(m_managerUri);
            relationship->setSecond(second);
        }
        
        //get the relationship
        CntAbstractRelationship *abstractRelationship = m_relationshipMap.value(relationship->relationshipType());
    
        //save the relationship
        TRAPD(symbianError, returnValue = abstractRelationship->saveRelationshipL(affectedContactIds, relationship, error));
    
        //if symbian error translate it into a qt error
        if (symbianError != KErrNone){
            returnValue = false;
            CntSymbianTransformError::transformError(symbianError, error);
        }
    }
    return returnValue;
}

/* !
 * Save many relationships
 *
 * \a affectedContactIds will include the affected contact ids
 * \a relationships to be saved
 * \a errorMap storage place for errors
 * \return true if there were no errors saving
 */
bool CntRelationship::saveRelationships(QSet<QContactLocalId> *affectedContactIds, QList<QContactRelationship>* relationships, QMap<int, QContactManager::Error>* errorMap, QContactManager::Error* error)
{
    QContactManager::Error singleError;    
    bool returnValue(true);
    bool hasManyRelationshipTypes(false);

    *error = QContactManager::NoError;

#ifdef SYMBIAN_BACKEND_USE_CNTMODEL_V2
    TInt symbianError = KErrNone;
    QString relationshipType;

    // validate relationships
    returnValue = validateRelationships(*relationships, relationshipType, error);
    hasManyRelationshipTypes = relationshipType.isEmpty();

    // if all relationships are valid and of the same type, we can try to use the batch save, which is much faster
    if (returnValue && !hasManyRelationshipTypes) {
        if (!relationships->isEmpty()) {
            CntAbstractRelationship *abstractRelationship = m_relationshipMap.value(relationshipType);
            TRAP(symbianError, returnValue = abstractRelationship->saveRelationshipsL(affectedContactIds, relationships, &singleError));
            returnValue = returnValue && singleError == QContactManager::NoError && symbianError == KErrNone;
        }
    }
#endif
    // if validation or batch save failed, or if there are many different relationship types,
    // the relationships need to be added one by one
    if (!returnValue || hasManyRelationshipTypes) {
        returnValue = true;
        for (int i = 0; i < relationships->count(); ++i) {
            // save the relationship
            saveRelationship(affectedContactIds, &(relationships->operator[](i)), &singleError);
            if (errorMap && singleError != QContactManager::NoError) {
                errorMap->insert(i, singleError);
            }
            
            // update the total error
            if (singleError != QContactManager::NoError) {
                *error = singleError;
                returnValue = false;
            } 
        }
    }

    return returnValue;
}

/* !
 * Remove the relationship
 *
 * \a affectedContactIds will include the affected contact ids
 * \a relationship to be removed
 * \a error Error returned
 * \return true if no error otherwise false
 */
bool CntRelationship::removeRelationship(QSet<QContactLocalId> *affectedContactIds, const QContactRelationship &relationship, QContactManager::Error* error)
{
    bool returnValue(false);
    *error = QContactManager::NoError;
    if (validateRelationship(relationship, error))
    {
        //get the relationship
        CntAbstractRelationship *abstractRelationship = m_relationshipMap.value(relationship.relationshipType());

        TRAPD(symbianError, returnValue = abstractRelationship->removeRelationshipL(affectedContactIds, relationship, error));

        //if symbian error translate it into a qt error
        if (symbianError != KErrNone){
            returnValue = false;
            CntSymbianTransformError::transformError(symbianError, error);
        }
    }
    return returnValue;
}

/* !
 * Remove many relationships
 *
 * \a affectedContactIds will include the affected contact ids
 * \a relationships to be removed
 * \a errorMap storage place for errors
 * \return true if there were no errors removing, false otherwise
 */
bool CntRelationship::removeRelationships(QSet<QContactLocalId> *affectedContactIds, const QList<QContactRelationship>& relationships, QMap<int, QContactManager::Error>* errorMap, QContactManager::Error* error)
{
    QContactManager::Error singleError;    
    bool returnValue(true);
    bool hasManyRelationshipTypes(false);

    *error = QContactManager::NoError;

#ifdef SYMBIAN_BACKEND_USE_CNTMODEL_V2
    TInt symbianError = KErrNone;
    QString relationshipType;

    // validate relationships
    returnValue = validateRelationships(relationships, relationshipType, error);
    hasManyRelationshipTypes = relationshipType.isEmpty();

    // if all relationships are valid and of the same type, we can try to use the batch remove, which is much faster
    if (returnValue && !hasManyRelationshipTypes) {
        if (!relationships.isEmpty()) {
            CntAbstractRelationship *abstractRelationship = m_relationshipMap.value(relationshipType);
            TRAP(symbianError, returnValue = abstractRelationship->removeRelationshipsL(affectedContactIds, relationships, &singleError));
            returnValue = returnValue && singleError == QContactManager::NoError && symbianError == KErrNone;
        }
    }
#endif
    // if validation or batch remove failed, or if there are many relationship types,
    // the relationships need to be removed one by one
    if (!returnValue || hasManyRelationshipTypes) {
        returnValue = true;
        for (int i = 0; i < relationships.count(); ++i) {
            //remove the relationships
            removeRelationship(affectedContactIds, relationships.at(i), &singleError);
            if (errorMap && singleError != QContactManager::NoError) {
                errorMap->insert(i, singleError);
            }
                
            // update the total error
            if (singleError != QContactManager::NoError) {
                returnValue = false;
                *error = singleError;
            }
        }
    }

    return returnValue;
}

bool CntRelationship::validateRelationship(const QContactRelationship &relationship, QContactManager::Error* error)
{
    *error = QContactManager::NoError;
    
    // check if supported in this manager
    if (!m_relationshipMap.contains(relationship.relationshipType())) {
        *error = QContactManager::NotSupportedError;
        return false;
    }
    
    QContactId first = relationship.first();
    QContactId second = relationship.second();    
    
    // zero id contacts not accepted
    if (!(first.localId() && second.localId())) {
        *error = QContactManager::InvalidRelationshipError;
        return false;
    }
    
    // "first" must be a contact in this manager
    if (!first.managerUri().isEmpty() && first.managerUri() != m_managerUri) {
        *error = QContactManager::InvalidRelationshipError;
        return false;
    }

    // "first" must be found in the database
    CContactItem* contact = 0;
    TRAP_IGNORE(contact = m_contactDatabase->ReadContactL(first.localId()));
    if (!contact) {
        *error = QContactManager::InvalidRelationshipError;
        return false;
    }
    delete contact;
    
    // check if "second" is in this manager 
    if (second.managerUri().isEmpty() || second.managerUri() == m_managerUri)
    {
        // circular relationships not allowed
        if (first.localId() == second.localId()) {
            *error = QContactManager::InvalidRelationshipError;
            return false;
        }
    
        // "second" must be found in the database
        contact = 0;
        TRAP_IGNORE(contact = m_contactDatabase->ReadContactL(second.localId()));
        if (!contact) {
            *error = QContactManager::InvalidRelationshipError;
            return false;
        }
        delete contact;        
    }
    
    // do additional checks in the actual implementation
    CntAbstractRelationship *abstractRelationship = m_relationshipMap.value(relationship.relationshipType());
    return abstractRelationship->validateRelationship(relationship, error);
}

#ifdef SYMBIAN_BACKEND_USE_CNTMODEL_V2
/*
 *  Validates the relationships. If all valid relationships are of the same type,
 *  the type will be passed back in relationshipType. Otherwise it will be empty.
 */
bool CntRelationship::validateRelationships(const QList<QContactRelationship> &relationships, QString &relationshipType, QContactManager::Error *error)
{
    QContactManager::Error validationError;
    bool hasManyRelationshipTypes = false;
    bool returnValue = true;

    foreach (QContactRelationship relationship, relationships) {
        if (validateRelationship(relationship, &validationError)) {
            // Update manager uri to this manager if it is empty
            if (relationship.second().managerUri().isEmpty()) {
                QContactId second = relationship.second();
                second.setManagerUri(m_managerUri);
                relationship.setSecond(second);
            }
            
            // get the relationship type
            if (relationship.relationshipType() != relationshipType && !relationshipType.isEmpty()) {
                hasManyRelationshipTypes = true;
            }
            else if (relationshipType.isEmpty()) {
                relationshipType = relationship.relationshipType();
            }
        }
        else {
            *error = validationError;
            returnValue = false;
        }
    }

    if (hasManyRelationshipTypes) {
        relationshipType.clear();
    }

    return returnValue;
}
#endif