TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kcmodulecontainer.cpp 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* This file is part of the KDE libraries
  2. Copyright (C) 2004 Frans Englich <frans.englich@telia.com>
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public
  5. License as published by the Free Software Foundation; either
  6. version 2 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public License
  12. along with this library; see the file COPYING.LIB. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  14. Boston, MA 02110-1301, USA.
  15. */
  16. #include <tqlayout.h>
  17. #include <tqpixmap.h>
  18. #include <tqstringlist.h>
  19. #include <tqtabwidget.h>
  20. #include <tqtooltip.h>
  21. #include <tqvaluelist.h>
  22. #include <kcmodule.h>
  23. #include <kcmoduleinfo.h>
  24. #include <kcmoduleloader.h>
  25. #include <kcmoduleproxy.h>
  26. #include <kdebug.h>
  27. #include <kdialog.h>
  28. #include <kglobal.h>
  29. #include <kiconloader.h>
  30. #include <kpushbutton.h>
  31. #include <kservice.h>
  32. #include <kstdguiitem.h>
  33. #include "kcmodulecontainer.h"
  34. #include "kcmodulecontainer.moc"
  35. /***********************************************************************/
  36. class KCModuleContainer::KCModuleContainerPrivate
  37. {
  38. public:
  39. KCModuleContainerPrivate( const TQStringList& mods )
  40. : modules( mods )
  41. , tabWidget( 0 )
  42. , buttons( 0 )
  43. , hasRootKCM( false )
  44. , btnRootMode( 0 )
  45. , btnLayout( 0 )
  46. , topLayout( 0 )
  47. {}
  48. TQStringList modules;
  49. TQTabWidget *tabWidget;
  50. int buttons;
  51. bool hasRootKCM: 1;
  52. KPushButton *btnRootMode;
  53. TQHBoxLayout *btnLayout;
  54. TQVBoxLayout *topLayout;
  55. };
  56. /***********************************************************************/
  57. /***********************************************************************/
  58. KCModuleContainer::KCModuleContainer( TQWidget* parent, const char* name,
  59. const TQString& mods )
  60. : KCModule( parent, name )
  61. {
  62. d = new KCModuleContainerPrivate( TQStringList::split( ",", TQString(mods).remove( " " )) );
  63. init();
  64. }
  65. KCModuleContainer::KCModuleContainer( TQWidget* parent, const char* name,
  66. const TQStringList& mods )
  67. : KCModule( parent, name ), d( new KCModuleContainerPrivate( mods ) )
  68. {
  69. init();
  70. }
  71. void KCModuleContainer::init()
  72. {
  73. d->topLayout = new TQVBoxLayout( this, 0, KDialog::spacingHint(), "topLayout" );
  74. d->tabWidget = new TQTabWidget(this, "tabWidget");
  75. d->tabWidget->setMargin(KDialog::marginHint());
  76. connect( d->tabWidget, TQT_SIGNAL( currentChanged( TQWidget* ) ), TQT_SLOT( tabSwitched( TQWidget* ) ));
  77. d->topLayout->addWidget( d->tabWidget );
  78. if ( !d->modules.isEmpty() )
  79. {
  80. /* Add our modules */
  81. for ( TQStringList::Iterator it = d->modules.begin(); it != d->modules.end(); ++it )
  82. addModule( (*it) );
  83. finalize();
  84. }
  85. }
  86. void KCModuleContainer::finalize()
  87. {
  88. setButtons( d->buttons );
  89. if ( d->hasRootKCM ) /* Add a root mode button */
  90. {
  91. if(!d->btnLayout) /* It could already be added */
  92. {
  93. d->btnLayout = new TQHBoxLayout(this, 0, 0, "btnLayout");
  94. d->btnRootMode = new KPushButton(KStdGuiItem::adminMode(), this, "btnRootMode");
  95. d->btnLayout->addWidget( d->btnRootMode );
  96. d->btnLayout->addStretch();
  97. d->topLayout->addLayout( d->btnLayout );
  98. }
  99. }
  100. }
  101. void KCModuleContainer::addModule( const TQString& module )
  102. {
  103. /* In case it doesn't exist we just silently drop it.
  104. * This allows people to easily extend containers.
  105. * For example, KCM monitor gamma can be in kdegraphics.
  106. */
  107. if ( !KService::serviceByDesktopName( module ) )
  108. {
  109. kdDebug(713) << "KCModuleContainer: module '" <<
  110. module << "' was not found and thus not loaded" << endl;
  111. return;
  112. }
  113. if( !KCModuleLoader::testModule( module ))
  114. return;
  115. KCModuleProxy* proxy = new KCModuleProxy( module, false, d->tabWidget, module.latin1());
  116. allModules.append( proxy );
  117. d->tabWidget->addTab( proxy, TQIconSet(KGlobal::iconLoader()->loadIcon(
  118. proxy->moduleInfo().icon(), KIcon::Desktop)),
  119. /* QT eats ampersands for dinner. But not this time. */
  120. proxy->moduleInfo().moduleName().replace( "&", "&&" ));
  121. d->tabWidget->setTabToolTip( proxy, proxy->moduleInfo().comment() );
  122. connect( proxy, TQT_SIGNAL(changed(KCModuleProxy *)), TQT_SLOT(moduleChanged(KCModuleProxy *)));
  123. /* Collect our buttons - we go for the common deliminator */
  124. d->buttons = d->buttons | proxy->realModule()->buttons();
  125. /* If we should add an Administrator Mode button */
  126. if ( proxy->moduleInfo().needsRootPrivileges() )
  127. d->hasRootKCM=true;
  128. }
  129. void KCModuleContainer::tabSwitched( TQWidget * module )
  130. {
  131. if ( !d->hasRootKCM )
  132. return;
  133. /* Not like this. Not like this. */
  134. disconnect( d->btnRootMode, 0, 0, 0 );
  135. /* Welcome to the real world huh baby? */
  136. KCModuleProxy* mod = (KCModuleProxy *) module;
  137. if ( mod->moduleInfo().needsRootPrivileges() && !mod->rootMode() )
  138. {
  139. d->btnRootMode->setEnabled( true );
  140. connect( d->btnRootMode, TQT_SIGNAL( clicked() ),
  141. TQT_SLOT( runAsRoot() ));
  142. connect( mod, TQT_SIGNAL( childClosed() ),
  143. TQT_SLOT ( rootExited() ));
  144. }
  145. else
  146. d->btnRootMode->setEnabled( false );
  147. setQuickHelp( mod->quickHelp() );
  148. setAboutData( const_cast<KAboutData*>(mod->aboutData()) );
  149. }
  150. void KCModuleContainer::runAsRoot()
  151. {
  152. if ( d->tabWidget->currentPage() )
  153. ( (KCModuleProxy *) d->tabWidget->currentPage() )->runAsRoot();
  154. d->btnRootMode->setEnabled( false );
  155. }
  156. void KCModuleContainer::rootExited()
  157. {
  158. connect( d->btnRootMode, TQT_SIGNAL( clicked() ), TQT_SLOT( runAsRoot() ));
  159. d->btnRootMode->setEnabled( true );
  160. }
  161. void KCModuleContainer::save()
  162. {
  163. ModuleList list = changedModules;
  164. ModuleList::iterator it;
  165. for ( it = list.begin() ; it !=list.end() ; ++it )
  166. {
  167. (*it)->save();
  168. }
  169. emit changed( false );
  170. }
  171. void KCModuleContainer::load()
  172. {
  173. ModuleList list = allModules;
  174. ModuleList::iterator it;
  175. for ( it = list.begin() ; it !=list.end() ; ++it )
  176. {
  177. (*it)->load();
  178. }
  179. emit changed( false );
  180. }
  181. void KCModuleContainer::defaults()
  182. {
  183. ModuleList list = allModules;
  184. ModuleList::iterator it;
  185. for ( it = list.begin() ; it !=list.end() ; ++it )
  186. {
  187. (*it)->defaults();
  188. }
  189. emit changed( true );
  190. }
  191. void KCModuleContainer::moduleChanged(KCModuleProxy * proxy)
  192. {
  193. changedModules.append( proxy );
  194. if( changedModules.isEmpty() )
  195. return;
  196. emit changed(true);
  197. }
  198. KCModuleContainer::~KCModuleContainer()
  199. {
  200. delete d;
  201. }
  202. /***********************************************************************/