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

KUtils

dialog.cpp

Go to the documentation of this file.
00001 /*  This file is part of the KDE project
00002     Copyright (C) 2003 Matthias Kretz <kretz@kde.org>
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 version 2 as published by the Free Software Foundation.
00007 
00008     This library is distributed in the hope that it will be useful,
00009     but WITHOUT ANY WARRANTY; without even the implied warranty of
00010     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011     Library General Public License for more details.
00012 
00013     You should have received a copy of the GNU Library General Public License
00014     along with this library; see the file COPYING.LIB.  If not, write to
00015     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016     Boston, MA 02110-1301, USA.
00017 
00018 */
00019 
00020 #include "dialog.h"
00021 #include "dialog_p.h"
00022 
00023 #include "dispatcher.h"
00024 //#include "componentsdialog_p.h"
00025 
00026 #include <klocale.h>
00027 #include <kservicegroup.h>
00028 #include <kdebug.h>
00029 #include <kservicetypetrader.h>
00030 #include <kconfig.h>
00031 #include <kstandarddirs.h>
00032 #include <kcomponentdata.h>
00033 #include <kiconloader.h>
00034 #include <QtCore/QFile>
00035 #include <QtGui/QCheckBox>
00036 #include <QtCore/QStack>
00037 
00038 uint qHash(const KCModuleInfo &info)
00039 {
00040     return qHash(info.fileName());
00041 }
00042 
00043 namespace KSettings
00044 {
00045 
00046 Dialog::Dialog(QWidget *parent)
00047     : KCMultiDialog(*new DialogPrivate, new KPageWidget, parent)
00048 {
00049 }
00050 
00051 Dialog::Dialog(const QStringList &components, QWidget *parent)
00052     : KCMultiDialog(*new DialogPrivate, new KPageWidget, parent)
00053 {
00054     Q_D(Dialog);
00055     d->components = components;
00056 }
00057 
00058 Dialog::~Dialog()
00059 {
00060 }
00061 
00062 void Dialog::setAllowComponentSelection(bool selection)
00063 {
00064     d_func()->staticlistview = !selection;
00065 }
00066 
00067 bool Dialog::allowComponentSelection() const
00068 {
00069     return !d_func()->staticlistview;
00070 }
00071 
00072 void Dialog::setKCMArguments(const QStringList& arguments)
00073 {
00074     Q_D(Dialog);
00075     d->arguments = arguments;
00076 }
00077 
00078 void Dialog::setComponentBlacklist(const QStringList& blacklist)
00079 {
00080     Q_D(Dialog);
00081     d->componentBlacklist = blacklist;
00082 }
00083 
00084 void Dialog::addPluginInfos(const KPluginInfo::List &plugininfos)
00085 {
00086     Q_D(Dialog);
00087     for (KPluginInfo::List::ConstIterator it = plugininfos.begin();
00088             it != plugininfos.end(); ++it ) {
00089         d->registeredComponents.append(it->pluginName());
00090         foreach (const KService::Ptr &service, it->kcmServices()) {
00091             d->kcmInfos << KCModuleInfo(service);
00092         }
00093     }
00094 
00095     // The plugin, when disabled, disables all the KCMs described by kcmServices().
00096     // - Normally they are grouped using a .setdlg file so that the group parent can get a
00097     // checkbox to enable/disable the plugin.
00098     // - If the plugin does not belong to a group and has only one KCM the checkbox can be
00099     // used with this KCM.
00100     // - If the plugin belongs to a group but there are other modules in the group that do not
00101     // belong to this plugin we give a kError and show no checkbox
00102     // - If the plugin belongs to multiple groups we give a kError and show no checkbox
00103     d->plugininfos = plugininfos;
00104 }
00105 
00106 KPluginInfo::List Dialog::pluginInfos() const
00107 {
00108     return d_func()->plugininfos;
00109 }
00110 
00111 void Dialog::showEvent(QShowEvent *)
00112 {
00113     Q_D(Dialog);
00114     if (d->firstshow) {
00115         setUpdatesEnabled(false);
00116         d->kcmInfos += d->instanceServices();
00117         if (!d->components.isEmpty()) {
00118             d->kcmInfos += d->parentComponentsServices(d->components);
00119         }
00120         d->createDialogFromServices();
00121         d->firstshow = false;
00122         setUpdatesEnabled(true);
00123     }
00124     Dispatcher::syncConfiguration();
00125 }
00126 
00127 DialogPrivate::DialogPrivate()
00128     : staticlistview(true), firstshow(true), pluginStateDirty(0)
00129 {
00130 }
00131 
00132 QSet<KCModuleInfo> DialogPrivate::instanceServices()
00133 {
00134     //kDebug(700) ;
00135     QString componentName = KGlobal::mainComponent().componentName();
00136     registeredComponents.append(componentName);
00137     //kDebug(700) << "calling KServiceGroup::childGroup( " << componentName << " )";
00138     KServiceGroup::Ptr service = KServiceGroup::childGroup( componentName );
00139 
00140     QSet<KCModuleInfo> ret;
00141 
00142     if( service && service->isValid() )
00143     {
00144         //kDebug(700) << "call was successful";
00145         KServiceGroup::List list = service->entries();
00146         for( KServiceGroup::List::ConstIterator it = list.begin();
00147                 it != list.end(); ++it )
00148         {
00149             KSycocaEntry::Ptr p = (*it);
00150             if( p->isType( KST_KService ) )
00151             {
00152                 //kDebug( 700 ) << "found service";
00153                 ret << KCModuleInfo(KService::Ptr::staticCast(p));
00154             }
00155             else
00156                 kWarning( 700 ) << "KServiceGroup::childGroup returned"
00157                     " something else than a KService" << endl;
00158         }
00159     }
00160 
00161     return ret;
00162 }
00163 
00164 QSet<KCModuleInfo> DialogPrivate::parentComponentsServices(const QStringList &kcdparents)
00165 {
00166     registeredComponents += kcdparents;
00167     QString constraint = kcdparents.join("' in [X-KDE-ParentComponents]) or ('");
00168     constraint = "('" + constraint + "' in [X-KDE-ParentComponents])";
00169 
00170     //kDebug(700) << "constraint = " << constraint;
00171     const QList<KService::Ptr> services = KServiceTypeTrader::self()->query("KCModule", constraint);
00172     QSet<KCModuleInfo> ret;
00173     foreach (const KService::Ptr &service, services) {
00174         ret << KCModuleInfo(service);
00175     }
00176     return ret;
00177 }
00178 
00179 bool DialogPrivate::isPluginForKCMEnabled(const KCModuleInfo *moduleinfo, KPluginInfo &pinfo) const
00180 {
00181     // if the user of this class requested to hide disabled modules
00182     // we check whether it should be enabled or not
00183     bool enabled = true;
00184     //kDebug(700) << "check whether the '" << moduleinfo->moduleName() << "' KCM should be shown";
00185     // for all parent components
00186     const QStringList parentComponents = moduleinfo->service()->property(
00187             "X-KDE-ParentComponents" ).toStringList();
00188     for( QStringList::ConstIterator pcit = parentComponents.begin();
00189             pcit != parentComponents.end(); ++pcit )
00190     {
00191         // if the parentComponent is not registered ignore it
00192         if (!registeredComponents.contains(*pcit)) {
00193             continue;
00194         }
00195 
00196         // we check if the parent component is a plugin
00197         // if not the KCModule must be enabled
00198         enabled = true;
00199         if (pinfo.pluginName() == *pcit) {
00200             // it is a plugin: we check whether the plugin is enabled
00201             pinfo.load();
00202             enabled = pinfo.isPluginEnabled();
00203             //kDebug(700) << "parent " << *pcit << " is " << (enabled ? "enabled" : "disabled");
00204         }
00205         // if it is enabled we're done for this KCModuleInfo
00206         if (enabled) {
00207             return true;
00208         }
00209     }
00210     return enabled;
00211 }
00212 
00213 void DialogPrivate::parseGroupFile( const QString & filename )
00214 {
00215     Q_Q(Dialog);
00216     KConfig file( filename, KConfig::SimpleConfig );
00217     const QStringList groups = file.groupList();
00218     foreach (const QString &group, groups) {
00219         if (group.isEmpty()) {
00220             continue;
00221         }
00222         KConfigGroup conf(&file, group);
00223 
00224         QWidget * page = new QWidget( q );
00225 
00226         const QString iconName = conf.readEntry("Icon");
00227         QCheckBox *checkBox = new QCheckBox(i18n("Enable component"), page);
00228         QLabel *iconLabel = new QLabel(page);
00229         QLabel *comment = new QLabel(conf.readEntry("Comment"), page);
00230         comment->setTextFormat(Qt::RichText);
00231         QVBoxLayout * layout = new QVBoxLayout(page);
00232         layout->addWidget(checkBox);
00233         layout->addWidget(iconLabel);
00234         layout->addWidget(comment);
00235         layout->addStretch();
00236         page->setLayout(layout);
00237 
00238         KPageWidgetItem *item = new KPageWidgetItem(page, conf.readEntry("Name"));
00239         item->setIcon(KIcon(iconName));
00240         iconLabel->setPixmap(item->icon().pixmap(128, 128));
00241         const int weight = conf.readEntry("Weight", 100);
00242         item->setProperty("_k_weight", weight);
00243         checkBoxForItem.insert(item, checkBox);
00244 
00245         const KPageWidgetModel *model = qobject_cast<const KPageWidgetModel *>(q->pageWidget()->model());
00246         Q_ASSERT(model);
00247         const QString parentId = conf.readEntry("Parent");
00248         KPageWidgetItem *parentItem = pageItemForGroupId.value(parentId);
00249         if (parentItem) {
00250             const QModelIndex parentIndex = model->index(parentItem);
00251             const int siblingCount = model->rowCount(parentIndex);
00252             int row = 0;
00253             for (; row < siblingCount; ++row) {
00254                 KPageWidgetItem *siblingItem = model->item(parentIndex.child(row, 0));
00255                 if (siblingItem->property("_k_weight").toInt() > weight) {
00256                     // the item we found is heavier than the new module
00257                     q->insertPage(siblingItem, item);
00258                     break;
00259                 }
00260             }
00261             if (row == siblingCount) {
00262                 // the new module is either the first or the heaviest item
00263                 q->addSubPage(parentItem, item);
00264             }
00265         } else {
00266             const int siblingCount = model->rowCount();
00267             int row = 0;
00268             for (; row < siblingCount; ++row) {
00269                 KPageWidgetItem *siblingItem = model->item(model->index(row, 0));
00270                 if (siblingItem->property("_k_weight").toInt() > weight) {
00271                     // the item we found is heavier than the new module
00272                     q->insertPage(siblingItem, item);
00273                     break;
00274                 }
00275             }
00276             if (row == siblingCount) {
00277                 // the new module is either the first or the heaviest item
00278                 q->addPage(item);
00279             }
00280         }
00281 
00282         pageItemForGroupId.insert(group, item);
00283     }
00284 }
00285 
00286 void DialogPrivate::createDialogFromServices()
00287 {
00288     Q_Q(Dialog);
00289     // read .setdlg files
00290     QString setdlgpath = KStandardDirs::locate( "appdata",
00291                                                     KGlobal::mainComponent().componentName() + ".setdlg" );
00292     const QStringList setdlgaddon = KGlobal::dirs()->findAllResources( "appdata",
00293             "ksettingsdialog/*.setdlg" );
00294     if (!setdlgpath.isNull()) {
00295         parseGroupFile(setdlgpath);
00296     }
00297     if (setdlgaddon.size() > 0) {
00298         for (QStringList::ConstIterator it = setdlgaddon.begin(); it != setdlgaddon.end(); ++it) {
00299             parseGroupFile(*it);
00300         }
00301     }
00302 
00303     //kDebug(700) << kcmInfos.count();
00304     foreach (const KCModuleInfo &info, kcmInfos) {
00305         const QStringList parentComponents = info.service()->property("X-KDE-ParentComponents").toStringList();
00306         bool blacklisted = false;
00307         foreach (const QString &parentComponent, parentComponents) {
00308             if (componentBlacklist.contains(parentComponent)) {
00309                 blacklisted = true;
00310                 break;
00311             }
00312         }
00313         if (blacklisted) {
00314             continue;
00315         }
00316         const QString parentId = info.service()->property("X-KDE-CfgDlgHierarchy", QVariant::String).toString();
00317         KPageWidgetItem *parent = pageItemForGroupId.value(parentId);
00318         KPageWidgetItem *item = q->addModule(info, parent, arguments);
00319         kDebug(700) << "added KCM '" << info.moduleName() << "'";
00320         foreach (KPluginInfo pinfo, plugininfos) {
00321             kDebug(700) << pinfo.pluginName();
00322             if (pinfo.kcmServices().contains(info.service())) {
00323                 const bool isEnabled = isPluginForKCMEnabled(&info, pinfo);
00324                 item->setEnabled(isEnabled);
00325                 kDebug(700) << "correct KPluginInfo for this KCM";
00326                 // this KCM belongs to a plugin
00327                 if (parent && pinfo.kcmServices().count() > 1) {
00328                     const KPluginInfo &plugin = pluginForItem.value(parent);
00329                     if (plugin.isValid()) {
00330                         if (plugin != pinfo) {
00331                             kError(700) << "A group contains more than one plugin: '"
00332                                 << plugin.pluginName() << "' and '" << pinfo.pluginName()
00333                                 << "'. Now it won't be possible to enable/disable the plugin."
00334                                 << endl;
00335                             parent->setCheckable(false);
00336                             q->disconnect(parent, SIGNAL(toggled(bool)), q, SLOT(_k_updateEnabledState(bool)));
00337                         }
00338                         // else everything is fine
00339                     } else {
00340                         QCheckBox *checkBox = checkBoxForItem.value(parent);
00341                         Q_ASSERT(checkBox);
00342                         pluginForItem.insert(parent, pinfo);
00343                         parent->setCheckable(true);
00344                         parent->setChecked(isEnabled);
00345                         checkBox->setChecked(isEnabled);
00346                         q->connect(parent, SIGNAL(toggled(bool)), q, SLOT(_k_updateEnabledState(bool)));
00347                         q->connect(parent, SIGNAL(toggled(bool)), checkBox, SLOT(setChecked(bool)));
00348                         q->connect(checkBox, SIGNAL(clicked(bool)), parent, SLOT(setChecked(bool)));
00349                     }
00350                 } else {
00351                     pluginForItem.insert(item, pinfo);
00352                     item->setCheckable(true);
00353                     item->setChecked(isEnabled);
00354                     q->connect(item, SIGNAL(toggled(bool)), q, SLOT(_k_updateEnabledState(bool)));
00355                 }
00356                 break;
00357             }
00358         }
00359     }
00360     // now that the KCMs are in, check for empty groups and remove them again
00361     {
00362         const KPageWidgetModel *model = qobject_cast<const KPageWidgetModel *>(q->pageWidget()->model());
00363         const QHash<QString, KPageWidgetItem *>::ConstIterator end = pageItemForGroupId.constEnd();
00364         QHash<QString, KPageWidgetItem *>::ConstIterator it = pageItemForGroupId.constBegin();
00365         for (; it != end; ++it) {
00366             const QModelIndex index = model->index(it.value());
00367             if (!index.child(0, 0).isValid()) {
00368                 // no children => remove this item
00369                 q->removePage(it.value());
00370             }
00371         }
00372     }
00373 
00374     // TODO: Don't show the reset button until the issue with the
00375     // KPluginSelector::load() method is solved.
00376     // Problem:
00377     // KCMultiDialog::show() call KCModule::load() to reset all KCMs
00378     // (KPluginSelector::load() resets all plugin selections and all plugin
00379     // KCMs).
00380     // The reset button calls KCModule::load(), too but in this case we want the
00381     // KPluginSelector to only reset the current visible plugin KCM and not
00382     // touch the plugin selections.
00383     // I have no idea how to check that in KPluginSelector::load()...
00384     //q->showButton(KDialog::User1, true);
00385 
00386     QObject::connect(q, SIGNAL(okClicked()), q, SLOT(_k_syncConfiguration()));
00387     QObject::connect(q, SIGNAL(applyClicked()), q, SLOT(_k_syncConfiguration()));
00388     QObject::connect(q, SIGNAL(configCommitted(const QByteArray &)), q,
00389             SLOT(_k_reparseConfiguration(const QByteArray &)));
00390 }
00391 
00392 void DialogPrivate::_k_syncConfiguration()
00393 {
00394     Q_Q(Dialog);
00395     const QHash<KPageWidgetItem *, KPluginInfo>::Iterator endIt = pluginForItem.end();
00396     QHash<KPageWidgetItem *, KPluginInfo>::Iterator it = pluginForItem.begin();
00397     for (; it != endIt; ++it) {
00398         KPageWidgetItem *item = it.key();
00399         KPluginInfo pinfo = it.value();
00400         pinfo.setPluginEnabled(item->isChecked());
00401         pinfo.save();
00402     }
00403     if (pluginStateDirty > 0) {
00404       emit q->pluginSelectionChanged();
00405       pluginStateDirty = 0;
00406     }
00407     Dispatcher::syncConfiguration();
00408 }
00409 
00410 void DialogPrivate::_k_reparseConfiguration(const QByteArray &a)
00411 {
00412     Dispatcher::reparseConfiguration(a);
00413 }
00414 
00415 /*
00416 void DialogPrivate::_k_configureTree()
00417 {
00418     kDebug( 700 ) ;
00419     QObject::connect(subdlg, SIGNAL(okClicked()), q, SLOT(_k_updateTreeList()));
00420     QObject::connect(subdlg, SIGNAL(applyClicked()), q, SLOT(_k_updateTreeList()));
00421     QObject::connect(subdlg, SIGNAL(okClicked()), q, SIGNAL(pluginSelectionChanged()));
00422     QObject::connect(subdlg, SIGNAL(applyClicked()), q, SIGNAL(pluginSelectionChanged()));
00423 }
00424 */
00425 
00426 void DialogPrivate::_k_clientChanged()
00427 {
00428     if (pluginStateDirty > 0) {
00429         Q_Q(Dialog);
00430         q->enableButton(KDialog::Apply, true);
00431     } else {
00432         KCMultiDialogPrivate::_k_clientChanged();
00433     }
00434 }
00435 
00436 void DialogPrivate::_k_updateEnabledState(bool enabled)
00437 {
00438     Q_Q(Dialog);
00439     KPageWidgetItem *item = qobject_cast<KPageWidgetItem *>(q->sender());
00440     if (!item) {
00441         kWarning(700) << "invalid sender";
00442         return;
00443     }
00444 
00445     // iterate over all child KPageWidgetItem objects and check whether they need to be enabled/disabled
00446     const KPageWidgetModel *model = qobject_cast<const KPageWidgetModel *>(q->pageWidget()->model());
00447     Q_ASSERT(model);
00448     QModelIndex index = model->index(item);
00449     if (!index.isValid()) {
00450         kWarning(700) << "could not find item in model";
00451         return;
00452     }
00453 
00454     const KPluginInfo &pinfo = pluginForItem.value(item);
00455     if (!pinfo.isValid()) {
00456         kWarning(700) << "could not find KPluginInfo in item";
00457         return;
00458     }
00459     if (pinfo.isPluginEnabled() != enabled) {
00460         ++pluginStateDirty;
00461     } else {
00462         --pluginStateDirty;
00463     }
00464     if (pluginStateDirty < 2) {
00465         _k_clientChanged();
00466     }
00467 
00468     //kDebug(700) ;
00469 
00470     QModelIndex firstborn = index.child(0, 0);
00471     if (firstborn.isValid()) {
00472         //kDebug(700) << "iterating over children";
00473         // change all children
00474         index = firstborn;
00475         QStack<QModelIndex> stack;
00476         while (index.isValid()) {
00477             //kDebug(700) << index;
00478             KPageWidgetItem *item = model->item(index);
00479             //kDebug(700) << "item->setEnabled(" << enabled << ')';
00480             item->setEnabled(enabled);
00481             firstborn = index.child(0, 0);
00482             if (firstborn.isValid()) {
00483                 stack.push(index);
00484                 index = firstborn;
00485             } else {
00486                 index = index.sibling(index.row() + 1, 0);
00487                 while (!index.isValid() && !stack.isEmpty()) {
00488                     index = stack.pop();
00489                     index = index.sibling(index.row() + 1, 0);
00490                 }
00491             }
00492         }
00493     } else {
00494         // change only this item
00495         kDebug(700) << "item->setEnabled(" << enabled << ')';
00496         item->setEnabled(enabled);
00497     }
00498 }
00499 
00500 } //namespace
00501 
00502 #include "dialog.moc"
00503 
00504 // vim: ts=4

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
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.6
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