summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMontel Laurent <montel@kde.org>2016-10-12 05:55:47 (GMT)
committerMontel Laurent <montel@kde.org>2016-10-12 05:55:47 (GMT)
commit5cda489eb9e3091fe73eb5656accf9353dae554e (patch)
treeda53e64adf2855cb5f9de3d99de37bda10c177dd
parent78a8c9b77309139160bad724256ad077a19bd212 (diff)
Implement export
-rw-r--r--kaddressbook/importexportplugins/vcards/CMakeLists.txt1
-rw-r--r--kaddressbook/importexportplugins/vcards/vcardimportexportplugininterface.cpp150
-rw-r--r--kaddressbook/importexportplugins/vcards/vcardimportexportplugininterface.h2
3 files changed, 153 insertions, 0 deletions
diff --git a/kaddressbook/importexportplugins/vcards/CMakeLists.txt b/kaddressbook/importexportplugins/vcards/CMakeLists.txt
index f9e5c1c..e204d96 100644
--- a/kaddressbook/importexportplugins/vcards/CMakeLists.txt
+++ b/kaddressbook/importexportplugins/vcards/CMakeLists.txt
@@ -16,6 +16,7 @@ target_link_libraries(kaddressbook_importexportvcardplugin
KF5::XmlGui
KF5::Contacts
KF5::KaddressbookGrantlee
+ KF5::JobWidgets
kaddressbookimportexportlibprivate
)
if (WIN32)
diff --git a/kaddressbook/importexportplugins/vcards/vcardimportexportplugininterface.cpp b/kaddressbook/importexportplugins/vcards/vcardimportexportplugininterface.cpp
index 4e540f3..0951e22 100644
--- a/kaddressbook/importexportplugins/vcards/vcardimportexportplugininterface.cpp
+++ b/kaddressbook/importexportplugins/vcards/vcardimportexportplugininterface.cpp
@@ -25,9 +25,14 @@
#include <QPointer>
#include <KMessageBox>
#include <QUrl>
+#include <QFileDialog>
#include <KContacts/VCardConverter>
+#include <PimCommon/RenameFileDialog>
+#include <KIOCore/kio/filecopyjob.h>
+#include <KJobWidgets>
#ifdef QGPGME_FOUND
+#include <QTemporaryFile>
#include <gpgme++/context.h>
#include <gpgme++/data.h>
#include <gpgme++/key.h>
@@ -378,6 +383,31 @@ void VCardImportExportPluginInterface::addKey(KContacts::Addressee &addr, KConta
#endif
}
+bool VCardImportExportPluginInterface::doExport(const QUrl &url, const QByteArray &data) const
+{
+ QUrl newUrl(url);
+ if (newUrl.isLocalFile() && QFileInfo(newUrl.toLocalFile()).exists()) {
+ PimCommon::RenameFileDialog *dialog = new PimCommon::RenameFileDialog(newUrl, false, parentWidget());
+ PimCommon::RenameFileDialog::RenameFileDialogResult result = static_cast<PimCommon::RenameFileDialog::RenameFileDialogResult>(dialog->exec());
+ if (result == PimCommon::RenameFileDialog::RENAMEFILE_RENAME) {
+ newUrl = dialog->newName();
+ } else if (result == PimCommon::RenameFileDialog::RENAMEFILE_IGNORE) {
+ delete dialog;
+ return true;
+ }
+ delete dialog;
+ }
+
+ QTemporaryFile tmpFile;
+ tmpFile.open();
+
+ tmpFile.write(data);
+ tmpFile.flush();
+ auto job = KIO::file_copy(QUrl::fromLocalFile(tmpFile.fileName()), newUrl, -1, KIO::Overwrite);
+ KJobWidgets::setWindow(job, parentWidget());
+ return job->exec();
+}
+
void VCardImportExportPluginInterface::exportVCard()
{
@@ -398,6 +428,105 @@ void VCardImportExportPluginInterface::exportVCard()
KMessageBox::sorry(Q_NULLPTR, i18n("You have not selected any contacts to export."));
return;
}
+
+
+ KContacts::VCardConverter converter;
+ QUrl url;
+
+ const KContacts::Addressee::List list = filterContacts(contacts, exportFields);
+ if (list.isEmpty()) { // no contact selected
+ return;
+ }
+
+ bool ok = true;
+ if (list.count() == 1) {
+ url = QFileDialog::getSaveFileUrl(parentWidget(), QString(), QUrl::fromLocalFile(
+ QString(list[ 0 ].givenName() +
+ QLatin1Char(QLatin1Char('_')) +
+ list[ 0 ].familyName() +
+ QLatin1String(".vcf"))));
+ if (url.isEmpty()) { // user canceled export
+ return;
+ }
+
+ switch(mExportVCardType)
+ {
+ case VCard2_1:
+ ok = doExport(url, converter.exportVCards(list, KContacts::VCardConverter::v2_1));
+ break;
+ case VCard3:
+ ok = doExport(url, converter.exportVCards(list, KContacts::VCardConverter::v3_0));
+ break;
+ case VCard4:
+ ok = doExport(url, converter.exportVCards(list, KContacts::VCardConverter::v4_0));
+ break;
+ }
+ } else {
+ const int answer =
+ KMessageBox::questionYesNoCancel(
+ parentWidget(),
+ i18nc("@info",
+ "You have selected a list of contacts, "
+ "shall they be exported to several files?"),
+ QString(),
+ KGuiItem(i18nc("@action:button", "Export to One File")),
+ KGuiItem(i18nc("@action:button", "Export to Several Files")));
+
+ switch (answer) {
+ case KMessageBox::No: {
+ const QUrl baseUrl = QFileDialog::getExistingDirectoryUrl();
+ if (baseUrl.isEmpty()) {
+ return; // user canceled export
+ }
+
+ for (int i = 0; i < list.count(); ++i) {
+ const KContacts::Addressee contact = list.at(i);
+
+ url = QUrl::fromLocalFile(baseUrl.path() + QLatin1Char('/') + contactFileName(contact) + QLatin1String(".vcf"));
+
+ bool tmpOk = false;
+
+ switch(mExportVCardType)
+ {
+ case VCard2_1:
+ tmpOk = doExport(url, converter.exportVCards(list, KContacts::VCardConverter::v2_1));
+ break;
+ case VCard3:
+ tmpOk = doExport(url, converter.exportVCards(list, KContacts::VCardConverter::v3_0));
+ break;
+ case VCard4:
+ tmpOk = doExport(url, converter.exportVCards(list, KContacts::VCardConverter::v4_0));
+ break;
+ }
+ ok = ok && tmpOk;
+ }
+ break;
+ }
+ case KMessageBox::Yes: {
+ url = QFileDialog::getSaveFileUrl(parentWidget(), QString(), QUrl::fromLocalFile(QStringLiteral("addressbook.vcf")));
+ if (url.isEmpty()) {
+ return; // user canceled export
+ }
+
+ switch(mExportVCardType)
+ {
+ case VCard2_1:
+ ok = doExport(url, converter.exportVCards(list, KContacts::VCardConverter::v2_1));
+ break;
+ case VCard3:
+ ok = doExport(url, converter.exportVCards(list, KContacts::VCardConverter::v3_0));
+ break;
+ case VCard4:
+ ok = doExport(url, converter.exportVCards(list, KContacts::VCardConverter::v4_0));
+ break;
+ }
+ break;
+ }
+ case KMessageBox::Cancel:
+ default:
+ return; // user canceled export
+ }
+ }
}
@@ -405,3 +534,24 @@ bool VCardImportExportPluginInterface::canImportFileType(const QUrl &url)
{
return url.path().endsWith(QStringLiteral(".vcf"));
}
+
+QString VCardImportExportPluginInterface::contactFileName(const KContacts::Addressee &contact) const
+{
+ if (!contact.givenName().isEmpty() && !contact.familyName().isEmpty()) {
+ return QStringLiteral("%1_%2").arg(contact.givenName()).arg(contact.familyName());
+ }
+
+ if (!contact.familyName().isEmpty()) {
+ return contact.familyName();
+ }
+
+ if (!contact.givenName().isEmpty()) {
+ return contact.givenName();
+ }
+
+ if (!contact.organization().isEmpty()) {
+ return contact.organization();
+ }
+
+ return contact.uid();
+}
diff --git a/kaddressbook/importexportplugins/vcards/vcardimportexportplugininterface.h b/kaddressbook/importexportplugins/vcards/vcardimportexportplugininterface.h
index da17333..014fdac 100644
--- a/kaddressbook/importexportplugins/vcards/vcardimportexportplugininterface.h
+++ b/kaddressbook/importexportplugins/vcards/vcardimportexportplugininterface.h
@@ -48,7 +48,9 @@ private:
void slotImportVCard();
void slotExportVCard4();
void slotExportVCard3();
+ QString contactFileName(const KContacts::Addressee &contact) const;
void slotExportVCard2();
+ bool doExport(const QUrl &url, const QByteArray &data) const;
ExportVCardType mExportVCardType;
};