kutils Library API Documentation

kcmultidialog.cpp

00001 /* 00002 Copyright (c) 2000 Matthias Elter <elter@kde.org> 00003 Copyright (c) 2003 Daniel Molkentin <molkentin@kde.org> 00004 Copyright (c) 2003 Matthias Kretz <kretz@kde.org> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to 00018 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00019 Boston, MA 02111-1307, USA. 00020 00021 */ 00022 00023 #include <qhbox.h> 00024 #include <qcursor.h> 00025 00026 #include <klocale.h> 00027 #include <kdebug.h> 00028 #include <kiconloader.h> 00029 #include <kmessagebox.h> 00030 #include <klibloader.h> 00031 #include <krun.h> 00032 #include <kprocess.h> 00033 #include <kaboutdata.h> 00034 00035 #include "kcmultidialog.h" 00036 #include "kcmultidialog.moc" 00037 #include "kcmoduleloader.h" 00038 #include "kcmoduleproxy.h" 00039 #include <assert.h> 00040 #include <qlayout.h> 00041 00042 KCMultiDialog::KCMultiDialog(QWidget *parent, const char *name, bool modal) 00043 : KDialogBase(IconList, i18n("Configure"), Help | Default |Cancel | Apply | 00044 Ok | User1, Ok, parent, name, modal, true, 00045 KGuiItem( i18n( "&Reset" ), "undo" ) ) 00046 , dialogface( IconList ) 00047 { 00048 showButton( User1, false );; 00049 init(); 00050 } 00051 00052 KCMultiDialog::KCMultiDialog( int dialogFace, const QString & caption, QWidget * parent, const char * name, bool modal ) 00053 : KDialogBase( dialogFace, caption, Help | Default | Cancel | Apply | Ok | 00054 User1, Ok, parent, name, modal, true, 00055 KGuiItem( i18n( "&Reset" ), "undo" ) ) 00056 , dialogface( dialogFace ) 00057 { 00058 showButton( User1, false );; 00059 init(); 00060 } 00061 00062 KCMultiDialog::KCMultiDialog( int dialogFace, const KGuiItem &user2, 00063 const KGuiItem &user3, int buttonMask, const QString &caption, 00064 QWidget *parent, const char *name, bool modal ) 00065 : KDialogBase( dialogFace, caption, buttonMask | Help | Default | Cancel | 00066 Apply | Ok | User1, Ok, parent, name, modal, true, 00067 KGuiItem( i18n( "&Reset" ), "undo" ), user2, user3 ) 00068 , dialogface( dialogFace ) 00069 { 00070 showButton( User1, false );; 00071 init(); 00072 } 00073 00074 inline void KCMultiDialog::init() 00075 { 00076 d = 0L; 00077 enableButton(Apply, false); 00078 connect(this, SIGNAL(aboutToShowPage(QWidget *)), this, SLOT(slotAboutToShow(QWidget *))); 00079 setInitialSize(QSize(640,480)); 00080 moduleParentComponents.setAutoDelete( true ); 00081 } 00082 00083 KCMultiDialog::~KCMultiDialog() 00084 { 00085 OrphanMap::Iterator end2 = m_orphanModules.end(); 00086 for( OrphanMap::Iterator it = m_orphanModules.begin(); it != end2; ++it ) 00087 delete ( *it ); 00088 } 00089 00090 void KCMultiDialog::slotDefault() 00091 { 00092 int curPageIndex = activePageIndex(); 00093 00094 ModuleList::Iterator end = m_modules.end(); 00095 for( ModuleList::Iterator it = m_modules.begin(); it != end; ++it ) 00096 if( pageIndex( ( QWidget * )( *it ).kcm->parent() ) == curPageIndex ) 00097 { 00098 ( *it ).kcm->defaults(); 00099 clientChanged( true ); 00100 return; 00101 } 00102 } 00103 00104 void KCMultiDialog::slotUser1() 00105 { 00106 int curPageIndex = activePageIndex(); 00107 00108 ModuleList::Iterator end = m_modules.end(); 00109 for( ModuleList::Iterator it = m_modules.begin(); it != end; ++it ) 00110 if( pageIndex( ( QWidget * )( *it ).kcm->parent() ) == curPageIndex ) 00111 { 00112 ( *it ).kcm->load(); 00113 clientChanged( false ); 00114 return; 00115 } 00116 } 00117 00118 void KCMultiDialog::apply() 00119 { 00120 QStringList updatedModules; 00121 ModuleList::Iterator end = m_modules.end(); 00122 for( ModuleList::Iterator it = m_modules.begin(); it != end; ++it ) 00123 { 00124 KCModuleProxy * m = ( *it ).kcm; 00125 //kdDebug(710) << k_funcinfo << m->name() << ' ' << 00126 // ( m->aboutData() ? m->aboutData()->appName() : "" ) << endl; 00127 if( m->changed() ) 00128 { 00129 m->save(); 00130 QStringList * names = moduleParentComponents[ m ]; 00131 kdDebug(710) << k_funcinfo << *names << " saved and added to the list" << endl; 00132 for( QStringList::ConstIterator it = names->begin(); it != names->end(); ++it ) 00133 if( updatedModules.find( *it ) == updatedModules.end() ) 00134 updatedModules.append( *it ); 00135 } 00136 } 00137 for( QStringList::const_iterator it = updatedModules.begin(); it != updatedModules.end(); ++it ) 00138 { 00139 kdDebug(710) << k_funcinfo << *it << " " << ( *it ).latin1() << endl; 00140 emit configCommitted( ( *it ).latin1() ); 00141 } 00142 emit configCommitted(); 00143 } 00144 00145 void KCMultiDialog::slotApply() 00146 { 00147 emit applyClicked(); 00148 apply(); 00149 } 00150 00151 00152 void KCMultiDialog::slotOk() 00153 { 00154 emit okClicked(); 00155 apply(); 00156 accept(); 00157 } 00158 00159 void KCMultiDialog::slotHelp() 00160 { 00161 QString docPath; 00162 00163 int curPageIndex = activePageIndex(); 00164 ModuleList::Iterator end = m_modules.end(); 00165 for( ModuleList::Iterator it = m_modules.begin(); it != end; ++it ) 00166 if( pageIndex( ( QWidget * )( *it ).kcm->parent() ) == curPageIndex ) 00167 { 00168 docPath = ( *it ).kcm->moduleInfo().docPath(); 00169 break; 00170 } 00171 00172 KURL url( KURL("help:/"), docPath ); 00173 00174 if (url.protocol() == "help" || url.protocol() == "man" || url.protocol() == "info") { 00175 KProcess process; 00176 process << "khelpcenter" 00177 << url.url(); 00178 process.start(KProcess::DontCare); 00179 process.detach(); 00180 } else { 00181 new KRun(url); 00182 } 00183 } 00184 00185 void KCMultiDialog::clientChanged(bool state) 00186 { 00187 kdDebug( 710 ) << k_funcinfo << state << endl; 00188 ModuleList::Iterator end = m_modules.end(); 00189 for( ModuleList::Iterator it = m_modules.begin(); it != end; ++it ) 00190 if( ( *it ).kcm->changed() ) 00191 { 00192 enableButton( Apply, true ); 00193 return; 00194 } 00195 enableButton( Apply, false ); 00196 } 00197 00198 void KCMultiDialog::addModule(const QString& path, bool withfallback) 00199 { 00200 kdDebug(710) << "KCMultiDialog::addModule " << path << endl; 00201 00202 KService::Ptr s = KService::serviceByStorageId(path); 00203 if (!s) { 00204 kdError() << "Desktop file '" << path << "' not found!" << endl; 00205 return; 00206 } 00207 00208 KCModuleInfo info(s); 00209 addModule(info, QStringList(), withfallback); 00210 } 00211 00212 void KCMultiDialog::addModule(const KCModuleInfo& moduleinfo, 00213 QStringList parentmodulenames, bool withfallback) 00214 { 00215 kdDebug(710) << "KCMultiDialog::addModule " << moduleinfo.moduleName() << 00216 endl; 00217 00218 QFrame* page = 0; 00219 if (!moduleinfo.service()->noDisplay()) 00220 switch( dialogface ) 00221 { 00222 case TreeList: 00223 parentmodulenames += moduleinfo.moduleName(); 00224 page = addHBoxPage( parentmodulenames, moduleinfo.comment(), 00225 SmallIcon( moduleinfo.icon(), 00226 IconSize( KIcon::Small ) ) ); 00227 break; 00228 case IconList: 00229 page = addHBoxPage( moduleinfo.moduleName(), 00230 moduleinfo.comment(), DesktopIcon( moduleinfo.icon(), 00231 KIcon::SizeMedium ) ); 00232 break; 00233 case Plain: 00234 page = plainPage(); 00235 ( new QHBoxLayout( page ) )->setAutoAdd( true ); 00236 break; 00237 default: 00238 kdError( 710 ) << "unsupported dialog face for KCMultiDialog" 00239 << endl; 00240 break; 00241 } 00242 if(!page) { 00243 KCModuleLoader::unloadModule(moduleinfo); 00244 return; 00245 } 00246 KCModuleProxy * module; 00247 if( m_orphanModules.contains( moduleinfo.service() ) ) 00248 { 00249 // the KCModule already exists - it was removed from the dialog in 00250 // removeAllModules 00251 module = m_orphanModules[ moduleinfo.service() ]; 00252 m_orphanModules.remove( moduleinfo.service() ); 00253 kdDebug( 710 ) << "use KCModule from the list of orphans for " << 00254 moduleinfo.moduleName() << ": " << module << endl; 00255 00256 module->reparent( page, 0, QPoint( 0, 0 ), true ); 00257 00258 if( module->changed() ) 00259 clientChanged( true ); 00260 00261 if( activePageIndex() == -1 ) 00262 showPage( pageIndex( page ) ); 00263 } 00264 else 00265 { 00266 module = new KCModuleProxy( moduleinfo, withfallback, page ); 00267 QStringList parentComponents = moduleinfo.service()->property( 00268 "X-KDE-ParentComponents" ).toStringList(); 00269 kdDebug(710) << k_funcinfo << "ParentComponents=" << parentComponents 00270 << endl; 00271 moduleParentComponents.insert( module, 00272 new QStringList( parentComponents ) ); 00273 00274 connect(module, SIGNAL(changed(bool)), this, SLOT(clientChanged(bool))); 00275 00276 if( m_modules.count() == 0 ) 00277 aboutToShowPage( page ); 00278 } 00279 CreatedModule cm; 00280 cm.kcm = module; 00281 cm.service = moduleinfo.service(); 00282 m_modules.append( cm ); 00283 } 00284 00285 void KCMultiDialog::removeAllModules() 00286 { 00287 kdDebug( 710 ) << k_funcinfo << endl; 00288 ModuleList::Iterator end = m_modules.end(); 00289 for( ModuleList::Iterator it = m_modules.begin(); it != end; ++it ) 00290 { 00291 kdDebug( 710 ) << "remove 2" << endl; 00292 KCModuleProxy * kcm = ( *it ).kcm; 00293 QObject * page = kcm->parent(); 00294 kcm->hide(); 00295 if( page ) 00296 { 00297 // I hate this 00298 kcm->reparent( 0, QPoint( 0, 0 ), false ); 00299 delete page; 00300 } 00301 m_orphanModules[ ( *it ).service ] = kcm; 00302 kdDebug( 710 ) << "added KCModule to the list of orphans: " << 00303 kcm << endl; 00304 } 00305 m_modules.clear(); 00306 // all modules are gone, none can be changed 00307 clientChanged( false ); 00308 } 00309 00310 void KCMultiDialog::show() 00311 { 00312 if( ! isVisible() ) 00313 { 00314 // call load() method of all KCMs 00315 ModuleList::Iterator end = m_modules.end(); 00316 for( ModuleList::Iterator it = m_modules.begin(); it != end; ++it ) 00317 ( *it ).kcm->load(); 00318 } 00319 KDialogBase::show(); 00320 } 00321 00322 void KCMultiDialog::slotAboutToShow(QWidget *page) 00323 { 00324 kdDebug( 710 ) << k_funcinfo << endl; 00325 // honor KCModule::buttons 00326 QObject * obj = page->child( 0, "KCModuleProxy" ); 00327 if( ! obj ) 00328 return; 00329 KCModuleProxy * module = ( KCModuleProxy* )obj->qt_cast( 00330 "KCModuleProxy" ); 00331 if( ! module ) 00332 return; 00333 // TODO: if the dialogface is Plain we should hide the buttons instead of 00334 // disabling 00335 enableButton( KDialogBase::Help, 00336 module->buttons() & KCModule::Help ); 00337 enableButton( KDialogBase::Default, 00338 module->buttons() & KCModule::Default ); 00339 } 00340 00341 // vim: sw=4 et sts=4
KDE Logo
This file is part of the documentation for kutils Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Sep 29 09:41:34 2004 by doxygen 1.3.8 written by Dimitri van Heesch, © 1997-2003