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 TQt, and distribute the resulting executable,
00021     without including the source code for TQt in the source distribution.
00022 */
00023 
00024 #include <tdeactionclasses.h>
00025 #include <tdeconfig.h>
00026 #include <kdebug.h>
00027 #include <tdelocale.h>
00028 #include <ktrader.h>
00029 
00030 #include <tqlayout.h>
00031 #include <tqobjectlist.h>
00032 #include <tqsignalmapper.h>
00033 #include <tqsplitter.h>
00034 #include <tqtimer.h>
00035 #include <tqwidgetstack.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( TQWidget* extensionBar, TQWidgetStack* detailsStack, KAB::Core *core, TQObject *parent,
00048                                     const char *name )
00049     : TQObject( parent, name ), mExtensionBar( extensionBar ), mCore( core ),
00050       mMapper( 0 ), mDetailsStack( detailsStack ), mActiveDetailsWidget( 0 )
00051 {
00052   Q_ASSERT( mExtensionBar );
00053   TQVBoxLayout* layout = new TQVBoxLayout( mExtensionBar );
00054   mSplitter = new TQSplitter( mExtensionBar );
00055   mSplitter->setOrientation( Qt::Vertical );
00056   layout->addWidget( mSplitter );
00057 
00058   createExtensionWidgets();
00059 
00060   mActionCollection = new TDEActionCollection( this, "ActionCollection" );
00061 
00062   extensionBar->setShown( false );
00063   TQTimer::singleShot( 0, this, TQT_SLOT( createActions() ) );
00064 }
00065 
00066 ExtensionManager::~ExtensionManager()
00067 {
00068 }
00069 
00070 
00071 void ExtensionManager::restoreSettings()
00072 {
00073   const TQStringList activeExtensions = KABPrefs::instance()->activeExtensions();
00074 
00075   typedef TQMap<TQString, ExtensionData>::ConstIterator ConstIterator;
00076   for ( ConstIterator it = mExtensionMap.begin(), end = mExtensionMap.end(); it != end; ++it ) {
00077     if ( activeExtensions.contains( it.data().identifier ) ) {
00078       TDEToggleAction *action = static_cast<TDEToggleAction*>( it.data().action );
00079       if ( action )
00080         action->setChecked( true );
00081       setExtensionActive( it.data().identifier, true );
00082     }
00083   }
00084   const TQValueList<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 ( TQStringList::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 TQString &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 TQString& 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 TQSignalMapper( this, "SignalMapper" );
00165   connect( mMapper, TQT_SIGNAL( mapped( const TQString& ) ),
00166            this, TQT_SLOT( activationToggled( const TQString& ) ) );
00167 
00168   ExtensionData::List::ConstIterator it;
00169   for ( TQMap<TQString, ExtensionData>::Iterator it = mExtensionMap.begin(), end = mExtensionMap.end(); it != end; ++it ) {
00170     ExtensionData& data = it.data();
00171     data.action = new TDEToggleAction( data.title, 0, mMapper, TQT_SLOT( map() ),
00172                                                mActionCollection,
00173                                                TQString( 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 TDEActionSeparator( mActionCollection ) );
00182   mCore->guiClient()->plugActionList( "extensions_list", mActionList );
00183 }
00184 
00185 TQWidget* ExtensionManager::activeDetailsWidget() const
00186 {
00187     return mActiveDetailsWidget;
00188 }
00189 
00190 void ExtensionManager::createExtensionWidgets()
00191 {
00192   // clean up
00193   for ( TQMap<TQString, 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, TQT_SIGNAL( modified( const TDEABC::Addressee::List& ) ),
00206              TQT_SIGNAL( modified( const TDEABC::Addressee::List& ) ) );
00207     connect( wdg, TQT_SIGNAL( deleted( const TQStringList& ) ),
00208              TQT_SIGNAL( deleted( const TQStringList& ) ) );
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 TDETrader::OfferList plugins = TDETrader::self()->query( "KAddressBook/Extension",
00220     TQString( "[X-TDE-KAddressBook-ExtensionPluginVersion] == %1" ).arg( KAB_EXTENSIONWIDGET_PLUGIN_VERSION ) );
00221 
00222   TDETrader::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, TQT_SIGNAL( modified( const TDEABC::Addressee::List& ) ),
00243                TQT_SIGNAL( modified( const TDEABC::Addressee::List& ) ) );
00244       connect( wdg, TQT_SIGNAL( deleted( const TQStringList& ) ),
00245                TQT_SIGNAL( deleted( const TQStringList& ) ) );
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"