File indexing completed on 2024-06-23 05:21:19

0001 /* Copyright (C) 2017 Roland Pallai <dap78@magex.hu>
0002    Copyright (C) 2013 Pali Rohár <pali.rohar@gmail.com>
0003 
0004    This file is part of the Trojita Qt IMAP e-mail client,
0005    http://trojita.flaska.net/
0006 
0007    This program is free software; you can redistribute it and/or
0008    modify it under the terms of the GNU General Public License as
0009    published by the Free Software Foundation; either version 2 of
0010    the License or (at your option) version 3 or any later version
0011    accepted by the membership of KDE e.V. (or its successor approved
0012    by the membership of KDE e.V.), which shall act as a proxy
0013    defined in Section 14 of version 3 of the license.
0014 
0015    This program is distributed in the hope that it will be useful,
0016    but WITHOUT ANY WARRANTY; without even the implied warranty of
0017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018    GNU General Public License for more details.
0019 
0020    You should have received a copy of the GNU General Public License
0021    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0022 */
0023 
0024 #include "AkonadiAddressbookNamesJob.h"
0025 
0026 AkonadiAddressbookNamesJob::AkonadiAddressbookNamesJob(const QString &email, AkonadiAddressbook *parent) :
0027     AddressbookNamesJob(parent), m_email(email), m_parent(parent), m_job(nullptr)
0028 {
0029 }
0030 
0031 void AkonadiAddressbookNamesJob::doStart()
0032 {
0033     Q_ASSERT(!m_job);
0034     m_job = new Akonadi::ContactSearchJob(this);
0035     m_job->setQuery(Akonadi::ContactSearchJob::Email, m_email, Akonadi::ContactSearchJob::ExactMatch);
0036     connect(m_job, &KJob::result, this, &AkonadiAddressbookNamesJob::searchResult);
0037 }
0038 
0039 void AkonadiAddressbookNamesJob::doStop()
0040 {
0041     Q_ASSERT(m_job);
0042     disconnect(m_job, &KJob::result, this, &AkonadiAddressbookNamesJob::searchResult);
0043     // Kills and deletes the job immediately
0044     bool ok = m_job->kill();
0045     Q_ASSERT(ok); Q_UNUSED(ok);
0046     m_job = nullptr;
0047     emit error(AddressbookJob::Stopped);
0048     finished();
0049 }
0050 
0051 void AkonadiAddressbookNamesJob::searchResult(KJob *job)
0052 {
0053     Akonadi::ContactSearchJob *searchJob = qobject_cast<Akonadi::ContactSearchJob*>(job);
0054     Q_ASSERT(searchJob);
0055     if (job->error()) {
0056         qWarning() << "AkonadiAddressbookNamesJob::searchResult:" << job->errorString();
0057         emit error(AddressbookJob::UnknownError);
0058     } else {
0059         QStringList displayNames;
0060         const auto contacts = searchJob->contacts();
0061 
0062         Q_FOREACH(const KContacts::Addressee &contact, contacts) {
0063             displayNames << contact.realName();
0064         }
0065         emit prettyNamesForAddressAvailable(displayNames);
0066     }
0067 
0068     finished();
0069 }