kaddressbook

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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 K_EXPORT_KADDRESSBOOK_XXFILTER( libkaddrbk_ldif_xxport, LDIFXXPort )
00052 
00053 LDIFXXPort::LDIFXXPort( KABC::AddressBook *ab, QWidget *parent, const char *name )
00054   : KAB::XXPort( ab, parent, name )
00055 {
00056   createImportAction( i18n( "Import LDIF Addressbook..." ) );
00057   createExportAction( i18n( "Export LDIF Addressbook..." ) );
00058 }
00059 
00060 /* import */
00061 
00062 KABC::AddresseeList LDIFXXPort::importContacts( const QString& ) const
00063 {
00064   KABC::AddresseeList addrList;
00065 
00066   QString fileName = KFileDialog::getOpenFileName( QDir::homeDirPath(),
00067                       "text/x-ldif", 0 );
00068   if ( fileName.isEmpty() )
00069     return addrList;
00070 
00071   QFile file( fileName );
00072   if ( !file.open( IO_ReadOnly ) ) {
00073     QString msg = i18n( "<qt>Unable to open <b>%1</b> for reading.</qt>" );
00074     KMessageBox::error( parentWidget(), msg.arg( fileName ) );
00075     return addrList;
00076   }
00077 
00078   QTextStream t( &file );
00079   t.setEncoding( QTextStream::Latin1 );
00080   QString wholeFile = t.read();
00081   QDateTime dtDefault = QFileInfo(file).lastModified();
00082   file.close();
00083 
00084   KABC::LDIFConverter::LDIFToAddressee( wholeFile, addrList, dtDefault );
00085 
00086   return addrList;
00087 }
00088 
00089 
00090 /* export */
00091 
00092 bool LDIFXXPort::exportContacts( const KABC::AddresseeList &list, const QString& )
00093 {
00094   KURL url = KFileDialog::getSaveURL( QDir::homeDirPath() + "/addressbook.ldif",
00095             "text/x-ldif" );
00096   if ( url.isEmpty() )
00097       return true;
00098 
00099   if ( !url.isLocalFile() ) {
00100     KTempFile tmpFile;
00101     if ( tmpFile.status() != 0 ) {
00102       QString txt = i18n( "<qt>Unable to open file <b>%1</b>.%2.</qt>" );
00103       KMessageBox::error( parentWidget(), txt.arg( url.url() )
00104                           .arg( strerror( tmpFile.status() ) ) );
00105       return false;
00106     }
00107 
00108     doExport( tmpFile.file(), list );
00109     tmpFile.close();
00110 
00111     return KIO::NetAccess::upload( tmpFile.name(), url, parentWidget() );
00112   } else {
00113     QString filename = url.path();
00114     QFile file( filename );
00115 
00116     if ( !file.open( IO_WriteOnly ) ) {
00117       QString txt = i18n( "<qt>Unable to open file <b>%1</b>.</qt>" );
00118       KMessageBox::error( parentWidget(), txt.arg( filename ) );
00119       return false;
00120     }
00121 
00122     doExport( &file, list );
00123     file.close();
00124 
00125     return true;
00126   }
00127 }
00128 
00129 void LDIFXXPort::doExport( QFile *fp, const KABC::AddresseeList &list )
00130 {
00131   QString str;
00132   KABC::LDIFConverter::addresseeToLDIF( list, str );
00133 
00134   QTextStream t( fp );
00135   t.setEncoding( QTextStream::UnicodeUTF8 );
00136   t << str;
00137 }
00138 
00139 #include "ldif_xxport.moc"
00140 
KDE Home | KDE Accessibility Home | Description of Access Keys