1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtQml module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include "qqmlinspectorservice_p.h"
43 #include "qqmlinspectorinterface_p.h"
44 #include "qqmldebugserver_p.h"
46 #include <private/qqmlglobal_p.h>
48 #include <QtCore/QCoreApplication>
49 #include <QtCore/QDebug>
50 #include <QtCore/QDir>
51 #include <QtCore/QPluginLoader>
53 // print detailed information about loading of plugins
54 DEFINE_BOOL_CONFIG_OPTION(qmlDebugVerbose, QML_DEBUGGER_VERBOSE)
58 Q_GLOBAL_STATIC(QQmlInspectorService, serviceInstance)
60 QQmlInspectorService::QQmlInspectorService()
61 : QQmlDebugService(QStringLiteral("QmlInspector"), 1)
62 , m_currentInspectorPlugin(0)
67 QQmlInspectorService *QQmlInspectorService::instance()
69 return serviceInstance();
72 void QQmlInspectorService::addView(QObject *view)
78 void QQmlInspectorService::removeView(QObject *view)
80 m_views.removeAll(view);
84 void QQmlInspectorService::sendMessage(const QByteArray &message)
86 if (state() != Enabled)
89 QQmlDebugService::sendMessage(message);
92 void QQmlInspectorService::stateChanged(State /*state*/)
94 QMetaObject::invokeMethod(this, "updateState", Qt::QueuedConnection);
97 void QQmlInspectorService::updateState()
99 if (m_views.isEmpty()) {
100 if (m_currentInspectorPlugin) {
101 m_currentInspectorPlugin->deactivate();
102 m_currentInspectorPlugin = 0;
107 if (state() == Enabled) {
108 if (m_inspectorPlugins.isEmpty())
109 loadInspectorPlugins();
111 if (m_inspectorPlugins.isEmpty()) {
112 qWarning() << "QQmlInspector: No plugins found.";
113 QQmlDebugServer::instance()->removeService(this);
117 foreach (QQmlInspectorInterface *inspector, m_inspectorPlugins) {
118 if (inspector->canHandleView(m_views.first())) {
119 m_currentInspectorPlugin = inspector;
124 if (!m_currentInspectorPlugin) {
125 qWarning() << "QQmlInspector: No plugin available for view '" << m_views.first()->metaObject()->className() << "'.";
128 m_currentInspectorPlugin->activate(m_views.first());
130 if (m_currentInspectorPlugin) {
131 m_currentInspectorPlugin->deactivate();
132 m_currentInspectorPlugin = 0;
137 void QQmlInspectorService::messageReceived(const QByteArray &message)
139 QMetaObject::invokeMethod(this, "processMessage", Qt::QueuedConnection, Q_ARG(QByteArray, message));
142 void QQmlInspectorService::processMessage(const QByteArray &message)
144 if (m_currentInspectorPlugin)
145 m_currentInspectorPlugin->clientMessage(message);
148 void QQmlInspectorService::loadInspectorPlugins()
150 QStringList pluginCandidates;
151 const QStringList paths = QCoreApplication::libraryPaths();
152 foreach (const QString &libPath, paths) {
153 const QDir dir(libPath + QLatin1String("/qmltooling"));
155 foreach (const QString &pluginPath, dir.entryList(QDir::Files))
156 pluginCandidates << dir.absoluteFilePath(pluginPath);
159 foreach (const QString &pluginPath, pluginCandidates) {
160 if (qmlDebugVerbose())
161 qDebug() << "QQmlInspector: Trying to load plugin " << pluginPath << "...";
163 QPluginLoader loader(pluginPath);
164 if (!loader.load()) {
165 if (qmlDebugVerbose())
166 qDebug() << "QQmlInspector: Error while loading: " << loader.errorString();
171 QQmlInspectorInterface *inspector =
172 qobject_cast<QQmlInspectorInterface*>(loader.instance());
174 if (qmlDebugVerbose())
175 qDebug() << "QQmlInspector: Plugin successfully loaded.";
176 m_inspectorPlugins << inspector;
178 if (qmlDebugVerbose())
179 qDebug() << "QQmlInspector: Plugin does not implement interface QQmlInspectorInterface.";