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

akonadi

  • sources
  • kde-4.12
  • kdepimlibs
  • 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  Item item;
60  if ( !reference.gid().isEmpty() ) {
61  item.setGid( reference.gid() );
62  } else {
63  item.setId( reference.uid().toLongLong() );
64  }
65  ItemFetchJob *job = new ItemFetchJob( item, mParent );
66  job->fetchScope().fetchFullPayload();
67  job->setProperty( "preferredEmail", reference.preferredEmail() );
68 
69  mParent->connect( job, SIGNAL(result(KJob*)), mParent, SLOT(fetchResult(KJob*)) );
70 
71  mFetchCount++;
72  }
73 
74  if ( mFetchCount == 0 ) { // nothing to fetch, so we can return immediately
75  mParent->emitResult();
76  }
77  }
78 
79  void searchResult( KJob *job )
80  {
81  if ( job->error() ) {
82  mParent->setError( job->error() );
83  mParent->setErrorText( job->errorText() );
84  mParent->emitResult();
85  return;
86  }
87 
88  ContactGroupSearchJob *searchJob = qobject_cast<ContactGroupSearchJob*>( job );
89 
90  if ( searchJob->contactGroups().isEmpty() ) {
91  mParent->emitResult();
92  return;
93  }
94 
95  mGroup = searchJob->contactGroups().first();
96  resolveGroup();
97  }
98 
99  void fetchResult( KJob *job )
100  {
101  const ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob*>( job );
102 
103  const Item::List items = fetchJob->items();
104  if ( !items.isEmpty() ) {
105  const QString email = fetchJob->property( "preferredEmail" ).toString();
106 
107  const Item item = items.first();
108  if ( item.hasPayload<KABC::Addressee>() ) {
109  KABC::Addressee contact = item.payload<KABC::Addressee>();
110  if ( !email.isEmpty() ) {
111  contact.insertEmail( email, true );
112  }
113 
114  mContacts.append( contact );
115  } else
116  kWarning() << "Contact for Akonadi item" << item.id() << "does not exist anymore!";
117  }
118 
119  mFetchCount--;
120 
121  if ( mFetchCount == 0 ) {
122  mParent->emitResult();
123  }
124  }
125 
126  ContactGroupExpandJob *mParent;
127  KABC::ContactGroup mGroup;
128  QString mName;
129  KABC::Addressee::List mContacts;
130 
131  int mFetchCount;
132 };
133 
134 ContactGroupExpandJob::ContactGroupExpandJob( const KABC::ContactGroup &group, QObject * parent )
135  : KJob( parent ), d( new Private( group, this ) )
136 {
137 }
138 
139 ContactGroupExpandJob::ContactGroupExpandJob( const QString &name, QObject * parent )
140  : KJob( parent ), d( new Private( name, this ) )
141 {
142 }
143 
144 ContactGroupExpandJob::~ContactGroupExpandJob()
145 {
146  delete d;
147 }
148 
149 void ContactGroupExpandJob::start()
150 {
151  if ( !d->mName.isEmpty() && !d->mName.contains( QLatin1Char( '@' ) ) ) {
152  // we have to search the contact group first
153  ContactGroupSearchJob *searchJob = new ContactGroupSearchJob( this );
154  searchJob->setQuery( ContactGroupSearchJob::Name, d->mName );
155  searchJob->setLimit( 1 );
156  connect( searchJob, SIGNAL(result(KJob*)), this, SLOT(searchResult(KJob*)) );
157  } else {
158  d->resolveGroup();
159  }
160 }
161 
162 KABC::Addressee::List ContactGroupExpandJob::contacts() const
163 {
164  return d->mContacts;
165 }
166 
167 #include "moc_contactgroupexpandjob.cpp"
Akonadi::ContactGroupSearchJob::setLimit
void setLimit(int limit)
Sets a limit on how many results will be returned by this search job.
Definition: contactgroupsearchjob.cpp:175
Akonadi::ContactGroupSearchJob::setQuery
void setQuery(Criterion criterion, const QString &value)
Sets the criterion and value for the search.
Definition: contactgroupsearchjob.cpp:62
Akonadi::ContactGroupExpandJob::contacts
KABC::Addressee::List contacts() const
Returns the list of contacts.
Definition: contactgroupexpandjob.cpp:162
Akonadi::ItemFetchScope::fetchFullPayload
void fetchFullPayload(bool fetch=true)
Sets whether the full payload shall be fetched.
Definition: itemfetchscope.cpp:68
Akonadi::ItemFetchJob::items
Item::List items() const
Returns the fetched items.
Definition: itemfetchjob.cpp:228
Akonadi::ItemFetchJob::fetchScope
ItemFetchScope & fetchScope()
Returns the item fetch scope.
Definition: itemfetchjob.cpp:256
Akonadi::ContactGroupExpandJob::start
virtual void start()
Starts the expand job.
Definition: contactgroupexpandjob.cpp:149
Akonadi::ContactGroupSearchJob
Job that searches for contact groups in the Akonadi storage.
Definition: contactgroupsearchjob.h:59
Akonadi::ContactGroupExpandJob::ContactGroupExpandJob
ContactGroupExpandJob(const KABC::ContactGroup &group, QObject *parent=0)
Creates a new contact group expand job.
Definition: contactgroupexpandjob.cpp:134
Akonadi::ContactGroupExpandJob::~ContactGroupExpandJob
~ContactGroupExpandJob()
Destroys the contact group expand job.
Definition: contactgroupexpandjob.cpp:144
Akonadi::ContactGroupExpandJob
Job that expands a ContactGroup to a list of contacts.
Definition: contactgroupexpandjob.h:64
Akonadi::ItemFetchJob
Job that fetches items from the Akonadi storage.
Definition: itemfetchjob.h:82
Akonadi::ContactGroupSearchJob::Name
The name of the contact group.
Definition: contactgroupsearchjob.h:80
Akonadi::ContactGroupSearchJob::contactGroups
KABC::ContactGroup::List contactGroups() const
Returns the contact groups that matched the search criteria.
Definition: contactgroupsearchjob.cpp:180
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:27 by doxygen 1.8.7 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 API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



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