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 "customexecutablerunconfiguration.h"
34 #include "customexecutableconfigurationwidget.h"
36 #include "buildconfiguration.h"
37 #include "debugginghelper.h"
38 #include "projectexplorerconstants.h"
41 #include <coreplugin/icore.h>
43 #include <utils/qtcprocess.h>
45 #include <QtGui/QDialog>
46 #include <QtGui/QDialogButtonBox>
47 #include <QtGui/QLabel>
48 #include <QtGui/QMainWindow>
49 #include <QtGui/QVBoxLayout>
51 #include <QtCore/QDir>
53 using namespace ProjectExplorer;
54 using namespace ProjectExplorer::Internal;
57 const char * const CUSTOM_EXECUTABLE_ID("ProjectExplorer.CustomExecutableRunConfiguration");
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");
67 void CustomExecutableRunConfiguration::ctor()
69 setDefaultDisplayName(defaultDisplayName());
71 connect(target(), SIGNAL(activeBuildConfigurationChanged(ProjectExplorer::BuildConfiguration*)),
72 this, SLOT(activeBuildConfigurationChanged()));
74 m_lastActiveBuildConfiguration = activeBuildConfiguration();
76 if (m_lastActiveBuildConfiguration) {
77 connect(m_lastActiveBuildConfiguration, SIGNAL(environmentChanged()),
78 this, SIGNAL(baseEnvironmentChanged()));
82 CustomExecutableRunConfiguration::CustomExecutableRunConfiguration(Target *parent) :
83 LocalApplicationRunConfiguration(parent, QLatin1String(CUSTOM_EXECUTABLE_ID)),
84 m_workingDirectory(QLatin1String(ProjectExplorer::Constants::DEFAULT_WORKING_DIR)),
86 m_baseEnvironmentBase(CustomExecutableRunConfiguration::BuildEnvironmentBase)
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)
103 // Note: Qt4Project deletes all empty customexecrunconfigs for which isConfigured() == false.
104 CustomExecutableRunConfiguration::~CustomExecutableRunConfiguration()
108 void CustomExecutableRunConfiguration::activeBuildConfigurationChanged()
110 if (m_lastActiveBuildConfiguration) {
111 disconnect(m_lastActiveBuildConfiguration, SIGNAL(environmentChanged()),
112 this, SIGNAL(baseEnvironmentChanged()));
114 m_lastActiveBuildConfiguration = activeBuildConfiguration();
115 if (m_lastActiveBuildConfiguration) {
116 connect(m_lastActiveBuildConfiguration, SIGNAL(environmentChanged()),
117 this, SIGNAL(baseEnvironmentChanged()));
121 QString CustomExecutableRunConfiguration::executable() const
123 Utils::Environment env = environment();
124 QString exec = env.searchInPath(Utils::expandMacros(m_executable, macroExpander()),
125 QStringList() << workingDirectory());
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);
143 QString oldExecutable = m_executable;
144 QString oldWorkingDirectory = m_workingDirectory;
145 QString oldCmdArguments = m_cmdArguments;
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();
158 return QDir::cleanPath(exec);
161 QString CustomExecutableRunConfiguration::rawExecutable() const
166 bool CustomExecutableRunConfiguration::isConfigured() const
168 return !m_executable.isEmpty();
171 LocalApplicationRunConfiguration::RunMode CustomExecutableRunConfiguration::runMode() const
176 QString CustomExecutableRunConfiguration::workingDirectory() const
178 return QDir::cleanPath(environment().expandVariables(
179 Utils::expandMacros(baseWorkingDirectory(), macroExpander())));
182 QString CustomExecutableRunConfiguration::baseWorkingDirectory() const
184 return m_workingDirectory;
188 QString CustomExecutableRunConfiguration::commandLineArguments() const
190 return Utils::QtcProcess::expandMacros(m_cmdArguments, macroExpander());
193 QString CustomExecutableRunConfiguration::rawCommandLineArguments() const
195 return m_cmdArguments;
198 QString CustomExecutableRunConfiguration::baseEnvironmentText() const
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");
210 Utils::Environment CustomExecutableRunConfiguration::baseEnvironment() const
212 Utils::Environment env;
213 if (m_baseEnvironmentBase == CustomExecutableRunConfiguration::CleanEnvironmentBase) {
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();
224 void CustomExecutableRunConfiguration::setBaseEnvironmentBase(BaseEnvironmentBase env)
226 if (m_baseEnvironmentBase == env)
228 m_baseEnvironmentBase = env;
229 emit baseEnvironmentChanged();
232 CustomExecutableRunConfiguration::BaseEnvironmentBase CustomExecutableRunConfiguration::baseEnvironmentBase() const
234 return m_baseEnvironmentBase;
237 Utils::Environment CustomExecutableRunConfiguration::environment() const
239 Utils::Environment env = baseEnvironment();
240 env.modify(userEnvironmentChanges());
244 QList<Utils::EnvironmentItem> CustomExecutableRunConfiguration::userEnvironmentChanges() const
246 return m_userEnvironmentChanges;
249 void CustomExecutableRunConfiguration::setUserEnvironmentChanges(const QList<Utils::EnvironmentItem> &diff)
251 if (m_userEnvironmentChanges != diff) {
252 m_userEnvironmentChanges = diff;
253 emit userEnvironmentChangesChanged(diff);
257 QString CustomExecutableRunConfiguration::defaultDisplayName() const
259 if (m_executable.isEmpty())
260 return tr("Custom Executable");
262 return tr("Run %1").arg(QDir::toNativeSeparators(m_executable));
265 QVariantMap CustomExecutableRunConfiguration::toMap() const
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));
277 bool CustomExecutableRunConfiguration::fromMap(const QVariantMap &map)
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());
286 setDefaultDisplayName(defaultDisplayName());
287 return LocalApplicationRunConfiguration::fromMap(map);
290 void CustomExecutableRunConfiguration::setExecutable(const QString &executable)
292 if (executable == m_executable)
294 m_executable = executable;
295 setDefaultDisplayName(defaultDisplayName());
299 void CustomExecutableRunConfiguration::setCommandLineArguments(const QString &commandLineArguments)
301 m_cmdArguments = commandLineArguments;
305 void CustomExecutableRunConfiguration::setBaseWorkingDirectory(const QString &workingDirectory)
307 m_workingDirectory = workingDirectory;
311 void CustomExecutableRunConfiguration::setRunMode(RunMode runMode)
317 QWidget *CustomExecutableRunConfiguration::createConfigurationWidget()
319 return new CustomExecutableConfigurationWidget(this);
322 QString CustomExecutableRunConfiguration::dumperLibrary() const
324 QString qmakePath = ProjectExplorer::DebuggingHelperLibrary::findSystemQt(environment());
325 QString qtInstallData = ProjectExplorer::DebuggingHelperLibrary::qtInstallDataDir(qmakePath);
326 return ProjectExplorer::DebuggingHelperLibrary::debuggingHelperLibraryByInstallData(qtInstallData);
329 QStringList CustomExecutableRunConfiguration::dumperLibraryLocations() const
331 QString qmakePath = ProjectExplorer::DebuggingHelperLibrary::findSystemQt(environment());
332 QString qtInstallData = ProjectExplorer::DebuggingHelperLibrary::qtInstallDataDir(qmakePath);
333 return ProjectExplorer::DebuggingHelperLibrary::locationsByInstallData(qtInstallData);
336 ProjectExplorer::Abi CustomExecutableRunConfiguration::abi() const
338 return ProjectExplorer::Abi::hostAbi();
343 CustomExecutableRunConfigurationFactory::CustomExecutableRunConfigurationFactory(QObject *parent) :
344 ProjectExplorer::IRunConfigurationFactory(parent)
348 CustomExecutableRunConfigurationFactory::~CustomExecutableRunConfigurationFactory()
353 bool CustomExecutableRunConfigurationFactory::canCreate(Target *parent, const QString &id) const
356 return id == QLatin1String(CUSTOM_EXECUTABLE_ID);
359 RunConfiguration *CustomExecutableRunConfigurationFactory::create(Target *parent, const QString &id)
361 if (!canCreate(parent, id))
364 RunConfiguration *rc(new CustomExecutableRunConfiguration(parent));
368 bool CustomExecutableRunConfigurationFactory::canRestore(Target *parent, const QVariantMap &map) const
370 QString id(idFromMap(map));
371 return canCreate(parent, id);
374 RunConfiguration *CustomExecutableRunConfigurationFactory::restore(Target *parent, const QVariantMap &map)
376 if (!canRestore(parent, map))
378 CustomExecutableRunConfiguration *rc(new CustomExecutableRunConfiguration(parent));
379 if (rc->fromMap(map))
385 bool CustomExecutableRunConfigurationFactory::canClone(Target *parent, RunConfiguration *source) const
387 return canCreate(parent, source->id());
390 RunConfiguration *CustomExecutableRunConfigurationFactory::clone(Target *parent, RunConfiguration *source)
392 if (!canClone(parent, source))
394 return new CustomExecutableRunConfiguration(parent, static_cast<CustomExecutableRunConfiguration*>(source));
397 QStringList CustomExecutableRunConfigurationFactory::availableCreationIds(Target *parent) const
400 return QStringList() << QLatin1String(CUSTOM_EXECUTABLE_ID);
403 QString CustomExecutableRunConfigurationFactory::displayNameForId(const QString &id) const
405 if (id == QLatin1String(CUSTOM_EXECUTABLE_ID))
406 return tr("Custom Executable");