24 #include <tqcheckbox.h> 29 #include <tqpushbutton.h> 31 #include <tdeabc/vcardconverter.h> 32 #include <kdialogbase.h> 33 #include <tdefiledialog.h> 34 #include <tdeio/netaccess.h> 35 #include <tdelocale.h> 36 #include <tdemessagebox.h> 37 #include <tdetempfile.h> 39 #include <tdeapplication.h> 40 #include <libtdepim/addresseeview.h> 44 #include "gpgmepp/context.h" 45 #include "gpgmepp/data.h" 46 #include "gpgmepp/key.h" 47 #include "qgpgme/dataprovider.h" 49 #include "xxportmanager.h" 51 #include "vcard_xxport.h" 53 K_EXPORT_KADDRESSBOOK_XXFILTER( libkaddrbk_vcard_xxport, VCardXXPort )
55 class VCardViewerDialog :
public KDialogBase
58 VCardViewerDialog(
const TDEABC::Addressee::List &list,
59 TQWidget *parent,
const char *name = 0 );
61 TDEABC::Addressee::List contacts()
const;
72 KPIM::AddresseeView *mView;
74 TDEABC::Addressee::List mContacts;
75 TDEABC::Addressee::List::Iterator mIt;
78 class VCardExportSelectionDialog :
public KDialogBase
81 VCardExportSelectionDialog( TQWidget *parent,
const char *name = 0 );
82 ~VCardExportSelectionDialog();
84 bool exportPrivateFields()
const;
85 bool exportBusinessFields()
const;
86 bool exportOtherFields()
const;
87 bool exportEncryptionKeys()
const;
90 TQCheckBox *mPrivateBox;
91 TQCheckBox *mBusinessBox;
92 TQCheckBox *mOtherBox;
93 TQCheckBox *mEncryptionKeys;
96 VCardXXPort::VCardXXPort( TDEABC::AddressBook *ab, TQWidget *parent,
const char *name )
97 :
KAB::XXPort( ab, parent, name )
99 createImportAction( i18n(
"Import vCard..." ) );
100 createExportAction( i18n(
"Export vCard 2.1..." ),
"v21" );
101 createExportAction( i18n(
"Export vCard 3.0..." ),
"v30" );
104 bool VCardXXPort::exportContacts(
const TDEABC::AddresseeList &addrList,
const TQString &data )
106 TDEABC::VCardConverter converter;
108 TDEABC::AddresseeList list;
110 list = filterContacts( addrList );
113 if ( list.isEmpty() ) {
115 }
else if ( list.count() == 1 ) {
116 url = KFileDialog::getSaveURL( list[ 0 ].givenName() +
"_" + list[ 0 ].familyName() +
".vcf" );
121 #if defined(KABC_VCARD_ENCODING_FIX) 122 ok = doExport( url, converter.createVCardsRaw( list, TDEABC::VCardConverter::v2_1 ) );
124 ok = doExport( url, converter.createVCardsRaw( list, TDEABC::VCardConverter::v3_0 ) );
126 ok = doExport( url, converter.createVCards( list, TDEABC::VCardConverter::v2_1 ) );
128 ok = doExport( url, converter.createVCards( list, TDEABC::VCardConverter::v3_0 ) );
131 TQString msg = i18n(
"You have selected a list of contacts, shall they be " 132 "exported to several files?" );
134 switch ( KMessageBox::questionYesNo( parentWidget(), msg, TQString(), i18n(
"Export to Several Files"), i18n(
"Export to One File") ) ) {
135 case KMessageBox::Yes: {
136 KURL baseUrl = KFileDialog::getExistingURL();
137 if ( baseUrl.isEmpty() )
140 TDEABC::AddresseeList::ConstIterator it;
142 for ( it = list.begin(); it != list.end(); ++it ) {
144 if ( (*it).givenName().isEmpty() && (*it).familyName().isEmpty() )
145 testUrl = baseUrl.url() +
"/" + (*it).organization();
147 testUrl = baseUrl.url() +
"/" + (*it).givenName() +
"_" + (*it).familyName();
149 if ( TDEIO::NetAccess::exists( testUrl + (counter == 0 ?
"" : TQString::number( counter )) +
".vcf",
false, parentWidget() ) ) {
151 url = testUrl + TQString::number( counter ) +
".vcf";
153 url = testUrl +
".vcf";
156 TDEABC::AddresseeList tmpList;
157 tmpList.append( *it );
160 #if defined(KABC_VCARD_ENCODING_FIX) 161 tmpOk = doExport( url, converter.createVCardsRaw( tmpList, TDEABC::VCardConverter::v2_1 ) );
163 tmpOk = doExport( url, converter.createVCardsRaw( tmpList, TDEABC::VCardConverter::v3_0 ) );
165 tmpOk = doExport( url, converter.createVCards( tmpList, TDEABC::VCardConverter::v2_1 ) );
167 tmpOk = doExport( url, converter.createVCards( tmpList, TDEABC::VCardConverter::v3_0 ) );
173 case KMessageBox::No:
175 url = KFileDialog::getSaveURL(
"addressbook.vcf" );
180 #if defined(KABC_VCARD_ENCODING_FIX) 181 ok = doExport( url, converter.createVCardsRaw( list, TDEABC::VCardConverter::v2_1 ) );
183 ok = doExport( url, converter.createVCardsRaw( list, TDEABC::VCardConverter::v3_0 ) );
185 ok = doExport( url, converter.createVCards( list, TDEABC::VCardConverter::v2_1 ) );
187 ok = doExport( url, converter.createVCards( list, TDEABC::VCardConverter::v3_0 ) );
196 TDEABC::AddresseeList VCardXXPort::importContacts(
const TQString& )
const 199 TDEABC::AddresseeList addrList;
202 if ( !XXPortManager::importData.isEmpty() ) {
203 #if defined(KABC_VCARD_ENCODING_FIX) 204 TQCString data = XXPortManager::importData.ascii();
205 addrList = parseVCard( data );
207 addrList = parseVCard( XXPortManager::importData );
210 if ( XXPortManager::importURL.isEmpty() )
211 urls = KFileDialog::getOpenURLs( TQString(),
"*.vcf|vCards", parentWidget(),
212 i18n(
"Select vCard to Import" ) );
214 urls.append( XXPortManager::importURL );
216 if ( urls.count() == 0 )
219 TQString caption( i18n(
"vCard Import Failed" ) );
220 bool anyFailures =
false;
221 KURL::List::Iterator it;
222 for ( it = urls.begin(); it != urls.end(); ++it ) {
223 if ( TDEIO::NetAccess::download( *it, fileName, parentWidget() ) ) {
225 TQFile file( fileName );
227 if ( file.open( IO_ReadOnly ) ) {
228 #if defined(KABC_VCARD_ENCODING_FIX) 229 TQByteArray data = file.readAll();
231 if ( data.size() > 0 )
232 addrList += parseVCard( data );
234 TQByteArray rawData = file.readAll();
236 if ( rawData.size() > 0 ) {
242 if ( TQString::fromLatin1( rawData ).lower().contains(
"version:3.0" ) ) {
243 vCardText = TQString::fromUtf8( rawData );
245 vCardText = TQString::fromLatin1( rawData );
247 addrList += parseVCard( vCardText );
250 TDEIO::NetAccess::removeTempFile( fileName );
252 TQString text = i18n(
"<qt>When trying to read the vCard, there was an error opening the file '%1': %2</qt>" );
253 text = text.arg( (*it).url() );
254 text = text.arg( kapp->translate(
"TQFile",
255 TQString(file.errorString()).latin1() ) );
256 KMessageBox::error( parentWidget(), text, caption );
260 TQString text = i18n(
"<qt>Unable to access vCard: %1</qt>" );
261 text = text.arg( TDEIO::NetAccess::lastErrorString() );
262 KMessageBox::error( parentWidget(), text, caption );
267 if ( !XXPortManager::importURL.isEmpty() ) {
268 if ( addrList.isEmpty() ) {
269 if ( anyFailures && urls.count() > 1 )
270 KMessageBox::information( parentWidget(),
271 i18n(
"No contacts were imported, due to errors with the vCards." ) );
272 else if ( !anyFailures )
273 KMessageBox::information( parentWidget(), i18n(
"The vCard does not contain any contacts." ) );
275 VCardViewerDialog dlg( addrList, parentWidget() );
277 addrList = dlg.contacts();
285 #if defined(KABC_VCARD_ENCODING_FIX) 286 TDEABC::AddresseeList VCardXXPort::parseVCard(
const TQByteArray &data )
const 288 TDEABC::VCardConverter converter;
290 return converter.parseVCardsRaw( data.data() );
293 bool VCardXXPort::doExport(
const KURL &url,
const TQByteArray &data )
295 if( TQFileInfo(url.path()).exists() ) {
296 if(KMessageBox::questionYesNo( parentWidget(), i18n(
"Do you want to overwrite file \"%1\"").arg( url.path()) ) == KMessageBox::No)
300 tmpFile.setAutoDelete(
true );
302 tmpFile.file()->writeBlock( data.data(), data.size() );
305 return TDEIO::NetAccess::upload( tmpFile.name(), url, parentWidget() );
308 TDEABC::AddresseeList VCardXXPort::parseVCard(
const TQString &data )
const 310 TDEABC::VCardConverter converter;
312 return converter.parseVCards( data );
315 bool VCardXXPort::doExport(
const KURL &url,
const TQString &data )
317 if( TQFileInfo(url.path()).exists() ) {
318 if(KMessageBox::questionYesNo( parentWidget(), i18n(
"Do you want to overwrite file \"%1\"").arg( url.path()) ) == KMessageBox::No)
322 tmpFile.setAutoDelete(
true );
324 TQTextStream stream( tmpFile.file() );
325 stream.setEncoding( TQTextStream::UnicodeUTF8 );
330 return TDEIO::NetAccess::upload( tmpFile.name(), url, parentWidget() );
334 TDEABC::AddresseeList VCardXXPort::filterContacts(
const TDEABC::AddresseeList &addrList )
336 TDEABC::AddresseeList list;
338 if ( addrList.isEmpty() )
341 VCardExportSelectionDialog dlg( parentWidget() );
345 TDEABC::AddresseeList::ConstIterator it;
346 for ( it = addrList.begin(); it != addrList.end(); ++it ) {
347 TDEABC::Addressee addr;
349 addr.setUid( (*it).uid() );
350 addr.setFormattedName( (*it).formattedName() );
351 addr.setPrefix( (*it).prefix() );
352 addr.setGivenName( (*it).givenName() );
353 addr.setAdditionalName( (*it).additionalName() );
354 addr.setFamilyName( (*it).familyName() );
355 addr.setSuffix( (*it).suffix() );
356 addr.setNickName( (*it).nickName() );
357 addr.setMailer( (*it).mailer() );
358 addr.setTimeZone( (*it).timeZone() );
359 addr.setGeo( (*it).geo() );
360 addr.setProductId( (*it).productId() );
361 addr.setSortString( (*it).sortString() );
362 addr.setUrl( (*it).url() );
363 addr.setSecrecy( (*it).secrecy() );
364 addr.setSound( (*it).sound() );
365 addr.setEmails( (*it).emails() );
366 addr.setCategories( (*it).categories() );
368 if ( dlg.exportPrivateFields() ) {
369 addr.setBirthday( (*it).birthday() );
370 addr.setNote( (*it).note() );
371 addr.setPhoto( (*it).photo() );
374 if ( dlg.exportBusinessFields() ) {
375 addr.setTitle( (*it).title() );
376 addr.setRole( (*it).role() );
377 addr.setOrganization( (*it).organization() );
379 addr.setLogo( (*it).logo() );
381 TDEABC::PhoneNumber::List phones = (*it).phoneNumbers( TDEABC::PhoneNumber::Work );
382 TDEABC::PhoneNumber::List::Iterator phoneIt;
383 for ( phoneIt = phones.begin(); phoneIt != phones.end(); ++phoneIt )
384 addr.insertPhoneNumber( *phoneIt );
386 TDEABC::Address::List addresses = (*it).addresses( TDEABC::Address::Work );
387 TDEABC::Address::List::Iterator addrIt;
388 for ( addrIt = addresses.begin(); addrIt != addresses.end(); ++addrIt )
389 addr.insertAddress( *addrIt );
392 TDEABC::PhoneNumber::List phones = (*it).phoneNumbers();
393 TDEABC::PhoneNumber::List::Iterator phoneIt;
394 for ( phoneIt = phones.begin(); phoneIt != phones.end(); ++phoneIt ) {
395 int type = (*phoneIt).type();
397 if ( type & TDEABC::PhoneNumber::Home && dlg.exportPrivateFields() )
398 addr.insertPhoneNumber( *phoneIt );
399 else if ( type & TDEABC::PhoneNumber::Work && dlg.exportBusinessFields() )
400 addr.insertPhoneNumber( *phoneIt );
401 else if ( dlg.exportOtherFields() )
402 addr.insertPhoneNumber( *phoneIt );
405 TDEABC::Address::List addresses = (*it).addresses();
406 TDEABC::Address::List::Iterator addrIt;
407 for ( addrIt = addresses.begin(); addrIt != addresses.end(); ++addrIt ) {
408 int type = (*addrIt).type();
410 if ( type & TDEABC::Address::Home && dlg.exportPrivateFields() )
411 addr.insertAddress( *addrIt );
412 else if ( type & TDEABC::Address::Work && dlg.exportBusinessFields() )
413 addr.insertAddress( *addrIt );
414 else if ( dlg.exportOtherFields() )
415 addr.insertAddress( *addrIt );
418 if ( dlg.exportOtherFields() )
419 addr.setCustoms( (*it).customs() );
421 if ( dlg.exportEncryptionKeys() ) {
422 addKey( addr, TDEABC::Key::PGP );
423 addKey( addr, TDEABC::Key::X509 );
432 void VCardXXPort::addKey( TDEABC::Addressee &addr, TDEABC::Key::Types type )
434 TQString fingerprint = addr.custom(
"KADDRESSBOOK",
435 (type == TDEABC::Key::PGP ?
"OPENPGPFP" :
"SMIMEFP") );
436 if ( fingerprint.isEmpty() )
439 GpgME::Context * context = GpgME::Context::createForProtocol( GpgME::Context::OpenPGP );
441 kdError() <<
"No context available" << endl;
445 context->setArmor(
false );
446 context->setTextMode(
false );
448 QGpgME::TQByteArrayDataProvider dataProvider;
449 GpgME::Data dataObj( &dataProvider );
450 GpgME::Error error = context->exportPublicKeys( fingerprint.latin1(), dataObj );
454 kdError() << error.asString() << endl;
460 key.setBinaryData( dataProvider.data() );
462 addr.insertKey( key );
467 VCardViewerDialog::VCardViewerDialog(
const TDEABC::Addressee::List &list,
468 TQWidget *parent,
const char *name )
469 : KDialogBase( Plain, i18n(
"Import vCard" ), Yes | No | Apply | Cancel, Yes,
470 parent, name, true, true, KStdGuiItem::no(), KStdGuiItem::yes() ),
473 TQFrame *page = plainPage();
474 TQVBoxLayout *layout =
new TQVBoxLayout( page, marginHint(), spacingHint() );
476 TQLabel *label =
new TQLabel( i18n(
"Do you want to import this contact in your address book?" ), page );
477 TQFont font = label->font();
478 font.setBold(
true );
479 label->setFont( font );
480 layout->addWidget( label );
482 mView =
new KPIM::AddresseeView( page );
483 mView->enableLinks( 0 );
484 mView->setVScrollBarMode( TQScrollView::Auto );
485 layout->addWidget( mView );
487 setButtonText( Apply, i18n(
"Import All..." ) );
489 mIt = mContacts.begin();
494 TDEABC::Addressee::List VCardViewerDialog::contacts()
const 499 void VCardViewerDialog::updateView()
501 mView->setAddressee( *mIt );
503 TDEABC::Addressee::List::Iterator it = mIt;
504 actionButton( Apply )->setEnabled( (++it) != mContacts.end() );
507 void VCardViewerDialog::slotUser1()
509 mIt = mContacts.remove( mIt );
511 if ( mIt == mContacts.end() )
517 void VCardViewerDialog::slotUser2()
521 if ( mIt == mContacts.end() )
527 void VCardViewerDialog::slotApply()
532 void VCardViewerDialog::slotCancel()
540 VCardExportSelectionDialog::VCardExportSelectionDialog( TQWidget *parent,
542 : KDialogBase( Plain, i18n(
"Select vCard Fields" ), Ok | Cancel, Ok,
543 parent, name, true, true )
545 TQFrame *page = plainPage();
547 TQVBoxLayout *layout =
new TQVBoxLayout( page, marginHint(), spacingHint() );
549 TQLabel *label =
new TQLabel( i18n(
"Select the fields which shall be exported in the vCard." ), page );
550 layout->addWidget( label );
552 mPrivateBox =
new TQCheckBox( i18n(
"Private fields" ), page );
553 layout->addWidget( mPrivateBox );
555 mBusinessBox =
new TQCheckBox( i18n(
"Business fields" ), page );
556 layout->addWidget( mBusinessBox );
558 mOtherBox =
new TQCheckBox( i18n(
"Other fields" ), page );
559 layout->addWidget( mOtherBox );
561 mEncryptionKeys =
new TQCheckBox( i18n(
"Encryption keys" ), page );
562 layout->addWidget( mEncryptionKeys );
564 TDEConfig config(
"kaddressbookrc" );
565 config.setGroup(
"XXPortVCard" );
567 mPrivateBox->setChecked( config.readBoolEntry(
"ExportPrivateFields",
true ) );
568 mBusinessBox->setChecked( config.readBoolEntry(
"ExportBusinessFields",
false ) );
569 mOtherBox->setChecked( config.readBoolEntry(
"ExportOtherFields",
false ) );
570 mEncryptionKeys->setChecked( config.readBoolEntry(
"ExportEncryptionKeys",
false ) );
573 VCardExportSelectionDialog::~VCardExportSelectionDialog()
575 TDEConfig config(
"kaddressbookrc" );
576 config.setGroup(
"XXPortVCard" );
578 config.writeEntry(
"ExportPrivateFields", mPrivateBox->isChecked() );
579 config.writeEntry(
"ExportBusinessFields", mBusinessBox->isChecked() );
580 config.writeEntry(
"ExportOtherFields", mOtherBox->isChecked() );
581 config.writeEntry(
"ExportEncryptionKeys", mEncryptionKeys->isChecked() );
584 bool VCardExportSelectionDialog::exportPrivateFields()
const 586 return mPrivateBox->isChecked();
589 bool VCardExportSelectionDialog::exportBusinessFields()
const 591 return mBusinessBox->isChecked();
594 bool VCardExportSelectionDialog::exportOtherFields()
const 596 return mOtherBox->isChecked();
599 bool VCardExportSelectionDialog::exportEncryptionKeys()
const 601 return mEncryptionKeys->isChecked();
604 #include "vcard_xxport.moc"