Commit c19d88e1 authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Add support for import

parent 87e31e59
......@@ -8,7 +8,7 @@ generate_export_header(kaddressbookimportexportlibprivate BASE_NAME kaddressbook
target_link_libraries(kaddressbookimportexportlibprivate
PRIVATE
KF5::I18n KF5::Contacts KF5::AkonadiContact KF5::Contacts KF5::KaddressbookImportExport
KF5::I18n KF5::Contacts KF5::AkonadiContact KF5::Contacts KF5::AkonadiWidgets KF5::KaddressbookImportExport
)
set_target_properties(kaddressbookimportexportlibprivate
......
......@@ -19,8 +19,19 @@
#include "importexportengine.h"
#include <AkonadiWidgets/CollectionDialog>
#include <KContacts/Addressee>
#include <KContacts/ContactGroup>
#include <AkonadiCore/ItemCreateJob>
#include <KLocalizedString>
#include <QProgressDialog>
#include <QPointer>
#include <AkonadiCore/Item>
ImportExportEngine::ImportExportEngine(QObject *parent)
: QObject(parent)
: QObject(parent),
mParentWidget(Q_NULLPTR),
mImportProgressDialog(Q_NULLPTR)
{
}
......@@ -40,7 +51,88 @@ void ImportExportEngine::exportContacts()
}
void ImportExportEngine::setParentWidget(QWidget *parent)
{
mParentWidget = parent;
}
QWidget *ImportExportEngine::parentWidget() const
{
return mParentWidget;
}
void ImportExportEngine::setDefaultAddressBook(const Akonadi::Collection &defaultAddressBook)
{
mDefaultAddressBook = defaultAddressBook;
}
void ImportExportEngine::importContacts()
{
if (mContactsList.isEmpty()) { // nothing to import
return;
}
const QStringList mimeTypes(KContacts::Addressee::mimeType());
QPointer<Akonadi::CollectionDialog> dlg = new Akonadi::CollectionDialog(mParentWidget);
dlg->setMimeTypeFilter(mimeTypes);
dlg->setAccessRightsFilter(Akonadi::Collection::CanCreateItem);
dlg->setWindowTitle(i18n("Select Address Book"));
dlg->setDescription(
i18n("Select the address book the imported contact(s) shall be saved in:"));
dlg->setDefaultCollection(mDefaultAddressBook);
if (!dlg->exec() || !dlg) {
delete dlg;
return;
}
const Akonadi::Collection collection = dlg->selectedCollection();
delete dlg;
if (!mImportProgressDialog) {
mImportProgressDialog = new QProgressDialog(mParentWidget);
mImportProgressDialog->setWindowTitle(i18n("Import Contacts"));
mImportProgressDialog->setLabelText(
i18np("Importing one contact to %2", "Importing %1 contacts to %2",
mContactsList.count(), collection.name()));
mImportProgressDialog->setCancelButton(Q_NULLPTR);
mImportProgressDialog->setAutoClose(true);
mImportProgressDialog->setRange(1, mContactsList.count());
}
mImportProgressDialog->show();
for (int i = 0; i < mContactsList.addressList().count(); ++i) {
Akonadi::Item item;
item.setPayload<KContacts::Addressee>(mContactsList.addressList().at(i));
item.setMimeType(KContacts::Addressee::mimeType());
Akonadi::ItemCreateJob *job = new Akonadi::ItemCreateJob(item, collection);
connect(job, &Akonadi::ItemCreateJob::result, this, &ImportExportEngine::slotImportJobDone);
}
const int numberGroup(mContactsList.contactGroupList().count());
for (int i = 0; i < numberGroup; ++i) {
Akonadi::Item groupItem(KContacts::ContactGroup::mimeType());
groupItem.setPayload<KContacts::ContactGroup>(mContactsList.contactGroupList().at(i));
Akonadi::Job *createJob = new Akonadi::ItemCreateJob(groupItem, collection);
connect(createJob, &KJob::result, this, &ImportExportEngine::slotImportJobDone);
}
}
void ImportExportEngine::slotImportJobDone(KJob *)
{
if (!mImportProgressDialog) {
return;
}
mImportProgressDialog->setValue(mImportProgressDialog->value() + 1);
// cleanup on last step
if (mImportProgressDialog->value() == mImportProgressDialog->maximum()) {
mImportProgressDialog->deleteLater();
mImportProgressDialog = Q_NULLPTR;
}
}
......@@ -22,7 +22,9 @@
#include <QObject>
#include <KAddressBookImportExport/KAddressBookImportExportContactList>
#include <AkonadiCore/Collection>
class QProgressDialog;
class KJob;
class ImportExportEngine : public QObject
{
Q_OBJECT
......@@ -33,8 +35,16 @@ public:
void importContacts();
void exportContacts();
void setParentWidget(QWidget *parent);
QWidget *parentWidget() const;
void setDefaultAddressBook(const Akonadi::Collection &defaultAddressBook);
private:
KAddressBookImportExport::KAddressBookImportExportContactList mContactsList;
Akonadi::Collection mDefaultAddressBook;
QWidget *mParentWidget;
QProgressDialog *mImportProgressDialog;
void slotImportJobDone(KJob *);
};
#endif // IMPORTEXPORTENGINE_H
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment