mirror of
https://bitbucket.org/smil3y/katie.git
synced 2025-02-27 04:13:08 +00:00
185 lines
6.9 KiB
C++
185 lines
6.9 KiB
C++
![]() |
/****************************************************************************
|
||
|
**
|
||
|
** Copyright (C) 2015 The Qt Company Ltd.
|
||
|
** Contact: http://www.qt.io/licensing/
|
||
|
**
|
||
|
** This file is part of the QtDBus module of the Qt Toolkit.
|
||
|
**
|
||
|
** $QT_BEGIN_LICENSE:LGPL$
|
||
|
** Commercial License Usage
|
||
|
** Licensees holding valid commercial Qt licenses may use this file in
|
||
|
** accordance with the commercial license agreement provided with the
|
||
|
** Software or, alternatively, in accordance with the terms contained in
|
||
|
** a written agreement between you and The Qt Company. For licensing terms
|
||
|
** and conditions see http://www.qt.io/terms-conditions. For further
|
||
|
** information use the contact form at http://www.qt.io/contact-us.
|
||
|
**
|
||
|
** GNU Lesser General Public License Usage
|
||
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
||
|
** General Public License version 2.1 or version 3 as published by the Free
|
||
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
||
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
||
|
** following information to ensure the GNU Lesser General Public License
|
||
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
||
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
||
|
**
|
||
|
** As a special exception, The Qt Company gives you certain additional
|
||
|
** rights. These rights are described in The Qt Company LGPL Exception
|
||
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
||
|
**
|
||
|
** GNU General Public License Usage
|
||
|
** Alternatively, this file may be used under the terms of the GNU
|
||
|
** General Public License version 3.0 as published by the Free Software
|
||
|
** Foundation and appearing in the file LICENSE.GPL included in the
|
||
|
** packaging of this file. Please review the following information to
|
||
|
** ensure the GNU General Public License version 3.0 requirements will be
|
||
|
** met: http://www.gnu.org/copyleft/gpl.html.
|
||
|
**
|
||
|
** $QT_END_LICENSE$
|
||
|
**
|
||
|
****************************************************************************/
|
||
|
|
||
|
#include <string.h>
|
||
|
|
||
|
#include <QtCore/qcoreapplication.h>
|
||
|
#include <QtCore/qvariant.h>
|
||
|
#include <QtCore/qmetaobject.h>
|
||
|
|
||
|
#include "qdbusutil_p.h"
|
||
|
#include "qdbusconnection_p.h"
|
||
|
#include "qdbusmetatype_p.h"
|
||
|
#include "qdbusabstractadaptor_p.h" // for QCLASSINFO_DBUS_*
|
||
|
|
||
|
|
||
|
QT_BEGIN_NAMESPACE
|
||
|
|
||
|
int qDBusNameToTypeId(const char *name)
|
||
|
{
|
||
|
int id = static_cast<int>( QVariant::nameToType(name) );
|
||
|
if (id == QVariant::UserType)
|
||
|
id = QMetaType::type(name);
|
||
|
return id;
|
||
|
}
|
||
|
|
||
|
QString qDBusInterfaceFromMetaObject(const QMetaObject *mo)
|
||
|
{
|
||
|
QString interface;
|
||
|
|
||
|
int idx = mo->indexOfClassInfo(QCLASSINFO_DBUS_INTERFACE);
|
||
|
if (idx >= mo->classInfoOffset()) {
|
||
|
interface = QLatin1String(mo->classInfo(idx).value());
|
||
|
} else {
|
||
|
interface = QLatin1String(mo->className());
|
||
|
interface.replace(QLatin1String("::"), QLatin1String("."));
|
||
|
|
||
|
if (interface.startsWith(QLatin1String("QDBus"))) {
|
||
|
interface.prepend(QLatin1String("org.qtproject.QtDBus."));
|
||
|
} else if (interface.startsWith(QLatin1Char('Q')) &&
|
||
|
interface.length() >= 2 && interface.at(1).isUpper()) {
|
||
|
// assume it's Qt
|
||
|
interface.prepend(QLatin1String("org.qtproject.Qt."));
|
||
|
} else if (!QCoreApplication::instance()||
|
||
|
QCoreApplication::instance()->applicationName().isEmpty()) {
|
||
|
interface.prepend(QLatin1String("local."));
|
||
|
} else {
|
||
|
interface.prepend(QLatin1Char('.')).prepend(QCoreApplication::instance()->applicationName());
|
||
|
QStringList domainName =
|
||
|
QCoreApplication::instance()->organizationDomain().split(QLatin1Char('.'),
|
||
|
QString::SkipEmptyParts);
|
||
|
if (domainName.isEmpty())
|
||
|
interface.prepend(QLatin1String("local."));
|
||
|
else
|
||
|
for (int i = 0; i < domainName.count(); ++i)
|
||
|
interface.prepend(QLatin1Char('.')).prepend(domainName.at(i));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return interface;
|
||
|
}
|
||
|
|
||
|
bool qDBusInterfaceInObject(QObject *obj, const QString &interface_name)
|
||
|
{
|
||
|
const QMetaObject *mo = obj->metaObject();
|
||
|
for ( ; mo != &QObject::staticMetaObject; mo = mo->superClass())
|
||
|
if (interface_name == qDBusInterfaceFromMetaObject(mo))
|
||
|
return true;
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
// calculates the metatypes for the method
|
||
|
// the slot must have the parameters in the following form:
|
||
|
// - zero or more value or const-ref parameters of any kind
|
||
|
// - zero or one const ref of QDBusMessage
|
||
|
// - zero or more non-const ref parameters
|
||
|
// No parameter may be a template.
|
||
|
// this function returns -1 if the parameters don't match the above form
|
||
|
// this function returns the number of *input* parameters, including the QDBusMessage one if any
|
||
|
// this function does not check the return type, so metaTypes[0] is always 0 and always present
|
||
|
// metaTypes.count() >= retval + 1 in all cases
|
||
|
//
|
||
|
// sig must be the normalised signature for the method
|
||
|
int qDBusParametersForMethod(const QMetaMethod &mm, QList<int>& metaTypes)
|
||
|
{
|
||
|
QDBusMetaTypeId::init();
|
||
|
|
||
|
QList<QByteArray> parameterTypes = mm.parameterTypes();
|
||
|
metaTypes.clear();
|
||
|
|
||
|
metaTypes.append(0); // return type
|
||
|
int inputCount = 0;
|
||
|
bool seenMessage = false;
|
||
|
QList<QByteArray>::ConstIterator it = parameterTypes.constBegin();
|
||
|
QList<QByteArray>::ConstIterator end = parameterTypes.constEnd();
|
||
|
for ( ; it != end; ++it) {
|
||
|
const QByteArray &type = *it;
|
||
|
if (type.endsWith('*')) {
|
||
|
//qWarning("Could not parse the method '%s'", mm.signature());
|
||
|
// pointer?
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
if (type.endsWith('&')) {
|
||
|
QByteArray basictype = type;
|
||
|
basictype.truncate(type.length() - 1);
|
||
|
|
||
|
int id = qDBusNameToTypeId(basictype);
|
||
|
if (id == 0) {
|
||
|
//qWarning("Could not parse the method '%s'", mm.signature());
|
||
|
// invalid type in method parameter list
|
||
|
return -1;
|
||
|
} else if (QDBusMetaType::typeToSignature(id) == 0)
|
||
|
return -1;
|
||
|
|
||
|
metaTypes.append( id );
|
||
|
seenMessage = true; // it cannot appear anymore anyways
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
if (seenMessage) { // && !type.endsWith('&')
|
||
|
//qWarning("Could not parse the method '%s'", mm.signature());
|
||
|
// non-output parameters after message or after output params
|
||
|
return -1; // not allowed
|
||
|
}
|
||
|
|
||
|
int id = qDBusNameToTypeId(type);
|
||
|
if (id == 0) {
|
||
|
//qWarning("Could not parse the method '%s'", mm.signature());
|
||
|
// invalid type in method parameter list
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
if (id == QDBusMetaTypeId::message)
|
||
|
seenMessage = true;
|
||
|
else if (QDBusMetaType::typeToSignature(id) == 0)
|
||
|
return -1;
|
||
|
|
||
|
metaTypes.append(id);
|
||
|
++inputCount;
|
||
|
}
|
||
|
|
||
|
return inputCount;
|
||
|
}
|
||
|
|
||
|
QT_END_NAMESPACE
|
||
|
|