kaddressbook
ldif_xxport.cpp00001
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 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
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
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
|