kaddressbook Library API Documentation

vcard_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 #include <qfont.h>
00026 #include <qlabel.h>
00027 #include <qlayout.h>
00028 
00029 #include <kabc/vcardconverter.h>
00030 #include <kdialogbase.h>
00031 #include <kfiledialog.h>
00032 #include <kio/netaccess.h>
00033 #include <klocale.h>
00034 #include <kmessagebox.h>
00035 #include <ktempfile.h>
00036 #include <kurl.h>
00037 #include <libkdepim/addresseeview.h>
00038 
00039 #include "xxportmanager.h"
00040 
00041 #include "vcard_xxport.h"
00042 
00043 class VCardXXPortFactory : public KAB::XXPortFactory
00044 {
00045   public:
00046     KAB::XXPort *xxportObject( KABC::AddressBook *ab, QWidget *parent, const char *name )
00047     {
00048       return new VCardXXPort( ab, parent, name );
00049     }
00050 };
00051 
00052 extern "C"
00053 {
00054   void *init_libkaddrbk_vcard_xxport()
00055   {
00056     return ( new VCardXXPortFactory() );
00057   }
00058 }
00059 
00060 class VCardViewerDialog : public KDialogBase
00061 {
00062   public:
00063     VCardViewerDialog( const KABC::Addressee &addr,
00064                        QWidget *parent, const char *name = 0 );
00065 };
00066 
00067 
00068 VCardXXPort::VCardXXPort( KABC::AddressBook *ab, QWidget *parent, const char *name )
00069   : KAB::XXPort( ab, parent, name )
00070 {
00071   createImportAction( i18n( "Import vCard..." ) );
00072   createExportAction( i18n( "Export vCard 2.1..." ), "v21" );
00073   createExportAction( i18n( "Export vCard 3.0..." ), "v30" );
00074 }
00075 
00076 bool VCardXXPort::exportContacts( const KABC::AddresseeList &list, const QString &data )
00077 {
00078   KABC::VCardConverter converter;
00079   KURL url;
00080 
00081   bool ok = true;
00082   if ( list.count() == 1 ) {
00083     url = KFileDialog::getSaveURL( list[ 0 ].givenName() + "_" + list[ 0 ].familyName() + ".vcf" );
00084     if ( url.isEmpty() )
00085       return true;
00086 
00087     if ( data == "v21" )
00088       ok = doExport( url, converter.createVCards( list, KABC::VCardConverter::v2_1 ) );
00089     else
00090       ok = doExport( url, converter.createVCards( list, KABC::VCardConverter::v3_0 ) );
00091   } else {
00092     QString msg = i18n( "You have selected a list of contacts, shall they be "
00093                         "exported to several files?" );
00094 
00095     switch ( KMessageBox::questionYesNo( parentWidget(), msg ) ) {
00096       case KMessageBox::Yes: {
00097         KURL baseUrl = KFileDialog::getExistingURL();
00098         if ( baseUrl.isEmpty() )
00099           return true;
00100 
00101         KABC::AddresseeList::ConstIterator it;
00102         for ( it = list.begin(); it != list.end(); ++it ) {
00103           url = baseUrl.url() + "/" + (*it).givenName() + "_" + (*it).familyName() + ".vcf";
00104 
00105           bool tmpOk;
00106           KABC::AddresseeList tmpList;
00107           tmpList.append( *it );
00108 
00109           if ( data == "v21" )
00110             tmpOk = doExport( url, converter.createVCards( tmpList, KABC::VCardConverter::v2_1 ) );
00111           else
00112             tmpOk = doExport( url, converter.createVCards( tmpList, KABC::VCardConverter::v3_0 ) );
00113 
00114           ok = ok && tmpOk;
00115         }
00116         break;
00117       }
00118       case KMessageBox::No:
00119       default: {
00120         url = KFileDialog::getSaveURL( "addressbook.vcf" );
00121         if ( url.isEmpty() )
00122           return true;
00123 
00124         if ( data == "v21" )
00125           ok = doExport( url, converter.createVCards( list, KABC::VCardConverter::v2_1 ) );
00126         else
00127           ok = doExport( url, converter.createVCards( list, KABC::VCardConverter::v3_0 ) );
00128       }
00129     }
00130   }
00131 
00132   return ok;
00133 }
00134 
00135 KABC::AddresseeList VCardXXPort::importContacts( const QString& ) const
00136 {
00137   QString fileName;
00138   KABC::AddresseeList addrList;
00139   KURL::List urls;
00140 
00141   if ( !XXPortManager::importData.isEmpty() )
00142     addrList = parseVCard( XXPortManager::importData );
00143   else {
00144     if ( XXPortManager::importURL.isEmpty() )
00145       urls = KFileDialog::getOpenURLs( QString::null, "*.vcf|vCards", parentWidget(),
00146                                        i18n( "Select vCard to Import" ) );
00147     else
00148       urls.append( XXPortManager::importURL );
00149 
00150     if ( urls.count() == 0 )
00151       return addrList;
00152 
00153     QString caption( i18n( "vCard Import Failed" ) );
00154     KURL::List::Iterator it;
00155     for ( it = urls.begin(); it != urls.end(); ++it ) {
00156       if ( KIO::NetAccess::download( *it, fileName, parentWidget() ) ) {
00157 
00158         QFile file( fileName );
00159 
00160         file.open( IO_ReadOnly );
00161         QByteArray rawData = file.readAll();
00162         file.close();
00163 
00164         QString data = QString::fromUtf8( rawData.data(), rawData.size() + 1 );
00165         addrList += parseVCard( data );
00166 
00167         KIO::NetAccess::removeTempFile( fileName );
00168       } else {
00169         QString text = i18n( "<qt>Unable to access <b>%1</b>.</qt>" );
00170         KMessageBox::error( parentWidget(), text.arg( (*it).url() ), caption );
00171       }
00172     }
00173 
00174     if ( !XXPortManager::importURL.isEmpty() ) { // a vcard was passed via cmd
00175       KABC::AddresseeList::Iterator addrIt;
00176       for ( addrIt = addrList.begin(); addrIt != addrList.end(); ++addrIt ) {
00177         VCardViewerDialog dlg( *addrIt, parentWidget() );
00178         if ( !dlg.exec() ) {
00179           addrIt = addrList.remove( addrIt );
00180           addrIt--;
00181         }
00182       }
00183     }
00184   }
00185 
00186   return addrList;
00187 }
00188 
00189 KABC::AddresseeList VCardXXPort::parseVCard( const QString &data ) const
00190 {
00191   KABC::VCardConverter converter;
00192 
00193   return converter.parseVCards( data );
00194 }
00195 
00196 bool VCardXXPort::doExport( const KURL &url, const QString &data )
00197 {
00198   KTempFile tmpFile;
00199   tmpFile.setAutoDelete( true );
00200 
00201   QTextStream stream( tmpFile.file() );
00202   stream.setEncoding( QTextStream::UnicodeUTF8 );
00203 
00204   stream << data;
00205   tmpFile.close();
00206 
00207   return KIO::NetAccess::upload( tmpFile.name(), url, parentWidget() );
00208 }
00209 
00210 // ---------- VCardViewer Dialog ---------------- //
00211 
00212 VCardViewerDialog::VCardViewerDialog( const KABC::Addressee &addr,
00213                                       QWidget *parent, const char *name )
00214   : KDialogBase( Plain, i18n( "Import vCard" ), Ok | Cancel, Ok,
00215                  parent, name, true, true )
00216 {
00217   QFrame *page = plainPage();
00218   QVBoxLayout *layout = new QVBoxLayout( page, marginHint(), spacingHint() );  
00219 
00220   QLabel *label = new QLabel( i18n( "Do you want to import this contact in your address book?" ), page );
00221   QFont font = label->font();
00222   font.setBold( true );
00223   label->setFont( font );
00224   layout->addWidget( label );
00225 
00226   KPIM::AddresseeView *view = new KPIM::AddresseeView( page );
00227   view->setAddressee( addr );
00228   view->setVScrollBarMode( QScrollView::Auto );
00229   layout->addWidget( view );
00230 }
00231 
00232 #include "vcard_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 Sat May 1 11:38:53 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003