Changeset View
Changeset View
Standalone View
Standalone View
src/appearancegtk3.cpp
Show All 23 Lines | |||||
24 | #include <QDir> | 24 | #include <QDir> | ||
25 | #include <QDebug> | 25 | #include <QDebug> | ||
26 | #include <QStandardPaths> | 26 | #include <QStandardPaths> | ||
27 | 27 | | |||
28 | #include <KSharedConfig> | 28 | #include <KSharedConfig> | ||
29 | #include <KConfigGroup> | 29 | #include <KConfigGroup> | ||
30 | 30 | | |||
31 | #include <gio/gio.h> | 31 | #include <gio/gio.h> | ||
32 | #include <gtk/gtk.h> | | |||
33 | 32 | | |||
34 | #include "appearancegtk3.h" | 33 | #include "appearancegtk3.h" | ||
35 | 34 | | |||
36 | QStringList AppearanceGTK3::installedThemes() const | 35 | QStringList AppearanceGTK3::installedThemes() const | ||
37 | { | 36 | { | ||
38 | QFileInfoList availableThemes; | 37 | QFileInfoList availableThemes; | ||
39 | for (const QString& themesDir : QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("themes"), QStandardPaths::LocateDirectory)) { | 38 | for (const QString& themesDir : QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("themes"), QStandardPaths::LocateDirectory)) { | ||
40 | QDir root(themesDir); | 39 | QDir root(themesDir); | ||
Show All 18 Lines | |||||
59 | return themes; | 58 | return themes; | ||
60 | } | 59 | } | ||
61 | 60 | | |||
62 | bool AppearanceGTK3::saveSettings(const KSharedConfig::Ptr& file) const | 61 | bool AppearanceGTK3::saveSettings(const KSharedConfig::Ptr& file) const | ||
63 | { | 62 | { | ||
64 | KConfigGroup group(file, QStringLiteral("Settings")); | 63 | KConfigGroup group(file, QStringLiteral("Settings")); | ||
65 | 64 | | |||
66 | group.writeEntry(QStringLiteral("gtk-theme-name"), m_settings["theme"]); | 65 | group.writeEntry(QStringLiteral("gtk-theme-name"), m_settings["theme"]); | ||
67 | group.writeEntry(QStringLiteral("gtk-application-prefer-dark-theme"), m_settings[QStringLiteral("application_prefer_dark_theme")]); | | |||
68 | 66 | | |||
69 | const bool sync = group.sync(); | 67 | const bool sync = group.sync(); | ||
70 | Q_ASSERT(sync); | 68 | Q_ASSERT(sync); | ||
71 | return true; | 69 | return true; | ||
72 | } | 70 | } | ||
73 | 71 | | |||
74 | bool AppearanceGTK3::loadSettings(const KSharedConfig::Ptr& file) | 72 | bool AppearanceGTK3::loadSettings(const KSharedConfig::Ptr& file) | ||
75 | { | 73 | { | ||
76 | KConfigGroup group(file, QStringLiteral("Settings")); | 74 | KConfigGroup group(file, QStringLiteral("Settings")); | ||
77 | 75 | | |||
78 | if (!file || !group.isValid()) { | 76 | if (!file || !group.isValid()) { | ||
79 | qWarning() << QStringLiteral("Cannot open the GTK3 config file") << file; | 77 | qWarning() << QStringLiteral("Cannot open the GTK3 config file") << file; | ||
80 | return false; | 78 | return false; | ||
81 | } | 79 | } | ||
82 | 80 | | |||
83 | m_settings = QMap<QString, QString> { | 81 | m_settings.clear(); | ||
84 | {QStringLiteral("application_prefer_dark_theme"), QStringLiteral("false")} | | |||
85 | }; | | |||
86 | 82 | | |||
87 | m_settings[QStringLiteral("theme")] = group.readEntry(QStringLiteral("gtk-theme-name")); | 83 | m_settings[QStringLiteral("theme")] = group.readEntry(QStringLiteral("gtk-theme-name")); | ||
88 | m_settings[QStringLiteral("application_prefer_dark_theme")] = group.readEntry(QStringLiteral("gtk-application-prefer-dark-theme")); | | |||
89 | for(auto it = m_settings.begin(); it != m_settings.end(); ) { | 84 | for(auto it = m_settings.begin(); it != m_settings.end(); ) { | ||
90 | if (it.value().isEmpty()) { | 85 | if (it.value().isEmpty()) { | ||
91 | it = m_settings.erase(it); | 86 | it = m_settings.erase(it); | ||
92 | } else { | 87 | } else { | ||
93 | ++it; | 88 | ++it; | ||
94 | } | 89 | } | ||
95 | } | 90 | } | ||
96 | return true; | 91 | return true; | ||
Show All 9 Lines | 100 | { | |||
106 | QString root = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation); | 101 | QString root = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation); | ||
107 | if(root.isEmpty()) { | 102 | if(root.isEmpty()) { | ||
108 | root = QFileInfo(QDir::home(), QStringLiteral(".config")).absoluteFilePath(); | 103 | root = QFileInfo(QDir::home(), QStringLiteral(".config")).absoluteFilePath(); | ||
109 | } | 104 | } | ||
110 | 105 | | |||
111 | return root + '/' + configFileName(); | 106 | return root + '/' + configFileName(); | ||
112 | } | 107 | } | ||
113 | 108 | | |||
114 | bool AppearanceGTK3::getApplicationPreferDarkTheme() const | | |||
115 | { | | |||
116 | return m_settings[QStringLiteral("application_prefer_dark_theme")] == QStringLiteral("1") || m_settings[QStringLiteral("application_prefer_dark_theme")] == QStringLiteral("true"); | | |||
117 | } | | |||
118 | | ||||
119 | void AppearanceGTK3::setApplicationPreferDarkTheme(bool enable) | | |||
120 | { | | |||
121 | m_settings[QStringLiteral("application_prefer_dark_theme")] = enable ? QStringLiteral("true") : QStringLiteral("false"); | | |||
122 | } | | |||
123 | | ||||
124 | bool AppearanceGTK3::saveSettings(const QString& file) const | 109 | bool AppearanceGTK3::saveSettings(const QString& file) const | ||
125 | { | 110 | { | ||
126 | auto cfg = KSharedConfig::openConfig(file, KConfig::NoGlobals); | 111 | auto cfg = KSharedConfig::openConfig(file, KConfig::NoGlobals); | ||
127 | return saveSettings(cfg); | 112 | return saveSettings(cfg); | ||
128 | } | 113 | } | ||
129 | 114 | | |||
130 | bool AppearanceGTK3::loadSettings(const QString& path) | 115 | bool AppearanceGTK3::loadSettings(const QString& path) | ||
131 | { | 116 | { | ||
Show All 11 Lines | |||||
143 | { | 128 | { | ||
144 | // FIXME kdebz#387417 | 129 | // FIXME kdebz#387417 | ||
145 | // We should maybe use GSettings everywhere in future, but at this moment we | 130 | // We should maybe use GSettings everywhere in future, but at this moment we | ||
146 | // need this to have this configuration available in sandboxed applications which | 131 | // need this to have this configuration available in sandboxed applications which | ||
147 | // is only possible through dconf | 132 | // is only possible through dconf | ||
148 | g_autoptr(GSettings) gsettings = g_settings_new("org.gnome.desktop.interface"); | 133 | g_autoptr(GSettings) gsettings = g_settings_new("org.gnome.desktop.interface"); | ||
149 | g_settings_set_string(gsettings, "gtk-theme", m_settings["theme"].toUtf8().constData()); | 134 | g_settings_set_string(gsettings, "gtk-theme", m_settings["theme"].toUtf8().constData()); | ||
150 | 135 | | |||
151 | g_object_set(gtk_settings_get_default(), "gtk-application-prefer-dark-theme", getApplicationPreferDarkTheme(), nullptr); | | |||
152 | | ||||
153 | auto cfg = KSharedConfig::openConfig(configFileName(), KConfig::NoGlobals); | 136 | auto cfg = KSharedConfig::openConfig(configFileName(), KConfig::NoGlobals); | ||
154 | return saveSettings(cfg); | 137 | return saveSettings(cfg); | ||
155 | } | 138 | } |