1 /*
2     SPDX-FileCopyrightText: 2019 Michail Vourlakos <mvourlakos@gmail.com>
3     SPDX-License-Identifier: GPL-2.0-or-later
4 */
5 
6 #include "schemes.h"
7 
8 // local
9 #include "../abstractwindowinterface.h"
10 #include "../../lattecorona.h"
11 #include "../../tools/commontools.h"
12 
13 // Qt
14 #include <QDir>
15 #include <QLatin1String>
16 
17 // KDE
18 #include <KDirWatch>
19 
20 
21 namespace Latte {
22 namespace WindowSystem {
23 namespace Tracker {
24 
Schemes(AbstractWindowInterface * parent)25 Schemes::Schemes(AbstractWindowInterface *parent)
26     : QObject(parent)
27 {
28     m_wm = parent;
29     init();
30 }
31 
~Schemes()32 Schemes::~Schemes()
33 {
34     m_windowScheme.clear();
35     //! it is just a reference to a real scheme file
36     m_schemes.take("kdeglobals");
37     qDeleteAll(m_schemes.values());
38     m_schemes.clear();
39 }
40 
init()41 void Schemes::init()
42 {
43     updateDefaultScheme();
44 
45     connect(this, &Schemes::colorSchemeChanged, this, [&](WindowId wid) {
46         if (wid == m_wm->activeWindow()) {
47             emit m_wm->activeWindowChanged(wid);
48         }
49     });
50 
51     connect(m_wm, &AbstractWindowInterface::windowRemoved, this, [&](WindowId wid) {
52         m_windowScheme.remove(wid);
53     });
54 
55     //! track for changing default scheme
56     QString kdeSettingsFile = Latte::configPath() + "/kdeglobals";
57 
58     KDirWatch::self()->addFile(kdeSettingsFile);
59 
60     connect(KDirWatch::self(), &KDirWatch::dirty, this, [ &, kdeSettingsFile](const QString & path) {
61         if (path == kdeSettingsFile) {
62             this->updateDefaultScheme();
63         }
64     });
65 
66     connect(KDirWatch::self(), &KDirWatch::created, this, [ &, kdeSettingsFile](const QString & path) {
67         if (path == kdeSettingsFile) {
68             this->updateDefaultScheme();
69         }
70     });
71 }
72 
73 //! Scheme support for windows
updateDefaultScheme()74 void Schemes::updateDefaultScheme()
75 {
76     QString defaultSchemePath = SchemeColors::possibleSchemeFile("kdeglobals");
77 
78     qDebug() << " Windows default color scheme :: " << defaultSchemePath;
79 
80     SchemeColors *dScheme;
81 
82     if (!m_schemes.contains(defaultSchemePath)) {
83         dScheme = new SchemeColors(this, defaultSchemePath);
84         m_schemes[defaultSchemePath] = dScheme;
85     } else {
86         dScheme = m_schemes[defaultSchemePath];
87     }
88 
89     if (!m_schemes.contains("kdeglobals") || m_schemes["kdeglobals"]->schemeFile() != defaultSchemePath) {
90         m_schemes["kdeglobals"] = dScheme;
91     }
92 
93     emit defaultSchemeChanged();
94 }
95 
schemeForFile(const QString & scheme)96 SchemeColors *Schemes::schemeForFile(const QString &scheme)
97 {
98     QString schemeFile = SchemeColors::possibleSchemeFile(scheme);
99 
100     if (!schemeFile.isEmpty() && !m_schemes.contains(schemeFile)) {
101         //! when this scheme file has not been loaded yet
102         m_schemes[schemeFile] = new SchemeColors(this, schemeFile);
103     }
104 
105     return m_schemes.contains(schemeFile) ? m_schemes[schemeFile] : nullptr;
106 }
107 
schemeForWindow(WindowId wid)108 SchemeColors *Schemes::schemeForWindow(WindowId wid)
109 {
110     if (!m_windowScheme.contains(wid)) {
111         return m_schemes["kdeglobals"];
112     } else {
113         return m_schemes[m_windowScheme[wid]];
114     }
115 
116     return nullptr;
117 }
118 
setColorSchemeForWindow(WindowId wid,QString scheme)119 void Schemes::setColorSchemeForWindow(WindowId wid, QString scheme)
120 {
121     if (scheme == QLatin1String("kdeglobals") && !m_windowScheme.contains(wid)) {
122         //default scheme does not have to be set
123         return;
124     }
125 
126     if (scheme == QLatin1String("kdeglobals")) {
127         //! a window that previously had an explicit set scheme now is set back to default scheme
128         m_windowScheme.remove(wid);
129     } else {
130         QString schemeFile = SchemeColors::possibleSchemeFile(scheme);
131 
132         if (!m_schemes.contains(schemeFile)) {
133             //! when this scheme file has not been loaded yet
134             m_schemes[schemeFile] = new SchemeColors(this, schemeFile);
135         }
136 
137         m_windowScheme[wid] = schemeFile;
138     }
139 
140     emit colorSchemeChanged(wid);
141 }
142 
143 }
144 }
145 }
146