• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

Solid

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

KDE's Doxygen guidelines are available online.

Solid

Skip menu "Solid"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal