Make unit tests build again
[qtcontacts-tracker:qtcontacts-tracker.git] / qtrackerrelationshipsaverequest.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the Qt Mobility Components.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qtrackerrelationshipsaverequest.h"
43
44 #include <QtTracker/Tracker>
45 #include <QtTracker/ontologies/nco.h>
46 #include <QHash>
47 using namespace SopranoLive;
48
49 QTrackerRelationshipSaveRequest::QTrackerRelationshipSaveRequest(QContactAbstractRequest* req, QContactManagerEngine* parent)
50 : QObject(parent), QTrackerContactAsyncRequest(req)
51 {
52     Q_ASSERT(req);
53     Q_ASSERT(req->type() == QContactAbstractRequest::RelationshipSaveRequest);
54
55     QContactRelationshipSaveRequest* r = qobject_cast<QContactRelationshipSaveRequest*>(req);
56     if (!r)
57     {
58         commitError(QString());
59     }
60
61     QList<QContactRelationship> links = r->relationships();
62     if(links.isEmpty()) {
63         QMap<int, QContactManager::Error> errors;
64         errors[0] = QContactManager::BadArgumentError;
65         QContactManagerEngine::updateRelationshipSaveRequest(r, links, QContactManager::BadArgumentError, errors);
66         QContactManagerEngine::updateRequestState(req, QContactAbstractRequest::FinishedState);
67         return;
68     }
69     QContactManagerEngine::updateRequestState(req, QContactAbstractRequest::ActiveState);
70
71
72     // the logic is like this
73     // 1) get contacts (first() and second())
74     // 2) when that is done set all metacontacts of 1sts to 2nds
75
76     QSet<QString> ids;
77     foreach(QContactRelationship rel, links)
78     {
79         ids << QString::number(rel.first().localId());
80         ids << QString::number(rel.second().localId());
81     }
82
83     RDFVariable contact;
84     QStringList idstrings(QStringList(ids.toList()));
85     contact.property<nco::contactUID>().isMemberOf(idstrings);
86     m_nodes = ::tracker()->modelVariable(contact);
87
88     // need to store LiveNodes in order to receive notification from model
89     QObject::connect(m_nodes.model(), SIGNAL(modelUpdated()), this, SLOT(nodesDataReady()));
90
91 }
92
93 void QTrackerRelationshipSaveRequest::nodesDataReady()
94 {
95     // now that nodes are ready we can enumerate them without blocking
96     RDFTransactionPtr transaction_ = ::tracker()->initiateTransaction();
97     connect(transaction_.data(), SIGNAL(commitFinished()), this, SLOT(commitFinished()));
98     connect(transaction_.data(), SIGNAL(commitError(QString)), this, SLOT(commitError(QString)));
99
100     QHash<QString, Live<nco::PersonContact> > lContacts;
101     for(int i = 0; i < m_nodes->rowCount(); i++)
102     {
103         Live<nco::PersonContact> contact = m_nodes->liveNode(i);
104         lContacts[contact->getContactUID()] = contact;
105     }
106     QContactRelationshipSaveRequest* r = qobject_cast<QContactRelationshipSaveRequest*>(req);
107     if (!r)
108         commitError(QString());
109     QList<QContactRelationship> links = r->relationships();
110     foreach(QContactRelationship rel, links)
111     {
112         Live<nco::PersonContact> first = lContacts.value(QString::number(rel.first().localId()));
113         Live<nco::PersonContact> second = lContacts.value(QString::number(rel.second().localId()));
114         //TODO: we should prefer the local contact information over the remote info.
115         mergeContacts(first, second);
116     }
117
118     transaction_->commit(false);
119     // temporary fix - signals not yet implemented in libqttracker
120     commitFinished();
121 }
122
123 void QTrackerRelationshipSaveRequest::mergeContacts(const Live<nco::PersonContact>& preferedContact, const Live<nco::PersonContact>& inferiorContact)
124 {
125    QList<LiveNode> mergedNodes = preferedContact->getHasIMAddresss();
126    mergedNodes += inferiorContact->getHasIMAddresss();
127    preferedContact->setHasIMAddresss( mergedNodes );
128    inferiorContact->remove();
129 }
130
131 void QTrackerRelationshipSaveRequest::commitFinished()
132 {
133     QContactRelationshipSaveRequest* r = qobject_cast<QContactRelationshipSaveRequest*>(req);
134     if (r && r->isActive())
135     {
136         QContactManager::Error error = QContactManager::NoError;
137         QMap<int, QContactManager::Error> errors;
138         errors[0] = error;
139         QContactManagerEngine::updateRelationshipSaveRequest(r, r->relationships(), error, errors);
140         QContactManagerEngine::updateRequestState(req, QContactAbstractRequest::FinishedState);
141     }
142     else
143         qWarning()<<Q_FUNC_INFO<<r;
144 }
145
146 void QTrackerRelationshipSaveRequest::commitError(QString message)
147 {
148     qWarning()<<Q_FUNC_INFO<<message;
149     QContactRelationshipSaveRequest* r = qobject_cast<QContactRelationshipSaveRequest*>(req);
150     if (r)
151     {
152         QContactManager::Error error = QContactManager::InvalidRelationshipError;
153         QMap<int, QContactManager::Error> errors; 
154         errors[0] = error;
155         QContactManagerEngine::updateRelationshipSaveRequest(r, r->relationships(), error, errors);
156         QContactManagerEngine::updateRequestState(req, QContactAbstractRequest::FinishedState);
157     }
158     else
159     {
160         QMap<int, QContactManager::Error> errors;
161         errors[0] = QContactManager::UnspecifiedError;
162         QContactManagerEngine::updateRelationshipSaveRequest(r, QList<QContactRelationship>(), QContactManager::UnspecifiedError, errors);
163         QContactManagerEngine::updateRequestState(req, QContactAbstractRequest::FinishedState);
164         return;
165     }
166 }
167