1/*
2 SPDX-FileCopyrightText: 2001 Michael Goffioul <kdeprint@swing.be>
3 SPDX-FileCopyrightText: 2004 Frans Englich <frans.englich@telia.com>
4 SPDX-FileCopyrightText: 2009 Dario Freddi <drf@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "kcmodule.h"
10#include "kabstractconfigmodule.h"
11#include "kcmutils_debug.h"
12
13#include <QVBoxLayout>
14
15#include <KConfigDialogManager>
16#include <KConfigSkeleton>
17#include <KLocalizedString>
18#include <KPluginMetaData>
19
20class KCModuleProxyInternal : public QWidget
21{
22 Q_OBJECT
23public:
24 KCModuleProxyInternal(QWidget *parent)
25 : QWidget(parent)
26 {
27 }
28};
29
30class KCModulePrivate
31{
32public:
33 KCModulePrivate(QWidget *parentWidget)
34 : _needsAuthorization(false)
35 , _unmanagedWidgetChangeState(false)
36 , _unmanagedWidgetDefaultState(false)
37 , _unmanagedWidgetDefaultStateCalled(false)
38 , parentWidget(parentWidget)
39 {
40 }
41
42 void authStatusChanged(int status);
43
44 QList<KConfigDialogManager *> managers;
45
46 bool _needsAuthorization : 1;
47
48 // this member is used to record the state on non-automatically
49 // managed widgets, allowing for mixed KConfigXT-drive and manual
50 // widgets to coexist peacefully and do the correct thing with
51 // the changed(bool) signal
52 bool _unmanagedWidgetChangeState : 1;
53 bool _unmanagedWidgetDefaultState : 1;
54 bool _unmanagedWidgetDefaultStateCalled : 1;
55 QVBoxLayout *m_topLayout = nullptr; /* Contains QScrollView view, and root stuff */
56 QWidget *parentWidget;
57 KCModuleProxyInternal *m_proxyInternal = nullptr;
58};
59
60KCModule::KCModule(QWidget *parent, const KPluginMetaData &data)
61 : KAbstractConfigModule(parent, data)
62 , d(new KCModulePrivate(parent))
63{
64}
65
66KConfigDialogManager *KCModule::addConfig(KCoreConfigSkeleton *config, QWidget *widget)
67{
68 KConfigDialogManager *manager = new KConfigDialogManager(widget, config);
69 manager->setObjectName(objectName());
70 connect(sender: manager, signal: &KConfigDialogManager::widgetModified, context: this, slot: &KCModule::widgetChanged);
71 connect(sender: manager, signal: &QObject::destroyed, context: this, slot: [this, manager]() {
72 d->managers.removeOne(t: manager);
73 });
74 d->managers.append(t: manager);
75 return manager;
76}
77
78KCModule::~KCModule()
79{
80 qDeleteAll(c: d->managers);
81 d->managers.clear();
82}
83
84void KCModule::load()
85{
86 KAbstractConfigModule::load();
87 for (KConfigDialogManager *manager : std::as_const(t&: d->managers)) {
88 manager->updateWidgets();
89 }
90 widgetChanged();
91}
92
93void KCModule::save()
94{
95 KAbstractConfigModule::save();
96 for (KConfigDialogManager *manager : std::as_const(t&: d->managers)) {
97 manager->updateSettings();
98 }
99}
100
101void KCModule::defaults()
102{
103 KAbstractConfigModule::defaults();
104 for (KConfigDialogManager *manager : std::as_const(t&: d->managers)) {
105 manager->updateWidgetsDefault();
106 }
107}
108
109QWidget *KCModule::widget()
110{
111 if (!d->m_proxyInternal) {
112 d->m_topLayout = new QVBoxLayout(d->parentWidget);
113 d->m_proxyInternal = new KCModuleProxyInternal(d->parentWidget);
114 d->m_topLayout->addWidget(d->m_proxyInternal);
115 }
116 return d->m_proxyInternal;
117}
118
119void KCModule::widgetChanged()
120{
121 setNeedsSave(d->_unmanagedWidgetChangeState || managedWidgetChangeState());
122 if (d->_unmanagedWidgetDefaultStateCalled) {
123 setRepresentsDefaults(d->_unmanagedWidgetDefaultState && managedWidgetDefaultState());
124 } else {
125 setRepresentsDefaults(!d->managers.isEmpty() && managedWidgetDefaultState());
126 }
127}
128
129bool KCModule::managedWidgetChangeState() const
130{
131 for (KConfigDialogManager *manager : std::as_const(t&: d->managers)) {
132 if (manager->hasChanged()) {
133 return true;
134 }
135 }
136
137 return false;
138}
139
140bool KCModule::managedWidgetDefaultState() const
141{
142 for (KConfigDialogManager *manager : std::as_const(t&: d->managers)) {
143 if (!manager->isDefault()) {
144 return false;
145 }
146 }
147
148 return true;
149}
150
151void KCModule::unmanagedWidgetChangeState(bool changed)
152{
153 d->_unmanagedWidgetChangeState = changed;
154 widgetChanged();
155}
156
157void KCModule::unmanagedWidgetDefaultState(bool defaulted)
158{
159 d->_unmanagedWidgetDefaultStateCalled = true;
160 d->_unmanagedWidgetDefaultState = defaulted;
161 widgetChanged();
162}
163
164QList<KConfigDialogManager *> KCModule::configs() const
165{
166 return d->managers;
167}
168
169#include "kcmodule.moc"
170#include "moc_kcmodule.cpp"
171

source code of kcmutils/src/kcmodule.cpp