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 <qfile.h>
00025
00026 #include <kfiledialog.h>
00027 #include <kio/netaccess.h>
00028 #include <klocale.h>
00029 #include <kmessagebox.h>
00030 #include <ktempfile.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( KABC::AddressBook *ab, QWidget *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 KABC::AddresseeList &list, const QString& )
00047 {
00048 KURL url = KFileDialog::getSaveURL( "addressbook.csv" );
00049 if ( url.isEmpty() )
00050 return true;
00051
00052 if ( !url.isLocalFile() ) {
00053 KTempFile tmpFile;
00054 if ( tmpFile.status() != 0 ) {
00055 QString txt = i18n( "<qt>Unable to open file <b>%1</b>.%2.</qt>" );
00056 KMessageBox::error( parentWidget(), txt.arg( url.url() )
00057 .arg( strerror( tmpFile.status() ) ) );
00058 return false;
00059 }
00060
00061 doExport( tmpFile.file(), list );
00062 tmpFile.close();
00063
00064 return KIO::NetAccess::upload( tmpFile.name(), url, parentWidget() );
00065 } else {
00066 QFile file( url.path() );
00067 if ( !file.open( IO_WriteOnly ) ) {
00068 QString txt = i18n( "<qt>Unable to open file <b>%1</b>.</qt>" );
00069 KMessageBox::error( parentWidget(), txt.arg( url.path() ) );
00070 return false;
00071 }
00072
00073 doExport( &file, list );
00074 file.close();
00075
00076 KMessageBox::information( parentWidget(), i18n( "The contacts have been exported successfully." ) );
00077
00078 return true;
00079 }
00080 }
00081
00082 KABC::AddresseeList CSVXXPort::importContacts( const QString& ) const
00083 {
00084 CSVImportDialog dlg( addressBook(), parentWidget() );
00085 if ( dlg.exec() )
00086 return dlg.contacts();
00087 else
00088 return KABC::AddresseeList();
00089 }
00090
00091 void CSVXXPort::doExport( QFile *fp, const KABC::AddresseeList &list )
00092 {
00093 QTextStream t( fp );
00094 t.setEncoding( QTextStream::Locale );
00095
00096 KABC::AddresseeList::ConstIterator iter;
00097 KABC::Field::List fields = addressBook()->fields();
00098 KABC::Field::List::Iterator fieldIter;
00099 bool first = true;
00100
00101
00102 for ( fieldIter = fields.begin(); fieldIter != fields.end(); ++fieldIter ) {
00103 if ( !first )
00104 t << ",";
00105
00106 t << "\"" << (*fieldIter)->label() << "\"";
00107 first = false;
00108 }
00109 t << "\n";
00110
00111
00112 KABC::Addressee addr;
00113 for ( iter = list.begin(); iter != list.end(); ++iter ) {
00114 addr = *iter;
00115 first = true;
00116
00117 for ( fieldIter = fields.begin(); fieldIter != fields.end(); ++fieldIter ) {
00118 if ( !first )
00119 t << ",";
00120
00121 t << "\"" << (*fieldIter)->value( addr ).replace( "\n", "\\n" ) << "\"";
00122 first = false;
00123 }
00124
00125 t << "\n";
00126 }
00127 }
00128
00129 #include "csv_xxport.moc"
|