| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtDBus 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 <string.h> |
| 41 | |
| 42 | #ifndef QT_BOOTSTRAPPED |
| 43 | #include <QtCore/qcoreapplication.h> |
| 44 | #include <QtCore/qlist.h> |
| 45 | #include <QtCore/qmetaobject.h> |
| 46 | #include <QtCore/qvariant.h> |
| 47 | |
| 48 | #include "qdbusutil_p.h" |
| 49 | #include "qdbusconnection_p.h" |
| 50 | #include "qdbusabstractadaptor_p.h" // for QCLASSINFO_DBUS_* |
| 51 | #endif |
| 52 | #include "qdbusmetatype_p.h" |
| 53 | |
| 54 | #ifndef QT_NO_DBUS |
| 55 | |
| 56 | QT_BEGIN_NAMESPACE |
| 57 | |
| 58 | bool qDBusCheckAsyncTag(const char *tag) |
| 59 | { |
| 60 | static const char noReplyTag[] = "Q_NOREPLY" ; |
| 61 | if (!tag || !*tag) |
| 62 | return false; |
| 63 | |
| 64 | const char *p = strstr(tag, noReplyTag); |
| 65 | if (p != nullptr && |
| 66 | (p == tag || *(p-1) == ' ') && |
| 67 | (p[sizeof noReplyTag - 1] == '\0' || p[sizeof noReplyTag - 1] == ' ')) |
| 68 | return true; |
| 69 | |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | #ifndef QT_BOOTSTRAPPED |
| 74 | |
| 75 | QString qDBusInterfaceFromMetaObject(const QMetaObject *mo) |
| 76 | { |
| 77 | QString interface; |
| 78 | |
| 79 | int idx = mo->indexOfClassInfo(QCLASSINFO_DBUS_INTERFACE); |
| 80 | if (idx >= mo->classInfoOffset()) { |
| 81 | interface = QLatin1String(mo->classInfo(idx).value()); |
| 82 | } else { |
| 83 | interface = QLatin1String(mo->className()); |
| 84 | interface.replace(QLatin1String("::" ), QLatin1String("." )); |
| 85 | |
| 86 | if (interface.startsWith(QLatin1String("QDBus" ))) { |
| 87 | interface.prepend(QLatin1String("org.qtproject.QtDBus." )); |
| 88 | } else if (interface.startsWith(QLatin1Char('Q')) && |
| 89 | interface.length() >= 2 && interface.at(1).isUpper()) { |
| 90 | // assume it's Qt |
| 91 | interface.prepend(QLatin1String("org.qtproject.Qt." )); |
| 92 | } else if (!QCoreApplication::instance()|| |
| 93 | QCoreApplication::instance()->applicationName().isEmpty()) { |
| 94 | interface.prepend(QLatin1String("local." )); |
| 95 | } else { |
| 96 | interface.prepend(QLatin1Char('.')).prepend(QCoreApplication::instance()->applicationName()); |
| 97 | const QString organizationDomain = QCoreApplication::instance()->organizationDomain(); |
| 98 | const auto domainName = QStringView{organizationDomain}.split(QLatin1Char('.'), Qt::SkipEmptyParts); |
| 99 | if (domainName.isEmpty()) { |
| 100 | interface.prepend(QLatin1String("local." )); |
| 101 | } else { |
| 102 | QString composedDomain; |
| 103 | // + 1 for additional dot, e.g. organizationDomain equals "example.com", |
| 104 | // then composedDomain will be equal "com.example." |
| 105 | composedDomain.reserve(organizationDomain.size() + 1); |
| 106 | for (auto it = domainName.rbegin(), end = domainName.rend(); it != end; ++it) |
| 107 | composedDomain += *it + QLatin1Char('.'); |
| 108 | |
| 109 | interface.prepend(composedDomain); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return interface; |
| 115 | } |
| 116 | |
| 117 | bool qDBusInterfaceInObject(QObject *obj, const QString &interface_name) |
| 118 | { |
| 119 | const QMetaObject *mo = obj->metaObject(); |
| 120 | for ( ; mo != &QObject::staticMetaObject; mo = mo->superClass()) |
| 121 | if (interface_name == qDBusInterfaceFromMetaObject(mo)) |
| 122 | return true; |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | // calculates the metatypes for the method |
| 127 | // the slot must have the parameters in the following form: |
| 128 | // - zero or more value or const-ref parameters of any kind |
| 129 | // - zero or one const ref of QDBusMessage |
| 130 | // - zero or more non-const ref parameters |
| 131 | // No parameter may be a template. |
| 132 | // this function returns -1 if the parameters don't match the above form |
| 133 | // this function returns the number of *input* parameters, including the QDBusMessage one if any |
| 134 | // this function does not check the return type, so metaTypes[0] is always 0 and always present |
| 135 | // metaTypes.count() >= retval + 1 in all cases |
| 136 | // |
| 137 | // sig must be the normalised signature for the method |
| 138 | int qDBusParametersForMethod(const QMetaMethod &mm, QList<QMetaType> &metaTypes, QString &errorMsg) |
| 139 | { |
| 140 | return qDBusParametersForMethod(mm.parameterTypes(), metaTypes, errorMsg); |
| 141 | } |
| 142 | |
| 143 | #endif // QT_BOOTSTRAPPED |
| 144 | |
| 145 | int qDBusParametersForMethod(const QList<QByteArray> ¶meterTypes, QList<QMetaType> &metaTypes, |
| 146 | QString &errorMsg) |
| 147 | { |
| 148 | QDBusMetaTypeId::init(); |
| 149 | metaTypes.clear(); |
| 150 | |
| 151 | metaTypes.append(QMetaType()); // return type |
| 152 | int inputCount = 0; |
| 153 | bool seenMessage = false; |
| 154 | QList<QByteArray>::ConstIterator it = parameterTypes.constBegin(); |
| 155 | QList<QByteArray>::ConstIterator end = parameterTypes.constEnd(); |
| 156 | for ( ; it != end; ++it) { |
| 157 | QByteArray type = *it; |
| 158 | if (type.endsWith('*')) { |
| 159 | errorMsg = QLatin1String("Pointers are not supported: " ) + QLatin1String(type); |
| 160 | return -1; |
| 161 | } |
| 162 | |
| 163 | if (type.endsWith('&')) { |
| 164 | QByteArray basictype = type; |
| 165 | basictype.truncate(type.length() - 1); |
| 166 | |
| 167 | QMetaType id = QMetaType::fromName(basictype); |
| 168 | if (!id.isValid()) { |
| 169 | errorMsg = QLatin1String("Unregistered output type in parameter list: " ) + QLatin1String(type); |
| 170 | return -1; |
| 171 | } else if (QDBusMetaType::typeToSignature(id) == nullptr) |
| 172 | return -1; |
| 173 | |
| 174 | metaTypes.append(id); |
| 175 | seenMessage = true; // it cannot appear anymore anyways |
| 176 | continue; |
| 177 | } |
| 178 | |
| 179 | if (seenMessage) { // && !type.endsWith('&') |
| 180 | errorMsg = QLatin1String("Invalid method, non-output parameters after message or after output parameters: " ) + QLatin1String(type); |
| 181 | return -1; // not allowed |
| 182 | } |
| 183 | |
| 184 | if (type.startsWith("QVector<" )) |
| 185 | type = "QList<" + type.mid(sizeof("QVector<" ) - 1); |
| 186 | |
| 187 | QMetaType id = QMetaType::fromName(type); |
| 188 | #ifdef QT_BOOTSTRAPPED |
| 189 | // in bootstrap mode QDBusMessage isn't included, thus we need to resolve it manually here |
| 190 | if (type == "QDBusMessage" ) { |
| 191 | id = QDBusMetaTypeId::message(); |
| 192 | } |
| 193 | #endif |
| 194 | |
| 195 | if (!id.isValid()) { |
| 196 | errorMsg = QLatin1String("Unregistered input type in parameter list: " ) + QLatin1String(type); |
| 197 | return -1; |
| 198 | } |
| 199 | |
| 200 | if (id == QDBusMetaTypeId::message()) |
| 201 | seenMessage = true; |
| 202 | else if (QDBusMetaType::typeToSignature(id) == nullptr) { |
| 203 | errorMsg = QLatin1String("Type not registered with QtDBus in parameter list: " ) + QLatin1String(type); |
| 204 | return -1; |
| 205 | } |
| 206 | |
| 207 | metaTypes.append(id); |
| 208 | ++inputCount; |
| 209 | } |
| 210 | |
| 211 | return inputCount; |
| 212 | } |
| 213 | |
| 214 | QT_END_NAMESPACE |
| 215 | |
| 216 | #endif // QT_NO_DBUS |
| 217 | |