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