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