File indexing completed on 2025-01-19 04:46:51
0001 /* 0002 SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "updatecontactjob.h" 0008 #include <Akonadi/ContactSearchJob> 0009 #include <Akonadi/ItemModifyJob> 0010 #include <KLocalizedString> 0011 #include <KMessageBox> 0012 0013 UpdateContactJob::UpdateContactJob(const QString &email, const KContacts::Addressee &contact, QWidget *parentWidget, QObject *parent) 0014 : KJob(parent) 0015 , mEmail(email) 0016 , mContact(contact) 0017 , mParentWidget(parentWidget) 0018 { 0019 } 0020 0021 UpdateContactJob::~UpdateContactJob() = default; 0022 0023 void UpdateContactJob::slotSearchDone(KJob *job) 0024 { 0025 if (job->error()) { 0026 setError(job->error()); 0027 setErrorText(job->errorText()); 0028 emitResult(); 0029 return; 0030 } 0031 0032 const Akonadi::ContactSearchJob *searchJob = qobject_cast<Akonadi::ContactSearchJob *>(job); 0033 0034 const KContacts::Addressee::List contacts = searchJob->contacts(); 0035 0036 if (contacts.isEmpty()) { 0037 const QString text = i18n("The vCard's primary email address is not in addressbook."); 0038 KMessageBox::information(mParentWidget, text); 0039 setError(UserDefinedError); 0040 emitResult(); 0041 return; 0042 } else if (contacts.count() > 1) { 0043 const QString text = i18n("There are two or more contacts with same email stored in addressbook."); 0044 KMessageBox::information(mParentWidget, text); 0045 setError(UserDefinedError); 0046 emitResult(); 0047 return; 0048 } 0049 Akonadi::Item item = searchJob->items().at(0); 0050 item.setPayload<KContacts::Addressee>(mContact); 0051 auto modifyJob = new Akonadi::ItemModifyJob(item); 0052 connect(modifyJob, &Akonadi::ItemModifyJob::result, this, &UpdateContactJob::slotUpdateContactDone); 0053 } 0054 0055 void UpdateContactJob::slotUpdateContactDone(KJob *job) 0056 { 0057 if (job->error()) { 0058 setError(job->error()); 0059 setErrorText(job->errorText()); 0060 emitResult(); 0061 return; 0062 } 0063 0064 const QString text = i18n( 0065 "The vCard was updated in your address book; " 0066 "you can add more information to this " 0067 "entry by opening the address book."); 0068 KMessageBox::information(mParentWidget, text, QString(), QStringLiteral("updatedtokabc")); 0069 0070 emitResult(); 0071 } 0072 0073 void UpdateContactJob::start() 0074 { 0075 if (mEmail.isEmpty()) { 0076 const QString text = i18n("Email not specified"); 0077 KMessageBox::information(mParentWidget, text); 0078 setError(UserDefinedError); 0079 emitResult(); 0080 return; 0081 } 0082 // first check whether a contact with the same email exists already 0083 auto searchJob = new Akonadi::ContactSearchJob(this); 0084 searchJob->setLimit(1); 0085 searchJob->setQuery(Akonadi::ContactSearchJob::Email, mEmail.toLower(), Akonadi::ContactSearchJob::ExactMatch); 0086 0087 connect(searchJob, &Akonadi::ContactSearchJob::result, this, &UpdateContactJob::slotSearchDone); 0088 } 0089 0090 #include "moc_updatecontactjob.cpp"