runconfiguration.cpp 6.83 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11
12
13
14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
#include "runconfiguration.h"
31

32
#include "outputformatter.h"
con's avatar
con committed
33
#include "project.h"
Tobias Hunger's avatar
Tobias Hunger committed
34
#include "target.h"
35
#include "buildconfiguration.h"
con's avatar
con committed
36

Tobias Hunger's avatar
Tobias Hunger committed
37
38
#include <extensionsystem/pluginmanager.h>

con's avatar
con committed
39
40
41
42
43
44
45
46
#include <QtCore/QTimer>

#ifdef Q_OS_MAC
#include <Carbon/Carbon.h>
#endif

using namespace ProjectExplorer;

Tobias Hunger's avatar
Tobias Hunger committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
namespace {
// Function objects:

class RunConfigurationFactoryMatcher
{
public:
    RunConfigurationFactoryMatcher(Target * target) : m_target(target)
    { }

    virtual bool operator()(IRunConfigurationFactory *) const = 0;

    Target *target() const
    {
        return m_target;
    }

private:
    Target *m_target;
};

class CreateMatcher : public RunConfigurationFactoryMatcher
{
public:
    CreateMatcher(Target *target, const QString &id) :
        RunConfigurationFactoryMatcher(target),
        m_id(id)
    { }
    ~CreateMatcher() { }

    bool operator()(IRunConfigurationFactory *factory) const
    {
        return factory->canCreate(target(), m_id);
    }

private:
    QString m_id;
};

class CloneMatcher : public RunConfigurationFactoryMatcher
{
public:
    CloneMatcher(Target *target, RunConfiguration *source) :
        RunConfigurationFactoryMatcher(target),
        m_source(source)
    { }
    ~CloneMatcher() { }

    bool operator()(IRunConfigurationFactory *factory) const
    {
        return factory->canClone(target(), m_source);
    }

private:
    RunConfiguration *m_source;
};

class RestoreMatcher : public RunConfigurationFactoryMatcher
{
public:
    RestoreMatcher(Target *target, const QVariantMap &map) :
        RunConfigurationFactoryMatcher(target),
        m_map(map)
    { }
    ~RestoreMatcher() { }

    bool operator()(IRunConfigurationFactory *factory) const
    {
        return factory->canRestore(target(), m_map);
    }

private:
    QVariantMap m_map;
};

// Helper methods:

IRunConfigurationFactory * findRunConfigurationFactory(RunConfigurationFactoryMatcher &matcher)
{
    QList<IRunConfigurationFactory *>
            factories(ExtensionSystem::PluginManager::instance()->
                getObjects<IRunConfigurationFactory>());
    foreach (IRunConfigurationFactory *factory, factories) {
        if (matcher(factory))
            return factory;
    }
    return 0;
}

} // namespace

con's avatar
con committed
137
// RunConfiguration
Tobias Hunger's avatar
Tobias Hunger committed
138
RunConfiguration::RunConfiguration(Target *target, const QString &id) :
139
    ProjectConfiguration(id),
Tobias Hunger's avatar
Tobias Hunger committed
140
    m_target(target)
con's avatar
con committed
141
{
Tobias Hunger's avatar
Tobias Hunger committed
142
    Q_ASSERT(m_target);
143
144
}

Tobias Hunger's avatar
Tobias Hunger committed
145
RunConfiguration::RunConfiguration(Target *target, RunConfiguration *source) :
146
    ProjectConfiguration(source),
Tobias Hunger's avatar
Tobias Hunger committed
147
    m_target(target)
148
{
Tobias Hunger's avatar
Tobias Hunger committed
149
    Q_ASSERT(m_target);
con's avatar
con committed
150
151
152
153
154
155
}

RunConfiguration::~RunConfiguration()
{
}

156
bool RunConfiguration::isEnabled(BuildConfiguration *bc) const
con's avatar
con committed
157
{
158
159
    Q_UNUSED(bc);
    return true;
con's avatar
con committed
160
161
}

162
163
bool RunConfiguration::isEnabled() const
{
Tobias Hunger's avatar
Tobias Hunger committed
164
165
    if (target()->project()->hasActiveBuildSettings()
        && !activeBuildConfiguration())
166
        return false;
Tobias Hunger's avatar
Tobias Hunger committed
167
    return isEnabled(activeBuildConfiguration());
168
169
}

170
BuildConfiguration *RunConfiguration::activeBuildConfiguration() const
con's avatar
con committed
171
{
Tobias Hunger's avatar
Tobias Hunger committed
172
    if (!target())
173
        return 0;
Tobias Hunger's avatar
Tobias Hunger committed
174
    return target()->activeBuildConfiguration();
con's avatar
con committed
175
176
}

Tobias Hunger's avatar
Tobias Hunger committed
177
Target *RunConfiguration::target() const
con's avatar
con committed
178
{
Tobias Hunger's avatar
Tobias Hunger committed
179
    return m_target;
con's avatar
con committed
180
181
}

dt's avatar
dt committed
182
183
184
185
186
ProjectExplorer::OutputFormatter *RunConfiguration::createOutputFormatter() const
{
    return new OutputFormatter();
}

187
188
IRunConfigurationFactory::IRunConfigurationFactory(QObject *parent) :
    QObject(parent)
con's avatar
con committed
189
190
191
192
193
194
195
{
}

IRunConfigurationFactory::~IRunConfigurationFactory()
{
}

Tobias Hunger's avatar
Tobias Hunger committed
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
IRunConfigurationFactory *IRunConfigurationFactory::createFactory(Target *parent, const QString &id)
{
    CreateMatcher matcher(parent, id);
    return findRunConfigurationFactory(matcher);
}

IRunConfigurationFactory *IRunConfigurationFactory::cloneFactory(Target *parent, RunConfiguration *source)
{
    CloneMatcher matcher(parent, source);
    return findRunConfigurationFactory(matcher);
}

IRunConfigurationFactory *IRunConfigurationFactory::restoreFactory(Target *parent, const QVariantMap &map)
{
    RestoreMatcher matcher(parent, map);
    return findRunConfigurationFactory(matcher);
}

214
IRunControlFactory::IRunControlFactory(QObject *parent)
215
    : QObject(parent)
con's avatar
con committed
216
217
218
{
}

219
IRunControlFactory::~IRunControlFactory()
con's avatar
con committed
220
221
222
{
}

dt's avatar
dt committed
223
RunControl::RunControl(RunConfiguration *runConfiguration, QString mode)
224
    : m_runMode(mode), m_runConfiguration(runConfiguration), m_outputFormatter(0)
con's avatar
con committed
225
{
dt's avatar
dt committed
226
    if (runConfiguration) {
227
        m_displayName  = runConfiguration->displayName();
dt's avatar
dt committed
228
229
230
231
232
        m_outputFormatter = runConfiguration->createOutputFormatter();
    }
    // We need to ensure that there's always a OutputFormatter
    if (!m_outputFormatter)
        m_outputFormatter = new OutputFormatter();
con's avatar
con committed
233
234
}

235
RunControl::~RunControl()
con's avatar
con committed
236
{
dt's avatar
dt committed
237
238
    delete m_outputFormatter;
}
239

dt's avatar
dt committed
240
241
242
OutputFormatter *RunControl::outputFormatter()
{
    return m_outputFormatter;
con's avatar
con committed
243
244
}

dt's avatar
dt committed
245
246
247
248
249
QString RunControl::runMode() const
{
    return m_runMode;
}

250
251
252
253
254
255
QString RunControl::displayName() const
{
    return m_displayName;
}

bool RunControl::sameRunConfiguration(RunControl *other)
con's avatar
con committed
256
{
257
    return other->m_runConfiguration.data() == m_runConfiguration.data();
con's avatar
con committed
258
259
260
261
262
263
264
265
}

void RunControl::bringApplicationToForeground(qint64 pid)
{
#ifdef Q_OS_MAC
    m_internalPid = pid;
    m_foregroundCount = 0;
    bringApplicationToForegroundInternal();
Friedemann Kleint's avatar
Friedemann Kleint committed
266
267
#else
    Q_UNUSED(pid)
con's avatar
con committed
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#endif
}

void RunControl::bringApplicationToForegroundInternal()
{
#ifdef Q_OS_MAC
    ProcessSerialNumber psn;
    GetProcessForPID(m_internalPid, &psn);
    if (SetFrontProcess(&psn) == procNotFound && m_foregroundCount < 15) {
        // somehow the mac/carbon api says
        // "-600 no eligible process with specified process id"
        // if we call SetFrontProcess too early
        ++m_foregroundCount;
        QTimer::singleShot(200, this, SLOT(bringApplicationToForegroundInternal()));
        return;
    }
#endif
}