Development/AkonadiPorting/AddressBook: Difference between revisions
Line 52: | Line 52: | ||
=== Find By Email === | === Find By Email === | ||
Another common use case is searching for matching contacts by email address. The code for that with KABC classes is almost identical: | |||
<code cppqt> | |||
KABC::AddressBook *addressBook = KABC::StdAddressBook::self(); | |||
KABC::Addressee contact = addressBook->findByEmail( emailString ); | |||
</code> | |||
Unsurprisingly, the Akonadi code looks also quite similar to the previous use case: | |||
<code cppqt> | |||
Akonadi::ContactSearchJob *job = new Akonadi::ContactSearchJob( this ); | |||
job->setQuery( Akonadi::ContactSearchJob::Email, emailString ); | |||
connect( job, SIGNAL( result( KJob* ) ), SLOT( contactSearchResult( KJob* ) ) ); | |||
</code> | |||
The code for the result slot is the same as above. | |||
=== Who Am I === | === Who Am I === |
Revision as of 13:03, 20 June 2010
Development/AkonadiPorting/AddressBook
Languages: عربي | Asturianu | Català | Česky | Kaszëbsczi | Dansk | Deutsch | English | Esperanto | Español | Eesti | فارسی | Suomi | Français | Galego | Italiano | 日本語 | 한국어 | Norwegian | Polski | Português Brasileiro | Română | Русский | Svenska | Slovenčina | Slovenščina | српски | Türkçe | Tiếng Việt | Українська | 简体中文 | 繁體中文
Introduction
The address book API KABC has been available for application developers for several major version releases of KDE.
Its main entry point for applications is the KABC::StdAddressBook singleton.
The most prevelant usage is loading all address book contents synchronously and the working with the loaded data set, e.g. by calling "find" methods or even iterating over the whole contact pool.
Aside from often needlessly loading the all contacts into memory in every application accessing the address book, the synchronous I/O either meant blocking the application or introducing unexpected re-entrancy when the address book plugins were using nested event loops to process jobs without returning from the called function.
Common Usage Patterns
Most usage patterns involve one of the KABC::AddressBook's "find" methods. Their functionality is now mostly available through Akonadi::ContactSearchJob
Find By Uid
A common use case is to look for a contact object by its identifier, e.g. acquired by user selection somewhen in the past.
The code to do that usually looks like this:
KABC::AddressBook *addressBook = KABC::StdAddressBook::self();
KABC::Addressee contact = addressBook->findByUid( uidString );
The equivalent using Akonadi API looks like this
Akonadi::ContactSearchJob *job = new Akonadi::ContactSearchJob( this );
job->setQuery( Akonadi::ContactSearchJob::ContactUid, uidString );
connect( job, SIGNAL( result( KJob* ) ), SLOT( contactSearchResult( KJob* ) ) );
and
void contactSearchResult( KJob *job )
{
if ( job->error() != 0 ) {
// error handling, see job->errorString()
return;
}
Akonadi::ContactSearchJob *searchJob = qobject_cast<Akonadi::ContactSearchJob*>( job );
const KABC::Addressee::List contacts = searchJob->contacts();
}
Find By Email
Another common use case is searching for matching contacts by email address. The code for that with KABC classes is almost identical:
KABC::AddressBook *addressBook = KABC::StdAddressBook::self();
KABC::Addressee contact = addressBook->findByEmail( emailString );
Unsurprisingly, the Akonadi code looks also quite similar to the previous use case:
Akonadi::ContactSearchJob *job = new Akonadi::ContactSearchJob( this );
job->setQuery( Akonadi::ContactSearchJob::Email, emailString );
connect( job, SIGNAL( result( KJob* ) ), SLOT( contactSearchResult( KJob* ) ) );
The code for the result slot is the same as above.