25 #include "pimcommon/widgets/renamefiledialog.h"
27 #include <kaddressbookgrantlee/widget/grantleecontactviewer.h>
30 #include <gpgme++/context.h>
31 #include <gpgme++/data.h>
32 #include <gpgme++/key.h>
33 #include <qgpgme/dataprovider.h>
34 #endif // QGPGME_FOUND
36 #include <KABC/VCardConverter>
40 #include <KFileDialog>
41 #include <KLocalizedString>
42 #include <KMessageBox>
43 #include <KPushButton>
44 #include <KTemporaryFile>
46 #include <KStandardGuiItem>
47 #include <KIO/NetAccess>
48 #include <KSharedConfig>
50 #include <QtCore/QFile>
51 #include <QtCore/QPointer>
61 KABC::VCardConverter converter;
64 const KABC::Addressee::List list = filterContacts( contacts.
addressList(), exportFields );
65 if ( list.isEmpty() ) {
70 if ( list.count() == 1 ) {
71 url = KFileDialog::getSaveUrl(
72 QString( list[ 0 ].givenName() +
74 list[ 0 ].familyName() +
76 if ( url.isEmpty() ) {
81 ok = doExport( url, converter.exportVCards( list, KABC::VCardConverter::v2_1 ) );
83 ok = doExport( url, converter.exportVCards( list, KABC::VCardConverter::v3_0 ) );
85 ok = doExport( url, converter.exportVCards( list, KABC::VCardConverter::v4_0 ) );
89 KMessageBox::questionYesNoCancel(
92 "You have selected a list of contacts, "
93 "shall they be exported to several files?" ),
95 KGuiItem( i18nc(
"@action:button",
"Export to One File" ) ),
96 KGuiItem( i18nc(
"@action:button",
"Export to Several Files" ) ) );
101 const KUrl baseUrl = KFileDialog::getExistingDirectoryUrl();
102 if ( baseUrl.isEmpty() ) {
106 for (
int i = 0; i < list.count(); ++i ) {
107 const KABC::Addressee contact = list.at( i );
114 tmpOk = doExport( url, converter.exportVCard( contact, KABC::VCardConverter::v2_1 ) );
116 tmpOk = doExport( url, converter.exportVCard( contact, KABC::VCardConverter::v3_0 ) );
118 tmpOk = doExport( url, converter.exportVCard( contact, KABC::VCardConverter::v4_0 ) );
125 case KMessageBox::Yes:
127 url = KFileDialog::getSaveUrl( KUrl(
QLatin1String(
"addressbook.vcf") ) );
128 if ( url.isEmpty() ) {
133 ok = doExport( url, converter.exportVCards( list, KABC::VCardConverter::v2_1 ) );
135 ok = doExport( url, converter.exportVCards( list, KABC::VCardConverter::v3_0 ) );
137 ok = doExport( url, converter.exportVCards( list, KABC::VCardConverter::v4_0 ) );
141 case KMessageBox::Cancel:
154 KABC::Addressee::List addrList;
163 const QString filter = i18n(
"*.vcf|vCard (*.vcf)\n*|all files (*)" );
165 KFileDialog::getOpenUrls(
169 i18nc(
"@title:window",
"Select vCard to Import" ) );
172 if ( urls.isEmpty() ) {
176 const QString caption( i18nc(
"@title:window",
"vCard Import Failed" ) );
177 bool anyFailures =
false;
179 const int numberOfUrl( urls.count() );
180 for (
int i = 0; i < numberOfUrl; ++i ) {
181 const KUrl url = urls.at( i );
183 if ( KIO::NetAccess::download( url, fileName,
parentWidget() ) ) {
185 QFile file( fileName );
187 if ( file.
open( QIODevice::ReadOnly ) ) {
191 addrList += parseVCard( data );
194 KIO::NetAccess::removeTempFile( fileName );
198 "<para>When trying to read the vCard, "
199 "there was an error opening the file <filename>%1</filename>:</para>"
209 "<para>Unable to access vCard:</para><para>%1</para>",
210 KIO::NetAccess::lastErrorString() );
217 if ( addrList.isEmpty() ) {
218 if ( anyFailures && urls.count() > 1 ) {
219 KMessageBox::information(
221 i18nc(
"@info",
"No contacts were imported, due to errors with the vCards." ) );
222 }
else if ( !anyFailures ) {
223 KMessageBox::information(
225 i18nc(
"@info",
"The vCard does not contain any contacts." ) );
229 if ( dlg->exec() && dlg ) {
230 addrList = dlg->contacts();
242 KABC::Addressee::List VCardXXPort::parseVCard(
const QByteArray &data )
const
244 KABC::VCardConverter converter;
246 return converter.parseVCards( data );
249 bool VCardXXPort::doExport(
const KUrl &url,
const QByteArray &data )
const
252 if ( newUrl.isLocalFile() &&
QFileInfo( newUrl.toLocalFile() ).exists() ) {
253 PimCommon::RenameFileDialog *dialog =
new PimCommon::RenameFileDialog(newUrl,
false,
parentWidget());
254 PimCommon::RenameFileDialog::RenameFileDialogResult result =
static_cast<PimCommon::RenameFileDialog::RenameFileDialogResult
>(dialog->exec());
255 if ( result == PimCommon::RenameFileDialog::RENAMEFILE_RENAME ) {
256 newUrl = dialog->newName();
257 }
else if (result == PimCommon::RenameFileDialog::RENAMEFILE_IGNORE) {
264 KTemporaryFile tmpFile;
267 tmpFile.write( data );
270 return KIO::NetAccess::upload( tmpFile.fileName(), newUrl,
parentWidget() );
273 KABC::Addressee::List VCardXXPort::filterContacts(
const KABC::Addressee::List &addrList, VCardExportSelectionWidget::ExportFields exportFieldType )
const
275 KABC::Addressee::List list;
277 if ( addrList.isEmpty() ) {
281 KABC::Addressee::List::ConstIterator it;
282 KABC::Addressee::List::ConstIterator end( addrList.end() );
283 for ( it = addrList.begin(); it != end; ++it ) {
284 KABC::Addressee addr;
286 addr.setUid( (*it).uid() );
287 addr.setFormattedName( (*it).formattedName() );
289 bool addrDone =
false;
291 QString fmtName = (*it).formattedName();
293 if ( splitNames.
count() >= 2 ) {
295 addr.setGivenName( splitNames.
takeFirst() );
296 addr.setFamilyName( splitNames.
takeLast() );
304 addr.setPrefix( (*it).prefix() );
305 addr.setGivenName( (*it).givenName() );
306 addr.setAdditionalName( (*it).additionalName() );
307 addr.setFamilyName( (*it).familyName() );
308 addr.setSuffix( (*it).suffix() );
311 addr.setNickName( (*it).nickName() );
312 addr.setMailer( (*it).mailer() );
313 addr.setTimeZone( (*it).timeZone() );
314 addr.setGeo( (*it).geo() );
315 addr.setProductId( (*it).productId() );
316 addr.setSortString( (*it).sortString() );
317 addr.setUrl( (*it).url() );
318 addr.setExtraUrlList( (*it).extraUrlList() );
319 addr.setSecrecy( (*it).secrecy() );
320 addr.setSound( (*it).sound() );
321 addr.setEmailList( (*it).emailList() );
322 addr.setCategories( (*it).categories() );
323 addr.setExtraSoundList( (*it).extraSoundList() );
324 addr.setGender( (*it).gender() );
325 addr.setLangs( (*it).langs() );
326 addr.setKind( (*it).kind() );
327 addr.setMembers( (*it).members() );
328 addr.setRelationShips( (*it).relationShips() );
329 addr.setSourcesUrlList( (*it).sourcesUrlList() );
332 addr.setBirthday( (*it).birthday() );
333 addr.setNote( (*it).note() );
337 if ( exportFieldType & VCardExportSelectionWidget::Private ) {
338 addr.setPhoto( (*it).photo() );
339 addr.setExtraPhotoList( (*it).extraPhotoList() );
343 addr.setLogo( (*it).logo() );
344 addr.setExtraLogoList( (*it).extraLogoList() );
348 if ( exportFieldType & VCardExportSelectionWidget::Business ) {
349 addr.setTitle( (*it).title() );
350 addr.setRole( (*it).role() );
351 addr.setOrganization( (*it).organization() );
352 addr.setDepartment( (*it).department() );
354 KABC::PhoneNumber::List phones = (*it).phoneNumbers( KABC::PhoneNumber::Work );
355 KABC::PhoneNumber::List::Iterator phoneIt;
356 for ( phoneIt = phones.begin(); phoneIt != phones.end(); ++phoneIt ) {
357 addr.insertPhoneNumber( *phoneIt );
360 KABC::Address::List addresses = (*it).addresses( KABC::Address::Work );
361 KABC::Address::List::Iterator addrIt;
362 for ( addrIt = addresses.begin(); addrIt != addresses.end(); ++addrIt ) {
363 addr.insertAddress( *addrIt );
367 KABC::PhoneNumber::List phones = (*it).phoneNumbers();
368 KABC::PhoneNumber::List::Iterator phoneIt;
369 for ( phoneIt = phones.begin(); phoneIt != phones.end(); ++phoneIt ) {
370 int phoneType = (*phoneIt).type();
372 if ( (phoneType & KABC::PhoneNumber::Home) && (exportFieldType & VCardExportSelectionWidget::Private) ) {
373 addr.insertPhoneNumber( *phoneIt );
374 }
else if ( (phoneType & KABC::PhoneNumber::Work) && (exportFieldType & VCardExportSelectionWidget::Business) ) {
375 addr.insertPhoneNumber( *phoneIt );
377 addr.insertPhoneNumber( *phoneIt );
381 KABC::Address::List addresses = (*it).addresses();
382 KABC::Address::List::Iterator addrIt;
383 for ( addrIt = addresses.begin(); addrIt != addresses.end(); ++addrIt ) {
384 int addressType = (*addrIt).type();
386 if ( (addressType & KABC::Address::Home) && exportFieldType & VCardExportSelectionWidget::Private ) {
387 addr.insertAddress( *addrIt );
388 }
else if ( (addressType & KABC::Address::Work) && (exportFieldType & VCardExportSelectionWidget::Business) ) {
389 addr.insertAddress( *addrIt );
390 }
else if ( exportFieldType & VCardExportSelectionWidget::Other ) {
391 addr.insertAddress( *addrIt );
395 if ( exportFieldType & VCardExportSelectionWidget::Other ) {
396 addr.setCustoms( (*it).customs() );
400 addKey( addr, KABC::Key::PGP );
401 addKey( addr, KABC::Key::X509 );
410 void VCardXXPort::addKey( KABC::Addressee &addr, KABC::Key::Type type )
const
419 GpgME::Context *context = GpgME::Context::createForProtocol( GpgME::OpenPGP );
421 kError() <<
"No context available";
425 context->setArmor(
false );
426 context->setTextMode(
false );
428 QGpgME::QByteArrayDataProvider dataProvider;
429 GpgME::Data dataObj( &dataProvider );
430 GpgME::Error error = context->exportPublicKeys( fingerprint.
toLatin1(), dataObj );
434 kError() << error.asString();
440 key.setBinaryData( dataProvider.data() );
442 addr.insertKey( key );
bool exportContacts(const ContactList &contacts, VCardExportSelectionWidget::ExportFields exportFieldType) const
Exports the list of contacts.
QString errorString() const
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
QWidget * parentWidget() const
Returns the parent widget that can be used as parent for GUI components.
QString join(const QString &separator) const
VCardXXPort(QWidget *parent=0)
int count(const T &value) const
QString contactFileName(const KABC::Addressee &contact) const
Returns a file name depending on the passed contact.
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
The base class for all import/export modules.
QString option(const QString &key) const
Returns the module specific option value for the given key.
QByteArray toLatin1() const
ContactList importContacts() const
Imports a list of contacts.