jsonkitspage.cpp 6.49 KB
Newer Older
1
2
/****************************************************************************
**
Eike Ziller's avatar
Eike Ziller committed
3
4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
5
6
7
8
9
10
11
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
Eike Ziller's avatar
Eike Ziller committed
12
13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Kai Koehne's avatar
Kai Koehne committed
14
** use the contact form at http://www.qt.io/contact-us.
15
16
17
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
Kai Koehne's avatar
Kai Koehne committed
18
19
20
21
22
23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
**
Eike Ziller's avatar
Eike Ziller committed
25
26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "jsonkitspage.h"
#include "jsonwizard.h"

#include "../iprojectmanager.h"
#include "../kit.h"
#include "../project.h"
#include "../projectexplorer.h"

#include <coreplugin/featureprovider.h>

#include <extensionsystem/pluginmanager.h>

Tobias Hunger's avatar
Tobias Hunger committed
43
#include <utils/algorithm.h>
44
#include <utils/macroexpander.h>
Eike Ziller's avatar
Eike Ziller committed
45
#include <utils/mimetypes/mimedatabase.h>
46
47
#include <utils/qtcassert.h>

48
49
using namespace Core;

50
51
namespace ProjectExplorer {

52
53
54
const char KEY_FEATURE[] = "feature";
const char KEY_CONDITION[] = "condition";

55
56
57
JsonKitsPage::JsonKitsPage(QWidget *parent) : TargetSetupPage(parent)
{ }

Orgad Shaneh's avatar
Orgad Shaneh committed
58
void JsonKitsPage::initializePage()
59
60
61
62
{
    JsonWizard *wiz = qobject_cast<JsonWizard *>(wizard());
    QTC_ASSERT(wiz, return);

Tobias Hunger's avatar
Tobias Hunger committed
63
    connect(wiz, &JsonWizard::filesPolished, this, &JsonKitsPage::setupProjectFiles);
64

65
    const QString platform = wiz->stringValue(QLatin1String("Platform"));
66
    const QSet<Id> preferred
67
            = evaluate(m_preferredFeatures, wiz->value(QLatin1String("PreferredFeatures")), wiz);
68
    const QSet<Id> required
69
            = evaluate(m_requiredFeatures, wiz->value(QLatin1String("RequiredFeatures")), wiz);
70
71
72

    setRequiredKitMatcher(KitMatcher([required](const Kit *k) { return k->hasFeatures(required); }));
    setPreferredKitMatcher(KitMatcher([platform, preferred](const Kit *k) { return k->hasPlatform(platform) && k->hasFeatures(preferred); }));
73
    setProjectPath(wiz->expander()->expand(unexpandedProjectPath()));
74
75
76
77
78
79
80
81
82

    TargetSetupPage::initializePage();
}

void JsonKitsPage::cleanupPage()
{
    JsonWizard *wiz = qobject_cast<JsonWizard *>(wizard());
    QTC_ASSERT(wiz, return);

83
    disconnect(wiz, &JsonWizard::allDone, this, 0);
84
85
86
87

    TargetSetupPage::cleanupPage();
}

88
89
90
91
92
93
94
95
96
97
void JsonKitsPage::setUnexpandedProjectPath(const QString &path)
{
    m_unexpandedProjectPath = path;
}

QString JsonKitsPage::unexpandedProjectPath() const
{
    return m_unexpandedProjectPath;
}

98
99
100
101
102
103
104
105
106
107
void JsonKitsPage::setRequiredFeatures(const QVariant &data)
{
    m_requiredFeatures = parseFeatures(data);
}

void JsonKitsPage::setPreferredFeatures(const QVariant &data)
{
    m_preferredFeatures = parseFeatures(data);
}

108
109
110
111
112
113
void JsonKitsPage::setupProjectFiles(const JsonWizard::GeneratorFiles &files)
{
    Project *project = 0;
    QList<IProjectManager *> managerList = ExtensionSystem::PluginManager::getObjects<IProjectManager>();

    foreach (const JsonWizard::GeneratorFile &f, files) {
114
        if (f.file.attributes() & GeneratedFile::OpenProjectAttribute) {
115
            QString errorMessage;
116
117
            const QFileInfo fi(f.file.path());
            const QString path = fi.absoluteFilePath();
118

Eike Ziller's avatar
Eike Ziller committed
119
120
121
            Utils::MimeDatabase mdb;
            Utils::MimeType mt = mdb.mimeTypeForFile(fi);
            if (!mt.isValid())
122
123
                continue;

Eike Ziller's avatar
Eike Ziller committed
124
            auto manager = Utils::findOrDefault(managerList, Utils::equal(&IProjectManager::mimeType, mt.name()));
Tobias Hunger's avatar
Tobias Hunger committed
125
            project = manager ? manager->openProject(path, &errorMessage) : 0;
126
            if (project) {
Tobias Hunger's avatar
Tobias Hunger committed
127
                if (setupProject(project))
128
129
                    project->saveSettings();
                delete project;
Tobias Hunger's avatar
Tobias Hunger committed
130
                project = 0;
131
132
133
134
135
            }
        }
    }
}

136
137
QSet<Id> JsonKitsPage::evaluate(const QVector<JsonKitsPage::ConditionalFeature> &list,
                                const QVariant &defaultSet, JsonWizard *wiz)
138
139
{
    if (list.isEmpty())
140
        return Id::fromStringList(defaultSet.toStringList());
141

142
    QSet<Id> features;
143
144
    foreach (const ConditionalFeature &f, list) {
        if (JsonWizard::boolFromVariant(f.condition, wiz->expander()))
145
            features.insert(Id::fromString(wiz->expander()->expand(f.feature)));
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
    }
    return features;
}

QVector<JsonKitsPage::ConditionalFeature> JsonKitsPage::parseFeatures(const QVariant &data,
                                                                      QString *errorMessage)
{
    QVector<ConditionalFeature> result;
    if (errorMessage)
        errorMessage->clear();

    if (data.isNull())
        return result;
    if (data.type() != QVariant::List) {
        if (errorMessage)
            *errorMessage = tr("Feature list is set and not of type list.");
        return result;
    }

    foreach (const QVariant &element, data.toList()) {
        if (element.type() == QVariant::String) {
167
            result.append({ element.toString(), QVariant(true) });
168
169
170
171
172
173
174
175
176
        } else if (element.type() == QVariant::Map) {
            const QVariantMap obj = element.toMap();
            const QString feature = obj.value(QLatin1String(KEY_FEATURE)).toString();
            if (feature.isEmpty()) {
                *errorMessage = tr("No \"%1\" key found in feature list object.")
                        .arg(QLatin1String(KEY_FEATURE));
                return QVector<ConditionalFeature>();
            }

177
            result.append({ feature, obj.value(QLatin1String(KEY_CONDITION), true) });
178
179
180
181
182
183
184
185
186
187
        } else {
            if (errorMessage)
                *errorMessage = tr("Feature list element is not a string or object.");
            return QVector<ConditionalFeature>();
        }
    }

    return result;
}

188
} // namespace ProjectExplorer