Akonadi Contacts

contactgroupexpandjob.cpp
1 /*
2  This file is part of Akonadi Contact.
3 
4  Copyright (c) 2009 Tobias Koenig <[email protected]>
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 #include "akonadi_contact_debug.h"
24 #include "job/contactgroupsearchjob.h"
25 #include <itemfetchjob.h>
26 #include <itemfetchscope.h>
27 #include <itemsearchjob.h>
28 
29 using namespace Akonadi;
30 
31 class Q_DECL_HIDDEN ContactGroupExpandJob::Private
32 {
33 public:
34  Private(const KContacts::ContactGroup &group, ContactGroupExpandJob *parent)
35  : mParent(parent)
36  , mGroup(group)
37  {
38  }
39 
40  Private(const QString &name, ContactGroupExpandJob *parent)
41  : mParent(parent)
42  , mName(name)
43  {
44  }
45 
46  void resolveGroup()
47  {
48  for (int i = 0, total = mGroup.dataCount(); i < total; ++i) {
49  const KContacts::ContactGroup::Data data = mGroup.data(i);
50 
51  KContacts::Addressee contact;
52  contact.setNameFromString(data.name());
53  contact.insertEmail(data.email(), true);
54 
55  mContacts.append(contact);
56  }
57 
58  for (int i = 0, total = mGroup.contactReferenceCount(); i < total; ++i) {
59  const KContacts::ContactGroup::ContactReference reference = mGroup.contactReference(i);
60 
61  Item item;
62  if (!reference.gid().isEmpty()) {
63  item.setGid(reference.gid());
64  } else {
65  item.setId(reference.uid().toLongLong());
66  }
67  ItemFetchJob *job = new ItemFetchJob(item, mParent);
69  job->setProperty("preferredEmail", reference.preferredEmail());
70 
71  mParent->connect(job, &ItemFetchJob::result, mParent, [this](KJob *job) {
72  fetchResult(job);
73  });
74 
75  mFetchCount++;
76  }
77 
78  if (mFetchCount == 0) { // nothing to fetch, so we can return immediately
79  mParent->emitResult();
80  }
81  }
82 
83  void searchResult(KJob *job)
84  {
85  if (job->error()) {
86  mParent->setError(job->error());
87  mParent->setErrorText(job->errorText());
88  mParent->emitResult();
89  return;
90  }
91 
92  ContactGroupSearchJob *searchJob = qobject_cast<ContactGroupSearchJob *>(job);
93 
94  if (searchJob->contactGroups().isEmpty()) {
95  mParent->emitResult();
96  return;
97  }
98 
99  mGroup = searchJob->contactGroups().at(0);
100  resolveGroup();
101  }
102 
103  void fetchResult(KJob *job)
104  {
105  const ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob *>(job);
106 
107  const Item::List items = fetchJob->items();
108  if (!items.isEmpty()) {
109  const QString email = fetchJob->property("preferredEmail").toString();
110 
111  const Item item = items.first();
112  if (item.hasPayload<KContacts::Addressee>()) {
113  KContacts::Addressee contact = item.payload<KContacts::Addressee>();
114  if (!email.isEmpty()) {
115  contact.insertEmail(email, true);
116  }
117 
118  mContacts.append(contact);
119  } else {
120  qCWarning(AKONADICONTACT_LOG) << "Contact for Akonadi item" << item.id() << "does not exist anymore!";
121  }
122  }
123 
124  mFetchCount--;
125 
126  if (mFetchCount == 0) {
127  mParent->emitResult();
128  }
129  }
130 
131  ContactGroupExpandJob *mParent = nullptr;
133  QString mName;
134  KContacts::Addressee::List mContacts;
135 
136  int mFetchCount = 0;
137 };
138 
140  : KJob(parent)
141  , d(new Private(group, this))
142 {
143 }
144 
146  : KJob(parent)
147  , d(new Private(name, this))
148 {
149 }
150 
152 {
153  delete d;
154 }
155 
157 {
158  if (!d->mName.isEmpty() && !d->mName.contains(QLatin1Char('@'))) {
159  // we have to search the contact group first
160  ContactGroupSearchJob *searchJob = new ContactGroupSearchJob(this);
161  searchJob->setQuery(ContactGroupSearchJob::Name, d->mName);
162  searchJob->setLimit(1);
163  connect(searchJob, &ContactGroupSearchJob::result, this, [this](KJob *job) {
164  d->searchResult(job);
165  });
166  } else {
167  QMetaObject::invokeMethod(this, [this]() { d->resolveGroup(); }, Qt::QueuedConnection);
168  }
169 }
170 
172 {
173  return d->mContacts;
174 }
175 
176 #include "moc_contactgroupexpandjob.cpp"
void setLimit(int limit)
Sets a limit on how many results will be returned by this search job.
void emitResult()
void setQuery(Criterion criterion, const QString &value)
Sets the criterion and value for the search.
void fetchFullPayload(bool fetch=true)
void insertEmail(const QString &email, bool preferred=false, const QMap< QString, QStringList > &param=QMap< QString, QStringList >())
Item::List items() const
ItemFetchScope & fetchScope()
void start() override
Starts the expand job.
QVariant property(const char *name) const const
KContacts::Addressee::List contacts() const
Returns the list of contacts.
Job that searches for contact groups in the Akonadi storage.
void setNameFromString(const QString &s)
bool isEmpty() const const
KContacts::ContactGroup::List contactGroups() const
Returns the contact groups that matched the search criteria.
ContactGroupExpandJob(const KContacts::ContactGroup &group, QObject *parent=nullptr)
Creates a new contact group expand job.
bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType type, QGenericReturnArgument ret, QGenericArgument val0, QGenericArgument val1, QGenericArgument val2, QGenericArgument val3, QGenericArgument val4, QGenericArgument val5, QGenericArgument val6, QGenericArgument val7, QGenericArgument val8, QGenericArgument val9)
const T & at(int i) const const
AddresseeList List
Job that expands a ContactGroup to a list of contacts.
bool isEmpty() const const
The name of the contact group.
bool setProperty(const char *name, const QVariant &value)
void result(KJob *job)
~ContactGroupExpandJob() override
Destroys the contact group expand job.
QueuedConnection
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
QString toString() const const
qlonglong toLongLong(bool *ok, int base) const const
QString errorText() const
int error() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Aug 10 2020 23:12:25 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.