kaddressbook Library API Documentation

xxportmanager.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 <qlayout.h>
00025 
00026 #include <kabc/addressbook.h>
00027 #include <kabc/resource.h>
00028 #include <kdebug.h>
00029 #include <klocale.h>
00030 #include <kmessagebox.h>
00031 #include <ktrader.h>
00032 #include <kapplication.h>
00033 
00034 #include "core.h"
00035 #include "kablock.h"
00036 #include "undocmds.h"
00037 #include "xxportselectdialog.h"
00038 
00039 #include "xxportmanager.h"
00040 
00041 KURL XXPortManager::importURL = KURL();
00042 QString XXPortManager::importData = QString::null;
00043 
00044 XXPortManager::XXPortManager( KAB::Core *core, QObject *parent, const char *name )
00045   : QObject( parent, name ), mCore( core )
00046 {
00047   loadPlugins();
00048 }
00049 
00050 XXPortManager::~XXPortManager()
00051 {
00052 }
00053 
00054 void XXPortManager::restoreSettings()
00055 {
00056 }
00057 
00058 void XXPortManager::saveSettings()
00059 {
00060 }
00061 
00062 void XXPortManager::importVCard( const KURL &url )
00063 {
00064   importURL = url;
00065   slotImport( "vcard", "<empty>" );
00066   importURL = KURL();
00067 }
00068 
00069 void XXPortManager::importVCard( const QString &vCard )
00070 {
00071   importData = vCard;
00072   slotImport( "vcard", "<empty>" );
00073   importData = "";
00074 }
00075 
00076 void XXPortManager::slotImport( const QString &identifier, const QString &data )
00077 {
00078   KAB::XXPort *obj = mXXPortObjects[ identifier ];
00079   if ( !obj ) {
00080     KMessageBox::error( mCore->widget(), i18n( "<qt>No import plugin available for <b>%1</b>.</qt>" ).arg( identifier ) );
00081     return;
00082   }
00083 
00084   KABC::Resource *resource = mCore->requestResource( mCore->widget() );
00085   if ( !resource )
00086     return;
00087 
00088   KABLock::self( mCore->addressBook() )->lock( resource );
00089 
00090   KABC::AddresseeList list = obj->importContacts( data );
00091   KABC::AddresseeList::Iterator it;
00092   bool imported = false;
00093   for ( it = list.begin(); it != list.end(); ++it ) {
00094     (*it).setResource( resource );
00095     // We use a PwNewCommand so the user can undo it.
00096     PwNewCommand *command = new PwNewCommand( mCore->addressBook(), *it );
00097     UndoStack::instance()->push( command );
00098     RedoStack::instance()->clear();
00099     imported = true;
00100   }
00101 
00102   KABLock::self( mCore->addressBook() )->unlock( resource );
00103 
00104   if ( imported )
00105     emit modified();
00106 }
00107 
00108 void XXPortManager::slotExport( const QString &identifier, const QString &data )
00109 {
00110   KAB::XXPort *obj = mXXPortObjects[ identifier ];
00111   if ( !obj ) {
00112     KMessageBox::error( mCore->widget(), i18n( "<qt>No export plugin available for <b>%1</b>.</qt>" ).arg( identifier ) );
00113     return;
00114   }
00115 
00116   KABC::AddresseeList addrList;
00117   XXPortSelectDialog dlg( mCore, obj->requiresSorting(), mCore->widget() );
00118   if ( dlg.exec() )
00119     addrList = dlg.contacts();
00120   else
00121     return;
00122 
00123   if ( !obj->exportContacts( addrList, data ) )
00124     KMessageBox::error( mCore->widget(), i18n( "Unable to export contacts." ) );
00125 }
00126 
00127 void XXPortManager::loadPlugins()
00128 {
00129   mXXPortObjects.clear();
00130 
00131   KTrader::OfferList plugins = KTrader::self()->query( "KAddressBook/XXPort" );
00132   KTrader::OfferList::ConstIterator it;
00133   for ( it = plugins.begin(); it != plugins.end(); ++it ) {
00134     if ( !(*it)->hasServiceType( "KAddressBook/XXPort" ) )
00135       continue;
00136 
00137     KLibFactory *factory = KLibLoader::self()->factory( (*it)->library().latin1() );
00138     if ( !factory ) {
00139       kdDebug(5720) << "XXPortManager::loadExtensions(): Factory creation failed" << endl;
00140       continue;
00141     }
00142 
00143     KAB::XXPortFactory *xxportFactory = static_cast<KAB::XXPortFactory*>( factory );
00144 
00145     if ( !xxportFactory ) {
00146       kdDebug(5720) << "XXPortManager::loadExtensions(): Cast failed" << endl;
00147       continue;
00148     }
00149 
00150     KAB::XXPort *obj = xxportFactory->xxportObject( mCore->addressBook(), mCore->widget() );
00151     if ( obj ) {
00152       if ( mCore->guiClient() )
00153         mCore->guiClient()->insertChildClient( obj );
00154 
00155       mXXPortObjects.insert( obj->identifier(), obj );
00156       connect( obj, SIGNAL( exportActivated( const QString&, const QString& ) ),
00157                this, SLOT( slotExport( const QString&, const QString& ) ) );
00158       connect( obj, SIGNAL( importActivated( const QString&, const QString& ) ),
00159                this, SLOT( slotImport( const QString&, const QString& ) ) );
00160 
00161       obj->setKApplication( kapp );
00162     }
00163   }
00164 }
00165 
00166 #include "xxportmanager.moc"
KDE Logo
This file is part of the documentation for kaddressbook Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Oct 4 14:41:41 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003