bool DummyEngine::saveContact(QContact* contact, bool batch, QContactManager::Error* error) { // ensure that the contact's details conform to their definitions if (!validateContact(*contact, error)) { *error = QContactManager::InvalidDetailError; return false; } // success! QContactId newId; newId.setManagerUri(managerUri()); newId.setLocalId(5); contact->setId(newId); *error = QContactManager::NoError; // if we need to emit signals (ie, this isn't part of a batch operation) // then emit the correct one. if (!batch) { QList<QContactLocalId> emitList; emitList.append(contact->id().localId()); emit contactsAdded(emitList); } return true; }
bool QContactWinCEEngine::saveContact(QContact* contact, QContactManager::Error* error) { if (contact == 0) { *error = QContactManager::BadArgumentError; return false; } QContactChangeSet cs; // ensure that the contact's details conform to their definitions if (!validateContact(*contact, error)) { *error = QContactManager::InvalidDetailError; return false; } SimpleComPointer<IItem> icontact; bool wasOld = false; // Figure out if this is a new or old contact if (d->m_ids.contains(contact->localId())) { // update existing contact HRESULT hr = d->m_app->GetItemFromOidEx(contact->localId(), 0, &icontact); if (SUCCEEDED(hr)) { wasOld = true; } else { if (HRESULT_CODE(hr) == ERROR_NOT_FOUND) { // Well, doesn't exist any more *error = QContactManager::DoesNotExistError; d->m_ids.removeAll(contact->localId()); } else { qWarning() << "Didn't get old contact" << HRESULT_CODE(hr); *error = QContactManager::UnspecifiedError; } } } else if (contact->localId() == 0) { // new contact! SimpleComPointer<IDispatch> idisp = 0; HRESULT hr = d->m_collection->Add(&idisp); if (SUCCEEDED(hr)) { // now get an actual item out of it (was IContact, which is not IItem) hr = idisp->QueryInterface<IItem>(&icontact); if (SUCCEEDED(hr)) { } else { qWarning() << "Failed to query interface" << HRESULT_CODE(hr); *error = QContactManager::UnspecifiedError; } } else { qWarning() << "Failed to create contact: "<< HRESULT_CODE(hr); *error = QContactManager::OutOfMemoryError; } } else { // Saving a contact with a non zero id, but that doesn't exist *error = QContactManager::DoesNotExistError; } if (icontact) { // Convert our QContact to the Icontact (via setProps) if (convertFromQContact(*contact, icontact, *error)) { HRESULT hr = icontact->Save(); if (SUCCEEDED(hr)) { // yay! we also need to set the new contact id long oid = 0; hr = icontact->get_Oid(&oid); if (SUCCEEDED(hr)) { *error = QContactManager::NoError; QContact c = this->contact((QContactLocalId)oid, QContactFetchHint(), error); if (*error == QContactManager::NoError) { *contact = c; if (wasOld) { cs.insertChangedContact(contact->localId()); } else { cs.insertAddedContact(contact->localId()); d->m_ids.append(contact->localId()); } } cs.emitSignals(this); return true; } qWarning() << "Saved contact, but couldn't retrieve id again??" << HRESULT_CODE(hr); // Blargh. *error = QContactManager::UnspecifiedError; } else { qWarning() << "Failed to save contact" << HRESULT_CODE(hr); } } else { qWarning() << "Failed to convert contact"; } } // error should have been set. return false; }