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 ) {
243 TQString tmp = TQString::fromLatin1(rawData).lower();
244 int ver = tmp.find(
"version:");
250 float vCardVersion = tmp.mid(ver + 8, tmp.find(
"\n", ver)).toFloat();
251 useUtf8 = (vCardVersion >= 3.0);
255 vCardText = TQString::fromUtf8( rawData );
258 vCardText = TQString::fromLatin1( rawData );
260 addrList += parseVCard( vCardText );
263 TDEIO::NetAccess::removeTempFile( fileName );
265 TQString text = i18n(
"<qt>When trying to read the vCard, there was an error opening the file '%1': %2</qt>" );
266 text = text.arg( (*it).url() );
267 text = text.arg( kapp->translate(
"TQFile",
268 TQString(file.errorString()).latin1() ) );
269 KMessageBox::error( parentWidget(), text, caption );
273 TQString text = i18n(
"<qt>Unable to access vCard: %1</qt>" );
274 text = text.arg( TDEIO::NetAccess::lastErrorString() );
275 KMessageBox::error( parentWidget(), text, caption );
280 if ( !XXPortManager::importURL.isEmpty() ) {
281 if ( addrList.isEmpty() ) {
282 if ( anyFailures && urls.count() > 1 )
283 KMessageBox::information( parentWidget(),
284 i18n(
"No contacts were imported, due to errors with the vCards." ) );
285 else if ( !anyFailures )
286 KMessageBox::information( parentWidget(), i18n(
"The vCard does not contain any contacts." ) );
288 VCardViewerDialog dlg( addrList, parentWidget() );
290 addrList = dlg.contacts();
298 #if defined(KABC_VCARD_ENCODING_FIX)
299 TDEABC::AddresseeList VCardXXPort::parseVCard(
const TQByteArray &data )
const
301 TDEABC::VCardConverter converter;
303 return converter.parseVCardsRaw( data.data() );
306 bool VCardXXPort::doExport(
const KURL &url,
const TQByteArray &data )
308 if( TQFileInfo(url.path()).exists() ) {
309 if(KMessageBox::questionYesNo( parentWidget(), i18n(
"Do you want to overwrite file \"%1\"").arg( url.path()) ) == KMessageBox::No)
313 tmpFile.setAutoDelete(
true );
315 tmpFile.file()->writeBlock( data.data(), data.size() );
318 return TDEIO::NetAccess::upload( tmpFile.name(), url, parentWidget() );
321 TDEABC::AddresseeList VCardXXPort::parseVCard(
const TQString &data )
const
323 TDEABC::VCardConverter converter;
325 return converter.parseVCards( data );
328 bool VCardXXPort::doExport(
const KURL &url,
const TQString &data )
330 if( TQFileInfo(url.path()).exists() ) {
331 if(KMessageBox::questionYesNo( parentWidget(), i18n(
"Do you want to overwrite file \"%1\"").arg( url.path()) ) == KMessageBox::No)
335 tmpFile.setAutoDelete(
true );
337 TQTextStream stream( tmpFile.file() );
338 stream.setEncoding( TQTextStream::UnicodeUTF8 );
343 return TDEIO::NetAccess::upload( tmpFile.name(), url, parentWidget() );
347 TDEABC::AddresseeList VCardXXPort::filterContacts(
const TDEABC::AddresseeList &addrList )
349 TDEABC::AddresseeList list;
351 if ( addrList.isEmpty() )
354 VCardExportSelectionDialog dlg( parentWidget() );
358 TDEABC::AddresseeList::ConstIterator it;
359 for ( it = addrList.begin(); it != addrList.end(); ++it ) {
360 TDEABC::Addressee addr;
362 addr.setUid( (*it).uid() );
363 addr.setFormattedName( (*it).formattedName() );
364 addr.setPrefix( (*it).prefix() );
365 addr.setGivenName( (*it).givenName() );
366 addr.setAdditionalName( (*it).additionalName() );
367 addr.setFamilyName( (*it).familyName() );
368 addr.setSuffix( (*it).suffix() );
369 addr.setNickName( (*it).nickName() );
370 addr.setMailer( (*it).mailer() );
371 addr.setTimeZone( (*it).timeZone() );
372 addr.setGeo( (*it).geo() );
373 addr.setProductId( (*it).productId() );
374 addr.setSortString( (*it).sortString() );
375 addr.setUrl( (*it).url() );
376 addr.setSecrecy( (*it).secrecy() );
377 addr.setSound( (*it).sound() );
378 addr.setEmails( (*it).emails() );
379 addr.setCategories( (*it).categories() );
381 if ( dlg.exportPrivateFields() ) {
382 addr.setBirthday( (*it).birthday() );
383 addr.setNote( (*it).note() );
384 addr.setPhoto( (*it).photo() );
387 if ( dlg.exportBusinessFields() ) {
388 addr.setTitle( (*it).title() );
389 addr.setRole( (*it).role() );
390 addr.setOrganization( (*it).organization() );
392 addr.setLogo( (*it).logo() );
394 TDEABC::PhoneNumber::List phones = (*it).phoneNumbers( TDEABC::PhoneNumber::Work );
395 TDEABC::PhoneNumber::List::Iterator phoneIt;
396 for ( phoneIt = phones.begin(); phoneIt != phones.end(); ++phoneIt )
397 addr.insertPhoneNumber( *phoneIt );
399 TDEABC::Address::List addresses = (*it).addresses( TDEABC::Address::Work );
400 TDEABC::Address::List::Iterator addrIt;
401 for ( addrIt = addresses.begin(); addrIt != addresses.end(); ++addrIt )
402 addr.insertAddress( *addrIt );
405 TDEABC::PhoneNumber::List phones = (*it).phoneNumbers();
406 TDEABC::PhoneNumber::List::Iterator phoneIt;
407 for ( phoneIt = phones.begin(); phoneIt != phones.end(); ++phoneIt ) {
408 int type = (*phoneIt).type();
410 if ( type & TDEABC::PhoneNumber::Home && dlg.exportPrivateFields() )
411 addr.insertPhoneNumber( *phoneIt );
412 else if ( type & TDEABC::PhoneNumber::Work && dlg.exportBusinessFields() )
413 addr.insertPhoneNumber( *phoneIt );
414 else if ( dlg.exportOtherFields() )
415 addr.insertPhoneNumber( *phoneIt );
418 TDEABC::Address::List addresses = (*it).addresses();
419 TDEABC::Address::List::Iterator addrIt;
420 for ( addrIt = addresses.begin(); addrIt != addresses.end(); ++addrIt ) {
421 int type = (*addrIt).type();
423 if ( type & TDEABC::Address::Home && dlg.exportPrivateFields() )
424 addr.insertAddress( *addrIt );
425 else if ( type & TDEABC::Address::Work && dlg.exportBusinessFields() )
426 addr.insertAddress( *addrIt );
427 else if ( dlg.exportOtherFields() )
428 addr.insertAddress( *addrIt );
431 if ( dlg.exportOtherFields() )
432 addr.setCustoms( (*it).customs() );
434 if ( dlg.exportEncryptionKeys() ) {
435 addKey( addr, TDEABC::Key::PGP );
436 addKey( addr, TDEABC::Key::X509 );
445 void VCardXXPort::addKey( TDEABC::Addressee &addr, TDEABC::Key::Types type )
447 TQString fingerprint = addr.custom(
"KADDRESSBOOK",
448 (type == TDEABC::Key::PGP ?
"OPENPGPFP" :
"SMIMEFP") );
449 if ( fingerprint.isEmpty() )
452 GpgME::Context * context = GpgME::Context::createForProtocol( GpgME::Context::OpenPGP );
454 kdError() <<
"No context available" << endl;
458 context->setArmor(
false );
459 context->setTextMode(
false );
461 QGpgME::TQByteArrayDataProvider dataProvider;
462 GpgME::Data dataObj( &dataProvider );
463 GpgME::Error error = context->exportPublicKeys( fingerprint.latin1(), dataObj );
467 kdError() << error.asString() << endl;
473 key.setBinaryData( dataProvider.data() );
475 addr.insertKey( key );
480 VCardViewerDialog::VCardViewerDialog(
const TDEABC::Addressee::List &list,
481 TQWidget *parent,
const char *name )
482 : KDialogBase( Plain, i18n(
"Import vCard" ), Yes | No | Apply | Cancel, Yes,
483 parent, name, true, true, KStdGuiItem::no(), KStdGuiItem::yes() ),
486 TQFrame *page = plainPage();
487 TQVBoxLayout *layout =
new TQVBoxLayout( page, marginHint(), spacingHint() );
489 TQLabel *label =
new TQLabel( i18n(
"Do you want to import this contact in your address book?" ), page );
490 TQFont font = label->font();
491 font.setBold(
true );
492 label->setFont( font );
493 layout->addWidget( label );
495 mView =
new KPIM::AddresseeView( page );
496 mView->enableLinks( 0 );
497 mView->setVScrollBarMode( TQScrollView::Auto );
498 layout->addWidget( mView );
500 setButtonText( Apply, i18n(
"Import All..." ) );
502 mIt = mContacts.begin();
507 TDEABC::Addressee::List VCardViewerDialog::contacts()
const
512 void VCardViewerDialog::updateView()
514 mView->setAddressee( *mIt );
516 TDEABC::Addressee::List::Iterator it = mIt;
517 actionButton( Apply )->setEnabled( (++it) != mContacts.end() );
520 void VCardViewerDialog::slotUser1()
522 mIt = mContacts.remove( mIt );
524 if ( mIt == mContacts.end() )
530 void VCardViewerDialog::slotUser2()
534 if ( mIt == mContacts.end() )
540 void VCardViewerDialog::slotApply()
545 void VCardViewerDialog::slotCancel()
553 VCardExportSelectionDialog::VCardExportSelectionDialog( TQWidget *parent,
555 : KDialogBase( Plain, i18n(
"Select vCard Fields" ), Ok | Cancel, Ok,
556 parent, name, true, true )
558 TQFrame *page = plainPage();
560 TQVBoxLayout *layout =
new TQVBoxLayout( page, marginHint(), spacingHint() );
562 TQLabel *label =
new TQLabel( i18n(
"Select the fields which shall be exported in the vCard." ), page );
563 layout->addWidget( label );
565 mPrivateBox =
new TQCheckBox( i18n(
"Private fields" ), page );
566 layout->addWidget( mPrivateBox );
568 mBusinessBox =
new TQCheckBox( i18n(
"Business fields" ), page );
569 layout->addWidget( mBusinessBox );
571 mOtherBox =
new TQCheckBox( i18n(
"Other fields" ), page );
572 layout->addWidget( mOtherBox );
574 mEncryptionKeys =
new TQCheckBox( i18n(
"Encryption keys" ), page );
575 layout->addWidget( mEncryptionKeys );
577 TDEConfig config(
"kaddressbookrc" );
578 config.setGroup(
"XXPortVCard" );
580 mPrivateBox->setChecked( config.readBoolEntry(
"ExportPrivateFields",
true ) );
581 mBusinessBox->setChecked( config.readBoolEntry(
"ExportBusinessFields",
false ) );
582 mOtherBox->setChecked( config.readBoolEntry(
"ExportOtherFields",
false ) );
583 mEncryptionKeys->setChecked( config.readBoolEntry(
"ExportEncryptionKeys",
false ) );
586 VCardExportSelectionDialog::~VCardExportSelectionDialog()
588 TDEConfig config(
"kaddressbookrc" );
589 config.setGroup(
"XXPortVCard" );
591 config.writeEntry(
"ExportPrivateFields", mPrivateBox->isChecked() );
592 config.writeEntry(
"ExportBusinessFields", mBusinessBox->isChecked() );
593 config.writeEntry(
"ExportOtherFields", mOtherBox->isChecked() );
594 config.writeEntry(
"ExportEncryptionKeys", mEncryptionKeys->isChecked() );
597 bool VCardExportSelectionDialog::exportPrivateFields()
const
599 return mPrivateBox->isChecked();
602 bool VCardExportSelectionDialog::exportBusinessFields()
const
604 return mBusinessBox->isChecked();
607 bool VCardExportSelectionDialog::exportOtherFields()
const
609 return mOtherBox->isChecked();
612 bool VCardExportSelectionDialog::exportEncryptionKeys()
const
614 return mEncryptionKeys->isChecked();
617 #include "vcard_xxport.moc"