File: SettingsBase.cpp

package info (click to toggle)
systemsettings 4%3A6.5.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,968 kB
  • sloc: cpp: 2,848; xml: 174; python: 47; makefile: 3; sh: 3
file content (310 lines) | stat: -rw-r--r-- 11,883 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 *   SPDX-FileCopyrightText: 2009 Ben Cooksley <bcooksley@kde.org>
 *   SPDX-FileCopyrightText: 2021 Alexander Lohnau <alexander.lohnau@gmx.de>
 *   SPDX-FileCopyrightText: 2022 ivan tkachenko <me@ratijas.tk>
 *
 *   SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "SettingsBase.h"
#include "ModuleView.h"
#include "SidebarMode.h"
#include "kcmmetadatahelpers.h"

#include <QFileInfo>
#include <QFontDatabase>
#include <QGuiApplication>
#include <QLoggingCategory>
#include <QMenu>
#include <QMenuBar>
#include <QScreen>
#include <QTimer>
#include <QtGlobal>

#include <KAboutData>
#include <KActionCollection>
#include <KConfigGroup>
#include <KDesktopFile>
#include <KFileUtils>
#include <KHelpMenu>
#include <KIO/JobUiDelegateFactory>
#include <KIO/OpenUrlJob>
#include <KLocalizedString>
#include <KStandardActions>
#include <KXMLGUIFactory>

SettingsBase::SettingsBase(SidebarMode::ApplicationMode mode, const QString &startupModule, const QStringList &startupModuleArgs, QWidget *parent)
    : KMainWindow(parent)
    , m_mode(mode)
    , m_startupModule(startupModule)
    , m_startupModuleArgs(startupModuleArgs)
    , m_actionCollection(new KActionCollection(this))
    , m_helpMenu(new KHelpMenu(this))
{
    // Prepare the view area
    stackedWidget = new QStackedWidget(this);
    setCentralWidget(stackedWidget);

    m_actionCollection->addAssociatedWidget(this);

    setProperty("_breeze_no_separator", true);

    if (m_mode == SidebarMode::InfoCenter) {
        setWindowTitle(i18nd("systemsettings", "Info Center"));
        setWindowIcon(QIcon::fromTheme(QStringLiteral("hwinfo")));
    } else {
        setWindowTitle(i18nd("systemsettings", "System Settings"));
        setWindowIcon(QIcon::fromTheme(QStringLiteral("preferences-system")));
    }

    // Initialise the window so we don't flicker
    initToolBar();
    // We can now launch the delayed loading safely
    initApplication();
    // Restore window size and position
    setAutoSaveSettings();
}

SettingsBase::~SettingsBase()
{
    setSettingsDirty();
    delete rootModule;
}

QSize SettingsBase::sizeHint() const
{
    // Take the font size into account for the window size, as we do for UI elements
    const float fontSize = QFontDatabase::systemFont(QFontDatabase::GeneralFont).pointSizeF();
    const QSize targetSize = QSize(qRound(93 * fontSize), qRound(65 * fontSize));

    // on smaller or portrait-rotated screens, do not max out height and/or width
    const QSize screenSize = (QGuiApplication::primaryScreen()->availableSize() * 0.9);
    return targetSize.boundedTo(screenSize);
}

void SettingsBase::initApplication()
{
    // Prepare the menu of all modules
    auto source = m_mode == SidebarMode::InfoCenter ? MetaDataSource::KInfoCenter : MetaDataSource::SystemSettings;
    pluginModules = findKCMsMetaData(source) << findExternalKCMModules(source);

    const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::AppDataLocation, QStringLiteral("categories"), QStandardPaths::LocateDirectory);
    categories = KFileUtils::findAllUniqueFiles(dirs, QStringList(QStringLiteral("*.desktop")));

    rootModule = new MenuItem(true, nullptr);
    initMenuList(rootModule);

    // Handle lost+found modules...
    if (lostFound) {
        for (const auto &metaData : std::as_const(pluginModules)) {
            auto infoItem = new MenuItem(false, lostFound);
            infoItem->setMetaData(metaData);
            qCDebug(SYSTEMSETTINGS_APP_LOG) << "Added " << metaData.pluginId();
        }
    }

    loadCurrentView();

    // enforce minimum window size
    setMinimumSize(SettingsBase::sizeHint());
    activateWindow();

    // Change size limit after screen resolution is changed
    m_screen = qGuiApp->primaryScreen();
    connect(qGuiApp, &QGuiApplication::primaryScreenChanged, this, [this](QScreen *screen) {
        if (m_screen) {
            disconnect(m_screen, &QScreen::geometryChanged, this, &SettingsBase::slotGeometryChanged);
        }
        m_screen = screen;
        slotGeometryChanged();
        connect(m_screen, &QScreen::geometryChanged, this, &SettingsBase::slotGeometryChanged);
    });
    connect(m_screen, &QScreen::geometryChanged, this, &SettingsBase::slotGeometryChanged);
}

void SettingsBase::initToolBar()
{
    // Fill the toolbar with default actions
    // Exit is the very last action
    quitAction = m_actionCollection->addAction(KStandardActions::Quit, QStringLiteral("quit_action"), this, &QWidget::close);

    if (m_mode == SidebarMode::SystemSettings) {
        highlightChangesAction = m_actionCollection->addAction(QStringLiteral("highlight_changes"), this, [this] {
            view->toggleDefaultsIndicatorsVisibility();
        });
        highlightChangesAction->setCheckable(true);
        highlightChangesAction->setText(i18nd("systemsettings", "Highlight Changed Settings"));
        highlightChangesAction->setIcon(QIcon::fromTheme(QStringLiteral("draw-highlight")));

        showIrrelevantAction = m_actionCollection->addAction(QStringLiteral("show_irrelevant_modules"), this, [this] {
            view->toggleShowIrrelevantModules();
        });
        showIrrelevantAction->setCheckable(true);
        showIrrelevantAction->setText(
            i18ndc("systemsettings", "@option:check the pages being referred to here are System Settings KCMs", "Show Inapplicable Pages"));
        showIrrelevantAction->setIcon(QIcon::fromTheme(QStringLiteral("view-hidden")));
    }

    reportPageSpecificBugAction = m_actionCollection->addAction(QStringLiteral("report_bug_in_current_module"), this, [this] {
        const QString bugReportUrlString =
            view->moduleView()->activeModuleMetadata().bugReportUrl() + QStringLiteral("&version=") + QGuiApplication::applicationVersion();
        auto job = new KIO::OpenUrlJob(QUrl(bugReportUrlString));
        job->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, nullptr));
        job->start();
    });
    reportPageSpecificBugAction->setText(i18nd("systemsettings", "Report a Bug in the Current Page…"));
    reportPageSpecificBugAction->setIcon(QIcon::fromTheme(QStringLiteral("tools-report-bug")));

    m_actionCollection->addAction(QStringLiteral("help_report_bug"), m_helpMenu->action(KHelpMenu::menuReportBug));
    m_actionCollection->addAction(QStringLiteral("help_contents"), m_helpMenu->action(KHelpMenu::menuHelpContents));
    m_actionCollection->addAction(QStringLiteral("help_about_app"), m_helpMenu->action(KHelpMenu::menuAboutApp));
    m_actionCollection->addAction(QStringLiteral("help_about_kde"), m_helpMenu->action(KHelpMenu::menuAboutKDE));
}

void SettingsBase::initMenuList(MenuItem *parent)
{
    // look for any categories inside this level, and recurse into them
    for (const QString &category : std::as_const(categories)) {
        const KDesktopFile file(category);
        const KConfigGroup entry = file.desktopGroup();
        QString parentCategory;
        QString parentCategory2;
        if (m_mode == SidebarMode::InfoCenter) {
            parentCategory = entry.readEntry("X-KDE-KInfoCenter-Parent-Category");
        } else {
            parentCategory = entry.readEntry("X-KDE-System-Settings-Parent-Category");
            parentCategory2 = entry.readEntry("X-KDE-System-Settings-Parent-Category-V2");
        }

        if (parentCategory == parent->category() ||
            // V2 entries must not be empty if they want to become a proper category.
            (!parentCategory2.isEmpty() && parentCategory2 == parent->category())) {
            auto menuItem = new MenuItem(true, parent);
            menuItem->setCategoryConfig(file);
            if (entry.readEntry("X-KDE-System-Settings-Category") == QLatin1String("lost-and-found")) {
                lostFound = menuItem;
                continue;
            }
            initMenuList(menuItem);
        }
    }

    // scan for any modules at this level and add them
    for (const auto &metaData : std::as_const(pluginModules)) {
        QString category;
        QString categoryv2;
        if (m_mode == SidebarMode::InfoCenter) {
            category = metaData.value(QStringLiteral("X-KDE-KInfoCenter-Category"));
        } else {
            category = metaData.value(QStringLiteral("X-KDE-System-Settings-Parent-Category"));
            categoryv2 = metaData.value(QStringLiteral("X-KDE-System-Settings-Parent-Category-V2"));
        }
        const QString parentCategoryKcm = parent->systemsettingsCategoryModule();
        bool isCategoryOwner = false;

        if (!parentCategoryKcm.isEmpty() && parentCategoryKcm == metaData.pluginId()) {
            parent->setMetaData(metaData);
            isCategoryOwner = true;
        }

        if (!parent->category().isEmpty() && (category == parent->category() || categoryv2 == parent->category())) {
            if (!metaData.isHidden()) {
                // Add the module info to the menu
                auto infoItem = new MenuItem(false, parent);
                infoItem->setMetaData(metaData);
                infoItem->setCategoryOwner(isCategoryOwner);

                if (m_mode == SidebarMode::InfoCenter && metaData.pluginId() == QStringLiteral("kcm_about-distro")) {
                    homeModule = infoItem;
                } else if (m_mode == SidebarMode::SystemSettings && metaData.pluginId() == QStringLiteral("kcm_landingpage")) {
                    homeModule = infoItem;
                }
            }
        }
    }

    parent->sortChildrenByWeight();
}

bool SettingsBase::queryClose()
{
    bool changes = true;
    changes = view->moduleView()->resolveChanges();
    return changes;
}

void SettingsBase::setStartupModule(const QString &startupModule)
{
    m_startupModule = startupModule;
    view->setStartupModule(startupModule);
}

void SettingsBase::setStartupModuleArgs(const QStringList &startupModuleArgs)
{
    m_startupModuleArgs = startupModuleArgs;

    view->setStartupModuleArgs(startupModuleArgs);
}

void SettingsBase::reloadStartupModule()
{
    view->reloadStartupModule();
}

void SettingsBase::about()
{
    delete aboutDialog;
    aboutDialog = nullptr;

    aboutDialog = new KAboutApplicationDialog(KAboutData::applicationData(), nullptr);
    aboutDialog->show();
}

void SettingsBase::loadCurrentView()
{
    view = new SidebarMode(this, m_mode, m_startupModule, m_startupModuleArgs, m_actionCollection, homeModule, rootModule);
    connect(view, &SidebarMode::viewChanged, this, &SettingsBase::viewChange);

    if (stackedWidget->indexOf(view->mainWidget()) == -1) {
        stackedWidget->addWidget(view->mainWidget());
    }

    if (highlightChangesAction) {
        highlightChangesAction->setChecked(view->defaultsIndicatorsVisible());
    }

    viewChange(false);

    stackedWidget->setCurrentWidget(view->mainWidget());

    view->giveFocus();

    // Update visibility of the "report a bug on this page" and "report bug in general"
    // actions based on whether the current page has a bug report URL set
    auto reportGeneralBugAction = m_actionCollection->action(QStringLiteral("help_report_bug"));
    if (reportGeneralBugAction) {
        reportGeneralBugAction->setVisible(false);
    }
    auto moduleView = view->moduleView();
    connect(moduleView, &ModuleView::moduleChanged, this, [this, moduleView, reportGeneralBugAction] {
        reportPageSpecificBugAction->setVisible(!moduleView->activeModuleMetadata().bugReportUrl().isEmpty());
        if (reportGeneralBugAction) {
            reportGeneralBugAction->setVisible(!reportPageSpecificBugAction->isVisible());
        }
    });

    show();
}

void SettingsBase::viewChange(bool state)
{
    setCaption(view->moduleView()->activeModuleName(), state);
}

void SettingsBase::slotGeometryChanged()
{
    setMinimumSize(SettingsBase::sizeHint());
    setSettingsDirty();
}

#include "moc_SettingsBase.cpp"