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 KURL url( KURL("help:/"), _docPath ); 00162 00163 if (url.protocol() == "help" || url.protocol() == "man" || url.protocol() == "info") { 00164 KProcess process; 00165 process << "khelpcenter" 00166 << url.url(); 00167 process.start(KProcess::DontCare); 00168 process.detach(); 00169 } else { 00170 new KRun(url); 00171 } 00172 } 00173 00174 void KCMultiDialog::clientChanged(bool state) 00175 { 00176 kdDebug( 710 ) << k_funcinfo << state << endl; 00177 ModuleList::Iterator end = m_modules.end(); 00178 for( ModuleList::Iterator it = m_modules.begin(); it != end; ++it ) 00179 if( ( *it ).kcm->changed() ) 00180 { 00181 enableButton( Apply, true ); 00182 return; 00183 } 00184 enableButton( Apply, false ); 00185 } 00186 00187 void KCMultiDialog::addModule(const QString& path, bool withfallback) 00188 { 00189 kdDebug(710) << "KCMultiDialog::addModule " << path << endl; 00190 00191 KService::Ptr s = KService::serviceByStorageId(path); 00192 if (!s) { 00193 kdError() << "Desktop file '" << path << "' not found!" << endl; 00194 return; 00195 } 00196 00197 KCModuleInfo info(s); 00198 addModule(info, QStringList(), withfallback); 00199 } 00200 00201 void KCMultiDialog::addModule(const KCModuleInfo& moduleinfo, 00202 QStringList parentmodulenames, bool withfallback) 00203 { 00204 kdDebug(710) << "KCMultiDialog::addModule " << moduleinfo.moduleName() << 00205 endl; 00206 00207 QFrame* page = 0; 00208 if (!moduleinfo.service()->noDisplay()) 00209 switch( dialogface ) 00210 { 00211 case TreeList: 00212 parentmodulenames += moduleinfo.moduleName();; 00213 page = addHBoxPage( parentmodulenames, moduleinfo.comment(), 00214 SmallIcon( moduleinfo.icon(), 00215 IconSize( KIcon::Small ) ) ); 00216 break; 00217 case IconList: 00218 page = addHBoxPage( moduleinfo.moduleName(), 00219 moduleinfo.comment(), DesktopIcon( moduleinfo.icon(), 00220 KIcon::SizeMedium ) ); 00221 break; 00222 case Plain: 00223 page = plainPage(); 00224 ( new QHBoxLayout( page ) )->setAutoAdd( true ); 00225 break; 00226 default: 00227 kdError( 710 ) << "unsupported dialog face for KCMultiDialog" 00228 << endl; 00229 break; 00230 } 00231 if(!page) { 00232 KCModuleLoader::unloadModule(moduleinfo); 00233 return; 00234 } 00235 KCModuleProxy * module; 00236 if( m_orphanModules.contains( moduleinfo.service() ) ) 00237 { 00238 // the KCModule already exists - it was removed from the dialog in 00239 // removeAllModules 00240 module = m_orphanModules[ moduleinfo.service() ]; 00241 m_orphanModules.remove( moduleinfo.service() ); 00242 kdDebug( 710 ) << "use KCModule from the list of orphans for " << 00243 moduleinfo.moduleName() << ": " << module << endl; 00244 00245 module->reparent( page, 0, QPoint( 0, 0 ), true ); 00246 00247 if( module->changed() ) 00248 clientChanged( true ); 00249 00250 if( activePageIndex() == -1 ) 00251 showPage( pageIndex( page ) ); 00252 } 00253 else 00254 { 00255 module = new KCModuleProxy( moduleinfo, withfallback, page ); 00256 QStringList parentComponents = moduleinfo.service()->property( 00257 "X-KDE-ParentComponents" ).toStringList(); 00258 kdDebug(710) << k_funcinfo << "ParentComponents=" << parentComponents 00259 << endl; 00260 moduleParentComponents.insert( module, 00261 new QStringList( parentComponents ) ); 00262 00263 connect(module, SIGNAL(changed(bool)), this, SLOT(clientChanged(bool))); 00264 00265 //setHelp( docpath, QString::null ); 00266 // FIXME: this will break if two KCMs have a different docPath 00267 _docPath = moduleinfo.docPath(); 00268 00269 if( m_modules.count() == 0 ) 00270 aboutToShowPage( page ); 00271 } 00272 CreatedModule cm; 00273 cm.kcm = module; 00274 cm.service = moduleinfo.service(); 00275 m_modules.append( cm ); 00276 } 00277 00278 void KCMultiDialog::removeAllModules() 00279 { 00280 kdDebug( 710 ) << k_funcinfo << endl; 00281 ModuleList::Iterator end = m_modules.end(); 00282 for( ModuleList::Iterator it = m_modules.begin(); it != end; ++it ) 00283 { 00284 kdDebug( 710 ) << "remove 2" << endl; 00285 KCModuleProxy * kcm = ( *it ).kcm; 00286 QObject * page = kcm->parent(); 00287 kcm->hide(); 00288 if( page ) 00289 { 00290 // I hate this 00291 kcm->reparent( 0, QPoint( 0, 0 ), false ); 00292 delete page; 00293 } 00294 m_orphanModules[ ( *it ).service ] = kcm; 00295 kdDebug( 710 ) << "added KCModule to the list of orphans: " << 00296 kcm << endl; 00297 } 00298 m_modules.clear(); 00299 // all modules are gone, none can be changed 00300 clientChanged( false ); 00301 } 00302 00303 void KCMultiDialog::show() 00304 { 00305 if( ! isVisible() ) 00306 { 00307 // call load() method of all KCMs 00308 ModuleList::Iterator end = m_modules.end(); 00309 for( ModuleList::Iterator it = m_modules.begin(); it != end; ++it ) 00310 ( *it ).kcm->load(); 00311 } 00312 KDialogBase::show(); 00313 } 00314 00315 void KCMultiDialog::slotAboutToShow(QWidget *page) 00316 { 00317 kdDebug( 710 ) << k_funcinfo << endl; 00318 // honor KCModule::buttons 00319 QObject * obj = page->child( 0, "KCModuleProxy" ); 00320 if( ! obj ) 00321 return; 00322 KCModuleProxy * module = ( KCModuleProxy* )obj->qt_cast( 00323 "KCModuleProxy" ); 00324 if( ! module ) 00325 return; 00326 // TODO: if the dialogface is Plain we should hide the buttons instead of 00327 // disabling 00328 enableButton( KDialogBase::Help, 00329 module->buttons() & KCModule::Help ); 00330 enableButton( KDialogBase::Default, 00331 module->buttons() & KCModule::Default ); 00332 } 00333 00334 // vim: sw=4 et sts=4
KDE Logo
This file is part of the documentation for kutils Library Version 3.2.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 12 15:09:15 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003