File indexing completed on 2024-11-24 04:39:33
0001 /* 0002 SPDX-FileCopyrightText: 2010 Tobias Koenig <tokoe@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "openemailaddressjob.h" 0008 0009 #include "job/addemailaddressjob.h" 0010 0011 #include <Akonadi/CollectionDialog> 0012 #include <Akonadi/ContactEditorDialog> 0013 #include <Akonadi/ContactSearchJob> 0014 #include <Akonadi/Item> 0015 #include <Akonadi/ItemCreateJob> 0016 #include <KContacts/Addressee> 0017 #include <QPointer> 0018 0019 using namespace Akonadi; 0020 0021 class Akonadi::OpenEmailAddressJobPrivate 0022 { 0023 public: 0024 OpenEmailAddressJobPrivate(OpenEmailAddressJob *qq, const QString &emailString, QWidget *parentWidget) 0025 : q(qq) 0026 , mCompleteAddress(emailString) 0027 , mParentWidget(parentWidget) 0028 { 0029 KContacts::Addressee::parseEmailAddress(emailString, mName, mEmail); 0030 } 0031 0032 void slotSearchDone(KJob *job) 0033 { 0034 if (job->error()) { 0035 q->setError(job->error()); 0036 q->setErrorText(job->errorText()); 0037 q->emitResult(); 0038 return; 0039 } 0040 0041 const Akonadi::ContactSearchJob *searchJob = qobject_cast<Akonadi::ContactSearchJob *>(job); 0042 0043 const Akonadi::Item::List contacts = searchJob->items(); 0044 if (!contacts.isEmpty()) { 0045 // open the editor with the matching item 0046 QPointer<Akonadi::ContactEditorDialog> dlg = new Akonadi::ContactEditorDialog(Akonadi::ContactEditorDialog::EditMode, mParentWidget); 0047 dlg->setContact(contacts.first()); 0048 dlg->exec(); 0049 delete dlg; 0050 0051 q->emitResult(); 0052 return; 0053 } 0054 0055 auto createJob = new Akonadi::AddEmailAddressJob(mCompleteAddress, mParentWidget, q); 0056 q->connect(createJob, &Akonadi::AddEmailAddressJob::result, q, [this](KJob *job) { 0057 slotAddContactDone(job); 0058 }); 0059 createJob->start(); 0060 } 0061 0062 void slotAddContactDone(KJob *job) 0063 { 0064 if (job->error()) { 0065 q->setError(job->error()); 0066 q->setErrorText(job->errorText()); 0067 q->emitResult(); 0068 return; 0069 } 0070 0071 const Akonadi::AddEmailAddressJob *createJob = qobject_cast<Akonadi::AddEmailAddressJob *>(job); 0072 0073 // open the editor with the matching item 0074 QPointer<Akonadi::ContactEditorDialog> dlg = new Akonadi::ContactEditorDialog(Akonadi::ContactEditorDialog::EditMode, mParentWidget); 0075 dlg->setContact(createJob->contact()); 0076 dlg->exec(); 0077 delete dlg; 0078 0079 q->emitResult(); 0080 } 0081 0082 OpenEmailAddressJob *const q; 0083 const QString mCompleteAddress; 0084 QString mEmail; 0085 QString mName; 0086 QWidget *const mParentWidget; 0087 }; 0088 0089 OpenEmailAddressJob::OpenEmailAddressJob(const QString &email, QWidget *parentWidget, QObject *parent) 0090 : KJob(parent) 0091 , d(new OpenEmailAddressJobPrivate(this, email, parentWidget)) 0092 { 0093 } 0094 0095 OpenEmailAddressJob::~OpenEmailAddressJob() = default; 0096 0097 void OpenEmailAddressJob::start() 0098 { 0099 // first check whether a contact with the same email exists already 0100 auto searchJob = new Akonadi::ContactSearchJob(this); 0101 searchJob->setLimit(1); 0102 searchJob->setQuery(Akonadi::ContactSearchJob::Email, d->mEmail.toLower(), Akonadi::ContactSearchJob::ExactMatch); 0103 connect(searchJob, &Akonadi::ContactSearchJob::result, this, [this](KJob *job) { 0104 d->slotSearchDone(job); 0105 }); 0106 } 0107 0108 #include "moc_openemailaddressjob.cpp"