summaryrefslogtreecommitdiffstats
path: root/src/jsonendpointmanager.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/jsonendpointmanager.cpp')
-rw-r--r--src/jsonendpointmanager.cpp155
1 files changed, 155 insertions, 0 deletions
diff --git a/src/jsonendpointmanager.cpp b/src/jsonendpointmanager.cpp
new file mode 100644
index 0000000..f12f1ec
--- /dev/null
+++ b/src/jsonendpointmanager.cpp
@@ -0,0 +1,155 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtAddOn.JsonStream module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** This file may be used under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation and
+** appearing in the file LICENSE.LGPL included in the packaging of this
+** file. Please review the following information to ensure the GNU Lesser
+** General Public License version 2.1 requirements will be met:
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt 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.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "jsonendpointmanager_p.h"
+#include "jsonendpoint.h"
+#include "jsonconnection.h"
+
+QT_BEGIN_NAMESPACE_JSONSTREAM
+
+static const QLatin1String kstrEndpointKey("endpoint");
+
+/****************************************************************************/
+
+/*!
+ \class JsonEndpointManager
+ \brief The JsonEndpointManager class ...
+
+*/
+
+/*!
+ Constructs a \c JsonEndpointManager object.
+ */
+
+JsonEndpointManager::JsonEndpointManager(JsonConnection *parent)
+ : QObject(parent), mInit(false), mEndpointPropertyName(kstrEndpointKey)
+{
+}
+
+/*!
+ Deletes the \c JsonEndpointManager object.
+ */
+
+JsonEndpointManager::~JsonEndpointManager()
+{
+ clear();
+}
+
+JsonEndpoint *JsonEndpointManager::defaultEndpoint()
+{
+ JsonEndpoint *endpoint;
+ endpoints();
+ if (mDefaultEndpoint) {
+ endpoint = mDefaultEndpoint;
+ }
+ else {
+ endpoint = new JsonEndpoint();
+ JsonConnection *connection = qobject_cast<JsonConnection *>(parent());
+ if (connection) {
+ connection->addEndpoint(endpoint);
+ }
+ mDefaultEndpoint = endpoint;
+ }
+ return endpoint;
+}
+
+void JsonEndpointManager::addEndpoint(JsonEndpoint *endpoint)
+{
+ mInit = false; // rehashing required
+ mEndpoints.insert(QString::number((ulong)endpoint), endpoint);
+}
+
+void JsonEndpointManager::removeEndpoint(JsonEndpoint *endpoint)
+{
+ mEndpoints.remove(endpoint->name());
+ endpoint->setConnection(0);
+}
+
+QHash<QString, JsonEndpoint*> & JsonEndpointManager::endpoints()
+{
+ if (!mInit) {
+ // rehash with real names
+ QList<JsonEndpoint *> lst = mEndpoints.values();
+ mEndpoints.clear();
+ mDefaultEndpoint = 0;
+ foreach (JsonEndpoint *e, lst) {
+ mEndpoints.insert(e->name(), e);
+ if (e->name().isEmpty())
+ mDefaultEndpoint = e;
+ }
+ mInit = true;
+ }
+ return mEndpoints;
+}
+
+JsonEndpoint *JsonEndpointManager::endpoint(const QJsonObject &message)
+{
+ JsonEndpoint *endpoint = 0;
+ QString str;
+ QHash<QString, JsonEndpoint*> & hash(endpoints());
+ if (mDefaultEndpoint && 1 == hash.size()) {
+ // no need to search - have only single default endpoint
+ }
+ else if (!(str = message.value(mEndpointPropertyName).toString()).isEmpty()) {
+ QHash<QString, JsonEndpoint*>::const_iterator it = hash.find(str);
+ if (it != hash.constEnd())
+ endpoint = *it;
+ }
+
+ if (endpoint == 0)
+ endpoint = mDefaultEndpoint;
+
+ return endpoint;
+}
+
+void JsonEndpointManager::clear()
+{
+ QList<JsonEndpoint *> lst = mEndpoints.values();
+ foreach (JsonEndpoint *endpoint, lst) {
+ endpoint->setConnection(0);
+ }
+ mEndpoints.clear();
+}
+
+#include "moc_jsonendpointmanager_p.cpp"
+
+QT_END_NAMESPACE_JSONSTREAM
+