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

kaddressbook

  • sources
  • kde-4.12
  • 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 <KCodecs>
43 #include <KFileDialog>
44 #include <KLocale>
45 #include <KMessageBox>
46 #include <KTemporaryFile>
47 #include <KUrl>
48 #include <KIO/NetAccess>
49 
50 #include <QtCore/QFile>
51 #include <QtCore/QTextStream>
52 
53 void doExport( QFile *file, const KABC::Addressee::List &list )
54 {
55  QString data;
56  KABC::LDIFConverter::addresseeToLDIF( list, data );
57 
58  QTextStream stream( file );
59  stream.setCodec( "UTF-8" );
60  stream << data;
61 }
62 
63 LDIFXXPort::LDIFXXPort( QWidget *parentWidget )
64  : XXPort( parentWidget )
65 {
66 }
67 
68 KABC::Addressee::List LDIFXXPort::importContacts() const
69 {
70  KABC::Addressee::List contacts;
71 
72  const QString fileName = KFileDialog::getOpenFileName( QDir::homePath(), QLatin1String("text/x-ldif"), 0 );
73  if ( fileName.isEmpty() ) {
74  return contacts;
75  }
76 
77  QFile file( fileName );
78  if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) ) {
79  const QString msg = i18n( "<qt>Unable to open <b>%1</b> for reading.</qt>", fileName );
80  KMessageBox::error( parentWidget(), msg );
81  return contacts;
82  }
83 
84  QTextStream stream( &file );
85  stream.setCodec( "ISO 8859-1" );
86 
87  const QString wholeFile = stream.readAll();
88  const QDateTime dtDefault = QFileInfo( file ).lastModified();
89  file.close();
90 
91  KABC::LDIFConverter::LDIFToAddressee( wholeFile, contacts, dtDefault );
92 
93  return contacts;
94 }
95 
96 bool LDIFXXPort::exportContacts( const KABC::Addressee::List &list ) 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  //TODO fix export in network as other export function
135  QFile file( fileName );
136 
137  if ( !file.open( QIODevice::WriteOnly ) ) {
138  QString txt = i18n( "<qt>Unable to open file <b>%1</b>.</qt>", fileName );
139  KMessageBox::error( parentWidget(), txt );
140  return false;
141  }
142 
143  doExport( &file, list );
144  file.close();
145 
146  return true;
147  }
148 }
LDIFXXPort::LDIFXXPort
LDIFXXPort(QWidget *parent=0)
Definition: ldif_xxport.cpp:63
LDIFXXPort::importContacts
KABC::Addressee::List importContacts() const
Imports a list of contacts.
Definition: ldif_xxport.cpp:68
LDIFXXPort::exportContacts
bool exportContacts(const KABC::Addressee::List &contacts) const
Exports the list of contacts.
Definition: ldif_xxport.cpp:96
XXPort::parentWidget
QWidget * parentWidget() const
Returns the parent widget that can be used as parent for GUI components.
Definition: xxport.cpp:51
doExport
void doExport(QFile *file, const KABC::Addressee::List &list)
Definition: ldif_xxport.cpp:53
XXPort
The base class for all import/export modules.
Definition: xxport.h:32
ldif_xxport.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:55:51 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

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