• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdepim
  • Sitemap
  • Contact Us
 

kaddressbook

csv_xxport.cpp

Go to the documentation of this file.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 <QtCore/QFile>
00025 #include <QtCore/QTextStream>
00026 
00027 #include <kfiledialog.h>
00028 #include <kio/netaccess.h>
00029 #include <klocale.h>
00030 #include <kmessagebox.h>
00031 #include <ktemporaryfile.h>
00032 #include <kurl.h>
00033 
00034 #include "csvimportdialog.h"
00035 
00036 #include "csv_xxport.h"
00037 
00038 K_EXPORT_KADDRESSBOOK_XXFILTER( kaddrbk_csv_xxport, CSVXXPort )
00039 
00040 CSVXXPort::CSVXXPort( KABC::AddressBook *ab, QWidget *parent, const char *name )
00041   : KAB::XXPort( ab, parent, name )
00042 {
00043   createImportAction( i18n( "Import CSV List..." ) );
00044   createExportAction( i18n( "Export CSV List..." ) );
00045 }
00046 
00047 bool CSVXXPort::exportContacts( const KABC::AddresseeList &list, const QString& )
00048 {
00049   KUrl url = KFileDialog::getSaveUrl( KUrl("addressbook.csv") );
00050   if ( url.isEmpty() )
00051       return true;
00052 
00053   if ( !url.isLocalFile() ) {
00054     KTemporaryFile tmpFile;
00055     if ( !tmpFile.open() ) {
00056       QString txt = i18n("<qt>Unable to open file <b>%1</b></qt>", url.url());
00057       KMessageBox::error( parentWidget(), txt );
00058       return false;
00059     }
00060 
00061     doExport( &tmpFile, list );
00062     tmpFile.flush();
00063 
00064     return KIO::NetAccess::upload( tmpFile.fileName(), url, parentWidget() );
00065   } else {
00066     QFile file( url.path() );
00067     if ( !file.open( QIODevice::WriteOnly ) ) {
00068       QString txt = i18n( "<qt>Unable to open file <b>%1</b>.</qt>", url.path() );
00069       KMessageBox::error( parentWidget(), txt );
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::Addressee::List CSVXXPort::importContacts( const QString& ) const
00083 {
00084   CSVImportDialog dlg( addressBook(), parentWidget() );
00085   if ( dlg.exec() )
00086     return dlg.contacts();
00087   else
00088     return KABC::Addressee::List();
00089 }
00090 
00091 void CSVXXPort::doExport( QFile *fp, const KABC::AddresseeList &list )
00092 {
00093   QTextStream t( fp );
00094   t.setCodec( QTextCodec::codecForLocale() );
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   // First output the column headings
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   // Then all the addressee objects
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"

kaddressbook

Skip menu "kaddressbook"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kdepim

Skip menu "kdepim"
  • akonadi
  •   clients
  •   kabc
  •   kcal
  •   kcm
  • akregator
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt
  • kdgantt1
  • kjots
  • kleopatra
  • kmail
  • kmobiletools
  • knode
  • knotes
  • kontact
  • kontactinterfaces
  • korganizer
  •   korgac
  • kpilot
  • ktimetracker
  •   doc
  • libkdepim
  • libkholidays
  • libkleo
  • libkpgp
  • maildir
Generated for kdepim by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal