OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / projectexplorer / customexecutablerunconfiguration.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 "customexecutablerunconfiguration.h"
34 #include "customexecutableconfigurationwidget.h"
35
36 #include "buildconfiguration.h"
37 #include "debugginghelper.h"
38 #include "projectexplorerconstants.h"
39 #include "target.h"
40
41 #include <coreplugin/icore.h>
42
43 #include <utils/qtcprocess.h>
44
45 #include <QtGui/QDialog>
46 #include <QtGui/QDialogButtonBox>
47 #include <QtGui/QLabel>
48 #include <QtGui/QMainWindow>
49 #include <QtGui/QVBoxLayout>
50
51 #include <QtCore/QDir>
52
53 using namespace ProjectExplorer;
54 using namespace ProjectExplorer::Internal;
55
56 namespace {
57 const char * const CUSTOM_EXECUTABLE_ID("ProjectExplorer.CustomExecutableRunConfiguration");
58
59 const char * const EXECUTABLE_KEY("ProjectExplorer.CustomExecutableRunConfiguration.Executable");
60 const char * const ARGUMENTS_KEY("ProjectExplorer.CustomExecutableRunConfiguration.Arguments");
61 const char * const WORKING_DIRECTORY_KEY("ProjectExplorer.CustomExecutableRunConfiguration.WorkingDirectory");
62 const char * const USE_TERMINAL_KEY("ProjectExplorer.CustomExecutableRunConfiguration.UseTerminal");
63 const char * const USER_ENVIRONMENT_CHANGES_KEY("ProjectExplorer.CustomExecutableRunConfiguration.UserEnvironmentChanges");
64 const char * const BASE_ENVIRONMENT_BASE_KEY("ProjectExplorer.CustomExecutableRunConfiguration.BaseEnvironmentBase");
65 }
66
67 void CustomExecutableRunConfiguration::ctor()
68 {
69     setDefaultDisplayName(defaultDisplayName());
70
71     connect(target(), SIGNAL(activeBuildConfigurationChanged(ProjectExplorer::BuildConfiguration*)),
72             this, SLOT(activeBuildConfigurationChanged()));
73
74     m_lastActiveBuildConfiguration = activeBuildConfiguration();
75
76     if (m_lastActiveBuildConfiguration) {
77         connect(m_lastActiveBuildConfiguration, SIGNAL(environmentChanged()),
78                 this, SIGNAL(baseEnvironmentChanged()));
79     }
80 }
81
82 CustomExecutableRunConfiguration::CustomExecutableRunConfiguration(Target *parent) :
83     LocalApplicationRunConfiguration(parent, QLatin1String(CUSTOM_EXECUTABLE_ID)),
84     m_workingDirectory(QLatin1String(ProjectExplorer::Constants::DEFAULT_WORKING_DIR)),
85     m_runMode(Gui),
86     m_baseEnvironmentBase(CustomExecutableRunConfiguration::BuildEnvironmentBase)
87 {
88     ctor();
89 }
90
91 CustomExecutableRunConfiguration::CustomExecutableRunConfiguration(Target *parent, CustomExecutableRunConfiguration *source) :
92     LocalApplicationRunConfiguration(parent, source),
93     m_executable(source->m_executable),
94     m_workingDirectory(source->m_workingDirectory),
95     m_cmdArguments(source->m_cmdArguments),
96     m_runMode(source->m_runMode),
97     m_userEnvironmentChanges(source->m_userEnvironmentChanges),
98     m_baseEnvironmentBase(source->m_baseEnvironmentBase)
99 {
100     ctor();
101 }
102
103 // Note: Qt4Project deletes all empty customexecrunconfigs for which isConfigured() == false.
104 CustomExecutableRunConfiguration::~CustomExecutableRunConfiguration()
105 {
106 }
107
108 void CustomExecutableRunConfiguration::activeBuildConfigurationChanged()
109 {
110     if (m_lastActiveBuildConfiguration) {
111         disconnect(m_lastActiveBuildConfiguration, SIGNAL(environmentChanged()),
112                    this, SIGNAL(baseEnvironmentChanged()));
113     }
114     m_lastActiveBuildConfiguration = activeBuildConfiguration();
115     if (m_lastActiveBuildConfiguration) {
116         connect(m_lastActiveBuildConfiguration, SIGNAL(environmentChanged()),
117                 this, SIGNAL(baseEnvironmentChanged()));
118     }
119 }
120
121 QString CustomExecutableRunConfiguration::executable() const
122 {
123     Utils::Environment env = environment();
124     QString exec = env.searchInPath(Utils::expandMacros(m_executable, macroExpander()),
125                                     QStringList() << workingDirectory());
126
127     if (exec.isEmpty() || !QFileInfo(exec).exists()) {
128         // Oh the executable doesn't exists, ask the user.
129         QWidget *confWidget = const_cast<CustomExecutableRunConfiguration *>(this)->createConfigurationWidget();
130         confWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
131         QDialog dialog(Core::ICore::instance()->mainWindow());
132         dialog.setLayout(new QVBoxLayout());
133         QLabel *label = new QLabel(tr("Could not find the executable, please specify one."));
134         label->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
135         dialog.layout()->addWidget(label);
136         dialog.layout()->addWidget(confWidget);
137         QDialogButtonBox *dbb = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
138         connect(dbb, SIGNAL(accepted()), &dialog, SLOT(accept()));
139         connect(dbb, SIGNAL(rejected()), &dialog, SLOT(reject()));
140         dialog.layout()->addWidget(dbb);
141         dialog.layout()->setSizeConstraint(QLayout::SetMinAndMaxSize);
142
143         QString oldExecutable = m_executable;
144         QString oldWorkingDirectory = m_workingDirectory;
145         QString oldCmdArguments = m_cmdArguments;
146
147         if (dialog.exec()) {
148             return executable();
149         } else {
150             CustomExecutableRunConfiguration *that = const_cast<CustomExecutableRunConfiguration *>(this);
151             that->m_executable = oldExecutable;
152             that->m_workingDirectory = oldWorkingDirectory;
153             that->m_cmdArguments = oldCmdArguments;
154             emit that->changed();
155             return QString();
156         }
157     }
158     return QDir::cleanPath(exec);
159 }
160
161 QString CustomExecutableRunConfiguration::rawExecutable() const
162 {
163     return m_executable;
164 }
165
166 bool CustomExecutableRunConfiguration::isConfigured() const
167 {
168     return !m_executable.isEmpty();
169 }
170
171 LocalApplicationRunConfiguration::RunMode CustomExecutableRunConfiguration::runMode() const
172 {
173     return m_runMode;
174 }
175
176 QString CustomExecutableRunConfiguration::workingDirectory() const
177 {
178     return QDir::cleanPath(environment().expandVariables(
179                 Utils::expandMacros(baseWorkingDirectory(), macroExpander())));
180 }
181
182 QString CustomExecutableRunConfiguration::baseWorkingDirectory() const
183 {
184     return m_workingDirectory;
185 }
186
187
188 QString CustomExecutableRunConfiguration::commandLineArguments() const
189 {
190     return Utils::QtcProcess::expandMacros(m_cmdArguments, macroExpander());
191 }
192
193 QString CustomExecutableRunConfiguration::rawCommandLineArguments() const
194 {
195     return m_cmdArguments;
196 }
197
198 QString CustomExecutableRunConfiguration::baseEnvironmentText() const
199 {
200     if (m_baseEnvironmentBase == CustomExecutableRunConfiguration::CleanEnvironmentBase) {
201         return tr("Clean Environment");
202     } else  if (m_baseEnvironmentBase == CustomExecutableRunConfiguration::SystemEnvironmentBase) {
203         return tr("System Environment");
204     } else  if (m_baseEnvironmentBase == CustomExecutableRunConfiguration::BuildEnvironmentBase) {
205         return tr("Build Environment");
206     }
207     return QString();
208 }
209
210 Utils::Environment CustomExecutableRunConfiguration::baseEnvironment() const
211 {
212     Utils::Environment env;
213     if (m_baseEnvironmentBase == CustomExecutableRunConfiguration::CleanEnvironmentBase) {
214         // Nothing
215     } else  if (m_baseEnvironmentBase == CustomExecutableRunConfiguration::SystemEnvironmentBase) {
216         env = Utils::Environment::systemEnvironment();
217     } else  if (m_baseEnvironmentBase == CustomExecutableRunConfiguration::BuildEnvironmentBase) {
218         if (activeBuildConfiguration())
219             env = activeBuildConfiguration()->environment();
220     }
221     return env;
222 }
223
224 void CustomExecutableRunConfiguration::setBaseEnvironmentBase(BaseEnvironmentBase env)
225 {
226     if (m_baseEnvironmentBase == env)
227         return;
228     m_baseEnvironmentBase = env;
229     emit baseEnvironmentChanged();
230 }
231
232 CustomExecutableRunConfiguration::BaseEnvironmentBase CustomExecutableRunConfiguration::baseEnvironmentBase() const
233 {
234     return m_baseEnvironmentBase;
235 }
236
237 Utils::Environment CustomExecutableRunConfiguration::environment() const
238 {
239     Utils::Environment env = baseEnvironment();
240     env.modify(userEnvironmentChanges());
241     return env;
242 }
243
244 QList<Utils::EnvironmentItem> CustomExecutableRunConfiguration::userEnvironmentChanges() const
245 {
246     return m_userEnvironmentChanges;
247 }
248
249 void CustomExecutableRunConfiguration::setUserEnvironmentChanges(const QList<Utils::EnvironmentItem> &diff)
250 {
251     if (m_userEnvironmentChanges != diff) {
252         m_userEnvironmentChanges = diff;
253         emit userEnvironmentChangesChanged(diff);
254     }
255 }
256
257 QString CustomExecutableRunConfiguration::defaultDisplayName() const
258 {
259     if (m_executable.isEmpty())
260         return tr("Custom Executable");
261     else
262         return tr("Run %1").arg(QDir::toNativeSeparators(m_executable));
263 }
264
265 QVariantMap CustomExecutableRunConfiguration::toMap() const
266 {
267     QVariantMap map(LocalApplicationRunConfiguration::toMap());
268     map.insert(QLatin1String(EXECUTABLE_KEY), m_executable);
269     map.insert(QLatin1String(ARGUMENTS_KEY), m_cmdArguments);
270     map.insert(QLatin1String(WORKING_DIRECTORY_KEY), m_workingDirectory);
271     map.insert(QLatin1String(USE_TERMINAL_KEY), m_runMode == Console);
272     map.insert(QLatin1String(USER_ENVIRONMENT_CHANGES_KEY), Utils::EnvironmentItem::toStringList(m_userEnvironmentChanges));
273     map.insert(QLatin1String(BASE_ENVIRONMENT_BASE_KEY), static_cast<int>(m_baseEnvironmentBase));
274     return map;
275 }
276
277 bool CustomExecutableRunConfiguration::fromMap(const QVariantMap &map)
278 {
279     m_executable = map.value(QLatin1String(EXECUTABLE_KEY)).toString();
280     m_cmdArguments = map.value(QLatin1String(ARGUMENTS_KEY)).toString();
281     m_workingDirectory = map.value(QLatin1String(WORKING_DIRECTORY_KEY)).toString();
282     m_runMode = map.value(QLatin1String(USE_TERMINAL_KEY)).toBool() ? Console : Gui;
283     m_userEnvironmentChanges = Utils::EnvironmentItem::fromStringList(map.value(QLatin1String(USER_ENVIRONMENT_CHANGES_KEY)).toStringList());
284     m_baseEnvironmentBase = static_cast<BaseEnvironmentBase>(map.value(QLatin1String(BASE_ENVIRONMENT_BASE_KEY), static_cast<int>(CustomExecutableRunConfiguration::BuildEnvironmentBase)).toInt());
285
286     setDefaultDisplayName(defaultDisplayName());
287     return LocalApplicationRunConfiguration::fromMap(map);
288 }
289
290 void CustomExecutableRunConfiguration::setExecutable(const QString &executable)
291 {
292     if (executable == m_executable)
293         return;
294     m_executable = executable;
295     setDefaultDisplayName(defaultDisplayName());
296     emit changed();
297 }
298
299 void CustomExecutableRunConfiguration::setCommandLineArguments(const QString &commandLineArguments)
300 {
301     m_cmdArguments = commandLineArguments;
302     emit changed();
303 }
304
305 void CustomExecutableRunConfiguration::setBaseWorkingDirectory(const QString &workingDirectory)
306 {
307     m_workingDirectory = workingDirectory;
308     emit changed();
309 }
310
311 void CustomExecutableRunConfiguration::setRunMode(RunMode runMode)
312 {
313     m_runMode = runMode;
314     emit changed();
315 }
316
317 QWidget *CustomExecutableRunConfiguration::createConfigurationWidget()
318 {
319     return new CustomExecutableConfigurationWidget(this);
320 }
321
322 QString CustomExecutableRunConfiguration::dumperLibrary() const
323 {
324     QString qmakePath = ProjectExplorer::DebuggingHelperLibrary::findSystemQt(environment());
325     QString qtInstallData = ProjectExplorer::DebuggingHelperLibrary::qtInstallDataDir(qmakePath);
326     return ProjectExplorer::DebuggingHelperLibrary::debuggingHelperLibraryByInstallData(qtInstallData);
327 }
328
329 QStringList CustomExecutableRunConfiguration::dumperLibraryLocations() const
330 {
331     QString qmakePath = ProjectExplorer::DebuggingHelperLibrary::findSystemQt(environment());
332     QString qtInstallData = ProjectExplorer::DebuggingHelperLibrary::qtInstallDataDir(qmakePath);
333     return ProjectExplorer::DebuggingHelperLibrary::locationsByInstallData(qtInstallData);
334 }
335
336 ProjectExplorer::Abi CustomExecutableRunConfiguration::abi() const
337 {
338     return ProjectExplorer::Abi::hostAbi();
339 }
340
341 // Factory
342
343 CustomExecutableRunConfigurationFactory::CustomExecutableRunConfigurationFactory(QObject *parent) :
344     ProjectExplorer::IRunConfigurationFactory(parent)
345 {
346 }
347
348 CustomExecutableRunConfigurationFactory::~CustomExecutableRunConfigurationFactory()
349 {
350
351 }
352
353 bool CustomExecutableRunConfigurationFactory::canCreate(Target *parent, const QString &id) const
354 {
355     Q_UNUSED(parent);
356     return id == QLatin1String(CUSTOM_EXECUTABLE_ID);
357 }
358
359 RunConfiguration *CustomExecutableRunConfigurationFactory::create(Target *parent, const QString &id)
360 {
361     if (!canCreate(parent, id))
362         return 0;
363
364     RunConfiguration *rc(new CustomExecutableRunConfiguration(parent));
365     return rc;
366 }
367
368 bool CustomExecutableRunConfigurationFactory::canRestore(Target *parent, const QVariantMap &map) const
369 {
370     QString id(idFromMap(map));
371     return canCreate(parent, id);
372 }
373
374 RunConfiguration *CustomExecutableRunConfigurationFactory::restore(Target *parent, const QVariantMap &map)
375 {
376     if (!canRestore(parent, map))
377         return 0;
378     CustomExecutableRunConfiguration *rc(new CustomExecutableRunConfiguration(parent));
379     if (rc->fromMap(map))
380         return rc;
381     delete rc;
382     return 0;
383 }
384
385 bool CustomExecutableRunConfigurationFactory::canClone(Target *parent, RunConfiguration *source) const
386 {
387     return canCreate(parent, source->id());
388 }
389
390 RunConfiguration *CustomExecutableRunConfigurationFactory::clone(Target *parent, RunConfiguration *source)
391 {
392     if (!canClone(parent, source))
393         return 0;
394     return new CustomExecutableRunConfiguration(parent, static_cast<CustomExecutableRunConfiguration*>(source));
395 }
396
397 QStringList CustomExecutableRunConfigurationFactory::availableCreationIds(Target *parent) const
398 {
399     Q_UNUSED(parent)
400     return QStringList() << QLatin1String(CUSTOM_EXECUTABLE_ID);
401 }
402
403 QString CustomExecutableRunConfigurationFactory::displayNameForId(const QString &id) const
404 {
405     if (id == QLatin1String(CUSTOM_EXECUTABLE_ID))
406         return tr("Custom Executable");
407     return QString();
408 }