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 <qlayout.h>
00031 #include <qobjectlist.h>
00032 #include <qsignalmapper.h>
00033 #include <qsplitter.h>
00034 #include <qtimer.h>
00035 #include <qwidgetstack.h>
00036 
00037 #include "addresseeeditorextension.h"
00038 #include "core.h"
00039 #include "kabprefs.h"
00040 
00041 #include "extensionmanager.h"
00042 
00043 ExtensionData::ExtensionData() : action( 0 ), widget( 0 ), weight( 0 ), isDetailsExtension( false )
00044 {
00045 }
00046 
00047 ExtensionManager::ExtensionManager( QWidget* extensionBar, QWidgetStack* detailsStack, KAB::Core *core, QObject *parent,
00048                                     const char *name )
00049     : QObject( parent, name ), mExtensionBar( extensionBar ), mCore( core ), 
00050       mMapper( 0 ), mDetailsStack( detailsStack ), mActiveDetailsWidget( 0 )
00051 {
00052   Q_ASSERT( mExtensionBar ); 
00053   QVBoxLayout* layout = new QVBoxLayout( mExtensionBar );
00054   mSplitter = new QSplitter( mExtensionBar );
00055   mSplitter->setOrientation( QSplitter::Vertical );
00056   layout->addWidget( mSplitter );
00057 
00058   createExtensionWidgets();
00059 
00060   mActionCollection = new KActionCollection( this, "ActionCollection" );
00061 
00062   extensionBar->setShown( false );
00063   QTimer::singleShot( 0, this, SLOT( createActions() ) );
00064 }
00065 
00066 ExtensionManager::~ExtensionManager()
00067 {
00068 }
00069 
00070 
00071 void ExtensionManager::restoreSettings() 
00072 {
00073   const QStringList activeExtensions = KABPrefs::instance()->activeExtensions();
00074 
00075   typedef QMap<QString, ExtensionData>::ConstIterator ConstIterator;
00076   for ( ConstIterator it = mExtensionMap.begin(), end = mExtensionMap.end(); it != end; ++it ) {
00077     if ( activeExtensions.contains( it.data().identifier ) ) {
00078       KToggleAction *action = static_cast<KToggleAction*>( it.data().action );
00079       if ( action )
00080         action->setChecked( true );
00081       setExtensionActive( it.data().identifier, true );
00082     }
00083   }
00084   const QValueList<int> sizes = KABPrefs::instance()->extensionsSplitterSizes();
00085   mSplitter->setSizes( sizes );
00086 }
00087 
00088 void ExtensionManager::saveSettings()
00089 {
00090   KABPrefs::instance()->setActiveExtensions( mActiveExtensions );
00091   KABPrefs::instance()->setExtensionsSplitterSizes( mSplitter->sizes() );
00092 }
00093 
00094 void ExtensionManager::reconfigure()
00095 {
00096   saveSettings();
00097   createExtensionWidgets();
00098   createActions();
00099   restoreSettings();
00100   mExtensionBar->setShown( !mActiveExtensions.isEmpty() );
00101 }
00102 
00103 bool ExtensionManager::isQuickEditVisible() const
00104 {
00105   return mActiveExtensions.contains( "contact_editor" );
00106 }
00107 
00108 void ExtensionManager::setSelectionChanged()
00109 {
00110   for ( QStringList::ConstIterator it = mActiveExtensions.begin(), end = mActiveExtensions.end(); it != end; ++it ) {
00111     if ( mExtensionMap.contains( *it ) && mExtensionMap[*it].widget )
00112       mExtensionMap[*it].widget->contactsSelectionChanged();
00113   } 
00114 }
00115 
00116 void ExtensionManager::activationToggled( const QString &extid )
00117 {
00118   if ( !mExtensionMap.contains( extid ) )
00119     return;
00120   const ExtensionData data = mExtensionMap[ extid ];
00121   const bool activated = data.action->isChecked();
00122   setExtensionActive( extid, activated );
00123 }
00124 
00125 void ExtensionManager::setExtensionActive( const QString& extid, bool active )
00126 {
00127   if ( !mExtensionMap.contains( extid ) )
00128     return;
00129   if ( mActiveExtensions.contains( extid ) == active )
00130     return; 
00131   const ExtensionData data = mExtensionMap[ extid ];
00132   if ( active ) {
00133     mActiveExtensions.append( extid );
00134     if ( data.widget ) {
00135       if ( data.isDetailsExtension ) {
00136         mActiveDetailsWidget = data.widget;
00137         emit detailsWidgetActivated( data.widget );
00138       } else {
00139           data.widget->show();
00140       }
00141       data.widget->contactsSelectionChanged();
00142     }
00143   } else {
00144     mActiveExtensions.remove( extid );
00145     if ( data.widget && !data.isDetailsExtension ) {
00146       data.widget->hide();
00147     }
00148     if ( data.isDetailsExtension ) {
00149       mActiveDetailsWidget = 0;
00150       emit detailsWidgetDeactivated( data.widget );
00151     }
00152   }
00153   mExtensionBar->setShown( !mActiveExtensions.isEmpty() );
00154 }
00155  
00156 void ExtensionManager::createActions()
00157 {
00158   mCore->guiClient()->unplugActionList( "extensions_list" );
00159   mActionList.setAutoDelete( true );
00160   mActionList.clear();
00161   mActionList.setAutoDelete( false );
00162 
00163   delete mMapper;
00164   mMapper = new QSignalMapper( this, "SignalMapper" );
00165   connect( mMapper, SIGNAL( mapped( const QString& ) ),
00166            this, SLOT( activationToggled( const QString& ) ) );
00167 
00168   ExtensionData::List::ConstIterator it;
00169   for ( QMap<QString, ExtensionData>::Iterator it = mExtensionMap.begin(), end = mExtensionMap.end(); it != end; ++it ) {
00170     ExtensionData& data = it.data();
00171     data.action = new KToggleAction( data.title, 0, mMapper, SLOT( map() ),
00172                                                mActionCollection,
00173                                                QString( data.identifier + "_extension" ).latin1() );
00174     mMapper->setMapping( data.action, data.identifier );
00175     mActionList.append( data.action );
00176 
00177     if ( mActiveExtensions.contains( data.identifier ) )
00178       data.action->setChecked( true );
00179   }
00180 
00181   mCore->guiClient()->plugActionList( "extensions_list", mActionList );
00182 }
00183 
00184 QWidget* ExtensionManager::activeDetailsWidget() const
00185 {
00186     return mActiveDetailsWidget;
00187 }
00188 
00189 void ExtensionManager::createExtensionWidgets()
00190 {
00191   // clean up
00192   for ( QMap<QString, ExtensionData>::ConstIterator it = mExtensionMap.begin(), end = mExtensionMap.end(); it != end; ++it ) {
00193     delete it.data().widget;
00194   }
00195   mExtensionMap.clear();
00196 
00197   KAB::ExtensionWidget *wdg = 0;
00198 
00199   {
00200     // add addressee editor as default
00201     wdg = new AddresseeEditorExtension( mCore, mDetailsStack );
00202     wdg->hide();
00203 
00204     connect( wdg, SIGNAL( modified( const KABC::Addressee::List& ) ),
00205              SIGNAL( modified( const KABC::Addressee::List& ) ) );
00206     connect( wdg, SIGNAL( deleted( const QStringList& ) ),
00207              SIGNAL( deleted( const QStringList& ) ) );
00208 
00209     ExtensionData data;
00210     data.identifier = wdg->identifier();
00211     data.title = wdg->title();
00212     data.widget = wdg;
00213     data.isDetailsExtension = true;
00214     mExtensionMap.insert( data.identifier, data );
00215   }
00216 
00217   // load the other extensions
00218   const KTrader::OfferList plugins = KTrader::self()->query( "KAddressBook/Extension",
00219     QString( "[X-KDE-KAddressBook-ExtensionPluginVersion] == %1" ).arg( KAB_EXTENSIONWIDGET_PLUGIN_VERSION ) );
00220 
00221   KTrader::OfferList::ConstIterator it;
00222   for ( it = plugins.begin(); it != plugins.end(); ++it ) {
00223     KLibFactory *factory = KLibLoader::self()->factory( (*it)->library().latin1() );
00224     if ( !factory ) {
00225       kdDebug(5720) << "ExtensionManager::loadExtensions(): Factory creation failed" << endl;
00226       continue;
00227     }
00228 
00229     KAB::ExtensionFactory *extensionFactory = static_cast<KAB::ExtensionFactory*>( factory );
00230 
00231     if ( !extensionFactory ) {
00232       kdDebug(5720) << "ExtensionManager::loadExtensions(): Cast failed" << endl;
00233       continue;
00234     }
00235 
00236     wdg = extensionFactory->extension( mCore, mSplitter );
00237     if ( wdg ) {
00238       wdg->hide();
00239       connect( wdg, SIGNAL( modified( const KABC::Addressee::List& ) ),
00240                SIGNAL( modified( const KABC::Addressee::List& ) ) );
00241       connect( wdg, SIGNAL( deleted( const QStringList& ) ),
00242                SIGNAL( deleted( const QStringList& ) ) );
00243 
00244       ExtensionData data;
00245       data.identifier = wdg->identifier();
00246       data.title = wdg->title();
00247       data.widget = wdg;
00248       mExtensionMap.insert( data.identifier, data );
00249     }
00250   }
00251 }
00252 
00253 #include "extensionmanager.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys