22 #include "pimcommon/widgets/renamefiledialog.h"
24 #include <kaddressbookgrantlee/widget/grantleecontactviewer.h>
27 #include <gpgme++/context.h>
28 #include <gpgme++/data.h>
29 #include <gpgme++/key.h>
30 #include <qgpgme/dataprovider.h>
31 #endif // QGPGME_FOUND
33 #include <KABC/VCardConverter>
35 #include <KApplication>
38 #include <KFileDialog>
40 #include <KMessageBox>
41 #include <KPushButton>
42 #include <KTemporaryFile>
44 #include <KStandardGuiItem>
45 #include <KIO/NetAccess>
47 #include <QtCore/QFile>
48 #include <QtCore/QPointer>
53 #include <QPushButton>
54 #include <QVBoxLayout>
57 class VCardViewerDialog :
public KDialog
61 VCardViewerDialog(
const KABC::Addressee::List &list,
65 KABC::Addressee::List contacts()
const;
78 KAddressBookGrantlee::GrantleeContactViewer *mView;
80 KABC::Addressee::List mContacts;
81 KABC::Addressee::List::Iterator mIt;
84 class VCardExportSelectionDialog :
public KDialog
87 VCardExportSelectionDialog( QWidget *parent );
88 ~VCardExportSelectionDialog();
90 bool exportPrivateFields()
const;
91 bool exportBusinessFields()
const;
92 bool exportOtherFields()
const;
93 bool exportEncryptionKeys()
const;
94 bool exportPictureFields()
const;
95 bool exportDisplayName()
const;
98 QCheckBox *mPrivateBox;
99 QCheckBox *mBusinessBox;
100 QCheckBox *mOtherBox;
101 QCheckBox *mEncryptionKeys;
102 QCheckBox *mPictureBox;
103 QCheckBox *mDisplayNameBox;
113 KABC::VCardConverter converter;
116 const KABC::Addressee::List list = filterContacts( contacts );
117 if ( list.isEmpty() ) {
122 if ( list.count() == 1 ) {
123 url = KFileDialog::getSaveUrl(
124 QString( list[ 0 ].givenName() +
125 QLatin1Char( QLatin1Char(
'_') ) +
126 list[ 0 ].familyName() +
127 QLatin1String(
".vcf" ) ) );
128 if ( url.isEmpty() ) {
132 if (
option( QLatin1String(
"version") ) == QLatin1String(
"v21") ) {
133 ok = doExport( url, converter.exportVCards( list, KABC::VCardConverter::v2_1 ) );
135 ok = doExport( url, converter.exportVCards( list, KABC::VCardConverter::v3_0 ) );
139 KMessageBox::questionYesNoCancel(
142 "You have selected a list of contacts, "
143 "shall they be exported to several files?" ),
145 KGuiItem( i18nc(
"@action:button",
"Export to One File" ) ),
146 KGuiItem( i18nc(
"@action:button",
"Export to Several Files" ) ) );
149 case KMessageBox::No:
151 const KUrl baseUrl = KFileDialog::getExistingDirectoryUrl();
152 if ( baseUrl.isEmpty() ) {
156 for (
int i = 0; i < list.count(); ++i ) {
157 const KABC::Addressee contact = list.at( i );
159 url = baseUrl.url() + QLatin1Char(
'/') +
contactFileName( contact ) + QLatin1String(
".vcf");
163 if (
option( QLatin1String(
"version") ) == QLatin1String(
"v21") ) {
164 tmpOk = doExport( url, converter.exportVCard( contact, KABC::VCardConverter::v2_1 ) );
166 tmpOk = doExport( url, converter.exportVCard( contact, KABC::VCardConverter::v3_0 ) );
173 case KMessageBox::Yes:
175 url = KFileDialog::getSaveUrl( KUrl( QLatin1String(
"addressbook.vcf") ) );
176 if ( url.isEmpty() ) {
180 if (
option( QLatin1String(
"version") ) == QLatin1String(
"v21") ) {
181 ok = doExport( url, converter.exportVCards( list, KABC::VCardConverter::v2_1 ) );
183 ok = doExport( url, converter.exportVCards( list, KABC::VCardConverter::v3_0 ) );
187 case KMessageBox::Cancel:
199 KABC::Addressee::List addrList;
202 if ( !
option( QLatin1String(
"importData") ).isEmpty() ) {
203 addrList = parseVCard(
option( QLatin1String(
"importData") ).toUtf8() );
205 if ( !
option( QLatin1String(
"importUrl") ).isEmpty() ) {
206 urls.append( KUrl(
option( QLatin1String(
"importUrl") ) ) );
209 KFileDialog::getOpenUrls(
211 QLatin1String(
"*.vcf|vCards"),
213 i18nc(
"@title:window",
"Select vCard to Import" ) );
216 if ( urls.isEmpty() ) {
220 const QString caption( i18nc(
"@title:window",
"vCard Import Failed" ) );
221 bool anyFailures =
false;
223 const int numberOfUrl( urls.count() );
224 for (
int i = 0; i < numberOfUrl; ++i ) {
225 const KUrl url = urls.at( i );
227 if ( KIO::NetAccess::download( url, fileName,
parentWidget() ) ) {
229 QFile file( fileName );
231 if ( file.open( QIODevice::ReadOnly ) ) {
232 const QByteArray data = file.readAll();
234 if ( !data.isEmpty() ) {
235 addrList += parseVCard( data );
238 KIO::NetAccess::removeTempFile( fileName );
240 const QString msg = i18nc(
242 "<para>When trying to read the vCard, "
243 "there was an error opening the file <filename>%1</filename>:</para>"
246 i18nc(
"QFile", file.errorString().toLatin1() ) );
251 const QString msg = i18nc(
253 "<para>Unable to access vCard:</para><para>%1</para>",
254 KIO::NetAccess::lastErrorString() );
260 if ( !
option( QLatin1String(
"importUrl") ).isEmpty() ) {
261 if ( addrList.isEmpty() ) {
262 if ( anyFailures && urls.count() > 1 ) {
263 KMessageBox::information(
265 i18nc(
"@info",
"No contacts were imported, due to errors with the vCards." ) );
266 }
else if ( !anyFailures ) {
267 KMessageBox::information(
269 i18nc(
"@info",
"The vCard does not contain any contacts." ) );
272 QPointer<VCardViewerDialog> dlg =
new VCardViewerDialog( addrList,
parentWidget() );
273 if ( dlg->exec() && dlg ) {
274 addrList = dlg->contacts();
285 KABC::Addressee::List VCardXXPort::parseVCard(
const QByteArray &data )
const
287 KABC::VCardConverter converter;
289 return converter.parseVCards( data );
292 bool VCardXXPort::doExport(
const KUrl &url,
const QByteArray &data )
const
295 if ( newUrl.isLocalFile() && QFileInfo( newUrl.toLocalFile() ).exists() ) {
296 PimCommon::RenameFileDialog *dialog =
new PimCommon::RenameFileDialog(newUrl,
false,
parentWidget());
297 PimCommon::RenameFileDialog::RenameFileDialogResult result =
static_cast<PimCommon::RenameFileDialog::RenameFileDialogResult
>(dialog->exec());
298 if ( result == PimCommon::RenameFileDialog::RENAMEFILE_RENAME ) {
299 newUrl = dialog->newName();
300 }
else if (result == PimCommon::RenameFileDialog::RENAMEFILE_IGNORE) {
307 KTemporaryFile tmpFile;
310 tmpFile.write( data );
313 return KIO::NetAccess::upload( tmpFile.fileName(), newUrl,
parentWidget() );
316 KABC::Addressee::List VCardXXPort::filterContacts(
const KABC::Addressee::List &addrList )
const
318 KABC::Addressee::List list;
320 if ( addrList.isEmpty() ) {
324 QPointer<VCardExportSelectionDialog> dlg =
new VCardExportSelectionDialog(
parentWidget() );
325 if ( !dlg->exec() || !dlg ) {
330 KABC::Addressee::List::ConstIterator it;
331 KABC::Addressee::List::ConstIterator end( addrList.end() );
332 for ( it = addrList.begin(); it != end; ++it ) {
333 KABC::Addressee addr;
335 addr.setUid( (*it).uid() );
336 addr.setFormattedName( (*it).formattedName() );
338 bool addrDone =
false;
339 if ( dlg->exportDisplayName() ) {
340 QString fmtName = (*it).formattedName();
341 QStringList splitNames = fmtName.split( QLatin1Char(
' '), QString::SkipEmptyParts );
342 if ( splitNames.count() >= 2 ) {
343 addr.setPrefix( QString() );
344 addr.setGivenName( splitNames.takeFirst() );
345 addr.setFamilyName( splitNames.takeLast() );
346 addr.setAdditionalName( splitNames.join( QLatin1String(
" ") ) );
347 addr.setSuffix( QString() );
353 addr.setPrefix( (*it).prefix() );
354 addr.setGivenName( (*it).givenName() );
355 addr.setAdditionalName( (*it).additionalName() );
356 addr.setFamilyName( (*it).familyName() );
357 addr.setSuffix( (*it).suffix() );
360 addr.setNickName( (*it).nickName() );
361 addr.setMailer( (*it).mailer() );
362 addr.setTimeZone( (*it).timeZone() );
363 addr.setGeo( (*it).geo() );
364 addr.setProductId( (*it).productId() );
365 addr.setSortString( (*it).sortString() );
366 addr.setUrl( (*it).url() );
367 addr.setSecrecy( (*it).secrecy() );
368 addr.setSound( (*it).sound() );
369 addr.setEmails( (*it).emails() );
370 addr.setCategories( (*it).categories() );
372 if ( dlg->exportPrivateFields() ) {
373 addr.setBirthday( (*it).birthday() );
374 addr.setNote( (*it).note() );
377 if ( dlg->exportPictureFields() ) {
378 if ( dlg->exportPrivateFields() ) {
379 addr.setPhoto( (*it).photo() );
382 if ( dlg->exportBusinessFields() ) {
383 addr.setLogo( (*it).logo() );
387 if ( dlg->exportBusinessFields() ) {
388 addr.setTitle( (*it).title() );
389 addr.setRole( (*it).role() );
390 addr.setOrganization( (*it).organization() );
391 addr.setDepartment( (*it).department() );
393 KABC::PhoneNumber::List phones = (*it).phoneNumbers( KABC::PhoneNumber::Work );
394 KABC::PhoneNumber::List::Iterator phoneIt;
395 for ( phoneIt = phones.begin(); phoneIt != phones.end(); ++phoneIt ) {
396 addr.insertPhoneNumber( *phoneIt );
399 KABC::Address::List addresses = (*it).addresses( KABC::Address::Work );
400 KABC::Address::List::Iterator addrIt;
401 for ( addrIt = addresses.begin(); addrIt != addresses.end(); ++addrIt ) {
402 addr.insertAddress( *addrIt );
406 KABC::PhoneNumber::List phones = (*it).phoneNumbers();
407 KABC::PhoneNumber::List::Iterator phoneIt;
408 for ( phoneIt = phones.begin(); phoneIt != phones.end(); ++phoneIt ) {
409 int type = (*phoneIt).type();
411 if ( type & KABC::PhoneNumber::Home && dlg->exportPrivateFields() ) {
412 addr.insertPhoneNumber( *phoneIt );
413 }
else if ( type & KABC::PhoneNumber::Work && dlg->exportBusinessFields() ) {
414 addr.insertPhoneNumber( *phoneIt );
415 }
else if ( dlg->exportOtherFields() ) {
416 addr.insertPhoneNumber( *phoneIt );
420 KABC::Address::List addresses = (*it).addresses();
421 KABC::Address::List::Iterator addrIt;
422 for ( addrIt = addresses.begin(); addrIt != addresses.end(); ++addrIt ) {
423 int type = (*addrIt).type();
425 if ( type & KABC::Address::Home && dlg->exportPrivateFields() ) {
426 addr.insertAddress( *addrIt );
427 }
else if ( type & KABC::Address::Work && dlg->exportBusinessFields() ) {
428 addr.insertAddress( *addrIt );
429 }
else if ( dlg->exportOtherFields() ) {
430 addr.insertAddress( *addrIt );
434 if ( dlg->exportOtherFields() ) {
435 addr.setCustoms( (*it).customs() );
438 if ( dlg->exportEncryptionKeys() ) {
439 addKey( addr, KABC::Key::PGP );
440 addKey( addr, KABC::Key::X509 );
451 void VCardXXPort::addKey( KABC::Addressee &addr, KABC::Key::Type type )
const
454 const QString fingerprint = addr.custom( QLatin1String(
"KADDRESSBOOK"),
455 ( type == KABC::Key::PGP ? QLatin1String(
"OPENPGPFP") : QLatin1String(
"SMIMEFP") ) );
456 if ( fingerprint.isEmpty() ) {
460 GpgME::Context *context = GpgME::Context::createForProtocol( GpgME::OpenPGP );
462 kError() <<
"No context available";
466 context->setArmor(
false );
467 context->setTextMode(
false );
469 QGpgME::QByteArrayDataProvider dataProvider;
470 GpgME::Data dataObj( &dataProvider );
471 GpgME::Error error = context->exportPublicKeys( fingerprint.toLatin1(), dataObj );
475 kError() << error.asString();
481 key.setBinaryData( dataProvider.data() );
483 addr.insertKey( key );
491 VCardViewerDialog::VCardViewerDialog(
const KABC::Addressee::List &list, QWidget *parent )
495 setCaption( i18nc(
"@title:window",
"Import vCard" ) );
496 setButtons( User1 | User2 | Apply | Cancel );
497 setButtonGuiItem(User1, KStandardGuiItem::no());
498 setButtonGuiItem(User2, KStandardGuiItem::yes());
499 setDefaultButton( User1 );
501 showButtonSeparator(
true );
503 QFrame *page =
new QFrame(
this );
504 setMainWidget( page );
506 QVBoxLayout *layout =
new QVBoxLayout( page );
507 layout->setSpacing( spacingHint() );
508 layout->setMargin( marginHint() );
512 i18nc(
"@info",
"Do you want to import this contact into your address book?" ), page );
513 QFont font = label->font();
514 font.setBold(
true );
515 label->setFont( font );
516 layout->addWidget( label );
518 mView =
new KAddressBookGrantlee::GrantleeContactViewer( page );
520 layout->addWidget( mView );
522 setButtonText( Apply, i18nc(
"@action:button",
"Import All..." ) );
524 mIt = mContacts.begin();
526 connect(
this, SIGNAL(user2Clicked()),
this, SLOT(slotYes()) );
527 connect(
this, SIGNAL(user1Clicked()),
this, SLOT(slotNo()) );
528 connect(
this, SIGNAL(applyClicked()),
this, SLOT(slotApply()) );
529 connect(
this, SIGNAL(cancelClicked()),
this, SLOT(slotCancel()) );
535 VCardViewerDialog::~VCardViewerDialog()
540 void VCardViewerDialog::readConfig()
542 KConfigGroup group( KGlobal::config(),
"VCardViewerDialog" );
543 const QSize size = group.readEntry(
"Size", QSize(600, 400) );
544 if ( size.isValid() ) {
549 void VCardViewerDialog::writeConfig()
551 KConfigGroup group( KGlobal::config(),
"VCardViewerDialog" );
552 group.writeEntry(
"Size", size() );
557 KABC::Addressee::List VCardViewerDialog::contacts()
const
562 void VCardViewerDialog::updateView()
564 mView->setRawContact( *mIt );
566 KABC::Addressee::List::Iterator it = mIt;
567 enableButton( Apply, ( ++it ) != mContacts.end() );
570 void VCardViewerDialog::slotYes()
574 if ( mIt == mContacts.end() ) {
582 void VCardViewerDialog::slotNo()
584 if ( mIt == mContacts.end() ) {
589 mIt = mContacts.erase( mIt );
590 if ( mIt == mContacts.end() ) {
597 void VCardViewerDialog::slotApply()
602 void VCardViewerDialog::slotCancel()
610 VCardExportSelectionDialog::VCardExportSelectionDialog( QWidget *parent )
613 setCaption( i18nc(
"@title:window",
"Select vCard Fields" ) );
614 setButtons( Ok | Cancel );
615 setDefaultButton( Ok );
617 showButtonSeparator(
true );
619 QFrame *page =
new QFrame(
this );
620 setMainWidget( page );
622 QGridLayout *layout =
new QGridLayout( page );
623 layout->setSpacing( spacingHint() );
624 layout->setMargin( marginHint() );
626 QGroupBox *gbox =
new QGroupBox(
627 i18nc(
"@title:group",
"Fields to be exported" ), page );
628 gbox->setFlat(
true );
629 layout->addWidget( gbox, 0, 0, 1, 2 );
631 mPrivateBox =
new QCheckBox( i18nc(
"@option:check",
"Private fields" ), page );
632 mPrivateBox->setToolTip(
633 i18nc(
"@info:tooltip",
"Export private fields" ) );
634 mPrivateBox->setWhatsThis(
635 i18nc(
"@info:whatsthis",
636 "Check this box if you want to export the contact's "
637 "private fields to the vCard output file." ) );
638 layout->addWidget( mPrivateBox, 1, 0 );
640 mBusinessBox =
new QCheckBox( i18nc(
"@option:check",
"Business fields" ), page );
641 mBusinessBox->setToolTip(
642 i18nc(
"@info:tooltip",
"Export business fields" ) );
643 mBusinessBox->setWhatsThis(
644 i18nc(
"@info:whatsthis",
645 "Check this box if you want to export the contact's "
646 "business fields to the vCard output file." ) );
647 layout->addWidget( mBusinessBox, 2, 0 );
649 mOtherBox =
new QCheckBox( i18nc(
"@option:check",
"Other fields" ), page );
650 mOtherBox->setToolTip(
651 i18nc(
"@info:tooltip",
"Export other fields" ) );
652 mOtherBox->setWhatsThis(
653 i18nc(
"@info:whatsthis",
654 "Check this box if you want to export the contact's "
655 "other fields to the vCard output file." ) );
656 layout->addWidget( mOtherBox, 3, 0 );
658 mEncryptionKeys =
new QCheckBox( i18nc(
"@option:check",
"Encryption keys" ), page );
659 mEncryptionKeys->setToolTip(
660 i18nc(
"@info:tooltip",
"Export encryption keys" ) );
661 mEncryptionKeys->setWhatsThis(
662 i18nc(
"@info:whatsthis",
663 "Check this box if you want to export the contact's "
664 "encryption keys to the vCard output file." ) );
665 layout->addWidget( mEncryptionKeys, 1, 1 );
667 mPictureBox =
new QCheckBox( i18nc(
"@option:check",
"Pictures" ), page );
668 mPictureBox->setToolTip(
669 i18nc(
"@info:tooltip",
"Export pictures" ) );
670 mPictureBox->setWhatsThis(
671 i18nc(
"@info:whatsthis",
672 "Check this box if you want to export the contact's "
673 "picture to the vCard output file." ) );
674 layout->addWidget( mPictureBox, 2, 1 );
676 gbox =
new QGroupBox(
677 i18nc(
"@title:group",
"Export options" ), page );
678 gbox->setFlat(
true );
679 layout->addWidget( gbox, 4, 0, 1, 2 );
681 mDisplayNameBox =
new QCheckBox( i18nc(
"@option:check",
"Display name as full name" ), page );
682 mDisplayNameBox->setToolTip(
683 i18nc(
"@info:tooltip",
"Export display name as full name" ) );
684 mDisplayNameBox->setWhatsThis(
685 i18nc(
"@info:whatsthis",
686 "Check this box if you want to export the contact's display name "
687 "in the vCard's full name field. This may be required to get the "
688 "name shown correctly in GMail or Android." ) );
689 layout->addWidget( mDisplayNameBox, 5, 0, 1, 2 );
691 KConfig config( QLatin1String(
"kaddressbookrc") );
692 const KConfigGroup group( &config,
"XXPortVCard" );
694 mPrivateBox->setChecked( group.readEntry(
"ExportPrivateFields",
true ) );
695 mBusinessBox->setChecked( group.readEntry(
"ExportBusinessFields",
true ) );
696 mOtherBox->setChecked( group.readEntry(
"ExportOtherFields",
true ) );
697 mEncryptionKeys->setChecked( group.readEntry(
"ExportEncryptionKeys",
true ) );
698 mPictureBox->setChecked( group.readEntry(
"ExportPictureFields",
true ) );
699 mDisplayNameBox->setChecked( group.readEntry(
"ExportDisplayName",
false ) );
702 VCardExportSelectionDialog::~VCardExportSelectionDialog()
704 KConfig config( QLatin1String(
"kaddressbookrc") );
705 KConfigGroup group( &config,
"XXPortVCard" );
707 group.writeEntry(
"ExportPrivateFields", mPrivateBox->isChecked() );
708 group.writeEntry(
"ExportBusinessFields", mBusinessBox->isChecked() );
709 group.writeEntry(
"ExportOtherFields", mOtherBox->isChecked() );
710 group.writeEntry(
"ExportEncryptionKeys", mEncryptionKeys->isChecked() );
711 group.writeEntry(
"ExportPictureFields", mPictureBox->isChecked() );
712 group.writeEntry(
"ExportDisplayName", mDisplayNameBox->isChecked() );
715 bool VCardExportSelectionDialog::exportPrivateFields()
const
717 return mPrivateBox->isChecked();
720 bool VCardExportSelectionDialog::exportBusinessFields()
const
722 return mBusinessBox->isChecked();
725 bool VCardExportSelectionDialog::exportOtherFields()
const
727 return mOtherBox->isChecked();
730 bool VCardExportSelectionDialog::exportEncryptionKeys()
const
732 return mEncryptionKeys->isChecked();
735 bool VCardExportSelectionDialog::exportPictureFields()
const
737 return mPictureBox->isChecked();
740 bool VCardExportSelectionDialog::exportDisplayName()
const
742 return mDisplayNameBox->isChecked();
744 #include "vcard_xxport.moc"
QWidget * parentWidget() const
Returns the parent widget that can be used as parent for GUI components.
VCardXXPort(QWidget *parent=0)
QString contactFileName(const KABC::Addressee &contact) const
Returns a file name depending on the passed contact.
The base class for all import/export modules.
QString option(const QString &key) const
Returns the module specific option value for the given key.
KABC::Addressee::List importContacts() const
Imports a list of contacts.
bool exportContacts(const KABC::Addressee::List &contacts) const
Exports the list of contacts.