File indexing completed on 2024-05-12 05:29:27

0001 // SPDX-FileCopyrightText: 2019, 2022 Mikhail Zolotukhin <zomial@protonmail.com>
0002 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0003 
0004 #include "gtk2.h"
0005 
0006 #include <QDir>
0007 #include <QRegularExpression>
0008 
0009 #include "config_editor/utils.h"
0010 
0011 namespace Gtk2ConfigEditor
0012 {
0013 namespace
0014 {
0015 
0016 void replaceValueInGtkrcContents(QString &gtkrcContents, const QString &paramName, const QVariant &paramValue)
0017 {
0018     const QRegularExpression regExp(paramName + QStringLiteral("=[^\n]*($|\n)"));
0019 
0020     QString newConfigString;
0021     if (paramValue.type() == QVariant::Type::String) {
0022         newConfigString = QStringLiteral("%1=\"%2\"\n").arg(paramName, paramValue.toString());
0023     } else if (paramValue.type() == QVariant::Type::Bool) {
0024         // GTK2 does not support 'true' and 'false' as values
0025         newConfigString = QStringLiteral("%1=%2\n").arg(paramName, QString::number(paramValue.toInt()));
0026     } else {
0027         newConfigString = QStringLiteral("%1=%2\n").arg(paramName, paramValue.toString());
0028     }
0029 
0030     if (gtkrcContents.contains(regExp)) {
0031         gtkrcContents.replace(regExp, newConfigString);
0032     } else {
0033         gtkrcContents = newConfigString + gtkrcContents;
0034     }
0035 }
0036 }
0037 
0038 void setValue(const QString &paramName, const QVariant &paramValue)
0039 {
0040     QString gtkrcPath = qEnvironmentVariable("GTK2_RC_FILES", QDir::homePath() + QStringLiteral("/.gtkrc-2.0"));
0041     if (gtkrcPath.contains(QStringLiteral(":/"))) { // I.e. env variable contains multiple paths
0042         gtkrcPath = QDir::homePath() + QStringLiteral("/.gtkrc-2.0");
0043     }
0044     QFile gtkrc(gtkrcPath);
0045     QString gtkrcContents = Utils::readFileContents(gtkrc);
0046     replaceValueInGtkrcContents(gtkrcContents, paramName, paramValue);
0047     gtkrc.remove();
0048     gtkrc.open(QIODevice::WriteOnly | QIODevice::Text);
0049     gtkrc.write(gtkrcContents.toUtf8());
0050 }
0051 
0052 void removeLegacyStrings()
0053 {
0054     QString gtkrcPath = QDir::homePath() + QStringLiteral("/.gtkrc-2.0");
0055     QFile gtkrc(gtkrcPath);
0056     QString gtkrcContents = Utils::readFileContents(gtkrc);
0057     if (gtkrcContents.isNull()) {
0058         return;
0059     }
0060 
0061     // Remove "include" lines
0062     // Example:
0063     // include "/usr/share/themes/Adwaita-dark/gtk-2.0/gtkrc"
0064     static const QRegularExpression includeRegExp(QStringLiteral("include .*\n"));
0065     gtkrcContents.remove(includeRegExp);
0066 
0067     // Remove redundant font config lines
0068     // Example:
0069     // style "user-font"
0070     // {
0071     //     font_name="Noto Sans Regular"
0072     // }
0073     // widget_class "*" style "user-font"
0074     static const QRegularExpression userFontStyleRegexp(QStringLiteral("style(.|\n)*{(.|\n)*}\nwidget_class.*\"user-font\""));
0075     gtkrcContents.remove(userFontStyleRegexp);
0076 
0077     gtkrc.remove();
0078     gtkrc.open(QIODevice::WriteOnly | QIODevice::Text);
0079     gtkrc.write(gtkrcContents.toUtf8());
0080 }
0081 
0082 }