kaddressbook Library API Documentation

ldif_xxport.cpp

00001 /*
00002     This file is part of KAddressbook.
00003     Copyright (c) 2000 - 2002 Oliver Strutynski <olistrut@gmx.de>
00004     Copyright (c) 2002 - 2003 Helge Deller <deller@kde.org>
00005                               Tobias Koenig <tokoe@kde.org>
00006 
00007     This program is free software; you can redistribute it and/or modify
00008     it under the terms of the GNU General Public License as published by
00009     the Free Software Foundation; either version 2 of the License, or
00010     (at your option) any later version.
00011 
00012     This program is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020 
00021     As a special exception, permission is given to link this program
00022     with any edition of Qt, and distribute the resulting executable,
00023     without including the source code for Qt in the source distribution.
00024 */
00025 
00026 /* 
00027     Description:
00028     The LDAP Data Interchange Format (LDIF) is a common ASCII-text based
00029     Internet interchange format. Most programs allow you to export data in
00030     LDIF format and e.g. Netscape and Mozilla store by default their
00031     Personal Address Book files in this format.
00032     This import and export filter reads and writes any LDIF version 1 files
00033     into your KDE Addressbook.
00034 */
00035 
00036 #include <qfile.h>
00037 
00038 #include <kfiledialog.h>
00039 #include <kio/netaccess.h>
00040 #include <klocale.h>
00041 #include <kmdcodec.h>
00042 #include <kmessagebox.h>
00043 #include <ktempfile.h>
00044 #include <kurl.h>
00045 #include <kabc/ldifconverter.h>
00046 
00047 #include <kdebug.h>
00048 
00049 #include "ldif_xxport.h"
00050 
00051 class LDIFXXPortFactory : public KAB::XXPortFactory
00052 {
00053   public:
00054     KAB::XXPort *xxportObject( KABC::AddressBook *ab, QWidget *parent, const char *name )
00055     {
00056       return new LDIFXXPort( ab, parent, name );
00057     }
00058 };
00059 
00060 extern "C"
00061 {
00062   void *init_libkaddrbk_ldif_xxport()
00063   {
00064     return ( new LDIFXXPortFactory() );
00065   }
00066 }
00067 
00068 
00069 LDIFXXPort::LDIFXXPort( KABC::AddressBook *ab, QWidget *parent, const char *name )
00070   : KAB::XXPort( ab, parent, name )
00071 {
00072   createImportAction( i18n( "Import LDIF Addressbook..." ) );
00073   createExportAction( i18n( "Export LDIF Addressbook..." ) );
00074 }
00075 
00076 /* import */
00077 
00078 KABC::AddresseeList LDIFXXPort::importContacts( const QString& ) const
00079 {
00080   KABC::AddresseeList addrList;
00081 
00082   QString fileName = KFileDialog::getOpenFileName( QDir::homeDirPath(), 
00083                       "*.[lL][dD][iI][fF]|" + i18n( "LDIF file (*.ldif)" ), 0 );
00084   if ( fileName.isEmpty() )
00085     return addrList;
00086 
00087   QFile file( fileName );
00088   if ( !file.open( IO_ReadOnly ) ) {
00089     QString msg = i18n( "<qt>Unable to open <b>%1</b> for reading.</qt>" );
00090     KMessageBox::error( parentWidget(), msg.arg( fileName ) );
00091     return addrList;
00092   }
00093 
00094   QTextStream t( &file );
00095   t.setEncoding( QTextStream::Latin1 );
00096   QString wholeFile = t.read();
00097   QDateTime dtDefault = QFileInfo(file).lastModified();
00098   file.close();
00099 
00100   KABC::LDIFConverter::LDIFToAddressee( wholeFile, addrList, dtDefault );
00101 
00102   return addrList;
00103 }
00104 
00105 
00106 /* export */
00107 
00108 bool LDIFXXPort::exportContacts( const KABC::AddresseeList &list, const QString& )
00109 {
00110   KURL url = KFileDialog::getSaveURL( QDir::homeDirPath() + "/addressbook.ldif", 
00111             "*.LDIF *.ldif *.Ldif|" + i18n( "LDIF file (*.ldif)" ) );
00112   if ( url.isEmpty() )
00113       return true;
00114 
00115   if ( !url.isLocalFile() ) {
00116     KTempFile tmpFile;
00117     if ( tmpFile.status() != 0 ) {
00118       QString txt = i18n( "<qt>Unable to open file <b>%1</b>.%2.</qt>" );
00119       KMessageBox::error( parentWidget(), txt.arg( url.url() )
00120                           .arg( strerror( tmpFile.status() ) ) );
00121       return false;
00122     }
00123 
00124     doExport( tmpFile.file(), list );
00125     tmpFile.close();
00126 
00127     return KIO::NetAccess::upload( tmpFile.name(), url, parentWidget() );
00128   } else {
00129     QString filename = url.path();
00130     QFile file( filename );
00131 
00132     if ( !file.open( IO_WriteOnly ) ) {
00133       QString txt = i18n( "<qt>Unable to open file <b>%1</b>.</qt>" );
00134       KMessageBox::error( parentWidget(), txt.arg( filename ) );
00135       return false;
00136     }
00137 
00138     doExport( &file, list );
00139     file.close();
00140 
00141     return true;
00142   }
00143 }
00144 
00145 void LDIFXXPort::doExport( QFile *fp, const KABC::AddresseeList &list )
00146 {
00147   QString str;
00148   KABC::LDIFConverter::addresseeToLDIF( list, str );
00149 
00150   QTextStream t( fp );
00151   t.setEncoding( QTextStream::UnicodeUTF8 );
00152   t << str;
00153 }
00154 
00155 #include "ldif_xxport.moc"
00156 
KDE Logo
This file is part of the documentation for kaddressbook Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:38:52 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003