File indexing completed on 2024-07-14 15:45:32

0001 /*
0002  * Copyright 2018  Michail Vourlakos <mvourlakos@gmail.com>
0003  *
0004  * This file is part of Latte-Dock
0005  *
0006  * Latte-Dock is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU General Public License as
0008  * published by the Free Software Foundation; either version 2 of
0009  * the License, or (at your option) any later version.
0010  *
0011  * Latte-Dock is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License
0017  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0018  *
0019  */
0020 
0021 #include "schemecolors.h"
0022 
0023 // local
0024 #include "../layouts/importer.h"
0025 
0026 // Qt
0027 #include <QDebug>
0028 #include <QDir>
0029 #include <QFileInfo>
0030 
0031 // KDE
0032 #include <KConfigGroup>
0033 #include <KDirWatch>
0034 #include <KSharedConfig>
0035 
0036 namespace Latte {
0037 namespace WindowSystem {
0038 
0039 SchemeColors::SchemeColors(QObject *parent, QString scheme, bool plasmaTheme) :
0040     QObject(parent),
0041     m_basedOnPlasmaTheme(plasmaTheme)
0042 {
0043     QString pSchemeFile = possibleSchemeFile(scheme);
0044 
0045     if (QFileInfo(pSchemeFile).exists()) {
0046         setSchemeFile(pSchemeFile);
0047         m_schemeName = schemeName(pSchemeFile);
0048 
0049         //! track scheme file for changes
0050         KDirWatch::self()->addFile(m_schemeFile);
0051 
0052         connect(KDirWatch::self(), &KDirWatch::dirty, this, [ & ](const QString & path) {
0053             if (path == m_schemeFile) {
0054                 updateScheme();
0055             }
0056         });
0057     }
0058 
0059     updateScheme();
0060 }
0061 
0062 SchemeColors::~SchemeColors()
0063 {
0064     ///
0065 }
0066 
0067 QColor SchemeColors::backgroundColor() const
0068 {
0069     return m_activeBackgroundColor;
0070 }
0071 
0072 QColor SchemeColors::textColor() const
0073 {
0074     return m_activeTextColor;
0075 }
0076 
0077 QColor SchemeColors::inactiveBackgroundColor() const
0078 {
0079     return m_inactiveBackgroundColor;
0080 }
0081 
0082 QColor SchemeColors::inactiveTextColor() const
0083 {
0084     return m_inactiveTextColor;
0085 }
0086 
0087 QColor SchemeColors::highlightColor() const
0088 {
0089     return m_highlightColor;
0090 }
0091 
0092 QColor SchemeColors::highlightedTextColor() const
0093 {
0094     return m_highlightedTextColor;
0095 }
0096 
0097 QColor SchemeColors::positiveTextColor() const
0098 {
0099     return m_positiveTextColor;
0100 }
0101 
0102 QColor SchemeColors::neutralTextColor() const
0103 {
0104     return m_neutralTextColor;
0105 }
0106 
0107 QColor SchemeColors::negativeTextColor() const
0108 {
0109     return m_negativeTextColor;
0110 }
0111 
0112 QColor SchemeColors::buttonTextColor() const
0113 {
0114     return m_buttonTextColor;
0115 }
0116 
0117 QColor SchemeColors::buttonBackgroundColor() const
0118 {
0119     return m_buttonBackgroundColor;
0120 }
0121 
0122 QColor SchemeColors::buttonHoverColor() const
0123 {
0124     return m_buttonHoverColor;
0125 }
0126 
0127 QColor SchemeColors::buttonFocusColor() const
0128 {
0129     return m_buttonFocusColor;
0130 }
0131 
0132 QString SchemeColors::schemeName() const
0133 {
0134     return m_schemeName;
0135 }
0136 
0137 QString SchemeColors::SchemeColors::schemeFile() const
0138 {
0139     return m_schemeFile;
0140 }
0141 
0142 void SchemeColors::setSchemeFile(QString file)
0143 {
0144     if (m_schemeFile == file) {
0145         return;
0146     }
0147 
0148     m_schemeFile = file;
0149     emit schemeFileChanged();
0150 }
0151 
0152 QString SchemeColors::possibleSchemeFile(QString scheme)
0153 {
0154     if (scheme.startsWith("/") && scheme.endsWith("colors") && QFileInfo(scheme).exists()) {
0155         return scheme;
0156     }
0157 
0158     QString tempScheme = scheme;
0159 
0160     if (scheme == "kdeglobals") {
0161         QString settingsFile = QDir::homePath() + "/.config/kdeglobals";
0162 
0163         if (QFileInfo(settingsFile).exists()) {
0164             KSharedConfigPtr filePtr = KSharedConfig::openConfig(settingsFile);
0165             KConfigGroup generalGroup = KConfigGroup(filePtr, "General");
0166             tempScheme = generalGroup.readEntry("ColorScheme", "");
0167         }
0168     }
0169 
0170     QString schemePath = Layouts::Importer::standardPath("color-schemes/" + tempScheme + ".colors");
0171 
0172     if (schemePath.isEmpty() || !QFileInfo(schemePath).exists()) {
0173         //! remove all whitespaces and "-" from scheme in order to access correctly its file
0174         QString schemeNameSimplified = tempScheme.simplified().remove(" ").remove("-");
0175 
0176         schemePath = Layouts::Importer::standardPath("color-schemes/" + schemeNameSimplified + ".colors");
0177     }
0178 
0179     if (QFileInfo(schemePath).exists()) {
0180         return schemePath;
0181     }
0182 
0183     return "";
0184 }
0185 
0186 QString SchemeColors::schemeName(QString originalFile)
0187 {
0188     if (!(originalFile.startsWith("/") && originalFile.endsWith("colors") && QFileInfo(originalFile).exists())) {
0189         return "";
0190     }
0191 
0192     QString fileNameNoExt =  originalFile;
0193 
0194     int lastSlash = originalFile.lastIndexOf("/");
0195 
0196     if (lastSlash >= 0) {
0197         fileNameNoExt.remove(0, lastSlash + 1);
0198     }
0199 
0200     if (fileNameNoExt.endsWith(".colors")) {
0201         fileNameNoExt.remove(".colors");
0202     }
0203 
0204     KSharedConfigPtr filePtr = KSharedConfig::openConfig(originalFile);
0205     KConfigGroup generalGroup = KConfigGroup(filePtr, "General");
0206 
0207     return generalGroup.readEntry("Name", fileNameNoExt);
0208 }
0209 
0210 void SchemeColors::updateScheme()
0211 {
0212     if (m_schemeFile.isEmpty() || !QFileInfo(m_schemeFile).exists()) {
0213         return;
0214     }
0215 
0216     KSharedConfigPtr filePtr = KSharedConfig::openConfig(m_schemeFile);
0217     KConfigGroup wmGroup = KConfigGroup(filePtr, "WM");
0218     KConfigGroup selGroup = KConfigGroup(filePtr, "Colors:Selection");
0219     //KConfigGroup viewGroup = KConfigGroup(filePtr, "Colors:View");
0220     KConfigGroup windowGroup = KConfigGroup(filePtr, "Colors:Window");
0221     KConfigGroup buttonGroup = KConfigGroup(filePtr, "Colors:Button");
0222 
0223     if (!m_basedOnPlasmaTheme) {
0224         m_activeBackgroundColor = wmGroup.readEntry("activeBackground", QColor());
0225         m_activeTextColor = wmGroup.readEntry("activeForeground", QColor());
0226         m_inactiveBackgroundColor = wmGroup.readEntry("inactiveBackground", QColor());
0227         m_inactiveTextColor = wmGroup.readEntry("inactiveForeground", QColor());
0228     } else {
0229         m_activeBackgroundColor = windowGroup.readEntry("BackgroundNormal", QColor());
0230         m_activeTextColor = windowGroup.readEntry("ForegroundNormal", QColor());
0231         m_inactiveBackgroundColor = windowGroup.readEntry("BackgroundAlternate", QColor());
0232         m_inactiveTextColor = windowGroup.readEntry("ForegroundInactive", QColor());
0233     }
0234 
0235     m_highlightColor = selGroup.readEntry("BackgroundNormal", QColor());
0236     m_highlightedTextColor = selGroup.readEntry("ForegroundNormal", QColor());
0237 
0238     m_positiveTextColor = windowGroup.readEntry("ForegroundPositive", QColor());
0239     m_neutralTextColor = windowGroup.readEntry("ForegroundNeutral", QColor());;
0240     m_negativeTextColor = windowGroup.readEntry("ForegroundNegative", QColor());
0241 
0242     m_buttonTextColor = buttonGroup.readEntry("ForegroundNormal", QColor());
0243     m_buttonBackgroundColor = buttonGroup.readEntry("BackgroundNormal", QColor());
0244     m_buttonHoverColor = buttonGroup.readEntry("DecorationHover", QColor());
0245     m_buttonFocusColor = buttonGroup.readEntry("DecorationFocus", QColor());
0246 
0247     emit colorsChanged();
0248 }
0249 
0250 }
0251 }