1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQml module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qqmldebugservice_p.h"
41#include "qqmldebugconnector_p.h"
42#include <private/qqmldata_p.h>
43#include <private/qqmlcontext_p.h>
44
45#include <QtCore/QDebug>
46#include <QtCore/QStringList>
47#include <QtCore/QFileInfo>
48
49QT_BEGIN_NAMESPACE
50
51class QQmlDebugServer;
52
53class QQmlDebugServicePrivate : public QObjectPrivate
54{
55 Q_DECLARE_PUBLIC(QQmlDebugService)
56public:
57 QQmlDebugServicePrivate(const QString &name, float version);
58
59 const QString name;
60 const float version;
61 QQmlDebugService::State state;
62};
63
64QQmlDebugServicePrivate::QQmlDebugServicePrivate(const QString &name, float version) :
65 name(name), version(version), state(QQmlDebugService::NotConnected)
66{
67}
68
69QQmlDebugService::QQmlDebugService(const QString &name, float version, QObject *parent)
70 : QObject(*(new QQmlDebugServicePrivate(name, version)), parent)
71{
72 Q_D(QQmlDebugService);
73 QQmlDebugConnector *server = QQmlDebugConnector::instance();
74
75 if (!server)
76 return;
77
78 if (server->service(name: d->name)) {
79 qWarning() << "QQmlDebugService: Conflicting plugin name" << d->name;
80 } else {
81 server->addService(name: d->name, service: this);
82 }
83}
84
85QQmlDebugService::~QQmlDebugService()
86{
87 Q_D(QQmlDebugService);
88 QQmlDebugConnector *server = QQmlDebugConnector::instance();
89
90 if (!server)
91 return;
92
93 if (server->service(name: d->name) != this)
94 qWarning() << "QQmlDebugService: Plugin" << d->name << "is not registered.";
95 else
96 server->removeService(name: d->name);
97}
98
99const QString &QQmlDebugService::name() const
100{
101 Q_D(const QQmlDebugService);
102 return d->name;
103}
104
105float QQmlDebugService::version() const
106{
107 Q_D(const QQmlDebugService);
108 return d->version;
109}
110
111QQmlDebugService::State QQmlDebugService::state() const
112{
113 Q_D(const QQmlDebugService);
114 return d->state;
115}
116
117void QQmlDebugService::setState(QQmlDebugService::State newState)
118{
119 Q_D(QQmlDebugService);
120 d->state = newState;
121}
122
123namespace {
124class ObjectReferenceHash : public QObject
125{
126 Q_OBJECT
127public:
128 ObjectReferenceHash() : nextId(0) {}
129
130 QHash<QObject *, int> objects;
131 QHash<int, QObject *> ids;
132
133 int nextId;
134
135 void remove(QObject *obj);
136};
137}
138Q_GLOBAL_STATIC(ObjectReferenceHash, objectReferenceHash)
139
140void ObjectReferenceHash::remove(QObject *obj)
141{
142 QHash<QObject *, int>::Iterator iter = objects.find(akey: obj);
143 if (iter != objects.end()) {
144 ids.remove(akey: iter.value());
145 objects.erase(it: iter);
146 }
147}
148
149/*!
150 Returns a unique id for \a object. Calling this method multiple times
151 for the same object will return the same id.
152*/
153int QQmlDebugService::idForObject(QObject *object)
154{
155 if (!object)
156 return -1;
157
158 ObjectReferenceHash *hash = objectReferenceHash();
159 QHash<QObject *, int>::Iterator iter = hash->objects.find(akey: object);
160
161 if (iter == hash->objects.end()) {
162 int id = hash->nextId++;
163 hash->ids.insert(akey: id, avalue: object);
164 iter = hash->objects.insert(akey: object, avalue: id);
165 connect(sender: object, signal: &QObject::destroyed, receiver: hash, slot: &ObjectReferenceHash::remove);
166 }
167 return iter.value();
168}
169
170/*!
171 Returns the mapping of objects to unique \a ids, created through calls to idForObject().
172*/
173const QHash<int, QObject *> &QQmlDebugService::objectsForIds()
174{
175 return objectReferenceHash()->ids;
176}
177
178QT_END_NAMESPACE
179
180#include "qqmldebugservice.moc"
181#include "moc_qqmldebugservice_p.cpp"
182

source code of qtdeclarative/src/qml/debugger/qqmldebugservice.cpp