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

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 "tabletprofileconfigadaptor.h"
0021 
0022 #include "deviceprofileconfigadaptor.h"
0023 #include "logging.h"
0024 
0025 using namespace Wacom;
0026 
0027 namespace Wacom
0028 {
0029 /**
0030  * Private class for the d-pointer.
0031  */
0032 class TabletProfileConfigAdaptorPrivate
0033 {
0034 public:
0035     TabletProfile *profile = nullptr;
0036 }; // CLASS
0037 } // NAMESPACE
0038 
0039 TabletProfileConfigAdaptor::TabletProfileConfigAdaptor(TabletProfile &profile)
0040     : ConfigAdaptor(nullptr)
0041     , d_ptr(new TabletProfileConfigAdaptorPrivate)
0042 {
0043     Q_D(TabletProfileConfigAdaptor);
0044     d->profile = &profile;
0045 }
0046 
0047 TabletProfileConfigAdaptor::~TabletProfileConfigAdaptor()
0048 {
0049     delete this->d_ptr;
0050 }
0051 
0052 bool TabletProfileConfigAdaptor::loadConfig(const KConfigGroup &config)
0053 {
0054     Q_D(TabletProfileConfigAdaptor);
0055     if (d->profile == nullptr) {
0056         qCWarning(COMMON) << "Profile is null";
0057         return false;
0058     }
0059 
0060     d->profile->setName(config.name());
0061     d->profile->clearDevices();
0062 
0063     QStringList devices = config.groupList();
0064 
0065     foreach (const QString &dev, devices) {
0066         const DeviceType *deviceType = DeviceType::find(dev);
0067 
0068         if (deviceType == nullptr) {
0069             qCWarning(COMMON) << QString::fromLatin1("Invalid device identifier '%1' found in configuration file!").arg(dev);
0070             continue;
0071         }
0072 
0073         KConfigGroup devconfig(&config, dev);
0074         DeviceProfile devprofile(*deviceType);
0075         DeviceProfileConfigAdaptor devadaptor(devprofile);
0076 
0077         devadaptor.loadConfig(devconfig);
0078 
0079         d->profile->setDevice(devprofile);
0080     }
0081 
0082     return true;
0083 }
0084 
0085 bool TabletProfileConfigAdaptor::saveConfig(KConfigGroup &config) const
0086 {
0087     Q_D(const TabletProfileConfigAdaptor);
0088     if (d->profile == nullptr) {
0089         qCWarning(COMMON) << "Profile is null";
0090         return false;
0091     }
0092 
0093     // delete all groups before writing out the new device groups
0094     QStringList groups = config.groupList();
0095 
0096     foreach (const QString &group, groups) {
0097         KConfigGroup(&config, group).deleteGroup();
0098     }
0099 
0100     // write out new device config groups
0101     QStringList devices = d->profile->listDevices();
0102 
0103     foreach (const QString &dev, devices) {
0104         const DeviceType *deviceType = DeviceType::find(dev);
0105         if (deviceType == nullptr) {
0106             qCWarning(COMMON) << QString::fromLatin1("Invalid device identifier '%1' found in configuration file!").arg(dev);
0107             continue;
0108         }
0109 
0110         KConfigGroup devconfig = KConfigGroup(&config, dev);
0111         DeviceProfile devprofile = d->profile->getDevice(*deviceType);
0112         DeviceProfileConfigAdaptor devadaptor(devprofile);
0113 
0114         devconfig.deleteGroup();
0115         devadaptor.saveConfig(devconfig);
0116     }
0117 
0118     return true;
0119 }