Changeset View
Changeset View
Standalone View
Standalone View
src/appearancegtk2.cpp
Show All 24 Lines | |||||
25 | #include <QStringList> | 25 | #include <QStringList> | ||
26 | #include <QDir> | 26 | #include <QDir> | ||
27 | #include <QDirIterator> | 27 | #include <QDirIterator> | ||
28 | #include <QDebug> | 28 | #include <QDebug> | ||
29 | #include <QProcess> | 29 | #include <QProcess> | ||
30 | #include <QStandardPaths> | 30 | #include <QStandardPaths> | ||
31 | #include <config.h> | 31 | #include <config.h> | ||
32 | 32 | | |||
33 | bool AppearanceGTK2::loadSettingsPrivate(const QString& path) | 33 | bool AppearanceGTK2::loadSettings(const QString& path) | ||
34 | { | 34 | { | ||
35 | QFile configFile(path); | 35 | QFile configFile(path); | ||
36 | 36 | | |||
37 | if (!configFile.open(QIODevice::ReadOnly | QIODevice::Text)) | 37 | bool canRead = configFile.open(QIODevice::ReadOnly | QIODevice::Text); | ||
38 | return false; | | |||
39 | 38 | | |||
39 | if(canRead) { | ||||
40 | // qDebug() << "The gtk2 config file exists..."; | ||||
40 | const QMap<QString, QString> foundSettings = readSettingsTuples(&configFile); | 41 | const QMap<QString, QString> foundSettings = readSettingsTuples(&configFile); | ||
42 | m_settings = QMap<QString, QString> { | ||||
43 | {"toolbar_style", "GTK_TOOLBAR_ICONS"}, | ||||
44 | {"show_icons_buttons", "0"}, | ||||
45 | {"show_icons_menus", "0"}, | ||||
46 | {"primary_button_warps_slider", "false"} | ||||
47 | }; | ||||
41 | 48 | | |||
42 | for(auto it = foundSettings.constBegin(), itEnd = foundSettings.constEnd(); it!=itEnd; ++it) { | 49 | for(auto it = foundSettings.constBegin(), itEnd = foundSettings.constEnd(); it!=itEnd; ++it) { | ||
43 | if (it.key() == "gtk-theme-name") | 50 | if (it.key() == "gtk-theme-name") | ||
44 | m_settings["theme"] = *it; | 51 | m_settings["theme"] = *it; | ||
45 | else if (it.key() == "gtk-icon-theme-name") | 52 | else if (it.key() == "gtk-icon-theme-name") | ||
46 | m_settings["icon"] = *it; | 53 | m_settings["icon"] = *it; | ||
47 | else if (it.key() == "gtk-fallback-icon-theme") | 54 | else if (it.key() == "gtk-fallback-icon-theme") | ||
48 | m_settings["icon_fallback"] = *it; | 55 | m_settings["icon_fallback"] = *it; | ||
49 | else if (it.key() == "gtk-cursor-theme-name") | 56 | else if (it.key() == "gtk-cursor-theme-name") | ||
50 | m_settings["cursor"] = *it; | 57 | m_settings["cursor"] = *it; | ||
51 | else if (it.key() == "gtk-font-name") | 58 | else if (it.key() == "gtk-font-name") | ||
52 | m_settings["font"] = *it; | 59 | m_settings["font"] = *it; | ||
53 | else if (it.key() == "gtk-toolbar-style") | 60 | else if (it.key() == "gtk-toolbar-style") | ||
54 | m_settings["toolbar_style"] = *it; | 61 | m_settings["toolbar_style"] = *it; | ||
55 | else if (it.key() == "gtk-button-images") | 62 | else if (it.key() == "gtk-button-images") | ||
56 | m_settings["show_icons_buttons"] = *it; | 63 | m_settings["show_icons_buttons"] = *it; | ||
57 | else if(it.key() == "gtk-menu-images") | 64 | else if(it.key() == "gtk-menu-images") | ||
58 | m_settings["show_icons_menus"] = *it; | 65 | m_settings["show_icons_menus"] = *it; | ||
59 | else if (it.key() == "gtk-primary-button-warps-slider") | 66 | else if (it.key() == "gtk-primary-button-warps-slider") | ||
60 | m_settings["primary_button_warps_slider"] = *it; | 67 | m_settings["primary_button_warps_slider"] = *it; | ||
61 | else | 68 | else | ||
62 | qWarning() << "unknown field" << it.key(); | 69 | qWarning() << "unknown field" << it.key(); | ||
63 | } | 70 | } | ||
64 | return true; | 71 | | ||
72 | } | ||||
73 | | ||||
74 | return canRead; | ||||
65 | } | 75 | } | ||
66 | 76 | | |||
67 | QString AppearanceGTK2::themesGtkrcFile(const QString& themeName) const | 77 | QString AppearanceGTK2::themesGtkrcFile(const QString& themeName) const | ||
68 | { | 78 | { | ||
69 | QStringList themes=installedThemes(); | 79 | QStringList themes=installedThemes(); | ||
70 | themes=themes.filter(QRegExp("/"+themeName+"/?$")); | 80 | themes=themes.filter(QRegExp("/"+themeName+"/?$")); | ||
71 | if(themes.size()==1) { | 81 | if(themes.size()==1) { | ||
72 | QDirIterator it(themes.first(), QDirIterator::Subdirectories); | 82 | QDirIterator it(themes.first(), QDirIterator::Subdirectories); | ||
73 | while(it.hasNext()) { | 83 | while(it.hasNext()) { | ||
74 | it.next(); | 84 | it.next(); | ||
75 | if(it.fileName()=="gtkrc") { | 85 | if(it.fileName()=="gtkrc") { | ||
76 | // qDebug() << "\tgtkrc file found at : " << it.filePath(); | 86 | // qDebug() << "\tgtkrc file found at : " << it.filePath(); | ||
77 | return it.filePath(); | 87 | return it.filePath(); | ||
78 | } | 88 | } | ||
79 | } | 89 | } | ||
80 | } | 90 | } | ||
81 | 91 | | |||
82 | return QString(); | 92 | return QString(); | ||
83 | } | 93 | } | ||
84 | 94 | | |||
85 | bool AppearanceGTK2::saveSettingsPrivate(const QString& gtkrcFile) const | 95 | bool AppearanceGTK2::saveSettings(const QString& gtkrcFile) const | ||
86 | { | 96 | { | ||
87 | QFile gtkrc(gtkrcFile); | 97 | QFile gtkrc(gtkrcFile); | ||
88 | gtkrc.remove(); | 98 | gtkrc.remove(); | ||
89 | 99 | | |||
90 | if(!gtkrc.open(QIODevice::WriteOnly | QIODevice::Text)) { | 100 | if(!gtkrc.open(QIODevice::WriteOnly | QIODevice::Text)) { | ||
91 | qWarning() << "There was unable to write the file .gtkrc-2.0"; | 101 | qWarning() << "There was unable to write the file .gtkrc-2.0"; | ||
92 | return false; | 102 | return false; | ||
93 | } | 103 | } | ||
94 | 104 | | |||
95 | QTextStream flow(>krc); | 105 | QTextStream flow(>krc); | ||
96 | 106 | | |||
97 | flow << "# File created by KDE Gtk Config" << "\n" | 107 | flow << "# File created by KDE Gtk Config" << "\n" | ||
98 | << "# Configs for GTK2 programs \n\n"; | 108 | << "# Configs for GTK2 programs \n\n"; | ||
99 | 109 | | |||
100 | QString themeGtkrcFile=themesGtkrcFile(getTheme()); | 110 | QString themeGtkrcFile=themesGtkrcFile(getTheme()); | ||
101 | 111 | | |||
102 | //TODO: is this really needed? | 112 | //TODO: is this really needed? | ||
103 | if(!themeGtkrcFile.isEmpty()) | 113 | if(!themeGtkrcFile.isEmpty()) | ||
104 | flow << "include \"" << themeGtkrcFile << "\"\n"; //We include the theme's gtkrc file | 114 | flow << "include \"" << themeGtkrcFile << "\"\n"; //We include the theme's gtkrc file | ||
105 | 115 | | |||
106 | if(QFile::exists("/etc/gtk-2.0/gtkrc")) | 116 | if(QFile::exists("/etc/gtk-2.0/gtkrc")) | ||
107 | flow << "include \"/etc/gtk-2.0/gtkrc\"\n"; //We include the /etc's config file | 117 | flow << "include \"/etc/gtk-2.0/gtkrc\"\n"; //We include the /etc's config file | ||
108 | 118 | | |||
109 | int nameEnd = m_settings["font"].lastIndexOf(QRegExp(" ([0-9]+|bold|italic)")); | 119 | int nameEnd = m_settings["font"].lastIndexOf(QRegExp(" ([0-9]+|bold|italic)")); | ||
110 | const auto fontFamily = m_settings["font"].leftRef(nameEnd); | 120 | QString fontFamily = m_settings["font"].left(nameEnd); | ||
111 | 121 | | |||
112 | //TODO: is this really needed? | 122 | //TODO: is this really needed? | ||
113 | flow << "style \"user-font\" \n" | 123 | flow << "style \"user-font\" \n" | ||
114 | << "{\n" | 124 | << "{\n" | ||
115 | << "\tfont_name=\""<< fontFamily << "\"\n" | 125 | << "\tfont_name=\""<< fontFamily << "\"\n" | ||
116 | << "}\n"; | 126 | << "}\n"; | ||
117 | 127 | | |||
118 | flow << "widget_class \"*\" style \"user-font\"\n"; | 128 | flow << "widget_class \"*\" style \"user-font\"\n"; | ||
Show All 20 Lines | |||||
139 | // qDebug() << "Symbolic link created for .gtkrc-2.0-kde4 :D"; | 149 | // qDebug() << "Symbolic link created for .gtkrc-2.0-kde4 :D"; | ||
140 | 150 | | |||
141 | if(gtkrcFile==defaultConfigFile()) | 151 | if(gtkrcFile==defaultConfigFile()) | ||
142 | QProcess::startDetached(QStandardPaths::findExecutable("reload_gtk_apps", {CMAKE_INSTALL_FULL_LIBEXECDIR})); | 152 | QProcess::startDetached(QStandardPaths::findExecutable("reload_gtk_apps", {CMAKE_INSTALL_FULL_LIBEXECDIR})); | ||
143 | 153 | | |||
144 | return true; | 154 | return true; | ||
145 | } | 155 | } | ||
146 | 156 | | |||
147 | void AppearanceGTK2::reset() | | |||
148 | { | | |||
149 | m_settings = QMap<QString, QString> { | | |||
150 | {"toolbar_style", "GTK_TOOLBAR_ICONS"}, | | |||
151 | {"show_icons_buttons", "0"}, | | |||
152 | {"show_icons_menus", "0"}, | | |||
153 | {"primary_button_warps_slider", "false"} | | |||
154 | }; | | |||
155 | } | | |||
156 | | ||||
157 | QString AppearanceGTK2::defaultConfigFile() const | 157 | QString AppearanceGTK2::defaultConfigFile() const | ||
158 | { | 158 | { | ||
159 | return QDir::homePath()+"/.gtkrc-2.0"; | 159 | return QDir::homePath()+"/.gtkrc-2.0"; | ||
160 | } | 160 | } | ||
161 | 161 | | |||
162 | QStringList AppearanceGTK2::installedThemes() const | 162 | QStringList AppearanceGTK2::installedThemes() const | ||
163 | { | 163 | { | ||
164 | QFileInfoList availableThemes; | 164 | QFileInfoList availableThemes; | ||
Show All 13 Lines | 176 | for(QFileInfoList::const_iterator it=availableThemes.constBegin(); it!=availableThemes.constEnd(); ++it) { | |||
178 | 178 | | |||
179 | //If it doesn't exist, we don't want it on the list | 179 | //If it doesn't exist, we don't want it on the list | ||
180 | if(hasGtkrc) | 180 | if(hasGtkrc) | ||
181 | paths += it->filePath(); | 181 | paths += it->filePath(); | ||
182 | } | 182 | } | ||
183 | 183 | | |||
184 | return paths; | 184 | return paths; | ||
185 | } | 185 | } | ||
186 | | ||||
187 | bool AppearanceGTK2::loadSettings() | | |||
188 | { | | |||
189 | reset(); | | |||
190 | | ||||
191 | return loadSettingsPrivate("/etc/gtk-2.0/gtkrc") && loadSettingsPrivate(defaultConfigFile()); | | |||
192 | } | | |||
193 | | ||||
194 | bool AppearanceGTK2::saveSettings() const | | |||
195 | { | | |||
196 | return saveSettings(defaultConfigFile()); | | |||
197 | } | | |||
198 | | ||||
199 | bool AppearanceGTK2::loadSettings(const QString& gtkrcFile) | | |||
200 | { | | |||
201 | reset(); | | |||
202 | return loadSettingsPrivate(gtkrcFile); | | |||
203 | } | | |||
204 | | ||||
205 | bool AppearanceGTK2::saveSettings(const QString& gtkrcFile) const | | |||
206 | { | | |||
207 | return saveSettingsPrivate(gtkrcFile); | | |||
208 | } | |