OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qt4projectmanager / qmldebugginglibrary.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2010 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 "qmldebugginglibrary.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 <QDesktopServices>
42 #include <QCoreApplication>
43 #include <QDir>
44 #include <QDebug>
45
46 namespace Qt4ProjectManager {
47
48
49 QString QmlDebuggingLibrary::libraryByInstallData(const QString &qtInstallData, bool debugBuild)
50 {
51     if (!Core::ICore::instance())
52         return QString();
53
54     const QStringList directories = installDirectories(qtInstallData);
55
56     QStringList binFilenames;
57     if (debugBuild) {
58         binFilenames << QLatin1String("QmlJSDebuggerd.lib");
59         binFilenames << QLatin1String("libQmlJSDebuggerd.a"); // mingw
60     } else {
61         binFilenames << QLatin1String("QmlJSDebugger.lib");
62     }
63     binFilenames << QLatin1String("libQmlJSDebugger.a");
64
65     return byInstallDataHelper(sourcePath(), sourceFileNames(), directories, binFilenames);
66 }
67
68 bool QmlDebuggingLibrary::canBuild(const QtVersion *qtVersion)
69 {
70     return qtVersion->qtVersion() >=  QtVersionNumber(4, 7, 1);
71 }
72
73 bool  QmlDebuggingLibrary::build(const QString &directory, const QString &makeCommand,
74                              const QString &qmakeCommand, const QString &mkspec,
75                              const Utils::Environment &env, const QString &targetMode,
76                              const QStringList &qmakeArguments, QString *output,  QString *errorMessage)
77 {
78     return buildHelper(QCoreApplication::translate("Qt4ProjectManager::QmlDebuggingLibrary", "QML Debugging"),
79                        QLatin1String("qmljsdebugger.pro"),
80                        directory, makeCommand, qmakeCommand, mkspec, env, targetMode,
81                        qmakeArguments, output, errorMessage);
82 }
83
84 static inline bool mkpath(const QString &targetDirectory, QString *errorMessage)
85 {
86     if (!QDir().mkpath(targetDirectory)) {
87         *errorMessage = QCoreApplication::translate("Qt4ProjectManager::QmlDebuggingLibrary", "The target directory %1 could not be created.").arg(targetDirectory);
88         return false;
89     }
90     return true;
91 }
92
93 QString QmlDebuggingLibrary::copy(const QString &qtInstallData, QString *errorMessage)
94 {
95     const QStringList directories = QmlDebuggingLibrary::installDirectories(qtInstallData);
96
97     // Try to find a writeable directory.
98     foreach (const QString &directory, directories) {
99         if (!mkpath(directory, errorMessage)) {
100             continue;
101         } else {
102             errorMessage->clear();
103         }
104
105         if (copyFiles(sourcePath(), sourceFileNames(),
106                       directory, errorMessage))
107         {
108             errorMessage->clear();
109             return directory;
110         }
111     }
112     *errorMessage = QCoreApplication::translate("Qt4ProjectManager::QmlDebuggingLibrary",
113                                                 "QML Debugging library could not be built in any of the directories:\n- %1\n\nReason: %2")
114                     .arg(directories.join(QLatin1String("\n- ")), *errorMessage);
115     return QString();
116 }
117
118 QStringList QmlDebuggingLibrary::recursiveFileList(const QDir &dir, const QString &prefix)
119 {
120     QStringList files;
121
122     QString _prefix = prefix;
123     if (!_prefix.isEmpty() && !_prefix.endsWith('/')) {
124         _prefix = _prefix + '/';
125     }
126     foreach (const QString &fileName, dir.entryList(QDir::Files)) {
127         files << _prefix + fileName;
128     }
129
130     foreach (const QString &subDir, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
131         files += recursiveFileList(QDir(dir.absoluteFilePath(subDir)), _prefix + subDir);
132     }
133     return files;
134 }
135
136 QStringList QmlDebuggingLibrary::installDirectories(const QString &qtInstallData)
137 {
138     const QChar slash = QLatin1Char('/');
139     const uint hash = qHash(qtInstallData);
140     QStringList directories;
141     directories
142             << (qtInstallData + QLatin1String("/qtc-qmldbg/"))
143             << QDir::cleanPath((QCoreApplication::applicationDirPath() + QLatin1String("/../qtc-qmldbg/") + QString::number(hash))) + slash
144             << (QDesktopServices::storageLocation(QDesktopServices::DataLocation) + QLatin1String("/qtc-qmldbg/") + QString::number(hash)) + slash;
145     return directories;
146 }
147
148 QString QmlDebuggingLibrary::sourcePath()
149 {
150     return Core::ICore::instance()->resourcePath() + QLatin1String("/qml/qmljsdebugger/");
151 }
152
153 QStringList QmlDebuggingLibrary::sourceFileNames()
154 {
155     return recursiveFileList(QDir(sourcePath()));
156 }
157
158 } // namespace