[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-pim
Subject:    Re: [Kde-pim] KDE - Counting Number of Address Books
From:       Tobias Koenig <tokoe () kde ! org>
Date:       2010-06-29 8:37:24
Message-ID: 20100629083724.GA4506 () ghostdog ! localnet
[Download RAW message or body]

[Attachment #2 (multipart/signed)]


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

["signature.asc" (application/pgp-signature)]

_______________________________________________
KDE PIM mailing list kde-pim@kde.org
https://mail.kde.org/mailman/listinfo/kde-pim
KDE PIM home page at http://pim.kde.org/

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic