qtmobility/plugins/contacts/qtcontacts-tracker/qtrackerrelationshipsaverequest.cpp
changeset 4 90517678cc4f
parent 1 2b40d63a9c3d
child 11 06b8e2af4411
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
    60 
    60 
    61     QList<QContactRelationship> links = r->relationships();
    61     QList<QContactRelationship> links = r->relationships();
    62     if(links.isEmpty()) {
    62     if(links.isEmpty()) {
    63         QMap<int, QContactManager::Error> errors;
    63         QMap<int, QContactManager::Error> errors;
    64         errors[0] = QContactManager::BadArgumentError;
    64         errors[0] = QContactManager::BadArgumentError;
    65         QContactManagerEngine::updateRelationshipSaveRequest(r, links, QContactManager::BadArgumentError, errors);
    65         QContactManagerEngine::updateRelationshipSaveRequest(r, links, QContactManager::BadArgumentError, errors, QContactAbstractRequest::FinishedState);
    66         QContactManagerEngine::updateRequestState(req, QContactAbstractRequest::FinishedState);
       
    67         return;
    66         return;
    68     }
    67     }
    69     QContactManagerEngine::updateRequestState(req, QContactAbstractRequest::ActiveState);
    68     QContactManagerEngine::updateRequestState(req, QContactAbstractRequest::ActiveState);
    70 
    69 
    71 
    70 
    81     }
    80     }
    82 
    81 
    83     RDFVariable contact;
    82     RDFVariable contact;
    84     QStringList idstrings(QStringList(ids.toList()));
    83     QStringList idstrings(QStringList(ids.toList()));
    85     contact.property<nco::contactUID>().isMemberOf(idstrings);
    84     contact.property<nco::contactUID>().isMemberOf(idstrings);
    86     nodes = ::tracker()->modelVariable(contact);
    85     m_nodes = ::tracker()->modelVariable(contact);
    87 
    86 
    88     // need to store LiveNodes in order to receive notification from model
    87     // need to store LiveNodes in order to receive notification from model
    89     QObject::connect(nodes.model(), SIGNAL(modelUpdated()), this, SLOT(nodesDataReady()));
    88     QObject::connect(m_nodes.model(), SIGNAL(modelUpdated()), this, SLOT(nodesDataReady()));
    90 
    89 
    91 }
    90 }
    92 
    91 
    93 void QTrackerRelationshipSaveRequest::nodesDataReady()
    92 void QTrackerRelationshipSaveRequest::nodesDataReady()
    94 {
    93 {
    96     RDFTransactionPtr transaction_ = ::tracker()->initiateTransaction();
    95     RDFTransactionPtr transaction_ = ::tracker()->initiateTransaction();
    97     connect(transaction_.data(), SIGNAL(commitFinished()), this, SLOT(commitFinished()));
    96     connect(transaction_.data(), SIGNAL(commitFinished()), this, SLOT(commitFinished()));
    98     connect(transaction_.data(), SIGNAL(commitError(QString)), this, SLOT(commitError(QString)));
    97     connect(transaction_.data(), SIGNAL(commitError(QString)), this, SLOT(commitError(QString)));
    99 
    98 
   100     QHash<QString, Live<nco::PersonContact> > lContacts;
    99     QHash<QString, Live<nco::PersonContact> > lContacts;
   101     for(int i = 0; i < nodes->rowCount();i++)
   100     for(int i = 0; i < m_nodes->rowCount(); i++)
   102     {
   101     {
   103         Live<nco::PersonContact> contact = nodes->liveNode(i);
   102         Live<nco::PersonContact> contact = m_nodes->liveNode(i);
   104         lContacts[contact->getContactUID()] = contact;
   103         lContacts[contact->getContactUID()] = contact;
   105     }
   104     }
   106     QContactRelationshipSaveRequest* r = qobject_cast<QContactRelationshipSaveRequest*>(req);
   105     QContactRelationshipSaveRequest* r = qobject_cast<QContactRelationshipSaveRequest*>(req);
   107     if (!r)
   106     if (!r)
   108         commitError(QString());
   107         commitError(QString());
   109     QList<QContactRelationship> links = r->relationships();
   108     QList<QContactRelationship> links = r->relationships();
   110     foreach(QContactRelationship rel, links)
   109     foreach(QContactRelationship rel, links)
   111     {
   110     {
   112         Live<nco::PersonContact> first = lContacts[QString::number(rel.first().localId())];
   111         Live<nco::PersonContact> first = lContacts.value(QString::number(rel.first().localId()));
   113         Live<nco::PersonContact> second = lContacts[QString::number(rel.second().localId())];
   112         Live<nco::PersonContact> second = lContacts.value(QString::number(rel.second().localId()));
   114         second->setMetacontact(first->getMetacontact());
   113         //TODO: we should prefer the local contact information over the remote info.
       
   114         mergeContacts(first, second);
   115     }
   115     }
   116 
   116 
   117     transaction_->commit(false);
   117     transaction_->commit(false);
   118     // temporary fix - signals not yet implemented in libqttracker
   118     // temporary fix - signals not yet implemented in libqttracker
   119     commitFinished();
   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();
   120 }
   128 }
   121 
   129 
   122 void QTrackerRelationshipSaveRequest::commitFinished()
   130 void QTrackerRelationshipSaveRequest::commitFinished()
   123 {
   131 {
   124     QContactRelationshipSaveRequest* r = qobject_cast<QContactRelationshipSaveRequest*>(req);
   132     QContactRelationshipSaveRequest* r = qobject_cast<QContactRelationshipSaveRequest*>(req);
   125     if (r && r->isActive())
   133     if (r && r->isActive())
   126     {
   134     {
   127         QContactManager::Error error = QContactManager::NoError;
   135         QContactManager::Error error = QContactManager::NoError;
   128         QMap<int, QContactManager::Error> errors;
   136         QMap<int, QContactManager::Error> errors;
   129         errors[0] = error;
   137         errors[0] = error;
   130         QContactManagerEngine::updateRelationshipSaveRequest(r, r->relationships(), error, errors);
   138         QContactManagerEngine::updateRelationshipSaveRequest(r, r->relationships(), error, errors, QContactAbstractRequest::FinishedState);
   131         QContactManagerEngine::updateRequestState(req, QContactAbstractRequest::FinishedState);
       
   132     }
   139     }
   133     else
   140     else
   134         qWarning()<<Q_FUNC_INFO<<r;
   141         qWarning()<<Q_FUNC_INFO<<r;
   135 }
   142 }
   136 
   143 
   141     if (r)
   148     if (r)
   142     {
   149     {
   143         QContactManager::Error error = QContactManager::InvalidRelationshipError;
   150         QContactManager::Error error = QContactManager::InvalidRelationshipError;
   144         QMap<int, QContactManager::Error> errors; 
   151         QMap<int, QContactManager::Error> errors; 
   145         errors[0] = error;
   152         errors[0] = error;
   146         QContactManagerEngine::updateRelationshipSaveRequest(r, r->relationships(), error, errors);
   153         QContactManagerEngine::updateRelationshipSaveRequest(r, r->relationships(), error, errors, QContactAbstractRequest::FinishedState);
   147         QContactManagerEngine::updateRequestState(req, QContactAbstractRequest::FinishedState);
       
   148     }
   154     }
   149     else
   155     else
   150     {
   156     {
   151         QMap<int, QContactManager::Error> errors;
   157         QMap<int, QContactManager::Error> errors;
   152         errors[0] = QContactManager::UnspecifiedError;
   158         errors[0] = QContactManager::UnspecifiedError;
   153         QContactManagerEngine::updateRelationshipSaveRequest(r, QList<QContactRelationship>(), QContactManager::UnspecifiedError, errors);
   159         QContactManagerEngine::updateRelationshipSaveRequest(r, QList<QContactRelationship>(), QContactManager::UnspecifiedError, errors, QContactAbstractRequest::FinishedState);
   154         QContactManagerEngine::updateRequestState(req, QContactAbstractRequest::FinishedState);
       
   155         return;
   160         return;
   156     }
   161     }
   157 }
   162 }
   158 
   163