File indexing completed on 2024-10-27 04:51:06

0001 /*
0002   SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: LGPL-2.0-or-later
0005 
0006 */
0007 
0008 #include "createnewcontactjob.h"
0009 
0010 #include <PimCommon/BroadcastStatus>
0011 
0012 #include <KContacts/Addressee>
0013 #include <KContacts/ContactGroup>
0014 
0015 #include <Akonadi/AgentFilterProxyModel>
0016 #include <Akonadi/AgentInstanceCreateJob>
0017 #include <Akonadi/AgentTypeDialog>
0018 #include <Akonadi/CollectionFetchJob>
0019 #include <Akonadi/CollectionFetchScope>
0020 #include <Akonadi/ContactEditorDialog>
0021 
0022 #include <KLocalizedString>
0023 #include <KMessageBox>
0024 #include <QPointer>
0025 
0026 CreateNewContactJob::CreateNewContactJob(QWidget *parentWidget, QObject *parent)
0027     : KJob(parent)
0028     , mParentWidget(parentWidget)
0029 {
0030 }
0031 
0032 CreateNewContactJob::~CreateNewContactJob() = default;
0033 
0034 void CreateNewContactJob::start()
0035 {
0036     auto const addressBookJob = new Akonadi::CollectionFetchJob(Akonadi::Collection::root(), Akonadi::CollectionFetchJob::Recursive);
0037 
0038     addressBookJob->fetchScope().setContentMimeTypes(QStringList() << KContacts::Addressee::mimeType());
0039     connect(addressBookJob, &KJob::result, this, &CreateNewContactJob::slotCollectionsFetched);
0040 }
0041 
0042 void CreateNewContactJob::slotCollectionsFetched(KJob *job)
0043 {
0044     if (job->error()) {
0045         setError(job->error());
0046         setErrorText(job->errorText());
0047         emitResult();
0048         return;
0049     }
0050 
0051     const Akonadi::CollectionFetchJob *addressBookJob = qobject_cast<Akonadi::CollectionFetchJob *>(job);
0052 
0053     Akonadi::Collection::List canCreateItemCollections;
0054 
0055     const Akonadi::Collection::List lstAddressCollection = addressBookJob->collections();
0056     for (const Akonadi::Collection &collection : lstAddressCollection) {
0057         if (Akonadi::Collection::CanCreateItem & collection.rights()) {
0058             canCreateItemCollections.append(collection);
0059         }
0060     }
0061     if (canCreateItemCollections.isEmpty()) {
0062         QPointer<Akonadi::AgentTypeDialog> dlg = new Akonadi::AgentTypeDialog(mParentWidget);
0063         dlg->setWindowTitle(i18nc("@title:window", "Add to Address Book"));
0064         dlg->agentFilterProxyModel()->addMimeTypeFilter(KContacts::Addressee::mimeType());
0065         dlg->agentFilterProxyModel()->addMimeTypeFilter(KContacts::ContactGroup::mimeType());
0066         dlg->agentFilterProxyModel()->addCapabilityFilter(QStringLiteral("Resource"));
0067 
0068         if (dlg->exec()) {
0069             const Akonadi::AgentType agentType = dlg->agentType();
0070             delete dlg;
0071             if (agentType.isValid()) {
0072                 auto createAgentJob = new Akonadi::AgentInstanceCreateJob(agentType, this);
0073                 connect(createAgentJob, &Akonadi::AgentInstanceCreateJob::result, this, &CreateNewContactJob::slotResourceCreationDone);
0074                 createAgentJob->configure(mParentWidget);
0075                 createAgentJob->start();
0076                 return;
0077             } else { // if agent is not valid => return error and finish job
0078                 setError(UserDefinedError);
0079                 emitResult();
0080                 return;
0081             }
0082         } else { // dialog canceled => return error and finish job
0083             delete dlg;
0084             setError(UserDefinedError);
0085             emitResult();
0086             return;
0087         }
0088     }
0089     createContact();
0090     emitResult();
0091 }
0092 
0093 void CreateNewContactJob::slotResourceCreationDone(KJob *job)
0094 {
0095     if (job->error()) {
0096         setError(job->error());
0097         setErrorText(job->errorText());
0098         emitResult();
0099         return;
0100     }
0101     createContact();
0102     emitResult();
0103 }
0104 
0105 void CreateNewContactJob::createContact()
0106 {
0107     QPointer<Akonadi::ContactEditorDialog> dlg = new Akonadi::ContactEditorDialog(Akonadi::ContactEditorDialog::CreateMode, mParentWidget);
0108     connect(dlg.data(), &Akonadi::ContactEditorDialog::contactStored, this, &CreateNewContactJob::contactStored);
0109     connect(dlg.data(), &Akonadi::ContactEditorDialog::error, this, &CreateNewContactJob::slotContactEditorError);
0110     dlg->exec();
0111     delete dlg;
0112 }
0113 
0114 void CreateNewContactJob::contactStored(const Akonadi::Item &item)
0115 {
0116     Q_UNUSED(item)
0117     PimCommon::BroadcastStatus::instance()->setStatusMsg(i18n("Contact created successfully"));
0118 }
0119 
0120 void CreateNewContactJob::slotContactEditorError(const QString &error)
0121 {
0122     KMessageBox::error(mParentWidget, i18n("Contact cannot be stored: %1", error), i18nc("@title:window", "Failed to store contact"));
0123 }
0124 
0125 #include "moc_createnewcontactjob.cpp"