summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMontel Laurent <montel@kde.org>2016-10-12 06:50:04 (GMT)
committerMontel Laurent <montel@kde.org>2016-10-12 06:50:04 (GMT)
commit05c26e54964ed984a2d8702db2b420af994d6df1 (patch)
treeb8de875c1133c428a296ea780a69a71ed2cacf56
parent39b3eadbc4ecc47be3b933c77677fbf8b320a286 (diff)
Add export gmx support
-rw-r--r--kaddressbook/importexportplugins/gmx/CMakeLists.txt1
-rw-r--r--kaddressbook/importexportplugins/gmx/gmximportexportplugininterface.cpp351
-rw-r--r--kaddressbook/importexportplugins/gmx/gmximportexportplugininterface.h3
-rw-r--r--kaddressbook/importexportplugins/vcards/engine/vcard_xxport.cpp436
-rw-r--r--kaddressbook/importexportplugins/vcards/engine/vcard_xxport.h42
5 files changed, 355 insertions, 478 deletions
diff --git a/kaddressbook/importexportplugins/gmx/CMakeLists.txt b/kaddressbook/importexportplugins/gmx/CMakeLists.txt
index 4de8fff..c9c61dc 100644
--- a/kaddressbook/importexportplugins/gmx/CMakeLists.txt
+++ b/kaddressbook/importexportplugins/gmx/CMakeLists.txt
@@ -12,5 +12,6 @@ target_link_libraries(kaddressbook_importexportgmxplugin KF5::KaddressbookImport
KF5::I18n
KF5::XmlGui
KF5::Contacts
+ KF5::JobWidgets
kaddressbookimportexportlibprivate
)
diff --git a/kaddressbook/importexportplugins/gmx/gmximportexportplugininterface.cpp b/kaddressbook/importexportplugins/gmx/gmximportexportplugininterface.cpp
index 5a90950..97df329 100644
--- a/kaddressbook/importexportplugins/gmx/gmximportexportplugininterface.cpp
+++ b/kaddressbook/importexportplugins/gmx/gmximportexportplugininterface.cpp
@@ -22,6 +22,20 @@
#include <KActionCollection>
#include <QAction>
#include <QUrl>
+#include <QFileDialog>
+#include <PimCommon/RenameFileDialog>
+#include <KMessageBox>
+#include <QTemporaryFile>
+#include <KJobWidgets>
+#include <QTextStream>
+#include <KAddressBookContactSelectionDialog>
+#include <QPointer>
+#include <KIOCore/kio/filecopyjob.h>
+
+#define GMX_FILESELECTION_STRING i18n( "GMX address book file (*.gmxa)" )
+const int typeHome = 0;
+const int typeWork = 1;
+const int typeOther = 2;
GMXImportExportPluginInterface::GMXImportExportPluginInterface(QObject *parent)
: KAddressBookImportExport::KAddressBookImportExportPluginInterface(parent)
@@ -76,9 +90,346 @@ void GMXImportExportPluginInterface::slotExportGmx()
void GMXImportExportPluginInterface::exportGMX()
{
+ QPointer<KAddressBookImportExport::KAddressBookContactSelectionDialog> dlg =
+ new KAddressBookImportExport::KAddressBookContactSelectionDialog(itemSelectionModel(), false, parentWidget());
+ dlg->setMessageText(i18n("Which contact do you want to export?"));
+ dlg->setDefaultAddressBook(defaultCollection());
+ if (!dlg->exec() || !dlg) {
+ delete dlg;
+ return;
+ }
+ const KContacts::AddresseeList contacts = dlg->selectedContacts().addressList();
+ delete dlg;
+
+ if (contacts.isEmpty()) {
+ KMessageBox::sorry(Q_NULLPTR, i18n("You have not selected any contacts to export."));
+ return;
+ }
+
+ KAddressBookImportExport::KAddressBookImportExportContactList contactLists;
+ contactLists.setAddressList(contacts);
+
+ QUrl url = QFileDialog::getSaveFileUrl(parentWidget(), QString(), QUrl::fromLocalFile(QDir::homePath() + QLatin1String("/addressbook.gmx")), GMX_FILESELECTION_STRING);
+ if (url.isEmpty()) {
+ return;
+ }
+
+ if (QFileInfo(url.isLocalFile() ?
+ url.toLocalFile() : url.path()).exists()) {
+ if (url.isLocalFile() && QFileInfo(url.toLocalFile()).exists()) {
+ PimCommon::RenameFileDialog::RenameFileDialogResult result = PimCommon::RenameFileDialog::RENAMEFILE_IGNORE;
+ PimCommon::RenameFileDialog *dialog = new PimCommon::RenameFileDialog(url, false, parentWidget());
+ result = static_cast<PimCommon::RenameFileDialog::RenameFileDialogResult>(dialog->exec());
+ if (result == PimCommon::RenameFileDialog::RENAMEFILE_RENAME) {
+ url = dialog->newName();
+ } else if (result == PimCommon::RenameFileDialog::RENAMEFILE_IGNORE) {
+ delete dialog;
+ return;
+ }
+ delete dialog;
+ }
+ }
+
+ if (!url.isLocalFile()) {
+ QTemporaryFile tmpFile;
+ if (!tmpFile.open()) {
+ const QString txt = i18n("<qt>Unable to open file <b>%1</b></qt>", url.url());
+ KMessageBox::error(parentWidget(), txt);
+ return;
+ }
+
+ doExport(&tmpFile, contactLists.addressList());
+ tmpFile.flush();
+ auto job = KIO::file_copy(QUrl::fromLocalFile(tmpFile.fileName()), url, -1, KIO::Overwrite);
+ KJobWidgets::setWindow(job, parentWidget());
+ job->exec();
+ } else {
+ QString fileName = url.toLocalFile();
+ QFile file(fileName);
+
+ if (!file.open(QIODevice::WriteOnly)) {
+ const QString txt = i18n("<qt>Unable to open file <b>%1</b>.</qt>", fileName);
+ KMessageBox::error(parentWidget(), txt);
+ return;
+ }
+ doExport(&file, contactLists.addressList());
+ file.close();
+ }
}
+static const QString dateString(const QDateTime &dt)
+{
+ if (!dt.isValid()) {
+ return QStringLiteral("1000-01-01 00:00:00");
+ }
+ QString d(dt.toString(Qt::ISODate));
+ d[10] = ' '; // remove the "T" in the middle of the string
+ return d;
+}
+
+static const QStringList assignedCategoriesSorted(const KContacts::AddresseeList &list)
+{
+ // Walk through the addressees and return a unique list of up to 31
+ // categories, alphabetically sorted
+ QStringList categoryList;
+ const KContacts::Addressee *addressee;
+ for (KContacts::AddresseeList::ConstIterator addresseeIt = list.begin();
+ addresseeIt != list.end() && categoryList.count() < 32; ++addresseeIt) {
+ addressee = &(*addresseeIt);
+ if (addressee->isEmpty()) {
+ continue;
+ }
+ const QStringList categories = addressee->categories();
+ for (int i = 0; i < categories.count() && categoryList.count() < 32; ++i) {
+ if (!categoryList.contains(categories[i])) {
+ categoryList.append(categories[i]);
+ }
+ }
+ }
+ categoryList.sort();
+ return categoryList;
+}
+
+void GMXImportExportPluginInterface::doExport(QFile *fp, const KContacts::AddresseeList &list) const
+{
+ if (!fp || !list.count()) {
+ return;
+ }
+
+ QTextStream t(fp);
+ t.setCodec("ISO 8859-1");
+
+ typedef QMap<int, const KContacts::Addressee *> AddresseeMap;
+ AddresseeMap addresseeMap;
+ const KContacts::Addressee *addressee;
+
+ t << "AB_ADDRESSES:\n";
+ t << "Address_id,Nickname,Firstname,Lastname,Title,Birthday,Comments,"
+ "Change_date,Status,Address_link_id,Categories\n";
+
+ QList<QString> categoryMap;
+ categoryMap.append(assignedCategoriesSorted(list));
+
+ int addresseeId = 0;
+ const QChar DELIM(QLatin1Char('#'));
+ for (KContacts::AddresseeList::ConstIterator it = list.begin();
+ it != list.end(); ++it) {
+ addressee = &(*it);
+ if (addressee->isEmpty()) {
+ continue;
+ }
+ addresseeMap[ ++addresseeId ] = addressee;
+
+ // Assign categories as bitfield
+ const QStringList categories = addressee->categories();
+ long int category = 0;
+ if (categories.count() > 0) {
+ for (int i = 0; i < categories.count(); ++i) {
+ if (categoryMap.contains(categories[i])) {
+ category |= 1 << categoryMap.indexOf(categories[i], 0);
+ }
+ }
+ }
+
+ // GMX sorts by nickname by default - don't leave empty
+ QString nickName = addressee->nickName();
+ if (nickName.isEmpty()) {
+ nickName = addressee->formattedName();
+ }
+
+ t << addresseeId << DELIM // Address_id
+ << nickName << DELIM // Nickname
+ << addressee->givenName() << DELIM // Firstname
+ << addressee->familyName() << DELIM // Lastname
+ << addressee->prefix() << DELIM // Title - Note: ->title()
+ // refers to the professional title
+ << dateString(addressee->birthday()) << DELIM // Birthday
+ << addressee->note() /*.replace('\n',"\r\n")*/ << DELIM // Comments
+ << dateString(addressee->revision()) << DELIM // Change_date
+ << "1" << DELIM // Status
+ << DELIM // Address_link_id
+ << category << endl; // Categories
+ }
+
+ t << "####\n";
+ t << "AB_ADDRESS_RECORDS:\n";
+ t << "Address_id,Record_id,Street,Country,Zipcode,City,Phone,Fax,Mobile,"
+ "Mobile_type,Email,Homepage,Position,Comments,Record_type_id,Record_type,"
+ "Company,Department,Change_date,Preferred,Status\n";
+
+ addresseeId = 1;
+ while ((addressee = addresseeMap[ addresseeId ]) != Q_NULLPTR) {
+
+ const KContacts::PhoneNumber::List cellPhones =
+ addressee->phoneNumbers(KContacts::PhoneNumber::Cell);
+
+ const QStringList emails = addressee->emails();
+
+ for (int recId = 0; recId < 3; ++recId) {
+ KContacts::Address address;
+ KContacts::PhoneNumber phone, fax, cell;
+
+ // address preference flag:
+ // & 1: preferred email address
+ // & 4: preferred cell phone
+ int prefFlag = 0;
+
+ switch (recId) {
+ // Assign address, phone and cellphone, fax if applicable
+ case typeHome:
+ address = addressee->address(KContacts::Address::Home);
+ phone = addressee->phoneNumber(KContacts::PhoneNumber::Home);
+ if (cellPhones.count() > 0) {
+ cell = cellPhones.at(0);
+ if (!cell.isEmpty()) {
+ prefFlag |= 4;
+ }
+ }
+ break;
+ case typeWork:
+ address = addressee->address(KContacts::Address::Work);
+ phone = addressee->phoneNumber(KContacts::PhoneNumber::Work);
+ if (cellPhones.count() >= 2) {
+ cell = cellPhones.at(1);
+ }
+ fax = addressee->phoneNumber(KContacts::PhoneNumber::Fax);
+ break;
+ case typeOther:
+ default:
+ if (addressee->addresses(KContacts::Address::Home).count() > 1) {
+ address = addressee->addresses(KContacts::Address::Home).at(1);
+ }
+ if ((address.isEmpty()) &&
+ (addressee->addresses(KContacts::Address::Work).count() > 1)) {
+ address = addressee->addresses(KContacts::Address::Work).at(1);
+ }
+ if (address.isEmpty()) {
+ address = addressee->address(KContacts::Address::Dom);
+ }
+ if (address.isEmpty()) {
+ address = addressee->address(KContacts::Address::Intl);
+ }
+ if (address.isEmpty()) {
+ address = addressee->address(KContacts::Address::Postal);
+ }
+ if (address.isEmpty()) {
+ address = addressee->address(KContacts::Address::Parcel);
+ }
+
+ if (addressee->phoneNumbers(KContacts::PhoneNumber::Home).count() > 1) {
+ phone = addressee->phoneNumbers(KContacts::PhoneNumber::Home).at(1);
+ }
+ if ((phone.isEmpty()) && (addressee->phoneNumbers(
+ KContacts::PhoneNumber::Work).count() > 1)) {
+ phone = addressee->phoneNumbers(KContacts::PhoneNumber::Work).at(1);
+ }
+ if (phone.isEmpty()) {
+ phone = addressee->phoneNumber(KContacts::PhoneNumber::Voice);
+ }
+ if (phone.isEmpty()) {
+ phone = addressee->phoneNumber(KContacts::PhoneNumber::Msg);
+ }
+ if (phone.isEmpty()) {
+ phone = addressee->phoneNumber(KContacts::PhoneNumber::Isdn);
+ }
+ if (phone.isEmpty()) {
+ phone = addressee->phoneNumber(KContacts::PhoneNumber::Car);
+ }
+ if (phone.isEmpty()) {
+ phone = addressee->phoneNumber(KContacts::PhoneNumber::Pager);
+ }
+
+ switch (cellPhones.count()) {
+ case 0:
+ break;
+ case 1:
+ case 2:
+ if (!address.isEmpty()) {
+ cell = cellPhones.at(0);
+ }
+ break;
+ default:
+ cell = cellPhones.at(2);
+ break;
+ }
+ break;
+ }
+
+ QString email;
+ if (emails.count() > recId) {
+ email = emails[ recId ];
+ if (email == addressee->preferredEmail()) {
+ prefFlag |= 1;
+ }
+ }
+
+ if (!address.isEmpty() || !phone.isEmpty() ||
+ !cell.isEmpty() || !email.isEmpty()) {
+ t << addresseeId << DELIM // Address_id
+ << recId << DELIM // Record_id
+ << address.street() << DELIM // Street
+ << address.country() << DELIM // Country
+ << address.postalCode() << DELIM // Zipcode
+ << address.locality() << DELIM // City
+ << phone.number() << DELIM // Phone
+ << fax.number() << DELIM // Fax
+ << cell.number() << DELIM // Mobile
+
+ << ((recId == typeWork) ? 0 : 1) << DELIM // Mobile_type
+
+ << email << DELIM // Email
+
+ << ((recId == typeWork) ?
+ addressee->url().url().url() :
+ QString()) << DELIM // Homepage
+
+ << ((recId == typeWork) ?
+ addressee->role() :
+ QString()) << DELIM // Position
+
+ << ((recId == typeHome) ?
+ addressee->custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-SpousesName")) :
+ QString()) << DELIM // Comments
+
+ << recId << DELIM // Record_type_id (0,1,2)
+
+ << DELIM // Record_type
+
+ << ((recId == typeWork) ?
+ addressee->organization() :
+ QString()) << DELIM // Company
+
+ << ((recId == typeWork) ?
+ addressee->custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-Department")) :
+ QString()) << DELIM // Department
+
+ << dateString(addressee->revision()) << DELIM // Change_date
+
+ << prefFlag << DELIM // Preferred:
+ // ( & 1: preferred email,
+ // & 4: preferred cell phone )
+ << 1 << endl; // Status (should always be "1")
+ }
+ }
+
+ ++addresseeId;
+ };
+
+ t << "####" << endl;
+ t << "AB_CATEGORIES:" << endl;
+ t << "Category_id,Name,Icon_id" << endl;
+
+ // Write Category List (beware: Category_ID 0 is reserved for none
+ // Interestingly: The index here is an int sequence and does not
+ // correspond to the bit reference used above.
+ for (int i = 0; i < categoryMap.size(); ++i) {
+ t << (i + 1) << DELIM << categoryMap.at(i) << DELIM << 0 << endl;
+ }
+ t << "####" << endl;
+}
+
+
void GMXImportExportPluginInterface::importGMX()
{
diff --git a/kaddressbook/importexportplugins/gmx/gmximportexportplugininterface.h b/kaddressbook/importexportplugins/gmx/gmximportexportplugininterface.h
index d6b9cce..93017e2 100644
--- a/kaddressbook/importexportplugins/gmx/gmximportexportplugininterface.h
+++ b/kaddressbook/importexportplugins/gmx/gmximportexportplugininterface.h
@@ -21,6 +21,8 @@
#define GMXIMPORTEXPORTPLUGININTERFACE_H
#include <KAddressBookImportExport/KAddressBookImportExportPluginInterface>
+#include <QFile>
+#include <KContacts/Addressee>
class GMXImportExportPluginInterface : public KAddressBookImportExport::KAddressBookImportExportPluginInterface
{
@@ -33,6 +35,7 @@ public:
void exec() Q_DECL_OVERRIDE;
bool canImportFileType(const QUrl &url) Q_DECL_OVERRIDE;
private:
+ void doExport(QFile *fp, const KContacts::AddresseeList &list) const;
void slotImportGmx();
void slotExportGmx();
void importGMX();
diff --git a/kaddressbook/importexportplugins/vcards/engine/vcard_xxport.cpp b/kaddressbook/importexportplugins/vcards/engine/vcard_xxport.cpp
deleted file mode 100644
index 0034354..0000000
--- a/kaddressbook/importexportplugins/vcards/engine/vcard_xxport.cpp
+++ /dev/null
@@ -1,436 +0,0 @@
-/*
- This file is part of KAddressBook.
- Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License along
- with this program; if not, write to the Free Software Foundation, Inc.,
- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-*/
-
-#include "vcard_xxport.h"
-
-#include "vcardviewerdialog.h"
-#include "vcardexportselectionwidget.h"
-
-#include "PimCommon/RenameFileDialog"
-
-#include <KaddressbookGrantlee/GrantleeContactViewer>
-
-#ifdef QGPGME_FOUND
-#include <gpgme++/context.h>
-#include <gpgme++/data.h>
-#include <gpgme++/key.h>
-#include <qgpgme/dataprovider.h>
-#endif // QGPGME_FOUND
-
-#include <KContacts/VCardConverter>
-
-#include "kaddressbook_debug.h"
-#include <QDialog>
-#include <QFileDialog>
-#include <KLocalizedString>
-#include <KMessageBox>
-#include <QTemporaryFile>
-#include <QUrl>
-#include <KStandardGuiItem>
-#include <KSharedConfig>
-
-#include <KJobWidgets>
-#include <KIO/StatJob>
-#include <KIO/FileCopyJob>
-#include <KIO/StoredTransferJob>
-
-#include <QFile>
-#include <QPointer>
-
-VCardXXPort::VCardXXPort(QWidget *parent)
- : XXPort(parent)
-{
-}
-
-bool VCardXXPort::exportContacts(const ContactList &contacts, VCardExportSelectionWidget::ExportFields exportFields) const
-{
- KContacts::VCardConverter converter;
- QUrl url;
-
- const KContacts::Addressee::List list = filterContacts(contacts.addressList(), exportFields);
- if (list.isEmpty()) { // no contact selected
- return true;
- }
-
- 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 true;
- }
-
- if (option(QStringLiteral("version")) == QLatin1String("v21")) {
- ok = doExport(url, converter.exportVCards(list, KContacts::VCardConverter::v2_1));
- } else if (option(QStringLiteral("version")) == QLatin1String("v30")) {
- ok = doExport(url, converter.exportVCards(list, KContacts::VCardConverter::v3_0));
- } else {
- ok = doExport(url, converter.exportVCards(list, KContacts::VCardConverter::v4_0));
- }
- } 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 true; // 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;
-
- if (option(QStringLiteral("version")) == QLatin1String("v21")) {
- tmpOk = doExport(url, converter.exportVCard(contact, KContacts::VCardConverter::v2_1));
- } else if (option(QStringLiteral("version")) == QLatin1String("v30")) {
- tmpOk = doExport(url, converter.exportVCard(contact, KContacts::VCardConverter::v3_0));
- } else {
- tmpOk = doExport(url, converter.exportVCard(contact, KContacts::VCardConverter::v4_0));
- }
-
- ok = ok && tmpOk;
- }
- break;
- }
- case KMessageBox::Yes: {
- url = QFileDialog::getSaveFileUrl(parentWidget(), QString(), QUrl::fromLocalFile(QStringLiteral("addressbook.vcf")));
- if (url.isEmpty()) {
- return true; // user canceled export
- }
-
- if (option(QStringLiteral("version")) == QLatin1String("v21")) {
- ok = doExport(url, converter.exportVCards(list, KContacts::VCardConverter::v2_1));
- } else if (option(QStringLiteral("version")) == QLatin1String("v30")) {
- ok = doExport(url, converter.exportVCards(list, KContacts::VCardConverter::v3_0));
- } else {
- ok = doExport(url, converter.exportVCards(list, KContacts::VCardConverter::v4_0));
- }
- break;
- }
- case KMessageBox::Cancel:
- default:
- return true; // user canceled export
- }
- }
-
- return ok;
-}
-
-ContactList VCardXXPort::importContacts() const
-{
- ContactList contactList;
- KContacts::Addressee::List addrList;
- QList<QUrl> urls;
-
- if (!option(QStringLiteral("importData")).isEmpty()) {
- addrList = parseVCard(option(QStringLiteral("importData")).toUtf8());
- } else {
- if (!option(QStringLiteral("importUrl")).isEmpty()) {
- urls.append(QUrl::fromLocalFile(option(QStringLiteral("importUrl"))));
- } else {
- const QString filter = i18n("*.vcf|vCard (*.vcf)\n*|all files (*)");
- urls =
- QFileDialog::getOpenFileUrls(parentWidget(), i18nc("@title:window", "Select vCard to Import"),
- QUrl(),
- filter);
- }
-
- if (urls.isEmpty()) {
- return contactList;
- }
-
- const QString caption(i18nc("@title:window", "vCard Import Failed"));
- bool anyFailures = false;
-
- const int numberOfUrl(urls.count());
- for (int i = 0; i < numberOfUrl; ++i) {
- const QUrl url = urls.at(i);
-
- auto job = KIO::storedGet(url);
- KJobWidgets::setWindow(job, parentWidget());
- if (job->exec()) {
-
- const QByteArray data = job->data();
- if (!data.isEmpty()) {
- addrList += parseVCard(data);
- }
- } else {
- const QString msg = xi18nc(
- "@info",
- "<para>Unable to access vCard:</para><para>%1</para>",
- job->errorString());
- KMessageBox::error(parentWidget(), msg, caption);
- anyFailures = true;
- }
- }
-
- if (!option(QStringLiteral("importUrl")).isEmpty()) { // a vcard was passed via cmd
- if (addrList.isEmpty()) {
- if (anyFailures && urls.count() > 1) {
- KMessageBox::information(
- parentWidget(),
- i18nc("@info", "No contacts were imported, due to errors with the vCards."));
- } else if (!anyFailures) {
- KMessageBox::information(
- parentWidget(),
- i18nc("@info", "The vCard does not contain any contacts."));
- }
- } else {
- QPointer<VCardViewerDialog> dlg = new VCardViewerDialog(addrList, parentWidget());
- if (dlg->exec() && dlg) {
- addrList = dlg->contacts();
- } else {
- addrList.clear();
- }
- delete dlg;
- }
- }
- }
- contactList.setAddressList(addrList);
- return contactList;
-}
-
-KContacts::Addressee::List VCardXXPort::parseVCard(const QByteArray &data) const
-{
- KContacts::VCardConverter converter;
-
- return converter.parseVCards(data);
-}
-
-bool VCardXXPort::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();
-}
-
-KContacts::Addressee::List VCardXXPort::filterContacts(const KContacts::Addressee::List &addrList, VCardExportSelectionWidget::ExportFields exportFieldType) const
-{
- KContacts::Addressee::List list;
-
- if (addrList.isEmpty()) {
- return addrList;
- }
-
- KContacts::Addressee::List::ConstIterator it;
- KContacts::Addressee::List::ConstIterator end(addrList.end());
- for (it = addrList.begin(); it != end; ++it) {
- KContacts::Addressee addr;
-
- addr.setUid((*it).uid());
- addr.setFormattedName((*it).formattedName());
-
- bool addrDone = false;
- if (exportFieldType & VCardExportSelectionWidget::DiplayName) { // output display name as N field
- QString fmtName = (*it).formattedName();
- QStringList splitNames = fmtName.split(QLatin1Char(' '), QString::SkipEmptyParts);
- if (splitNames.count() >= 2) {
- addr.setPrefix(QString());
- addr.setGivenName(splitNames.takeFirst());
- addr.setFamilyName(splitNames.takeLast());
- addr.setAdditionalName(splitNames.join(QLatin1Char(' ')));
- addr.setSuffix(QString());
- addrDone = true;
- }
- }
-
- if (!addrDone) { // not wanted, or could not be split
- addr.setPrefix((*it).prefix());
- addr.setGivenName((*it).givenName());
- addr.setAdditionalName((*it).additionalName());
- addr.setFamilyName((*it).familyName());
- addr.setSuffix((*it).suffix());
- }
-
- addr.setExtraNickNameList((*it).extraNickNameList());
- addr.setMailer((*it).mailer());
- addr.setTimeZone((*it).timeZone());
- addr.setGeo((*it).geo());
- addr.setProductId((*it).productId());
- addr.setSortString((*it).sortString());
- addr.setUrl((*it).url());
- addr.setExtraUrlList((*it).extraUrlList());
- addr.setSecrecy((*it).secrecy());
- addr.setSound((*it).sound());
- addr.setEmailList((*it).emailList());
- addr.setCategories((*it).categories());
- addr.setExtraSoundList((*it).extraSoundList());
- addr.setGender((*it).gender());
- addr.setLangs((*it).langs());
- addr.setKind((*it).kind());
- addr.setMembers((*it).members());
- addr.setRelationShips((*it).relationShips());
- addr.setSourcesUrlList((*it).sourcesUrlList());
- addr.setImppList((*it).imppList());
- addr.setFieldGroupList((*it).fieldGroupList());
-
- if (exportFieldType & VCardExportSelectionWidget::Private) {
- addr.setBirthday((*it).birthday(), (*it).birthdayHasTime());
- addr.setNote((*it).note());
- }
-
- if (exportFieldType & VCardExportSelectionWidget::Picture) {
- if (exportFieldType & VCardExportSelectionWidget::Private) {
- addr.setPhoto((*it).photo());
- addr.setExtraPhotoList((*it).extraPhotoList());
- }
-
- if (exportFieldType & VCardExportSelectionWidget::Business) {
- addr.setLogo((*it).logo());
- addr.setExtraLogoList((*it).extraLogoList());
- }
- }
-
- if (exportFieldType & VCardExportSelectionWidget::Business) {
- addr.setExtraTitleList((*it).extraTitleList());
- addr.setExtraRoleList((*it).extraRoleList());
- addr.setExtraOrganizationList((*it).extraOrganizationList());
- addr.setDepartment((*it).department());
-
- KContacts::PhoneNumber::List phones = (*it).phoneNumbers(KContacts::PhoneNumber::Work);
- KContacts::PhoneNumber::List::Iterator phoneIt;
- KContacts::PhoneNumber::List::Iterator phoneEnd(phones.end());
- for (phoneIt = phones.begin(); phoneIt != phoneEnd; ++phoneIt) {
- addr.insertPhoneNumber(*phoneIt);
- }
-
- KContacts::Address::List addresses = (*it).addresses(KContacts::Address::Work);
- KContacts::Address::List::Iterator addrIt;
- KContacts::Address::List::Iterator addrEnd(addresses.end());
- for (addrIt = addresses.begin(); addrIt != addrEnd; ++addrIt) {
- addr.insertAddress(*addrIt);
- }
- }
-
- KContacts::PhoneNumber::List phones = (*it).phoneNumbers();
- KContacts::PhoneNumber::List::Iterator phoneIt;
- KContacts::PhoneNumber::List::Iterator phoneEnd(phones.end());
- for (phoneIt = phones.begin(); phoneIt != phoneEnd; ++phoneIt) {
- int phoneType = (*phoneIt).type();
-
- if ((phoneType & KContacts::PhoneNumber::Home) && (exportFieldType & VCardExportSelectionWidget::Private)) {
- addr.insertPhoneNumber(*phoneIt);
- } else if ((phoneType & KContacts::PhoneNumber::Work) && (exportFieldType & VCardExportSelectionWidget::Business)) {
- addr.insertPhoneNumber(*phoneIt);
- } else if ((exportFieldType & VCardExportSelectionWidget::Other)) {
- addr.insertPhoneNumber(*phoneIt);
- }
- }
-
- KContacts::Address::List addresses = (*it).addresses();
- KContacts::Address::List::Iterator addrIt;
- KContacts::Address::List::Iterator addrEnd(addresses.end());
- for (addrIt = addresses.begin(); addrIt != addrEnd; ++addrIt) {
- int addressType = (*addrIt).type();
-
- if ((addressType & KContacts::Address::Home) && exportFieldType & VCardExportSelectionWidget::Private) {
- addr.insertAddress(*addrIt);
- } else if ((addressType & KContacts::Address::Work) && (exportFieldType & VCardExportSelectionWidget::Business)) {
- addr.insertAddress(*addrIt);
- } else if (exportFieldType & VCardExportSelectionWidget::Other) {
- addr.insertAddress(*addrIt);
- }
- }
-
- if (exportFieldType & VCardExportSelectionWidget::Other) {
- addr.setCustoms((*it).customs());
- }
-
- if (exportFieldType & VCardExportSelectionWidget::Encryption) {
- addKey(addr, KContacts::Key::PGP);
- addKey(addr, KContacts::Key::X509);
- }
-
- list.append(addr);
- }
-
- return list;
-}
-
-void VCardXXPort::addKey(KContacts::Addressee &addr, KContacts::Key::Type type) const
-{
-#ifdef QGPGME_FOUND
- const QString fingerprint = addr.custom(QStringLiteral("KADDRESSBOOK"),
- (type == KContacts::Key::PGP ? QStringLiteral("OPENPGPFP") : QStringLiteral("SMIMEFP")));
- if (fingerprint.isEmpty()) {
- return;
- }
-
- GpgME::Context *context = GpgME::Context::createForProtocol(GpgME::OpenPGP);
- if (!context) {
- qCritical() << "No context available";
- return;
- }
-
- context->setArmor(false);
- context->setTextMode(false);
-
- QGpgME::QByteArrayDataProvider dataProvider;
- GpgME::Data dataObj(&dataProvider);
- GpgME::Error error = context->exportPublicKeys(fingerprint.toLatin1(), dataObj);
- delete context;
-
- if (error) {
- qCritical() << error.asString();
- return;
- }
-
- KContacts::Key key;
- key.setType(type);
- key.setBinaryData(dataProvider.data());
-
- addr.insertKey(key);
-#else
- return;
-#endif
-}
-
diff --git a/kaddressbook/importexportplugins/vcards/engine/vcard_xxport.h b/kaddressbook/importexportplugins/vcards/engine/vcard_xxport.h
deleted file mode 100644
index 514867c..0000000
--- a/kaddressbook/importexportplugins/vcards/engine/vcard_xxport.h
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- This file is part of KAddressBook.
- Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License along
- with this program; if not, write to the Free Software Foundation, Inc.,
- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-*/
-
-#ifndef VCARD_XXPORT_H
-#define VCARD_XXPORT_H
-
-#include "xxport.h"
-
-class VCardXXPort : public XXPort
-{
-public:
- explicit VCardXXPort(QWidget *parent = Q_NULLPTR);
-
- bool exportContacts(const ContactList &contacts, VCardExportSelectionWidget::ExportFields exportFieldType) const Q_DECL_OVERRIDE;
- ContactList importContacts() const Q_DECL_OVERRIDE;
-
-private:
- KContacts::Addressee::List parseVCard(const QByteArray &data) const;
- bool doExport(const QUrl &url, const QByteArray &data) const;
-
- void addKey(KContacts::Addressee &addr, KContacts::Key::Type type) const;
-
- KContacts::Addressee::List filterContacts(const KContacts::Addressee::List &, VCardExportSelectionWidget::ExportFields exportFieldType) const;
-};
-
-#endif