qtmobility/plugins/contacts/qtcontacts-tracker/qtrackerrelationshipsaverequest.cpp
changeset 14 6fbed849b4f4
parent 11 06b8e2af4411
child 15 1f895d8a5b2b
equal deleted inserted replaced
11:06b8e2af4411 14:6fbed849b4f4
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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, QContactAbstractRequest::FinishedState);
       
    66         return;
       
    67     }
       
    68     QContactManagerEngine::updateRequestState(req, QContactAbstractRequest::ActiveState);
       
    69 
       
    70 
       
    71     // the logic is like this
       
    72     // 1) get contacts (first() and second())
       
    73     // 2) when that is done set all metacontacts of 1sts to 2nds
       
    74 
       
    75     QSet<QString> ids;
       
    76     foreach(QContactRelationship rel, links)
       
    77     {
       
    78         ids << QString::number(rel.first().localId());
       
    79         ids << QString::number(rel.second().localId());
       
    80     }
       
    81 
       
    82     RDFVariable contact;
       
    83     QStringList idstrings(QStringList(ids.toList()));
       
    84     contact.property<nco::contactUID>().isMemberOf(idstrings);
       
    85     m_nodes = ::tracker()->modelVariable(contact);
       
    86 
       
    87     // need to store LiveNodes in order to receive notification from model
       
    88     QObject::connect(m_nodes.model(), SIGNAL(modelUpdated()), this, SLOT(nodesDataReady()));
       
    89 
       
    90 }
       
    91 
       
    92 void QTrackerRelationshipSaveRequest::nodesDataReady()
       
    93 {
       
    94     // now that nodes are ready we can enumerate them without blocking
       
    95     RDFTransactionPtr transaction_ = ::tracker()->initiateTransaction();
       
    96     connect(transaction_.data(), SIGNAL(commitFinished()), this, SLOT(commitFinished()));
       
    97     connect(transaction_.data(), SIGNAL(commitError(QString)), this, SLOT(commitError(QString)));
       
    98 
       
    99     QHash<QString, Live<nco::PersonContact> > lContacts;
       
   100     for(int i = 0; i < m_nodes->rowCount(); i++)
       
   101     {
       
   102         Live<nco::PersonContact> contact = m_nodes->liveNode(i);
       
   103         lContacts[contact->getContactUID()] = contact;
       
   104     }
       
   105     QContactRelationshipSaveRequest* r = qobject_cast<QContactRelationshipSaveRequest*>(req);
       
   106     if (!r)
       
   107         commitError(QString());
       
   108     QList<QContactRelationship> links = r->relationships();
       
   109     foreach(QContactRelationship rel, links)
       
   110     {
       
   111         Live<nco::PersonContact> first = lContacts.value(QString::number(rel.first().localId()));
       
   112         Live<nco::PersonContact> second = lContacts.value(QString::number(rel.second().localId()));
       
   113         //TODO: we should prefer the local contact information over the remote info.
       
   114         mergeContacts(first, second);
       
   115     }
       
   116 
       
   117     transaction_->commit(false);
       
   118     // temporary fix - signals not yet implemented in libqttracker
       
   119     commitFinished();
       
   120 }
       
   121 
       
   122 void QTrackerRelationshipSaveRequest::mergeContacts(const Live<nco::PersonContact>& preferedContact, const Live<nco::PersonContact>& inferiorContact)
       
   123 {
       
   124    QList<LiveNode> mergedNodes = preferedContact->getHasIMAddresss();
       
   125    mergedNodes += inferiorContact->getHasIMAddresss();
       
   126    preferedContact->setHasIMAddresss( mergedNodes );
       
   127    inferiorContact->remove();
       
   128 }
       
   129 
       
   130 void QTrackerRelationshipSaveRequest::commitFinished()
       
   131 {
       
   132     QContactRelationshipSaveRequest* r = qobject_cast<QContactRelationshipSaveRequest*>(req);
       
   133     if (r && r->isActive())
       
   134     {
       
   135         QContactManager::Error error = QContactManager::NoError;
       
   136         QMap<int, QContactManager::Error> errors;
       
   137         errors[0] = error;
       
   138         QContactManagerEngine::updateRelationshipSaveRequest(r, r->relationships(), error, errors, QContactAbstractRequest::FinishedState);
       
   139     }
       
   140     else
       
   141         qWarning()<<Q_FUNC_INFO<<r;
       
   142 }
       
   143 
       
   144 void QTrackerRelationshipSaveRequest::commitError(QString message)
       
   145 {
       
   146     qWarning()<<Q_FUNC_INFO<<message;
       
   147     QContactRelationshipSaveRequest* r = qobject_cast<QContactRelationshipSaveRequest*>(req);
       
   148     if (r)
       
   149     {
       
   150         QContactManager::Error error = QContactManager::InvalidRelationshipError;
       
   151         QMap<int, QContactManager::Error> errors; 
       
   152         errors[0] = error;
       
   153         QContactManagerEngine::updateRelationshipSaveRequest(r, r->relationships(), error, errors, QContactAbstractRequest::FinishedState);
       
   154     }
       
   155     else
       
   156     {
       
   157         QMap<int, QContactManager::Error> errors;
       
   158         errors[0] = QContactManager::UnspecifiedError;
       
   159         QContactManagerEngine::updateRelationshipSaveRequest(r, QList<QContactRelationship>(), QContactManager::UnspecifiedError, errors, QContactAbstractRequest::FinishedState);
       
   160         return;
       
   161     }
       
   162 }
       
   163