Akonadi Contacts

contactgroupexpandjob.cpp
1 /*
2  This file is part of Akonadi Contact.
3 
4  SPDX-FileCopyrightText: 2009 Tobias Koenig <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 
9 #include "contactgroupexpandjob.h"
10 #include "akonadi_contact_debug.h"
11 #include "job/contactgroupsearchjob.h"
12 #include <itemfetchjob.h>
13 #include <itemfetchscope.h>
14 #include <itemsearchjob.h>
15 
16 using namespace Akonadi;
17 
18 class Q_DECL_HIDDEN ContactGroupExpandJob::Private
19 {
20 public:
21  Private(const KContacts::ContactGroup &group, ContactGroupExpandJob *parent)
22  : mParent(parent)
23  , mGroup(group)
24  {
25  }
26 
27  Private(const QString &name, ContactGroupExpandJob *parent)
28  : mParent(parent)
29  , mName(name)
30  {
31  }
32 
33  void resolveGroup()
34  {
35  for (int i = 0, total = mGroup.dataCount(); i < total; ++i) {
36  const KContacts::ContactGroup::Data data = mGroup.data(i);
37 
38  KContacts::Addressee contact;
39  contact.setNameFromString(data.name());
40  contact.insertEmail(data.email(), true);
41 
42  mContacts.append(contact);
43  }
44 
45  for (int i = 0, total = mGroup.contactReferenceCount(); i < total; ++i) {
46  const KContacts::ContactGroup::ContactReference reference = mGroup.contactReference(i);
47 
48  Item item;
49  if (!reference.gid().isEmpty()) {
50  item.setGid(reference.gid());
51  } else {
52  item.setId(reference.uid().toLongLong());
53  }
54  auto job = new ItemFetchJob(item, mParent);
55  job->fetchScope().fetchFullPayload();
56  job->setProperty("preferredEmail", reference.preferredEmail());
57 
58  mParent->connect(job, &ItemFetchJob::result, mParent, [this](KJob *job) {
59  fetchResult(job);
60  });
61 
62  mFetchCount++;
63  }
64 
65  if (mFetchCount == 0) { // nothing to fetch, so we can return immediately
66  mParent->emitResult();
67  }
68  }
69 
70  void searchResult(KJob *job)
71  {
72  if (job->error()) {
73  mParent->setError(job->error());
74  mParent->setErrorText(job->errorText());
75  mParent->emitResult();
76  return;
77  }
78 
79  auto searchJob = qobject_cast<ContactGroupSearchJob *>(job);
80 
81  if (searchJob->contactGroups().isEmpty()) {
82  mParent->emitResult();
83  return;
84  }
85 
86  mGroup = searchJob->contactGroups().at(0);
87  resolveGroup();
88  }
89 
90  void fetchResult(KJob *job)
91  {
92  const ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob *>(job);
93 
94  const Item::List items = fetchJob->items();
95  if (!items.isEmpty()) {
96  const QString email = fetchJob->property("preferredEmail").toString();
97 
98  const Item item = items.first();
99  if (item.hasPayload<KContacts::Addressee>()) {
100  auto contact = item.payload<KContacts::Addressee>();
101  if (!email.isEmpty()) {
102  contact.insertEmail(email, true);
103  }
104 
105  mContacts.append(contact);
106  } else {
107  qCWarning(AKONADICONTACT_LOG) << "Contact for Akonadi item" << item.id() << "does not exist anymore!";
108  }
109  }
110 
111  mFetchCount--;
112 
113  if (mFetchCount == 0) {
114  mParent->emitResult();
115  }
116  }
117 
118  ContactGroupExpandJob *mParent = nullptr;
120  QString mName;
121  KContacts::Addressee::List mContacts;
122 
123  int mFetchCount = 0;
124 };
125 
127  : KJob(parent)
128  , d(new Private(group, this))
129 {
130 }
131 
133  : KJob(parent)
134  , d(new Private(name, this))
135 {
136 }
137 
139 {
140  delete d;
141 }
142 
144 {
145  if (!d->mName.isEmpty() && !d->mName.contains(QLatin1Char('@'))) {
146  // we have to search the contact group first
147  auto searchJob = new ContactGroupSearchJob(this);
148  searchJob->setQuery(ContactGroupSearchJob::Name, d->mName);
149  searchJob->setLimit(1);
150  connect(searchJob, &ContactGroupSearchJob::result, this, [this](KJob *job) {
151  d->searchResult(job);
152  });
153  } else {
155  this,
156  [this]() {
157  d->resolveGroup();
158  },
160  }
161 }
162 
164 {
165  return d->mContacts;
166 }
167 
168 #include "moc_contactgroupexpandjob.cpp"
void emitResult()
void setId(Id identifier)
T payload() const
void insertEmail(const QString &email, bool preferred=false, const QMap< QString, QStringList > &param=QMap< QString, QStringList >())
void setGid(const QString &gid)
Item::List items() const
Id id() const
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
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)
AddresseeList List
Job that expands a ContactGroup to a list of contacts.
The name of the contact group.
void result(KJob *job)
~ContactGroupExpandJob() override
Destroys the contact group expand job.
bool hasPayload() const
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-2021 The KDE developers.
Generated on Wed Jun 23 2021 23:09:24 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.