Contacts Asynchronous APIIntroductionThe Contacts Asynchronous API enables a client to asynchronously fetch, update, or remove contact and relationship data from a contact manager. Use of the asynchronous API offers the programmer greater flexibility when requesting information from remote or slow local datastores. Using the APIThe asynchronous API offered by the Contacts module is available through classes derived from the QContactAbstractRequest class. It consists of two major sections:
The functionality offered by the synchronous API in these two categories is also available through the asynchronous API. There is one category of functionality which is not provided by the asynchronous API which is provided by the synchronous API: some information and reporting functionality is only provided through the synchronous API. For detailed information about the information and reporting functionality provided, please refer to the documentation for the Contacts Synchronous API. The functions to set and retrieve the id of the self-contact are also only provided by the synchronous API. Contact ManipulationThe most common type of operation that clients will perform involves retrieval or modification of contacts. For in-depth information about contact manipulation, please refer to the Contacts Synchronous API. There are four different types of operation which are supported by the asynchronous API:
These operations are supported via the QContactIdFetchRequest, QContactFetchRequest, QContactSaveRequest and QContactRemoveRequest classes, respectively. The synchronous API offered by the QContactManager class to allow manipulation of contacts consists of the following functions:
Relationship ManipulationContacts may be related in various ways. The contacts API allows clients to define relationships between contacts. For in-depth information about relationship manipulation, please refer to the Contacts Synchronous API. Support for relationships is backend specific. There are three different types of operation which are supported by the asynchronous API:
These operations are supported via the QContactRelationshipFetchRequest, QContactRelationshipSaveRequest and QContactRelationshipRemoveRequest classes respectively. The synchronous API offered by the QContactManager class to allow manipulation of relationships consists of the following functions:
Examples Of UsageFetching ContactsThe client sets up a request for contacts matching a specific criteria from a particular manager. Results from the request will be displayed to the user as they are received. void RequestExample::performRequest() { // retrieve any contact whose first name is "Alice" QContactDetailFilter dfil; dfil.setDetailType(QContactName::Type, QContactName::FieldFirstName); dfil.setValue("Alice"); dfil.setMatchFlags(QContactFilter::MatchExactly); // m_fetchRequest was created with m_fetchRequest = new QContactFetchRequest() in the ctor. m_fetchRequest->setManager(this->m_manager); // m_manager is a QContactManager*. m_fetchRequest->setFilter(dfil); connect(m_fetchRequest, SIGNAL(resultsAvailable()), this, SLOT(printContacts())); connect(m_fetchRequest, SIGNAL(stateChanged(QContactAbstractRequest::State)), this, SLOT(stateChanged(QContactAbstractRequest::State))); if (!m_fetchRequest->start()) { qDebug() << "Unable to request contacts!"; QCoreApplication::exit(0); } else { qDebug() << "Requested contacts; awaiting results..."; } } void RequestExample::printContacts() { QList<QContact> results = m_fetchRequest->contacts(); for (m_previousLastIndex = 0; m_previousLastIndex < results.size(); ++m_previousLastIndex) { qDebug() << "Found an Alice:" << results.at(m_previousLastIndex).displayLabel(); } } void RequestExample::stateChanged(QContactAbstractRequest::State state) { // once we've finished retrieving results, stop processing events. if (state == QContactAbstractRequest::FinishedState || state == QContactAbstractRequest::CanceledState) { qDebug() << "Finished displaying asynchronously retrieved contacts!"; QCoreApplication::exit(0); } } Other Asynchronous OperationsAll other asynchronous operations are performed in a similar manner to the previous example. A request of the desired type (which is derived from QContactAbstractRequest) is created, certain criteria are set which determine the intent of the request, and the signals of the request are connected to slots which deals with the results. The request can then be started. |