OSDN Git Service

Update license.
[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 (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
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.
18 **
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.
22 **
23 ** Other Usage
24 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "qmlobservertool.h"
34
35 #include "qt4project.h"
36 #include "qt4projectmanagerconstants.h"
37 #include <coreplugin/icore.h>
38 #include <utils/qtcassert.h>
39
40 #include <projectexplorer/project.h>
41 #include <QtGui/QDesktopServices>
42 #include <QtCore/QCoreApplication>
43 #include <QtCore/QDir>
44 #include <QtCore/QDebug>
45
46 namespace Qt4ProjectManager {
47
48 static inline QStringList validBinaryFilenames()
49 {
50     return QStringList()
51             << QLatin1String("debug/qmlobserver.exe")
52             << QLatin1String("qmlobserver.exe")
53             << QLatin1String("qmlobserver")
54             << QLatin1String("QMLObserver.app/Contents/MacOS/QMLObserver");
55 }
56
57 bool QmlObserverTool::canBuild(const QtVersion *qtVersion)
58 {
59     return (qtVersion->supportsTargetId(Constants::DESKTOP_TARGET_ID)
60             || qtVersion->supportsTargetId(Constants::QT_SIMULATOR_TARGET_ID))
61             && (qtVersion->qtVersion() >= QtVersionNumber(4, 7, 1));
62 }
63
64 QString QmlObserverTool::toolForProject(ProjectExplorer::Project *project)
65 {
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);
74                 return toolPath;
75             }
76         }
77     }
78     return QString();
79 }
80
81 QString QmlObserverTool::toolByInstallData(const QString &qtInstallData)
82 {
83     if (!Core::ICore::instance())
84         return QString();
85
86     const QStringList directories = installDirectories(qtInstallData);
87     const QStringList binFilenames = validBinaryFilenames();
88
89     return byInstallDataHelper(sourcePath(), sourceFileNames(), directories, binFilenames);
90 }
91
92 QStringList QmlObserverTool::locationsByInstallData(const QString &qtInstallData)
93 {
94     QStringList result;
95     QFileInfo fileInfo;
96     const QStringList binFilenames = validBinaryFilenames();
97     foreach(const QString &directory, installDirectories(qtInstallData)) {
98         if (getHelperFileInfoFor(binFilenames, directory, &fileInfo))
99             result << fileInfo.filePath();
100     }
101     return result;
102 }
103
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)
109 {
110     return buildHelper(QCoreApplication::translate("Qt4ProjectManager::QmlObserverTool", "QMLObserver"),
111                        QLatin1String("qmlobserver.pro"),
112                        directory, makeCommand, qmakeCommand, mkspec, env, targetMode,
113                        qmakeArguments, output, errorMessage);
114 }
115
116 static inline bool mkpath(const QString &targetDirectory, QString *errorMessage)
117 {
118     if (!QDir().mkpath(targetDirectory)) {
119         *errorMessage = QCoreApplication::translate("ProjectExplorer::QmlObserverTool", "The target directory %1 could not be created.").arg(targetDirectory);
120         return false;
121     }
122     return true;
123 }
124
125 QString QmlObserverTool::copy(const QString &qtInstallData, QString *errorMessage)
126 {
127     const QStringList directories = QmlObserverTool::installDirectories(qtInstallData);
128
129     // Try to find a writeable directory.
130     foreach(const QString &directory, directories) {
131         if (!mkpath(directory, errorMessage)) {
132             continue;
133         } else {
134             errorMessage->clear();
135         }
136
137         if (copyFiles(sourcePath(), sourceFileNames(), directory, errorMessage)) {
138             errorMessage->clear();
139             return directory;
140         }
141     }
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);
145     return QString();
146 }
147
148 QStringList QmlObserverTool::recursiveFileList(const QDir &dir, const QString &prefix)
149 {
150     QStringList files;
151
152     QString _prefix = prefix;
153     if (!_prefix.isEmpty() && !_prefix.endsWith('/')) {
154         _prefix = _prefix + '/';
155     }
156     foreach (const QString &fileName, dir.entryList(QDir::Files)) {
157         files << _prefix + fileName;
158     }
159
160     foreach (const QString &subDir, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
161         files += recursiveFileList(QDir(dir.absoluteFilePath(subDir)), _prefix + subDir);
162     }
163     return files;
164 }
165
166 QStringList QmlObserverTool::installDirectories(const QString &qtInstallData)
167 {
168     const QChar slash = QLatin1Char('/');
169     const uint hash = qHash(qtInstallData);
170     QStringList directories;
171     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;
175     return directories;
176 }
177
178 QString QmlObserverTool::sourcePath()
179 {
180     return Core::ICore::instance()->resourcePath() + QLatin1String("/qml/qmlobserver/");
181 }
182
183 QStringList QmlObserverTool::sourceFileNames()
184 {
185     return recursiveFileList(QDir(sourcePath()));
186 }
187
188 } // namespace