[Telepathy-commits] [telepathy-qt4/master] Moved ProtocolInfo parameters handling into a separate class.

Andre Moreira Magalhaes (andrunko) andre.magalhaes at collabora.co.uk
Mon Jan 5 08:56:29 PST 2009


---
 TelepathyQt4/cli-connection-manager.cpp |  138 ++++++++++++++++---------------
 TelepathyQt4/cli-connection-manager.h   |  122 ++++++++++------------------
 2 files changed, 113 insertions(+), 147 deletions(-)

diff --git a/TelepathyQt4/cli-connection-manager.cpp b/TelepathyQt4/cli-connection-manager.cpp
index 38ed616..d6f45b8 100644
--- a/TelepathyQt4/cli-connection-manager.cpp
+++ b/TelepathyQt4/cli-connection-manager.cpp
@@ -26,7 +26,6 @@
 #include <QtCore/QTimer>
 
 #include <TelepathyQt4/Client/DBus>
-#include <TelepathyQt4/Constants>
 #include <TelepathyQt4/Types>
 
 #include "TelepathyQt4/debug-internal.hpp"
@@ -37,103 +36,108 @@ namespace Client
 {
 
 
-struct ProtocolInfo::Private
+// FIXME proper map dbusSignature to QVariant on mType
+ProtocolParameter::ProtocolParameter(const QString &name,
+                                     const QDBusSignature &dbusSignature,
+                                     QVariant defaultValue,
+                                     Telepathy::ConnMgrParamFlag flags)
+    : mName(name),
+      mDBusSignature(dbusSignature),
+      mType(QVariant::Invalid),
+      mDefaultValue(defaultValue),
+      mFlags(flags)
 {
-    QString cmName;
-    QString protocolName;
-
-    QMap<QString,QDBusSignature> parameters;
-    QMap<QString,QVariant> defaults;
-    QSet<QString> requiredParameters;
-    QSet<QString> registerParameters;
-    QSet<QString> propertyParameters;
-    QSet<QString> secretParameters;
-
-    Private(const QString& cmName, const QString& protocolName)
-        : cmName(cmName), protocolName(protocolName)
-    {
-    }
-};
+}
 
 
-ProtocolInfo::ProtocolInfo(const QString& cmName, const QString& protocol)
-    : mPriv(new Private(cmName, protocol))
+ProtocolParameter::~ProtocolParameter()
 {
 }
 
 
-ProtocolInfo::~ProtocolInfo()
+bool ProtocolParameter::isRequired() const
 {
+    return mFlags & ConnMgrParamFlagRequired;
 }
 
 
-QString ProtocolInfo::cmName() const
+bool ProtocolParameter::isSecret() const
 {
-    return mPriv->cmName;
+    return mFlags & ConnMgrParamFlagSecret;
 }
 
 
-QString ProtocolInfo::protocolName() const
+bool ProtocolParameter::requiredForRegistration() const
 {
-    return mPriv->protocolName;
+    return mFlags & ConnMgrParamFlagRegister;
 }
 
 
-QStringList ProtocolInfo::parameters() const
+bool ProtocolParameter::operator==(const ProtocolParameter &other) const
 {
-    return mPriv->parameters.keys();
+    return (mName == other.name());
 }
 
 
-bool ProtocolInfo::hasParameter(const QString& param) const
+bool ProtocolParameter::operator==(const QString &name) const
 {
-    return mPriv->parameters.contains(param);
+    return (mName == name);
 }
 
 
-QDBusSignature ProtocolInfo::parameterDBusSignature(const QString& param) const
+struct ProtocolInfo::Private
 {
-    return mPriv->parameters.value(param);
-}
+    QString cmName;
+    QString protocolName;
+    ProtocolParameterList params;
+
+    Private(const QString& cmName, const QString& protocolName)
+        : cmName(cmName), protocolName(protocolName)
+    {
+    }
+};
 
 
-QVariant::Type ProtocolInfo::parameterType(const QString& param) const
+ProtocolInfo::ProtocolInfo(const QString& cmName, const QString& protocol)
+    : mPriv(new Private(cmName, protocol))
 {
-    return QVariant::Invalid;
 }
 
 
-bool ProtocolInfo::parameterIsRequired(const QString& param,
-        bool registering) const
+ProtocolInfo::~ProtocolInfo()
 {
-    if (registering)
-        return mPriv->registerParameters.contains(param);
-    else
-        return mPriv->requiredParameters.contains(param);
+    Q_FOREACH (ProtocolParameter *param, mPriv->params) {
+        delete param;
+    }
 }
 
 
-bool ProtocolInfo::parameterIsSecret(const QString& param) const
+QString ProtocolInfo::cmName() const
 {
-    return mPriv->secretParameters.contains(param);
+    return mPriv->cmName;
 }
 
 
-bool ProtocolInfo::parameterIsDBusProperty(const QString& param) const
+QString ProtocolInfo::protocolName() const
 {
-    return mPriv->propertyParameters.contains(param);
+    return mPriv->protocolName;
 }
 
 
-bool ProtocolInfo::parameterHasDefault(const QString& param) const
+const ProtocolParameterList &ProtocolInfo::parameters() const
 {
-    return mPriv->defaults.contains(param);
+    return mPriv->params;
 }
 
 
-QVariant ProtocolInfo::getParameterDefault(const QString& param) const
+bool ProtocolInfo::hasParameter(const QString &name) const
 {
-    return mPriv->defaults.value(param);
+    Q_FOREACH (ProtocolParameter *param, mPriv->params) {
+        if (param->name() == name) {
+            return true;
+        }
+    }
+    return false;
 }
 
 
@@ -143,6 +147,24 @@ bool ProtocolInfo::canRegister() const
 }
 
 
+void ProtocolInfo::addParameter(const ParamSpec &spec)
+{
+    QVariant defaultValue;
+    if (spec.flags & ConnMgrParamFlagHasDefault)
+        defaultValue = spec.defaultValue.variant();
+
+    uint flags = spec.flags;
+    if (spec.name.endsWith("password"))
+        flags |= Telepathy::ConnMgrParamFlagSecret;
+
+    ProtocolParameter *param = new ProtocolParameter(spec.name,
+            QDBusSignature(spec.signature),
+            defaultValue,
+            (Telepathy::ConnMgrParamFlag) flags);
+    mPriv->params.append(param);
+}
+
+
 struct ConnectionManager::Private
 {
     ConnectionManager& parent;
@@ -357,28 +379,8 @@ void ConnectionManager::onGetParametersReturn(
     Q_FOREACH (const ParamSpec& spec, parameters) {
         debug() << "Parameter" << spec.name << "has flags" << spec.flags
             << "and signature" << spec.signature;
-        info->mPriv->parameters.insert(spec.name,
-                QDBusSignature(spec.signature));
-
-        if (spec.flags & ConnMgrParamFlagHasDefault)
-            info->mPriv->defaults.insert(spec.name,
-                    spec.defaultValue.variant());
-
-        if (spec.flags & ConnMgrParamFlagRequired)
-            info->mPriv->requiredParameters.insert(spec.name);
-
-        if (spec.flags & ConnMgrParamFlagRegister)
-            info->mPriv->registerParameters.insert(spec.name);
-
-        if ((spec.flags & ConnMgrParamFlagSecret)
-                || spec.name.endsWith("password"))
-            info->mPriv->secretParameters.insert(spec.name);
 
-#if 0
-        // enable when we merge the new telepathy-spec
-        if (spec.flags & ConnMgrParamFlag)
-            info->mPriv->propertyParameters.insert(spec.name);
-#endif
+        info->addParameter(spec);
     }
     continueIntrospection();
 }
diff --git a/TelepathyQt4/cli-connection-manager.h b/TelepathyQt4/cli-connection-manager.h
index 2bc3f8c..48e9d22 100644
--- a/TelepathyQt4/cli-connection-manager.h
+++ b/TelepathyQt4/cli-connection-manager.h
@@ -42,6 +42,7 @@
 
 #include <TelepathyQt4/_gen/cli-connection-manager.h>
 
+#include <TelepathyQt4/Constants>
 #include <TelepathyQt4/Client/DBus>
 #include <TelepathyQt4/Client/DBusProxy>
 #include <TelepathyQt4/Client/OptionalInterfaceFactory>
@@ -51,6 +52,41 @@ namespace Telepathy
 namespace Client
 {
 
+class ProtocolParameter;
+
+typedef QList<ProtocolParameter*> ProtocolParameterList;
+
+class ProtocolParameter
+{
+public:
+    ProtocolParameter(const QString &name,
+                      const QDBusSignature &dbusSignature,
+                      QVariant defaultValue,
+                      Telepathy::ConnMgrParamFlag flags);
+    ~ProtocolParameter();
+
+    QString name() const { return mName; }
+    QDBusSignature dbusSignature() const { return mDBusSignature; }
+    QVariant type() const { return mType; }
+    QVariant defaultValue() const { return mDefaultValue; }
+
+    bool isRequired() const;
+    bool isSecret() const;
+    bool requiredForRegistration() const;
+
+    bool operator==(const ProtocolParameter &other) const;
+    bool operator==(const QString &name) const;
+
+private:
+    Q_DISABLE_COPY(ProtocolParameter);
+
+    QString mName;
+    QDBusSignature mDBusSignature;
+    QVariant mType;
+    QVariant mDefaultValue;
+    Telepathy::ConnMgrParamFlag mFlags;
+};
+
 
 class ProtocolInfo
 {
@@ -71,97 +107,23 @@ public:
     QString protocolName() const;
 
     /**
-     * Return the names of all supported parameters. The parameters' names
+     * Return all supported parameters. The parameters' names
      * may either be the well-known strings specified by the Telepathy D-Bus
      * API Specification (e.g. "account" and "password"), or
      * implementation-specific strings.
      *
-     * \return A list of parameter names
+     * \return A list of parameters
      */
-    QStringList parameters() const;
+    const ProtocolParameterList &parameters() const;
 
     /**
      * Return whether a given parameter can be passed to the connection
      * manager when creating a connection to this protocol.
      *
-     * \param param The name of a parameter
+     * \param name The name of a parameter
      * \return true if the given parameter exists
      */
-    bool hasParameter(const QString& param) const;
-
-    /**
-     * Return the D-Bus signature of the given parameter. Commonly used
-     * signatures include "s" (string) and "u" (32-bit unsigned integer).
-     *
-     * \return A D-Bus signature
-     */
-    QDBusSignature parameterDBusSignature(const QString& param) const;
-
-    /**
-     * Return the QVariant::Type corresponding to the D-Bus signature of
-     * the given parameter, if a suitable type is known. Otherwise return
-     * QVariant::Invalid.
-     *
-     * \param param The name of a parameter
-     * \return A QVariant type, or QVariant::Invalid
-     */
-    QVariant::Type parameterType(const QString& param) const;
-
-    /**
-     * Return whether the given parameter is required.
-     *
-     * The set of required parameters can be different depending on the value
-     * of the special parameter <code>register</code>, which indicates
-     * whether we are registering for a new account, or connecting to an
-     * existing account. Normally, <code>register</code> is false.
-     *
-     * \param param The name of a parameter
-     * \param registering The value that will be given to the special
-     *                    parameter <code>register</code>
-     * \return true if this parameter must always be given
-     */
-    bool parameterIsRequired(const QString& param,
-            bool registering = false) const;
-
-    /**
-     * Return whether this parameter is marked as "secret". Secret parameters
-     * (such as passwords) should be stored in an appropriate password store
-     * (such as KWallet) rather than in normal configuration files, and should
-     * not appear in debugging logs.
-     *
-     * \param param The name of a parameter
-     * \return true if this parameter is a password, key or other secret
-     */
-    bool parameterIsSecret(const QString& param) const;
-
-    /**
-     * Return whether this parameter corresponds to a D-Bus property with
-     * the same name. These parameters should be handled specially by the
-     * AccountManager.
-     *
-     * \param param The name of a parameter
-     * \return true if this parameter is a D-Bus property
-     */
-    bool parameterIsDBusProperty(const QString& param) const;
-
-    /**
-     * Return whether this parameter has a default value.
-     *
-     * \param param The name of a parameter
-     * \return true if omitting this parameter when creating a connection
-     *         is equivalent to using getParameterDefault()
-     */
-    bool parameterHasDefault(const QString& param) const;
-
-    /**
-     * If parameterHasDefault() returns true for the given parameter, return
-     * the default value of the parameter. Otherwise, return an invalid
-     * variant.
-     *
-     * \param param The name of a parameter
-     * \return The default value of the given parameter, if any
-     */
-    QVariant getParameterDefault(const QString& param) const;
+    bool hasParameter(const QString &name) const;
 
     /**
      * Return whether it might be possible to register new accounts on this
@@ -175,6 +137,8 @@ public:
 private:
     ProtocolInfo(const QString& cmName, const QString& protocol);
 
+    void addParameter(const ParamSpec &spec);
+
     struct Private;
     friend struct Private;
     friend class ConnectionManager;
-- 
1.5.6.5




More information about the Telepathy-commits mailing list