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 TQt, and distribute the resulting executable, 00023 without including the source code for TQt 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 <tqfile.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, TQWidget *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 TQString& ) const 00063 { 00064 KABC::AddresseeList addrList; 00065 00066 TQString fileName = KFileDialog::getOpenFileName( TQDir::homeDirPath(), 00067 "text/x-ldif", 0 ); 00068 if ( fileName.isEmpty() ) 00069 return addrList; 00070 00071 TQFile file( fileName ); 00072 if ( !file.open( IO_ReadOnly ) ) { 00073 TQString 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 TQTextStream t( &file ); 00079 t.setEncoding( TQTextStream::Latin1 ); 00080 TQString wholeFile = t.read(); 00081 TQDateTime dtDefault = TQFileInfo(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 TQString& ) 00093 { 00094 KURL url = KFileDialog::getSaveURL( TQDir::homeDirPath() + "/addressbook.ldif", 00095 "text/x-ldif" ); 00096 if ( url.isEmpty() ) 00097 return true; 00098 00099 if( TQFileInfo(url.path()).exists() ) { 00100 if(KMessageBox::questionYesNo( parentWidget(), i18n("Do you want to overwrite file \"%1\"").arg( url.path()) ) == KMessageBox::No) 00101 return false; 00102 } 00103 00104 00105 if ( !url.isLocalFile() ) { 00106 KTempFile tmpFile; 00107 if ( tmpFile.status() != 0 ) { 00108 TQString txt = i18n( "<qt>Unable to open file <b>%1</b>.%2.</qt>" ); 00109 KMessageBox::error( parentWidget(), txt.arg( url.url() ) 00110 .arg( strerror( tmpFile.status() ) ) ); 00111 return false; 00112 } 00113 00114 doExport( tmpFile.file(), list ); 00115 tmpFile.close(); 00116 00117 return KIO::NetAccess::upload( tmpFile.name(), url, parentWidget() ); 00118 } else { 00119 TQString filename = url.path(); 00120 TQFile file( filename ); 00121 00122 if ( !file.open( IO_WriteOnly ) ) { 00123 TQString txt = i18n( "<qt>Unable to open file <b>%1</b>.</qt>" ); 00124 KMessageBox::error( parentWidget(), txt.arg( filename ) ); 00125 return false; 00126 } 00127 00128 doExport( &file, list ); 00129 file.close(); 00130 00131 return true; 00132 } 00133 } 00134 00135 void LDIFXXPort::doExport( TQFile *fp, const KABC::AddresseeList &list ) 00136 { 00137 TQString str; 00138 KABC::LDIFConverter::addresseeToLDIF( list, str ); 00139 00140 TQTextStream t( fp ); 00141 t.setEncoding( TQTextStream::UnicodeUTF8 ); 00142 t << str; 00143 } 00144 00145 #include "ldif_xxport.moc" 00146