File indexing completed on 2024-12-22 04:55:32
0001 /* 0002 SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "importexportengine.h" 0008 0009 #include <Akonadi/CollectionDialog> 0010 #include <Akonadi/Item> 0011 #include <Akonadi/ItemCreateJob> 0012 #include <KLocalizedString> 0013 #include <QPointer> 0014 #include <QProgressDialog> 0015 0016 using namespace KAddressBookImportExport; 0017 0018 ImportExportEngine::ImportExportEngine(QObject *parent) 0019 : QObject(parent) 0020 { 0021 } 0022 0023 ImportExportEngine::~ImportExportEngine() = default; 0024 0025 void ImportExportEngine::setContactList(const ContactList &contacts) 0026 { 0027 mContactsList = contacts; 0028 } 0029 0030 void ImportExportEngine::setParentWidget(QWidget *parent) 0031 { 0032 mParentWidget = parent; 0033 } 0034 0035 QWidget *ImportExportEngine::parentWidget() const 0036 { 0037 return mParentWidget; 0038 } 0039 0040 void ImportExportEngine::setDefaultAddressBook(const Akonadi::Collection &defaultAddressBook) 0041 { 0042 mDefaultAddressBook = defaultAddressBook; 0043 } 0044 0045 void ImportExportEngine::importContacts() 0046 { 0047 if (mContactsList.isEmpty()) { // nothing to import 0048 Q_EMIT finished(); 0049 deleteLater(); 0050 return; 0051 } 0052 0053 const QStringList mimeTypes(KContacts::Addressee::mimeType()); 0054 0055 QPointer<Akonadi::CollectionDialog> dlg = new Akonadi::CollectionDialog(mParentWidget); 0056 dlg->setMimeTypeFilter(mimeTypes); 0057 dlg->setAccessRightsFilter(Akonadi::Collection::CanCreateItem); 0058 dlg->setWindowTitle(i18nc("@title:window", "Select Address Book")); 0059 dlg->setDescription(i18n("Select the address book the imported contact(s) shall be saved in:")); 0060 dlg->setDefaultCollection(mDefaultAddressBook); 0061 0062 if (!dlg->exec()) { 0063 delete dlg; 0064 deleteLater(); 0065 return; 0066 } 0067 0068 const Akonadi::Collection collection = dlg->selectedCollection(); 0069 delete dlg; 0070 0071 mNumberElementToImport = mContactsList.count(); 0072 if (!mImportProgressDialog) { 0073 mImportProgressDialog = new QProgressDialog(mParentWidget); 0074 mImportProgressDialog->setWindowTitle(i18nc("@title:window", "Import Contacts")); 0075 mImportProgressDialog->setLabelText(i18np("Importing one contact to %2", "Importing %1 contacts to %2", mNumberElementToImport, collection.name())); 0076 mImportProgressDialog->setCancelButton(nullptr); 0077 mImportProgressDialog->setAutoClose(true); 0078 mImportProgressDialog->setRange(1, mNumberElementToImport); 0079 } 0080 0081 mImportProgressDialog->show(); 0082 mImportDone = 0; 0083 0084 for (int i = 0; i < mContactsList.addressList().count(); ++i) { 0085 Akonadi::Item item; 0086 item.setPayload<KContacts::Addressee>(mContactsList.addressList().at(i)); 0087 item.setMimeType(KContacts::Addressee::mimeType()); 0088 0089 auto job = new Akonadi::ItemCreateJob(item, collection); 0090 connect(job, &Akonadi::ItemCreateJob::result, this, &ImportExportEngine::slotImportJobDone); 0091 } 0092 const int numberGroup(mContactsList.contactGroupList().count()); 0093 for (int i = 0; i < numberGroup; ++i) { 0094 Akonadi::Item groupItem(KContacts::ContactGroup::mimeType()); 0095 groupItem.setPayload<KContacts::ContactGroup>(mContactsList.contactGroupList().at(i)); 0096 0097 Akonadi::Job *createJob = new Akonadi::ItemCreateJob(groupItem, collection); 0098 connect(createJob, &KJob::result, this, &ImportExportEngine::slotImportJobDone); 0099 } 0100 } 0101 0102 void ImportExportEngine::slotImportJobDone(KJob *) 0103 { 0104 mImportDone++; 0105 if (mImportProgressDialog) { 0106 mImportProgressDialog->setValue(mImportProgressDialog->value() + 1); 0107 0108 // cleanup on last step 0109 if (mImportProgressDialog->value() == mImportProgressDialog->maximum()) { 0110 mImportProgressDialog->deleteLater(); 0111 mImportProgressDialog = nullptr; 0112 } 0113 } 0114 if (mImportDone >= mNumberElementToImport) { 0115 Q_EMIT finished(); 0116 deleteLater(); 0117 } 0118 } 0119 0120 #include "moc_importexportengine.cpp"