File indexing completed on 2022-11-29 15:04:27

0001 /*
0002     SPDX-FileCopyrightText: 2018 Michail Vourlakos <mvourlakos@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 
0005 */
0006 
0007 #include "schemecolors.h"
0008 
0009 // local
0010 #include <config-latte.h>
0011 #include "../layouts/importer.h"
0012 #include "../tools/commontools.h"
0013 
0014 // Qt
0015 #include <QDebug>
0016 #include <QDir>
0017 #include <QFileInfo>
0018 #include <QLatin1String>
0019 
0020 // KDE
0021 #include <KConfigGroup>
0022 #include <KDirWatch>
0023 #include <KSharedConfig>
0024 
0025 namespace Latte {
0026 namespace WindowSystem {
0027 
0028 SchemeColors::SchemeColors(QObject *parent, QString scheme, bool plasmaTheme) :
0029     QObject(parent),
0030     m_basedOnPlasmaTheme(plasmaTheme)
0031 {
0032     QString pSchemeFile = possibleSchemeFile(scheme);
0033 
0034     if (QFileInfo(pSchemeFile).exists()) {
0035         setSchemeFile(pSchemeFile);
0036         m_schemeName = schemeName(pSchemeFile);
0037 
0038         //! track scheme file for changes
0039         KDirWatch::self()->addFile(m_schemeFile);
0040 
0041         connect(KDirWatch::self(), &KDirWatch::created, this, [ & ](const QString & path) {
0042             if (path == m_schemeFile) {
0043                 updateScheme();
0044             }
0045         });
0046 
0047         connect(KDirWatch::self(), &KDirWatch::dirty, this, [ & ](const QString & path) {
0048             if (path == m_schemeFile) {
0049                 updateScheme();
0050             }
0051         });
0052     }
0053 
0054     updateScheme();
0055 }
0056 
0057 SchemeColors::~SchemeColors()
0058 {
0059     ///
0060 }
0061 
0062 QColor SchemeColors::backgroundColor() const
0063 {
0064     return m_activeBackgroundColor;
0065 }
0066 
0067 QColor SchemeColors::textColor() const
0068 {
0069     return m_activeTextColor;
0070 }
0071 
0072 QColor SchemeColors::inactiveBackgroundColor() const
0073 {
0074     return m_inactiveBackgroundColor;
0075 }
0076 
0077 QColor SchemeColors::inactiveTextColor() const
0078 {
0079     return m_inactiveTextColor;
0080 }
0081 
0082 QColor SchemeColors::highlightColor() const
0083 {
0084     return m_highlightColor;
0085 }
0086 
0087 QColor SchemeColors::highlightedTextColor() const
0088 {
0089     return m_highlightedTextColor;
0090 }
0091 
0092 QColor SchemeColors::positiveTextColor() const
0093 {
0094     return m_positiveTextColor;
0095 }
0096 
0097 QColor SchemeColors::neutralTextColor() const
0098 {
0099     return m_neutralTextColor;
0100 }
0101 
0102 QColor SchemeColors::negativeTextColor() const
0103 {
0104     return m_negativeTextColor;
0105 }
0106 
0107 QColor SchemeColors::buttonTextColor() const
0108 {
0109     return m_buttonTextColor;
0110 }
0111 
0112 QColor SchemeColors::buttonBackgroundColor() const
0113 {
0114     return m_buttonBackgroundColor;
0115 }
0116 
0117 QColor SchemeColors::buttonHoverColor() const
0118 {
0119     return m_buttonHoverColor;
0120 }
0121 
0122 QColor SchemeColors::buttonFocusColor() const
0123 {
0124     return m_buttonFocusColor;
0125 }
0126 
0127 QString SchemeColors::schemeName() const
0128 {
0129     return m_schemeName;
0130 }
0131 
0132 QString SchemeColors::SchemeColors::schemeFile() const
0133 {
0134     return m_schemeFile;
0135 }
0136 
0137 void SchemeColors::setSchemeFile(QString file)
0138 {
0139     if (m_schemeFile == file) {
0140         return;
0141     }
0142 
0143     m_schemeFile = file;
0144     emit schemeFileChanged();
0145 }
0146 
0147 QString SchemeColors::possibleSchemeFile(QString scheme)
0148 {
0149     if (scheme == QLatin1String("kdeglobals")
0150             || (scheme.endsWith("kdeglobals") && QFileInfo(scheme).exists()) ) {
0151         // do nothing, accept kdeglobals case
0152     } else if (scheme.startsWith("/") && scheme.endsWith("colors") && QFileInfo(scheme).exists()) {
0153         return scheme;
0154     }
0155 
0156     QString schemePath;
0157     QString tempScheme = scheme;
0158 
0159     if (scheme == QLatin1String("kdeglobals")
0160             || (scheme.endsWith("kdeglobals") && QFileInfo(scheme).exists()) ) {
0161         QString settingsFile = Latte::configPath() + "/kdeglobals";
0162 
0163         bool supportsAutoAccentColor{false}; // introduced on plasma 5.25
0164 
0165         if (QFileInfo(settingsFile).exists()) {
0166             KSharedConfigPtr filePtr = KSharedConfig::openConfig(settingsFile);
0167             KConfigGroup wmGroup = KConfigGroup(filePtr, "WM");
0168             KConfigGroup generalGroup = KConfigGroup(filePtr, "General");
0169 
0170             if (wmGroup.hasKey("activeBackground")) {
0171                 supportsAutoAccentColor = true;
0172             } else {
0173                 tempScheme = generalGroup.readEntry("ColorScheme", "BreezeLight");
0174             }
0175         }
0176 
0177         if (supportsAutoAccentColor) {
0178             schemePath = Latte::configPath() + "/kdeglobals";
0179         } else {
0180             schemePath = Layouts::Importer::standardPath("color-schemes/" + tempScheme + ".colors");
0181         }
0182     } else {
0183         schemePath = Layouts::Importer::standardPath("color-schemes/" + tempScheme + ".colors");
0184     }
0185 
0186     if (schemePath.isEmpty() || !QFileInfo(schemePath).exists()) {
0187         //! remove all whitespaces and "-" from scheme in order to access correctly its file
0188         QString schemeNameSimplified = tempScheme.simplified().remove(" ").remove("-");
0189 
0190         schemePath = Layouts::Importer::standardPath("color-schemes/" + schemeNameSimplified + ".colors");
0191     }
0192 
0193     if (QFileInfo(schemePath).exists()) {
0194         return schemePath;
0195     }
0196 
0197     return "";
0198 }
0199 
0200 QString SchemeColors::schemeName(QString originalFile)
0201 {
0202     if (originalFile.endsWith("kdeglobals") && QFileInfo(originalFile).exists()) {
0203         return "kdeglobals";
0204     }
0205 
0206     if (!(originalFile.startsWith("/") && originalFile.endsWith("colors") && QFileInfo(originalFile).exists())) {
0207         return "";
0208     }
0209 
0210     QString fileNameNoExt =  originalFile;
0211 
0212     int lastSlash = originalFile.lastIndexOf("/");
0213 
0214     if (lastSlash >= 0) {
0215         fileNameNoExt.remove(0, lastSlash + 1);
0216     }
0217 
0218     if (fileNameNoExt.endsWith(".colors")) {
0219         fileNameNoExt.remove(".colors");
0220     }
0221 
0222     KSharedConfigPtr filePtr = KSharedConfig::openConfig(originalFile);
0223     KConfigGroup generalGroup = KConfigGroup(filePtr, "General");
0224 
0225     return generalGroup.readEntry("Name", fileNameNoExt);
0226 }
0227 
0228 void SchemeColors::updateScheme()
0229 {
0230     if (m_schemeFile.isEmpty() || !QFileInfo(m_schemeFile).exists()) {
0231         return;
0232     }
0233 
0234     KSharedConfigPtr filePtr = KSharedConfig::openConfig(m_schemeFile);
0235     KConfigGroup wmGroup = KConfigGroup(filePtr, "WM");
0236     KConfigGroup selGroup = KConfigGroup(filePtr, "Colors:Selection");
0237     //KConfigGroup viewGroup = KConfigGroup(filePtr, "Colors:View");
0238     KConfigGroup windowGroup = KConfigGroup(filePtr, "Colors:Window");
0239     KConfigGroup buttonGroup = KConfigGroup(filePtr, "Colors:Button");
0240 
0241     if (!m_basedOnPlasmaTheme) {
0242         m_activeBackgroundColor = wmGroup.readEntry("activeBackground", QColor());
0243         m_activeTextColor = wmGroup.readEntry("activeForeground", QColor());
0244         m_inactiveBackgroundColor = wmGroup.readEntry("inactiveBackground", QColor());
0245         m_inactiveTextColor = wmGroup.readEntry("inactiveForeground", QColor());
0246     } else {
0247         m_activeBackgroundColor = windowGroup.readEntry("BackgroundNormal", QColor());
0248         m_activeTextColor = windowGroup.readEntry("ForegroundNormal", QColor());
0249         m_inactiveBackgroundColor = windowGroup.readEntry("BackgroundAlternate", QColor());
0250         m_inactiveTextColor = windowGroup.readEntry("ForegroundInactive", QColor());
0251     }
0252 
0253     m_highlightColor = selGroup.readEntry("BackgroundNormal", QColor());
0254     m_highlightedTextColor = selGroup.readEntry("ForegroundNormal", QColor());
0255 
0256     m_positiveTextColor = windowGroup.readEntry("ForegroundPositive", QColor());
0257     m_neutralTextColor = windowGroup.readEntry("ForegroundNeutral", QColor());;
0258     m_negativeTextColor = windowGroup.readEntry("ForegroundNegative", QColor());
0259 
0260     m_buttonTextColor = buttonGroup.readEntry("ForegroundNormal", QColor());
0261     m_buttonBackgroundColor = buttonGroup.readEntry("BackgroundNormal", QColor());
0262     m_buttonHoverColor = buttonGroup.readEntry("DecorationHover", QColor());
0263     m_buttonFocusColor = buttonGroup.readEntry("DecorationFocus", QColor());
0264 
0265     emit colorsChanged();
0266 }
0267 
0268 }
0269 }