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., 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 TQt, and distribute the resulting executable, 00021 without including the source code for TQt in the source distribution. 00022 */ 00023 00024 #include <tqlayout.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 TQString XXPortManager::importData = TQString(); 00043 00044 XXPortManager::XXPortManager( KAB::Core *core, TQObject *parent, const char *name ) 00045 : TQObject( 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::importVCardFromData( const TQString &vCard ) 00070 { 00071 importData = vCard; 00072 slotImport( "vcard", "<empty>" ); 00073 importData = ""; 00074 } 00075 00076 void XXPortManager::slotImport( const TQString &identifier, const TQString &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 KABC::AddresseeList list = obj->importContacts( data ); 00089 KABC::AddresseeList::Iterator it; 00090 for ( it = list.begin(); it != list.end(); ++it ) 00091 (*it).setResource( resource ); 00092 00093 if ( !list.isEmpty() ) { 00094 NewCommand *command = new NewCommand( mCore->addressBook(), list ); 00095 mCore->commandHistory()->addCommand( command ); 00096 emit modified(); 00097 } 00098 } 00099 00100 void XXPortManager::slotExport( const TQString &identifier, const TQString &data ) 00101 { 00102 KAB::XXPort *obj = mXXPortObjects[ identifier ]; 00103 if ( !obj ) { 00104 KMessageBox::error( mCore->widget(), i18n( "<qt>No export plugin available for <b>%1</b>.</qt>" ).arg( identifier ) ); 00105 return; 00106 } 00107 00108 KABC::AddresseeList addrList; 00109 XXPortSelectDialog dlg( mCore, obj->requiresSorting(), mCore->widget() ); 00110 if ( dlg.exec() ) 00111 addrList = dlg.contacts(); 00112 else 00113 return; 00114 00115 if ( !obj->exportContacts( addrList, data ) ) 00116 KMessageBox::error( mCore->widget(), i18n( "Unable to export contacts." ) ); 00117 } 00118 00119 void XXPortManager::loadPlugins() 00120 { 00121 mXXPortObjects.clear(); 00122 00123 const KTrader::OfferList plugins = KTrader::self()->query( "KAddressBook/XXPort", 00124 TQString( "[X-KDE-KAddressBook-XXPortPluginVersion] == %1" ).arg( KAB_XXPORT_PLUGIN_VERSION ) ); 00125 KTrader::OfferList::ConstIterator it; 00126 for ( it = plugins.begin(); it != plugins.end(); ++it ) { 00127 if ( !(*it)->hasServiceType( "KAddressBook/XXPort" ) ) 00128 continue; 00129 00130 KLibFactory *factory = KLibLoader::self()->factory( (*it)->library().latin1() ); 00131 if ( !factory ) { 00132 kdDebug(5720) << "XXPortManager::loadExtensions(): Factory creation failed" << endl; 00133 continue; 00134 } 00135 00136 KAB::XXPortFactory *xxportFactory = static_cast<KAB::XXPortFactory*>( factory ); 00137 00138 if ( !xxportFactory ) { 00139 kdDebug(5720) << "XXPortManager::loadExtensions(): Cast failed" << endl; 00140 continue; 00141 } 00142 00143 KAB::XXPort *obj = xxportFactory->xxportObject( mCore->addressBook(), mCore->widget() ); 00144 if ( obj ) { 00145 if ( mCore->guiClient() ) 00146 mCore->guiClient()->insertChildClient( obj ); 00147 00148 mXXPortObjects.insert( obj->identifier(), obj ); 00149 connect( obj, TQT_SIGNAL( exportActivated( const TQString&, const TQString& ) ), 00150 this, TQT_SLOT( slotExport( const TQString&, const TQString& ) ) ); 00151 connect( obj, TQT_SIGNAL( importActivated( const TQString&, const TQString& ) ), 00152 this, TQT_SLOT( slotImport( const TQString&, const TQString& ) ) ); 00153 00154 obj->setKApplication( kapp ); 00155 } 00156 } 00157 } 00158 00159 #include "xxportmanager.moc"