00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "dialog.h"
00021 #include "dialog_p.h"
00022
00023 #include "dispatcher.h"
00024
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
00096
00097
00098
00099
00100
00101
00102
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
00135 QString componentName = KGlobal::mainComponent().componentName();
00136 registeredComponents.append(componentName);
00137
00138 KServiceGroup::Ptr service = KServiceGroup::childGroup( componentName );
00139
00140 QSet<KCModuleInfo> ret;
00141
00142 if( service && service->isValid() )
00143 {
00144
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
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
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
00182
00183 bool enabled = true;
00184
00185
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
00192 if (!registeredComponents.contains(*pcit)) {
00193 continue;
00194 }
00195
00196
00197
00198 enabled = true;
00199 if (pinfo.pluginName() == *pcit) {
00200
00201 pinfo.load();
00202 enabled = pinfo.isPluginEnabled();
00203
00204 }
00205
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
00257 q->insertPage(siblingItem, item);
00258 break;
00259 }
00260 }
00261 if (row == siblingCount) {
00262
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
00272 q->insertPage(siblingItem, item);
00273 break;
00274 }
00275 }
00276 if (row == siblingCount) {
00277
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
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
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
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
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
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
00369 q->removePage(it.value());
00370 }
00371 }
00372 }
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
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
00417
00418
00419
00420
00421
00422
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
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
00469
00470 QModelIndex firstborn = index.child(0, 0);
00471 if (firstborn.isValid()) {
00472
00473
00474 index = firstborn;
00475 QStack<QModelIndex> stack;
00476 while (index.isValid()) {
00477
00478 KPageWidgetItem *item = model->item(index);
00479
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
00495 kDebug(700) << "item->setEnabled(" << enabled << ')';
00496 item->setEnabled(enabled);
00497 }
00498 }
00499
00500 }
00501
00502 #include "dialog.moc"
00503
00504