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 | #include <gio/gio.h> | ||||
35 | #include <gtk/gtk.h> | ||||
broulik: Alternatively you could compile the project with `QT_NO_KEYWORDS` | |||||
36 | | ||||
37 | #include "configeditor.h" | ||||
38 | | ||||
39 | void ConfigEditor::setGtk3ConfigValueDconf(const QString ¶mName, const QString ¶mValue) const | ||||
40 | { | ||||
41 | gtk_init(nullptr, nullptr); | ||||
42 | g_autoptr(GSettings) gsettings = g_settings_new("org.gnome.desktop.interface"); | ||||
43 | g_settings_set_string(gsettings, paramName.toUtf8().constData(), paramValue.toUtf8().constData()); | ||||
44 | } | ||||
45 | | ||||
46 | void ConfigEditor::setGtk3ConfigValueSettingsIni(const QString ¶mName, const QString ¶mValue) const | ||||
47 | { | ||||
48 | QString configLocation = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation); | ||||
49 | QString gtk3ConfigPath = configLocation + QStringLiteral("/gtk-3.0/settings.ini"); | ||||
50 | | ||||
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 | KSharedConfig::Ptr gtk3Config = KSharedConfig::openConfig(gtk3ConfigPath, KConfig::NoGlobals); | ||||
52 | KConfigGroup group = gtk3Config->group(QStringLiteral("Settings")); | ||||
53 | | ||||
54 | group.writeEntry(paramName, paramValue); | ||||
55 | group.sync(); | ||||
56 | } | ||||
57 | | ||||
58 | void ConfigEditor::setGtk3ConfigValueXSettingsd(const QString ¶mName, const QString ¶mValue) const | ||||
59 | { | ||||
60 | using qsp = QStandardPaths; | ||||
61 | QString configLocation = qsp::writableLocation(qsp::GenericConfigLocation); | ||||
62 | | ||||
63 | QDir xsettingsdPath = configLocation + QStringLiteral("/xsettingsd"); | ||||
64 | if (!xsettingsdPath.exists()) { | ||||
65 | xsettingsdPath.mkpath(QStringLiteral(".")); | ||||
66 | } | ||||
67 | | ||||
68 | QString xSettingsdConfigPath = xsettingsdPath.path() + QStringLiteral("/xsettingsd.conf"); | ||||
69 | | ||||
70 | QFile xSettingsdConfig(xSettingsdConfigPath); | ||||
71 | QString xSettingsdConfigContents = readFileContents(xSettingsdConfig); | ||||
72 | replaceValueInXSettingsdContents(xSettingsdConfigContents, paramName, paramValue); | ||||
73 | xSettingsdConfig.remove(); | ||||
74 | xSettingsdConfig.open(QIODevice::WriteOnly | QIODevice::Text); | ||||
75 | xSettingsdConfig.write(xSettingsdConfigContents.toUtf8()); | ||||
76 | reloadXSettingsd(); | ||||
77 | } | ||||
78 | | ||||
79 | void ConfigEditor::setGtk2ConfigValue(const QString ¶mName, const QString ¶mValue) const | ||||
80 | { | ||||
81 | QString gtkrcPath = QDir::homePath() + QStringLiteral("/.gtkrc-2.0"); | ||||
82 | QFile gtkrc(gtkrcPath); | ||||
ervin: QStringLiteral please. | |||||
83 | QString gtkrcContents = readFileContents(gtkrc); | ||||
84 | replaceValueInGtkrcContents(gtkrcContents, paramName, paramValue); | ||||
85 | gtkrc.remove(); | ||||
86 | gtkrc.open(QIODevice::WriteOnly | QIODevice::Text); | ||||
87 | gtkrc.write(gtkrcContents.toUtf8()); | ||||
88 | reloadGtk2Apps(); | ||||
89 | } | ||||
90 | | ||||
91 | QString ConfigEditor::readFileContents(QFile &file) const | ||||
92 | { | ||||
93 | if (file.open(QIODevice::ReadWrite | QIODevice::Text)) { | ||||
94 | return file.readAll(); | ||||
95 | } else { | ||||
96 | return QString(); | ||||
97 | } | ||||
ervin: QString() or {} instead of "" | |||||
98 | } | ||||
99 | | ||||
100 | void ConfigEditor::replaceValueInGtkrcContents(QString >krcContents, const QString ¶mName, const QString ¶mValue) const | ||||
101 | { | ||||
102 | const QRegularExpression regExp(paramName + QStringLiteral("=[^\n]*($|\n)")); | ||||
103 | | ||||
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 | static const QStringList nonStringProperties{ | ||||
105 | QStringLiteral("gtk-toolbar-style"), | ||||
106 | QStringLiteral("gtk-menu-images"), | ||||
broulik: make `static` so it doesn't have to recreate it every time? | |||||
107 | QStringLiteral("gtk-button-images"), | ||||
108 | QStringLiteral("gtk-primary-button-warps-slider"), | ||||
109 | }; | ||||
110 | | ||||
111 | QString newConfigString; | ||||
112 | if (nonStringProperties.contains(paramName)) { | ||||
113 | newConfigString = paramName + QStringLiteral("=") + paramValue + QStringLiteral("\n"); | ||||
114 | } else { | ||||
115 | newConfigString = paramName + QStringLiteral("=\"") + paramValue + QStringLiteral("\"\n"); | ||||
116 | } | ||||
117 | | ||||
118 | if (gtkrcContents.contains(regExp)) { | ||||
119 | gtkrcContents.replace(regExp, newConfigString); | ||||
120 | } else { | ||||
121 | gtkrcContents = newConfigString + QStringLiteral("\n") + gtkrcContents; | ||||
122 | } | ||||
123 | } | ||||
124 | | ||||
125 | void ConfigEditor::replaceValueInXSettingsdContents(QString &xSettingsdContents, const QString ¶mName, const QString ¶mValue) const | ||||
126 | { | ||||
127 | const QRegularExpression regExp(paramName + QStringLiteral(" [^\n]*($|\n)")); | ||||
128 | | ||||
129 | static const QStringList nonStringProperties{ | ||||
130 | QStringLiteral("Gtk/ButtonImages"), | ||||
131 | QStringLiteral("Gtk/MenuImages"), | ||||
132 | QStringLiteral("Gtk/ToolbarStyle"), | ||||
133 | }; | ||||
134 | | ||||
135 | QString newConfigString; | ||||
136 | if (nonStringProperties.contains(paramName)) { | ||||
137 | newConfigString = paramName + QStringLiteral(" ") + paramValue + QStringLiteral("\n"); | ||||
138 | } else { | ||||
139 | newConfigString = paramName + QStringLiteral(" \"") + paramValue + QStringLiteral("\"\n"); | ||||
140 | } | ||||
141 | | ||||
142 | if (xSettingsdContents.contains(regExp)) { | ||||
143 | xSettingsdContents.replace(regExp, newConfigString); | ||||
144 | } else { | ||||
145 | xSettingsdContents = newConfigString + QStringLiteral("\n") + xSettingsdContents; | ||||
146 | } | ||||
147 | } | ||||
148 | | ||||
149 | void ConfigEditor::reloadGtk2Apps() const | ||||
150 | { | ||||
151 | QProcess::startDetached(QStandardPaths::findExecutable(QStringLiteral("reload_gtk_apps"))); | ||||
152 | } | ||||
153 | | ||||
154 | void ConfigEditor::reloadXSettingsd() const | ||||
155 | { | ||||
156 | pid_t xSettingsdPid = pidOfXSettingsd(); | ||||
157 | if (xSettingsdPid == 0) { | ||||
158 | QProcess::startDetached(QStandardPaths::findExecutable(QStringLiteral("xsettingsd"))); | ||||
159 | } else { | ||||
160 | kill(xSettingsdPid, SIGHUP); | ||||
161 | } | ||||
162 | } | ||||
163 | | ||||
164 | pid_t ConfigEditor::pidOfXSettingsd() const | ||||
165 | { | ||||
166 | QProcess pidof {}; | ||||
167 | pidof.start(QStringLiteral("pidof"), QStringList() << QStringLiteral("-s") << QStringLiteral("xsettingsd")); | ||||
168 | pidof.waitForFinished(); | ||||
169 | QString xsettingsdPid = QString(pidof.readAllStandardOutput()).remove('\n'); | ||||
170 | return xsettingsdPid.toInt(); | ||||
171 | } | ||||
broulik: I would prefer using more higher level `QProcess` here | |||||
nicolasfella: QProcess pidof; |
Alternatively you could compile the project with QT_NO_KEYWORDS