1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
|
/*
This file is part of KAddressbook.
Copyright (c) 2003 Tobias Koenig <tokoe@kde.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
As a special exception, permission is given to link this program
with any edition of TQt, and distribute the resulting executable,
without including the source code for TQt in the source distribution.
*/
#include <tqcheckbox.h>
#include <tqfile.h>
#include <tqfont.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqpushbutton.h>
#include <kabc/vcardconverter.h>
#include <kdialogbase.h>
#include <kfiledialog.h>
#include <kio/netaccess.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <ktempfile.h>
#include <kurl.h>
#include <kapplication.h>
#include <libkdepim/addresseeview.h>
#include "config.h" // ??
#include "gpgmepp/context.h"
#include "gpgmepp/data.h"
#include "gpgmepp/key.h"
#include "qgpgme/dataprovider.h"
#include "xxportmanager.h"
#include "vcard_xxport.h"
K_EXPORT_KADDRESSBOOK_XXFILTER( libkaddrbk_vcard_xxport, VCardXXPort )
class VCardViewerDialog : public KDialogBase
{
public:
VCardViewerDialog( const KABC::Addressee::List &list,
TQWidget *parent, const char *name = 0 );
KABC::Addressee::List contacts() const;
protected:
void slotUser1();
void slotUser2();
void slotApply();
void slotCancel();
private:
void updateView();
KPIM::AddresseeView *mView;
KABC::Addressee::List mContacts;
KABC::Addressee::List::Iterator mIt;
};
class VCardExportSelectionDialog : public KDialogBase
{
public:
VCardExportSelectionDialog( TQWidget *parent, const char *name = 0 );
~VCardExportSelectionDialog();
bool exportPrivateFields() const;
bool exportBusinessFields() const;
bool exportOtherFields() const;
bool exportEncryptionKeys() const;
private:
TQCheckBox *mPrivateBox;
TQCheckBox *mBusinessBox;
TQCheckBox *mOtherBox;
TQCheckBox *mEncryptionKeys;
};
VCardXXPort::VCardXXPort( KABC::AddressBook *ab, TQWidget *parent, const char *name )
: KAB::XXPort( ab, parent, name )
{
createImportAction( i18n( "Import vCard..." ) );
createExportAction( i18n( "Export vCard 2.1..." ), "v21" );
createExportAction( i18n( "Export vCard 3.0..." ), "v30" );
}
bool VCardXXPort::exportContacts( const KABC::AddresseeList &addrList, const TQString &data )
{
KABC::VCardConverter converter;
KURL url;
KABC::AddresseeList list;
list = filterContacts( addrList );
bool ok = true;
if ( list.isEmpty() ) {
return ok;
} else if ( list.count() == 1 ) {
url = KFileDialog::getSaveURL( list[ 0 ].givenName() + "_" + list[ 0 ].familyName() + ".vcf" );
if ( url.isEmpty() )
return true;
if ( data == "v21" )
#if defined(KABC_VCARD_ENCODING_FIX)
ok = doExport( url, converter.createVCardsRaw( list, KABC::VCardConverter::v2_1 ) );
else
ok = doExport( url, converter.createVCardsRaw( list, KABC::VCardConverter::v3_0 ) );
#else
ok = doExport( url, converter.createVCards( list, KABC::VCardConverter::v2_1 ) );
else
ok = doExport( url, converter.createVCards( list, KABC::VCardConverter::v3_0 ) );
#endif
} else {
TQString msg = i18n( "You have selected a list of contacts, shall they be "
"exported to several files?" );
switch ( KMessageBox::questionYesNo( parentWidget(), msg, TQString(), i18n("Export to Several Files"), i18n("Export to One File") ) ) {
case KMessageBox::Yes: {
KURL baseUrl = KFileDialog::getExistingURL();
if ( baseUrl.isEmpty() )
return true;
KABC::AddresseeList::ConstIterator it;
uint counter = 0;
for ( it = list.begin(); it != list.end(); ++it ) {
TQString testUrl;
if ( (*it).givenName().isEmpty() && (*it).familyName().isEmpty() )
testUrl = baseUrl.url() + "/" + (*it).organization();
else
testUrl = baseUrl.url() + "/" + (*it).givenName() + "_" + (*it).familyName();
if ( KIO::NetAccess::exists( testUrl + (counter == 0 ? "" : TQString::number( counter )) + ".vcf", false, parentWidget() ) ) {
counter++;
url = testUrl + TQString::number( counter ) + ".vcf";
} else
url = testUrl + ".vcf";
bool tmpOk;
KABC::AddresseeList tmpList;
tmpList.append( *it );
if ( data == "v21" )
#if defined(KABC_VCARD_ENCODING_FIX)
tmpOk = doExport( url, converter.createVCardsRaw( tmpList, KABC::VCardConverter::v2_1 ) );
else
tmpOk = doExport( url, converter.createVCardsRaw( tmpList, KABC::VCardConverter::v3_0 ) );
#else
tmpOk = doExport( url, converter.createVCards( tmpList, KABC::VCardConverter::v2_1 ) );
else
tmpOk = doExport( url, converter.createVCards( tmpList, KABC::VCardConverter::v3_0 ) );
#endif
ok = ok && tmpOk;
}
break;
}
case KMessageBox::No:
default: {
url = KFileDialog::getSaveURL( "addressbook.vcf" );
if ( url.isEmpty() )
return true;
if ( data == "v21" )
#if defined(KABC_VCARD_ENCODING_FIX)
ok = doExport( url, converter.createVCardsRaw( list, KABC::VCardConverter::v2_1 ) );
else
ok = doExport( url, converter.createVCardsRaw( list, KABC::VCardConverter::v3_0 ) );
#else
ok = doExport( url, converter.createVCards( list, KABC::VCardConverter::v2_1 ) );
else
ok = doExport( url, converter.createVCards( list, KABC::VCardConverter::v3_0 ) );
#endif
}
}
}
return ok;
}
KABC::AddresseeList VCardXXPort::importContacts( const TQString& ) const
{
TQString fileName;
KABC::AddresseeList addrList;
KURL::List urls;
if ( !XXPortManager::importData.isEmpty() ) {
#if defined(KABC_VCARD_ENCODING_FIX)
TQCString data = XXPortManager::importData.ascii();
addrList = parseVCard( data );
#else
addrList = parseVCard( XXPortManager::importData );
#endif
} else {
if ( XXPortManager::importURL.isEmpty() )
urls = KFileDialog::getOpenURLs( TQString(), "*.vcf|vCards", parentWidget(),
i18n( "Select vCard to Import" ) );
else
urls.append( XXPortManager::importURL );
if ( urls.count() == 0 )
return addrList;
TQString caption( i18n( "vCard Import Failed" ) );
bool anyFailures = false;
KURL::List::Iterator it;
for ( it = urls.begin(); it != urls.end(); ++it ) {
if ( KIO::NetAccess::download( *it, fileName, parentWidget() ) ) {
TQFile file( fileName );
if ( file.open( IO_ReadOnly ) ) {
#if defined(KABC_VCARD_ENCODING_FIX)
TQByteArray data = file.readAll();
file.close();
if ( data.size() > 0 )
addrList += parseVCard( data );
#else
TQByteArray rawData = file.readAll();
file.close();
if ( rawData.size() > 0 ) {
TQString vCardText;
// With version 3.0, vCards are encoded with UTF-8 by default. Otherwise, use tqfromLatin1()
// and hope that are fields are encoded correctly.
if ( TQString::tqfromLatin1( rawData ).lower().contains( "version:3.0" ) ) {
vCardText = TQString::fromUtf8( rawData );
} else {
vCardText = TQString::tqfromLatin1( rawData );
}
addrList += parseVCard( vCardText );
}
#endif
KIO::NetAccess::removeTempFile( fileName );
} else {
TQString text = i18n( "<qt>When trying to read the vCard, there was an error opening the file '%1': %2</qt>" );
text = text.tqarg( (*it).url() );
text = text.tqarg( kapp->translate( "TQFile",
TQString(file.errorString()).latin1() ) );
KMessageBox::error( parentWidget(), text, caption );
anyFailures = true;
}
} else {
TQString text = i18n( "<qt>Unable to access vCard: %1</qt>" );
text = text.tqarg( KIO::NetAccess::lastErrorString() );
KMessageBox::error( parentWidget(), text, caption );
anyFailures = true;
}
}
if ( !XXPortManager::importURL.isEmpty() ) { // a vcard was passed via cmd
if ( addrList.isEmpty() ) {
if ( anyFailures && urls.count() > 1 )
KMessageBox::information( parentWidget(),
i18n( "No contacts were imported, due to errors with the vCards." ) );
else if ( !anyFailures )
KMessageBox::information( parentWidget(), i18n( "The vCard does not contain any contacts." ) );
} else {
VCardViewerDialog dlg( addrList, parentWidget() );
dlg.exec();
addrList = dlg.contacts();
}
}
}
return addrList;
}
#if defined(KABC_VCARD_ENCODING_FIX)
KABC::AddresseeList VCardXXPort::parseVCard( const TQByteArray &data ) const
{
KABC::VCardConverter converter;
return converter.parseVCardsRaw( data.data() );
}
bool VCardXXPort::doExport( const KURL &url, const TQByteArray &data )
{
if( TQFileInfo(url.path()).exists() ) {
if(KMessageBox::questionYesNo( parentWidget(), i18n("Do you want to overwrite file \"%1\"").tqarg( url.path()) ) == KMessageBox::No)
return false;
}
KTempFile tmpFile;
tmpFile.setAutoDelete( true );
tmpFile.file()->writeBlock( data.data(), data.size() );
tmpFile.close();
return KIO::NetAccess::upload( tmpFile.name(), url, parentWidget() );
}
#else
KABC::AddresseeList VCardXXPort::parseVCard( const TQString &data ) const
{
KABC::VCardConverter converter;
return converter.parseVCards( data );
}
bool VCardXXPort::doExport( const KURL &url, const TQString &data )
{
if( TQFileInfo(url.path()).exists() ) {
if(KMessageBox::questionYesNo( parentWidget(), i18n("Do you want to overwrite file \"%1\"").tqarg( url.path()) ) == KMessageBox::No)
return false;
}
KTempFile tmpFile;
tmpFile.setAutoDelete( true );
TQTextStream stream( tmpFile.file() );
stream.setEncoding( TQTextStream::UnicodeUTF8 );
stream << data;
tmpFile.close();
return KIO::NetAccess::upload( tmpFile.name(), url, parentWidget() );
}
#endif
KABC::AddresseeList VCardXXPort::filterContacts( const KABC::AddresseeList &addrList )
{
KABC::AddresseeList list;
if ( addrList.isEmpty() )
return addrList;
VCardExportSelectionDialog dlg( parentWidget() );
if ( !dlg.exec() )
return list;
KABC::AddresseeList::ConstIterator it;
for ( it = addrList.begin(); it != addrList.end(); ++it ) {
KABC::Addressee addr;
addr.setUid( (*it).uid() );
addr.setFormattedName( (*it).formattedName() );
addr.setPrefix( (*it).prefix() );
addr.setGivenName( (*it).givenName() );
addr.setAdditionalName( (*it).additionalName() );
addr.setFamilyName( (*it).familyName() );
addr.setSuffix( (*it).suffix() );
addr.setNickName( (*it).nickName() );
addr.setMailer( (*it).mailer() );
addr.setTimeZone( (*it).timeZone() );
addr.setGeo( (*it).geo() );
addr.setProductId( (*it).productId() );
addr.setSortString( (*it).sortString() );
addr.setUrl( (*it).url() );
addr.setSecrecy( (*it).secrecy() );
addr.setSound( (*it).sound() );
addr.setEmails( (*it).emails() );
addr.setCategories( (*it).categories() );
if ( dlg.exportPrivateFields() ) {
addr.setBirthday( (*it).birthday() );
addr.setNote( (*it).note() );
addr.setPhoto( (*it).photo() );
}
if ( dlg.exportBusinessFields() ) {
addr.setTitle( (*it).title() );
addr.setRole( (*it).role() );
addr.setOrganization( (*it).organization() );
addr.setLogo( (*it).logo() );
KABC::PhoneNumber::List phones = (*it).phoneNumbers( KABC::PhoneNumber::Work );
KABC::PhoneNumber::List::Iterator phoneIt;
for ( phoneIt = phones.begin(); phoneIt != phones.end(); ++phoneIt )
addr.insertPhoneNumber( *phoneIt );
KABC::Address::List addresses = (*it).addresses( KABC::Address::Work );
KABC::Address::List::Iterator addrIt;
for ( addrIt = addresses.begin(); addrIt != addresses.end(); ++addrIt )
addr.insertAddress( *addrIt );
}
KABC::PhoneNumber::List phones = (*it).phoneNumbers();
KABC::PhoneNumber::List::Iterator phoneIt;
for ( phoneIt = phones.begin(); phoneIt != phones.end(); ++phoneIt ) {
int type = (*phoneIt).type();
if ( type & KABC::PhoneNumber::Home && dlg.exportPrivateFields() )
addr.insertPhoneNumber( *phoneIt );
else if ( type & KABC::PhoneNumber::Work && dlg.exportBusinessFields() )
addr.insertPhoneNumber( *phoneIt );
else if ( dlg.exportOtherFields() )
addr.insertPhoneNumber( *phoneIt );
}
KABC::Address::List addresses = (*it).addresses();
KABC::Address::List::Iterator addrIt;
for ( addrIt = addresses.begin(); addrIt != addresses.end(); ++addrIt ) {
int type = (*addrIt).type();
if ( type & KABC::Address::Home && dlg.exportPrivateFields() )
addr.insertAddress( *addrIt );
else if ( type & KABC::Address::Work && dlg.exportBusinessFields() )
addr.insertAddress( *addrIt );
else if ( dlg.exportOtherFields() )
addr.insertAddress( *addrIt );
}
if ( dlg.exportOtherFields() )
addr.setCustoms( (*it).customs() );
if ( dlg.exportEncryptionKeys() ) {
addKey( addr, KABC::Key::PGP );
addKey( addr, KABC::Key::X509 );
}
list.append( addr );
}
return list;
}
void VCardXXPort::addKey( KABC::Addressee &addr, KABC::Key::Types type )
{
TQString fingerprint = addr.custom( "KADDRESSBOOK",
(type == KABC::Key::PGP ? "OPENPGPFP" : "SMIMEFP") );
if ( fingerprint.isEmpty() )
return;
GpgME::Context * context = GpgME::Context::createForProtocol( GpgME::Context::OpenPGP );
if ( !context ) {
kdError() << "No context available" << endl;
return;
}
context->setArmor( false );
context->setTextMode( false );
QGpgME::TQByteArrayDataProvider dataProvider;
GpgME::Data dataObj( &dataProvider );
GpgME::Error error = context->exportPublicKeys( fingerprint.latin1(), dataObj );
delete context;
if ( error ) {
kdError() << error.asString() << endl;
return;
}
KABC::Key key;
key.setType( type );
key.setBinaryData( dataProvider.data() );
addr.insertKey( key );
}
// ---------- VCardViewer Dialog ---------------- //
VCardViewerDialog::VCardViewerDialog( const KABC::Addressee::List &list,
TQWidget *parent, const char *name )
: KDialogBase( Plain, i18n( "Import vCard" ), Yes | No | Apply | Cancel, Yes,
parent, name, true, true, KStdGuiItem::no(), KStdGuiItem::yes() ),
mContacts( list )
{
TQFrame *page = plainPage();
TQVBoxLayout *tqlayout = new TQVBoxLayout( page, marginHint(), spacingHint() );
TQLabel *label = new TQLabel( i18n( "Do you want to import this contact in your address book?" ), page );
TQFont font = label->font();
font.setBold( true );
label->setFont( font );
tqlayout->addWidget( label );
mView = new KPIM::AddresseeView( page );
mView->enableLinks( 0 );
mView->setVScrollBarMode( TQScrollView::Auto );
tqlayout->addWidget( mView );
setButtonText( Apply, i18n( "Import All..." ) );
mIt = mContacts.begin();
updateView();
}
KABC::Addressee::List VCardViewerDialog::contacts() const
{
return mContacts;
}
void VCardViewerDialog::updateView()
{
mView->setAddressee( *mIt );
KABC::Addressee::List::Iterator it = mIt;
actionButton( Apply )->setEnabled( (++it) != mContacts.end() );
}
void VCardViewerDialog::slotUser1()
{
mIt = mContacts.remove( mIt );
if ( mIt == mContacts.end() )
slotApply();
updateView();
}
void VCardViewerDialog::slotUser2()
{
mIt++;
if ( mIt == mContacts.end() )
slotApply();
updateView();
}
void VCardViewerDialog::slotApply()
{
TQDialog::accept();
}
void VCardViewerDialog::slotCancel()
{
mContacts.clear();
TQDialog::accept();
}
// ---------- VCardExportSelection Dialog ---------------- //
VCardExportSelectionDialog::VCardExportSelectionDialog( TQWidget *parent,
const char *name )
: KDialogBase( Plain, i18n( "Select vCard Fields" ), Ok | Cancel, Ok,
parent, name, true, true )
{
TQFrame *page = plainPage();
TQVBoxLayout *tqlayout = new TQVBoxLayout( page, marginHint(), spacingHint() );
TQLabel *label = new TQLabel( i18n( "Select the fields which shall be exported in the vCard." ), page );
tqlayout->addWidget( label );
mPrivateBox = new TQCheckBox( i18n( "Private fields" ), page );
tqlayout->addWidget( mPrivateBox );
mBusinessBox = new TQCheckBox( i18n( "Business fields" ), page );
tqlayout->addWidget( mBusinessBox );
mOtherBox = new TQCheckBox( i18n( "Other fields" ), page );
tqlayout->addWidget( mOtherBox );
mEncryptionKeys = new TQCheckBox( i18n( "Encryption keys" ), page );
tqlayout->addWidget( mEncryptionKeys );
KConfig config( "kaddressbookrc" );
config.setGroup( "XXPortVCard" );
mPrivateBox->setChecked( config.readBoolEntry( "ExportPrivateFields", true ) );
mBusinessBox->setChecked( config.readBoolEntry( "ExportBusinessFields", false ) );
mOtherBox->setChecked( config.readBoolEntry( "ExportOtherFields", false ) );
mEncryptionKeys->setChecked( config.readBoolEntry( "ExportEncryptionKeys", false ) );
}
VCardExportSelectionDialog::~VCardExportSelectionDialog()
{
KConfig config( "kaddressbookrc" );
config.setGroup( "XXPortVCard" );
config.writeEntry( "ExportPrivateFields", mPrivateBox->isChecked() );
config.writeEntry( "ExportBusinessFields", mBusinessBox->isChecked() );
config.writeEntry( "ExportOtherFields", mOtherBox->isChecked() );
config.writeEntry( "ExportEncryptionKeys", mEncryptionKeys->isChecked() );
}
bool VCardExportSelectionDialog::exportPrivateFields() const
{
return mPrivateBox->isChecked();
}
bool VCardExportSelectionDialog::exportBusinessFields() const
{
return mBusinessBox->isChecked();
}
bool VCardExportSelectionDialog::exportOtherFields() const
{
return mOtherBox->isChecked();
}
bool VCardExportSelectionDialog::exportEncryptionKeys() const
{
return mEncryptionKeys->isChecked();
}
#include "vcard_xxport.moc"
|