| 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 "qserviceinterfacedescriptor_p.h" |
| 41 | #ifndef QT_NO_DATASTREAM |
| 42 | #include <qdatastream.h> |
| 43 | #endif |
| 44 | |
| 45 | #include <QDebug> |
| 46 | #include <QStringList> |
| 47 | |
| 48 | QT_BEGIN_NAMESPACE |
| 49 | |
| 50 | /*! |
| 51 | \class QServiceInterfaceDescriptor |
| 52 | \ingroup servicefw |
| 53 | \inmodule QtServiceFramework |
| 54 | \brief The QServiceInterfaceDescriptor class identifies a service implementation. |
| 55 | |
| 56 | A service can implement multiple interfaces and each interface can have multiple implementations. |
| 57 | The QServiceInterfaceDescriptor class enscapsulates this information, as illustrated |
| 58 | by the diagram below. |
| 59 | |
| 60 | \image qserviceinterfacedescriptor.png Service-Interface-Implementation |
| 61 | |
| 62 | The major version tag indicates the interface version and the minor version tag identifies the implementation |
| 63 | version. Subsequent versions of the same interface must be binary compatible to previous versions |
| 64 | of the same interface. |
| 65 | |
| 66 | In the above example service A and B implement the interface \e com.nokia.qt.x. |
| 67 | In fact Service A provides two different implementations for the very same interface. |
| 68 | This is indicated by the changed minor version number. Although Service B is |
| 69 | using the same interface it's implementation actually utilizes the second version of |
| 70 | the interface \e com.nokia.qt.x. Binary compatibility guarantees that clients |
| 71 | who know version 1 can utilize version 2. If an existing interface has to be changed |
| 72 | in a non-compatible way a new interface (name) is required. |
| 73 | |
| 74 | \section1 Namespaces |
| 75 | |
| 76 | A QServiceInterfaceDescriptor (the quadruble of service name, |
| 77 | interface name, interface version and implementation version) uniquely |
| 78 | identifies a service implementation on a device. Interface names follow |
| 79 | the java namespace convention. |
| 80 | |
| 81 | The namespace \e com.nokia.qt.* is reserved for future Qt development. |
| 82 | |
| 83 | \sa QServiceFilter, QServiceManager |
| 84 | */ |
| 85 | |
| 86 | /*! |
| 87 | \enum QServiceInterfaceDescriptor::Attribute |
| 88 | |
| 89 | This enum describes the possible attribute types which can be attached |
| 90 | to a QServiceInterfaceDescriptor. |
| 91 | |
| 92 | \value Capabilities The capabilities attribute is a QStringList and |
| 93 | describes the capabilities that a service client |
| 94 | would require to use the service if capability |
| 95 | checks are enforced. |
| 96 | \value Location This attribute points to either the location |
| 97 | where the plug-in providing this service is stored or |
| 98 | where the name of the service IPC path is found. |
| 99 | If the service is plug-in based the location is the |
| 100 | name and/or path of the plugin. If the service is |
| 101 | IPC based the location is the name of the socket address. |
| 102 | \value ServiceDescription This attribute provides a general description for |
| 103 | the service. |
| 104 | \value InterfaceDescription This attribute provides a description for the interface |
| 105 | implementation. |
| 106 | \value ServiceType This attribute specifies the QService::Type that the |
| 107 | service is being provided. |
| 108 | */ |
| 109 | |
| 110 | /*! |
| 111 | Creates a new QServiceInterfaceDescriptor. |
| 112 | */ |
| 113 | QServiceInterfaceDescriptor::QServiceInterfaceDescriptor() |
| 114 | : d(0) |
| 115 | { |
| 116 | } |
| 117 | |
| 118 | /*! |
| 119 | Destroys the QServiceInterfaceDescriptor object. |
| 120 | */ |
| 121 | QServiceInterfaceDescriptor::~QServiceInterfaceDescriptor() |
| 122 | { |
| 123 | if (d) |
| 124 | delete d; |
| 125 | } |
| 126 | |
| 127 | /*! |
| 128 | Creates a copy of QServiceInterfaceDescriptor contained in \a other. |
| 129 | */ |
| 130 | QServiceInterfaceDescriptor::QServiceInterfaceDescriptor(const QServiceInterfaceDescriptor& other) |
| 131 | : d(0) |
| 132 | { |
| 133 | (*this) = other; //use assignment operator |
| 134 | } |
| 135 | |
| 136 | /*! |
| 137 | \fn QServiceInterfaceDescriptor& QServiceInterfaceDescriptor::operator=(const QServiceInterfaceDescriptor& other) |
| 138 | |
| 139 | Copies the content of the QServiceInterfaceDescriptor object contained |
| 140 | in \a other into this one. |
| 141 | */ |
| 142 | QServiceInterfaceDescriptor& QServiceInterfaceDescriptor::operator=(const QServiceInterfaceDescriptor& other) |
| 143 | { |
| 144 | if (&other == this) |
| 145 | return *this; |
| 146 | |
| 147 | if ( !other.isValid() ) { |
| 148 | if (d) |
| 149 | delete d; |
| 150 | d = 0; |
| 151 | return *this; |
| 152 | } |
| 153 | |
| 154 | if (!d) |
| 155 | d = new QServiceInterfaceDescriptorPrivate; |
| 156 | |
| 157 | (*d) = *(other.d); |
| 158 | return *this; |
| 159 | } |
| 160 | |
| 161 | /*! |
| 162 | \fn bool QServiceInterfaceDescriptor::operator==(const QServiceInterfaceDescriptor& other) const |
| 163 | |
| 164 | Compares a QServiceInterfaceDescriptor to \a other. Returns true if they |
| 165 | are equal and false otherwise. |
| 166 | */ |
| 167 | bool QServiceInterfaceDescriptor::operator==(const QServiceInterfaceDescriptor& other) const |
| 168 | { |
| 169 | if (isValid() ^ other.isValid()) |
| 170 | return false; |
| 171 | |
| 172 | if (!d) |
| 173 | return true; |
| 174 | |
| 175 | if ((*d) == *(other.d)) |
| 176 | return true; |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | /*! |
| 181 | \fn bool QServiceInterfaceDescriptor::operator!=(const QServiceInterfaceDescriptor& other) const |
| 182 | |
| 183 | Compares a QServiceInterfaceDescriptor to \a other. Returns true |
| 184 | if they are not equal and false otherwise. |
| 185 | */ |
| 186 | |
| 187 | /*! |
| 188 | \fn bool QServiceInterfaceDescriptor::isValid() const |
| 189 | |
| 190 | Returns true if this descriptor is valid; otherwise returns false. |
| 191 | */ |
| 192 | bool QServiceInterfaceDescriptor::isValid() const |
| 193 | { |
| 194 | return d ? true : false; |
| 195 | } |
| 196 | |
| 197 | /*! |
| 198 | \fn bool QServiceInterfaceDescriptor::scope() const |
| 199 | |
| 200 | Returns true if this implementation is provided for all users on the system. |
| 201 | |
| 202 | \sa QService::Scope |
| 203 | */ |
| 204 | QService::Scope QServiceInterfaceDescriptor::scope() const |
| 205 | { |
| 206 | return d ? d->scope : QService::UserScope; |
| 207 | } |
| 208 | |
| 209 | /*! |
| 210 | \fn QString QServiceInterfaceDescriptor::serviceName() const |
| 211 | |
| 212 | Returns the name of service that provides this implementation. |
| 213 | */ |
| 214 | QString QServiceInterfaceDescriptor::serviceName() const |
| 215 | { |
| 216 | return d ? d->serviceName : QString(); |
| 217 | } |
| 218 | |
| 219 | /*! |
| 220 | \fn QString QServiceInterfaceDescriptor::interfaceName() const |
| 221 | |
| 222 | Returns the name of the interface that is implemented. |
| 223 | */ |
| 224 | QString QServiceInterfaceDescriptor::interfaceName() const |
| 225 | { |
| 226 | return d ? d->interfaceName : QString(); |
| 227 | } |
| 228 | |
| 229 | /*! |
| 230 | \fn int QServiceInterfaceDescriptor::majorVersion() const |
| 231 | |
| 232 | Returns the version of the interface. |
| 233 | |
| 234 | Subsequent versions of an interface are binary compatible |
| 235 | to previous versions of the same interface. If an interface |
| 236 | is broken it must use a new interface name. |
| 237 | */ |
| 238 | int QServiceInterfaceDescriptor::majorVersion() const |
| 239 | { |
| 240 | return d ? d->major : -1; |
| 241 | } |
| 242 | |
| 243 | /*! |
| 244 | \fn int QServiceInterfaceDescriptor::minorVersion() const |
| 245 | |
| 246 | Returns the version of the implementation. |
| 247 | */ |
| 248 | int QServiceInterfaceDescriptor::minorVersion() const |
| 249 | { |
| 250 | return d ? d->minor : -1; |
| 251 | } |
| 252 | |
| 253 | /*! |
| 254 | \fn QVariant QServiceInterfaceDescriptor::attribute(QServiceInterfaceDescriptor::Attribute which) const |
| 255 | |
| 256 | Returns the value for the attribute \a which; otherwise returns |
| 257 | an invalid QVariant. |
| 258 | */ |
| 259 | QVariant QServiceInterfaceDescriptor::attribute(QServiceInterfaceDescriptor::Attribute which) const |
| 260 | { |
| 261 | if (d) |
| 262 | return d->attributes.value(which); |
| 263 | return QVariant(); |
| 264 | } |
| 265 | |
| 266 | /*! |
| 267 | \fn QString QServiceInterfaceDescriptor::customAttribute(const QString& which) const |
| 268 | |
| 269 | Returns the value for the custom attribute \a which; otherwise |
| 270 | returns a null string. |
| 271 | */ |
| 272 | QString QServiceInterfaceDescriptor::customAttribute(const QString& which) const |
| 273 | { |
| 274 | if (d) |
| 275 | return d->customAttributes[which]; |
| 276 | return QString(); |
| 277 | } |
| 278 | |
| 279 | /*! |
| 280 | Returns a list of custom attributes attached to the service. |
| 281 | */ |
| 282 | QStringList QServiceInterfaceDescriptor::customAttributes() const |
| 283 | { |
| 284 | if (d) |
| 285 | return d->customAttributes.keys(); |
| 286 | return QStringList(); |
| 287 | } |
| 288 | |
| 289 | #ifndef QT_NO_DEBUG_STREAM |
| 290 | QDebug operator<<(QDebug dbg, const QServiceInterfaceDescriptor &desc) |
| 291 | { |
| 292 | if (desc.isValid()) { |
| 293 | QString serviceInterface = QString(QLatin1String("%1 %2.%3")).arg(desc.interfaceName()) |
| 294 | .arg(desc.majorVersion() < 0 ? '?' : desc.majorVersion()) |
| 295 | .arg(desc.minorVersion() < 0 ? '?' : desc.minorVersion()); |
| 296 | dbg.nospace() << "QServiceInterfaceDescriptor("; |
| 297 | dbg.nospace() << "service=" << desc.serviceName() << ", "; |
| 298 | dbg.nospace() << "interface=" << serviceInterface; |
| 299 | dbg.nospace() << ")"; |
| 300 | } else { |
| 301 | dbg.nospace() << "QServiceInterfaceDescriptor(invalid)"; |
| 302 | } |
| 303 | return dbg.space(); |
| 304 | } |
| 305 | #endif |
| 306 | |
| 307 | #ifndef QT_NO_DATASTREAM |
| 308 | |
| 309 | QDataStream &operator<<(QDataStream &out, const QServiceInterfaceDescriptor::Attribute &k) |
| 310 | { |
| 311 | out << qint8(k); |
| 312 | return out; |
| 313 | } |
| 314 | |
| 315 | QDataStream &operator>>(QDataStream &in, QServiceInterfaceDescriptor::Attribute &k) |
| 316 | { |
| 317 | quint8 key; |
| 318 | in >> key; |
| 319 | k = (QServiceInterfaceDescriptor::Attribute)key; |
| 320 | return in; |
| 321 | } |
| 322 | /*! |
| 323 | \fn QDataStream &operator<<(QDataStream &out, const QServiceInterfaceDescriptor &dc) |
| 324 | \relates QServiceInterfaceDescriptor |
| 325 | |
| 326 | Writes service interface descriptor \a dc to the stream \a out and returns a reference |
| 327 | to the stream. |
| 328 | */ |
| 329 | |
| 330 | QDataStream &operator<<(QDataStream &out, const QServiceInterfaceDescriptor &dc) |
| 331 | { |
| 332 | const quint32 magicNumber = 0x77AFAFA; |
| 333 | const quint16 majorVersion = 1; |
| 334 | const quint16 minorVersion = 0; |
| 335 | const qint8 valid = dc.isValid(); |
| 336 | out << magicNumber << majorVersion << minorVersion; |
| 337 | out << valid; |
| 338 | if (valid) { |
| 339 | out << dc.d->serviceName; |
| 340 | out << dc.d->interfaceName; |
| 341 | out << dc.d->major; |
| 342 | out << dc.d->minor; |
| 343 | out << dc.d->attributes; |
| 344 | out << dc.d->customAttributes; |
| 345 | out << (qint8)dc.d->scope; |
| 346 | } |
| 347 | return out; |
| 348 | } |
| 349 | |
| 350 | /*! |
| 351 | \fn QDataStream &operator>>(QDataStream &in, QServiceInterfaceDescriptor &dc) |
| 352 | \relates QServiceInterfaceDescriptor |
| 353 | |
| 354 | Reads a service interface descriptor into \a dc from the stream \a in and returns a |
| 355 | reference to the stream. |
| 356 | */ |
| 357 | QDataStream &operator>>(QDataStream &in, QServiceInterfaceDescriptor &dc) |
| 358 | { |
| 359 | const quint32 magicNumber = 0x77AFAFA; |
| 360 | quint32 storedMagicNumber; |
| 361 | in >> storedMagicNumber; |
| 362 | if (storedMagicNumber != magicNumber) { |
| 363 | qWarning() << "Datastream doesn't provide searialized QServiceInterfaceDescriptor"; |
| 364 | return in; |
| 365 | } |
| 366 | |
| 367 | const quint16 currentMajorVersion = 1; |
| 368 | quint16 majorVersion = 0; |
| 369 | quint16 minorVersion = 0; |
| 370 | |
| 371 | in >> majorVersion >> minorVersion; |
| 372 | if (majorVersion != currentMajorVersion) { |
| 373 | qWarning() << "Unknown serialization format for QServiceInterfaceDescriptor."; |
| 374 | return in; |
| 375 | } |
| 376 | //Allow all minor versions. |
| 377 | |
| 378 | qint8 valid; |
| 379 | in >> valid; |
| 380 | if (valid) { |
| 381 | if (!dc.isValid()) |
| 382 | dc.d = new QServiceInterfaceDescriptorPrivate; |
| 383 | in >> dc.d->serviceName; |
| 384 | in >> dc.d->interfaceName; |
| 385 | in >> dc.d->major; |
| 386 | in >> dc.d->minor; |
| 387 | in >> dc.d->attributes; |
| 388 | in >> dc.d->customAttributes; |
| 389 | in >> valid; |
| 390 | dc.d->scope = (QService::Scope) valid; |
| 391 | } else { //input stream contains invalid descriptor |
| 392 | //use assignment operator |
| 393 | dc = QServiceInterfaceDescriptor(); |
| 394 | } |
| 395 | |
| 396 | return in; |
| 397 | } |
| 398 | #endif //QT_NO_DATASTREAM |
| 399 | |
| 400 | |
| 401 | |
| 402 | QT_END_NAMESPACE |
| 403 |