File indexing completed on 2024-05-05 05:35:17

0001 //////////////////////////////////////////////////////////////////////////////
0002 // oxygenexceptionlist.cpp
0003 // window decoration exceptions
0004 // -------------------
0005 //
0006 // SPDX-FileCopyrightText: 2009 Hugo Pereira Da Costa <hugo.pereira@free.fr>
0007 //
0008 // SPDX-License-Identifier: MIT
0009 //////////////////////////////////////////////////////////////////////////////
0010 
0011 #include "oxygenexceptionlist.h"
0012 
0013 namespace Oxygen
0014 {
0015 //______________________________________________________________
0016 void ExceptionList::readConfig(KSharedConfig::Ptr config)
0017 {
0018     _exceptions.clear();
0019 
0020     QString groupName;
0021     for (int index = 0; config->hasGroup(groupName = exceptionGroupName(index)); ++index) {
0022         // create exception
0023         InternalSettings exception;
0024 
0025         // reset group
0026         readConfig(&exception, config.data(), groupName);
0027 
0028         // create new configuration
0029         InternalSettingsPtr configuration(new InternalSettings());
0030         configuration.data()->load();
0031 
0032         // apply changes from exception
0033         configuration->setEnabled(exception.enabled());
0034         configuration->setExceptionType(exception.exceptionType());
0035         configuration->setExceptionPattern(exception.exceptionPattern());
0036         configuration->setMask(exception.mask());
0037 
0038         // propagate all features found in mask to the output configuration
0039         if (exception.mask() & BorderSize)
0040             configuration->setBorderSize(exception.borderSize());
0041         configuration->setHideTitleBar(exception.hideTitleBar());
0042 
0043         // append to exceptions
0044         _exceptions.append(configuration);
0045     }
0046 }
0047 
0048 //______________________________________________________________
0049 void ExceptionList::writeConfig(KSharedConfig::Ptr config)
0050 {
0051     // remove all existing exceptions
0052     QString groupName;
0053     for (int index = 0; config->hasGroup(groupName = exceptionGroupName(index)); ++index) {
0054         config->deleteGroup(groupName);
0055     }
0056 
0057     // rewrite current exceptions
0058     int index = 0;
0059     for (const InternalSettingsPtr &exception : std::as_const(_exceptions)) {
0060         writeConfig(exception.data(), config.data(), exceptionGroupName(index));
0061         ++index;
0062     }
0063 }
0064 
0065 //_______________________________________________________________________
0066 QString ExceptionList::exceptionGroupName(int index)
0067 {
0068     return QStringLiteral("Windeco Exception %1").arg(index);
0069 }
0070 
0071 //______________________________________________________________
0072 void ExceptionList::writeConfig(KCoreConfigSkeleton *skeleton, KConfig *config, const QString &groupName)
0073 {
0074     // list of items to be written
0075     static const QStringList keys = {QStringLiteral("Enabled"),
0076                                      QStringLiteral("ExceptionPattern"),
0077                                      QStringLiteral("ExceptionType"),
0078                                      QStringLiteral("HideTitleBar"),
0079                                      QStringLiteral("Mask"),
0080                                      QStringLiteral("BorderSize")};
0081 
0082     // write all items
0083     for (const auto &key : keys) {
0084         KConfigSkeletonItem *item(skeleton->findItem(key));
0085         if (!item)
0086             continue;
0087 
0088         if (!groupName.isEmpty())
0089             item->setGroup(groupName);
0090         KConfigGroup configGroup(config, item->group());
0091         configGroup.writeEntry(item->key(), item->property());
0092     }
0093 }
0094 
0095 //______________________________________________________________
0096 void ExceptionList::readConfig(KCoreConfigSkeleton *skeleton, KConfig *config, const QString &groupName)
0097 {
0098     const auto skelItems = skeleton->items();
0099     for (KConfigSkeletonItem *item : skelItems) {
0100         if (!groupName.isEmpty())
0101             item->setGroup(groupName);
0102         item->readConfig(config);
0103     }
0104 }
0105 }