Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / src / declarative / debugger / qdeclarativeinspectorservice.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qdeclarativeinspectorservice_p.h"
43 #include "qdeclarativeinspectorinterface_p.h"
44 #include "qdeclarativedebugserver_p.h"
45
46 #include <QtCore/QCoreApplication>
47 #include <QtCore/QDebug>
48 #include <QtCore/QDir>
49 #include <QtCore/QPluginLoader>
50
51 // print detailed information about loading of plugins
52 DEFINE_BOOL_CONFIG_OPTION(qmlDebugVerbose, QML_DEBUGGER_VERBOSE)
53
54 QT_BEGIN_NAMESPACE
55
56 Q_GLOBAL_STATIC(QDeclarativeInspectorService, serviceInstance)
57
58 QDeclarativeInspectorService::QDeclarativeInspectorService()
59     : QDeclarativeDebugService(QLatin1String("QDeclarativeObserverMode"), 1)
60     , m_currentInspectorPlugin(0)
61 {
62     registerService();
63 }
64
65 QDeclarativeInspectorService *QDeclarativeInspectorService::instance()
66 {
67     return serviceInstance();
68 }
69
70 void QDeclarativeInspectorService::addView(QObject *view)
71 {
72     m_views.append(view);
73     updateStatus();
74 }
75
76 void QDeclarativeInspectorService::removeView(QObject *view)
77 {
78     m_views.removeAll(view);
79     updateStatus();
80 }
81
82 void QDeclarativeInspectorService::sendMessage(const QByteArray &message)
83 {
84     if (status() != Enabled)
85         return;
86
87     QDeclarativeDebugService::sendMessage(message);
88 }
89
90 void QDeclarativeInspectorService::statusChanged(Status /*status*/)
91 {
92     QMetaObject::invokeMethod(this, "updateStatus", Qt::QueuedConnection);
93 }
94
95 void QDeclarativeInspectorService::updateStatus()
96 {
97     if (m_views.isEmpty()) {
98         if (m_currentInspectorPlugin) {
99             m_currentInspectorPlugin->deactivate();
100             m_currentInspectorPlugin = 0;
101         }
102         return;
103     }
104
105     if (status() == Enabled) {
106         if (m_inspectorPlugins.isEmpty())
107             loadInspectorPlugins();
108
109         if (m_inspectorPlugins.isEmpty()) {
110             qWarning() << "QDeclarativeInspector: No plugins found.";
111             QDeclarativeDebugServer::instance()->removeService(this);
112             return;
113         }
114
115         foreach (QDeclarativeInspectorInterface *inspector, m_inspectorPlugins) {
116             if (inspector->canHandleView(m_views.first())) {
117                 m_currentInspectorPlugin = inspector;
118                 break;
119             }
120         }
121
122         if (!m_currentInspectorPlugin) {
123             qWarning() << "QDeclarativeInspector: No plugin available for view '" << m_views.first()->metaObject()->className() << "'.";
124             return;
125         }
126         m_currentInspectorPlugin->activate(m_views.first());
127     } else {
128         if (m_currentInspectorPlugin) {
129             m_currentInspectorPlugin->deactivate();
130             m_currentInspectorPlugin = 0;
131         }
132     }
133 }
134
135 void QDeclarativeInspectorService::messageReceived(const QByteArray &message)
136 {
137     QMetaObject::invokeMethod(this, "processMessage", Qt::QueuedConnection, Q_ARG(QByteArray, message));
138 }
139
140 void QDeclarativeInspectorService::processMessage(const QByteArray &message)
141 {
142     if (m_currentInspectorPlugin)
143         m_currentInspectorPlugin->clientMessage(message);
144 }
145
146 void QDeclarativeInspectorService::loadInspectorPlugins()
147 {
148     QStringList pluginCandidates;
149     const QStringList paths = QCoreApplication::libraryPaths();
150     foreach (const QString &libPath, paths) {
151         const QDir dir(libPath + QLatin1String("/qmltooling"));
152         if (dir.exists())
153             foreach (const QString &pluginPath, dir.entryList(QDir::Files))
154                 pluginCandidates << dir.absoluteFilePath(pluginPath);
155     }
156
157     foreach (const QString &pluginPath, pluginCandidates) {
158         if (qmlDebugVerbose())
159             qDebug() << "QDeclarativeInspector: Trying to load plugin " << pluginPath << "...";
160
161         QPluginLoader loader(pluginPath);
162         if (!loader.load()) {
163             if (qmlDebugVerbose())
164                 qDebug() << "QDeclarativeInspector: Error while loading: " << loader.errorString();
165
166             continue;
167         }
168
169         QDeclarativeInspectorInterface *inspector =
170                 qobject_cast<QDeclarativeInspectorInterface*>(loader.instance());
171         if (inspector) {
172             if (qmlDebugVerbose())
173                 qDebug() << "QDeclarativeInspector: Plugin successfully loaded.";
174             m_inspectorPlugins << inspector;
175         } else {
176             if (qmlDebugVerbose())
177                 qDebug() << "QDeclarativeInspector: Plugin does not implement interface QDeclarativeInspectorInterface.";
178
179             loader.unload();
180         }
181     }
182 }
183
184 QT_END_NAMESPACE