khintssettings.cpp 21.5 KB
Newer Older
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
/*  This file is part of the KDE libraries
 *  Copyright 2013 Kevin Ottens <ervin+bluesystems@kde.org>
 *  Copyright 2013 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
 *  Copyright 2013 Alejandro Fiestas Olivares <afiestas@kde.org>
 *
 *  This library is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2 of the License or ( at
 *  your option ) version 3 or, at the discretion of KDE e.V. ( which shall
 *  act as a proxy as in section 14 of the GPLv3 ), any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 */

#include "khintssettings.h"

#include <QDebug>
#include <QDir>
#include <QString>
#include <QFileInfo>
#include <QToolBar>
#include <QPalette>
#include <QToolButton>
#include <QMainWindow>
#include <QApplication>
#include <QGuiApplication>
#include <QDialogButtonBox>
Martin Flöser's avatar
Martin Flöser committed
36
#include <QScreen>
37
#include <QStandardPaths>
Jan Grulich's avatar
Jan Grulich committed
38
#include <QTemporaryFile>
39

Jan Grulich's avatar
Jan Grulich committed
40
#include <QDBusArgument>
41
42
43
44
45
46
47
48
#include <QDBusConnection>
#include <QDBusInterface>

#include <kiconloader.h>
#include <kconfiggroup.h>
#include <ksharedconfig.h>
#include <kcolorscheme.h>

Martin Flöser's avatar
Martin Flöser committed
49
#include <config-platformtheme.h>
50
51
#ifdef UNIT_TEST
#undef HAVE_X11
Martin Flöser's avatar
Martin Flöser committed
52
53
54
55
56
57
58
#define HAVE_X11 0
#endif
#if HAVE_X11
#include <QX11Info>
#include <X11/Xcursor/Xcursor.h>
#endif

Laurent Montel's avatar
Laurent Montel committed
59
static const QString defaultLookAndFeelPackage = QStringLiteral("org.kde.breeze.desktop");
60

Jan Grulich's avatar
Jan Grulich committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const QDBusArgument &operator>>(const QDBusArgument &argument, QMap<QString, QVariantMap> &map)
{
    argument.beginMap();
    map.clear();

    while (!argument.atEnd()) {
        QString key;
        QVariantMap value;
        argument.beginMapEntry();
        argument >> key >> value;
        argument.endMapEntry();
        map.insert(key, value);
    }

    argument.endMap();
    return argument;
}

static inline bool checkUsePortalSupport()
{
    return !QStandardPaths::locate(QStandardPaths::RuntimeLocation, QStringLiteral("flatpak-info")).isEmpty() || qEnvironmentVariableIsSet("SNAP");
}

84
KHintsSettings::KHintsSettings(KSharedConfig::Ptr kdeglobals)
Friedrich W. H. Kossebau's avatar
Friedrich W. H. Kossebau committed
85
    : QObject(nullptr)
86
    , mKdeGlobals(kdeglobals)
Jan Grulich's avatar
Jan Grulich committed
87
    , mUsePortal(checkUsePortalSupport())
88
{
89
    if (!mKdeGlobals) {
90
        mKdeGlobals = KSharedConfig::openConfig();
91
    }
92
    KConfigGroup cg(mKdeGlobals, "KDE");
93

Jan Grulich's avatar
Jan Grulich committed
94
95
96
97
    if (mUsePortal) {
        updatePortalSetting();
    }

98
    // try to extract the proper defaults file from a lookandfeel package
Jan Grulich's avatar
Jan Grulich committed
99
    const QString looknfeel = readConfigValue(cg, QStringLiteral("LookAndFeelPackage"), defaultLookAndFeelPackage).toString();
Marco Martin's avatar
Marco Martin committed
100
    mDefaultLnfConfig = KSharedConfig::openConfig(QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("plasma/look-and-feel/") + defaultLookAndFeelPackage + QStringLiteral("/contents/defaults")));
101
    if (looknfeel != defaultLookAndFeelPackage) {
Marco Martin's avatar
Marco Martin committed
102
        mLnfConfig = KSharedConfig::openConfig(QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("plasma/look-and-feel/") + looknfeel + QStringLiteral("/contents/defaults")));
103
104
    }

Jan Grulich's avatar
Jan Grulich committed
105
    const auto cursorBlinkRate = readConfigValue(cg, QStringLiteral("CursorBlinkRate"), 1000).toInt();
106
    m_hints[QPlatformTheme::CursorFlashTime] = cursorBlinkRate > 0 ? qBound(200, cursorBlinkRate, 2000) : 0; // 0 => no blinking
Jan Grulich's avatar
Jan Grulich committed
107
108
109
    m_hints[QPlatformTheme::MouseDoubleClickInterval] = readConfigValue(cg, QStringLiteral("DoubleClickInterval"), 400);
    m_hints[QPlatformTheme::StartDragDistance] = readConfigValue(cg, QStringLiteral("StartDragDist"), 10);
    m_hints[QPlatformTheme::StartDragTime] = readConfigValue(cg, QStringLiteral("StartDragTime"), 500);
110

111
    KConfigGroup cgToolbar(mKdeGlobals, "Toolbar style");
112
113
    m_hints[QPlatformTheme::ToolButtonStyle] = toolButtonStyle(cgToolbar);

114
    KConfigGroup cgToolbarIcon(mKdeGlobals, "MainToolbarIcons");
Jan Grulich's avatar
Jan Grulich committed
115
    m_hints[QPlatformTheme::ToolBarIconSize] = readConfigValue(cgToolbarIcon, QStringLiteral("Size"), 22);
116

Jan Grulich's avatar
Jan Grulich committed
117
    m_hints[QPlatformTheme::ItemViewActivateItemOnSingleClick] = readConfigValue(cg, QStringLiteral("SingleClick"), true);
118

119
    m_hints[QPlatformTheme::SystemIconThemeName] = readConfigValue(QStringLiteral("Icons"), QStringLiteral("Theme"), QStringLiteral("breeze"));
120

121
    m_hints[QPlatformTheme::SystemIconFallbackThemeName] = QStringLiteral("hicolor");
122
    m_hints[QPlatformTheme::IconThemeSearchPaths] = xdgIconThemePaths();
123

124
125
126
127
128
129
    QStringList styleNames{
        QStringLiteral(BREEZE_STYLE_NAME),
        QStringLiteral("oxygen"),
        QStringLiteral("fusion"),
        QStringLiteral("windows")
    };
Jan Grulich's avatar
Jan Grulich committed
130
    const QString configuredStyle = readConfigValue(cg, QStringLiteral("widgetStyle"), QString()).toString();
131
132
133
134
    if (!configuredStyle.isEmpty()) {
        styleNames.removeOne(configuredStyle);
        styleNames.prepend(configuredStyle);
    }
Laurent Montel's avatar
Laurent Montel committed
135
    const QString lnfStyle = readConfigValue(QStringLiteral("KDE"), QStringLiteral("widgetStyle"), QString()).toString();
Marco Martin's avatar
Marco Martin committed
136
137
    if (!lnfStyle.isEmpty()) {
        styleNames.removeOne(lnfStyle);
138
139
140
141
        styleNames.prepend(lnfStyle);
    }
    m_hints[QPlatformTheme::StyleNames] = styleNames;

142
    m_hints[QPlatformTheme::DialogButtonBoxLayout] = QDialogButtonBox::KdeLayout;
Jan Grulich's avatar
Jan Grulich committed
143
    m_hints[QPlatformTheme::DialogButtonBoxButtonsHaveIcons] = readConfigValue(cg, QStringLiteral("ShowIconsOnPushButtons"), true);
144
145
    m_hints[QPlatformTheme::UseFullScreenForPopupMenu] = true;
    m_hints[QPlatformTheme::KeyboardScheme] = QPlatformTheme::KdeKeyboardScheme;
Jan Grulich's avatar
Jan Grulich committed
146
    m_hints[QPlatformTheme::UiEffects] = readConfigValue(cg, QStringLiteral("GraphicEffectsLevel"), 0) != 0 ? QPlatformTheme::GeneralUiEffect : 0;
147
148
    m_hints[QPlatformTheme::IconPixmapSizes] = QVariant::fromValue(QList<int>() << 512 << 256 << 128 << 64 << 32 << 22 << 16 << 8);

Jan Grulich's avatar
Jan Grulich committed
149
    m_hints[QPlatformTheme::WheelScrollLines] = readConfigValue(cg, QStringLiteral("WheelScrollLines"), 3);
150
    if (qobject_cast<QApplication *>(QCoreApplication::instance())) {
Jan Grulich's avatar
Jan Grulich committed
151
        QApplication::setWheelScrollLines(readConfigValue(cg, QStringLiteral("WheelScrollLines"), 3).toInt());
152
153
    }

154
    updateShowIconsInMenuItems(cg);
155

Kai Uwe Broulik's avatar
Kai Uwe Broulik committed
156
157
    m_hints[QPlatformTheme::ShowShortcutsInContextMenus] = true;

158
159
160
161
162
163
164
165
166
167
168
    QMetaObject::invokeMethod(this, "delayedDBusConnects", Qt::QueuedConnection);
    QMetaObject::invokeMethod(this, "setupIconLoader", Qt::QueuedConnection);

    loadPalettes();
}

KHintsSettings::~KHintsSettings()
{
    qDeleteAll(m_palettes);
}

169
170
171
QVariant KHintsSettings::readConfigValue(const QString &group, const QString &key, const QVariant &defaultValue)
{
    KConfigGroup userCg(mKdeGlobals, group);
Jan Grulich's avatar
Jan Grulich committed
172
    QVariant value = readConfigValue(userCg, key, QString());
173
174
175
176
177
178

    if (!value.isNull()) {
        return value;
    }

    if (mLnfConfig) {
179
180
        KConfigGroup lnfCg(mLnfConfig, "kdeglobals");
        lnfCg = KConfigGroup(&lnfCg, group);
181
182
183
184
185
186
187
188
189
        if (lnfCg.isValid()) {
            value = lnfCg.readEntry(key, defaultValue);

            if (!value.isNull()) {
                return value;
            }
        }
    }

190
191
    KConfigGroup lnfCg(mDefaultLnfConfig, "kdeglobals");
    lnfCg = KConfigGroup(&lnfCg, group);
192
    if (lnfCg.isValid()) {
Jan Grulich's avatar
Jan Grulich committed
193
        return lnfCg.readEntry(key, defaultValue);
194
195
196
197
198
    }

    return defaultValue;
}

Jan Grulich's avatar
Jan Grulich committed
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
QVariant KHintsSettings::readConfigValue(const KConfigGroup &cg, const QString &key, const QVariant &defaultValue) const
{
    if (mUsePortal) {
        const QString settingName = QStringLiteral("org.kde.kdeglobals.%1").arg(cg.name());
        auto groupIt = mKdeGlobalsPortal.constFind(settingName);
        if (groupIt != mKdeGlobalsPortal.constEnd()) {
            auto valueIt = groupIt.value().constFind(key);
            if (valueIt != groupIt.value().constEnd()) {
                return valueIt.value();
            }
        }
    }

    return cg.readEntry(key, defaultValue);
}

215
216
217
218
QStringList KHintsSettings::xdgIconThemePaths() const
{
    QStringList paths;

219
220
221
    // make sure we have ~/.local/share/icons in paths if it exists
    paths << QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("icons"), QStandardPaths::LocateDirectory);

222
    const QFileInfo homeIconDir(QDir::homePath() + QStringLiteral("/.icons"));
223
    if (homeIconDir.isDir()) {
224
        paths << homeIconDir.absoluteFilePath();
225
    }
226
227
228
229
230
231
232

    return paths;
}

void KHintsSettings::delayedDBusConnects()
{
    QDBusConnection::sessionBus().connect(QString(), QStringLiteral("/KToolBar"), QStringLiteral("org.kde.KToolBar"),
233
                                          QStringLiteral("styleChanged"), this, SLOT(toolbarStyleChanged()));
234
    QDBusConnection::sessionBus().connect(QString(), QStringLiteral("/KGlobalSettings"), QStringLiteral("org.kde.KGlobalSettings"),
235
                                          QStringLiteral("notifyChange"), this, SLOT(slotNotifyChange(int,int)));
Jan Grulich's avatar
Jan Grulich committed
236
237
238
239
    if (mUsePortal) {
        QDBusConnection::sessionBus().connect(QString(), QStringLiteral("/org/freedesktop/portal/desktop"), QStringLiteral("org.freedesktop.portal.Settings"),
                                              QStringLiteral("SettingChanged"), this, SLOT(slotPortalSettingChanged(QString,QString,QDBusVariant)));
    }
240
241
242
243
244
245
246
247
248
}

void KHintsSettings::setupIconLoader()
{
    connect(KIconLoader::global(), &KIconLoader::iconChanged, this, &KHintsSettings::iconChanged);
}

void KHintsSettings::toolbarStyleChanged()
{
249
250
    mKdeGlobals->reparseConfiguration();
    KConfigGroup cg(mKdeGlobals, "Toolbar style");
251
252
253
254
255
256

    m_hints[QPlatformTheme::ToolButtonStyle] = toolButtonStyle(cg);
    //from gtksymbol.cpp
    QWidgetList widgets = QApplication::allWidgets();
    for (int i = 0; i < widgets.size(); ++i) {
        QWidget *widget = widgets.at(i);
257
        if (qobject_cast<QToolButton *>(widget)) {
258
259
260
261
262
263
264
265
            QEvent event(QEvent::StyleChange);
            QApplication::sendEvent(widget, &event);
        }
    }
}

void KHintsSettings::slotNotifyChange(int type, int arg)
{
266
267
    mKdeGlobals->reparseConfiguration();
    KConfigGroup cg(mKdeGlobals, "KDE");
268

269
    switch (type) {
270
    case PaletteChanged: {
271
272
273
274
        // Don't change the palette if the application has a custom one set
        if (!qApp->property("KDE_COLOR_SCHEME_PATH").toString().isEmpty()) {
            break;
        }
275
        loadPalettes();
276
277
278
279

        //QApplication::setPalette and QGuiApplication::setPalette are different functions
        //and non virtual. Call the correct one
        if (qobject_cast<QApplication *>(QCoreApplication::instance())) {
280
281
282
283
284
285
            QPalette palette = *m_palettes[QPlatformTheme::SystemPalette];
            QApplication::setPalette(palette);
            // QTBUG QGuiApplication::paletteChanged() signal is only emitted by QGuiApplication
            // so things like SystemPalette QtQuick item that use it won't notice a palette
            // change when a QApplication which causes e.g. QML System Settings modules to not update
            emit qApp->paletteChanged(palette);
286
287
288
289
290
        } else if (qobject_cast<QGuiApplication *>(QCoreApplication::instance())) {
            QGuiApplication::setPalette(*m_palettes[QPlatformTheme::SystemPalette]);
        }
        break;
    }
291
292
293
294
295
296
297
298
    case SettingsChanged: {

        SettingsCategory category = static_cast<SettingsCategory>(arg);
        if (category == SETTINGS_QT || category == SETTINGS_MOUSE) {
            updateQtSettings(cg);
        } else if (category == SETTINGS_STYLE) {
            m_hints[QPlatformTheme::DialogButtonBoxButtonsHaveIcons] = cg.readEntry("ShowIconsOnPushButtons", true);
            m_hints[QPlatformTheme::UiEffects] = cg.readEntry("GraphicEffectsLevel", 0) != 0 ? QPlatformTheme::GeneralUiEffect : 0;
299
300

            updateShowIconsInMenuItems(cg);
301
302
303
304
305
306
307
308
309
310
        }
        break;
    }
    case ToolbarStyleChanged: {
        toolbarStyleChanged();
        break;
    }
    case IconChanged:
        iconChanged(arg); //Once the KCM is ported to use IconChanged, this should not be needed
        break;
Martin Flöser's avatar
Martin Flöser committed
311
312
313
    case CursorChanged:
        updateCursorTheme();
        break;
314
    case StyleChanged: {
315
        QApplication *app = qobject_cast<QApplication *>(QCoreApplication::instance());
316
317
318
319
        if (!app) {
            return;
        }

320
321
        // HOTFIX here. Hardcoded default value is duplicated and may be inconsistent with the one actually defined in kcm_style kcfg
        const QString theme = readConfigValue(cg, QStringLiteral("widgetStyle"), QStringLiteral(BREEZE_STYLE_NAME)).toString();
322

323
        QStringList styleNames;
324
325
326
327
328
329
330
        if (theme != QStringLiteral(BREEZE_STYLE_NAME)) {
            styleNames << theme;
        }
        styleNames << QStringLiteral(BREEZE_STYLE_NAME)
                   << QStringLiteral("oxygen")
                   << QStringLiteral("fusion")
                   << QStringLiteral("windows");
Laurent Montel's avatar
Laurent Montel committed
331
        const QString lnfStyle = readConfigValue(QStringLiteral("KDE"), QStringLiteral("widgetStyle"), QString()).toString();
332
333
334
335
336
        if (!lnfStyle.isEmpty() && !styleNames.contains(lnfStyle)) {
            styleNames.prepend(lnfStyle);
        }
        m_hints[QPlatformTheme::StyleNames] = styleNames;

337
338
339
340
341
342
343
344
345
        app->setStyle(theme);
        loadPalettes();
        break;
    }
    default:
        qWarning() << "Unknown type of change in KGlobalSettings::slotNotifyChange: " << type;
    }
}

Jan Grulich's avatar
Jan Grulich committed
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
void KHintsSettings::slotPortalSettingChanged(const QString &group, const QString &key, const QDBusVariant &value)
{
    if (group == QLatin1String("org.kde.kdeglobals.General") && key == QLatin1String("ColorScheme")) {
        // For colors obtain complete configuration again
        updatePortalSetting();
        slotNotifyChange(PaletteChanged, 0);
    } else if (group == QLatin1String("org.kde.kdeglobals.KDE") && key == QLatin1String("widgetStyle")) {
        mKdeGlobalsPortal[group][key] = value.variant().toString();
        slotNotifyChange(StyleChanged, 0);
    } else if (group == QLatin1String("org.kde.kdeglobals.Icons") && key == QLatin1String("Theme")) {
        mKdeGlobalsPortal[group][key] = value.variant().toString();
        // Change icons for each group
        for (int i = 0; i <= 5; ++i) {
            iconChanged(i);
        }
    } else if (group == QLatin1String("org.kde.kdeglobals.Toolbar style") && key == QLatin1String("ToolButtonStyle")) {
        mKdeGlobalsPortal[group][key] = value.variant().toString();
        toolbarStyleChanged();
    }
}

367
368
369
370
void KHintsSettings::iconChanged(int group)
{
    KIconLoader::Group iconGroup = (KIconLoader::Group) group;
    if (iconGroup != KIconLoader::MainToolbar) {
371
        m_hints[QPlatformTheme::SystemIconThemeName] = readConfigValue(QStringLiteral("Icons"), QStringLiteral("Theme"), QStringLiteral("breeze"));
372
373
374
375
376
377
378
379
380
381
382
        return;
    }

    const int currentSize = KIconLoader::global()->currentSize(KIconLoader::MainToolbar);
    if (m_hints[QPlatformTheme::ToolBarIconSize] == currentSize) {
        return;
    }

    m_hints[QPlatformTheme::ToolBarIconSize] = currentSize;

    //If we are not a QApplication, means that we are a QGuiApplication, then we do nothing.
383
    if (!qobject_cast<QApplication *>(QCoreApplication::instance())) {
384
385
386
387
        return;
    }

    QWidgetList widgets = QApplication::allWidgets();
388
389
    Q_FOREACH (QWidget *widget, widgets) {
        if (qobject_cast<QToolBar *>(widget) || qobject_cast<QMainWindow *>(widget)) {
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
            QEvent event(QEvent::StyleChange);
            QApplication::sendEvent(widget, &event);
        }
    }
}

void KHintsSettings::updateQtSettings(KConfigGroup &cg)
{
    int flash = qBound(200, cg.readEntry("CursorBlinkRate", 1000), 2000);
    m_hints[QPlatformTheme::CursorFlashTime] = flash;

    int doubleClickInterval = cg.readEntry("DoubleClickInterval", 400);
    m_hints[QPlatformTheme::MouseDoubleClickInterval] = doubleClickInterval;

    int startDragDistance = cg.readEntry("StartDragDist", 10);
    m_hints[QPlatformTheme::StartDragDistance] = startDragDistance;

    int startDragTime = cg.readEntry("StartDragTime", 10);
    m_hints[QPlatformTheme::StartDragTime] = startDragTime;

410
    m_hints[QPlatformTheme::ItemViewActivateItemOnSingleClick] = cg.readEntry("SingleClick", true);
411

412
    updateShowIconsInMenuItems(cg);
413

414
    int wheelScrollLines = cg.readEntry("WheelScrollLines", 3);
415
    m_hints[QPlatformTheme::WheelScrollLines] = wheelScrollLines;
416
    QApplication *app = qobject_cast<QApplication *>(QCoreApplication::instance());
417
    if (app) {
418
        QApplication::setWheelScrollLines(cg.readEntry("WheelScrollLines", 3));
419
420
421
    }
}

422
423
void KHintsSettings::updateShowIconsInMenuItems(KConfigGroup &cg)
{
Jan Grulich's avatar
Jan Grulich committed
424
    bool showIcons = readConfigValue(cg, QStringLiteral("ShowIconsInMenuItems"), true).toBool();
425
426
427
    QCoreApplication::setAttribute(Qt::AA_DontShowIconsInMenus, !showIcons);
}

Jan Grulich's avatar
Jan Grulich committed
428
Qt::ToolButtonStyle KHintsSettings::toolButtonStyle(const KConfigGroup &cg)
429
{
Jan Grulich's avatar
Jan Grulich committed
430
    const QString buttonStyle = readConfigValue(cg, QStringLiteral("ToolButtonStyle"), QStringLiteral("TextBesideIcon")).toString().toLower();
Laurent Montel's avatar
Laurent Montel committed
431
432
433
434
435
    return buttonStyle == QLatin1String("textbesideicon") ? Qt::ToolButtonTextBesideIcon
           : buttonStyle == QLatin1String("icontextright") ? Qt::ToolButtonTextBesideIcon
           : buttonStyle == QLatin1String("textundericon") ? Qt::ToolButtonTextUnderIcon
           : buttonStyle == QLatin1String("icontextbottom") ? Qt::ToolButtonTextUnderIcon
           : buttonStyle == QLatin1String("textonly") ? Qt::ToolButtonTextOnly
436
           : Qt::ToolButtonIconOnly;
437
438
439
440
441
442
443
}

void KHintsSettings::loadPalettes()
{
    qDeleteAll(m_palettes);
    m_palettes.clear();

Jan Grulich's avatar
Jan Grulich committed
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
    if (mUsePortal && mKdeGlobalsPortal.contains(QStringLiteral("org.kde.kdeglobals.Colors:View"))) {
        // Construct a temporary KConfig file containing color setting so we can create a KColorScheme from it
        QTemporaryFile file;
        file.open();

        KSharedConfigPtr tempConfig = KSharedConfig::openConfig(file.fileName(), KConfig::SimpleConfig);
        for (auto groupIt = mKdeGlobalsPortal.constBegin(); groupIt != mKdeGlobalsPortal.constEnd(); ++groupIt) {
            if (groupIt.key().startsWith(QStringLiteral("org.kde.kdeglobals.Colors:"))) {
                KConfigGroup tempGroup(tempConfig, groupIt.key().right(groupIt.key().length() - QStringLiteral("org.kde.kdeglobals.").length()));
                for (auto valueIt = groupIt.value().constBegin(); valueIt != groupIt.value().constEnd(); ++valueIt) {
                    tempGroup.writeEntry(valueIt.key(), valueIt.value());
                }
            }
        }
        m_palettes[QPlatformTheme::SystemPalette] = new QPalette(KColorScheme::createApplicationPalette(tempConfig));
    } else if (mKdeGlobals->hasGroup("Colors:View")) {
460
461
462
        m_palettes[QPlatformTheme::SystemPalette] = new QPalette(KColorScheme::createApplicationPalette(mKdeGlobals));
    } else {
        KConfigGroup cg(mKdeGlobals, "KDE");
Jan Grulich's avatar
Jan Grulich committed
463
        const QString looknfeel = readConfigValue(cg, QStringLiteral("LookAndFeelPackage"), defaultLookAndFeelPackage).toString();
464
        QString path = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("plasma/look-and-feel/") + looknfeel + QStringLiteral("/contents/colors"));
465
466
467
468
469
        if (!path.isEmpty()) {
            m_palettes[QPlatformTheme::SystemPalette] = new QPalette(KColorScheme::createApplicationPalette(KSharedConfig::openConfig(path)));
            return;
        }

470
471
        const QString scheme = readConfigValue(QStringLiteral("General"), QStringLiteral("ColorScheme"), QStringLiteral("Breeze")).toString();
        path = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("color-schemes/") + scheme + QStringLiteral(".colors"));
472
473
474
475
476

        if (!path.isEmpty()) {
            m_palettes[QPlatformTheme::SystemPalette] = new QPalette(KColorScheme::createApplicationPalette(KSharedConfig::openConfig(path)));
        }
    }
477
}
Martin Flöser's avatar
Martin Flöser committed
478
479
480

void KHintsSettings::updateCursorTheme()
{
Laurent Montel's avatar
Laurent Montel committed
481
    KConfig config(QStringLiteral("kcminputrc"));
Martin Flöser's avatar
Martin Flöser committed
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
    KConfigGroup g(&config, "Mouse");

    int size      = g.readEntry("cursorSize", -1);

    // Default cursor size is 16 points
    if (size == -1) {
        if (QScreen *s = QGuiApplication::primaryScreen()) {
            size = s->logicalDotsPerInchY() * 16 / 72;
        } else {
            size = 0;
        }
    }

#if HAVE_X11
    if (QX11Info::isPlatformX11()) {
497
        const QString theme = g.readEntry("cursorTheme", QString());
Martin Flöser's avatar
Martin Flöser committed
498
499
500
501
502
503
504
505
506
507
        // Note that in X11R7.1 and earlier, calling XcursorSetTheme()
        // with a NULL theme would cause Xcursor to use "default", but
        // in 7.2 and later it will cause it to revert to the theme that
        // was configured when the application was started.
        XcursorSetTheme(QX11Info::display(), theme.isNull() ?
                        "default" : QFile::encodeName(theme).constData());
        XcursorSetDefaultSize(QX11Info::display(), size);
    }
#endif
}
Jan Grulich's avatar
Jan Grulich committed
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525

void KHintsSettings::updatePortalSetting()
{
    mKdeGlobalsPortal.clear();

    QDBusMessage message = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.portal.Desktop"),
                                                          QStringLiteral("/org/freedesktop/portal/desktop"),
                                                          QStringLiteral("org.freedesktop.portal.Settings"),
                                                          QStringLiteral("ReadAll"));
    message << QStringList{QStringLiteral("org.kde.kdeglobals.*")};

    // FIXME: async?
    QDBusMessage resultMessage = QDBusConnection::sessionBus().call(message);
    if (resultMessage.type() == QDBusMessage::ReplyMessage) {
        QDBusArgument dbusArgument = resultMessage.arguments().at(0).value<QDBusArgument>();
        dbusArgument >> mKdeGlobalsPortal;
    }
}