[Kde-pim] KDE - Counting Number of Address Books

Tobias Koenig tokoe at kde.org
Tue Jun 29 09:37:24 BST 2010


On Tue, Jun 29, 2010 at 09:56:50AM +0200, GOO Creations wrote:

> Dear KDEPIM Developers
Hej Christoph,

>  Akonadi::ChangeRecorder *changeRecorder = new
>  Akonadi::ChangeRecorder();
>  changeRecorder->setCollectionMonitored( Akonadi::Collection::root() );
>  changeRecorder->setMimeTypeMonitored( KABC::Addressee::mimeType() );
>  changeRecorder->fetchCollection( true );
>
>  Akonadi::EntityTreeModel *model = new Akonadi::EntityTreeModel(
>  changeRecorder);
>  model->setItemPopulationStrategy(
>  Akonadi::EntityTreeModel::NoItemPopulation );
>
>  QMessageBox msgBox;
>  msgBox.setText(QString::number(model->rowCount()));
>  msgBox.exec();
EntityTreeModel uses asynchronous loading of the content, so at the point where
you ask for the rowCount(), the data might have not been loaded yet.
If you only want to know the number of collections using the EntityTreeModel is
overkill. You can use the following code instead:

  using namespace Akonadi;
  CollectionFetchJob *job = new CollectionFetchJob( Collection::root(), CollectionFetchJob::Recursive );
  connect( job, SIGNAL( result( KJob* ) ), SLOT( collectionsFetched( KJob* ) ) );

  ...

  void MyClass::collectionsFetched( KJob *job )
  {
    if ( job->error() ) {
      // error handling
      return;
    }

    const CollectionFetchJob *fetchJob = qobject_cast<CollectionFetchJob*>( job );

    int addressBookCounter = 0;
    foreach ( const Collection &collection, fetchJob->collections() ) {
      if ( collection.contentMimeTypes().contains( KABC::Addressee::mimeType() ) ||
           collection.contentMimeTypes().contains( KABC::ContactGroup::mimeType() ) )
        addressBookCounter++;
    }
  }

alternatively you can use the synchronous way (however that is not recommended):

  CollectionFetchJob *job = new CollectionFetchJob( Collection::root(), CollectionFetchJob::Recursive );
  if ( job->exec() ) {
    int addressBookCounter = 0;
    foreach ( const Collection &collection, job->collections() ) {
      if ( collection.contentMimeTypes().contains( KABC::Addressee::mimeType() ) ||
           collection.contentMimeTypes().contains( KABC::ContactGroup::mimeType() ) )
        addressBookCounter++;
    }
  }

Ciao,
Tobias
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://mail.kde.org/pipermail/kde-pim/attachments/20100629/c13babbc/attachment.sig>
-------------- next part --------------
_______________________________________________
KDE PIM mailing list kde-pim at kde.org
https://mail.kde.org/mailman/listinfo/kde-pim
KDE PIM home page at http://pim.kde.org/


More information about the kde-pim mailing list