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

akonadi

  • akonadi
  • contact
contactgroupexpandjob.cpp
1 /*
2  This file is part of Akonadi Contact.
3 
4  Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
5 
6  This library is free software; you can redistribute it and/or modify it
7  under the terms of the GNU Library General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or (at your
9  option) any later version.
10 
11  This library is distributed in the hope that it will be useful, but WITHOUT
12  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14  License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301, USA.
20 */
21 
22 #include "contactgroupexpandjob.h"
23 
24 #include <akonadi/contact/contactgroupsearchjob.h>
25 #include <akonadi/itemfetchjob.h>
26 #include <akonadi/itemfetchscope.h>
27 #include <akonadi/itemsearchjob.h>
28 
29 using namespace Akonadi;
30 
31 class ContactGroupExpandJob::Private
32 {
33  public:
34  Private( const KABC::ContactGroup &group, ContactGroupExpandJob *parent )
35  : mParent( parent ), mGroup( group ), mFetchCount( 0 )
36  {
37  }
38 
39  Private( const QString &name, ContactGroupExpandJob *parent )
40  : mParent( parent ), mName( name ), mFetchCount( 0 )
41  {
42  }
43 
44  void resolveGroup()
45  {
46  for ( unsigned int i = 0; i < mGroup.dataCount(); ++i ) {
47  const KABC::ContactGroup::Data data = mGroup.data( i );
48 
49  KABC::Addressee contact;
50  contact.setNameFromString( data.name() );
51  contact.insertEmail( data.email(), true );
52 
53  mContacts.append( contact );
54  }
55 
56  for ( unsigned int i = 0; i < mGroup.contactReferenceCount(); ++i ) {
57  const KABC::ContactGroup::ContactReference reference = mGroup.contactReference( i );
58 
59  ItemFetchJob *job = new ItemFetchJob( Item( reference.uid().toLongLong() ), mParent );
60  job->fetchScope().fetchFullPayload();
61  job->setProperty( "preferredEmail", reference.preferredEmail() );
62 
63  mParent->connect( job, SIGNAL(result(KJob*)), mParent, SLOT(fetchResult(KJob*)) );
64 
65  mFetchCount++;
66  }
67 
68  if ( mFetchCount == 0 ) { // nothing to fetch, so we can return immediately
69  mParent->emitResult();
70  }
71  }
72 
73  void searchResult( KJob *job )
74  {
75  if ( job->error() ) {
76  mParent->setError( job->error() );
77  mParent->setErrorText( job->errorText() );
78  mParent->emitResult();
79  return;
80  }
81 
82  ContactGroupSearchJob *searchJob = qobject_cast<ContactGroupSearchJob*>( job );
83 
84  if ( searchJob->contactGroups().isEmpty() ) {
85  mParent->emitResult();
86  return;
87  }
88 
89  mGroup = searchJob->contactGroups().first();
90  resolveGroup();
91  }
92 
93  void fetchResult( KJob *job )
94  {
95  const ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob*>( job );
96 
97  const Item::List items = fetchJob->items();
98  if ( !items.isEmpty() ) {
99  const QString email = fetchJob->property( "preferredEmail" ).toString();
100 
101  const Item item = items.first();
102  if ( item.hasPayload<KABC::Addressee>() ) {
103  KABC::Addressee contact = item.payload<KABC::Addressee>();
104  if ( !email.isEmpty() ) {
105  contact.insertEmail( email, true );
106  }
107 
108  mContacts.append( contact );
109  } else
110  kWarning() << "Contact for Akonadi item" << item.id() << "does not exist anymore!";
111  }
112 
113  mFetchCount--;
114 
115  if ( mFetchCount == 0 ) {
116  mParent->emitResult();
117  }
118  }
119 
120  ContactGroupExpandJob *mParent;
121  KABC::ContactGroup mGroup;
122  QString mName;
123  KABC::Addressee::List mContacts;
124 
125  int mFetchCount;
126 };
127 
128 ContactGroupExpandJob::ContactGroupExpandJob( const KABC::ContactGroup &group, QObject * parent )
129  : KJob( parent ), d( new Private( group, this ) )
130 {
131 }
132 
133 ContactGroupExpandJob::ContactGroupExpandJob( const QString &name, QObject * parent )
134  : KJob( parent ), d( new Private( name, this ) )
135 {
136 }
137 
138 ContactGroupExpandJob::~ContactGroupExpandJob()
139 {
140  delete d;
141 }
142 
143 void ContactGroupExpandJob::start()
144 {
145  if ( !d->mName.isEmpty() && !d->mName.contains( QLatin1Char( '@' ) ) ) {
146  // we have to search the contact group first
147  ContactGroupSearchJob *searchJob = new ContactGroupSearchJob( this );
148  searchJob->setQuery( ContactGroupSearchJob::Name, d->mName );
149  searchJob->setLimit( 1 );
150  connect( searchJob, SIGNAL(result(KJob*)), this, SLOT(searchResult(KJob*)) );
151  } else {
152  d->resolveGroup();
153  }
154 }
155 
156 KABC::Addressee::List ContactGroupExpandJob::contacts() const
157 {
158  return d->mContacts;
159 }
160 
161 #include "moc_contactgroupexpandjob.cpp"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:27:34 by doxygen 1.8.3.1 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-4.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
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