Changes: Drop usage of emitResultLater
[qtcontacts-tracker:qtcontacts-tracker.git] / src / engine / relationshipfetchrequest.cpp
1 /** This file is part of QtContacts tracker storage plugin
2  **
3  ** Copyright (c) 2009-2011 Nokia Corporation and/or its subsidiary(-ies).
4  **
5  ** Contact:  Nokia Corporation (info@qt.nokia.com)
6  **
7  ** GNU Lesser General Public License Usage
8  ** This file may be used under the terms of the GNU Lesser General Public License
9  ** version 2.1 as published by the Free Software Foundation and appearing in the
10  ** file LICENSE.LGPL included in the packaging of this file.  Please review the
11  ** following information to ensure the GNU Lesser General Public License version
12  ** 2.1 requirements will be met:
13  ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
14  **
15  ** In addition, as a special exception, Nokia gives you certain additional rights.
16  ** These rights are described in the Nokia Qt LGPL Exception version 1.1, included
17  ** in the file LGPL_EXCEPTION.txt in this package.
18  **
19  ** Other Usage
20  ** Alternatively, this file may be used in accordance with the terms and
21  ** conditions contained in a signed written agreement between you and Nokia.
22  **/
23
24 #include "relationshipfetchrequest.h"
25
26 #include "engine/engine.h"
27
28 #include <QtSparql>
29
30 ///////////////////////////////////////////////////////////////////////////////////////////////////
31
32 static bool isSupportedRelationshipType(const QString &relationshipType)
33 {
34     if (relationshipType.isEmpty() // wild card
35         || (relationshipType == QContactRelationship::HasMember)) {
36         return true;
37     }
38
39     // unsupported type
40     return false;
41 }
42
43
44 QTrackerRelationshipFetchRequest::QTrackerRelationshipFetchRequest(QContactAbstractRequest *request,
45                                                                    QContactTrackerEngine *engine,
46                                                                    QObject *parent)
47     : QTrackerBaseRequest<QContactRelationshipFetchRequest>(engine, parent)
48     , m_firstContactId(staticCast(request)->first())
49     , m_secondContactId(staticCast(request)->second())
50     , m_relationshipType(staticCast(request)->relationshipType())
51 {
52 }
53
54 QTrackerRelationshipFetchRequest::~QTrackerRelationshipFetchRequest()
55 {
56 }
57
58 void
59 QTrackerRelationshipFetchRequest::run()
60 {
61     if (canceled()) {
62         emitResult();
63         return;
64     }
65
66     const static QString requestTemplatePrefix = QLatin1String
67             ("SELECT tracker:id(?g) tracker:id(?c)\n"
68              "WHERE\n"
69              "{\n"
70              "  ?c a nco:Contact ; nco:belongsToGroup ?g .\n"
71              "  ?g a nco:Contact ; a nco:ContactGroup .\n");
72
73     const static QString requestTemplateSuffix = QLatin1String("}");
74     const static QString idTemplate = QLatin1String("  FILTER(tracker:id(%1) = %2) .\n");
75
76     if (not isSupportedRelationshipType(m_relationshipType)) {
77         qctWarn(QString::fromLatin1("Only HasMember relationships supported, got %1").
78                 arg(m_relationshipType));
79         setLastError(QContactManager::NotSupportedError);
80         emitResult();
81     }
82
83     QString queryString = requestTemplatePrefix;
84
85     // define group by id if given
86     if (m_firstContactId != QContactId()) {
87         queryString += idTemplate.arg(QLatin1String("?g"),
88                                       QString::number(m_firstContactId.localId()));
89     }
90
91     // define membercontact by id if given
92     if (m_secondContactId != QContactId()) {
93         queryString += idTemplate.arg(QLatin1String("?c"),
94                                       QString::number(m_secondContactId.localId()));
95     }
96
97     queryString += requestTemplateSuffix;
98
99     // run query
100     QScopedPointer<QSparqlResult> result(runQuery(QSparqlQuery(queryString), SyncQueryOptions));
101
102     if (result.isNull()) {
103         return; // runQuery() called reportError()
104     }
105
106     // fetch result
107     QContactId contactId;
108     contactId.setManagerUri(engine()->managerUri());
109
110     while (not canceled() && result->next()) {
111         if (engine()->hasDebugFlag(QContactTrackerEngine::ShowModels)) {
112             qDebug() << result->current();
113         }
114
115         bool ok0, ok1;
116
117         QContactLocalId localIdFirst = result->value(0).toUInt(&ok0);
118         QContactLocalId localIdSecond = result->value(1).toUInt(&ok1);
119
120         if (ok0 && ok1 && (localIdFirst!=localIdSecond)) {
121             QContactRelationship relationship;
122             contactId.setLocalId(localIdFirst);
123             relationship.setFirst(contactId);
124
125             contactId.setLocalId(localIdSecond);
126             relationship.setSecond(contactId);
127
128             relationship.setRelationshipType(QContactRelationship::HasMember);
129
130             m_relationships.append(relationship);
131         }
132     }
133
134     emitResult();
135 }
136
137
138 void
139 QTrackerRelationshipFetchRequest::updateRequest(QContactManager::Error error)
140 {
141     engine()->updateRelationshipFetchRequest(staticCast(engine()->request(this).data()),
142                                              m_relationships, error, QContactAbstractRequest::FinishedState);
143 }