00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <qcheckbox.h>
00025 #include <qfile.h>
00026 #include <qfont.h>
00027 #include <qlabel.h>
00028 #include <qlayout.h>
00029 #include <qpushbutton.h>
00030 #include <qtextcodec.h>
00031
00032 #include <kabc/vcardconverter.h>
00033 #include <kdialogbase.h>
00034 #include <kfiledialog.h>
00035 #include <kio/netaccess.h>
00036 #include <klocale.h>
00037 #include <kmessagebox.h>
00038 #include <ktempfile.h>
00039 #include <kurl.h>
00040 #include <libkdepim/addresseeview.h>
00041
00042 #include "config.h"
00043
00044 #include "gpgmepp/context.h"
00045 #include "gpgmepp/data.h"
00046 #include "gpgmepp/key.h"
00047 #include "qgpgme/dataprovider.h"
00048
00049 #include "xxportmanager.h"
00050
00051 #include "vcard_xxport.h"
00052
00053 class VCardXXPortFactory : public KAB::XXPortFactory
00054 {
00055 public:
00056 KAB::XXPort *xxportObject( KABC::AddressBook *ab, QWidget *parent, const char *name )
00057 {
00058 return new VCardXXPort( ab, parent, name );
00059 }
00060 };
00061
00062 extern "C"
00063 {
00064 void *init_libkaddrbk_vcard_xxport()
00065 {
00066 return ( new VCardXXPortFactory() );
00067 }
00068 }
00069
00070 class VCardViewerDialog : public KDialogBase
00071 {
00072 public:
00073 VCardViewerDialog( const KABC::Addressee::List &list,
00074 QWidget *parent, const char *name = 0 );
00075
00076 KABC::Addressee::List contacts() const;
00077
00078 protected:
00079 void slotUser1();
00080 void slotUser2();
00081 void slotApply();
00082 void slotCancel();
00083
00084 private:
00085 void updateView();
00086
00087 KPIM::AddresseeView *mView;
00088
00089 KABC::Addressee::List mContacts;
00090 KABC::Addressee::List::Iterator mIt;
00091 };
00092
00093 class VCardExportSelectionDialog : public KDialogBase
00094 {
00095 public:
00096 VCardExportSelectionDialog( QWidget *parent, const char *name = 0 );
00097 ~VCardExportSelectionDialog();
00098
00099 bool exportPrivateFields() const;
00100 bool exportBusinessFields() const;
00101 bool exportOtherFields() const;
00102 bool exportEncryptionKeys() const;
00103
00104 private:
00105 QCheckBox *mPrivateBox;
00106 QCheckBox *mBusinessBox;
00107 QCheckBox *mOtherBox;
00108 QCheckBox *mEncryptionKeys;
00109 };
00110
00111 VCardXXPort::VCardXXPort( KABC::AddressBook *ab, QWidget *parent, const char *name )
00112 : KAB::XXPort( ab, parent, name )
00113 {
00114 createImportAction( i18n( "Import vCard..." ) );
00115 createExportAction( i18n( "Export vCard 2.1..." ), "v21" );
00116 createExportAction( i18n( "Export vCard 3.0..." ), "v30" );
00117 }
00118
00119 bool VCardXXPort::exportContacts( const KABC::AddresseeList &addrList, const QString &data )
00120 {
00121 KABC::VCardConverter converter;
00122 KURL url;
00123 KABC::AddresseeList list;
00124
00125 list = filterContacts( addrList );
00126
00127 bool ok = true;
00128 if ( list.isEmpty() ) {
00129 return ok;
00130 } else if ( list.count() == 1 ) {
00131 url = KFileDialog::getSaveURL( list[ 0 ].givenName() + "_" + list[ 0 ].familyName() + ".vcf" );
00132 if ( url.isEmpty() )
00133 return true;
00134
00135 if ( data == "v21" )
00136 ok = doExport( url, converter.createVCards( list, KABC::VCardConverter::v2_1 ) );
00137 else
00138 ok = doExport( url, converter.createVCards( list, KABC::VCardConverter::v3_0 ) );
00139 } else {
00140 QString msg = i18n( "You have selected a list of contacts, shall they be "
00141 "exported to several files?" );
00142
00143 switch ( KMessageBox::questionYesNo( parentWidget(), msg ) ) {
00144 case KMessageBox::Yes: {
00145 KURL baseUrl = KFileDialog::getExistingURL();
00146 if ( baseUrl.isEmpty() )
00147 return true;
00148
00149 KABC::AddresseeList::ConstIterator it;
00150 for ( it = list.begin(); it != list.end(); ++it ) {
00151 url = baseUrl.url() + "/" + (*it).givenName() + "_" + (*it).familyName() + ".vcf";
00152
00153 bool tmpOk;
00154 KABC::AddresseeList tmpList;
00155 tmpList.append( *it );
00156
00157 if ( data == "v21" )
00158 tmpOk = doExport( url, converter.createVCards( tmpList, KABC::VCardConverter::v2_1 ) );
00159 else
00160 tmpOk = doExport( url, converter.createVCards( tmpList, KABC::VCardConverter::v3_0 ) );
00161
00162 ok = ok && tmpOk;
00163 }
00164 break;
00165 }
00166 case KMessageBox::No:
00167 default: {
00168 url = KFileDialog::getSaveURL( "addressbook.vcf" );
00169 if ( url.isEmpty() )
00170 return true;
00171
00172 if ( data == "v21" )
00173 ok = doExport( url, converter.createVCards( list, KABC::VCardConverter::v2_1 ) );
00174 else
00175 ok = doExport( url, converter.createVCards( list, KABC::VCardConverter::v3_0 ) );
00176 }
00177 }
00178 }
00179
00180 return ok;
00181 }
00182
00183 KABC::AddresseeList VCardXXPort::importContacts( const QString& ) const
00184 {
00185 QString fileName;
00186 KABC::AddresseeList addrList;
00187 KURL::List urls;
00188
00189 if ( !XXPortManager::importData.isEmpty() )
00190 addrList = parseVCard( XXPortManager::importData );
00191 else {
00192 if ( XXPortManager::importURL.isEmpty() )
00193 urls = KFileDialog::getOpenURLs( QString::null, "*.vcf|vCards", parentWidget(),
00194 i18n( "Select vCard to Import" ) );
00195 else
00196 urls.append( XXPortManager::importURL );
00197
00198 if ( urls.count() == 0 )
00199 return addrList;
00200
00201 QString caption( i18n( "vCard Import Failed" ) );
00202 KURL::List::Iterator it;
00203 for ( it = urls.begin(); it != urls.end(); ++it ) {
00204 if ( KIO::NetAccess::download( *it, fileName, parentWidget() ) ) {
00205
00206 QFile file( fileName );
00207
00208 file.open( IO_ReadOnly );
00209 QByteArray rawData = file.readAll();
00210 file.close();
00211
00212 QString data = QString::fromUtf8( rawData.data(), rawData.size() );
00213 addrList += parseVCard( data );
00214
00215 KIO::NetAccess::removeTempFile( fileName );
00216 } else {
00217 QString text = i18n( "<qt>Unable to access <b>%1</b>.</qt>" );
00218 KMessageBox::error( parentWidget(), text.arg( (*it).url() ), caption );
00219 }
00220 }
00221
00222 if ( !XXPortManager::importURL.isEmpty() ) {
00223 if ( addrList.isEmpty() ) {
00224 KMessageBox::information( parentWidget(), i18n( "The vCard does not contain any contacts." ) );
00225 } else {
00226 VCardViewerDialog dlg( addrList, parentWidget() );
00227 dlg.exec();
00228 addrList = dlg.contacts();
00229 }
00230 }
00231 }
00232
00233 return addrList;
00234 }
00235
00236 KABC::AddresseeList VCardXXPort::parseVCard( const QString &data ) const
00237 {
00238 KABC::VCardConverter converter;
00239
00240 return converter.parseVCards( data );
00241 }
00242
00243 bool VCardXXPort::doExport( const KURL &url, const QString &data )
00244 {
00245 KTempFile tmpFile;
00246 tmpFile.setAutoDelete( true );
00247
00248 QTextStream stream( tmpFile.file() );
00249 stream.setEncoding( QTextStream::UnicodeUTF8 );
00250
00251 stream << data;
00252 tmpFile.close();
00253
00254 return KIO::NetAccess::upload( tmpFile.name(), url, parentWidget() );
00255 }
00256
00257 KABC::AddresseeList VCardXXPort::filterContacts( const KABC::AddresseeList &addrList )
00258 {
00259 KABC::AddresseeList list;
00260
00261 if ( addrList.isEmpty() )
00262 return addrList;
00263
00264 VCardExportSelectionDialog dlg( parentWidget() );
00265 if ( !dlg.exec() )
00266 return list;
00267
00268 KABC::AddresseeList::ConstIterator it;
00269 for ( it = addrList.begin(); it != addrList.end(); ++it ) {
00270 KABC::Addressee addr;
00271
00272 addr.setUid( (*it).uid() );
00273 addr.setFormattedName( (*it).formattedName() );
00274 addr.setPrefix( (*it).prefix() );
00275 addr.setGivenName( (*it).givenName() );
00276 addr.setAdditionalName( (*it).additionalName() );
00277 addr.setFamilyName( (*it).familyName() );
00278 addr.setSuffix( (*it).suffix() );
00279 addr.setNickName( (*it).nickName() );
00280 addr.setMailer( (*it).mailer() );
00281 addr.setTimeZone( (*it).timeZone() );
00282 addr.setGeo( (*it).geo() );
00283 addr.setProductId( (*it).productId() );
00284 addr.setSortString( (*it).sortString() );
00285 addr.setUrl( (*it).url() );
00286 addr.setSecrecy( (*it).secrecy() );
00287 addr.setSound( (*it).sound() );
00288 addr.setEmails( (*it).emails() );
00289 addr.setCategories( (*it).categories() );
00290
00291 if ( dlg.exportPrivateFields() ) {
00292 addr.setBirthday( (*it).birthday() );
00293 addr.setNote( (*it).note() );
00294 addr.setPhoto( (*it).photo() );
00295 }
00296
00297 if ( dlg.exportBusinessFields() ) {
00298 addr.setTitle( (*it).title() );
00299 addr.setRole( (*it).role() );
00300 addr.setOrganization( (*it).organization() );
00301
00302 addr.setLogo( (*it).logo() );
00303
00304 KABC::PhoneNumber::List phones = (*it).phoneNumbers( KABC::PhoneNumber::Work );
00305 KABC::PhoneNumber::List::Iterator phoneIt;
00306 for ( phoneIt = phones.begin(); phoneIt != phones.end(); ++phoneIt )
00307 addr.insertPhoneNumber( *phoneIt );
00308
00309 KABC::Address::List addresses = (*it).addresses( KABC::Address::Work );
00310 KABC::Address::List::Iterator addrIt;
00311 for ( addrIt = addresses.begin(); addrIt != addresses.end(); ++addrIt )
00312 addr.insertAddress( *addrIt );
00313 }
00314
00315 KABC::PhoneNumber::List phones = (*it).phoneNumbers();
00316 KABC::PhoneNumber::List::Iterator phoneIt;
00317 for ( phoneIt = phones.begin(); phoneIt != phones.end(); ++phoneIt ) {
00318 int type = (*phoneIt).type();
00319
00320 if ( type & KABC::PhoneNumber::Home && dlg.exportPrivateFields() )
00321 addr.insertPhoneNumber( *phoneIt );
00322 else if ( type & KABC::PhoneNumber::Work && dlg.exportBusinessFields() )
00323 addr.insertPhoneNumber( *phoneIt );
00324 else if ( dlg.exportOtherFields() )
00325 addr.insertPhoneNumber( *phoneIt );
00326 }
00327
00328 KABC::Address::List addresses = (*it).addresses();
00329 KABC::Address::List::Iterator addrIt;
00330 for ( addrIt = addresses.begin(); addrIt != addresses.end(); ++addrIt ) {
00331 int type = (*addrIt).type();
00332
00333 if ( type & KABC::Address::Home && dlg.exportPrivateFields() )
00334 addr.insertAddress( *addrIt );
00335 else if ( type & KABC::Address::Work && dlg.exportBusinessFields() )
00336 addr.insertAddress( *addrIt );
00337 else if ( dlg.exportOtherFields() )
00338 addr.insertAddress( *addrIt );
00339 }
00340
00341 if ( dlg.exportOtherFields() )
00342 addr.setCustoms( (*it).customs() );
00343
00344 if ( dlg.exportEncryptionKeys() ) {
00345 addKey( addr, KABC::Key::PGP );
00346 addKey( addr, KABC::Key::X509 );
00347 }
00348
00349 list.append( addr );
00350 }
00351
00352 return list;
00353 }
00354
00355 void VCardXXPort::addKey( KABC::Addressee &addr, KABC::Key::Types type )
00356 {
00357 QString fingerprint = addr.custom( "KADDRESSBOOK",
00358 (type == KABC::Key::PGP ? "OPENPGPFP" : "SMIMEFP") );
00359 if ( fingerprint.isEmpty() )
00360 return;
00361
00362 GpgME::Context * context = GpgME::Context::createForProtocol( GpgME::Context::OpenPGP );
00363 if ( !context ) {
00364 kdError() << "No context available" << endl;
00365 return;
00366 }
00367
00368 context->setArmor( false );
00369 context->setTextMode( false );
00370
00371 QGpgME::QByteArrayDataProvider dataProvider;
00372 GpgME::Data dataObj( &dataProvider );
00373 GpgME::Error error = context->exportPublicKeys( fingerprint.latin1(), dataObj );
00374
00375 if ( error ) {
00376 kdError() << error.asString() << endl;
00377 return;
00378 }
00379
00380 KABC::Key key;
00381 key.setType( type );
00382 key.setBinaryData( dataProvider.data() );
00383
00384 addr.insertKey( key );
00385 }
00386
00387
00388
00389 VCardViewerDialog::VCardViewerDialog( const KABC::Addressee::List &list,
00390 QWidget *parent, const char *name )
00391 : KDialogBase( Plain, i18n( "Import vCard" ), Yes | No | Apply | Cancel, Yes,
00392 parent, name, true, true, KStdGuiItem::no(), KStdGuiItem::yes() ),
00393 mContacts( list )
00394 {
00395 QFrame *page = plainPage();
00396 QVBoxLayout *layout = new QVBoxLayout( page, marginHint(), spacingHint() );
00397
00398 QLabel *label = new QLabel( i18n( "Do you want to import this contact in your address book?" ), page );
00399 QFont font = label->font();
00400 font.setBold( true );
00401 label->setFont( font );
00402 layout->addWidget( label );
00403
00404 mView = new KPIM::AddresseeView( page );
00405 mView->enableLinks( 0 );
00406 mView->setVScrollBarMode( QScrollView::Auto );
00407 layout->addWidget( mView );
00408
00409 setButtonText( Apply, i18n( "Import All..." ) );
00410
00411 mIt = mContacts.begin();
00412
00413 updateView();
00414 }
00415
00416 KABC::Addressee::List VCardViewerDialog::contacts() const
00417 {
00418 return mContacts;
00419 }
00420
00421 void VCardViewerDialog::updateView()
00422 {
00423 mView->setAddressee( *mIt );
00424
00425 KABC::Addressee::List::Iterator it = mIt;
00426 actionButton( Apply )->setEnabled( (++it) != mContacts.end() );
00427 }
00428
00429 void VCardViewerDialog::slotUser1()
00430 {
00431 mIt = mContacts.remove( mIt );
00432
00433 if ( mIt == mContacts.end() )
00434 slotApply();
00435
00436 updateView();
00437 }
00438
00439 void VCardViewerDialog::slotUser2()
00440 {
00441 mIt++;
00442
00443 if ( mIt == mContacts.end() )
00444 slotApply();
00445
00446 updateView();
00447 }
00448
00449 void VCardViewerDialog::slotApply()
00450 {
00451 QDialog::accept();
00452 }
00453
00454 void VCardViewerDialog::slotCancel()
00455 {
00456 mContacts.clear();
00457 QDialog::accept();
00458 }
00459
00460
00461
00462 VCardExportSelectionDialog::VCardExportSelectionDialog( QWidget *parent,
00463 const char *name )
00464 : KDialogBase( Plain, i18n( "Select vCard Fields" ), Ok | Cancel, Ok,
00465 parent, name, true, true )
00466 {
00467 QFrame *page = plainPage();
00468
00469 QVBoxLayout *layout = new QVBoxLayout( page, marginHint(), spacingHint() );
00470
00471 QLabel *label = new QLabel( i18n( "Select the fields which shall be exported in the vCard." ), page );
00472 layout->addWidget( label );
00473
00474 mPrivateBox = new QCheckBox( i18n( "Private fields" ), page );
00475 layout->addWidget( mPrivateBox );
00476
00477 mBusinessBox = new QCheckBox( i18n( "Business fields" ), page );
00478 layout->addWidget( mBusinessBox );
00479
00480 mOtherBox = new QCheckBox( i18n( "Other fields" ), page );
00481 layout->addWidget( mOtherBox );
00482
00483 mEncryptionKeys = new QCheckBox( i18n( "Encryption keys" ), page );
00484 layout->addWidget( mEncryptionKeys );
00485
00486 KConfig config( "kaddressbookrc" );
00487 config.setGroup( "XXPortVCard" );
00488
00489 mPrivateBox->setChecked( config.readBoolEntry( "ExportPrivateFields", true ) );
00490 mBusinessBox->setChecked( config.readBoolEntry( "ExportBusinessFields", false ) );
00491 mOtherBox->setChecked( config.readBoolEntry( "ExportOtherFields", false ) );
00492 mEncryptionKeys->setChecked( config.readBoolEntry( "ExportEncryptionKeys", false ) );
00493 }
00494
00495 VCardExportSelectionDialog::~VCardExportSelectionDialog()
00496 {
00497 KConfig config( "kaddressbookrc" );
00498 config.setGroup( "XXPortVCard" );
00499
00500 config.writeEntry( "ExportPrivateFields", mPrivateBox->isChecked() );
00501 config.writeEntry( "ExportBusinessFields", mBusinessBox->isChecked() );
00502 config.writeEntry( "ExportOtherFields", mOtherBox->isChecked() );
00503 config.writeEntry( "ExportEncryptionKeys", mEncryptionKeys->isChecked() );
00504 }
00505
00506 bool VCardExportSelectionDialog::exportPrivateFields() const
00507 {
00508 return mPrivateBox->isChecked();
00509 }
00510
00511 bool VCardExportSelectionDialog::exportBusinessFields() const
00512 {
00513 return mBusinessBox->isChecked();
00514 }
00515
00516 bool VCardExportSelectionDialog::exportOtherFields() const
00517 {
00518 return mOtherBox->isChecked();
00519 }
00520
00521 bool VCardExportSelectionDialog::exportEncryptionKeys() const
00522 {
00523 return mEncryptionKeys->isChecked();
00524 }
00525
00526 #include "vcard_xxport.moc"