addkitoperation.cpp 14.9 KB
Newer Older
Tobias Hunger's avatar
Tobias Hunger committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: http://www.qt-project.org/
**
**
** GNU Lesser General Public License Usage
**
** 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.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**************************************************************************/

#include "addkitoperation.h"

#include "addkeysoperation.h"
#include "findkeyoperation.h"
#include "findvalueoperation.h"
#include "getoperation.h"
#include "rmkeysoperation.h"

#include "settings.h"

#include <iostream>

// Qt version file stuff:
static char PREFIX[] = "Profile.";
static char VERSION[] = "Version";
static char COUNT[] = "Profile.Count";
static char DEFAULT[] = "Profile.Default";

// Kit:
static char ID[] = "PE.Profile.Id";
static char DISPLAYNAME[] = "PE.Profile.Name";
static char ICON[] = "PE.Profile.Icon";
static char AUTODETECTED[] = "PE.Profile.Autodetected";
static char DATA[] = "PE.Profile.Data";

// Standard KitInformation:
static char DEBUGGER[] = "Debugger.Information";
static char DEBUGGER_ENGINE[] = "EngineType";
static char DEBUGGER_BINARY[] = "Binary";
static char DEVICE_TYPE[] = "PE.Profile.DeviceType";
static char SYSROOT[] = "PE.Profile.SysRoot";
static char TOOLCHAIN[] = "PE.Profile.ToolChain";
static char MKSPEC[] = "QtPM4.mkSpecInformation";
static char QT[] = "QtSupport.QtInformation";

66
67
68
69
70
71
AddKitOperation::AddKitOperation()
    : m_debuggerEngine(0)
    , m_debugger(QLatin1String("auto"))
{
}

Tobias Hunger's avatar
Tobias Hunger committed
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
QString AddKitOperation::name() const
{
    return QLatin1String("addKit");
}

QString AddKitOperation::helpText() const
{
    return QLatin1String("add a Kit to Qt Creator");
}

QString AddKitOperation::argumentsHelpText() const
{
    return QLatin1String("    --id <ID>                                  id of the new kit.\n"
                         "    --name <NAME>                              display name of the new kit.\n"
                         "    --icon <PATH>                              icon of the new kit.\n"
                         "    --debuggerengine <ENGINE>                  debuggerengine of the new kit.\n"
                         "    --debugger <PATH>                          debugger of the new kit.\n"
                         "    --devicetype <TYPE>                        device type of the new kit.\n"
                         "    --sysroot <PATH>                           sysroot of the new kit.\n"
                         "    --toolchain <ID>                           tool chain of the new kit.\n"
                         "    --qt <ID>                                  Qt of the new kit.\n"
                         "    --mkspec <PATH>                            mkspec of the new kit.\n"
                         "    <KEY> <TYPE:VALUE>                         extra key value pairs\n");
}

bool AddKitOperation::setArguments(const QStringList &args)
{
    m_debuggerEngine = 0;

    for (int i = 0; i < args.count(); ++i) {
        const QString current = args.at(i);
        const QString next = ((i + 1) < args.count()) ? args.at(i + 1) : QString();

        if (current == QLatin1String("--id")) {
            if (next.isNull())
                return false;
            ++i; // skip next;
            m_id = next;
            continue;
        }

        if (current == QLatin1String("--name")) {
            if (next.isNull())
                return false;
            ++i; // skip next;
            m_displayName = next;
            continue;
        }

        if (current == QLatin1String("--icon")) {
            if (next.isNull())
                return false;
            ++i; // skip next;
            m_icon = next;
            continue;
        }

        if (current == QLatin1String("--debuggerengine")) {
            if (next.isNull())
                return false;
            ++i; // skip next;
            bool ok;
            m_debuggerEngine = next.toInt(&ok);
            if (!ok) {
                std::cerr << "Debugger type is not an integer!" << std::endl;
                return false;
            }
            continue;
        }

        if (current == QLatin1String("--debugger")) {
            if (next.isNull())
                return false;
            ++i; // skip next;
            m_debugger = next;
            continue;
        }

        if (current == QLatin1String("--devicetype")) {
            if (next.isNull())
                return false;
            ++i; // skip next;
            m_deviceType = next;
            continue;
        }

        if (current == QLatin1String("--sysroot")) {
            if (next.isNull())
                return false;
            ++i; // skip next;
            m_sysRoot = next;
            continue;
        }

        if (current == QLatin1String("--toolchain")) {
            if (next.isNull())
                return false;
            ++i; // skip next;
            m_tc = next;
            continue;
        }

        if (current == QLatin1String("--qt")) {
            if (next.isNull())
                return false;
            ++i; // skip next;
            m_qt = next;
            continue;
        }

        if (current == QLatin1String("--mkspec")) {
            if (next.isNull())
                return false;
            ++i; // skip next;
            m_mkspec = next;
            continue;
        }

        if (next.isNull())
            return false;
        ++i; // skip next;
        KeyValuePair pair(current, next);
        if (!pair.value.isValid())
            return false;
        m_extra << pair;
    }

    if (m_icon.isEmpty())
        m_icon = QLatin1String(":///DESKTOP///");

    return !m_id.isEmpty() && !m_displayName.isEmpty() && !m_deviceType.isEmpty();
}

int AddKitOperation::execute() const
{
    QVariantMap map = load(QLatin1String("profiles"));
    if (map.isEmpty())
        map = initializeKits();

    map = addKit(map, m_id, m_displayName, m_icon, m_debuggerEngine, m_debugger,
                 m_deviceType, m_sysRoot, m_tc, m_qt, m_mkspec, m_extra);

    if (map.isEmpty())
        return -2;

    return save(map, QLatin1String("profiles")) ? 0 : -3;
}

#ifdef WITH_TESTS
bool AddKitOperation::test() const
{
    QVariantMap map = initializeKits();

225
    if (map.count() != 3
Tobias Hunger's avatar
Tobias Hunger committed
226
227
228
229
230
231
232
233
234
235
236
237
238
239
            || !map.contains(QLatin1String(VERSION))
            || map.value(QLatin1String(VERSION)).toInt() != 1
            || !map.contains(QLatin1String(COUNT))
            || map.value(QLatin1String(COUNT)).toInt() != 0
            || !map.contains(QLatin1String(DEFAULT))
            || map.value(QLatin1String(DEFAULT)).toInt() != -1)
        return false;

    map = addKit(map, QLatin1String("testId"), QLatin1String("Test Qt Version"), QLatin1String("/tmp/icon.png"),
                 1, QLatin1String("/usr/bin/gdb-test"),
                 QLatin1String("Desktop"), QString(),
                 QLatin1String("{some-tc-id}"), QLatin1String("{some-qt-id}"), QLatin1String("unsupported/mkspec"),
                 KeyValuePairList() << KeyValuePair(QLatin1String("PE.Profile.Data/extraData"), QVariant(QLatin1String("extraValue"))));

240
    if (map.count() != 4
Tobias Hunger's avatar
Tobias Hunger committed
241
242
243
244
245
246
247
248
249
250
            || !map.contains(QLatin1String(VERSION))
            || map.value(QLatin1String(VERSION)).toInt() != 1
            || !map.contains(QLatin1String(COUNT))
            || map.value(QLatin1String(COUNT)).toInt() != 1
            || !map.contains(QLatin1String(DEFAULT))
            || map.value(QLatin1String(DEFAULT)).toInt() != 0
            || !map.contains(QLatin1String("Profile.0")))
        return false;

    QVariantMap profile0 = map.value(QLatin1String("Profile.0")).toMap();
251
    if (profile0.count() != 6
Tobias Hunger's avatar
Tobias Hunger committed
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
            || !profile0.contains(QLatin1String(ID))
            || profile0.value(QLatin1String(ID)).toString() != QLatin1String("testId")
            || !profile0.contains(QLatin1String(DISPLAYNAME))
            || profile0.value(QLatin1String(DISPLAYNAME)).toString() != QLatin1String("Test Qt Version")
            || !profile0.contains(QLatin1String(AUTODETECTED))
            || profile0.value(QLatin1String(AUTODETECTED)).toBool() != true)
        return false;

    // Ignore existing ids:
    QVariantMap result = addKit(map, QLatin1String("testId"), QLatin1String("Test Qt Version X"), QLatin1String("/tmp/icon3.png"),
                                1, QLatin1String("/usr/bin/gdb-test3"),
                                QLatin1String("Desktop"), QString(),
                                QLatin1String("{some-tc-id3}"), QLatin1String("{some-qt-id3}"), QLatin1String("unsupported/mkspec3"),
                                KeyValuePairList() << KeyValuePair(QLatin1String("PE.Profile.Data/extraData"), QVariant(QLatin1String("extraValue3"))));
    if (!result.isEmpty())
        return false;

    // Make sure name is unique:
    map = addKit(map, QLatin1String("testId2"), QLatin1String("Test Qt Version"), QLatin1String("/tmp/icon2.png"),
                 1, QLatin1String("/usr/bin/gdb-test2"),
                 QLatin1String("Desktop"), QString(),
                 QLatin1String("{some-tc-id2}"), QLatin1String("{some-qt-id2}"), QLatin1String("unsupported/mkspec2"),
                 KeyValuePairList() << KeyValuePair(QLatin1String("PE.Profile.Data/extraData"), QVariant(QLatin1String("extraValue2"))));
275
    if (map.count() != 5
Tobias Hunger's avatar
Tobias Hunger committed
276
277
278
279
280
281
282
283
284
285
286
287
288
            || !map.contains(QLatin1String(VERSION))
            || map.value(QLatin1String(VERSION)).toInt() != 1
            || !map.contains(QLatin1String(COUNT))
            || map.value(QLatin1String(COUNT)).toInt() != 2
            || !map.contains(QLatin1String(DEFAULT))
            || map.value(QLatin1String(DEFAULT)).toInt() != 0
            || !map.contains(QLatin1String("Profile.0"))
            || !map.contains(QLatin1String("Profile.1")))

    if (map.value(QLatin1String("Profile.0")) != profile0)
        return false;

    QVariantMap profile1 = map.value(QLatin1String("Profile.1")).toMap();
289
    if (profile1.count() != 6
Tobias Hunger's avatar
Tobias Hunger committed
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
            || !profile1.contains(QLatin1String(ID))
            || profile1.value(QLatin1String(ID)).toString() != QLatin1String("testId2")
            || !profile1.contains(QLatin1String(DISPLAYNAME))
            || profile1.value(QLatin1String(DISPLAYNAME)).toString() != QLatin1String("Test Qt Version2")
            || !profile1.contains(QLatin1String(AUTODETECTED))
            || profile1.value(QLatin1String(AUTODETECTED)).toBool() != true)
        return false;

    return true;
}
#endif

QVariantMap AddKitOperation::addKit(const QVariantMap &map,
                                    const QString &id, const QString &displayName, const QString &icon,
                                    const quint32 &debuggerType, const QString &debugger,
                                    const QString &deviceType, const QString &sysRoot,
                                    const QString &tc, const QString &qt, const QString &mkspec,
                                    const KeyValuePairList &extra)
{
    // Sanity check: Make sure autodetection source is not in use already:
    QStringList valueKeys = FindValueOperation::findValues(map, QVariant(id));
    bool hasId = false;
    foreach (const QString &k, valueKeys) {
        if (k.endsWith(QString(QLatin1Char('/')) + QLatin1String(ID))) {
            hasId = true;
            break;
        }
    }
    if (hasId) {
        std::cerr << "Error: Id " << qPrintable(id) << " already defined as kit." << std::endl;
        return QVariantMap();
    }

    // Find position to insert:
    bool ok;
    int count = GetOperation::get(map, QLatin1String(COUNT)).toInt(&ok);
    if (!ok || count < 0) {
        std::cerr << "Error: Count found in kits file seems wrong." << std::endl;
        return QVariantMap();
    }
    const QString kit = QString::fromLatin1(PREFIX) + QString::number(count);

    int defaultKit = GetOperation::get(map, QLatin1String(DEFAULT)).toInt(&ok);
    if (!ok) {
        std::cerr << "Error: Default kit seems wrong." << std::endl;
        return QVariantMap();
    }

    if (defaultKit < 0)
        defaultKit = 0;

    // remove data:
    QStringList toRemove;
    toRemove << QLatin1String(COUNT) << QLatin1String(DEFAULT);
    QVariantMap cleaned = RmKeysOperation::rmKeys(map, toRemove);

    // Sanity check: Make sure displayName is unique.
    QStringList nameKeys = FindKeyOperation::findKey(map, QLatin1String(DISPLAYNAME));
    QStringList nameList;
    foreach (const QString nameKey, nameKeys)
        nameList << GetOperation::get(map, nameKey).toString();
    const QString uniqueName = makeUnique(displayName, nameList);

    // insert data:
    KeyValuePairList data;
    data << KeyValuePair(QStringList() << kit << QLatin1String(ID), QVariant(id));
    data << KeyValuePair(QStringList() << kit << QLatin1String(DISPLAYNAME), QVariant(uniqueName));
    data << KeyValuePair(QStringList() << kit << QLatin1String(ICON), QVariant(icon));
    data << KeyValuePair(QStringList() << kit << QLatin1String(AUTODETECTED), QVariant(true));

    data << KeyValuePair(QStringList() << kit << QLatin1String(DATA)
                         << QLatin1String(DEBUGGER) << QLatin1String(DEBUGGER_ENGINE), QVariant(debuggerType));
    data << KeyValuePair(QStringList() << kit << QLatin1String(DATA)
                         << QLatin1String(DEBUGGER) << QLatin1String(DEBUGGER_BINARY), QVariant(debugger));
    data << KeyValuePair(QStringList() << kit << QLatin1String(DATA) << DEVICE_TYPE, QVariant(deviceType));
    data << KeyValuePair(QStringList() << kit << QLatin1String(DATA) << SYSROOT, QVariant(sysRoot));
    data << KeyValuePair(QStringList() << kit << QLatin1String(DATA) << TOOLCHAIN, QVariant(tc));
    data << KeyValuePair(QStringList() << kit << QLatin1String(DATA) << QT, QVariant(qt));
    data << KeyValuePair(QStringList() << kit << QLatin1String(DATA) << MKSPEC, QVariant(mkspec));

    data << KeyValuePair(QStringList() << QLatin1String(DEFAULT), QVariant(defaultKit));
    data << KeyValuePair(QStringList() << QLatin1String(COUNT), QVariant(count + 1));

    KeyValuePairList qtExtraList;
    foreach (const KeyValuePair &pair, extra)
        qtExtraList << KeyValuePair(QStringList() << kit << pair.key, pair.value);
    data.append(qtExtraList);

    return AddKeysOperation::addKeys(cleaned, data);
}

QVariantMap AddKitOperation::initializeKits()
{
    QVariantMap map;
    map.insert(QLatin1String(VERSION), 1);
    map.insert(QLatin1String(DEFAULT), -1);
    map.insert(QLatin1String(COUNT), 0);
    return map;
}