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   mActionList.append( new KActionSeparator( mActionCollection ) );
00182   mCore->guiClient()->plugActionList( "extensions_list", mActionList );
00183 }
00184 
00185 QWidget* ExtensionManager::activeDetailsWidget() const
00186 {
00187     return mActiveDetailsWidget;
00188 }
00189 
00190 void ExtensionManager::createExtensionWidgets()
00191 {
00192   // clean up
00193   for ( QMap<QString, ExtensionData>::ConstIterator it = mExtensionMap.begin(), end = mExtensionMap.end(); it != end; ++it ) {
00194     delete it.data().widget;
00195   }
00196   mExtensionMap.clear();
00197 
00198   KAB::ExtensionWidget *wdg = 0;
00199 
00200   {
00201     // add addressee editor as default
00202     wdg = new AddresseeEditorExtension( mCore, mDetailsStack );
00203     wdg->hide();
00204 
00205     connect( wdg, SIGNAL( modified( const KABC::Addressee::List& ) ),
00206              SIGNAL( modified( const KABC::Addressee::List& ) ) );
00207     connect( wdg, SIGNAL( deleted( const QStringList& ) ),
00208              SIGNAL( deleted( const QStringList& ) ) );
00209 
00210     ExtensionData data;
00211     data.identifier = wdg->identifier();
00212     data.title = wdg->title();
00213     data.widget = wdg;
00214     data.isDetailsExtension = true;
00215     mExtensionMap.insert( data.identifier, data );
00216   }
00217 
00218   // load the other extensions
00219   const KTrader::OfferList plugins = KTrader::self()->query( "KAddressBook/Extension",
00220     QString( "[X-KDE-KAddressBook-ExtensionPluginVersion] == %1" ).arg( KAB_EXTENSIONWIDGET_PLUGIN_VERSION ) );
00221 
00222   KTrader::OfferList::ConstIterator it;
00223   for ( it = plugins.begin(); it != plugins.end(); ++it ) {
00224     KLibFactory *factory = KLibLoader::self()->factory( (*it)->library().latin1() );
00225     if ( !factory ) {
00226       kdDebug(5720) << "ExtensionManager::loadExtensions(): Factory creation failed" << endl;
00227       continue;
00228     }
00229 
00230     KAB::ExtensionFactory *extensionFactory = static_cast<KAB::ExtensionFactory*>( factory );
00231 
00232     if ( !extensionFactory ) {
00233       kdDebug(5720) << "ExtensionManager::loadExtensions(): Cast failed" << endl;
00234       continue;
00235     }
00236 
00237     wdg = extensionFactory->extension( mCore, mSplitter );
00238     if ( wdg ) {
00239       if ( wdg->identifier() == "distribution_list_editor_ng" )
00240           mSplitter->moveToFirst( wdg );
00241       wdg->hide();
00242       connect( wdg, SIGNAL( modified( const KABC::Addressee::List& ) ),
00243                SIGNAL( modified( const KABC::Addressee::List& ) ) );
00244       connect( wdg, SIGNAL( deleted( const QStringList& ) ),
00245                SIGNAL( deleted( const QStringList& ) ) );
00246 
00247       ExtensionData data;
00248       data.identifier = wdg->identifier();
00249       data.title = wdg->title();
00250       data.widget = wdg;
00251       mExtensionMap.insert( data.identifier, data );
00252     }
00253   }
00254 }
00255 
00256 #include "extensionmanager.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys