OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qt4projectmanager / qmlobservertool.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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, 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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "qmlobservertool.h"
35
36 #include "qt4project.h"
37 #include "qt4projectmanagerconstants.h"
38 #include <coreplugin/icore.h>
39 #include <utils/qtcassert.h>
40
41 #include <projectexplorer/project.h>
42 #include <QDesktopServices>
43 #include <QCoreApplication>
44 #include <QDir>
45 #include <QDebug>
46
47 namespace Qt4ProjectManager {
48
49 static inline QStringList validBinaryFilenames()
50 {
51     return QStringList()
52             << QLatin1String("debug/qmlobserver.exe")
53             << QLatin1String("qmlobserver.exe")
54             << QLatin1String("qmlobserver")
55             << QLatin1String("QMLObserver.app/Contents/MacOS/QMLObserver");
56 }
57
58 bool QmlObserverTool::canBuild(const QtVersion *qtVersion)
59 {
60     return (qtVersion->supportsTargetId(Constants::DESKTOP_TARGET_ID)
61             || qtVersion->supportsTargetId(Constants::QT_SIMULATOR_TARGET_ID))
62             && checkMinimumQtVersion(qtVersion->qtVersionString(), 4, 7, 1);
63 }
64
65 QString QmlObserverTool::toolForProject(ProjectExplorer::Project *project)
66 {
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);
75                 return toolPath;
76             }
77         }
78     }
79     return QString();
80 }
81
82 QString QmlObserverTool::toolByInstallData(const QString &qtInstallData)
83 {
84     if (!Core::ICore::instance())
85         return QString();
86
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();
91
92     return byInstallDataHelper(mainFilename, directories, binFilenames);
93 }
94
95 QStringList QmlObserverTool::locationsByInstallData(const QString &qtInstallData)
96 {
97     QStringList result;
98     QFileInfo fileInfo;
99     const QStringList binFilenames = validBinaryFilenames();
100     foreach(const QString &directory, installDirectories(qtInstallData)) {
101         if (getHelperFileInfoFor(binFilenames, directory, &fileInfo))
102             result << fileInfo.filePath();
103     }
104     return result;
105 }
106
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)
111 {
112     return buildHelper(QCoreApplication::translate("Qt4ProjectManager::QmlObserverTool", "QMLObserver"),
113                        QLatin1String("qmlobserver.pro"),
114                        directory, makeCommand, qmakeCommand, mkspec, env, targetMode,
115                        output, errorMessage);
116 }
117
118 static inline bool mkpath(const QString &targetDirectory, QString *errorMessage)
119 {
120     if (!QDir().mkpath(targetDirectory)) {
121         *errorMessage = QCoreApplication::translate("ProjectExplorer::QmlObserverTool", "The target directory %1 could not be created.").arg(targetDirectory);
122         return false;
123     }
124     return true;
125 }
126
127 QString QmlObserverTool::copy(const QString &qtInstallData, QString *errorMessage)
128 {
129     const QStringList directories = QmlObserverTool::installDirectories(qtInstallData);
130
131     QString sourcePath = Core::ICore::instance()->resourcePath() + QLatin1String("/qml/qmlobserver/");
132     QString libSourcePath = Core::ICore::instance()->resourcePath() + QLatin1String("/qml/qmljsdebugger/");
133
134     QStringList observerFiles = recursiveFileList(QDir(sourcePath));
135     QStringList qmljsDebuggerFiles = recursiveFileList(QDir(libSourcePath));
136
137     // Try to find a writeable directory.
138     foreach(const QString &directory, directories) {
139         if (!mkpath(directory, errorMessage)) {
140             continue;
141         } else {
142             errorMessage->clear();
143         }
144
145         if (copyFiles(sourcePath, observerFiles, directory, errorMessage)
146                 && copyFiles(libSourcePath, qmljsDebuggerFiles,
147                              directory + QLatin1String("/qmljsdebugger/"), errorMessage))
148         {
149             errorMessage->clear();
150             return directory;
151         }
152     }
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);
156     return QString();
157 }
158
159 QStringList QmlObserverTool::recursiveFileList(const QDir &dir, const QString &prefix)
160 {
161     QStringList files;
162
163     QString _prefix = prefix;
164     if (!_prefix.isEmpty() && !_prefix.endsWith('/')) {
165         _prefix = _prefix + '/';
166     }
167     foreach (const QString &fileName, dir.entryList(QDir::Files)) {
168         files << _prefix + fileName;
169     }
170
171     foreach (const QString &subDir, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
172         files += recursiveFileList(QDir(dir.absoluteFilePath(subDir)), _prefix + subDir);
173     }
174     return files;
175 }
176
177 QStringList QmlObserverTool::installDirectories(const QString &qtInstallData)
178 {
179     const QChar slash = QLatin1Char('/');
180     const uint hash = qHash(qtInstallData);
181     QStringList directories;
182     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;
186     return directories;
187 }
188
189 } // namespace