summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMontel Laurent <montel@kde.org>2016-10-10 07:09:08 (GMT)
committerMontel Laurent <montel@kde.org>2016-10-10 07:09:08 (GMT)
commitc19d88e129718f6acaf1d70efe05ebd2c5970c58 (patch)
treea592ca661feb8c0f9e7b0e3fd613d619885febaa
parent87e31e59a89e356f20c21e26f7acc12b75eaa741 (diff)
Add support for import
-rw-r--r--kaddressbook/importexportplugins/shared/CMakeLists.txt2
-rw-r--r--kaddressbook/importexportplugins/shared/importexportengine.cpp94
-rw-r--r--kaddressbook/importexportplugins/shared/importexportengine.h12
3 files changed, 105 insertions, 3 deletions
diff --git a/kaddressbook/importexportplugins/shared/CMakeLists.txt b/kaddressbook/importexportplugins/shared/CMakeLists.txt
index d34671c..330412a 100644
--- a/kaddressbook/importexportplugins/shared/CMakeLists.txt
+++ b/kaddressbook/importexportplugins/shared/CMakeLists.txt
@@ -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
diff --git a/kaddressbook/importexportplugins/shared/importexportengine.cpp b/kaddressbook/importexportplugins/shared/importexportengine.cpp
index e75f089..373fee6 100644
--- a/kaddressbook/importexportplugins/shared/importexportengine.cpp
+++ b/kaddressbook/importexportplugins/shared/importexportengine.cpp
@@ -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;
+ }
}
+
diff --git a/kaddressbook/importexportplugins/shared/importexportengine.h b/kaddressbook/importexportplugins/shared/importexportengine.h
index bbea166..f3a2d61 100644
--- a/kaddressbook/importexportplugins/shared/importexportengine.h
+++ b/kaddressbook/importexportplugins/shared/importexportengine.h
@@ -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