Changeset View
Changeset View
Standalone View
Standalone View
src/appearancegtk3.cpp
Show All 19 Lines | |||||
20 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. | 20 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
21 | */ | 21 | */ | ||
22 | 22 | | |||
23 | #include "appearancegtk3.h" | 23 | #include "appearancegtk3.h" | ||
24 | #include <QFile> | 24 | #include <QFile> | ||
25 | #include <QDir> | 25 | #include <QDir> | ||
26 | #include <QDebug> | 26 | #include <QDebug> | ||
27 | #include <QStandardPaths> | 27 | #include <QStandardPaths> | ||
28 | #include <KSharedConfig> | | |||
29 | #include <KConfigGroup> | | |||
30 | 28 | | |||
31 | QStringList AppearanceGTK3::installedThemes() const | 29 | QStringList AppearanceGTK3::installedThemes() const | ||
32 | { | 30 | { | ||
33 | QFileInfoList availableThemes; | 31 | QFileInfoList availableThemes; | ||
34 | foreach(const QString& themesDir, QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, "themes", QStandardPaths::LocateDirectory)) { | 32 | foreach(const QString& themesDir, QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, "themes", QStandardPaths::LocateDirectory)) { | ||
35 | QDir root(themesDir); | 33 | QDir root(themesDir); | ||
36 | availableThemes += root.entryInfoList(QDir::NoDotAndDotDot|QDir::AllDirs); | 34 | availableThemes += root.entryInfoList(QDir::NoDotAndDotDot|QDir::AllDirs); | ||
37 | } | 35 | } | ||
Show All 10 Lines | 43 | for(QFileInfoList::iterator it=availableThemes.begin(); it!=availableThemes.end(); ++it) { | |||
48 | //If it doesn't exist, we don't want it on the list | 46 | //If it doesn't exist, we don't want it on the list | ||
49 | if(hasGtkrc) | 47 | if(hasGtkrc) | ||
50 | themes += it->filePath(); | 48 | themes += it->filePath(); | ||
51 | } | 49 | } | ||
52 | 50 | | |||
53 | return themes; | 51 | return themes; | ||
54 | } | 52 | } | ||
55 | 53 | | |||
56 | bool AppearanceGTK3::saveSettings(const KSharedConfig::Ptr& file) const | 54 | bool AppearanceGTK3::saveSettings(const QString& file) const | ||
57 | { | 55 | { | ||
58 | KConfigGroup group(file, "Settings"); | 56 | //Opening GTK3 config file $ENV{XDG_CONFIG_HOME}/gtk-3.0/m_settings.ini | ||
57 | QDir::home().mkpath(file.left(file.lastIndexOf('/'))); //we make sure the path exists | ||||
58 | QFile file_gtk3(file); | ||||
59 | 59 | | |||
60 | group.writeEntry("gtk-font-name", m_settings["font"]); | 60 | if(!file_gtk3.open(QIODevice::WriteOnly | QIODevice::Text)) { | ||
61 | group.writeEntry("gtk-theme-name", m_settings["theme"]); | 61 | qWarning() << "Couldn't open GTK3 config file for writing at:" << file_gtk3.fileName(); | ||
62 | group.writeEntry("gtk-icon-theme-name", m_settings["icon"]); | 62 | return false; | ||
63 | group.writeEntry("gtk-fallback-icon-theme", m_settings["icon_fallback"]); | 63 | } | ||
64 | group.writeEntry("gtk-cursor-theme-name", m_settings["cursor"]); | 64 | QTextStream flow3(&file_gtk3); | ||
65 | group.writeEntry("gtk-toolbar-style", m_settings["toolbar_style"]); | 65 | flow3 << "[Settings]\n"; | ||
66 | group.writeEntry("gtk-menu-images", m_settings["show_icons_menus"]); | 66 | flow3 << "gtk-font-name=" << m_settings["font"] << "\n"; | ||
67 | group.writeEntry("gtk-button-images", m_settings["show_icons_buttons"]); | 67 | flow3 << "gtk-theme-name=" << m_settings["theme"] << "\n"; | ||
68 | group.writeEntry("gtk-primary-button-warps-slider", m_settings["primary_button_warps_slider"]); | 68 | flow3 << "gtk-icon-theme-name="<< m_settings["icon"] << "\n"; | ||
69 | group.writeEntry("gtk-application-prefer-dark-theme", m_settings["application_prefer_dark_theme"]); | 69 | flow3 << "gtk-fallback-icon-theme=" << m_settings["icon_fallback"] << "\n"; | ||
70 | flow3 << "gtk-cursor-theme-name=" << m_settings["cursor"] << "\n"; | ||||
71 | flow3 << "gtk-toolbar-style=" << m_settings["toolbar_style"] << "\n"; | ||||
72 | flow3 << "gtk-menu-images=" << m_settings["show_icons_menus"] << "\n"; | ||||
73 | flow3 << "gtk-button-images=" << m_settings["show_icons_buttons"] << "\n"; | ||||
74 | flow3 << "gtk-primary-button-warps-slider=" << m_settings["primary_button_warps_slider"] << "\n"; | ||||
75 | flow3 << "gtk-application-prefer-dark-theme=" << m_settings["application_prefer_dark_theme"] << "\n"; | ||||
70 | 76 | | |||
71 | const bool sync = group.sync(); | | |||
72 | Q_ASSERT(sync); | | |||
73 | return true; | 77 | return true; | ||
74 | } | 78 | } | ||
75 | 79 | | |||
76 | bool AppearanceGTK3::loadSettings(const KSharedConfig::Ptr& file) | 80 | bool AppearanceGTK3::loadSettings(const QString& path) | ||
77 | { | 81 | { | ||
78 | KConfigGroup group(file, "Settings"); | 82 | QFile fileGtk3(path); | ||
83 | bool canRead=fileGtk3.open(QIODevice::ReadOnly | QIODevice::Text); | ||||
79 | 84 | | |||
80 | if (!file || !group.isValid()) { | 85 | if(canRead) { | ||
81 | qWarning() << "Cannot open the GTK3 config file" << file; | 86 | const QMap<QString, QString> foundSettings = readSettingsTuples(&fileGtk3); | ||
82 | return false; | | |||
83 | } | | |||
84 | 87 | | |||
85 | m_settings = QMap<QString, QString> { | 88 | m_settings = QMap<QString, QString> { | ||
86 | {"toolbar_style", "GTK_TOOLBAR_ICONS"}, | 89 | {"toolbar_style", "GTK_TOOLBAR_ICONS"}, | ||
87 | {"show_icons_buttons", "0"}, | 90 | {"show_icons_buttons", "0"}, | ||
88 | {"show_icons_menus", "0"}, | 91 | {"show_icons_menus", "0"}, | ||
89 | {"primary_button_warps_slider", "false"}, | 92 | {"primary_button_warps_slider", "false"}, | ||
90 | {"application_prefer_dark_theme", "false"} | 93 | {"application_prefer_dark_theme", "false"} | ||
91 | }; | 94 | }; | ||
92 | 95 | | |||
93 | m_settings["theme"] = group.readEntry("gtk-theme-name"); | 96 | for(auto it = foundSettings.constBegin(), itEnd = foundSettings.constEnd(); it!=itEnd; ++it) { | ||
94 | m_settings["icon"] = group.readEntry("gtk-icon-theme-name"); | 97 | if (it.key() == "gtk-theme-name") | ||
95 | m_settings["icon_fallback"] = group.readEntry("gtk-fallback-icon-theme"); | 98 | m_settings["theme"] = *it; | ||
96 | m_settings["cursor"] = group.readEntry("gtk-cursor-theme-name"); | 99 | else if (it.key() == "gtk-icon-theme-name") | ||
97 | m_settings["font"] = group.readEntry("gtk-font-name"); | 100 | m_settings["icon"] = *it; | ||
98 | m_settings["toolbar_style"] = group.readEntry("gtk-toolbar-style"); | 101 | else if (it.key() == "gtk-fallback-icon-theme") | ||
99 | m_settings["show_icons_buttons"] = group.readEntry("gtk-button-images"); | 102 | m_settings["icon_fallback"] = *it; | ||
100 | m_settings["show_icons_menus"] = group.readEntry("gtk-menu-images"); | 103 | else if (it.key() == "gtk-cursor-theme-name") | ||
101 | m_settings["primary_button_warps_slider"] = group.readEntry("gtk-primary-button-warps-slider"); | 104 | m_settings["cursor"] = *it; | ||
102 | m_settings["application_prefer_dark_theme"] = group.readEntry("gtk-application-prefer-dark-theme"); | 105 | else if (it.key() == "gtk-font-name") | ||
103 | for(auto it = m_settings.begin(); it != m_settings.end(); ) { | 106 | m_settings["font"] = *it; | ||
104 | if (it.value().isEmpty()) | 107 | else if (it.key() == "gtk-toolbar-style") | ||
105 | it = m_settings.erase(it); | 108 | m_settings["toolbar_style"] = *it; | ||
109 | else if (it.key() == "gtk-button-images") | ||||
110 | m_settings["show_icons_buttons"] = *it; | ||||
111 | else if (it.key() == "gtk-menu-images") | ||||
112 | m_settings["show_icons_menus"] = *it; | ||||
113 | else if (it.key() == "gtk-primary-button-warps-slider") | ||||
114 | m_settings["primary_button_warps_slider"] = *it; | ||||
115 | else if (it.key() == "gtk-application-prefer-dark-theme") | ||||
116 | m_settings["application_prefer_dark_theme"] = *it; | ||||
106 | else | 117 | else | ||
107 | ++it; | 118 | qWarning() << "unknown field" << it.key(); | ||
108 | } | | |||
109 | return true; | | |||
110 | } | 119 | } | ||
120 | } else | ||||
121 | qWarning() << "Cannot open the GTK3 config file" << path; | ||||
111 | 122 | | |||
112 | QString AppearanceGTK3::configFileName() const | 123 | return canRead; | ||
113 | { | | |||
114 | return QStringLiteral("gtk-3.0/settings.ini"); | | |||
115 | } | 124 | } | ||
116 | 125 | | |||
117 | QString AppearanceGTK3::defaultConfigFile() const | 126 | QString AppearanceGTK3::defaultConfigFile() const | ||
118 | { | 127 | { | ||
119 | QString root = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation); | 128 | QString root = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation); | ||
120 | if(root.isEmpty()) | 129 | if(root.isEmpty()) | ||
121 | root = QFileInfo(QDir::home(), ".config").absoluteFilePath(); | 130 | root = QFileInfo(QDir::home(), ".config").absoluteFilePath(); | ||
122 | 131 | | |||
123 | return root + '/' + configFileName(); | 132 | return root+"/gtk-3.0/settings.ini"; | ||
124 | } | 133 | } | ||
125 | 134 | | |||
126 | bool AppearanceGTK3::getApplicationPreferDarkTheme() const | 135 | bool AppearanceGTK3::getApplicationPreferDarkTheme() const | ||
127 | { | 136 | { | ||
128 | return m_settings["application_prefer_dark_theme"] == "1" || m_settings["application_prefer_dark_theme"] == "true"; | 137 | return m_settings["application_prefer_dark_theme"] == "1" || m_settings["application_prefer_dark_theme"] == "true"; | ||
129 | } | 138 | } | ||
130 | 139 | | |||
131 | void AppearanceGTK3::setApplicationPreferDarkTheme(const bool& enable) | 140 | void AppearanceGTK3::setApplicationPreferDarkTheme(const bool& enable) | ||
132 | { | 141 | { | ||
133 | m_settings["application_prefer_dark_theme"] = enable ? "true" : "false"; | 142 | m_settings["application_prefer_dark_theme"] = enable ? "true" : "false"; | ||
134 | } | 143 | } | ||
135 | | ||||
136 | bool AppearanceGTK3::saveSettings(const QString& file) const | | |||
137 | { | | |||
138 | auto cfg = KSharedConfig::openConfig(file); | | |||
139 | return saveSettings(cfg); | | |||
140 | } | | |||
141 | | ||||
142 | bool AppearanceGTK3::loadSettings(const QString& path) | | |||
143 | { | | |||
144 | auto cfg = KSharedConfig::openConfig(path); | | |||
145 | return loadSettings(cfg); | | |||
146 | } | | |||
147 | | ||||
148 | bool AppearanceGTK3::loadSettings() | | |||
149 | { | | |||
150 | auto cfg = KSharedConfig::openConfig(configFileName()); | | |||
151 | cfg->setReadDefaults(true); | | |||
152 | return loadSettings(cfg); | | |||
153 | } | | |||
154 | | ||||
155 | bool AppearanceGTK3::saveSettings() const | | |||
156 | { | | |||
157 | auto cfg = KSharedConfig::openConfig(configFileName()); | | |||
158 | cfg->setReadDefaults(true); | | |||
159 | return saveSettings(cfg); | | |||
160 | } | |