File indexing completed on 2024-12-22 05:17:14

0001 /*
0002  * This file is part of the KDE wacomtablet project. For copyright
0003  * information and license terms see the AUTHORS and COPYING files
0004  * in the top-level directory of this distribution.
0005  *
0006  * This program 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  * This program 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 #include "deviceprofileconfigadaptor.h"
0021 
0022 #include "logging.h"
0023 
0024 #include "deviceproperty.h"
0025 
0026 #include <QString>
0027 #include <QStringList>
0028 #include <QtGlobal>
0029 
0030 using namespace Wacom;
0031 
0032 DeviceProfileConfigAdaptor::DeviceProfileConfigAdaptor(DeviceProfile &profile)
0033     : ConfigAdaptor(&profile)
0034 {
0035 }
0036 
0037 DeviceProfileConfigAdaptor::~DeviceProfileConfigAdaptor()
0038 {
0039 }
0040 
0041 bool DeviceProfileConfigAdaptor::loadConfig(const KConfigGroup &config)
0042 {
0043     const DeviceProperty *deviceProperty = nullptr;
0044 
0045     QStringList keys = config.keyList();
0046 
0047     foreach (const QString &key, keys) {
0048         QString lookupKey = key.toLower();
0049 
0050         // handle old configuration format - this should be removed in the future as
0051         // it prevents us from using configuration keys which start with 'x' or '0'.
0052         // "1" is removed for "1TVResolution[01]" parameter
0053         if (lookupKey.startsWith(QLatin1String("x")) || lookupKey.startsWith(QLatin1String("0")) || lookupKey.startsWith(QLatin1String("1"))) {
0054             lookupKey.remove(0, 1);
0055         }
0056 
0057         // underscore is replaced for now to keep compatibility with older configuration files ("Screen_No" parameter)
0058         lookupKey = lookupKey.replace(QLatin1String("_"), QLatin1String(""));
0059 
0060         deviceProperty = DeviceProperty::find(lookupKey);
0061 
0062         if (deviceProperty == nullptr) {
0063             qCWarning(COMMON) << QString::fromLatin1("Unable to read unsupported configuration property '%1' from config file!").arg(key);
0064             continue;
0065         }
0066 
0067         setProperty(deviceProperty->id(), config.readEntry(key));
0068     }
0069 
0070     return true;
0071 }
0072 
0073 bool DeviceProfileConfigAdaptor::saveConfig(KConfigGroup &config) const
0074 {
0075     const DeviceProperty *deviceProperty = nullptr;
0076     QList<Property> properties = getProperties();
0077     QString value;
0078 
0079     foreach (const Property &property, properties) {
0080         deviceProperty = DeviceProperty::map(property);
0081 
0082         if (deviceProperty == nullptr) {
0083             qCWarning(COMMON) << QString::fromLatin1("Unable to save unsupported system property '%1' to config file!").arg(property.key());
0084             continue;
0085         }
0086 
0087         value = getProperty(deviceProperty->id());
0088 
0089         if (value.isEmpty()) {
0090             config.deleteEntry(deviceProperty->key());
0091         } else {
0092             config.writeEntry(deviceProperty->key(), value);
0093         }
0094     }
0095 
0096     return !properties.isEmpty();
0097 }