Changeset View
Changeset View
Standalone View
Standalone View
kded-module/configeditor.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2019 Mikhail Zolotukhin <zomial@protonmail.com> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of | ||||
7 | * the License or (at your option) version 3 or any later version | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
11 | * | ||||
12 | * This program is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | * GNU General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU General Public License | ||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include <QDebug> | ||||
22 | #include <QDir> | ||||
23 | #include <QRegularExpression> | ||||
24 | #include <QStandardPaths> | ||||
25 | #include <QProcess> | ||||
26 | | ||||
27 | #include <KSharedConfig> | ||||
28 | #include <KConfigGroup> | ||||
29 | | ||||
30 | #include <string> | ||||
31 | #include <csignal> | ||||
32 | #include <cstdio> | ||||
33 | | ||||
34 | #undef signals | ||||
broulik: Alternatively you could compile the project with `QT_NO_KEYWORDS` | |||||
35 | #include <gio/gio.h> | ||||
36 | #include <gtk/gtk.h> | ||||
37 | #define signals Q_SIGNALS | ||||
38 | | ||||
39 | #include "configeditor.h" | ||||
40 | | ||||
41 | void ConfigEditor::setGtk3ConfigValueDconf(const QString ¶mName, const QString ¶mValue) | ||||
42 | { | ||||
43 | gtk_init(nullptr, nullptr); | ||||
44 | g_autoptr(GSettings) gsettings = g_settings_new("org.gnome.desktop.interface"); | ||||
45 | g_settings_set_string(gsettings, paramName.toUtf8().constData(), paramValue.toUtf8().constData()); | ||||
46 | } | ||||
47 | | ||||
48 | void ConfigEditor::setGtk3ConfigValueSettingsIni(const QString ¶mName, const QString ¶mValue) | ||||
49 | { | ||||
50 | using qsp = QStandardPaths; | ||||
Better wrap the const char* in a QStringLiteral. Also, but more of a nitpick this time: style wise I'd favor using = for those initialization (and others in the file) than parenthesis. It gets too close the most vexing parse territory, I'd rather not potentially expose a future developer touching that file to it. ervin: Better wrap the const char* in a QStringLiteral.
Also, but more of a nitpick this time: style… | |||||
Where generally should I use equals sign (=), where parenthesis (()) and where braces ({}) for initialization? I didn't find any note about that in coding style. gikari: Where generally should I use equals sign (`=`), where parenthesis (`()`) and where braces… | |||||
ngraham: I believe in general use equals, but you can use braces for lists. | |||||
51 | QString configLocation(qsp::writableLocation(qsp::GenericConfigLocation)); | ||||
52 | QString gtk3ConfigPath(configLocation + "/gtk-3.0/settings.ini"); | ||||
53 | | ||||
54 | KSharedConfig::Ptr gtk3Config = KSharedConfig::openConfig(gtk3ConfigPath, KConfig::NoGlobals); | ||||
55 | KConfigGroup group(gtk3Config, QStringLiteral("Settings")); | ||||
56 | | ||||
57 | group.writeEntry(paramName, paramValue); | ||||
58 | group.sync(); | ||||
59 | } | ||||
60 | | ||||
61 | void ConfigEditor::setGtk3ConfigValueXSettingsd(const QString ¶mName, const QString ¶mValue) | ||||
62 | { | ||||
63 | using qsp = QStandardPaths; | ||||
64 | QString configLocation(qsp::writableLocation(qsp::GenericConfigLocation)); | ||||
65 | | ||||
66 | QDir xsettingsdPath(configLocation + "/xsettingsd"); | ||||
67 | if (!xsettingsdPath.exists()) { | ||||
68 | xsettingsdPath.mkpath("."); | ||||
69 | } | ||||
70 | | ||||
71 | QString xSettingsdConfigPath(xsettingsdPath.path() + "/xsettingsd.conf"); | ||||
72 | | ||||
73 | QFile xSettingsdConfig(xSettingsdConfigPath); | ||||
74 | QString xSettingsdConfigContents(readFileContents(xSettingsdConfig)); | ||||
75 | replaceValueInXSettingsdContents(xSettingsdConfigContents, paramName, paramValue); | ||||
76 | xSettingsdConfig.remove(); | ||||
77 | xSettingsdConfig.open(QIODevice::WriteOnly | QIODevice::Text); | ||||
78 | xSettingsdConfig.write(xSettingsdConfigContents.toUtf8()); | ||||
79 | reloadXSettingsd(); | ||||
80 | } | ||||
81 | | ||||
82 | void ConfigEditor::setGtk2ConfigValue(const QString ¶mName, const QString ¶mValue) | ||||
ervin: QStringLiteral please. | |||||
83 | { | ||||
84 | QString gtkrcPath(QDir::homePath() + "/.gtkrc-2.0"); | ||||
85 | QFile gtkrc(gtkrcPath); | ||||
86 | QString gtkrcContents(readFileContents(gtkrc)); | ||||
87 | replaceValueInGtkrcContents(gtkrcContents, paramName, paramValue); | ||||
88 | gtkrc.remove(); | ||||
89 | gtkrc.open(QIODevice::WriteOnly | QIODevice::Text); | ||||
90 | gtkrc.write(gtkrcContents.toUtf8()); | ||||
91 | reloadGtk2Apps(); | ||||
92 | } | ||||
93 | | ||||
94 | QString ConfigEditor::readFileContents(QFile &file) | ||||
95 | { | ||||
96 | if (file.open(QIODevice::ReadWrite | QIODevice::Text)) { | ||||
97 | return file.readAll(); | ||||
ervin: QString() or {} instead of "" | |||||
98 | } else { | ||||
99 | return ""; | ||||
100 | } | ||||
101 | } | ||||
102 | | ||||
103 | void ConfigEditor::replaceValueInGtkrcContents(QString >krcContents, const QString ¶mName, const QString ¶mValue) | ||||
QStringLiteral please There are more of those in the file, I'll stop pointing them out. ;-) ervin: QStringLiteral please
There are more of those in the file, I'll stop pointing them out. ;-) | |||||
104 | { | ||||
105 | QRegularExpression regExp(paramName + "=[^\n]*($|\n)"); | ||||
broulik: make `static` so it doesn't have to recreate it every time? | |||||
106 | | ||||
107 | static const QStringList nonStringProperties{ | ||||
108 | QStringLiteral("gtk-toolbar-style"), | ||||
109 | QStringLiteral("gtk-menu-images"), | ||||
110 | QStringLiteral("gtk-button-images"), | ||||
111 | QStringLiteral("gtk-primary-button-warps-slider"), | ||||
112 | }; | ||||
113 | | ||||
114 | QString newConfigString; | ||||
115 | if (nonStringProperties.contains(paramName)) { | ||||
116 | newConfigString = paramName + "=" + paramValue + "\n"; | ||||
117 | } else { | ||||
118 | newConfigString = paramName + "=\"" + paramValue + "\"\n"; | ||||
119 | } | ||||
120 | | ||||
121 | if (gtkrcContents.contains(regExp)) { | ||||
122 | gtkrcContents.replace(regExp, newConfigString); | ||||
123 | } else { | ||||
124 | gtkrcContents = newConfigString + "\n" + gtkrcContents; | ||||
125 | } | ||||
126 | } | ||||
127 | | ||||
128 | void ConfigEditor::replaceValueInXSettingsdContents(QString &xSettingsdContents, const QString ¶mName, const QString ¶mValue) | ||||
129 | { | ||||
130 | QRegularExpression regExp(paramName + " [^\n]*($|\n)"); | ||||
131 | | ||||
132 | static const QStringList nonStringProperties{ | ||||
133 | QStringLiteral("Gtk/ButtonImages"), | ||||
134 | QStringLiteral("Gtk/MenuImages"), | ||||
135 | QStringLiteral("Gtk/ToolbarStyle"), | ||||
136 | }; | ||||
137 | | ||||
138 | QString newConfigString; | ||||
139 | if (nonStringProperties.contains(paramName)) { | ||||
140 | newConfigString = paramName + " " + paramValue + "\n"; | ||||
141 | } else { | ||||
142 | newConfigString = paramName + " \"" + paramValue + "\"\n"; | ||||
143 | } | ||||
144 | | ||||
145 | if (xSettingsdContents.contains(regExp)) { | ||||
146 | xSettingsdContents.replace(regExp, newConfigString); | ||||
147 | } else { | ||||
148 | xSettingsdContents = newConfigString + "\n" + xSettingsdContents; | ||||
149 | } | ||||
150 | } | ||||
151 | | ||||
152 | void ConfigEditor::reloadGtk2Apps() | ||||
153 | { | ||||
154 | QProcess::startDetached(QStandardPaths::findExecutable(QStringLiteral("reload_gtk_apps"))); | ||||
155 | } | ||||
156 | | ||||
157 | void ConfigEditor::reloadXSettingsd() | ||||
158 | { | ||||
159 | pid_t pidOfXSettingsd(getPidOfXSettingsd()); | ||||
160 | if (pidOfXSettingsd == 0) { | ||||
161 | QProcess::startDetached(QStandardPaths::findExecutable(QStringLiteral("xsettingsd"))); | ||||
162 | } else { | ||||
163 | kill(pidOfXSettingsd, SIGHUP); | ||||
164 | } | ||||
165 | } | ||||
166 | | ||||
167 | pid_t ConfigEditor::getPidOfXSettingsd() | ||||
168 | { | ||||
169 | char line[512]; | ||||
170 | FILE *cmd(popen("pidof -s xsettingsd", "r")); | ||||
broulik: I would prefer using more higher level `QProcess` here | |||||
171 | fgets(line, 512, cmd); | ||||
172 | pclose(cmd); | ||||
173 | return std::atoi(line); | ||||
174 | } | ||||
nicolasfella: QProcess pidof; |
Alternatively you could compile the project with QT_NO_KEYWORDS