Changeset View
Changeset View
Standalone View
Standalone View
gtkconfig/kded/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 | ||||
35 | #include <gio/gio.h> | ||||
36 | #include <gtk/gtk.h> | ||||
37 | #define signals Q_SIGNALS | ||||
38 | | ||||
39 | #include "configeditor.h" | ||||
40 | | ||||
41 | void ConfigEditor::setGtk3ConfigValueWayland(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::setGtk3ConfigValueX11SettingsIni(const QString ¶mName, const QString ¶mValue) | ||||
49 | { | ||||
50 | using qsp = QStandardPaths; | ||||
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, "Settings"}; | ||||
56 | | ||||
57 | group.writeEntry(paramName, paramValue); | ||||
58 | group.sync(); | ||||
59 | } | ||||
60 | | ||||
61 | void ConfigEditor::setGtk3ConfigValueX11XSettingsd(const QString ¶mName, const QString ¶mValue) | ||||
62 | { | ||||
63 | QString xSettingsdConfigPath {QDir::homePath() + "/.config/xsettingsd/xsettingsd.conf"}; | ||||
64 | QFile xSettingsdConfig {xSettingsdConfigPath}; | ||||
65 | QString xSettingsdConfigContents {readFileContents(xSettingsdConfig)}; | ||||
66 | replaceValueInXSettingsdContents(xSettingsdConfigContents, paramName, paramValue); | ||||
67 | xSettingsdConfig.remove(); | ||||
68 | xSettingsdConfig.open(QIODevice::WriteOnly | QIODevice::Text); | ||||
69 | xSettingsdConfig.write(xSettingsdConfigContents.toUtf8()); | ||||
70 | reloadXSettingsd(); | ||||
71 | } | ||||
72 | | ||||
73 | void ConfigEditor::setGtk2ConfigValue(const QString ¶mName, const QString ¶mValue) | ||||
74 | { | ||||
75 | QString gtkrcPath {QDir::homePath() + "/.gtkrc-2.0"}; | ||||
76 | QFile gtkrc {gtkrcPath}; | ||||
77 | QString gtkrcContents {readFileContents(gtkrc)}; | ||||
78 | replaceValueInGtkrcContents(gtkrcContents, paramName, paramValue); | ||||
79 | gtkrc.remove(); | ||||
80 | gtkrc.open(QIODevice::WriteOnly | QIODevice::Text); | ||||
81 | gtkrc.write(gtkrcContents.toUtf8()); | ||||
82 | reloadGtk2Apps(); | ||||
83 | } | ||||
84 | | ||||
85 | QString ConfigEditor::readFileContents(QFile &file) | ||||
86 | { | ||||
87 | if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { | ||||
88 | return file.readAll(); | ||||
89 | } else { | ||||
90 | qWarning() << "Unable to read the " << file.fileName() << " file"; | ||||
91 | return ""; | ||||
92 | } | ||||
93 | } | ||||
94 | | ||||
95 | | ||||
96 | void ConfigEditor::replaceValueInGtkrcContents(QString >krcContents, const QString ¶mName, const QString ¶mValue) | ||||
97 | { | ||||
98 | QRegularExpression regExp {paramName + "=[^\n]*($|\n)"}; | ||||
99 | | ||||
100 | static const QStringList nonStringProperties{ | ||||
101 | "gtk-toolbar-style", | ||||
102 | "gtk-menu-images", | ||||
103 | "gtk-button-images", | ||||
104 | "gtk-primary-button-warps-slider", | ||||
105 | }; | ||||
106 | | ||||
107 | QString newConfigString; | ||||
108 | if (nonStringProperties.contains(paramName)) { | ||||
109 | newConfigString = paramName + "=" + paramValue + "\n"; | ||||
110 | } else { | ||||
111 | newConfigString = paramName + "=\"" + paramValue + "\"\n"; | ||||
112 | } | ||||
113 | | ||||
114 | if (gtkrcContents.contains(regExp)) { | ||||
115 | gtkrcContents.replace(regExp, newConfigString); | ||||
116 | } else { | ||||
117 | gtkrcContents = newConfigString + "\n" + gtkrcContents; | ||||
118 | } | ||||
119 | } | ||||
120 | | ||||
121 | void ConfigEditor::replaceValueInXSettingsdContents(QString &xSettingsdContents, const QString ¶mName, const QString ¶mValue) | ||||
122 | { | ||||
123 | QRegularExpression regExp {paramName + " [^\n]*($|\n)"}; | ||||
124 | QString newConfigString {paramName + " \"" + paramValue + "\"\n"}; | ||||
125 | | ||||
126 | if (xSettingsdContents.contains(regExp)) { | ||||
127 | xSettingsdContents.replace(regExp, newConfigString); | ||||
128 | } else { | ||||
129 | xSettingsdContents = newConfigString + "\n" + xSettingsdContents; | ||||
130 | } | ||||
131 | } | ||||
132 | | ||||
133 | void ConfigEditor::reloadGtk2Apps() | ||||
134 | { | ||||
135 | QProcess::startDetached(QStandardPaths::findExecutable("gtk2apps_reloader")); | ||||
136 | } | ||||
137 | | ||||
138 | void ConfigEditor::reloadXSettingsd() | ||||
139 | { | ||||
140 | pid_t pidOfXSettingsd = getPidOfXSettingsd(); | ||||
141 | qDebug() << "Pid of XSettings: " << pidOfXSettingsd; | ||||
142 | if (pidOfXSettingsd == 0) { | ||||
143 | qDebug() << "XSettingsd does not exist. Starting new instance..."; | ||||
144 | QProcess::startDetached(QStandardPaths::findExecutable("xsettingsd")); | ||||
145 | } else { | ||||
146 | qDebug() << "XSettingsd exists. Sending signal..."; | ||||
147 | kill(pidOfXSettingsd, SIGHUP); | ||||
148 | } | ||||
149 | } | ||||
150 | | ||||
151 | pid_t ConfigEditor::getPidOfXSettingsd() | ||||
152 | { | ||||
153 | char line[512]; | ||||
154 | FILE *cmd = popen("pidof -s xsettingsd", "r"); | ||||
155 | fgets(line, 512, cmd); | ||||
156 | pclose(cmd); | ||||
157 | return std::atoi(line); | ||||
158 | } |