Development/AkonadiPorting/AddressBook: Difference between revisions
Neverendingo (talk | contribs) m (Text replace - "<code cppqt>" to "<syntaxhighlight lang="cpp-qt">") |
Neverendingo (talk | contribs) m (Text replace - "</code>" to "</syntaxhighlight>") |
||
Line 25: | Line 25: | ||
KABC::Addressee contact = addressBook->findByUid( uidString ); | KABC::Addressee contact = addressBook->findByUid( uidString ); | ||
</ | </syntaxhighlight> | ||
The equivalent using Akonadi API looks like this | The equivalent using Akonadi API looks like this | ||
Line 33: | Line 33: | ||
connect( job, SIGNAL( result( KJob* ) ), SLOT( contactSearchResult( KJob* ) ) ); | connect( job, SIGNAL( result( KJob* ) ), SLOT( contactSearchResult( KJob* ) ) ); | ||
</ | </syntaxhighlight> | ||
and | and | ||
<syntaxhighlight lang="cpp-qt"> | <syntaxhighlight lang="cpp-qt"> | ||
Line 47: | Line 47: | ||
const KABC::Addressee::List contacts = searchJob->contacts(); | const KABC::Addressee::List contacts = searchJob->contacts(); | ||
} | } | ||
</ | </syntaxhighlight> | ||
{{tip|Sometimes it is necessary to transport some context from the method which creates the job to the result slot. A convenient way to do this is using the job's [http://doc.qt.nokia.com/latest/qobject.html#setProperty setProperty()] method.}} | {{tip|Sometimes it is necessary to transport some context from the method which creates the job to the result slot. A convenient way to do this is using the job's [http://doc.qt.nokia.com/latest/qobject.html#setProperty setProperty()] method.}} | ||
Line 58: | Line 58: | ||
KABC::Addressee contact = addressBook->findByEmail( emailString ); | KABC::Addressee contact = addressBook->findByEmail( emailString ); | ||
</ | </syntaxhighlight> | ||
Unsurprisingly, the Akonadi code looks also quite similar to the previous use case: | Unsurprisingly, the Akonadi code looks also quite similar to the previous use case: | ||
Line 66: | Line 66: | ||
connect( job, SIGNAL( result( KJob* ) ), SLOT( contactSearchResult( KJob* ) ) ); | connect( job, SIGNAL( result( KJob* ) ), SLOT( contactSearchResult( KJob* ) ) ); | ||
</ | </syntaxhighlight> | ||
The code for the result slot is the same as above. | The code for the result slot is the same as above. | ||
Line 77: | Line 77: | ||
KABC::Addressee contact = addressBook->whoAmI(); | KABC::Addressee contact = addressBook->whoAmI(); | ||
</ | </syntaxhighlight> | ||
{{Note|Even this had its shortcomings, because this depends on the users adding themselves to the address book and marking the respective entry as "this is me".}} | {{Note|Even this had its shortcomings, because this depends on the users adding themselves to the address book and marking the respective entry as "this is me".}} | ||
Line 99: | Line 99: | ||
} | } | ||
} | } | ||
</ | </syntaxhighlight> | ||
With Akonadi, the application will need the [http://api.kde.org/4.x-api/kdepimlibs/akonadi/classAkonadi_1_1Item.html Akonadi::Item] or at least the item's identifier. | With Akonadi, the application will need the [http://api.kde.org/4.x-api/kdepimlibs/akonadi/classAkonadi_1_1Item.html Akonadi::Item] or at least the item's identifier. | ||
Line 112: | Line 112: | ||
Akonadi::ItemModifyJob *job = new Akonadi::ItemModifyJob( item ); | Akonadi::ItemModifyJob *job = new Akonadi::ItemModifyJob( item ); | ||
connect( job, SIGNAL( result( KJob* ) ), SLOT( contactModifyResult( KJob* ) ) ); | connect( job, SIGNAL( result( KJob* ) ), SLOT( contactModifyResult( KJob* ) ) ); | ||
</ | </syntaxhighlight> | ||
The code for the result slot is similar to the one for search. | The code for the result slot is similar to the one for search. | ||
Line 121: | Line 121: | ||
<code> | <code> | ||
find_package( KdepimLibs 4.5 REQUIRED ) | find_package( KdepimLibs 4.5 REQUIRED ) | ||
</ | </syntaxhighlight> | ||
However, "target_link_libraries" need to be extended to contain | However, "target_link_libraries" need to be extended to contain | ||
<code> | <code> | ||
${KDEPIMLIBS_AKONADI_LIBS} | ${KDEPIMLIBS_AKONADI_LIBS} | ||
</ | </syntaxhighlight> | ||
for code classes like Akonadi::Item and | for code classes like Akonadi::Item and | ||
<code> | <code> | ||
${KDEPIMLIBS_AKONADI_CONTACT_LIBS} | ${KDEPIMLIBS_AKONADI_CONTACT_LIBS} | ||
</ | </syntaxhighlight> | ||
for contact related classes like Akonadi::ContactSearchJob. | for contact related classes like Akonadi::ContactSearchJob. | ||
Line 136: | Line 136: | ||
<syntaxhighlight lang="cpp-qt"> | <syntaxhighlight lang="cpp-qt"> | ||
#include <Akonadi/Item> | #include <Akonadi/Item> | ||
</ | </syntaxhighlight> | ||
for contact related specializations they are like | for contact related specializations they are like | ||
<syntaxhighlight lang="cpp-qt"> | <syntaxhighlight lang="cpp-qt"> | ||
#include <Akonadi/Contact/ContactSearchJob> | #include <Akonadi/Contact/ContactSearchJob> | ||
</ | </syntaxhighlight> | ||
In both cases also available in lowercase variants with ".h" suffix. | In both cases also available in lowercase variants with ".h" suffix. |
Revision as of 18:23, 29 June 2011
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.
Who Am I
Another use case for KABC::AddressBook is not directly available in Akonadi:
KABC::AddressBook *addressBook = KABC::StdAddressBook::self();
KABC::Addressee contact = addressBook->whoAmI();
Concrete use cases this has been deployed for are getting the user's full name and/or their email address, which might be better served by using KDE's identity management instead, see KPIMIdentities::IdentityManager.
It contains the identity users can configure in the Systemsettings module for personal information, so if there is data yet, the application can just embed the respective KCM in a dialog and let the user configure this centrally for all of KDE.
Modifying A Contact
When using KABC::StdAddressBook, modifying a contact (for example one retrieved by findByUid()) worked like this:
addressBook->insertAddressee( contact );
KABC::Ticket *ticket = addressBook->requestSaveTicket();
if ( !ticket ) {
// error
} else if ( !addressBook->save( ticket ) ) {
// error
addressBook->releaseSaveTicket( ticket );
}
}
With Akonadi, the application will need the Akonadi::Item or at least the item's identifier. Assuming the contact got retrieved using ContactSearchJob, consider keeping the item or Item::id() around, see ContactSearchJob::items().
Lets say there is just the item's identifier:
Akonadi::Item item( itemId );
item.setPayload<KABC::Addressee>( contact );
item.setMimeType( KABC::Addressee::mimeType() );
Akonadi::ItemModifyJob *job = new Akonadi::ItemModifyJob( item );
connect( job, SIGNAL( result( KJob* ) ), SLOT( contactModifyResult( KJob* ) ) );
The code for the result slot is similar to the one for search.
Buildsystem Adjustments
Since the application is already linking against KDE PIM Libs classes, its CMakeList.txt already has the necessary
find_package( KdepimLibs 4.5 REQUIRED )
</syntaxhighlight>
However, "target_link_libraries" need to be extended to contain
${KDEPIMLIBS_AKONADI_LIBS}
</syntaxhighlight>
for code classes like Akonadi::Item and
${KDEPIMLIBS_AKONADI_CONTACT_LIBS}
</syntaxhighlight>
for contact related classes like Akonadi::ContactSearchJob.
Includes for Akonadi core classes are like
#include <Akonadi/Item>
for contact related specializations they are like
#include <Akonadi/Contact/ContactSearchJob>
In both cases also available in lowercase variants with ".h" suffix.
Further Reading
Akonadi porting efforts have resulted in a couple of useful classes for various aspects of dealing with PIM data.
For contacts such classes would be:
- Akonadi::ContactEditor (also available as a dialog)
- Akonadi::ContactViewer (again also available as a dialog)
- Akonadi::ContactsTreeModel