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

akonadi/contact

  • 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  void searchResult( KJob *job )
73  {
74  if ( job->error() ) {
75  mParent->setError( job->error() );
76  mParent->setErrorText( job->errorText() );
77  mParent->emitResult();
78  return;
79  }
80 
81  ContactGroupSearchJob *searchJob = qobject_cast<ContactGroupSearchJob*>( job );
82 
83  if ( searchJob->contactGroups().isEmpty() ) {
84  mParent->emitResult();
85  return;
86  }
87 
88  mGroup = searchJob->contactGroups().first();
89  resolveGroup();
90  }
91 
92  void fetchResult( KJob *job )
93  {
94  const ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob*>( job );
95 
96  const Item::List items = fetchJob->items();
97  if ( !items.isEmpty() ) {
98  const QString email = fetchJob->property( "preferredEmail" ).toString();
99 
100  const Item item = items.first();
101  if ( item.hasPayload<KABC::Addressee>() ) {
102  KABC::Addressee contact = item.payload<KABC::Addressee>();
103  if ( !email.isEmpty() )
104  contact.insertEmail( email, true );
105 
106  mContacts.append( contact );
107  } else
108  kWarning() << "Contact for Akonadi item" << item.id() << "does not exist anymore!";
109  }
110 
111  mFetchCount--;
112 
113  if ( mFetchCount == 0 )
114  mParent->emitResult();
115  }
116 
117  ContactGroupExpandJob *mParent;
118  KABC::ContactGroup mGroup;
119  QString mName;
120  KABC::Addressee::List mContacts;
121 
122  int mFetchCount;
123 };
124 
125 ContactGroupExpandJob::ContactGroupExpandJob( const KABC::ContactGroup &group, QObject * parent )
126  : KJob( parent ), d( new Private( group, this ) )
127 {
128 }
129 
130 ContactGroupExpandJob::ContactGroupExpandJob( const QString &name, QObject * parent )
131  : KJob( parent ), d( new Private( name, this ) )
132 {
133 }
134 
135 ContactGroupExpandJob::~ContactGroupExpandJob()
136 {
137  delete d;
138 }
139 
140 void ContactGroupExpandJob::start()
141 {
142  if ( !d->mName.isEmpty() && !d->mName.contains( QLatin1Char( '@' ) ) ) {
143  // we have to search the contact group first
144  ContactGroupSearchJob *searchJob = new ContactGroupSearchJob( this );
145  searchJob->setQuery( ContactGroupSearchJob::Name, d->mName );
146  searchJob->setLimit( 1 );
147  connect( searchJob, SIGNAL(result(KJob*)), this, SLOT(searchResult(KJob*)) );
148  } else {
149  d->resolveGroup();
150  }
151 }
152 
153 KABC::Addressee::List ContactGroupExpandJob::contacts() const
154 {
155  return d->mContacts;
156 }
157 
158 #include "contactgroupexpandjob.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Dec 10 2012 13:48:34 by doxygen 1.8.1.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi/contact

Skip menu "akonadi/contact"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.9.4 API Reference

Skip menu "kdepimlibs-4.9.4 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • 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
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