| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies). |
| 4 | ** Contact: http://www.qt-project.org/legal |
| 5 | ** |
| 6 | ** This file is part of the QtSystems module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include <QRegularExpression> |
| 41 | #include <QStringList> |
| 42 | #include <QDebug> |
| 43 | #ifndef QT_NO_DATASTREAM |
| 44 | #include <qdatastream.h> |
| 45 | #endif |
| 46 | |
| 47 | #include "qservicefilter.h" |
| 48 | |
| 49 | QT_BEGIN_NAMESPACE |
| 50 | |
| 51 | class QServiceFilterPrivate |
| 52 | { |
| 53 | public: |
| 54 | QString interface; |
| 55 | QString service; |
| 56 | int majorVersion; |
| 57 | int minorVersion; |
| 58 | QServiceFilter::VersionMatchRule matchingRule; |
| 59 | QHash<QString,QString> customAttributes; |
| 60 | QStringList capabilities; |
| 61 | QServiceFilter::CapabilityMatchRule capMatchingRule; |
| 62 | }; |
| 63 | |
| 64 | |
| 65 | /*! |
| 66 | \class QServiceFilter |
| 67 | |
| 68 | \ingroup servicefw |
| 69 | \inmodule QtServiceFramework |
| 70 | \brief The QServiceFilter class defines criteria for defining a sub-set of |
| 71 | all available services. |
| 72 | |
| 73 | A QServiceFilter can be used to constrain the number of services when searching |
| 74 | for services. Only those services that match all filter criteria are returned |
| 75 | by \l QServiceManager::findInterfaces(). |
| 76 | |
| 77 | |
| 78 | \sa QServiceInterfaceDescriptor, QServiceManager |
| 79 | */ |
| 80 | |
| 81 | /*! |
| 82 | \enum QServiceFilter::VersionMatchRule |
| 83 | |
| 84 | This enum describes how interface version matching is performed. |
| 85 | |
| 86 | \value ExactVersionMatch The filter matches any interface implementation that implements |
| 87 | the exact version provided. |
| 88 | \value MinimumVersionMatch The filter matches any interface implementation that implements |
| 89 | either the given major/minor version or any subsequent version. |
| 90 | */ |
| 91 | |
| 92 | /*! |
| 93 | \enum QServiceFilter::CapabilityMatchRule |
| 94 | |
| 95 | This enum describes the capability/permission matching rules. Some platforms restrict what services clients |
| 96 | can access using "capabilities" or permissions. Services with more capabilities require |
| 97 | more privileged clients. Platforms without capabilities may ignore this type of matching |
| 98 | rule as the default behavior is to ignore any capability restrictions. |
| 99 | |
| 100 | This is a brief example. Assuming that the system knows the services S1 - S6 which require capabilities as stated below: |
| 101 | \table |
| 102 | \header \li Service \li Required capabilities |
| 103 | \row \li S1 \li \{\} |
| 104 | \row \li S2 \li \{A\} |
| 105 | \row \li S3 \li \{A,B\} |
| 106 | \row \li S4 \li \{A,B,C,D\} |
| 107 | \row \li S5 \li \{A,D\} |
| 108 | \row \li S6 \li \{F\} |
| 109 | \endtable |
| 110 | |
| 111 | The matching rules would apply as follows: |
| 112 | |
| 113 | \table |
| 114 | \header \li Matching rule \li Filter's capabilities \li Matching services |
| 115 | \row \li MatchLoadable \li \{\} \li S1 |
| 116 | \row \li MatchLoadable \li \{A\} \li S1, S2 |
| 117 | \row \li MatchLoadable \li \{A,B,C\} \li S1, S2, S3 |
| 118 | \row \li MatchMinimum \li \{\} \li S1, S2, S3, S4, S5, S6 |
| 119 | \row \li MatchMinimum \li \{A\} \li S2, S3, S4, S5 |
| 120 | \row \li MatchMinimum \li \{A,B,C\} \li S4 |
| 121 | \endtable |
| 122 | |
| 123 | \value MatchMinimum The filter matches any service that requires at least the given |
| 124 | filter capabilities. This may mean that the returned services |
| 125 | may require more capabilities than the specified ones. |
| 126 | Such a search is equivalent to a wildcard match if the passed filter's capability list is empty. In mathematical set notation |
| 127 | this rule is equivalent to Cap\sub{(Filter)} \\ Cap\sub{(Service)} = {}. This is the default matching rule. |
| 128 | \value MatchLoadable The filter matches any service that could be loaded by the client. |
| 129 | Using this matching rule guarantees that the returned services do not |
| 130 | require more capabilites than specified by this rule. It includes services |
| 131 | with no capability requirements. If this rule |
| 132 | is provided alongside an empty capability search list the returned |
| 133 | services do not require any capabilities and thus can be accessed |
| 134 | by any client. The equivalent set notation is Cap\sub{(Service)} \\ Cap\sub{(Filter)} = {}. |
| 135 | */ |
| 136 | |
| 137 | /*! |
| 138 | Creates a new filter object that matches all service implementations. |
| 139 | */ |
| 140 | QServiceFilter::QServiceFilter() |
| 141 | { |
| 142 | d = new QServiceFilterPrivate(); |
| 143 | d->majorVersion = -1; |
| 144 | d->minorVersion = -1; |
| 145 | d->matchingRule = QServiceFilter::MinimumVersionMatch; |
| 146 | d->capMatchingRule = QServiceFilter::MatchMinimum; |
| 147 | } |
| 148 | |
| 149 | /*! |
| 150 | Creates a copy of QServiceFilter object contained in \a other. |
| 151 | */ |
| 152 | QServiceFilter::QServiceFilter(const QServiceFilter& other) |
| 153 | { |
| 154 | d = new QServiceFilterPrivate(); |
| 155 | (*this) = other; |
| 156 | } |
| 157 | |
| 158 | /*! |
| 159 | \fn QServiceFilter::QServiceFilter(const QString& interfaceName, const QString& version, QServiceFilter::VersionMatchRule rule) |
| 160 | |
| 161 | Creates a new filter object that matches all service |
| 162 | implementations implementing \a interfaceName that match the specified |
| 163 | \a version using the given \a rule. |
| 164 | */ |
| 165 | QServiceFilter::QServiceFilter(const QString& interfaceName, const QString& version, QServiceFilter::VersionMatchRule rule) |
| 166 | { |
| 167 | d = new QServiceFilterPrivate(); |
| 168 | d->majorVersion = -1; |
| 169 | d->minorVersion = -1; |
| 170 | d->matchingRule = QServiceFilter::MinimumVersionMatch; |
| 171 | d->capMatchingRule = QServiceFilter::MatchMinimum; |
| 172 | setInterface(interfaceName, version, rule); |
| 173 | } |
| 174 | |
| 175 | /*! |
| 176 | Destroys this instance of QServiceFilter. |
| 177 | */ |
| 178 | QServiceFilter::~QServiceFilter() |
| 179 | { |
| 180 | delete d; |
| 181 | } |
| 182 | |
| 183 | /*! |
| 184 | \fn QServiceFilter& QServiceFilter::operator=(const QServiceFilter& other) |
| 185 | |
| 186 | Copies the content of the QServiceFilter object contained in |
| 187 | \a other into this one. |
| 188 | */ |
| 189 | QServiceFilter& QServiceFilter::operator=(const QServiceFilter& other) |
| 190 | { |
| 191 | if (&other == this) |
| 192 | return *this; |
| 193 | |
| 194 | d->interface = other.d->interface; |
| 195 | d->service = other.d->service; |
| 196 | d->majorVersion = other.d->majorVersion; |
| 197 | d->minorVersion = other.d->minorVersion; |
| 198 | d->matchingRule = other.d->matchingRule; |
| 199 | d->customAttributes = other.d->customAttributes; |
| 200 | d->capabilities = other.d->capabilities; |
| 201 | d->capMatchingRule = other.d->capMatchingRule; |
| 202 | |
| 203 | return *this; |
| 204 | } |
| 205 | |
| 206 | /*! |
| 207 | \fn void QServiceFilter::setServiceName(const QString& serviceName) |
| 208 | |
| 209 | The filter only matches implementations which are provided by the service |
| 210 | specified by \a serviceName. |
| 211 | |
| 212 | If the \a serviceName is empty the filter matches any service. |
| 213 | */ |
| 214 | void QServiceFilter::setServiceName(const QString& serviceName) |
| 215 | { |
| 216 | d->service = serviceName; |
| 217 | } |
| 218 | |
| 219 | /*! |
| 220 | \fn void QServiceFilter::setInterface(const QString &interfaceName, const QString& version, QServiceFilter::VersionMatchRule rule) |
| 221 | |
| 222 | Sets the filter to match any interface implementation that implements |
| 223 | \a interfaceName with version \a version. The version is matched |
| 224 | according to the given \a rule. If \a version is not set, the filter matches any version of the |
| 225 | interface implementation. |
| 226 | |
| 227 | This method does nothing if \a version is not a valid version string or |
| 228 | if \a interfaceName is empty. |
| 229 | |
| 230 | A valid version string has the format x.y whereby x and y are positive integer |
| 231 | numbers. |
| 232 | */ |
| 233 | void QServiceFilter::setInterface(const QString &interfaceName, const QString& version, QServiceFilter::VersionMatchRule rule) |
| 234 | { |
| 235 | //unset interface name |
| 236 | if (interfaceName.isEmpty() && version.isEmpty()) |
| 237 | { |
| 238 | d->interface = interfaceName; |
| 239 | d->majorVersion = d->minorVersion = -1; |
| 240 | d->matchingRule = rule; |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | if (interfaceName.isEmpty()) { |
| 245 | qWarning() << "Empty interface name. Ignoring filter details"; |
| 246 | return; |
| 247 | } |
| 248 | |
| 249 | if (version.isEmpty()) { |
| 250 | d->majorVersion = d->minorVersion = -1; |
| 251 | d->matchingRule = rule; |
| 252 | d->interface = interfaceName; |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | // Match x.y as version format. |
| 257 | // This differs from regex in servicemetadata in that 0.x versions are |
| 258 | // accepted for the search filter. |
| 259 | QRegularExpression rx(QLatin1String("^(0+|[1-9][0-9]*)\\.(0+|[1-9][0-9]*)$")); |
| 260 | QRegularExpressionMatch match = rx.match(version); |
| 261 | int pos = match.capturedStart(); |
| 262 | QStringList list = match.capturedTexts(); |
| 263 | bool success = false; |
| 264 | int temp_major = -1; |
| 265 | int temp_minor = -1; |
| 266 | if (pos == 0 && list.count() == 3 |
| 267 | && match.capturedLength() == version.length() ) |
| 268 | { |
| 269 | temp_major = list[1].toInt(&success); |
| 270 | if ( success ) { |
| 271 | temp_minor = list[2].toInt(&success); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | if (success) { |
| 276 | d->majorVersion = temp_major; |
| 277 | d->minorVersion = temp_minor; |
| 278 | d->interface = interfaceName; |
| 279 | d->matchingRule = rule; |
| 280 | } else { |
| 281 | qWarning() << "Invalid version tag" << version << ". Ignoring filter details."; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | /*! |
| 286 | \fn QString QServiceFilter::serviceName() const |
| 287 | |
| 288 | Returns the service name for this filter. |
| 289 | |
| 290 | \sa setServiceName() |
| 291 | */ |
| 292 | QString QServiceFilter::serviceName() const |
| 293 | { |
| 294 | return d->service; |
| 295 | } |
| 296 | |
| 297 | /*! |
| 298 | \fn QString QServiceFilter::interfaceName() const |
| 299 | |
| 300 | Returns the interface name for this filter. |
| 301 | |
| 302 | \sa setInterface() |
| 303 | */ |
| 304 | QString QServiceFilter::interfaceName() const |
| 305 | { |
| 306 | return d->interface; |
| 307 | } |
| 308 | |
| 309 | /*! |
| 310 | \fn int QServiceFilter::majorVersion() const |
| 311 | |
| 312 | Returns the major interface version for this filter. |
| 313 | |
| 314 | \sa setInterface() |
| 315 | */ |
| 316 | int QServiceFilter::majorVersion() const |
| 317 | { |
| 318 | return d->majorVersion; |
| 319 | } |
| 320 | |
| 321 | /*! |
| 322 | \fn int QServiceFilter::minorVersion() const |
| 323 | |
| 324 | Returns the minor interface version for this filter. |
| 325 | |
| 326 | \sa setInterface() |
| 327 | */ |
| 328 | int QServiceFilter::minorVersion() const |
| 329 | { |
| 330 | return d->minorVersion; |
| 331 | } |
| 332 | |
| 333 | /*! |
| 334 | \fn void QServiceFilter::setCustomAttribute(const QString& key, const QString& value) |
| 335 | |
| 336 | The filter only matches implementations which have the custom attribute |
| 337 | \a key with the given \a value. Such constraints are specified via the |
| 338 | \e{<customproperty>} tag within the service xml. |
| 339 | |
| 340 | \sa customAttribute(), clearCustomAttribute() |
| 341 | */ |
| 342 | void QServiceFilter::setCustomAttribute(const QString& key, const QString& value) |
| 343 | { |
| 344 | d->customAttributes.insert(key, value); |
| 345 | } |
| 346 | |
| 347 | /*! |
| 348 | \fn QString QServiceFilter::customAttribute(const QString& key) const |
| 349 | |
| 350 | Returns the value for the custom attribute \a key; otherwise |
| 351 | returns a null string. |
| 352 | |
| 353 | \sa setCustomAttribute(), clearCustomAttribute() |
| 354 | */ |
| 355 | QString QServiceFilter::customAttribute(const QString& key) const |
| 356 | { |
| 357 | return d->customAttributes.value(key); |
| 358 | } |
| 359 | |
| 360 | /*! |
| 361 | \fn void QServiceFilter::clearCustomAttribute(const QString &key) |
| 362 | |
| 363 | Clears the custom attribute \a key from the filter's set of constraints. |
| 364 | If \a key is empty all custom attributes are cleared. |
| 365 | |
| 366 | \sa setCustomAttribute() |
| 367 | */ |
| 368 | void QServiceFilter::clearCustomAttribute(const QString &key) |
| 369 | { |
| 370 | if (key.isEmpty()) |
| 371 | d->customAttributes.clear(); |
| 372 | else |
| 373 | d->customAttributes.remove(key); |
| 374 | } |
| 375 | |
| 376 | /*! |
| 377 | \fn QServiceFilter::VersionMatchRule QServiceFilter::versionMatchRule() const |
| 378 | |
| 379 | Returns the version match rule for this filter. |
| 380 | |
| 381 | \sa setInterface() |
| 382 | */ |
| 383 | QServiceFilter::VersionMatchRule QServiceFilter::versionMatchRule() const |
| 384 | { |
| 385 | return d->matchingRule; |
| 386 | } |
| 387 | |
| 388 | /*! |
| 389 | \fn QList<QString> QServiceFilter::customAttributes() const |
| 390 | |
| 391 | Returns the list of custom keys which have been added to the filter. |
| 392 | */ |
| 393 | QStringList QServiceFilter::customAttributes() const |
| 394 | { |
| 395 | return d->customAttributes.keys(); |
| 396 | } |
| 397 | |
| 398 | /*! |
| 399 | \fn void QServiceFilter::setCapabilities(QServiceFilter::CapabilityMatchRule rule, const QStringList& capabilities ) |
| 400 | |
| 401 | Sets the list of \a capabilities which are used to constrain |
| 402 | searches for services. The capabilities are matched according |
| 403 | to the given \a rule. |
| 404 | |
| 405 | \sa capabilities() |
| 406 | */ |
| 407 | void QServiceFilter::setCapabilities(QServiceFilter::CapabilityMatchRule rule, const QStringList& capabilities ) |
| 408 | { |
| 409 | d->capMatchingRule = rule; |
| 410 | d->capabilities = capabilities; |
| 411 | } |
| 412 | |
| 413 | /*! |
| 414 | \fn QStringList QServiceFilter::capabilities() const |
| 415 | |
| 416 | Returns the list of capabilities which are used to limit services searches. |
| 417 | |
| 418 | The filter matches any services that requires the given or less |
| 419 | capabilities and thus enabling clients to query for services |
| 420 | for which they have the required capabilties. |
| 421 | |
| 422 | \sa setCapabilities(), capabilityMatchRule() |
| 423 | */ |
| 424 | QStringList QServiceFilter::capabilities() const |
| 425 | { |
| 426 | return d->capabilities; |
| 427 | } |
| 428 | |
| 429 | /*! |
| 430 | Returns the capability matching rule for this filter. |
| 431 | |
| 432 | \sa setCapabilities(), capabilities() |
| 433 | */ |
| 434 | QServiceFilter::CapabilityMatchRule QServiceFilter::capabilityMatchRule() const |
| 435 | { |
| 436 | return d->capMatchingRule; |
| 437 | } |
| 438 | |
| 439 | #ifndef QT_NO_DATASTREAM |
| 440 | /*! |
| 441 | \fn QDataStream &operator<<(QDataStream &out, const QServiceFilter &sf) |
| 442 | \relates QServiceFilter |
| 443 | |
| 444 | Writes service filter \a sf to the stream \a out and returns a reference |
| 445 | to the stream. |
| 446 | */ |
| 447 | QDataStream &operator<<(QDataStream &out, const QServiceFilter &sf) |
| 448 | { |
| 449 | const quint32 magicNumber = 0x78AFAFA; |
| 450 | const qint32 mj = sf.d->majorVersion; |
| 451 | const qint32 mn = sf.d->minorVersion; |
| 452 | const qint8 versionrule = (qint32) sf.d->matchingRule; |
| 453 | const qint8 caprule = (qint8) sf.d->capMatchingRule; |
| 454 | const quint16 majorVersion = 1; |
| 455 | const quint16 minorVersion = 0; |
| 456 | |
| 457 | out << magicNumber |
| 458 | << majorVersion |
| 459 | << minorVersion |
| 460 | << sf.d->interface |
| 461 | << sf.d->service |
| 462 | << mj |
| 463 | << mn |
| 464 | << versionrule |
| 465 | << sf.d->customAttributes |
| 466 | << caprule |
| 467 | << sf.d->capabilities; |
| 468 | return out; |
| 469 | } |
| 470 | |
| 471 | /*! |
| 472 | \fn QDataStream &operator>>(QDataStream &in, QServiceFilter &sf) |
| 473 | \relates QServiceFilter |
| 474 | |
| 475 | Reads a service filter into \a sf from the stream \a in and returns a |
| 476 | reference to the stream. |
| 477 | */ |
| 478 | QDataStream &operator>>(QDataStream &in, QServiceFilter &sf) |
| 479 | { |
| 480 | const quint32 magicNumber = 0x78AFAFA; |
| 481 | qint32 mj, mn; |
| 482 | qint8 versionrule, caprule; |
| 483 | |
| 484 | quint32 storedMagicNumber; |
| 485 | in >> storedMagicNumber; |
| 486 | if (storedMagicNumber != magicNumber) { |
| 487 | qWarning() << Q_FUNC_INFO << "Datastream doesn't provide serialized QServiceFilter"; |
| 488 | return in; |
| 489 | } |
| 490 | |
| 491 | const quint16 currentMajorVersion = 1; |
| 492 | quint16 majorVersion = 0; |
| 493 | quint16 minorVersion = 0; |
| 494 | |
| 495 | in >> majorVersion >> minorVersion; |
| 496 | if (majorVersion != currentMajorVersion) { |
| 497 | qWarning() << "Unknown serialization format for QServiceFilter."; |
| 498 | return in; |
| 499 | } |
| 500 | //Allow all minor versions. |
| 501 | |
| 502 | in >> sf.d->interface |
| 503 | >> sf.d->service |
| 504 | >> mj |
| 505 | >> mn |
| 506 | >> versionrule |
| 507 | >> sf.d->customAttributes |
| 508 | >> caprule |
| 509 | >> sf.d->capabilities; |
| 510 | |
| 511 | sf.d->majorVersion = mj; |
| 512 | sf.d->minorVersion = mn; |
| 513 | sf.d->matchingRule = (QServiceFilter::VersionMatchRule) versionrule; |
| 514 | sf.d->capMatchingRule = (QServiceFilter::CapabilityMatchRule) caprule; |
| 515 | |
| 516 | return in; |
| 517 | } |
| 518 | #endif //QT_NO_DATASTREAM |
| 519 | |
| 520 | |
| 521 | QT_END_NAMESPACE |
| 522 |