tools/qdbus/src/qdbusutil.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.
00004 **
00005 ** This file is part of the tools applications of the Qt Toolkit.
00006 **
00007 ** This file may be used under the terms of the GNU General Public
00008 ** License version 2.0 as published by the Free Software Foundation
00009 ** and appearing in the file LICENSE.GPL included in the packaging of
00010 ** this file.  Please review the following information to ensure GNU
00011 ** General Public Licensing requirements will be met:
00012 ** http://www.trolltech.com/products/qt/opensource.html
00013 **
00014 ** If you are unsure which license is appropriate for your use, please
00015 ** review the following information:
00016 ** http://www.trolltech.com/products/qt/licensing.html or contact the
00017 ** sales department at sales@trolltech.com.
00018 **
00019 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00020 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00021 **
00022 ****************************************************************************/
00023 
00024 #include "qdbusutil_p.h"
00025 
00026 #include <dbus/dbus.h>
00027 
00028 #include <QtCore/qstringlist.h>
00029 #include <QtCore/qregexp.h>
00030 
00031 #include "qdbusargument.h"
00032 
00041 namespace QDBusUtil
00042 {
00048     bool isValidPartOfObjectPath(const QString &part)
00049     {
00050         if (part.isEmpty())
00051             return false;       // can't be valid if it's empty
00052 
00053         const QChar *c = part.unicode();
00054         for (int i = 0; i < part.length(); ++i) {
00055             register ushort u = c[i].unicode();
00056             if (!((u >= 'a' && u <= 'z') ||
00057                   (u >= 'A' && u <= 'Z') ||
00058                   (u >= '0' && u <= '9') ||
00059                   u == '_'))
00060                 return false;
00061         }
00062         return true;
00063     }
00064 
00078     bool isValidInterfaceName(const QString& ifaceName)
00079     {
00080         if (ifaceName.isEmpty() || ifaceName.length() > DBUS_MAXIMUM_NAME_LENGTH)
00081             return false;
00082 
00083         QStringList parts = ifaceName.split(QLatin1Char('.'));
00084         if (parts.count() < 2)
00085             return false;           // at least two parts
00086 
00087         for (int i = 0; i < parts.count(); ++i)
00088             if (!isValidMemberName(parts.at(i)))
00089                 return false;
00090 
00091         return true;
00092     }
00093 
00101     bool isValidUniqueConnectionName(const QString &connName)
00102     {
00103         if (connName.isEmpty() || connName.length() > DBUS_MAXIMUM_NAME_LENGTH ||
00104             !connName.startsWith(QLatin1Char(':')))
00105             return false;
00106 
00107         QStringList parts = connName.mid(1).split(QLatin1Char('.'));
00108         if (parts.count() < 1)
00109             return false;
00110 
00111         QRegExp regex(QLatin1String("[a-zA-Z0-9_-]+"));
00112         for (int i = 0; i < parts.count(); ++i)
00113             if (!regex.exactMatch(parts.at(i)))
00114                 return false;
00115 
00116         return true;
00117     }
00118 
00134     bool isValidBusName(const QString &busName)
00135     {
00136         if (busName.isEmpty() || busName.length() > DBUS_MAXIMUM_NAME_LENGTH)
00137             return false;
00138 
00139         if (busName.startsWith(QLatin1Char(':')))
00140             return isValidUniqueConnectionName(busName);
00141 
00142         QStringList parts = busName.split(QLatin1Char('.'));
00143         if (parts.count() < 1)
00144             return false;
00145 
00146         QRegExp regex(QLatin1String("[a-zA-Z_-][a-zA-Z0-9_-]*"));
00147         for (int i = 0; i < parts.count(); ++i)
00148             if (!regex.exactMatch(parts.at(i)))
00149                 return false;
00150 
00151         return true;
00152     }
00153 
00160     bool isValidMemberName(const QString &memberName)
00161     {
00162         if (memberName.isEmpty() || memberName.length() > DBUS_MAXIMUM_NAME_LENGTH)
00163             return false;
00164 
00165         QRegExp regex(QLatin1String("[a-zA-Z_][a-zA-Z0-9_]*"));
00166         return regex.exactMatch(memberName);
00167     }
00168 
00174     bool isValidErrorName(const QString &errorName)
00175     {
00176         return isValidInterfaceName(errorName);
00177     }
00178 
00192     bool isValidObjectPath(const QString &path)
00193     {
00194         if (path == QLatin1String("/"))
00195             return true;
00196 
00197         if (!path.startsWith(QLatin1Char('/')) || path.indexOf(QLatin1String("//")) != -1 ||
00198             path.endsWith(QLatin1Char('/')))
00199             return false;
00200 
00201         QStringList parts = path.split(QLatin1Char('/'));
00202         Q_ASSERT(parts.count() >= 1);
00203         parts.removeFirst();    // it starts with /, so we get an empty first part
00204 
00205         for (int i = 0; i < parts.count(); ++i)
00206             if (!isValidPartOfObjectPath(parts.at(i)))
00207                 return false;
00208 
00209         return true;
00210     }
00211 
00220     bool isValidSignature(const QString &signature)
00221     {
00222         return dbus_signature_validate(signature.toUtf8(), 0);
00223     }
00224 
00231     bool isValidSingleSignature(const QString &signature)
00232     {
00233         return dbus_signature_validate_single(signature.toUtf8(), 0);
00234     }
00235 
00236 } // namespace QDBusUtil

Generated on Thu Mar 15 12:03:09 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1