• 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
  • ldif
ldif_xxport.cpp
Go to the documentation of this file.
1 /*
2  This file is part of KAddressbook.
3  Copyright (c) 2000 - 2002 Oliver Strutynski <olistrut@gmx.de>
4  Copyright (c) 2002 - 2003 Helge Deller <deller@kde.org>
5  Tobias Koenig <tokoe@kde.org>
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License along
18  with this program; if not, write to the Free Software Foundation, Inc.,
19  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 
21  As a special exception, permission is given to link this program
22  with any edition of Qt, and distribute the resulting executable,
23  without including the source code for Qt in the source distribution.
24 */
25 
26 /*
27  Description:
28  The LDAP Data Interchange Format (LDIF) is a common ASCII-text based
29  Internet interchange format. Most programs allow you to export data in
30  LDIF format and e.g. Netscape and Mozilla store by default their
31  Personal Address Book files in this format.
32  This import and export filter reads and writes any LDIF version 1 files
33  into your KDE Addressbook.
34 */
35 
36 #include "ldif_xxport.h"
37 
38 #include "pimcommon/widgets/renamefiledialog.h"
39 
40 #include <KABC/LDIFConverter>
41 
42 #include <KFileDialog>
43 #include <KLocalizedString>
44 #include <KMessageBox>
45 #include <KTemporaryFile>
46 #include <KUrl>
47 #include <KIO/NetAccess>
48 
49 #include <QtCore/QFile>
50 #include <QtCore/QTextStream>
51 
52 void doExport( QFile *file, const ContactList &list )
53 {
54  QString data;
55  KABC::LDIFConverter::addresseeAndContactGroupToLDIF( list.addressList(), list.contactGroupList(), data );
56 
57  QTextStream stream( file );
58  stream.setCodec( "UTF-8" );
59  stream << data;
60 }
61 
62 LDIFXXPort::LDIFXXPort( QWidget *parentWidget )
63  : XXPort( parentWidget )
64 {
65 }
66 
67 ContactList LDIFXXPort::importContacts() const
68 {
69  ContactList contactList;
70  const QString fileName = KFileDialog::getOpenFileName( QDir::homePath(), QLatin1String("text/x-ldif"), 0 );
71  if ( fileName.isEmpty() ) {
72  return contactList;
73  }
74 
75  QFile file( fileName );
76  if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) ) {
77  const QString msg = i18n( "<qt>Unable to open <b>%1</b> for reading.</qt>", fileName );
78  KMessageBox::error( parentWidget(), msg );
79  return contactList;
80  }
81 
82  QTextStream stream( &file );
83  stream.setCodec( "ISO 8859-1" );
84 
85  const QString wholeFile = stream.readAll();
86  const QDateTime dtDefault = QFileInfo( file ).lastModified();
87  file.close();
88  KABC::ContactGroup::List lstGroup;
89  KABC::Addressee::List lstAddresses;
90  KABC::LDIFConverter::LDIFToAddressee( wholeFile, lstAddresses, lstGroup, dtDefault );
91  contactList.setAddressList(lstAddresses);
92  contactList.setContactGroupList(lstGroup);
93  return contactList;
94 }
95 
96 bool LDIFXXPort::exportContacts( const ContactList &list, VCardExportSelectionWidget::ExportFields ) const
97 {
98  const KUrl url =
99  KFileDialog::getSaveUrl( KUrl( QDir::homePath() + QLatin1String("/addressbook.ldif") ), QLatin1String("text/x-ldif") );
100  if ( url.isEmpty() ) {
101  return true;
102  }
103 
104  if ( !url.isLocalFile() ) {
105  KTemporaryFile tmpFile;
106  if ( !tmpFile.open() ) {
107  const QString msg = i18n( "<qt>Unable to open file <b>%1</b></qt>", url.url() );
108  KMessageBox::error( parentWidget(), msg );
109  return false;
110  }
111 
112  doExport( &tmpFile, list );
113  tmpFile.flush();
114 
115  return KIO::NetAccess::upload( tmpFile.fileName(), url, parentWidget() );
116  } else {
117  QString fileName = url.toLocalFile();
118 
119  if ( QFileInfo( fileName ).exists() ) {
120  if ( url.isLocalFile() && QFileInfo( url.toLocalFile() ).exists() ) {
121  PimCommon::RenameFileDialog::RenameFileDialogResult result = PimCommon::RenameFileDialog::RENAMEFILE_IGNORE;
122  PimCommon::RenameFileDialog *dialog = new PimCommon::RenameFileDialog(url, false, parentWidget());
123  result = static_cast<PimCommon::RenameFileDialog::RenameFileDialogResult>(dialog->exec());
124  if ( result == PimCommon::RenameFileDialog::RENAMEFILE_RENAME ) {
125  fileName = dialog->newName().toLocalFile();
126  } else if (result == PimCommon::RenameFileDialog::RENAMEFILE_IGNORE) {
127  delete dialog;
128  return true;
129  }
130  delete dialog;
131  }
132  }
133 
134  QFile file( fileName );
135 
136  if ( !file.open( QIODevice::WriteOnly ) ) {
137  QString txt = i18n( "<qt>Unable to open file <b>%1</b>.</qt>", fileName );
138  KMessageBox::error( parentWidget(), txt );
139  return false;
140  }
141 
142  doExport( &file, list );
143  file.close();
144 
145  return true;
146  }
147 }
QTextStream::setCodec
void setCodec(QTextCodec *codec)
LDIFXXPort::LDIFXXPort
LDIFXXPort(QWidget *parent=0)
Definition: ldif_xxport.cpp:62
QWidget
ContactList::addressList
KABC::Addressee::List addressList() const
Definition: contactlist.cpp:64
XXPort::parentWidget
QWidget * parentWidget() const
Returns the parent widget that can be used as parent for GUI components.
Definition: xxport.cpp:41
QDir::homePath
QString homePath()
QFile
QTextStream
LDIFXXPort::exportContacts
bool exportContacts(const ContactList &contacts, VCardExportSelectionWidget::ExportFields) const
Exports the list of contacts.
Definition: ldif_xxport.cpp:96
LDIFXXPort::importContacts
ContactList importContacts() const
Imports a list of contacts.
Definition: ldif_xxport.cpp:67
ContactList
Definition: contactlist.h:27
ContactList::setAddressList
void setAddressList(const KABC::Addressee::List &value)
Definition: contactlist.cpp:69
QString::isEmpty
bool isEmpty() const
ContactList::setContactGroupList
void setContactGroupList(const KABC::ContactGroup::List &value)
Definition: contactlist.cpp:59
QString
QFileInfo::lastModified
QDateTime lastModified() const
QFile::open
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
QFileInfo
XXPort
The base class for all import/export modules.
Definition: xxport.h:35
QFile::close
virtual void close()
QLatin1String
ContactList::contactGroupList
KABC::ContactGroup::List contactGroupList() const
Definition: contactlist.cpp:54
ldif_xxport.h
QTextStream::readAll
QString readAll()
QDateTime
doExport
void doExport(QFile *file, const ContactList &list)
Definition: ldif_xxport.cpp:52
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