1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** 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, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "qmlobservertool.h"
36 #include "qt4project.h"
37 #include "qt4projectmanagerconstants.h"
38 #include <coreplugin/icore.h>
39 #include <utils/qtcassert.h>
41 #include <projectexplorer/project.h>
42 #include <QDesktopServices>
43 #include <QCoreApplication>
47 namespace Qt4ProjectManager {
49 static inline QStringList validBinaryFilenames()
52 << QLatin1String("debug/qmlobserver.exe")
53 << QLatin1String("qmlobserver.exe")
54 << QLatin1String("qmlobserver")
55 << QLatin1String("QMLObserver.app/Contents/MacOS/QMLObserver");
58 bool QmlObserverTool::canBuild(const QtVersion *qtVersion)
60 return (qtVersion->supportsTargetId(Constants::DESKTOP_TARGET_ID)
61 || qtVersion->supportsTargetId(Constants::QT_SIMULATOR_TARGET_ID))
62 && checkMinimumQtVersion(qtVersion->qtVersionString(), 4, 7, 1);
65 QString QmlObserverTool::toolForProject(ProjectExplorer::Project *project)
67 if (project->id() == Qt4ProjectManager::Constants::QT4PROJECT_ID) {
68 Qt4Project *qt4Project = static_cast<Qt4Project*>(project);
69 if (qt4Project && qt4Project->activeTarget()
70 && qt4Project->activeTarget()->activeBuildConfiguration()) {
71 QtVersion *version = qt4Project->activeTarget()->activeBuildConfiguration()->qtVersion();
72 if (version->isValid()) {
73 QString qtInstallData = version->versionInfo().value("QT_INSTALL_DATA");
74 QString toolPath = toolByInstallData(qtInstallData);
82 QString QmlObserverTool::toolByInstallData(const QString &qtInstallData)
84 if (!Core::ICore::instance())
87 const QString mainFilename = Core::ICore::instance()->resourcePath()
88 + QLatin1String("/qml/qmlobserver/main.cpp");
89 const QStringList directories = installDirectories(qtInstallData);
90 const QStringList binFilenames = validBinaryFilenames();
92 return byInstallDataHelper(mainFilename, directories, binFilenames);
95 QStringList QmlObserverTool::locationsByInstallData(const QString &qtInstallData)
99 const QStringList binFilenames = validBinaryFilenames();
100 foreach(const QString &directory, installDirectories(qtInstallData)) {
101 if (getHelperFileInfoFor(binFilenames, directory, &fileInfo))
102 result << fileInfo.filePath();
107 bool QmlObserverTool::build(const QString &directory, const QString &makeCommand,
108 const QString &qmakeCommand, const QString &mkspec,
109 const Utils::Environment &env, const QString &targetMode,
110 QString *output, QString *errorMessage)
112 return buildHelper(QCoreApplication::translate("Qt4ProjectManager::QmlObserverTool", "QMLObserver"),
113 QLatin1String("qmlobserver.pro"),
114 directory, makeCommand, qmakeCommand, mkspec, env, targetMode,
115 output, errorMessage);
118 static inline bool mkpath(const QString &targetDirectory, QString *errorMessage)
120 if (!QDir().mkpath(targetDirectory)) {
121 *errorMessage = QCoreApplication::translate("ProjectExplorer::QmlObserverTool", "The target directory %1 could not be created.").arg(targetDirectory);
127 QString QmlObserverTool::copy(const QString &qtInstallData, QString *errorMessage)
129 const QStringList directories = QmlObserverTool::installDirectories(qtInstallData);
131 QString sourcePath = Core::ICore::instance()->resourcePath() + QLatin1String("/qml/qmlobserver/");
132 QString libSourcePath = Core::ICore::instance()->resourcePath() + QLatin1String("/qml/qmljsdebugger/");
134 QStringList observerFiles = recursiveFileList(QDir(sourcePath));
135 QStringList qmljsDebuggerFiles = recursiveFileList(QDir(libSourcePath));
137 // Try to find a writeable directory.
138 foreach(const QString &directory, directories) {
139 if (!mkpath(directory, errorMessage)) {
142 errorMessage->clear();
145 if (copyFiles(sourcePath, observerFiles, directory, errorMessage)
146 && copyFiles(libSourcePath, qmljsDebuggerFiles,
147 directory + QLatin1String("/qmljsdebugger/"), errorMessage))
149 errorMessage->clear();
153 *errorMessage = QCoreApplication::translate("ProjectExplorer::QmlObserverTool",
154 "QMLObserver could not be built in any of the directories:\n- %1\n\nReason: %2")
155 .arg(directories.join(QLatin1String("\n- ")), *errorMessage);
159 QStringList QmlObserverTool::recursiveFileList(const QDir &dir, const QString &prefix)
163 QString _prefix = prefix;
164 if (!_prefix.isEmpty() && !_prefix.endsWith('/')) {
165 _prefix = _prefix + '/';
167 foreach (const QString &fileName, dir.entryList(QDir::Files)) {
168 files << _prefix + fileName;
171 foreach (const QString &subDir, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
172 files += recursiveFileList(QDir(dir.absoluteFilePath(subDir)), _prefix + subDir);
177 QStringList QmlObserverTool::installDirectories(const QString &qtInstallData)
179 const QChar slash = QLatin1Char('/');
180 const uint hash = qHash(qtInstallData);
181 QStringList directories;
183 << (qtInstallData + QLatin1String("/qtc-qmlobserver/"))
184 << QDir::cleanPath((QCoreApplication::applicationDirPath() + QLatin1String("/../qtc-qmlobserver/") + QString::number(hash))) + slash
185 << (QDesktopServices::storageLocation(QDesktopServices::DataLocation) + QLatin1String("/qtc-qmlobserver/") + QString::number(hash)) + slash;