File indexing completed on 2024-05-05 05:28:59

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Hugo Pereira Da Costa <hugo.pereira@free.fr>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "breezesettingsprovider.h"
0008 
0009 #include "breezeexceptionlist.h"
0010 
0011 #include <QRegularExpression>
0012 #include <QTextStream>
0013 
0014 namespace Breeze
0015 {
0016 SettingsProvider *SettingsProvider::s_self = nullptr;
0017 
0018 //__________________________________________________________________
0019 SettingsProvider::SettingsProvider()
0020     : m_config(KSharedConfig::openConfig(QStringLiteral("breezerc")))
0021 {
0022     reconfigure();
0023 }
0024 
0025 //__________________________________________________________________
0026 SettingsProvider::~SettingsProvider()
0027 {
0028     s_self = nullptr;
0029 }
0030 
0031 //__________________________________________________________________
0032 SettingsProvider *SettingsProvider::self()
0033 {
0034     // TODO: this is not thread safe!
0035     if (!s_self) {
0036         s_self = new SettingsProvider();
0037     }
0038 
0039     return s_self;
0040 }
0041 
0042 //__________________________________________________________________
0043 void SettingsProvider::reconfigure()
0044 {
0045     if (!m_defaultSettings) {
0046         m_defaultSettings = InternalSettingsPtr(new InternalSettings());
0047         m_defaultSettings->setCurrentGroup(QStringLiteral("Windeco"));
0048     }
0049 
0050     m_defaultSettings->load();
0051 
0052     ExceptionList exceptions;
0053     exceptions.readConfig(m_config);
0054     m_exceptions = exceptions.get();
0055 }
0056 
0057 //__________________________________________________________________
0058 InternalSettingsPtr SettingsProvider::internalSettings(Decoration *decoration) const
0059 {
0060     QString windowTitle;
0061     QString windowClass;
0062 
0063     // get the client
0064     const auto client = decoration->client();
0065 
0066     for (auto internalSettings : std::as_const(m_exceptions)) {
0067         // discard disabled exceptions
0068         if (!internalSettings->enabled()) {
0069             continue;
0070         }
0071 
0072         // discard exceptions with empty exception pattern
0073         if (internalSettings->exceptionPattern().isEmpty()) {
0074             continue;
0075         }
0076 
0077         /*
0078         decide which value is to be compared
0079         to the regular expression, based on exception type
0080         */
0081         QString value;
0082         switch (internalSettings->exceptionType()) {
0083         case InternalSettings::ExceptionWindowTitle: {
0084             value = windowTitle.isEmpty() ? (windowTitle = client->caption()) : windowTitle;
0085             break;
0086         }
0087 
0088         default:
0089         case InternalSettings::ExceptionWindowClassName: {
0090             value = windowClass.isEmpty() ? (windowClass = client->windowClass()) : windowClass;
0091             break;
0092         }
0093         }
0094 
0095         // check matching
0096         QRegularExpression rx(internalSettings->exceptionPattern());
0097         if (rx.match(value).hasMatch()) {
0098             return internalSettings;
0099         }
0100     }
0101 
0102     return m_defaultSettings;
0103 }
0104 
0105 }