kutils Library API Documentation

kcmodulecontainer.cpp

00001 /* This file is part of the KDE libraries
00002     Copyright (C) 2004 Frans Englich <frans.englich@telia.com>
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017     Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #include <qlayout.h>
00021 #include <qpixmap.h>
00022 #include <qstringlist.h>
00023 #include <qtabwidget.h>
00024 #include <qtooltip.h>
00025 #include <qvaluelist.h>
00026 
00027 #include <kcmodule.h>
00028 #include <kcmoduleinfo.h>
00029 #include <kcmoduleloader.h>
00030 #include <kcmoduleproxy.h>
00031 #include <kdebug.h>
00032 #include <kdialog.h>
00033 #include <kglobal.h>
00034 #include <kiconloader.h>
00035 #include <kpushbutton.h>
00036 #include <kservice.h>
00037 #include <kstdguiitem.h>
00038 
00039 #include "kcmodulecontainer.h"
00040 #include "kcmodulecontainer.moc"
00041 
00042 /***********************************************************************/
00043 class KCModuleContainer::KCModuleContainerPrivate
00044 {
00045     public:
00046         KCModuleContainerPrivate( const QStringList& mods )
00047             : modules( mods )
00048             , tabWidget( 0 )
00049             , buttons( 0 )
00050             , hasRootKCM( false )
00051             , btnRootMode( 0 )
00052             , btnLayout( 0 )
00053             , topLayout( 0 )
00054             {};
00055 
00056         QStringList modules;
00057         QTabWidget *tabWidget;
00058         int buttons;
00059         bool hasRootKCM;
00060         KPushButton *btnRootMode;
00061         QHBoxLayout *btnLayout;
00062         QVBoxLayout *topLayout;
00063 
00064 
00065 };
00066 /***********************************************************************/
00067 
00068 
00069 
00070 
00071 
00072 /***********************************************************************/
00073 KCModuleContainer::KCModuleContainer( QWidget* parent, const char* name, 
00074     const QString& mods )
00075     : KCModule( parent, name )
00076 {
00077     d = new KCModuleContainerPrivate( QStringList::split( ",", QString(mods).remove( " " )) );
00078     init();
00079 }
00080 
00081 KCModuleContainer::KCModuleContainer( QWidget* parent, const char* name, 
00082     const QStringList& mods )
00083     : KCModule( parent, name ), d( new KCModuleContainerPrivate( mods ) )
00084 {
00085     init();
00086 }
00087 
00088 void KCModuleContainer::init()
00089 {
00090     d->topLayout = new QVBoxLayout( this, 0, KDialog::spacingHint(), "topLayout" );
00091     d->tabWidget = new QTabWidget(this, "tabWidget");
00092     connect( d->tabWidget, SIGNAL( currentChanged( QWidget* ) ), SLOT( tabSwitched( QWidget* ) ));
00093     d->topLayout->addWidget( d->tabWidget );
00094 
00095     if ( !d->modules.isEmpty() )
00096     {
00097         /* Add our modules */
00098         for ( QStringList::Iterator it = d->modules.begin(); it != d->modules.end(); ++it )
00099             addModule( (*it) );
00100 
00101         finalize();
00102     }
00103 
00104 }
00105 
00106 void KCModuleContainer::finalize()
00107 {
00108     setButtons( d->buttons );
00109     if ( d->hasRootKCM ) /* Add a root mode button */
00110     {
00111         if(!d->btnLayout) /* It could already be added */
00112         {
00113             d->btnLayout = new QHBoxLayout(this, 0, 0, "btnLayout");
00114             d->btnRootMode = new KPushButton(KStdGuiItem::adminMode(), this, "btnRootMode");
00115                     
00116             d->btnLayout->addWidget( d->btnRootMode );
00117             d->btnLayout->addStretch();
00118             d->topLayout->addLayout( d->btnLayout );
00119         }
00120     }
00121 }
00122 
00123 void KCModuleContainer::addModule( const QString& module )
00124 {
00125     /* In case it doesn't exist we just silently drop it.
00126      * This allows people to easily extend containers.
00127      * For example, KCM monitor gamma can be in kdegraphics.
00128      */
00129     if ( !KService::serviceByDesktopName( module ) )
00130     {
00131         kdDebug(713) << "KCModuleContainer: module '" << 
00132             module << "' was not found and thus not loaded" << endl;
00133         return;
00134     }
00135 
00136     if( !KCModuleLoader::testModule( module ))
00137         return;
00138 
00139     KCModuleProxy* proxy = new KCModuleProxy( module, false, d->tabWidget, module.latin1());
00140 
00141     d->tabWidget->addTab( proxy, QIconSet(KGlobal::iconLoader()->loadIcon( proxy->moduleInfo().icon(), KIcon::Desktop)),
00142             /* QT eats ampersands for dinner. But not this time. */
00143             proxy->moduleInfo().moduleName().replace( "&", "&&" ));
00144 
00145     d->tabWidget->setTabToolTip( proxy, proxy->moduleInfo().comment() );
00146 
00147     connect( proxy, SIGNAL(changed(KCModuleProxy *)), SLOT(moduleChanged(KCModuleProxy *)));
00148 
00149     /* Collect our buttons - we go for the common deliminator */
00150     d->buttons = d->buttons | proxy->realModule()->buttons();
00151 
00152     /* If we should add an Administrator Mode button */
00153     if ( proxy->moduleInfo().needsRootPrivileges() )
00154         d->hasRootKCM=true;
00155 
00156 
00157 }
00158 
00159 void KCModuleContainer::tabSwitched( QWidget * module )
00160 {
00161     if ( !d->hasRootKCM )
00162         return;
00163 
00164     /* Not like this. Not like this. */
00165     disconnect( d->btnRootMode, 0, 0, 0 );
00166     /* Welcome to the real world huh baby? */
00167     
00168     KCModuleProxy* mod = (KCModuleProxy *) module;
00169 
00170     if ( mod->moduleInfo().needsRootPrivileges() && !mod->rootMode() )
00171     {
00172         d->btnRootMode->setEnabled( true );
00173         connect( d->btnRootMode, SIGNAL( clicked() ), 
00174                 SLOT( runAsRoot() ));
00175         connect( mod, SIGNAL( childClosed() ), 
00176                 SLOT ( rootExited() ));
00177     }
00178     else
00179         d->btnRootMode->setEnabled( false );
00180 
00181     setQuickHelp( mod->quickHelp() );
00182     setAboutData( const_cast<KAboutData*>(mod->aboutData()) );
00183 
00184 }
00185 
00186 void KCModuleContainer::runAsRoot()
00187 {
00188     if ( d->tabWidget->currentPage() )
00189         ( (KCModuleProxy *) d->tabWidget->currentPage() )->runAsRoot();
00190     d->btnRootMode->setEnabled( false );
00191 }
00192 
00193 void KCModuleContainer::rootExited()
00194 {
00195     connect( d->btnRootMode, SIGNAL( clicked() ), SLOT( runAsRoot() ));
00196     d->btnRootMode->setEnabled( true );
00197 }
00198 
00199 void KCModuleContainer::save()
00200 {
00201     ModuleList list = changedModules;
00202     ModuleList::iterator it;
00203     for ( it = list.begin() ; it !=list.end() ; ++it )
00204     {
00205         (*it)->save();
00206     }
00207 
00208     emit changed( false );
00209 
00210 }
00211 
00212 void KCModuleContainer::load()
00213 {
00214     ModuleList list = allModules;
00215     ModuleList::iterator it;
00216     for ( it = list.begin() ; it !=list.end() ; ++it )
00217     {
00218         (*it)->load();
00219     }
00220 
00221     emit changed( false );
00222 
00223 }
00224 
00225 void KCModuleContainer::defaults()
00226 {
00227     ModuleList list = allModules;
00228     ModuleList::iterator it;
00229     for ( it = list.begin() ; it !=list.end() ; ++it )
00230     {
00231         (*it)->defaults();
00232     }
00233 
00234     emit changed( true );
00235 }
00236 
00237 
00238 void KCModuleContainer::moduleChanged(KCModuleProxy * proxy)
00239 {
00240     changedModules.append( proxy );
00241     if( changedModules.isEmpty() )
00242         return;
00243 
00244     emit changed(true);
00245 }
00246 
00247 KCModuleContainer::~KCModuleContainer()
00248 {
00249     delete d;
00250 }
00251 
00252 /***********************************************************************/
00253 
00254 
00255 
00256 
KDE Logo
This file is part of the documentation for kutils Library Version 3.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Jul 20 12:45:02 2006 by doxygen 1.4.4 written by Dimitri van Heesch, © 1997-2003