Akonadi Contacts

contactgroupexpandjob.cpp
1/*
2 This file is part of Akonadi Contact.
3
4 SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org>
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>
15using namespace Akonadi;
16
17class Akonadi::ContactGroupExpandJobPrivate
18{
19public:
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
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) {
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;
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"
Job that expands a ContactGroup to a list of contacts.
~ContactGroupExpandJob() override
Destroys the contact group expand job.
ContactGroupExpandJob(const KContacts::ContactGroup &group, QObject *parent=nullptr)
Creates a new contact group expand job.
KContacts::Addressee::List contacts() const
Returns the list of contacts.
void start() override
Starts the expand job.
Job that searches for contact groups in the Akonadi storage.
@ Name
The name of the contact group.
Item::List items() const
AddresseeList List
void setNameFromString(const QString &s)
void addEmail(const Email &email)
int contactReferenceCount() const
Data & data(int index)
ContactReference & contactReference(int index)
void setErrorText(const QString &errorText)
void emitResult()
int error() const
void result(KJob *job)
void setError(int errorCode)
QString errorText() const
A widget for editing the display name of a contact.
QString name(StandardShortcut id)
T & first()
bool isEmpty() const const
bool invokeMethod(QObject *context, Functor &&function, FunctorReturnType *ret)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QVariant property(const char *name) const const
bool isEmpty() const const
qlonglong toLongLong(bool *ok, int base) const const
QueuedConnection
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:13:20 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.