1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
/*
Copyright 2006 Kevin Ottens <ervin@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) version 3, or any
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "predicate.h"
#include <QMetaEnum>
#include <QStringList>
#include <solid-lite/device.h>
#include <solid-lite/deviceinterface.h>
namespace Solid {
class Predicate::Private {
public:
Private() : isValid(false), type(PropertyCheck),
compOperator(Predicate::Equals),
operand1(nullptr), operand2(nullptr) {}
bool isValid;
Type type;
DeviceInterface::Type ifaceType;
QString property;
QVariant value;
Predicate::ComparisonOperator compOperator;
Predicate* operand1;
Predicate* operand2;
};
}// namespace Solid
Solid::Predicate::Predicate()
: d(new Private())
{
}
Solid::Predicate::Predicate(const Predicate& other)
: d(new Private())
{
*this = other;
}
Solid::Predicate::Predicate(const DeviceInterface::Type& ifaceType,
const QString& property, const QVariant& value,
ComparisonOperator compOperator)
: d(new Private())
{
d->isValid = true;
d->ifaceType = ifaceType;
d->property = property;
d->value = value;
d->compOperator = compOperator;
}
Solid::Predicate::Predicate(const QString& ifaceName,
const QString& property, const QVariant& value,
ComparisonOperator compOperator)
: d(new Private())
{
DeviceInterface::Type ifaceType = DeviceInterface::stringToType(ifaceName);
if (((int)ifaceType) != -1) {
d->isValid = true;
d->ifaceType = ifaceType;
d->property = property;
d->value = value;
d->compOperator = compOperator;
}
}
Solid::Predicate::Predicate(const DeviceInterface::Type& ifaceType)
: d(new Private())
{
d->isValid = true;
d->type = InterfaceCheck;
d->ifaceType = ifaceType;
}
Solid::Predicate::Predicate(const QString& ifaceName)
: d(new Private())
{
DeviceInterface::Type ifaceType = DeviceInterface::stringToType(ifaceName);
if (((int)ifaceType) != -1) {
d->isValid = true;
d->type = InterfaceCheck;
d->ifaceType = ifaceType;
}
}
Solid::Predicate::~Predicate()
{
if (d->type != PropertyCheck && d->type != InterfaceCheck) {
delete d->operand1;
delete d->operand2;
}
delete d;
}
Solid::Predicate& Solid::Predicate::operator=(const Predicate& other)
{
d->isValid = other.d->isValid;
d->type = other.d->type;
if (d->type != PropertyCheck && d->type != InterfaceCheck) {
Predicate* operand1 = new Predicate(*(other.d->operand1));
delete d->operand1;
d->operand1 = operand1;
Predicate* operand2 = new Predicate(*(other.d->operand2));
delete d->operand2;
d->operand2 = operand2;
}
else {
d->ifaceType = other.d->ifaceType;
d->property = other.d->property;
d->value = other.d->value;
d->compOperator = other.d->compOperator;
}
return *this;
}
Solid::Predicate Solid::Predicate::operator&(const Predicate& other)
{
Predicate result;
result.d->isValid = true;
result.d->type = Conjunction;
result.d->operand1 = new Predicate(*this);
result.d->operand2 = new Predicate(other);
return result;
}
Solid::Predicate& Solid::Predicate::operator&=(const Predicate& other)
{
*this = *this & other;
return *this;
}
Solid::Predicate Solid::Predicate::operator|(const Predicate& other)
{
Predicate result;
result.d->isValid = true;
result.d->type = Disjunction;
result.d->operand1 = new Predicate(*this);
result.d->operand2 = new Predicate(other);
return result;
}
Solid::Predicate& Solid::Predicate::operator|=(const Predicate& other)
{
*this = *this | other;
return *this;
}
bool Solid::Predicate::isValid() const
{
return d->isValid;
}
bool Solid::Predicate::matches(const Device& device) const
{
if (!d->isValid) return false;
switch (d->type) {
case Disjunction:
return d->operand1->matches(device)
|| d->operand2->matches(device);
case Conjunction:
return d->operand1->matches(device)
&& d->operand2->matches(device);
case PropertyCheck: {
const DeviceInterface* iface = device.asDeviceInterface(d->ifaceType);
if (iface != nullptr) {
const int index = iface->metaObject()->indexOfProperty(d->property.toLatin1());
QMetaProperty metaProp = iface->metaObject()->property(index);
QVariant value = metaProp.isReadable() ? metaProp.read(iface) : QVariant();
QVariant expected = d->value;
if (metaProp.isEnumType() && expected.typeId() == QMetaType::QString) {
QMetaEnum metaEnum = metaProp.enumerator();
int value = metaEnum.keysToValue(d->value.toString().toLatin1());
if (value >= 0) {// No value found for these keys, resetting expected to invalid
expected = value;
}
else {
expected = QVariant();
}
}
if (d->compOperator == Mask) {
bool v_ok;
int v = value.toInt(&v_ok);
bool e_ok;
int e = expected.toInt(&e_ok);
return (e_ok && v_ok && (v & e));
}
else {
return (value == expected);
}
}
break;
}
case InterfaceCheck:
return device.isDeviceInterface(d->ifaceType);
}
return false;
}
QSet<Solid::DeviceInterface::Type> Solid::Predicate::usedTypes() const
{
QSet<DeviceInterface::Type> res;
if (d->isValid) {
switch (d->type) {
case Disjunction:
case Conjunction:
res += d->operand1->usedTypes();
res += d->operand2->usedTypes();
break;
case PropertyCheck:
case InterfaceCheck:
res << d->ifaceType;
break;
}
}
return res;
}
QString Solid::Predicate::toString() const
{
if (!d->isValid) return "False";
if (d->type != PropertyCheck && d->type != InterfaceCheck) {
QString op = " AND ";
if (d->type == Disjunction) op = " OR ";
return '[' + d->operand1->toString() + op + d->operand2->toString() + ']';
}
else {
QString ifaceName = DeviceInterface::typeToString(d->ifaceType);
if (ifaceName.isEmpty()) ifaceName = "Unknown";
if (d->type == InterfaceCheck) {
return "IS " + ifaceName;
}
QString value;
switch (d->value.typeId()) {
case QMetaType::QStringList: {
value = '{';
const QStringList list = d->value.toStringList();
QStringList::ConstIterator it = list.begin();
QStringList::ConstIterator end = list.end();
for (; it != end; ++it) {
value += '\'' + *it + '\'';
if (it + 1 != end) {
value += ", ";
}
}
value += '}';
break;
}
case QMetaType::Bool:
value = (d->value.toBool() ? "true" : "false");
break;
case QMetaType::Int:
case QMetaType::UInt:
case QMetaType::LongLong:
case QMetaType::ULongLong:
value = d->value.toString();
break;
default:
value = '\'' + d->value.toString() + '\'';
break;
}
QString str_operator = "==";
if (d->compOperator != Equals) str_operator = " &";
return ifaceName + '.' + d->property + ' ' + str_operator + ' ' + value;
}
}
Solid::Predicate::Type Solid::Predicate::type() const
{
return d->type;
}
Solid::DeviceInterface::Type Solid::Predicate::interfaceType() const
{
return d->ifaceType;
}
QString Solid::Predicate::propertyName() const
{
return d->property;
}
QVariant Solid::Predicate::matchingValue() const
{
return d->value;
}
Solid::Predicate::ComparisonOperator Solid::Predicate::comparisonOperator() const
{
return d->compOperator;
}
Solid::Predicate Solid::Predicate::firstOperand() const
{
if (d->operand1) {
return *d->operand1;
}
return Predicate();
}
Solid::Predicate Solid::Predicate::secondOperand() const
{
if (d->operand2) {
return *d->operand2;
}
return Predicate();
}
|