• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepimlibs API Reference
  • KDE Home
  • Contact Us
 

akonadi

Integration in your Application

Akonadi::Control provides ways to ensure that the Akonadi server is running, to monitor its availability and provide help on server-side errors.

A more low-level interface to the Akonadi server is provided by Akonadi::ServerManager.

A set of standard actions is provided by Akonadi::StandardActionManager. These provide consistent look and feel across applications.

This library provides classes for KDE applications to communicate with the Akonadi server. The most high-level interface to Akonadi is the Models and Views provided in this library. Ready to use models are provided for use with views to interact with a tree of collections, a list of items in a collection, or a combined tree of Collections and items.

In the Akonadi concept, Items are individual objects of PIM data, e.g. emails, contacts, events, notes etc. The data in an item is stored in a typed payload. For example, if an Akonadi Item holds a contact, the contact is available as a KABC::Addressee:

if (item.hasPayload<KABC::Addressee>())
{
KABC::Addressee addr = item.payload<KABC::Addressee>();
// use addr in some way...
}

Additionally, an Item must have a mimetype which corresponds to the type of payload it holds.

Collections are simply containers of Items. A Collection has a name and a list of mimetypes that it may contain. A collection may for example contain events if it can contain the mimetype 'text/calendar'. A Collection itself (as opposed to its contents) has a mimetype, which is the same for all Collections. A Collection which can itself contain Collections must be able to contain the Collection mimetype.

Collection col;
// This collection can contain events and nested collections.
col.setContentMimetypes( QStringList() << Collection::mimeType() << "text/calendar" );

This system makes it simple to create PIM applications. For example, to create an application for viewing and editing events, you simply need to tell Akonadi to retrieve all items matching the mimetype 'text/calendar'.

In order to avoid typos, improve readability, and to encapsulate the correct mimetypes for particular pim items, many of the standard classes have an accessor for the kind of mimetype the can handle. For example, you can use KMime::Message::mimeType() for emails, KABC::Addressee::mimeType() for contacts etc. It makes sense to define a similar static function in your own types.

col.setContentMimetypes( QStringList() << Collection::mimeType() << KABC::Addressee::mimeType() << KMime::Message::mimeType() );

Models and Views

Akonadi models and views are a high level way to interact with the Akonadi server. Most applications will use these classes. See the EntityTreeModel documentation for more information.

Models provide an interface for viewing, deleting and moving Items and Collections. New Items can also be created by dropping data of the appropriate type on a model. Additionally, the models are updated automatically if another application changes the data or inserts or deletes items etc.

Akonadi provides several models for particular uses, e.g. the MailModel is used for emails and the ContactsModel is used for showing contacts. Additional specific models can be implemented using EntityTreeModel as a base class.

A typical use of these would be to create a model and use proxy models to make the view show different parts of the model. For example, show a collection tree in on one side and show items in a selected collection in another view.

mailModel = new MailModel( session, monitor, this);
collectionTree = new EntityMimeTypeFilterModel(this);
collectionTree->setSourceModel(mailModel);
// Filter out everything that is not a collection.
collectionTree->addMimeTypeInclusionFilter( Collection::mimeType() );
collectionTree->setHeaderSet(EntityTreeModel::CollectionTreeHeaders);
collectionView = new EntityTreeView(this);
collectionView->setModel(collectionTree);
itemList = new EntityMimeTypeFilterModel(this);
itemList->setSourceModel(mailModel);
// Filter out collections
itemList->addMimeTypeExclusionFilter( Collection::mimeType() );
itemList->setHeaderSet(EntityTreeModel::ItemListHeaders);
itemView = new EntityTreeView(this);
itemView->setModel(itemList);
mailmodelapp.png
An email application using MailModel

The content of the model is determined by the configuration of the Monitor passed into it. The examples below show a use of the EntityTreeModel and some proxy models for a simple heirarchical note collection. As the model is generic, the configuration and proxy models will also work with any other mimetype.

// Configure what should be shown in the model:
Monitor *monitor = new Monitor( this );
monitor->fetchCollection( true );
monitor->setItemFetchScope( scope );
monitor->setCollectionMonitored( Collection::root() );
monitor->setMimeTypeMonitored( MyEntity::mimeType() );
Session *session = new Session( QByteArray( "MyEmailApp-" ) + QByteArray::number( qrand() ), this );
monitor->setSession(session);
EntityTreeModel *entityTree = new Akonadi::EntityTreeModel(monitor, this);
entitytreemodel.png
A plain EntityTreeModel in a view

The EntityTreeModel can be further configured for certain behaviours such as fetching of collections and items.

To create a model of only a collection tree and no items, set that in the model. This is just like CollectionModel:

entityTree->setItemPopulationStrategy(EntityTreeModel::NoItemPopulation);
entitytreemodel-collections.png
A plain EntityTreeModel which does not fetch items.

Or, create a model of only items and not child collections. This is just like ItemModel:

entityTree->setRootCollection(myCollection);
entityTree->setCollectionFetchStrategy(EntityTreeModel::FetchNoCollections);

Or, to create a model which includes items and first level collections:

entityTree->setCollectionFetchStrategy(EntityTreeModel::FetchFirstLevelCollections);

The items in the model can also be inserted lazily for performance reasons. The Collection tree is always built immediately.

Additionally, a KDescendantsProxyModel may be used to alter how the items in the tree are presented.

// ... Create an entityTreeModel
KDescendantsProxyModel *descProxy = new KDescendantsProxyModel(this);
descProxy->setSourceModel(entityTree);
view->setModel(descProxy);
descendantentitiesproxymodel.png
A KDescendantsProxyModel wrapping an EntityTreeModel

KDescendantsProxyModel can also display ancestors of each Entity in the list.

// ... Create an entityTreeModel
KDescendantsProxyModel *descProxy = new KDescendantsProxyModel(this);
descProxy->setSourceModel(entityTree);
// #### This is new
descProxy->setDisplayAncestorData(true, QString(" / "));
view->setModel(descProxy);
descendantentitiesproxymodel-withansecnames.png
A DescendantEntitiesProxyModel with ancestor names.

This proxy can be combined with a filter to for example remove collections.

// ... Create an entityTreeModel
DescendantEntitiesProxyModel *descProxy = new DescendantEntitiesProxyModel(this);
descProxy->setSourceModel(entityTree);
// #### This is new.
EntityMimeTypeFilterModel *filterModel = new EntityMimeTypeFilterModel(this);
filterModel->setSourceModel(descProxy);
filterModel->setExclusionFilter(QStringList() << Collection::mimeType());
view->setModel(filterModel);
descendantentitiesproxymodel-colfilter.png
An EntityMimeTypeFilterModel wrapping a DescendantEntitiesProxyModel wrapping an EntityTreeModel

It is also possible to show the root item as part of the selectable model:

entityTree->setIncludeRootCollection(true);
entitytreemodel-showroot.png
An EntityTreeModel showing Collection::root

By default the displayed name of the root collection is '[*]', because it doesn't require i18n, and is generic. It can be changed too.

entityTree->setIncludeRootCollection(true);
entityTree->setRootCollectionDisplayName(i18nc("Name of top level for all collections in the application", "[All]"))
entitytreemodel-showrootwithname.png
An EntityTreeModel showing Collection::root with an application specific name.

These can of course be combined to create an application which uses one EntityTreeModel along with several proxies and views.

// ... create an EntityTreeModel.
EntityMimeTypeFilterModel *collectionTree = new EntityMimeTypeFilterModel(this);
collectionTree->setSourceModel(entityTree);
// Filter to include collections only:
collectionTree->setInclusionFilter(QStringList() << Collection::mimeType());
EntityTreeView *treeView = new EntityTreeView(this);
treeView->setModel(collectionTree);
EntityMimeTypeFilterModel *itemList = new EntityMimeTypeFilterModel(this);
itemList->setSourceModel(entityTree);
// Filter *out* collections
itemList->setExclusionFilter(QStringList() << Collection::mimeType());
EntityTreeView *listView = new EntityTreeView(this);
listView->setModel(itemList);
treeandlistapp.png
A single EntityTreeModel with several views and proxies.

Or to also show items of child collections in the list:

// ... Create an entityTreeModel
collectionTree = new EntityMimeTypeFilterModel(this);
collectionTree->setSourceModel(entityTree);
// Include only collections in this proxy model.
collectionTree->addMimeTypeInclusionFilter( Collection::mimeType() );
treeview->setModel(collectionTree);
descendedList = new DescendantEntitiesProxyModel(this);
descendedList->setSourceModel(entityTree);
itemList = new EntityMimeTypeFilterModel(this);
itemList->setSourceModel(descendedList);
// Exclude collections from the list view.
itemList->addMimeTypeExclusionFilter( Collection::mimeType() );
listView = new EntityTreeView(this);
listView->setModel(itemList);
treeandlistappwithdesclist.png
Showing descendants of all Collections in the list

Note that it is important in this case to use the DescendantEntitesProxyModel before the EntityMimeTypeFilterModel. Otherwise, by filtering out the collections first, you would also be filtering out their child items.

A SelectionProxyModel can be used to simplify managing selection in one view through multiple proxy models to a representation in another view. The selectionModel of the initial view is used to create a proxied model which includes only the selected indexes and their children.

// ... Create an entityTreeModel
collectionTree = new EntityMimeTypeFilterModel(this);
collectionTree->setSourceModel(entityTree);
// Include only collections in this proxy model.
collectionTree->addMimeTypeInclusionFilter( Collection::mimeType() );
treeview->setModel(collectionTree);
// SelectionProxyModel can handle complex selections:
treeview->setSelectionMode(QAbstractItemView::ExtendedSelection);
SelectionProxyModel *selProxy = new SelectionProxyModel(treeview->selectionModel(), this);
selProxy->setSourceModel(entityTree);
EntityTreeView *selView = new EntityTreeView(splitter);
selView->setModel(selProxy);
selectionproxymodelsimpleselection.png
A Selection in one view creating a model for use with another view.

The SelectionProxyModel can handle complex selections.

selectionproxymodelmultipleselection.png
Non-contiguous selection creating a new simple model in a second view.

If an index and one or more of its descendants are selected, only the top-most selected index (including all of its descendants) are included in the proxy model. (Though this is configurable. See below)

selectionproxymodelmultipleselection-withdescendant.png
Selecting an item and its descendant.

SelectionProxyModel allows configuration using the methods setStartWithChildTrees, setOmitDescendants, setIncludeAllSelected. See testapp/proxymodeltestapp to try out the 5 valid configurations.

Obviously, the SelectionProxyModel may be used in a view, or further processed with other proxy models. See the example_contacts application for example which uses a further DescendantEntitiesProxyModel and EntityMimeTypeFilterModel on top of a SelectionProxyModel.

The SelectionProxyModel orders its items in the same top-to-bottom order as they appear in the source model. Note that this order may be different to the order in the selection model if there is a QSortFilterProxyModel between the selection and the source model.

selectionproxymodel-ordered.png
Ordered items in the SelectionProxyModel

Jobs and Monitors

The lower level way to interact with Akonadi is to use Jobs and Monitors (This is what models use internally). Jobs are used to make changes to akonadi, and in some cases (e.g., a fetch job) emit a signal with data resulting from the job. A Monitor reports changes made to the data stored in Akonadi (e.g., creating, updating, deleting or moving an item or collection ) via signals.

Typically, an application will configure a monitor to report changes to a particular Collection, mimetype or resource, and then connect to the signals it emits.

Most applications will use some of the low level api for actions unrelated to a model-tree view, such as creating new items and collections.

Tricky details

Change Conflicts

It is possible that while an application is editing an item, that item gets updated in akonadi. Akonadi will notify the application that that item has changed via a Monitor signal. It is the responsibility of the application to handle the conflict by for example offering the user a dialog to resolve it. Alternatively, the application could ignore the dataChanged signal for that item, and will get another chance to resolve the conflict when trying to save the result back to akonadi. In that case, the ItemModifyJob will fail and report that the revision number of the item on the server differs from its revision number as reported by the job. Again, it is up to the application to handle this case.

This is something that every application using akonadi will have to handle.

Using Entity::Id as an identifier

Items and Collections have a id() member which is a unique identifier used by akonadi. It can be useful to use the id() as an identifier when storing Collections or Items.

However, as an item and a collection can have the same id(), if you need to store both Collections and Items together by a simple identifier, conflicts can occur.

QString getRemoteIdById( Entity::Id id )
{
// Note:
// m_items is QHash<Entity::Id, Item>
// m_collections is QHash<Entity::Id, Collection>
if ( m_items.contains( id ) )
{
// Oops, we could accidentally match a collection here.
return m_items.value( id ).remoteId();
} else if ( m_collections.contains( id ) )
{
return m_collections.value( id ).remoteId();
}
return QString();
}

In this case, it makes more sense to use a normal qint64 as the internal identifier, and use the sign bit to determine if the identifier refers to a Collection or an Item. This is done in the implementation of EntityTreeModel to tell Collections and Items apart.

QString getRemoteIdByInternalIdentifier( qint64 internalIdentifier )
{
// Note:
// m_items is QHash<Entity::Id, Item>
// m_collections is QHash<Entity::Id, Collection>
// If the id is negative, it refers to an Item
// Otherwise it refers to a Collection.
if ( internalIdentifier < 0 )
{
// Reverse the sign of the id before using it.
return m_items.value( internalIdentifier * -1 ).remoteId();
} else
{
return m_collections.value( internalIdentifier ).remoteId();
}
}

Unordered Lists

Collection and Item both provide a ::List to represent groups of objects. However the objects in the list are usually not ordered in any particular way, even though the API provides methods to work with an ordered list. It makes more sense to think of it as a Set instead of a list in most cases.

For example, when using an ItemFetchJob to fetch the items in a collection, the items could be in any order when returned from the job. The order that a Monitor emits notices of changes is also indeterminate. By using a Transaction however, it is sometimes possible to retrieve objects in order. Additionally, using s constructor overload in the CollectionFetchJob it is possible to retrieve collections in a particular order.

Collection::List getCollections(QList<Collection::Id> idsToGet)
{
Collection::List getList;
foreach ( Collection::Id id, idsToGet ) {
getList << Collection(id);
}
CollectionFetchJob *job = CollectionFetchJob(getList);
if (job->exec())
{
// job->collections() is in the same order as the ids in idsToGet.
}
}

Resources

The KDEPIM module includes resources for handling many types of PIM data, such as imap email, vcard files and vcard directories, ical event files etc. These cover many of the sources for your PIM data, but in the case that you need to use data from another source (for example a website providing a contacts storage service and an api), you simply have to write a new resource.

http://techbase.kde.org/Development/Tutorials/Akonadi/Resources

Serializers

Serializers provide the functionality of converting raw data, for example from a file, to a strongly typed object of PIM data. For example, the addressee serializer reads data from a file and creates a KABC::Addressee object.

New serializers can also easily be written if the data you are dealing with is not one of the standard PIM data types.

Implementation details

Updating Akonadi Models

Note
The details here are only relevant if you are writing a new view using EntityTreeModel, or writing a new model.

Because communication with akonadi happens asynchronously, and the models only hold a cached copy of the data on the akonadi server, some typical behaviours of models are not followed by Akonadi models.

For example, when setting data on a model via a view, most models syncronously update their internal store and notify akonadi to update its view of the data by returning true.

dot_inline_dotgraph_1.png

Akonadi models only cache data from the akonadi server. To update data on an Akonadi::Entity stored in a model, the model makes a request to the akonadi server to update the model data. At that point the data cached internally in the model is not updated, so false is always returned from setData. If the request to update data on the akonadi server is successful, an Akonadi::Monitor notifies the model that the data on that item has changed. The model then updates its internal data store and notifies the view that the data has changed. The details of how the Monitor communicates with akonadi are omitted for clarity.

dot_inline_dotgraph_2.png

Similarly, in drag and drop operations, most models would update an internal data store and return true from dropMimeData if the drop is successful.

dot_inline_dotgraph_3.png

Akonadi models, for the same reason as above, always return false from dropMimeData. At the same time a suitable request is sent to the akonadi server to make the changes resulting from the drop (for example, moving or copying an entity, or adding a new entity to a collection etc). If that request is successful, the Akonadi::Monitor notifies the model that the data is changed and the model updates its internal store and notifies the view that the model data is changed.

dot_inline_dotgraph_4.png

Lazy Model Population

Note
This page is not part of the Akonadi API. It is provided as internal documentation for Akonadi maintainers. It was originally a blog post here: http://steveire.wordpress.com/2009/10/06/cache-invalidation-in-akonadi-models/

If using EntityTreeModel::LazyPopulation with your model, items will be fetched into the model when the collection they are a part of is selected. This ensures that the model is as sparsely populated as possible for performance reasons. As a consequence however, it is necessary to purge unused items from the model too. This is handled automatically when using an Akonadi::SelectionProxyModel.

The problem is knowing when to invalidate the cache. If no application is currently showing the contents of a Collection, there is no need for the Items in that Collection to be fetched, cached and kept up to date in the model. The effect we would like to achieve is to purge the Items in a Collection when those items are no longer shown anywhere in the application. Generally, that will mean that the Collection is not selected.

In Qt Model-View, the application data is stored in a model, and there may be one or more views attached to it displaying its contents. The model doesn’t have any knowledge of the views, and so it can’t know whether any particular Collection is selected, and purge its Iitems.

To solve this, we use the KSelectionProxyModel, which already has a lot of code for managing the selection a user makes in a view. A subclass, Akonadi::SelectionProxyModel implements a reference counting system which increments the refcount of a Collection when it is selected, and decrements it when deselected. If the reference count of a Collection goes down to zero, it is put in a queue to be purged. It is not purged immediately, but queued because if the user is clicking around several Collection in a short time, we don’t want to purge the Collections each click or we’d lose the benefit of the caching. Like other similar optimisation techniques, this violates the object-oriented principle of modularity, but it is worth it for the benefit it brings. The effect can be seen in the akonadiconsole tool by not filtering out the items from the tree.

In the screenshots below I removed the filtering out of Items in the tree so that the fetching/purging can be seen. In real applications, the Items in the tree on the left would not be visible.

bufferedcaching1.png
When a Collection is clicked, its Items are put into the model. The rest of the Collections have no items.
bufferedcaching2.png
The Inbox Collection is selected, so its items are fetched. Personal Contacts is no longer selected, so it is put into a queue to be purged.
bufferedcaching3.png
Select another Collection and its items are fetched too.
bufferedcaching4.png
Another Collection is selected, pushing the Personal contacts out of the queue and purging them
bufferedcaching6.png
If the Collection is selected again, its Items are refetched

For this example, I used a queue length of just two Collections, so that if a Collection was deselected two clicks ago, it will be purged. In real applications, a longer queue length will be used, but it’s harder to illustrate in screenshots. Another unrealistic part of this demo is that this feature will like be used in applications like KMail where Collections can contain tens of thousands of Items and fetching them is an expensive operation.

This feature should be totally invisible to users and even developers using Akonadi, but it should offset the main disadvantage of using a cache of Items in the EntityTreeModel.

This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:28 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal