kaddressbook Library API Documentation

csv_xxport.cpp

00001 /* 00002 This file is part of KAddressbook. 00003 Copyright (c) 2003 Tobias Koenig <tokoe@kde.org> 00004 00005 This program is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or 00008 (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with this program; if not, write to the Free Software 00017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00018 00019 As a special exception, permission is given to link this program 00020 with any edition of Qt, and distribute the resulting executable, 00021 without including the source code for Qt in the source distribution. 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 class CSVXXPortFactory : public KAB::XXPortFactory 00038 { 00039 public: 00040 KAB::XXPort *xxportObject( KABC::AddressBook *ab, QWidget *parent, const char *name ) 00041 { 00042 return new CSVXXPort( ab, parent, name ); 00043 } 00044 }; 00045 00046 extern "C" 00047 { 00048 void *init_libkaddrbk_csv_xxport() 00049 { 00050 return ( new CSVXXPortFactory() ); 00051 } 00052 } 00053 00054 00055 CSVXXPort::CSVXXPort( KABC::AddressBook *ab, QWidget *parent, const char *name ) 00056 : KAB::XXPort( ab, parent, name ) 00057 { 00058 createImportAction( i18n( "Import CSV List..." ) ); 00059 createExportAction( i18n( "Export CSV List..." ) ); 00060 } 00061 00062 bool CSVXXPort::exportContacts( const KABC::AddresseeList &list, const QString& ) 00063 { 00064 KURL url = KFileDialog::getSaveURL( "addressbook.csv" ); 00065 if ( url.isEmpty() ) 00066 return true; 00067 00068 if ( !url.isLocalFile() ) { 00069 KTempFile tmpFile; 00070 if ( tmpFile.status() != 0 ) { 00071 QString txt = i18n( "<qt>Unable to open file <b>%1</b>.%2.</qt>" ); 00072 KMessageBox::error( parentWidget(), txt.arg( url.url() ) 00073 .arg( strerror( tmpFile.status() ) ) ); 00074 return false; 00075 } 00076 00077 doExport( tmpFile.file(), list ); 00078 tmpFile.close(); 00079 00080 return KIO::NetAccess::upload( tmpFile.name(), url, parentWidget() ); 00081 } else { 00082 QFile file( url.path() ); 00083 if ( !file.open( IO_WriteOnly ) ) { 00084 QString txt = i18n( "<qt>Unable to open file <b>%1</b>.</qt>" ); 00085 KMessageBox::error( parentWidget(), txt.arg( url.path() ) ); 00086 return false; 00087 } 00088 00089 doExport( &file, list ); 00090 file.close(); 00091 00092 KMessageBox::information( parentWidget(), i18n( "The contacts have been exported successfully." ) ); 00093 00094 return true; 00095 } 00096 } 00097 00098 KABC::AddresseeList CSVXXPort::importContacts( const QString& ) const 00099 { 00100 CSVImportDialog dlg( addressBook(), parentWidget() ); 00101 if ( dlg.exec() ) 00102 return dlg.contacts(); 00103 else 00104 return KABC::AddresseeList(); 00105 } 00106 00107 void CSVXXPort::doExport( QFile *fp, const KABC::AddresseeList &list ) 00108 { 00109 QTextStream t( fp ); 00110 t.setEncoding( QTextStream::Locale ); 00111 00112 KABC::AddresseeList::ConstIterator iter; 00113 KABC::Field::List fields = addressBook()->fields(); 00114 KABC::Field::List::Iterator fieldIter; 00115 bool first = true; 00116 00117 // First output the column headings 00118 for ( fieldIter = fields.begin(); fieldIter != fields.end(); ++fieldIter ) { 00119 if ( !first ) 00120 t << ","; 00121 00122 t << "\"" << (*fieldIter)->label() << "\""; 00123 first = false; 00124 } 00125 t << "\n"; 00126 00127 // Then all the addressee objects 00128 KABC::Addressee addr; 00129 for ( iter = list.begin(); iter != list.end(); ++iter ) { 00130 addr = *iter; 00131 first = true; 00132 00133 for ( fieldIter = fields.begin(); fieldIter != fields.end(); ++fieldIter ) { 00134 if ( !first ) 00135 t << ","; 00136 00137 t << "\"" << (*fieldIter)->value( addr ).replace( "\n", "\\n" ) << "\""; 00138 first = false; 00139 } 00140 00141 t << "\n"; 00142 } 00143 } 00144 00145 #include "csv_xxport.moc"
KDE Logo
This file is part of the documentation for kaddressbook Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Jul 28 23:58:08 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003