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

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 "tabletbackend.h"
0021 
0022 #include "logging.h"
0023 #include "procsystemadaptor.h"
0024 #include "procsystemproperty.h"
0025 #include "property.h"
0026 #include "propertyset.h"
0027 
0028 namespace Wacom
0029 {
0030 class TabletBackendPrivate
0031 {
0032 public:
0033     TabletBackend::DeviceMap deviceAdaptors;
0034     PropertyAdaptor *statusLEDAdaptor;
0035     TabletInformation tabletInformation;
0036 };
0037 }
0038 
0039 using namespace Wacom;
0040 
0041 TabletBackend::TabletBackend(const Wacom::TabletInformation &tabletInformation)
0042     : d_ptr(new TabletBackendPrivate)
0043 {
0044     Q_D(TabletBackend);
0045     d->tabletInformation = tabletInformation;
0046 
0047     d_ptr->statusLEDAdaptor = new ProcSystemAdaptor(d->tabletInformation.getDeviceName(DeviceType::Pad));
0048 }
0049 
0050 TabletBackend::~TabletBackend()
0051 {
0052     // delete all property adaptors
0053     DeviceMap::iterator deviceIter;
0054 
0055     for (deviceIter = d_ptr->deviceAdaptors.begin(); deviceIter != d_ptr->deviceAdaptors.end(); ++deviceIter) {
0056         AdaptorList::iterator adaptorIter = deviceIter.value().begin();
0057 
0058         while (adaptorIter != deviceIter.value().end()) {
0059             delete (*adaptorIter);
0060             adaptorIter = deviceIter.value().erase(adaptorIter);
0061         }
0062     }
0063 
0064     delete d_ptr->statusLEDAdaptor;
0065 
0066     // delete private class
0067     delete d_ptr;
0068 }
0069 
0070 void TabletBackend::addAdaptor(const DeviceType &deviceType, PropertyAdaptor *adaptor)
0071 {
0072     Q_D(TabletBackend);
0073 
0074     d->deviceAdaptors[deviceType].append(adaptor);
0075 }
0076 
0077 const TabletInformation &TabletBackend::getInformation() const
0078 {
0079     Q_D(const TabletBackend);
0080     return d->tabletInformation;
0081 }
0082 
0083 const QString TabletBackend::getProperty(const DeviceType &type, const Property &property) const
0084 {
0085     Q_D(const TabletBackend);
0086 
0087     DeviceMap::const_iterator adaptors = d->deviceAdaptors.constFind(type);
0088     if (adaptors == d->deviceAdaptors.constEnd()) {
0089         qCWarning(KDED) << QString::fromLatin1("Could not get property '%1' from unsupported device type '%2'!").arg(property.key()).arg(type.key());
0090         return QString();
0091     }
0092 
0093     foreach (const PropertyAdaptor *adaptor, adaptors.value()) {
0094         if (adaptor->supportsProperty(property)) {
0095             return adaptor->getProperty(property);
0096         }
0097     }
0098 
0099     return QString();
0100 }
0101 
0102 void TabletBackend::setProfile(const TabletProfile &profile)
0103 {
0104     Q_D(TabletBackend);
0105 
0106     foreach (const DeviceType &deviceType, DeviceType::list()) {
0107         if (d->tabletInformation.hasDevice(deviceType)) {
0108             if (profile.hasDevice(deviceType)) {
0109                 qCDebug(KDED) << QString::fromLatin1("Setting profile '%1' on tablet '%2', device '%3'")
0110                                      .arg(profile.getName())
0111                                      .arg(d->tabletInformation.get(TabletInfo::TabletName))
0112                                      .arg(deviceType.key());
0113                 DeviceProfile deviceProfile = profile.getDevice(deviceType);
0114                 setProfile(deviceType, deviceProfile);
0115             } else {
0116                 qCDebug(KDED) << QString::fromLatin1("Skipping '%1' settings as the current profile does not contain any settings for this device...")
0117                                      .arg(deviceType.key());
0118             }
0119         } else {
0120             qCDebug(KDED) << QString::fromLatin1("Skipping '%1' settings as the device does not support it...").arg(deviceType.key());
0121         }
0122     }
0123 }
0124 
0125 void TabletBackend::setProfile(const DeviceType &deviceType, const DeviceProfile &profile)
0126 {
0127     Q_D(TabletBackend);
0128 
0129     DeviceMap::iterator adaptors = d->deviceAdaptors.find(deviceType);
0130     if (adaptors == d->deviceAdaptors.end()) {
0131         qCWarning(KDED) << QString::fromLatin1("Could not set profile on unsupported device type '%1'!").arg(deviceType.key());
0132         return;
0133     }
0134 
0135     QString value;
0136 
0137     // set properties on all adaptors
0138     foreach (PropertyAdaptor *adaptor, adaptors.value()) {
0139         // ask adaptor which properties it supports
0140         // this will also ensure that the properties are set in the correct order
0141         foreach (const Property &property, adaptor->getProperties()) {
0142             // set property value if it is supported by the profile and not empty
0143             if (profile.supportsProperty(property)) {
0144                 value = profile.getProperty(property);
0145 
0146                 if (!value.isEmpty()) {
0147                     adaptor->setProperty(property, value);
0148                 }
0149             }
0150         }
0151     }
0152 }
0153 
0154 void TabletBackend::setStatusLED(int led)
0155 {
0156     Q_D(TabletBackend);
0157     if (d->tabletInformation.statusLEDs() > 0) {
0158         d_ptr->statusLEDAdaptor->setProperty(Property::StatusLEDs, QString::number(led));
0159     }
0160 }
0161 
0162 void TabletBackend::setStatusLEDBrightness(int brightness)
0163 {
0164     Q_D(TabletBackend);
0165     if (d->tabletInformation.statusLEDs() > 0) {
0166         d_ptr->statusLEDAdaptor->setProperty(Property::StatusLEDsBrightness, QString::number(brightness));
0167     }
0168 }
0169 
0170 bool TabletBackend::setProperty(const DeviceType &type, const Property &property, const QString &value)
0171 {
0172     Q_D(TabletBackend);
0173 
0174     DeviceMap::iterator adaptors = d->deviceAdaptors.find(type);
0175     if (adaptors == d->deviceAdaptors.end()) {
0176         qCWarning(KDED)
0177             << QString::fromLatin1("Could not set property '%1' to '%2' on unsupported device type '%3'!").arg(property.key()).arg(value).arg(type.key());
0178         return false;
0179     }
0180 
0181     bool returnValue = false;
0182     foreach (PropertyAdaptor *adaptor, adaptors.value()) {
0183         if (adaptor->supportsProperty(property)) {
0184             if (adaptor->setProperty(property, value)) {
0185                 returnValue = true;
0186             }
0187         }
0188     }
0189 
0190     return returnValue;
0191 }