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 <Akonadi/ItemFetchJob>
13 #include <Akonadi/ItemFetchScope>
14 #include <Akonadi/ItemSearchJob>
15 using namespace Akonadi;
16 
17 class Akonadi::ContactGroupExpandJobPrivate
18 {
19 public:
20  ContactGroupExpandJobPrivate(const KContacts::ContactGroup &group, ContactGroupExpandJob *parent)
21  : mParent(parent)
22  , mGroup(group)
23  {
24  }
25 
26  ContactGroupExpandJobPrivate(const QString &name, ContactGroupExpandJob *parent)
27  : mParent(parent)
28  , mName(name)
29  {
30  }
31 
32  void resolveGroup()
33  {
34  for (int i = 0, total = mGroup.dataCount(); i < total; ++i) {
35  const KContacts::ContactGroup::Data data = mGroup.data(i);
36 
37  KContacts::Addressee contact;
38  contact.setNameFromString(data.name());
39  KContacts::Email email(data.email());
40  email.setPreferred(true);
41  contact.addEmail(email);
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 preferredEmail = 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 (!preferredEmail.isEmpty()) {
102  KContacts::Email email(preferredEmail);
103  email.setPreferred(true);
104  contact.addEmail(email);
105  }
106  mContacts.append(contact);
107  } else {
108  qCWarning(AKONADICONTACT_LOG) << "Contact for Akonadi item" << item.id() << "does not exist anymore!";
109  }
110  }
111 
112  mFetchCount--;
113 
114  if (mFetchCount == 0) {
115  mParent->emitResult();
116  }
117  }
118 
119  ContactGroupExpandJob *const mParent;
121  QString mName;
122  KContacts::Addressee::List mContacts;
123 
124  int mFetchCount = 0;
125 };
126 
128  : KJob(parent)
129  , d(new ContactGroupExpandJobPrivate(group, this))
130 {
131 }
132 
134  : KJob(parent)
135  , d(new ContactGroupExpandJobPrivate(name, this))
136 {
137 }
138 
140 
142 {
143  if (!d->mName.isEmpty() && !d->mName.contains(QLatin1Char('@'))) {
144  // we have to search the contact group first
145  auto searchJob = new ContactGroupSearchJob(this);
146  searchJob->setQuery(ContactGroupSearchJob::Name, d->mName);
147  searchJob->setLimit(1);
148  connect(searchJob, &ContactGroupSearchJob::result, this, [this](KJob *job) {
149  d->searchResult(job);
150  });
151  } else {
153  this,
154  [this]() {
155  d->resolveGroup();
156  },
158  }
159 }
160 
162 {
163  return d->mContacts;
164 }
165 
166 #include "moc_contactgroupexpandjob.cpp"
void result(KJob *job)
qlonglong toLongLong(bool *ok, int base) const const
~ContactGroupExpandJob() override
Destroys the contact group expand job.
void setGid(const QString &gid)
KContacts::Addressee::List contacts() const
Returns the list of contacts.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
bool hasPayload() const
ContactGroupExpandJob(const KContacts::ContactGroup &group, QObject *parent=nullptr)
Creates a new contact group expand job.
QVector< Item > List
Item::List items() const
bool isEmpty() const const
QString errorText() const
QueuedConnection
Job that expands a ContactGroup to a list of contacts.
Id id() const
void setNameFromString(const QString &s)
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 char * name(StandardAction id)
void addEmail(const Email &email)
Job that searches for contact groups in the Akonadi storage.
AddresseeList List
int error() const
T payload() const
void setId(Id identifier)
@ Name
The name of the contact group.
QString toString() const const
QVariant property(const char *name) const const
void start() override
Starts the expand job.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sat Apr 1 2023 04:09:04 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.