Qt 4.8
qdeclarativedebugservice.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtDeclarative 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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "private/qdeclarativedebugservice_p.h"
43 #include "private/qdeclarativedebugservice_p_p.h"
44 #include "private/qdeclarativedebugserver_p.h"
45 
46 #include <QtCore/QDebug>
47 #include <QtCore/QStringList>
48 
50 
52 : server(0)
53 {
54 }
55 
58 {
60  d->name = name;
63 
64  if (!d->server)
65  return;
66 
67  if (d->server->serviceNames().contains(name)) {
68  qWarning() << "QDeclarativeDebugService: Conflicting plugin name" << name;
69  d->server = 0;
70  } else {
71  d->server->addService(this);
72  }
73 }
74 
76 {
78  if (d->server) {
79  d->server->removeService(this);
80  }
81 }
82 
84 {
86  return d->name;
87 }
88 
90 {
92  return d->status;
93 }
94 
95 namespace {
96 
97  struct ObjectReference
98  {
99  QPointer<QObject> object;
100  int id;
101  };
102 
103  struct ObjectReferenceHash
104  {
105  ObjectReferenceHash() : nextId(0) {}
106 
109 
110  int nextId;
111  };
112 
113 }
114 Q_GLOBAL_STATIC(ObjectReferenceHash, objectReferenceHash);
115 
116 
122 {
123  if (!object)
124  return -1;
125 
126  ObjectReferenceHash *hash = objectReferenceHash();
128  hash->objects.find(object);
129 
130  if (iter == hash->objects.end()) {
131  int id = hash->nextId++;
132 
133  hash->ids.insert(id, object);
134  iter = hash->objects.insert(object, ObjectReference());
135  iter->object = object;
136  iter->id = id;
137  } else if (iter->object != object) {
138  int id = hash->nextId++;
139 
140  hash->ids.remove(iter->id);
141 
142  hash->ids.insert(id, object);
143  iter->object = object;
144  iter->id = id;
145  }
146  return iter->id;
147 }
148 
155 {
156  ObjectReferenceHash *hash = objectReferenceHash();
157 
158  QHash<int, QObject *>::Iterator iter = hash->ids.find(id);
159  if (iter == hash->ids.end())
160  return 0;
161 
162 
164  hash->objects.find(*iter);
165  Q_ASSERT(objIter != hash->objects.end());
166 
167  if (objIter->object == 0) {
168  hash->ids.erase(iter);
169  hash->objects.erase(objIter);
170  return 0;
171  } else {
172  return *iter;
173  }
174 }
175 
177 {
178  return QDeclarativeDebugServer::instance() != 0;
179 }
180 
182 {
185 }
186 
188 {
189  if(!obj)
190  return QLatin1String("NULL");
191 
192  QString objectName = obj->objectName();
193  if(objectName.isEmpty())
194  objectName = QLatin1String("<unnamed>");
195 
196  QString rv = QString::fromUtf8(obj->metaObject()->className()) +
197  QLatin1String(": ") + objectName;
198 
199  return rv;
200 }
201 
203 {
205 
206  if (status() != Enabled)
207  return;
208 
209  d->server->sendMessage(this, message);
210 }
211 
213 {
215 
216  if (status() != Enabled)
217  return false;
218 
219  return d->server->waitForMessage(this);
220 }
221 
223 {
224 }
225 
227 {
228 }
229 
virtual void messageReceived(const QByteArray &)
double d
Definition: qnumeric_p.h:62
static uint hash(const uchar *p, int n)
Definition: qhash.cpp:68
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static QObject * objectForId(int)
Returns the object for unique id.
static QDeclarativeDebugServer * instance()
QString objectName
the name of this object
Definition: qobject.h:114
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_D(Class)
Definition: qglobal.h:2482
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
Definition: qstring.cpp:4302
Q_CORE_EXPORT void qWarning(const char *,...)
QDeclarativeDebugService(const QString &, QObject *parent=0)
static int idForObject(QObject *)
Returns a unique id for object.
static QAuServer & server()
Definition: qsound.cpp:79
static QString objectToString(QObject *obj)
The QHash::iterator class provides an STL-style non-const iterator for QHash and QMultiHash.
Definition: qhash.h:330
const char * className() const
Returns the class name.
Definition: qobjectdefs.h:491
QString objectName() const
QObject * parent
Definition: qobject.h:92
Q_GLOBAL_STATIC(ObjectReferenceHash, objectReferenceHash)
virtual const QMetaObject * metaObject() const
Returns a pointer to the meta-object of this object.
void sendMessage(const QByteArray &)