File indexing completed on 2024-07-14 04:51:41

0001 /*
0002     SPDX-FileCopyrightText: 2007-2010 Urs Wolfer <uwolfer@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "preferencesdialog.h"
0008 #include "hostpreferenceslist.h"
0009 #include "krdc_debug.h"
0010 #include "ui_general.h"
0011 
0012 #include <KConfigSkeleton>
0013 #include <KLocalizedString>
0014 #include <KPluginMetaData>
0015 #include <KPluginWidget>
0016 
0017 PreferencesDialog::PreferencesDialog(QWidget *parent, KConfigSkeleton *skeleton)
0018     : KConfigDialog(parent, QStringLiteral("preferences"), skeleton)
0019     , m_settingsChanged(false)
0020 {
0021     QWidget *generalPage = new QWidget(this);
0022     Ui::General generalUi;
0023     generalUi.setupUi(generalPage);
0024     addPage(generalPage, i18nc("General Config", "General"), QStringLiteral("krdc"), i18n("General Configuration"));
0025 
0026     HostPreferencesList *hostPreferencesList =
0027         new HostPreferencesList(this, qobject_cast<MainWindow *>(parent), skeleton->config()->group(QStringLiteral("hostpreferences")));
0028     addPage(hostPreferencesList, i18n("Hosts"), QStringLiteral("computer"), i18n("Host Configuration"));
0029 
0030     m_pluginSelector = new KPluginWidget();
0031     const QVector<KPluginMetaData> offers = KPluginMetaData::findPlugins(QStringLiteral("krdc"));
0032     m_pluginSelector->setConfig(KSharedConfig::openConfig()->group(QStringLiteral("Plugins")));
0033     m_pluginSelector->addPlugins(offers, i18n("Plugins"));
0034     addPage(m_pluginSelector, i18n("Plugins"), QStringLiteral("preferences-plugin"), i18n("Plugin Configuration"));
0035 
0036     connect(this, SIGNAL(accepted()), SLOT(saveState()));
0037     QPushButton *defaultsButton = buttonBox()->button(QDialogButtonBox::RestoreDefaults);
0038     connect(defaultsButton, SIGNAL(clicked()), SLOT(loadDefaults()));
0039     connect(m_pluginSelector, SIGNAL(changed(bool)), SLOT(settingsChanged()));
0040 }
0041 
0042 void PreferencesDialog::saveState()
0043 {
0044     // TODO: relaod plugins at runtime?
0045     m_pluginSelector->save();
0046 }
0047 
0048 void PreferencesDialog::loadDefaults()
0049 {
0050     m_pluginSelector->defaults();
0051 }
0052 
0053 void PreferencesDialog::settingsChanged()
0054 {
0055     enableButton(QDialogButtonBox::Apply);
0056     enableButton(QDialogButtonBox::RestoreDefaults);
0057 }
0058 
0059 bool PreferencesDialog::isDefault()
0060 {
0061     return KConfigDialog::isDefault() && m_pluginSelector->isDefault();
0062 }
0063 
0064 void PreferencesDialog::enableButton(QDialogButtonBox::StandardButton standardButton)
0065 {
0066     QPushButton *button = buttonBox()->button(standardButton);
0067     if (button) {
0068         button->setEnabled(true);
0069     }
0070 }