1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "qmlobservertool.h"
35 #include "qt4project.h"
36 #include "qt4projectmanagerconstants.h"
37 #include <coreplugin/icore.h>
38 #include <utils/qtcassert.h>
40 #include <projectexplorer/project.h>
41 #include <QtGui/QDesktopServices>
42 #include <QtCore/QCoreApplication>
43 #include <QtCore/QDir>
44 #include <QtCore/QDebug>
46 namespace Qt4ProjectManager {
48 static inline QStringList validBinaryFilenames()
51 << QLatin1String("debug/qmlobserver.exe")
52 << QLatin1String("qmlobserver.exe")
53 << QLatin1String("qmlobserver")
54 << QLatin1String("QMLObserver.app/Contents/MacOS/QMLObserver");
57 bool QmlObserverTool::canBuild(const QtVersion *qtVersion)
59 return (qtVersion->supportsTargetId(Constants::DESKTOP_TARGET_ID)
60 || qtVersion->supportsTargetId(Constants::QT_SIMULATOR_TARGET_ID))
61 && (qtVersion->qtVersion() >= QtVersionNumber(4, 7, 1));
64 QString QmlObserverTool::toolForProject(ProjectExplorer::Project *project)
66 if (project->id() == Qt4ProjectManager::Constants::QT4PROJECT_ID) {
67 Qt4Project *qt4Project = static_cast<Qt4Project*>(project);
68 if (qt4Project && qt4Project->activeTarget()
69 && qt4Project->activeTarget()->activeBuildConfiguration()) {
70 QtVersion *version = qt4Project->activeTarget()->activeBuildConfiguration()->qtVersion();
71 if (version->isValid()) {
72 QString qtInstallData = version->versionInfo().value("QT_INSTALL_DATA");
73 QString toolPath = toolByInstallData(qtInstallData);
81 QString QmlObserverTool::toolByInstallData(const QString &qtInstallData)
83 if (!Core::ICore::instance())
86 const QStringList directories = installDirectories(qtInstallData);
87 const QStringList binFilenames = validBinaryFilenames();
89 return byInstallDataHelper(sourcePath(), sourceFileNames(), directories, binFilenames);
92 QStringList QmlObserverTool::locationsByInstallData(const QString &qtInstallData)
96 const QStringList binFilenames = validBinaryFilenames();
97 foreach(const QString &directory, installDirectories(qtInstallData)) {
98 if (getHelperFileInfoFor(binFilenames, directory, &fileInfo))
99 result << fileInfo.filePath();
104 bool QmlObserverTool::build(const QString &directory, const QString &makeCommand,
105 const QString &qmakeCommand, const QString &mkspec,
106 const Utils::Environment &env, const QString &targetMode,
107 const QStringList &qmakeArguments, QString *output,
108 QString *errorMessage)
110 return buildHelper(QCoreApplication::translate("Qt4ProjectManager::QmlObserverTool", "QMLObserver"),
111 QLatin1String("qmlobserver.pro"),
112 directory, makeCommand, qmakeCommand, mkspec, env, targetMode,
113 qmakeArguments, output, errorMessage);
116 static inline bool mkpath(const QString &targetDirectory, QString *errorMessage)
118 if (!QDir().mkpath(targetDirectory)) {
119 *errorMessage = QCoreApplication::translate("ProjectExplorer::QmlObserverTool", "The target directory %1 could not be created.").arg(targetDirectory);
125 QString QmlObserverTool::copy(const QString &qtInstallData, QString *errorMessage)
127 const QStringList directories = QmlObserverTool::installDirectories(qtInstallData);
129 // Try to find a writeable directory.
130 foreach(const QString &directory, directories) {
131 if (!mkpath(directory, errorMessage)) {
134 errorMessage->clear();
137 if (copyFiles(sourcePath(), sourceFileNames(), directory, errorMessage)) {
138 errorMessage->clear();
142 *errorMessage = QCoreApplication::translate("ProjectExplorer::QmlObserverTool",
143 "QMLObserver could not be built in any of the directories:\n- %1\n\nReason: %2")
144 .arg(directories.join(QLatin1String("\n- ")), *errorMessage);
148 QStringList QmlObserverTool::recursiveFileList(const QDir &dir, const QString &prefix)
152 QString _prefix = prefix;
153 if (!_prefix.isEmpty() && !_prefix.endsWith('/')) {
154 _prefix = _prefix + '/';
156 foreach (const QString &fileName, dir.entryList(QDir::Files)) {
157 files << _prefix + fileName;
160 foreach (const QString &subDir, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
161 files += recursiveFileList(QDir(dir.absoluteFilePath(subDir)), _prefix + subDir);
166 QStringList QmlObserverTool::installDirectories(const QString &qtInstallData)
168 const QChar slash = QLatin1Char('/');
169 const uint hash = qHash(qtInstallData);
170 QStringList directories;
172 << (qtInstallData + QLatin1String("/qtc-qmlobserver/"))
173 << QDir::cleanPath((QCoreApplication::applicationDirPath() + QLatin1String("/../qtc-qmlobserver/") + QString::number(hash))) + slash
174 << (QDesktopServices::storageLocation(QDesktopServices::DataLocation) + QLatin1String("/qtc-qmlobserver/") + QString::number(hash)) + slash;
178 QString QmlObserverTool::sourcePath()
180 return Core::ICore::instance()->resourcePath() + QLatin1String("/qml/qmlobserver/");
183 QStringList QmlObserverTool::sourceFileNames()
185 return recursiveFileList(QDir(sourcePath()));