• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KUtils

kcmultidialog.cpp

Go to the documentation of this file.
00001 /*
00002    Copyright (c) 2000 Matthias Elter <elter@kde.org>
00003    Copyright (c) 2003 Daniel Molkentin <molkentin@kde.org>
00004    Copyright (c) 2003,2006 Matthias Kretz <kretz@kde.org>
00005    Copyright (c) 2004 Frans Englich <frans.englich@telia.com>
00006    Copyright (c) 2006 Tobias Koenig <tokoe@kde.org>
00007 
00008    This library is free software; you can redistribute it and/or
00009    modify it under the terms of the GNU Library General Public
00010    License as published by the Free Software Foundation; either
00011    version 2 of the License, or (at your option) any later version.
00012 
00013    This library is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016    Library General Public License for more details.
00017 
00018    You should have received a copy of the GNU Library General Public License
00019    along with this library; see the file COPYING.LIB.  If not, write to
00020    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00021    Boston, MA 02110-1301, USA.
00022 
00023 */
00024 
00025 #include "kcmultidialog.h"
00026 #include "kcmultidialog_p.h"
00027 
00028 #include <QtCore/QStringList>
00029 #include <QtCore/QProcess>
00030 
00031 #include <kauthorized.h>
00032 #include <kguiitem.h>
00033 #include <khbox.h>
00034 #include <kicon.h>
00035 #include <klocale.h>
00036 #include <kpagewidgetmodel.h>
00037 #include <kpushbutton.h>
00038 #include <ktoolinvocation.h>
00039 #include <kdebug.h>
00040 
00041 #include "kcmoduleloader.h"
00042 #include "kcmoduleproxy.h"
00043 
00044 void KCMultiDialogPrivate::_k_slotCurrentPageChanged( KPageWidgetItem *item )
00045 {
00046   kDebug(710) ;
00047 
00048   if ( !item )
00049     return;
00050 
00051   KCModuleProxy *module = 0;
00052   for ( int i = 0; i < modules.count(); ++i ) {
00053     if ( modules[ i ].item == item ) {
00054       module = modules[ i ].kcm;
00055       break;
00056     }
00057   }
00058 
00059   if ( !module )
00060     return;
00061   kDebug(710) << "found module for page: " << module->moduleInfo().moduleName();
00062 
00063   currentModule = module;
00064 
00065   updateButtons(currentModule);
00066 }
00067 
00068 void KCMultiDialogPrivate::updateButtons(KCModuleProxy *currentModule)
00069 {
00070     Q_Q(KCMultiDialog);
00071     q->enableButton(KDialog::Help, currentModule->buttons() & KCModule::Help);
00072     q->enableButton(KDialog::Default, currentModule->buttons() & KCModule::Default);
00073 }
00074 
00075 void KCMultiDialogPrivate::_k_clientChanged()
00076 {
00077     Q_Q(KCMultiDialog);
00078   for ( int i = 0; i < modules.count(); ++i ) {
00079     if ( modules[ i ].kcm->changed() ) {
00080             q->enableButton(KDialog::Apply, true);
00081       return;
00082     }
00083   }
00084 
00085     q->enableButton(KDialog::Apply, false);
00086 }
00087 
00088 void KCMultiDialogPrivate::_k_dialogClosed()
00089 {
00090   kDebug(710) ;
00091 
00097   for ( int i = 0; i < modules.count(); ++i )
00098     modules[ i ].kcm->deleteClient();
00099 }
00100 
00101 void KCMultiDialogPrivate::init()
00102 {
00103     Q_Q(KCMultiDialog);
00104     q->setFaceType(KPageDialog::Auto);
00105     q->setCaption(i18n("Configure"));
00106     q->setButtons(KDialog::Help | KDialog::Default |KDialog::Cancel | KDialog::Apply | KDialog::Ok | KDialog::User1);
00107     q->setButtonGuiItem(KDialog::User1, KStandardGuiItem::reset());
00108     q->setDefaultButton(KDialog::Ok);
00109     q->setModal(false);
00110     q->showButtonSeparator(true);
00111 
00112     q->connect(q, SIGNAL(finished()), SLOT(_k_dialogClosed()));
00113 
00114     q->showButton(KDialog::User1, false);
00115     q->enableButton(KDialog::Apply, false);
00116 
00117     q->connect(q, SIGNAL(currentPageChanged(KPageWidgetItem*, KPageWidgetItem*)),
00118             SLOT(_k_slotCurrentPageChanged(KPageWidgetItem*)));
00119 
00120     q->connect(q, SIGNAL(applyClicked()), SLOT(slotApplyClicked()));
00121     q->connect(q, SIGNAL(okClicked()), SLOT(slotOkClicked()));
00122     q->connect(q, SIGNAL(defaultClicked()), SLOT(slotDefaultClicked()));
00123     q->connect(q, SIGNAL(helpClicked()), SLOT(slotHelpClicked()));
00124     q->connect(q, SIGNAL(user1Clicked()), SLOT(slotUser1Clicked()));
00125 
00126     q->setInitialSize(QSize(800, 550));
00127 }
00128 
00129 KCMultiDialog::KCMultiDialog( QWidget *parent )
00130     : KPageDialog(*new KCMultiDialogPrivate, NULL, parent)
00131 {
00132     d_func()->init();
00133 }
00134 
00135 KCMultiDialog::KCMultiDialog(KPageWidget *pageWidget, QWidget *parent, Qt::WFlags flags)
00136     : KPageDialog(*new KCMultiDialogPrivate, pageWidget, parent, flags)
00137 {
00138     d_func()->init();
00139 }
00140 
00141 KCMultiDialog::KCMultiDialog(KCMultiDialogPrivate &dd, KPageWidget *pageWidget, QWidget *parent, Qt::WFlags flags)
00142     : KPageDialog(dd, pageWidget, parent, flags)
00143 {
00144     d_func()->init();
00145 }
00146 
00147 KCMultiDialog::~KCMultiDialog()
00148 {
00149 }
00150 
00151 void KCMultiDialog::slotDefaultClicked()
00152 {
00153     Q_D(KCMultiDialog);
00154   const KPageWidgetItem *item = currentPage();
00155   if ( !item )
00156     return;
00157 
00158   for ( int i = 0; i < d->modules.count(); ++i ) {
00159     if ( d->modules[ i ].item == item ) {
00160       d->modules[ i ].kcm->defaults();
00161             d->_k_clientChanged();
00162       return;
00163     }
00164   }
00165 }
00166 
00167 void KCMultiDialog::slotUser1Clicked()
00168 {
00169   const KPageWidgetItem *item = currentPage();
00170   if ( !item )
00171     return;
00172 
00173     Q_D(KCMultiDialog);
00174   for ( int i = 0; i < d->modules.count(); ++i ) {
00175     if ( d->modules[ i ].item == item ) {
00176       d->modules[ i ].kcm->load();
00177             d->_k_clientChanged();
00178       return;
00179     }
00180   }
00181 }
00182 
00183 void KCMultiDialogPrivate::apply()
00184 {
00185     Q_Q(KCMultiDialog);
00186     QStringList updatedComponents;
00187 
00188     foreach (const CreatedModule &module, modules) {
00189         KCModuleProxy *proxy = module.kcm;
00190 
00191         if (proxy->changed()) {
00192             proxy->save();
00193 
00198             const QStringList componentNames = module.componentNames;
00199             foreach (const QString &componentName, module.componentNames) {
00200                 if (!updatedComponents.contains(componentName)) {
00201                     updatedComponents.append(componentName);
00202                 }
00203             }
00204         }
00205     }
00206 
00207     // Send the configCommitted signal for every updated component.
00208     foreach (const QString &name, updatedComponents) {
00209         emit q->configCommitted(name.toLatin1());
00210     }
00211 
00212     emit q->configCommitted();
00213 }
00214 
00215 void KCMultiDialog::slotApplyClicked()
00216 {
00217   setButtonFocus( Apply );
00218 
00219     d_func()->apply();
00220 }
00221 
00222 
00223 void KCMultiDialog::slotOkClicked()
00224 {
00225   setButtonFocus( Ok );
00226 
00227     d_func()->apply();
00228   accept();
00229 }
00230 
00231 void KCMultiDialog::slotHelpClicked()
00232 {
00233   const KPageWidgetItem *item = currentPage();
00234   if ( !item )
00235     return;
00236 
00237     Q_D(KCMultiDialog);
00238   QString docPath;
00239   for ( int i = 0; i < d->modules.count(); ++i ) {
00240     if ( d->modules[ i ].item == item ) {
00241       docPath = d->modules[ i ].kcm->moduleInfo().docPath();
00242       break;
00243     }
00244   }
00245 
00246   KUrl docUrl( KUrl( "help:/" ), docPath );
00247   if ( docUrl.protocol() == "help" || docUrl.protocol() == "man" || docUrl.protocol() == "info" ) {
00248     QProcess::startDetached("khelpcenter", QStringList() << docUrl.url());
00249   } else {
00250     KToolInvocation::invokeBrowser( docUrl.url() );
00251   }
00252 }
00253 
00254 
00255 KPageWidgetItem* KCMultiDialog::addModule( const QString& path, const QStringList& args )
00256 {
00257   QString complete = path;
00258 
00259   if ( !path.endsWith( ".desktop" ) )
00260     complete += ".desktop";
00261 
00262   KService::Ptr service = KService::serviceByStorageId( complete );
00263 
00264   return addModule( KCModuleInfo( service ), 0, args );
00265 }
00266 
00267 KPageWidgetItem* KCMultiDialog::addModule( const KCModuleInfo& moduleInfo,
00268                                            KPageWidgetItem *parentItem, const QStringList& args )
00269 {
00270   if ( !moduleInfo.service() )
00271     return 0;
00272 
00273   //KAuthorized::authorizeControlModule( moduleInfo.service()->menuId() ) is
00274   //checked in noDisplay already
00275   if ( moduleInfo.service()->noDisplay() )
00276     return 0;
00277 
00278     KCModuleProxy *kcm = new KCModuleProxy(moduleInfo, 0, args);
00279 
00280     kDebug(710) << moduleInfo.moduleName();
00281     KPageWidgetItem *item = new KPageWidgetItem(kcm, moduleInfo.moduleName());
00282   item->setHeader( moduleInfo.comment() );
00283   item->setIcon( KIcon( moduleInfo.icon() ) );
00284     item->setProperty("_k_weight", moduleInfo.weight());
00285 
00286     bool updateCurrentPage = false;
00287     const KPageWidgetModel *model = qobject_cast<const KPageWidgetModel *>(pageWidget()->model());
00288     Q_ASSERT(model);
00289     if (parentItem) {
00290         const QModelIndex parentIndex = model->index(parentItem);
00291         const int siblingCount = model->rowCount(parentIndex);
00292         int row = 0;
00293         for (; row < siblingCount; ++row) {
00294             KPageWidgetItem *siblingItem = model->item(parentIndex.child(row, 0));
00295             if (siblingItem->property("_k_weight").toInt() > moduleInfo.weight()) {
00296                 // the item we found is heavier than the new module
00297                 kDebug(710) << "adding KCM " << item->name() << " before " << siblingItem->name();
00298                 insertPage(siblingItem, item);
00299                 break;
00300             }
00301         }
00302         if (row >= siblingCount) {
00303             // the new module is either the first or the heaviest item
00304             kDebug(710) << "adding KCM " << item->name() << " with parent " << parentItem->name();
00305             addSubPage(parentItem, item);
00306         }
00307     } else {
00308         const int siblingCount = model->rowCount();
00309         int row = 0;
00310         for (; row < siblingCount; ++row) {
00311             KPageWidgetItem *siblingItem = model->item(model->index(row, 0));
00312             if (siblingItem->property("_k_weight").toInt() > moduleInfo.weight()) {
00313                 // the item we found is heavier than the new module
00314                 kDebug(710) << "adding KCM " << item->name() << " before " << siblingItem->name();
00315                 insertPage(siblingItem, item);
00316                 if ( siblingItem == currentPage() )
00317                     updateCurrentPage = true;
00318 
00319                 break;
00320             }
00321         }
00322         if (row == siblingCount) {
00323             // the new module is either the first or the heaviest item
00324             kDebug(710) << "adding KCM " << item->name() << " at the top level";
00325             addPage(item);
00326         }
00327     }
00328 
00329     connect(kcm, SIGNAL(changed(bool)), this, SLOT(_k_clientChanged()));
00330 
00331 
00332     Q_D(KCMultiDialog);
00333   KCMultiDialogPrivate::CreatedModule cm;
00334   cm.kcm = kcm;
00335   cm.item = item;
00336   cm.componentNames = moduleInfo.service()->property( "X-KDE-ParentComponents" ).toStringList();
00337   d->modules.append( cm );
00338 
00339   if ( d->modules.count() == 1 || updateCurrentPage )
00340   {
00341     setCurrentPage( item );
00342     d->updateButtons(kcm);
00343   }
00344   return item;
00345 }
00346 
00347 void KCMultiDialog::clear()
00348 {
00349     Q_D(KCMultiDialog);
00350   kDebug( 710 ) ;
00351 
00352   for ( int i = 0; i < d->modules.count(); ++i ) {
00353     removePage( d->modules[ i ].item );
00354     delete d->modules[ i ].kcm;
00355   }
00356 
00357   d->modules.clear();
00358 
00359     d->_k_clientChanged();
00360 }
00361 
00362 
00363 
00364 #include "kcmultidialog.moc"

KUtils

Skip menu "KUtils"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal