• Skip to content
  • Skip to link menu
KDE 4.6 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • KDE Home
  • Contact Us
 

akonadi

contactgroupexpandjob.cpp
00001 /*
00002     This file is part of Akonadi Contact.
00003 
00004     Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or modify it
00007     under the terms of the GNU Library General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or (at your
00009     option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful, but WITHOUT
00012     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00014     License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to the
00018     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00019     02110-1301, USA.
00020 */
00021 
00022 #include "contactgroupexpandjob.h"
00023 
00024 #include <akonadi/contact/contactgroupsearchjob.h>
00025 #include <akonadi/itemfetchjob.h>
00026 #include <akonadi/itemfetchscope.h>
00027 #include <akonadi/itemsearchjob.h>
00028 
00029 using namespace Akonadi;
00030 
00031 class ContactGroupExpandJob::Private
00032 {
00033   public:
00034     Private( const KABC::ContactGroup &group, ContactGroupExpandJob *parent )
00035       : mParent( parent ), mGroup( group ), mFetchCount( 0 )
00036     {
00037     }
00038 
00039     Private( const QString &name, ContactGroupExpandJob *parent )
00040       : mParent( parent ), mName( name ), mFetchCount( 0 )
00041     {
00042     }
00043 
00044     void resolveGroup()
00045     {
00046       for ( unsigned int i = 0; i < mGroup.dataCount(); ++i ) {
00047         const KABC::ContactGroup::Data data = mGroup.data( i );
00048 
00049         KABC::Addressee contact;
00050         contact.setNameFromString( data.name() );
00051         contact.insertEmail( data.email(), true );
00052 
00053         mContacts.append( contact );
00054       }
00055 
00056       for ( unsigned int i = 0; i < mGroup.contactReferenceCount(); ++i ) {
00057         const KABC::ContactGroup::ContactReference reference = mGroup.contactReference( i );
00058 
00059         ItemFetchJob *job = new ItemFetchJob( Item( reference.uid().toLongLong() ), mParent );
00060         job->fetchScope().fetchFullPayload();
00061         job->setProperty( "preferredEmail", reference.preferredEmail() );
00062 
00063         mParent->connect( job, SIGNAL( result( KJob* ) ), mParent, SLOT( fetchResult( KJob* ) ) );
00064 
00065         mFetchCount++;
00066       }
00067 
00068       if ( mFetchCount == 0 ) // nothing to fetch, so we can return immediately
00069         mParent->emitResult();
00070     }
00071 
00072     void searchResult( KJob *job )
00073     {
00074       if ( job->error() ) {
00075         mParent->setError( job->error() );
00076         mParent->setErrorText( job->errorText() );
00077         mParent->emitResult();
00078         return;
00079       }
00080 
00081       ContactGroupSearchJob *searchJob = qobject_cast<ContactGroupSearchJob*>( job );
00082 
00083       if ( searchJob->contactGroups().isEmpty() ) {
00084         mParent->emitResult();
00085         return;
00086       }
00087 
00088       mGroup = searchJob->contactGroups().first();
00089       resolveGroup();
00090     }
00091 
00092     void fetchResult( KJob *job )
00093     {
00094       const ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob*>( job );
00095 
00096       const Item::List items = fetchJob->items();
00097       if ( !items.isEmpty() ) {
00098         const QString email = fetchJob->property( "preferredEmail" ).toString();
00099 
00100         const Item item = items.first();
00101         if ( item.hasPayload<KABC::Addressee>() ) {
00102           KABC::Addressee contact = item.payload<KABC::Addressee>();
00103           if ( !email.isEmpty() )
00104             contact.insertEmail( email, true );
00105 
00106           mContacts.append( contact );
00107         } else
00108           kWarning() << "Contact for Akonadi item" << item.id() << "does not exist anymore!";
00109       }
00110 
00111       mFetchCount--;
00112 
00113       if ( mFetchCount == 0 )
00114         mParent->emitResult();
00115     }
00116 
00117     ContactGroupExpandJob *mParent;
00118     KABC::ContactGroup mGroup;
00119     QString mName;
00120     KABC::Addressee::List mContacts;
00121 
00122     int mFetchCount;
00123 };
00124 
00125 ContactGroupExpandJob::ContactGroupExpandJob( const KABC::ContactGroup &group, QObject * parent )
00126   : KJob( parent ), d( new Private( group, this ) )
00127 {
00128 }
00129 
00130 ContactGroupExpandJob::ContactGroupExpandJob( const QString &name, QObject * parent )
00131   : KJob( parent ), d( new Private( name, this ) )
00132 {
00133 }
00134 
00135 ContactGroupExpandJob::~ContactGroupExpandJob()
00136 {
00137   delete d;
00138 }
00139 
00140 void ContactGroupExpandJob::start()
00141 {
00142   if ( !d->mName.isEmpty() ) {
00143     // we have to search the contact group first
00144     ContactGroupSearchJob *searchJob = new ContactGroupSearchJob( this );
00145     searchJob->setQuery( ContactGroupSearchJob::Name, d->mName );
00146     searchJob->setLimit( 1 );
00147     connect( searchJob, SIGNAL( result( KJob* ) ), this, SLOT( searchResult( KJob* ) ) );
00148   } else {
00149     d->resolveGroup();
00150   }
00151 }
00152 
00153 KABC::Addressee::List ContactGroupExpandJob::contacts() const
00154 {
00155   return d->mContacts;
00156 }
00157 
00158 #include "contactgroupexpandjob.moc"

akonadi

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

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.7.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal