src/dbus/qdbusmisc.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtDBus module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <string.h>
       
    43 
       
    44 #include <QtCore/qcoreapplication.h>
       
    45 #include <QtCore/qvariant.h>
       
    46 #include <QtCore/qmetaobject.h>
       
    47 
       
    48 #include "qdbusutil_p.h"
       
    49 #include "qdbusconnection_p.h"
       
    50 #include "qdbusmetatype_p.h"
       
    51 #include "qdbusabstractadaptor_p.h" // for QCLASSINFO_DBUS_*
       
    52 
       
    53 QT_BEGIN_NAMESPACE
       
    54 
       
    55 bool qDBusCheckAsyncTag(const char *tag)
       
    56 {
       
    57     static const char noReplyTag[] = "Q_NOREPLY";
       
    58     if (!tag || !*tag)
       
    59         return false;
       
    60 
       
    61     const char *p = strstr(tag, noReplyTag);
       
    62     if (p != NULL &&
       
    63         (p == tag || *(p-1) == ' ') &&
       
    64         (p[sizeof noReplyTag - 1] == '\0' || p[sizeof noReplyTag - 1] == ' '))
       
    65         return true;
       
    66 
       
    67     return false;
       
    68 }
       
    69 
       
    70 int qDBusNameToTypeId(const char *name)
       
    71 {
       
    72     int id = static_cast<int>( QVariant::nameToType(name) );
       
    73     if (id == QVariant::UserType)
       
    74         id = QMetaType::type(name);
       
    75     return id;
       
    76 }
       
    77 
       
    78 QString qDBusInterfaceFromMetaObject(const QMetaObject *mo)
       
    79 {
       
    80     QString interface;
       
    81 
       
    82     int idx = mo->indexOfClassInfo(QCLASSINFO_DBUS_INTERFACE);
       
    83     if (idx >= mo->classInfoOffset()) {
       
    84         interface = QLatin1String(mo->classInfo(idx).value());
       
    85     } else {
       
    86         interface = QLatin1String(mo->className());
       
    87         interface.replace(QLatin1String("::"), QLatin1String("."));
       
    88 
       
    89         if (interface.startsWith(QLatin1String("QDBus"))) {
       
    90             interface.prepend(QLatin1String("com.trolltech.QtDBus."));
       
    91         } else if (interface.startsWith(QLatin1Char('Q')) &&
       
    92                    interface.length() >= 2 && interface.at(1).isUpper()) {
       
    93             // assume it's Qt
       
    94             interface.prepend(QLatin1String("com.trolltech.Qt."));
       
    95         } else if (!QCoreApplication::instance()||
       
    96                    QCoreApplication::instance()->applicationName().isEmpty()) {
       
    97             interface.prepend(QLatin1String("local."));
       
    98          } else {
       
    99             interface.prepend(QLatin1Char('.')).prepend(QCoreApplication::instance()->applicationName());
       
   100             QStringList domainName =
       
   101                 QCoreApplication::instance()->organizationDomain().split(QLatin1Char('.'),
       
   102                                                                          QString::SkipEmptyParts);
       
   103             if (domainName.isEmpty())
       
   104                  interface.prepend(QLatin1String("local."));
       
   105             else
       
   106                 for (int i = 0; i < domainName.count(); ++i)
       
   107                     interface.prepend(QLatin1Char('.')).prepend(domainName.at(i));
       
   108          }
       
   109      }
       
   110 
       
   111     return interface;
       
   112 }
       
   113 
       
   114 bool qDBusInterfaceInObject(QObject *obj, const QString &interface_name)
       
   115 {
       
   116     const QMetaObject *mo = obj->metaObject();
       
   117     for ( ; mo != &QObject::staticMetaObject; mo = mo->superClass())
       
   118         if (interface_name == qDBusInterfaceFromMetaObject(mo))
       
   119             return true;
       
   120     return false;
       
   121 }
       
   122 
       
   123 // calculates the metatypes for the method
       
   124 // the slot must have the parameters in the following form:
       
   125 //  - zero or more value or const-ref parameters of any kind
       
   126 //  - zero or one const ref of QDBusMessage
       
   127 //  - zero or more non-const ref parameters
       
   128 // No parameter may be a template.
       
   129 // this function returns -1 if the parameters don't match the above form
       
   130 // this function returns the number of *input* parameters, including the QDBusMessage one if any
       
   131 // this function does not check the return type, so metaTypes[0] is always 0 and always present
       
   132 // metaTypes.count() >= retval + 1 in all cases
       
   133 //
       
   134 // sig must be the normalised signature for the method
       
   135 int qDBusParametersForMethod(const QMetaMethod &mm, QList<int>& metaTypes)
       
   136 {
       
   137     QDBusMetaTypeId::init();
       
   138 
       
   139     QList<QByteArray> parameterTypes = mm.parameterTypes();
       
   140     metaTypes.clear();
       
   141 
       
   142     metaTypes.append(0);        // return type
       
   143     int inputCount = 0;
       
   144     bool seenMessage = false;
       
   145     QList<QByteArray>::ConstIterator it = parameterTypes.constBegin();
       
   146     QList<QByteArray>::ConstIterator end = parameterTypes.constEnd();
       
   147     for ( ; it != end; ++it) {
       
   148         const QByteArray &type = *it;
       
   149         if (type.endsWith('*')) {
       
   150             //qWarning("Could not parse the method '%s'", mm.signature());
       
   151             // pointer?
       
   152             return -1;
       
   153         }
       
   154 
       
   155         if (type.endsWith('&')) {
       
   156             QByteArray basictype = type;
       
   157             basictype.truncate(type.length() - 1);
       
   158 
       
   159             int id = qDBusNameToTypeId(basictype);
       
   160             if (id == 0) {
       
   161                 //qWarning("Could not parse the method '%s'", mm.signature());
       
   162                 // invalid type in method parameter list
       
   163                 return -1;
       
   164             } else if (QDBusMetaType::typeToSignature(id) == 0)
       
   165                 return -1;
       
   166 
       
   167             metaTypes.append( id );
       
   168             seenMessage = true; // it cannot appear anymore anyways
       
   169             continue;
       
   170         }
       
   171 
       
   172         if (seenMessage) {      // && !type.endsWith('&')
       
   173             //qWarning("Could not parse the method '%s'", mm.signature());
       
   174             // non-output parameters after message or after output params
       
   175             return -1;          // not allowed
       
   176         }
       
   177 
       
   178         int id = qDBusNameToTypeId(type);
       
   179         if (id == 0) {
       
   180             //qWarning("Could not parse the method '%s'", mm.signature());
       
   181             // invalid type in method parameter list
       
   182             return -1;
       
   183         }
       
   184 
       
   185         if (id == QDBusMetaTypeId::message)
       
   186             seenMessage = true;
       
   187         else if (QDBusMetaType::typeToSignature(id) == 0)
       
   188             return -1;
       
   189 
       
   190         metaTypes.append(id);
       
   191         ++inputCount;
       
   192     }
       
   193 
       
   194     return inputCount;
       
   195 }
       
   196 
       
   197 QT_END_NAMESPACE