Changeset View
Changeset View
Standalone View
Standalone View
src/appearancegtk2.cpp
Show All 23 Lines | |||||
24 | #include <QFile> | 24 | #include <QFile> | ||
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 | #include <QRegularExpression> | ||||
32 | 33 | | |||
33 | bool AppearanceGTK2::loadSettingsPrivate(const QString& path) | 34 | bool AppearanceGTK2::loadSettingsPrivate(const QString& path) | ||
34 | { | 35 | { | ||
35 | QFile configFile(path); | 36 | QFile configFile(path); | ||
36 | 37 | | |||
37 | if (!configFile.open(QIODevice::ReadOnly | QIODevice::Text)) | 38 | if (!configFile.open(QIODevice::ReadOnly | QIODevice::Text)) | ||
38 | return false; | 39 | return false; | ||
39 | 40 | | |||
Show All 39 Lines | 77 | // qDebug() << "\tgtkrc file found at : " << it.filePath(); | |||
79 | } | 80 | } | ||
80 | } | 81 | } | ||
81 | 82 | | |||
82 | return QString(); | 83 | return QString(); | ||
83 | } | 84 | } | ||
84 | 85 | | |||
85 | bool AppearanceGTK2::saveSettingsPrivate(const QString& gtkrcFile) const | 86 | bool AppearanceGTK2::saveSettingsPrivate(const QString& gtkrcFile) const | ||
86 | { | 87 | { | ||
87 | QFile gtkrc(gtkrcFile); | 88 | QFile gtkrc{gtkrcFile}; | ||
88 | gtkrc.remove(); | | |||
89 | | ||||
90 | if(!gtkrc.open(QIODevice::WriteOnly | QIODevice::Text)) { | | |||
91 | qWarning() << "There was unable to write the file .gtkrc-2.0"; | | |||
92 | return false; | | |||
93 | } | | |||
94 | | ||||
95 | QTextStream flow(>krc); | | |||
96 | 89 | | |||
97 | flow << "# File created by KDE Gtk Config" << "\n" | 90 | if (gtkrc.open(QIODevice::ReadOnly | QIODevice::Text)) { | ||
98 | << "# Configs for GTK2 programs \n\n"; | 91 | QString fileContents{gtkrc.readAll()}; | ||
99 | 92 | | |||
100 | QString themeGtkrcFile=themesGtkrcFile(getTheme()); | 93 | modifyGtkrcContents(fileContents); | ||
101 | 94 | | |||
102 | //TODO: is this really needed? | 95 | gtkrc.remove(); | ||
103 | if(!themeGtkrcFile.isEmpty()) | 96 | gtkrc.open(QIODevice::WriteOnly | QIODevice::Text); | ||
104 | flow << "include \"" << themeGtkrcFile << "\"\n"; //We include the theme's gtkrc file | 97 | gtkrc.write(fileContents.toUtf8()); | ||
105 | | ||||
106 | if(QFile::exists("/etc/gtk-2.0/gtkrc")) | | |||
107 | flow << "include \"/etc/gtk-2.0/gtkrc\"\n"; //We include the /etc's config file | | |||
108 | | ||||
109 | int nameEnd = m_settings["font"].lastIndexOf(QRegExp(" ([0-9]+|bold|italic)")); | | |||
110 | const auto fontFamily = m_settings["font"].leftRef(nameEnd); | | |||
111 | | ||||
112 | //TODO: is this really needed? | | |||
113 | flow << "style \"user-font\" \n" | | |||
114 | << "{\n" | | |||
115 | << "\tfont_name=\""<< fontFamily << "\"\n" | | |||
116 | << "}\n"; | | |||
117 | | ||||
118 | flow << "widget_class \"*\" style \"user-font\"\n"; | | |||
119 | flow << "gtk-font-name=\"" << m_settings["font"] << "\"\n"; | | |||
120 | flow << "gtk-theme-name=\"" << m_settings["theme"] << "\"\n"; | | |||
121 | flow << "gtk-icon-theme-name=\""<< m_settings["icon"] << "\"\n"; | | |||
122 | flow << "gtk-fallback-icon-theme=\"" << m_settings["icon_fallback"] << "\"\n"; | | |||
123 | flow << "gtk-cursor-theme-name=\"" << m_settings["cursor"] << "\"\n"; | | |||
124 | flow << "gtk-toolbar-style=" << m_settings["toolbar_style"] << "\n"; | | |||
125 | flow << "gtk-menu-images=" << m_settings["show_icons_menus"] << "\n"; | | |||
126 | flow << "gtk-button-images=" << m_settings["show_icons_buttons"] << "\n"; | | |||
127 | flow << "gtk-primary-button-warps-slider=" << m_settings["primary_button_warps_slider"] << "\n"; | | |||
128 | | ||||
129 | //we're done with the ~/.gtk-2.0 file | | |||
130 | gtkrc.close(); | | |||
131 | | ||||
132 | //TODO: do we really need the linked file? | | |||
133 | if(QFile::remove(gtkrcFile+"-kde4")) | | |||
134 | qDebug() << "ready to create the symbolic link"; | | |||
135 | | ||||
136 | if( !QFile::link(gtkrcFile, gtkrcFile+"-kde4") ) | | |||
137 | qWarning() << "Couldn't create the symboling link to .gtkrc-2.0-kde4 :("; | | |||
138 | // else | | |||
139 | // qDebug() << "Symbolic link created for .gtkrc-2.0-kde4 :D"; | | |||
140 | 98 | | |||
141 | if(gtkrcFile==defaultConfigFile()) | 99 | if (QFileInfo(gtkrc).filePath() == defaultConfigFile()) { | ||
142 | QProcess::startDetached(QStandardPaths::findExecutable("reload_gtk_apps", {CMAKE_INSTALL_FULL_LIBEXECDIR})); | 100 | QProcess::startDetached(QStandardPaths::findExecutable("reload_gtk_apps", {CMAKE_INSTALL_FULL_LIBEXECDIR})); | ||
101 | } | ||||
143 | 102 | | |||
144 | return true; | 103 | return true; | ||
104 | } else { | ||||
105 | qWarning() << "There was unable to write the .gtkrc-2.0 file"; | ||||
106 | return false; | ||||
107 | } | ||||
108 | } | ||||
109 | | ||||
110 | void AppearanceGTK2::modifyGtkrcContents(QString& fileContents) const | ||||
111 | { | ||||
112 | modifyGtkrcProperty("gtk-font-name", m_settings["font"], fileContents); | ||||
113 | modifyGtkrcProperty("gtk-theme-name", m_settings["theme"], fileContents); | ||||
114 | modifyGtkrcProperty("gtk-icon-theme-name", m_settings["icon"], fileContents); | ||||
115 | modifyGtkrcProperty("gtk-fallback-icon-theme", m_settings["icon_fallback"], fileContents); | ||||
116 | modifyGtkrcProperty("gtk-cursor-theme-name", m_settings["cursor"], fileContents); | ||||
117 | modifyGtkrcProperty("gtk-toolbar-style", m_settings["toolbar_style"], fileContents); | ||||
118 | modifyGtkrcProperty("gtk-menu-images", m_settings["show_icons_menus"], fileContents); | ||||
119 | modifyGtkrcProperty("gtk-button-images", m_settings["show_icons_buttons"], fileContents); | ||||
120 | modifyGtkrcProperty("gtk-primary-button-warps-slider", m_settings["primary_button_warps_slider"], fileContents); | ||||
121 | } | ||||
122 | | ||||
123 | void AppearanceGTK2::modifyGtkrcProperty(const QString& propertyName, const QString& newValue, QString& fileContents) const | ||||
124 | { | ||||
125 | const QRegularExpression regExp{propertyName + "=[^\n]*($|\n)"}; | ||||
apol: const? | |||||
126 | | ||||
127 | static const QStringList nonStringProperties{ | ||||
128 | "gtk-toolbar-style", | ||||
129 | "gtk-menu-images", | ||||
130 | "gtk-button-images", | ||||
131 | "gtk-primary-button-warps-slider", | ||||
132 | }; | ||||
133 | | ||||
134 | QString newConfigString; | ||||
135 | if (nonStringProperties.contains(propertyName)) { | ||||
136 | newConfigString = propertyName + "=" + newValue + "\n"; | ||||
apol: This constructor isn't necessary. | |||||
137 | } else { | ||||
138 | newConfigString = propertyName + "=\"" + newValue + "\"\n"; | ||||
apol: This constructor isn't necessary | |||||
139 | } | ||||
140 | | ||||
141 | if (fileContents.contains(regExp)) { | ||||
142 | fileContents.replace(regExp, newConfigString); | ||||
143 | } else { | ||||
apol: This constructor isn't necessary. | |||||
144 | fileContents = newConfigString + "\n" + fileContents; | ||||
145 | } | ||||
145 | } | 146 | } | ||
146 | 147 | | |||
147 | void AppearanceGTK2::reset() | 148 | void AppearanceGTK2::reset() | ||
148 | { | 149 | { | ||
149 | m_settings = QMap<QString, QString> { | 150 | m_settings = QMap<QString, QString> { | ||
150 | {"toolbar_style", "GTK_TOOLBAR_ICONS"}, | 151 | {"toolbar_style", "GTK_TOOLBAR_ICONS"}, | ||
151 | {"show_icons_buttons", "0"}, | 152 | {"show_icons_buttons", "0"}, | ||
152 | {"show_icons_menus", "0"}, | 153 | {"show_icons_menus", "0"}, | ||
▲ Show 20 Lines • Show All 58 Lines • Show Last 20 Lines |
const?