kaddressbook

extensionmanager.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 Qt, and distribute the resulting executable,
00021     without including the source code for Qt in the source distribution.
00022 */
00023 
00024 #include <kactionclasses.h>
00025 #include <kconfig.h>
00026 #include <kdebug.h>
00027 #include <klocale.h>
00028 #include <ktrader.h>
00029 
00030 #include <qsignalmapper.h>
00031 #include <qtimer.h>
00032 
00033 #include "addresseeeditorextension.h"
00034 #include "core.h"
00035 #include "kabprefs.h"
00036 
00037 #include "extensionmanager.h"
00038 
00039 ExtensionManager::ExtensionManager( KAB::Core *core, QWidget *parent,
00040                                     const char *name )
00041   : QHBox( parent, name ), mCore( core ), mCurrentExtensionWidget( 0 ),
00042     mMapper( 0 )
00043 {
00044   createExtensionWidgets();
00045 
00046   mActionCollection = new KActionCollection( this, "ActionCollection" );
00047 
00048   QTimer::singleShot( 0, this, SLOT( createActions() ) );
00049 }
00050 
00051 ExtensionManager::~ExtensionManager()
00052 {
00053 }
00054 
00055 void ExtensionManager::restoreSettings()
00056 {
00057   for ( uint index = 0; index < mExtensionList.size(); ++index ) {
00058     ExtensionData data = mExtensionList[ index ];
00059     if ( data.identifier == KABPrefs::instance()->currentExtension() ) {
00060       KToggleAction *action = static_cast<KToggleAction*>( mActionList.at( index ) );
00061       if ( action )
00062         action->setChecked( true );
00063       setActiveExtension( index );
00064       return;
00065     }
00066   }
00067 
00068   if ( mActionList.first() )
00069     static_cast<KToggleAction*>( mActionList.first() )->setChecked( true );
00070   setActiveExtension( 0 );
00071 }
00072 
00073 void ExtensionManager::saveSettings()
00074 {
00075   KAction *action;
00076   uint index = 0;
00077   for ( action = mActionList.first(); action; action = mActionList.next(), index++ )
00078     if ( static_cast<KToggleAction*>( action )->isChecked() )
00079       break;
00080 
00081   Q_ASSERT( index < mExtensionList.size() );
00082 
00083   KABPrefs::instance()->setCurrentExtension( mExtensionList[ index ].identifier );
00084 }
00085 
00086 void ExtensionManager::reconfigure()
00087 {
00088   saveSettings();
00089   createExtensionWidgets();
00090   createActions();
00091   restoreSettings();
00092 }
00093 
00094 bool ExtensionManager::isQuickEditVisible() const
00095 {
00096   return ( mCurrentExtensionWidget &&
00097       mCurrentExtensionWidget->identifier() == "contact_editor" );
00098 }
00099 
00100 void ExtensionManager::setSelectionChanged()
00101 {
00102   if ( mCurrentExtensionWidget )
00103     mCurrentExtensionWidget->contactsSelectionChanged();
00104 }
00105 
00106 void ExtensionManager::setActiveExtension( int id )
00107 {
00108   if ( id == 0 ) {
00109     hide();
00110     if ( mCurrentExtensionWidget )
00111       mCurrentExtensionWidget->hide();
00112     mCurrentExtensionWidget = 0;
00113   } else if ( id > 0 ) {
00114     if ( mCurrentExtensionWidget )
00115       mCurrentExtensionWidget->hide();
00116 
00117     mCurrentExtensionWidget = mExtensionList[ id ].widget;
00118     if ( mCurrentExtensionWidget ) {
00119       show();
00120       mCurrentExtensionWidget->show();
00121       mCurrentExtensionWidget->contactsSelectionChanged();
00122     } else {
00123       hide();
00124       mCurrentExtensionWidget = 0;
00125     }
00126   }
00127 }
00128 
00129 void ExtensionManager::createActions()
00130 {
00131   mCore->guiClient()->unplugActionList( "extensions_list" );
00132   mActionList.setAutoDelete( true );
00133   mActionList.clear();
00134   mActionList.setAutoDelete( false );
00135 
00136   delete mMapper;
00137   mMapper = new QSignalMapper( this, "SignalMapper" );
00138   connect( mMapper, SIGNAL( mapped( int ) ),
00139            this, SLOT( setActiveExtension( int ) ) );
00140 
00141   int actionCounter = 0;
00142   ExtensionData::List::ConstIterator it;
00143   for ( it = mExtensionList.begin(); it != mExtensionList.end(); ++it ) {
00144     ExtensionData data = *it;
00145     KToggleAction *action = new KToggleAction( data.title, 0, mMapper, SLOT( map() ),
00146                                                mActionCollection,
00147                                                QString( data.identifier + "_extension" ).latin1() );
00148     action->setExclusiveGroup( "extensions" );
00149     mMapper->setMapping( action, actionCounter++ );
00150     mActionList.append( action );
00151 
00152     if ( data.widget == mCurrentExtensionWidget )
00153       action->setChecked( true );
00154   }
00155 
00156   mCore->guiClient()->plugActionList( "extensions_list", mActionList );
00157 
00158   if ( mCurrentExtensionWidget == 0 && mActionList.first() )
00159     static_cast<KToggleAction*>( mActionList.first() )->setChecked( true );
00160 }
00161 
00162 void ExtensionManager::createExtensionWidgets()
00163 {
00164   // clean up
00165   ExtensionData::List::ConstIterator dataIt;
00166   for ( dataIt = mExtensionList.begin(); dataIt != mExtensionList.end(); ++dataIt )
00167     delete (*dataIt).widget;
00168   mExtensionList.clear();
00169 
00170   KAB::ExtensionWidget *wdg = 0;
00171 
00172   {
00173     // add 'None' entry
00174     ExtensionData data;
00175     data.identifier = "none";
00176     data.title = i18n( "None" );
00177     data.widget = 0;
00178     mExtensionList.append( data );
00179   }
00180 
00181   {
00182     // add addressee editor as default
00183     wdg = new AddresseeEditorExtension( mCore, this );
00184     wdg->hide();
00185 
00186     connect( wdg, SIGNAL( modified( const KABC::Addressee::List& ) ),
00187              SIGNAL( modified( const KABC::Addressee::List& ) ) );
00188     connect( wdg, SIGNAL( deleted( const QStringList& ) ),
00189              SIGNAL( deleted( const QStringList& ) ) );
00190 
00191     ExtensionData data;
00192     data.identifier = wdg->identifier();
00193     data.title = wdg->title();
00194     data.widget = wdg;
00195     mExtensionList.append( data );
00196   }
00197 
00198   // load the other extensions
00199   const KTrader::OfferList plugins = KTrader::self()->query( "KAddressBook/Extension",
00200     QString( "[X-KDE-KAddressBook-ExtensionPluginVersion] == %1" ).arg( KAB_EXTENSIONWIDGET_PLUGIN_VERSION ) );
00201 
00202   KTrader::OfferList::ConstIterator it;
00203   for ( it = plugins.begin(); it != plugins.end(); ++it ) {
00204     KLibFactory *factory = KLibLoader::self()->factory( (*it)->library().latin1() );
00205     if ( !factory ) {
00206       kdDebug(5720) << "ExtensionManager::loadExtensions(): Factory creation failed" << endl;
00207       continue;
00208     }
00209 
00210     KAB::ExtensionFactory *extensionFactory = static_cast<KAB::ExtensionFactory*>( factory );
00211 
00212     if ( !extensionFactory ) {
00213       kdDebug(5720) << "ExtensionManager::loadExtensions(): Cast failed" << endl;
00214       continue;
00215     }
00216 
00217     wdg = extensionFactory->extension( mCore, this );
00218     if ( wdg ) {
00219       wdg->hide();
00220       connect( wdg, SIGNAL( modified( const KABC::Addressee::List& ) ),
00221                SIGNAL( modified( const KABC::Addressee::List& ) ) );
00222       connect( wdg, SIGNAL( deleted( const QStringList& ) ),
00223                SIGNAL( deleted( const QStringList& ) ) );
00224 
00225       ExtensionData data;
00226       data.identifier = wdg->identifier();
00227       data.title = wdg->title();
00228       data.widget = wdg;
00229       mExtensionList.append( data );
00230     }
00231   }
00232 
00233   mCurrentExtensionWidget = 0;
00234 }
00235 
00236 #include "extensionmanager.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys