ldif_xxport.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
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
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
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
This file is part of the documentation for kaddressbook Library Version 3.2.2.