• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kaddressbook

  • sources
  • kde-4.14
  • kdepim
  • kaddressbook
  • xxport
  • vcard
vcard_xxport.cpp
Go to the documentation of this file.
1 /*
2  This file is part of KAddressBook.
3  Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 
20 #include "vcard_xxport.h"
21 
22 #include "vcardviewerdialog.h"
23 #include "vcardexportselectionwidget.h"
24 
25 #include "pimcommon/widgets/renamefiledialog.h"
26 
27 #include <kaddressbookgrantlee/widget/grantleecontactviewer.h>
28 
29 #ifdef QGPGME_FOUND
30 #include <gpgme++/context.h>
31 #include <gpgme++/data.h>
32 #include <gpgme++/key.h>
33 #include <qgpgme/dataprovider.h>
34 #endif // QGPGME_FOUND
35 
36 #include <KABC/VCardConverter>
37 
38 #include <KDebug>
39 #include <KDialog>
40 #include <KFileDialog>
41 #include <KLocalizedString>
42 #include <KMessageBox>
43 #include <KPushButton>
44 #include <KTemporaryFile>
45 #include <KUrl>
46 #include <KStandardGuiItem>
47 #include <KIO/NetAccess>
48 #include <KSharedConfig>
49 
50 #include <QtCore/QFile>
51 #include <QtCore/QPointer>
52 
53 
54 VCardXXPort::VCardXXPort( QWidget *parent )
55  : XXPort( parent )
56 {
57 }
58 
59 bool VCardXXPort::exportContacts( const ContactList &contacts, VCardExportSelectionWidget::ExportFields exportFields) const
60 {
61  KABC::VCardConverter converter;
62  KUrl url;
63 
64  const KABC::Addressee::List list = filterContacts( contacts.addressList(), exportFields );
65  if ( list.isEmpty() ) { // no contact selected
66  return true;
67  }
68 
69  bool ok = true;
70  if ( list.count() == 1 ) {
71  url = KFileDialog::getSaveUrl(
72  QString( list[ 0 ].givenName() +
73  QLatin1Char( QLatin1Char('_') ) +
74  list[ 0 ].familyName() +
75  QLatin1String( ".vcf" ) ) );
76  if ( url.isEmpty() ) { // user canceled export
77  return true;
78  }
79 
80  if ( option( QLatin1String("version") ) == QLatin1String("v21") ) {
81  ok = doExport( url, converter.exportVCards( list, KABC::VCardConverter::v2_1 ) );
82  } else if ( option( QLatin1String("version") ) == QLatin1String("v30") ) {
83  ok = doExport( url, converter.exportVCards( list, KABC::VCardConverter::v3_0 ) );
84  } else {
85  ok = doExport( url, converter.exportVCards( list, KABC::VCardConverter::v4_0 ) );
86  }
87  } else {
88  const int answer =
89  KMessageBox::questionYesNoCancel(
90  parentWidget(),
91  i18nc( "@info",
92  "You have selected a list of contacts, "
93  "shall they be exported to several files?" ),
94  QString(),
95  KGuiItem( i18nc( "@action:button", "Export to One File" ) ),
96  KGuiItem( i18nc( "@action:button", "Export to Several Files" ) ) );
97 
98  switch( answer ) {
99  case KMessageBox::No:
100  {
101  const KUrl baseUrl = KFileDialog::getExistingDirectoryUrl();
102  if ( baseUrl.isEmpty() ) {
103  return true; // user canceled export
104  }
105 
106  for ( int i = 0; i < list.count(); ++i ) {
107  const KABC::Addressee contact = list.at( i );
108 
109  url = baseUrl.url() + QLatin1Char('/') + contactFileName( contact ) + QLatin1String(".vcf");
110 
111  bool tmpOk = false;
112 
113  if ( option( QLatin1String("version") ) == QLatin1String("v21") ) {
114  tmpOk = doExport( url, converter.exportVCard( contact, KABC::VCardConverter::v2_1 ) );
115  } else if ( option( QLatin1String("version") ) == QLatin1String("v30") ) {
116  tmpOk = doExport( url, converter.exportVCard( contact, KABC::VCardConverter::v3_0 ) );
117  } else {
118  tmpOk = doExport( url, converter.exportVCard( contact, KABC::VCardConverter::v4_0 ) );
119  }
120 
121  ok = ok && tmpOk;
122  }
123  break;
124  }
125  case KMessageBox::Yes:
126  {
127  url = KFileDialog::getSaveUrl( KUrl( QLatin1String("addressbook.vcf") ) );
128  if ( url.isEmpty() ) {
129  return true; // user canceled export
130  }
131 
132  if ( option( QLatin1String("version") ) == QLatin1String("v21") ) {
133  ok = doExport( url, converter.exportVCards( list, KABC::VCardConverter::v2_1 ) );
134  } else if ( option( QLatin1String("version") ) == QLatin1String("v30") ) {
135  ok = doExport( url, converter.exportVCards( list, KABC::VCardConverter::v3_0 ) );
136  } else {
137  ok = doExport( url, converter.exportVCards( list, KABC::VCardConverter::v4_0 ) );
138  }
139  break;
140  }
141  case KMessageBox::Cancel:
142  default:
143  return true; // user canceled export
144  }
145  }
146 
147  return ok;
148 }
149 
150 ContactList VCardXXPort::importContacts() const
151 {
152  QString fileName;
153  ContactList contactList;
154  KABC::Addressee::List addrList;
155  KUrl::List urls;
156 
157  if ( !option( QLatin1String("importData") ).isEmpty() ) {
158  addrList = parseVCard( option( QLatin1String("importData") ).toUtf8() );
159  } else {
160  if ( !option( QLatin1String("importUrl") ).isEmpty() ) {
161  urls.append( KUrl( option( QLatin1String("importUrl") ) ) );
162  } else {
163  const QString filter = i18n( "*.vcf|vCard (*.vcf)\n*|all files (*)" );
164  urls =
165  KFileDialog::getOpenUrls(
166  KUrl(),
167  filter,
168  parentWidget(),
169  i18nc( "@title:window", "Select vCard to Import" ) );
170  }
171 
172  if ( urls.isEmpty() ) {
173  return contactList;
174  }
175 
176  const QString caption( i18nc( "@title:window", "vCard Import Failed" ) );
177  bool anyFailures = false;
178 
179  const int numberOfUrl( urls.count() );
180  for ( int i = 0; i < numberOfUrl; ++i ) {
181  const KUrl url = urls.at( i );
182 
183  if ( KIO::NetAccess::download( url, fileName, parentWidget() ) ) {
184 
185  QFile file( fileName );
186 
187  if ( file.open( QIODevice::ReadOnly ) ) {
188  const QByteArray data = file.readAll();
189  file.close();
190  if ( !data.isEmpty() ) {
191  addrList += parseVCard( data );
192  }
193 
194  KIO::NetAccess::removeTempFile( fileName );
195  } else {
196  const QString msg = i18nc(
197  "@info",
198  "<para>When trying to read the vCard, "
199  "there was an error opening the file <filename>%1</filename>:</para>"
200  "<para>%2</para>",
201  url.pathOrUrl(),
202  i18nc( "QFile", file.errorString().toLatin1() ) );
203  KMessageBox::error( parentWidget(), msg, caption );
204  anyFailures = true;
205  }
206  } else {
207  const QString msg = i18nc(
208  "@info",
209  "<para>Unable to access vCard:</para><para>%1</para>",
210  KIO::NetAccess::lastErrorString() );
211  KMessageBox::error( parentWidget(), msg, caption );
212  anyFailures = true;
213  }
214  }
215 
216  if ( !option( QLatin1String("importUrl") ).isEmpty() ) { // a vcard was passed via cmd
217  if ( addrList.isEmpty() ) {
218  if ( anyFailures && urls.count() > 1 ) {
219  KMessageBox::information(
220  parentWidget(),
221  i18nc( "@info", "No contacts were imported, due to errors with the vCards." ) );
222  } else if ( !anyFailures ) {
223  KMessageBox::information(
224  parentWidget(),
225  i18nc( "@info", "The vCard does not contain any contacts." ) );
226  }
227  } else {
228  QPointer<VCardViewerDialog> dlg = new VCardViewerDialog( addrList, parentWidget() );
229  if ( dlg->exec() && dlg ) {
230  addrList = dlg->contacts();
231  } else {
232  addrList.clear();
233  }
234  delete dlg;
235  }
236  }
237  }
238  contactList.setAddressList(addrList);
239  return contactList;
240 }
241 
242 KABC::Addressee::List VCardXXPort::parseVCard( const QByteArray &data ) const
243 {
244  KABC::VCardConverter converter;
245 
246  return converter.parseVCards( data );
247 }
248 
249 bool VCardXXPort::doExport( const KUrl &url, const QByteArray &data ) const
250 {
251  KUrl newUrl(url);
252  if ( newUrl.isLocalFile() && QFileInfo( newUrl.toLocalFile() ).exists() ) {
253  PimCommon::RenameFileDialog *dialog = new PimCommon::RenameFileDialog(newUrl, false, parentWidget());
254  PimCommon::RenameFileDialog::RenameFileDialogResult result = static_cast<PimCommon::RenameFileDialog::RenameFileDialogResult>(dialog->exec());
255  if ( result == PimCommon::RenameFileDialog::RENAMEFILE_RENAME ) {
256  newUrl = dialog->newName();
257  } else if (result == PimCommon::RenameFileDialog::RENAMEFILE_IGNORE) {
258  delete dialog;
259  return true;
260  }
261  delete dialog;
262  }
263 
264  KTemporaryFile tmpFile;
265  tmpFile.open();
266 
267  tmpFile.write( data );
268  tmpFile.flush();
269 
270  return KIO::NetAccess::upload( tmpFile.fileName(), newUrl, parentWidget() );
271 }
272 
273 KABC::Addressee::List VCardXXPort::filterContacts( const KABC::Addressee::List &addrList, VCardExportSelectionWidget::ExportFields exportFieldType ) const
274 {
275  KABC::Addressee::List list;
276 
277  if ( addrList.isEmpty() ) {
278  return addrList;
279  }
280 
281  KABC::Addressee::List::ConstIterator it;
282  KABC::Addressee::List::ConstIterator end( addrList.end() );
283  for ( it = addrList.begin(); it != end; ++it ) {
284  KABC::Addressee addr;
285 
286  addr.setUid( (*it).uid() );
287  addr.setFormattedName( (*it).formattedName() );
288 
289  bool addrDone = false;
290  if ( exportFieldType & VCardExportSelectionWidget::DiplayName ) { // output display name as N field
291  QString fmtName = (*it).formattedName();
292  QStringList splitNames = fmtName.split( QLatin1Char(' '), QString::SkipEmptyParts );
293  if ( splitNames.count() >= 2 ) {
294  addr.setPrefix( QString() );
295  addr.setGivenName( splitNames.takeFirst() );
296  addr.setFamilyName( splitNames.takeLast() );
297  addr.setAdditionalName( splitNames.join( QLatin1String(" ") ) );
298  addr.setSuffix( QString() );
299  addrDone = true;
300  }
301  }
302 
303  if ( !addrDone ) { // not wanted, or could not be split
304  addr.setPrefix( (*it).prefix() );
305  addr.setGivenName( (*it).givenName() );
306  addr.setAdditionalName( (*it).additionalName() );
307  addr.setFamilyName( (*it).familyName() );
308  addr.setSuffix( (*it).suffix() );
309  }
310 
311  addr.setNickName( (*it).nickName() );
312  addr.setMailer( (*it).mailer() );
313  addr.setTimeZone( (*it).timeZone() );
314  addr.setGeo( (*it).geo() );
315  addr.setProductId( (*it).productId() );
316  addr.setSortString( (*it).sortString() );
317  addr.setUrl( (*it).url() );
318  addr.setExtraUrlList( (*it).extraUrlList() );
319  addr.setSecrecy( (*it).secrecy() );
320  addr.setSound( (*it).sound() );
321  addr.setEmailList( (*it).emailList() );
322  addr.setCategories( (*it).categories() );
323  addr.setExtraSoundList( (*it).extraSoundList() );
324  addr.setGender( (*it).gender() );
325  addr.setLangs( (*it).langs() );
326  addr.setKind( (*it).kind() );
327  addr.setMembers( (*it).members() );
328  addr.setRelationShips( (*it).relationShips() );
329  addr.setSourcesUrlList( (*it).sourcesUrlList() );
330 
331  if ( exportFieldType & VCardExportSelectionWidget::Private ) {
332  addr.setBirthday( (*it).birthday() );
333  addr.setNote( (*it).note() );
334  }
335 
336  if ( exportFieldType & VCardExportSelectionWidget::Picture ) {
337  if ( exportFieldType & VCardExportSelectionWidget::Private ) {
338  addr.setPhoto( (*it).photo() );
339  addr.setExtraPhotoList( (*it).extraPhotoList() );
340  }
341 
342  if ( exportFieldType & VCardExportSelectionWidget::Business ) {
343  addr.setLogo( (*it).logo() );
344  addr.setExtraLogoList( (*it).extraLogoList() );
345  }
346  }
347 
348  if ( exportFieldType & VCardExportSelectionWidget::Business ) {
349  addr.setTitle( (*it).title() );
350  addr.setRole( (*it).role() );
351  addr.setOrganization( (*it).organization() );
352  addr.setDepartment( (*it).department() );
353 
354  KABC::PhoneNumber::List phones = (*it).phoneNumbers( KABC::PhoneNumber::Work );
355  KABC::PhoneNumber::List::Iterator phoneIt;
356  for ( phoneIt = phones.begin(); phoneIt != phones.end(); ++phoneIt ) {
357  addr.insertPhoneNumber( *phoneIt );
358  }
359 
360  KABC::Address::List addresses = (*it).addresses( KABC::Address::Work );
361  KABC::Address::List::Iterator addrIt;
362  for ( addrIt = addresses.begin(); addrIt != addresses.end(); ++addrIt ) {
363  addr.insertAddress( *addrIt );
364  }
365  }
366 
367  KABC::PhoneNumber::List phones = (*it).phoneNumbers();
368  KABC::PhoneNumber::List::Iterator phoneIt;
369  for ( phoneIt = phones.begin(); phoneIt != phones.end(); ++phoneIt ) {
370  int phoneType = (*phoneIt).type();
371 
372  if ( (phoneType & KABC::PhoneNumber::Home) && (exportFieldType & VCardExportSelectionWidget::Private) ) {
373  addr.insertPhoneNumber( *phoneIt );
374  } else if ( (phoneType & KABC::PhoneNumber::Work) && (exportFieldType & VCardExportSelectionWidget::Business) ) {
375  addr.insertPhoneNumber( *phoneIt );
376  } else if ( (exportFieldType & VCardExportSelectionWidget::Other) ) {
377  addr.insertPhoneNumber( *phoneIt );
378  }
379  }
380 
381  KABC::Address::List addresses = (*it).addresses();
382  KABC::Address::List::Iterator addrIt;
383  for ( addrIt = addresses.begin(); addrIt != addresses.end(); ++addrIt ) {
384  int addressType = (*addrIt).type();
385 
386  if ( (addressType & KABC::Address::Home) && exportFieldType & VCardExportSelectionWidget::Private ) {
387  addr.insertAddress( *addrIt );
388  } else if ( (addressType & KABC::Address::Work) && (exportFieldType & VCardExportSelectionWidget::Business) ) {
389  addr.insertAddress( *addrIt );
390  } else if ( exportFieldType & VCardExportSelectionWidget::Other ) {
391  addr.insertAddress( *addrIt );
392  }
393  }
394 
395  if ( exportFieldType & VCardExportSelectionWidget::Other ) {
396  addr.setCustoms( (*it).customs() );
397  }
398 
399  if ( exportFieldType & VCardExportSelectionWidget::Encryption ) {
400  addKey( addr, KABC::Key::PGP );
401  addKey( addr, KABC::Key::X509 );
402  }
403 
404  list.append( addr );
405  }
406 
407  return list;
408 }
409 
410 void VCardXXPort::addKey( KABC::Addressee &addr, KABC::Key::Type type ) const
411 {
412 #ifdef QGPGME_FOUND
413  const QString fingerprint = addr.custom( QLatin1String("KADDRESSBOOK"),
414  ( type == KABC::Key::PGP ? QLatin1String("OPENPGPFP") : QLatin1String("SMIMEFP") ) );
415  if ( fingerprint.isEmpty() ) {
416  return;
417  }
418 
419  GpgME::Context *context = GpgME::Context::createForProtocol( GpgME::OpenPGP );
420  if ( !context ) {
421  kError() << "No context available";
422  return;
423  }
424 
425  context->setArmor( false );
426  context->setTextMode( false );
427 
428  QGpgME::QByteArrayDataProvider dataProvider;
429  GpgME::Data dataObj( &dataProvider );
430  GpgME::Error error = context->exportPublicKeys( fingerprint.toLatin1(), dataObj );
431  delete context;
432 
433  if ( error ) {
434  kError() << error.asString();
435  return;
436  }
437 
438  KABC::Key key;
439  key.setType( type );
440  key.setBinaryData( dataProvider.data() );
441 
442  addr.insertKey( key );
443 #else
444  return;
445 #endif
446 }
VCardXXPort::exportContacts
bool exportContacts(const ContactList &contacts, VCardExportSelectionWidget::ExportFields exportFieldType) const
Exports the list of contacts.
Definition: vcard_xxport.cpp:59
QWidget
QByteArray
ContactList::addressList
KABC::Addressee::List addressList() const
Definition: contactlist.cpp:64
QIODevice::errorString
QString errorString() const
VCardViewerDialog
Definition: vcardviewerdialog.h:27
QString::split
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
QPointer
QByteArray::isEmpty
bool isEmpty() const
XXPort::parentWidget
QWidget * parentWidget() const
Returns the parent widget that can be used as parent for GUI components.
Definition: xxport.cpp:41
QStringList::join
QString join(const QString &separator) const
VCardXXPort::VCardXXPort
VCardXXPort(QWidget *parent=0)
Definition: vcard_xxport.cpp:54
VCardExportSelectionWidget::Business
Definition: vcardexportselectionwidget.h:33
QFile
QList::count
int count(const T &value) const
XXPort::contactFileName
QString contactFileName(const KABC::Addressee &contact) const
Returns a file name depending on the passed contact.
Definition: xxport.cpp:46
vcard_xxport.h
VCardExportSelectionWidget::Other
Definition: vcardexportselectionwidget.h:34
ContactList
Definition: contactlist.h:27
ContactList::setAddressList
void setAddressList(const KABC::Addressee::List &value)
Definition: contactlist.cpp:69
QString::isEmpty
bool isEmpty() const
QIODevice::readAll
QByteArray readAll()
QString
QFile::open
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
QStringList
vcardviewerdialog.h
QFileInfo
XXPort
The base class for all import/export modules.
Definition: xxport.h:35
XXPort::option
QString option(const QString &key) const
Returns the module specific option value for the given key.
Definition: xxport.cpp:36
VCardExportSelectionWidget::Private
Definition: vcardexportselectionwidget.h:32
QLatin1Char
vcardexportselectionwidget.h
QFile::close
virtual void close()
QList::takeLast
T takeLast()
QString::toLatin1
QByteArray toLatin1() const
VCardExportSelectionWidget::DiplayName
Definition: vcardexportselectionwidget.h:37
QList::takeFirst
T takeFirst()
QLatin1String
VCardExportSelectionWidget::Encryption
Definition: vcardexportselectionwidget.h:35
VCardExportSelectionWidget::Picture
Definition: vcardexportselectionwidget.h:36
VCardXXPort::importContacts
ContactList importContacts() const
Imports a list of contacts.
Definition: vcard_xxport.cpp:150
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:32:34 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kaddressbook

Skip menu "kaddressbook"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal