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 <tqfile.h>
00037
00038 #include <tdefiledialog.h>
00039 #include <tdeio/netaccess.h>
00040 #include <tdelocale.h>
00041 #include <kmdcodec.h>
00042 #include <tdemessagebox.h>
00043 #include <tdetempfile.h>
00044 #include <kurl.h>
00045 #include <tdeabc/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( TDEABC::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
00061
00062 TDEABC::AddresseeList LDIFXXPort::importContacts( const TQString& ) const
00063 {
00064 TDEABC::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 TDEABC::LDIFConverter::LDIFToAddressee( wholeFile, addrList, dtDefault );
00085
00086 return addrList;
00087 }
00088
00089
00090
00091
00092 bool LDIFXXPort::exportContacts( const TDEABC::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 TDEIO::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 TDEABC::AddresseeList &list )
00136 {
00137 TQString str;
00138 TDEABC::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