kaddressbook Library API Documentation

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., 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 <kactionclasses.h>
00025 #include <kconfig.h>
00026 #include <kdebug.h>
00027 #include <klocale.h>
00028 #include <ktrader.h>
00029 
00030 #include "addresseeeditorwidget.h"
00031 #include "simpleaddresseeeditor.h"
00032 #include "core.h"
00033 #include "kabprefs.h"
00034 
00035 #include "extensionmanager.h"
00036 
00037 ExtensionManager::ExtensionManager( KAB::Core *core, QWidget *parent,
00038                                     const char *name )
00039   : QHBox( parent, name ), mCore( core ), mCurrentExtensionWidget( 0 )
00040 {
00041   mActionExtensions = new KSelectAction( i18n( "Show Extension Bar" ), 0,
00042                                          mCore->actionCollection(),
00043                                          "options_show_extensions" );
00044 
00045   connect( mActionExtensions, SIGNAL( activated( int ) ),
00046            SLOT( setActiveExtension( int ) ) );
00047 
00048   createExtensionWidgets();
00049 }
00050 
00051 ExtensionManager::~ExtensionManager()
00052 {
00053 }
00054 
00055 void ExtensionManager::restoreSettings()
00056 {
00057   int index = 0;
00058   for ( int index = 0; index < mExtensionList.size(); ++index ) {
00059     ExtensionData data = mExtensionList[ index ];
00060     if ( data.identifier == KABPrefs::instance()->mCurrentExtension ) {
00061       mActionExtensions->setCurrentItem( index );
00062       setActiveExtension( index );
00063       return;
00064     }
00065   }
00066 
00067   mActionExtensions->setCurrentItem( 0 );
00068   setActiveExtension( 0 );
00069 }
00070 
00071 void ExtensionManager::saveSettings()
00072 {
00073   int index = mActionExtensions->currentItem();
00074 
00075   Q_ASSERT( index < mExtensionList.size() );
00076 
00077   KABPrefs::instance()->mCurrentExtension = mExtensionList[ index ].identifier;
00078 }
00079 
00080 void ExtensionManager::reconfigure()
00081 {
00082   saveSettings();
00083   createExtensionWidgets();
00084   restoreSettings();  
00085 }
00086 
00087 bool ExtensionManager::isQuickEditVisible() const
00088 {
00089   return ( mCurrentExtensionWidget &&
00090       mCurrentExtensionWidget->identifier() == "contact_editor" );
00091 }
00092 
00093 void ExtensionManager::setSelectionChanged()
00094 {
00095   if ( mCurrentExtensionWidget )
00096     mCurrentExtensionWidget->contactsSelectionChanged();
00097 }
00098 
00099 void ExtensionManager::setActiveExtension( int id )
00100 {
00101   if ( id == 0 ) {
00102     hide();
00103     if ( mCurrentExtensionWidget )
00104       mCurrentExtensionWidget->hide();
00105     mCurrentExtensionWidget = 0;
00106   } else if ( id > 0 ) {
00107     if ( mCurrentExtensionWidget )
00108       mCurrentExtensionWidget->hide();
00109 
00110     mCurrentExtensionWidget = mExtensionList[ id ].widget;
00111     if ( mCurrentExtensionWidget ) {
00112       show();
00113       mCurrentExtensionWidget->show();
00114       mCurrentExtensionWidget->contactsSelectionChanged();
00115     } else {
00116       hide();
00117       mCurrentExtensionWidget = 0;
00118     }
00119   }
00120 }
00121 
00122 void ExtensionManager::createExtensionWidgets()
00123 {
00124   // clean up
00125   ExtensionData::List::Iterator dataIt;
00126   for ( dataIt = mExtensionList.begin(); dataIt != mExtensionList.end(); ++dataIt )
00127     delete (*dataIt).widget;
00128   mExtensionList.clear();
00129 
00130   KAB::ExtensionWidget *wdg = 0;
00131 
00132   {
00133     // add 'None' entry
00134     ExtensionData data;
00135     data.identifier = "none";
00136     data.title = i18n( "None" );
00137     data.widget = 0;
00138     mExtensionList.append( data );
00139   }
00140 
00141   {
00142     // add addressee editor as default
00143     if ( KABPrefs::instance()->mEditorType == KABPrefs::SimpleEditor ) {
00144       wdg = new SimpleAddresseeEditor( mCore, true, this );
00145     } else {
00146       wdg = new AddresseeEditorWidget( mCore, true, this );
00147     }
00148     wdg->hide();
00149 
00150     connect( wdg, SIGNAL( modified( const KABC::Addressee::List& ) ),
00151              SIGNAL( modified( const KABC::Addressee::List& ) ) );
00152     connect( wdg, SIGNAL( deleted( const QStringList& ) ),
00153              SIGNAL( deleted( const QStringList& ) ) );
00154 
00155     ExtensionData data;
00156     data.identifier = wdg->identifier();
00157     data.title = wdg->title();
00158     data.widget = wdg;
00159     mExtensionList.append( data );
00160   }
00161 
00162   // load the other extensions
00163   KTrader::OfferList plugins = KTrader::self()->query( "KAddressBook/Extension" );
00164   KTrader::OfferList::ConstIterator it;
00165 
00166   for ( it = plugins.begin(); it != plugins.end(); ++it ) {
00167     KLibFactory *factory = KLibLoader::self()->factory( (*it)->library().latin1() );
00168     if ( !factory ) {
00169       kdDebug(5720) << "ExtensionManager::loadExtensions(): Factory creation failed" << endl;
00170       continue;
00171     }
00172 
00173     KAB::ExtensionFactory *extensionFactory = static_cast<KAB::ExtensionFactory*>( factory );
00174 
00175     if ( !extensionFactory ) {
00176       kdDebug(5720) << "ExtensionManager::loadExtensions(): Cast failed" << endl;
00177       continue;
00178     }
00179 
00180     wdg = extensionFactory->extension( mCore, this );
00181     if ( wdg ) {
00182       wdg->hide();
00183       connect( wdg, SIGNAL( modified( const KABC::Addressee::List& ) ),
00184                SIGNAL( modified( const KABC::Addressee::List& ) ) );
00185       connect( wdg, SIGNAL( deleted( const QStringList& ) ),
00186                SIGNAL( deleted( const QStringList& ) ) );
00187 
00188       ExtensionData data;
00189       data.identifier = wdg->identifier();
00190       data.title = wdg->title();
00191       data.widget = wdg;
00192       mExtensionList.append( data );
00193     }
00194   }
00195 
00196   QStringList extensionTitles;
00197   for ( dataIt = mExtensionList.begin(); dataIt != mExtensionList.end(); ++dataIt )
00198     extensionTitles.append( (*dataIt).title );
00199 
00200   mActionExtensions->setItems( extensionTitles );
00201   mCurrentExtensionWidget = 0;
00202 }
00203 
00204 #include "extensionmanager.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 Aug 2 09:54:30 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003