kaddressbook
csv_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 #include <tqfile.h>
00025
00026 #include <tdefiledialog.h>
00027 #include <tdeio/netaccess.h>
00028 #include <tdelocale.h>
00029 #include <tdemessagebox.h>
00030 #include <tdetempfile.h>
00031 #include <kurl.h>
00032
00033 #include "csvimportdialog.h"
00034
00035 #include "csv_xxport.h"
00036
00037 K_EXPORT_KADDRESSBOOK_XXFILTER( libkaddrbk_csv_xxport, CSVXXPort )
00038
00039 CSVXXPort::CSVXXPort( TDEABC::AddressBook *ab, TQWidget *parent, const char *name )
00040 : KAB::XXPort( ab, parent, name )
00041 {
00042 createImportAction( i18n( "Import CSV List..." ) );
00043 createExportAction( i18n( "Export CSV List..." ) );
00044 }
00045
00046 bool CSVXXPort::exportContacts( const TDEABC::AddresseeList &list, const TQString& )
00047 {
00048 KURL url = KFileDialog::getSaveURL( "addressbook.csv" );
00049 if ( url.isEmpty() )
00050 return true;
00051
00052 if( TQFileInfo(url.path()).exists() ) {
00053 if(KMessageBox::questionYesNo( parentWidget(), i18n("Do you want to overwrite file \"%1\"").arg( url.path()) ) == KMessageBox::No)
00054 return false;
00055 }
00056
00057 if ( !url.isLocalFile() ) {
00058 KTempFile tmpFile;
00059 if ( tmpFile.status() != 0 ) {
00060 TQString txt = i18n( "<qt>Unable to open file <b>%1</b>.%2.</qt>" );
00061 KMessageBox::error( parentWidget(), txt.arg( url.url() )
00062 .arg( strerror( tmpFile.status() ) ) );
00063 return false;
00064 }
00065
00066 doExport( tmpFile.file(), list );
00067 tmpFile.close();
00068
00069 return TDEIO::NetAccess::upload( tmpFile.name(), url, parentWidget() );
00070 } else {
00071 TQFile file( url.path() );
00072 if ( !file.open( IO_WriteOnly ) ) {
00073 TQString txt = i18n( "<qt>Unable to open file <b>%1</b>.</qt>" );
00074 KMessageBox::error( parentWidget(), txt.arg( url.path() ) );
00075 return false;
00076 }
00077
00078 doExport( &file, list );
00079 file.close();
00080
00081 KMessageBox::information( parentWidget(), i18n( "The contacts have been exported successfully." ) );
00082
00083 return true;
00084 }
00085 }
00086
00087 TDEABC::AddresseeList CSVXXPort::importContacts( const TQString& ) const
00088 {
00089 CSVImportDialog dlg( addressBook(), parentWidget() );
00090 if ( dlg.exec() )
00091 return dlg.contacts();
00092 else
00093 return TDEABC::AddresseeList();
00094 }
00095
00096 void CSVXXPort::doExport( TQFile *fp, const TDEABC::AddresseeList &list )
00097 {
00098 TQTextStream t( fp );
00099 t.setEncoding( TQTextStream::Locale );
00100
00101 TDEABC::AddresseeList::ConstIterator iter;
00102 TDEABC::Field::List fields = addressBook()->fields();
00103 TDEABC::Field::List::Iterator fieldIter;
00104 bool first = true;
00105
00106
00107 for ( fieldIter = fields.begin(); fieldIter != fields.end(); ++fieldIter ) {
00108 if ( !first )
00109 t << ",";
00110
00111 t << "\"" << (*fieldIter)->label() << "\"";
00112 first = false;
00113 }
00114 t << "\n";
00115
00116
00117 TDEABC::Addressee addr;
00118 for ( iter = list.begin(); iter != list.end(); ++iter ) {
00119 addr = *iter;
00120 first = true;
00121
00122 for ( fieldIter = fields.begin(); fieldIter != fields.end(); ++fieldIter ) {
00123 if ( !first )
00124 t << ",";
00125
00126 t << "\"" << (*fieldIter)->value( addr ).replace( "\n", "\\n" ) << "\"";
00127 first = false;
00128 }
00129
00130 t << "\n";
00131 }
00132 }
00133
00134 #include "csv_xxport.moc"
|