settingsaccessor.cpp 117 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** 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
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
23
24
25
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29

30
#include "settingsaccessor.h"
31

32
#include "abi.h"
33
#include "buildconfiguration.h"
Tobias Hunger's avatar
Tobias Hunger committed
34
#include "devicesupport/devicemanager.h"
35
#include "project.h"
36
37
#include "projectexplorer.h"
#include "projectexplorersettings.h"
Tobias Hunger's avatar
Tobias Hunger committed
38
#include "target.h"
39
40
#include "toolchain.h"
#include "toolchainmanager.h"
Tobias Hunger's avatar
Tobias Hunger committed
41
42
#include "kit.h"
#include "kitmanager.h"
43

dt's avatar
dt committed
44
#include <coreplugin/icore.h>
45
#include <coreplugin/idocument.h>
Tobias Hunger's avatar
Tobias Hunger committed
46
#include <extensionsystem/pluginmanager.h>
47
#include <utils/persistentsettings.h>
48
#include <utils/hostosinfo.h>
49
#include <utils/qtcassert.h>
50
#include <utils/qtcprocess.h>
51

52
#include <QApplication>
53
#include <QFile>
54
#include <QMessageBox>
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
namespace ProjectExplorer {
namespace Internal {

// -------------------------------------------------------------------------
// UserFileVersionHandler
// -------------------------------------------------------------------------
class UserFileVersionHandler
{
public:
    UserFileVersionHandler();
    virtual ~UserFileVersionHandler();

    // The user file version this handler accepts for input.
    virtual int userFileVersion() const = 0;
    virtual QString displayUserFileVersion() const = 0;
    // Update from userFileVersion() to userFileVersion() + 1
    virtual QVariantMap update(Project *project, const QVariantMap &map) = 0;

protected:
    typedef QPair<QLatin1String,QLatin1String> Change;
    QVariantMap renameKeys(const QList<Change> &changes, QVariantMap map);
};

UserFileVersionHandler::UserFileVersionHandler()
{
}

UserFileVersionHandler::~UserFileVersionHandler()
{
}

/**
 * Performs a simple renaming of the listed keys in \a changes recursively on \a map.
 */
QVariantMap UserFileVersionHandler::renameKeys(const QList<Change> &changes, QVariantMap map)
{
    foreach (const Change &change, changes) {
        QVariantMap::iterator oldSetting = map.find(change.first);
        if (oldSetting != map.end()) {
            map.insert(change.second, oldSetting.value());
            map.erase(oldSetting);
        }
    }

    QVariantMap::iterator i = map.begin();
    while (i != map.end()) {
        QVariant v = i.value();
        if (v.type() == QVariant::Map)
            i.value() = renameKeys(changes, v.toMap());

        ++i;
    }

    return map;
}

} // Internal
} // ProjectExplorer


116
using namespace ProjectExplorer;
117
using namespace Internal;
Jarek Kobus's avatar
Jarek Kobus committed
118
119
using Utils::PersistentSettingsReader;
using Utils::PersistentSettingsWriter;
120
121

namespace {
122

123
const char USER_STICKY_KEYS_KEY[] = "ProjectExplorer.Project.UserStickyKeys";
124

125
const char SHARED_SETTINGS[] = "SharedSettings";
126

127
// Version 0 is used in Qt Creator 1.3.x and
128
// (in a slighly different flavour) post 1.3 master.
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
class Version0Handler : public UserFileVersionHandler
{
public:
    int userFileVersion() const
    {
        return 0;
    }

    QString displayUserFileVersion() const
    {
        return QLatin1String("1.3");
    }

    QVariantMap update(Project *project, const QVariantMap &map);

private:
    QVariantMap convertBuildConfigurations(Project *project, const QVariantMap &map);
    QVariantMap convertRunConfigurations(Project *project, const QVariantMap &map);
    QVariantMap convertBuildSteps(Project *project, const QVariantMap &map);
};

Tobias Hunger's avatar
Tobias Hunger committed
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
// Version 1 is used in master post Qt Creator 1.3.x.
// It was never used in any official release but is required for the
// transition to later versions (which introduce support for targets).
class Version1Handler : public UserFileVersionHandler
{
public:
    int userFileVersion() const
    {
        return 1;
    }

    QString displayUserFileVersion() const
    {
        return QLatin1String("1.3+git");
    }

    QVariantMap update(Project *project, const QVariantMap &map);

private:
    struct TargetDescription
    {
        TargetDescription(QString tid, QString dn) :
            id(tid),
            displayName(dn)
        {
        }

        TargetDescription(const TargetDescription &td) :
            id(td.id),
            displayName(td.displayName)
        {
        }

        QString id;
        QString displayName;
    };
};

188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Version 2 is used in master post Qt Creator 2.0 alpha.
class Version2Handler : public UserFileVersionHandler
{
public:
    int userFileVersion() const
    {
        return 2;
    }

    QString displayUserFileVersion() const
    {
        return QLatin1String("2.0-alpha+git");
    }

    QVariantMap update(Project *project, const QVariantMap &map);
};

205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Version 3 reflect the move of symbian signing from run to build step.
class Version3Handler : public UserFileVersionHandler
{
public:
    int userFileVersion() const
    {
        return 3;
    }

    QString displayUserFileVersion() const
    {
        return QLatin1String("2.0-alpha2+git");
    }

    QVariantMap update(Project *project, const QVariantMap &map);
};

Tobias Hunger's avatar
Tobias Hunger committed
222
223
224
225
226
227
228
229
230
231
232
// Version 4 reflects the introduction of deploy steps
class Version4Handler : public UserFileVersionHandler
{
public:
    int userFileVersion() const
    {
        return 4;
    }

    QString displayUserFileVersion() const
    {
233
        return QLatin1String("2.1pre1");
Tobias Hunger's avatar
Tobias Hunger committed
234
235
236
237
238
    }

    QVariantMap update(Project *project, const QVariantMap &map);
};

239
240
241
242
243
244
245
246
247
248
249
// Version 5 reflects the introduction of new deploy steps for Symbian/Maemo
class Version5Handler : public UserFileVersionHandler
{
public:
    int userFileVersion() const
    {
        return 5;
    }

    QString displayUserFileVersion() const
    {
250
        return QLatin1String("2.1pre2");
251
252
253
254
255
    }

    QVariantMap update(Project *project, const QVariantMap &map);
};

256
// Version 6 reflects the introduction of new deploy steps for Symbian/Maemo
Tobias Hunger's avatar
Tobias Hunger committed
257
258
259
260
261
262
263
264
265
266
class Version6Handler : public UserFileVersionHandler
{
public:
    int userFileVersion() const
    {
        return 6;
    }

    QString displayUserFileVersion() const
    {
267
        return QLatin1String("2.1pre3");
Tobias Hunger's avatar
Tobias Hunger committed
268
269
270
271
272
    }

    QVariantMap update(Project *project, const QVariantMap &map);
};

273
// Version 7 reflects the introduction of new deploy configuration for Symbian
274
275
276
277
278
279
280
281
282
283
class Version7Handler : public UserFileVersionHandler
{
public:
    int userFileVersion() const
    {
        return 7;
    }

    QString displayUserFileVersion() const
    {
284
        return QLatin1String("2.1pre4");
285
286
287
288
289
    }

    QVariantMap update(Project *project, const QVariantMap &map);
};

290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Version 8 reflects the change of environment variable expansion rules,
// turning some env variables into expandos, the change of argument quoting rules,
// and the change of VariableManager's expansion syntax.
class Version8Handler : public UserFileVersionHandler
{
public:
    int userFileVersion() const
    {
        return 8;
    }

    QString displayUserFileVersion() const
    {
        // pre5 because we renamed 2.2 to 2.1 later, so people already have 2.2pre4 files
        return QLatin1String("2.2pre5");
    }

    QVariantMap update(Project *project, const QVariantMap &map);
};

310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Version 9 reflects the refactoring of the Maemo deploy step.
class Version9Handler : public UserFileVersionHandler
{
public:
    int userFileVersion() const
    {
        return 9;
    }

    QString displayUserFileVersion() const
    {
        return QLatin1String("2.3pre1");
    }

    QVariantMap update(Project *project, const QVariantMap &map);
};

Daniel Teske's avatar
Daniel Teske committed
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Version 10 introduces disabling buildsteps, and handles upgrading custom process steps
class Version10Handler : public UserFileVersionHandler
{
public:
    int userFileVersion() const
    {
        return 10;
    }

    QString displayUserFileVersion() const
    {
        return QLatin1String("2.5pre1");
    }

    QVariantMap update(Project *project, const QVariantMap &map);
};
343

Tobias Hunger's avatar
Tobias Hunger committed
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Version 10 introduces disabling buildsteps, and handles upgrading custom process steps
class Version11Handler : public UserFileVersionHandler
{
public:
    Version11Handler();
    ~Version11Handler();

    int userFileVersion() const
    {
        return 11;
    }

    QString displayUserFileVersion() const
    {
        return QLatin1String("2.6pre1");
    }

    QVariantMap update(Project *project, const QVariantMap &map);

private:
364
365
366
367
    Kit *uniqueKit(Kit *k);
    void addBuildConfiguration(Kit *k, const QVariantMap &bc, int bcPos, int bcCount);
    void addDeployConfiguration(Kit *k, const QVariantMap &dc, int dcPos, int dcActive);
    void addRunConfigurations(Kit *k,
368
                              const QMap<int, QVariantMap> &rcs, int activeRc, const QString &projectDir);
Tobias Hunger's avatar
Tobias Hunger committed
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385

    void parseQtversionFile();
    void parseToolChainFile();

    class ToolChainExtraData {
    public:
        explicit ToolChainExtraData(const QString &mks = QString(), const QString &d = QString()) :
            m_mkspec(mks), m_debugger(d)
        { }

        QString m_mkspec;
        QString m_debugger;
    };

    QHash<QString, ToolChainExtraData> m_toolChainExtras;
    QHash<int, QString> m_qtVersionExtras;

Tobias Hunger's avatar
Tobias Hunger committed
386
    QHash<Kit *, QVariantMap> m_targets;
Tobias Hunger's avatar
Tobias Hunger committed
387
388
};

389
390
} // namespace

Tobias Hunger's avatar
Tobias Hunger committed
391
392
393
394
//
// Helper functions:
//

395
396
QT_BEGIN_NAMESPACE

hjk's avatar
hjk committed
397
398
class HandlerNode
{
399
400
401
402
public:
    QSet<QString> strings;
    QHash<QString, HandlerNode> children;
};
hjk's avatar
hjk committed
403

404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
Q_DECLARE_TYPEINFO(HandlerNode, Q_MOVABLE_TYPE);

QT_END_NAMESPACE

static HandlerNode buildHandlerNodes(const char * const **keys)
{
    HandlerNode ret;
    while (const char *rname = *(*keys)++) {
        QString name = QLatin1String(rname);
        if (name.endsWith(QLatin1Char('.'))) {
            HandlerNode sub = buildHandlerNodes(keys);
            foreach (const QString &key, name.split(QLatin1Char('|')))
                ret.children.insert(key, sub);
        } else {
            ret.strings.insert(name);
        }
    }
    return ret;
}

static QVariantMap processHandlerNodes(const HandlerNode &node, const QVariantMap &map,
                                       QVariant (*handler)(const QVariant &var))
{
    QVariantMap result;
    QMapIterator<QString, QVariant> it(map);
    while (it.hasNext()) {
        it.next();
        const QString &key = it.key();
        if (node.strings.contains(key)) {
            result.insert(key, handler(it.value()));
            goto handled;
        }
        if (it.value().type() == QVariant::Map)
            for (QHash<QString, HandlerNode>::ConstIterator subit = node.children.constBegin();
                 subit != node.children.constEnd(); ++subit)
                if (key.startsWith(subit.key())) {
                    result.insert(key, processHandlerNodes(subit.value(), it.value().toMap(), handler));
                    goto handled;
                }
        result.insert(key, it.value());
      handled: ;
    }
    return result;
}
448
449
450
451

// -------------------------------------------------------------------------
// UserFileAccessor
// -------------------------------------------------------------------------
Tobias Hunger's avatar
Tobias Hunger committed
452
SettingsAccessor::SettingsAccessor(Project *project) :
453
    m_firstVersion(-1),
454
455
456
457
    m_lastVersion(-1),
    m_userFileAcessor(QByteArray("qtcUserFileName"),
                      QLatin1String(".user"),
                      QString::fromLocal8Bit(qgetenv("QTC_EXTENSION")),
458
                      true,
Tobias Hunger's avatar
Tobias Hunger committed
459
460
                      true,
                      this),
461
462
463
464
    m_sharedFileAcessor(QByteArray("qtcSharedFileName"),
                        QLatin1String(".shared"),
                        QString::fromLocal8Bit(qgetenv("QTC_SHARED_EXTENSION")),
                        false,
Tobias Hunger's avatar
Tobias Hunger committed
465
466
467
                        false,
                        this),
    m_project(project)
468
{
Tobias Hunger's avatar
Tobias Hunger committed
469
470
    QTC_CHECK(m_project);

471
    addVersionHandler(new Version0Handler);
Tobias Hunger's avatar
Tobias Hunger committed
472
    addVersionHandler(new Version1Handler);
473
    addVersionHandler(new Version2Handler);
474
    addVersionHandler(new Version3Handler);
Tobias Hunger's avatar
Tobias Hunger committed
475
    addVersionHandler(new Version4Handler);
476
    addVersionHandler(new Version5Handler);
Tobias Hunger's avatar
Tobias Hunger committed
477
    addVersionHandler(new Version6Handler);
478
    addVersionHandler(new Version7Handler);
479
    addVersionHandler(new Version8Handler);
480
    addVersionHandler(new Version9Handler);
Daniel Teske's avatar
Daniel Teske committed
481
    addVersionHandler(new Version10Handler);
Tobias Hunger's avatar
Tobias Hunger committed
482
    addVersionHandler(new Version11Handler);
483
484
}

485
SettingsAccessor::~SettingsAccessor()
486
487
488
489
{
    qDeleteAll(m_handlers);
}

Tobias Hunger's avatar
Tobias Hunger committed
490
491
Project *SettingsAccessor::project() const
{ return m_project; }
492

493
494
namespace {

495
496
497
498
499
// It's assumed that the shared map has the same structure as the user map.
template <class Operation_T>
void synchronizeSettings(QVariantMap *userMap,
                         const QVariantMap &sharedMap,
                         Operation_T *op)
500
501
502
503
504
{
    QVariantMap::const_iterator it = sharedMap.begin();
    QVariantMap::const_iterator eit = sharedMap.end();
    for (; it != eit; ++it) {
        const QString &key = it.key();
505
506
507
508
        const QVariant &sharedValue = it.value();
        const QVariant &userValue = userMap->value(key);
        if (sharedValue.type() == QVariant::Map) {
            if (userValue.type() != QVariant::Map) {
509
510
511
                // This should happen only if the user manually changed the file in such a way.
                continue;
            }
512
513
514
515
516
517
518
            QVariantMap nestedUserMap = userValue.toMap();
            synchronizeSettings(&nestedUserMap,
                                sharedValue.toMap(),
                                op);
            userMap->insert(key, nestedUserMap);
        } else if (userMap->contains(key) && userValue != sharedValue) {
            op->apply(userMap, key, sharedValue);
519
520
521
522
        }
    }
}

523
524

struct MergeSharedSetting
525
{
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
    MergeSharedSetting(const QSet<QString> &sticky) : m_userSticky(sticky) {}

    void apply(QVariantMap *userMap, const QString &key, const QVariant &sharedValue)
    {
        if (!m_userSticky.contains(key))
            userMap->insert(key, sharedValue);
    }
    QSet<QString> m_userSticky;
};

// When restoring settings...
//   We check whether a .shared file exists. If so, we compare the settings in this file with
//   corresponding ones in the .user file. Whenever we identify a corresponding setting which
//   has a different value and which is not marked as sticky, we merge the .shared value into
//   the .user value.
void mergeSharedSettings(QVariantMap *userMap, const QVariantMap &sharedMap)
{
    if (sharedMap.isEmpty())
        return;

    QSet<QString> stickyKeys;
547
    const QVariant stickyList = userMap->take(QLatin1String(USER_STICKY_KEYS_KEY)).toList();
548
549
550
551
    if (stickyList.isValid()) {
        if (stickyList.type() != QVariant::List) {
            // File is messed up... The user probably changed something.
            return;
552
        }
553
554
        foreach (const QVariant &v, stickyList.toList())
            stickyKeys.insert(v.toString());
555
    }
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586

    MergeSharedSetting op(stickyKeys);
    synchronizeSettings(userMap, sharedMap, &op);
}


struct TrackUserStickySetting
{
    void apply(QVariantMap *, const QString &key, const QVariant &)
    {
        m_userSticky.insert(key);
    }
    QSet<QString> m_userSticky;
};

// When saving settings...
//   If a .shared file was considered in the previous restoring step, we check whether for
//   any of the current .shared settings there's a .user one which is different. If so, this
//   means the user explicitly changed it and we mark this setting as sticky.
//   Note that settings are considered sticky only when they differ from the .shared ones.
//   Although this approach is more flexible than permanent/forever sticky settings, it has
//   the side-effect that if a particular value unintentionally becomes the same in both
//   the .user and .shared files, this setting will "unstick".
void trackUserStickySettings(QVariantMap *userMap, const QVariantMap &sharedMap)
{
    if (sharedMap.isEmpty())
        return;

    TrackUserStickySetting op;
    synchronizeSettings(userMap, sharedMap, &op);

587
    userMap->insert(QLatin1String(USER_STICKY_KEYS_KEY), QVariant(op.m_userSticky.toList()));
588
589
590
591
}

} // Anonymous

592

Tobias Hunger's avatar
Tobias Hunger committed
593
QVariantMap SettingsAccessor::restoreSettings() const
594
{
Tobias Hunger's avatar
Tobias Hunger committed
595
    if (m_lastVersion < 0)
596
597
        return QVariantMap();

598
    SettingsData settings;
Tobias Hunger's avatar
Tobias Hunger committed
599
600
601
602
603
    QString fn = project()->property(m_userFileAcessor.id()).toString();
    if (fn.isEmpty())
        fn = project()->document()->fileName() + m_userFileAcessor.suffix();
    settings.m_fileName = Utils::FileName::fromString(fn);
    if (!m_userFileAcessor.readFile(&settings))
604
605
606
        settings.clear(); // No user settings, but there can still be shared ones.

    if (settings.isValid()) {
Tobias Hunger's avatar
Tobias Hunger committed
607
        if (settings.m_version > m_lastVersion + 1) {
608
            QMessageBox::information(
hjk's avatar
hjk committed
609
                Core::ICore::mainWindow(),
610
                QApplication::translate("ProjectExplorer::SettingsAccessor",
611
                                        "Using Old Settings File for '%1'").arg(project()->displayName()),
612
                QApplication::translate("ProjectExplorer::SettingsAccessor",
Jarek Kobus's avatar
Jarek Kobus committed
613
                                        "<html><head/><body><p>A versioned backup of the .user "
614
615
616
617
618
619
620
621
622
623
624
                                        "settings file will be used, because the non-versioned "
                                        "file was created by an incompatible newer version of "
                                        "Qt Creator.</p><p>Project settings changes made since "
                                        "the last time this version of Qt Creator was used "
                                        "with this project are ignored, and changes made now "
                                        "will <b>not</b> be propagated to the newer version."
                                        "</p></body></html>"),
                QMessageBox::Ok);
        }

        // Verify environment.
625
626
        const QByteArray fileId = settings.environmentId();
        const QByteArray creatorId = ProjectExplorerPlugin::instance()->projectExplorerSettings().environmentId.toByteArray();
627
        if (fileId.isEmpty() || fileId != creatorId) {
628
            QString backup = fn + QLatin1Char('.') + QString::fromLatin1(fileId).mid(1, 7);
629
630
            QFile::copy(fn, backup);

631
632
633
634
635
            if (!fileId.isEmpty()) {
                // TODO tr, casing check
                QMessageBox msgBox(
                    QMessageBox::Question,
                    QApplication::translate("ProjectExplorer::SettingsAccessor",
636
                                            "Settings File for '%1' from a different Environment?").arg(project()->displayName()),
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
                    QApplication::translate("ProjectExplorer::SettingsAccessor",
                                            "Qt Creator has found a .user settings file which was "
                                            "created for another development setup, maybe "
                                            "originating from another machine.\n\n"
                                            "The .user settings files contain environment specific "
                                            "settings. They should not be copied to a different "
                                            "environment. \n\n"
                                            "Do you still want to load the settings file?"),
                    QMessageBox::Yes | QMessageBox::No,
                    Core::ICore::mainWindow());
                msgBox.setDefaultButton(QMessageBox::No);
                msgBox.setEscapeButton(QMessageBox::No);
                if (msgBox.exec() == QMessageBox::No)
                    return QVariantMap();
            }
652
653
654
655
        }

        // Do we need to generate a backup?
        if (settings.m_version < m_lastVersion + 1 && !settings.m_usingBackup) {
656
            const QString &backupFileName = settings.m_fileName.toString()
657
                    + QLatin1Char('.')
658
659
                    + m_handlers.value(settings.m_version)->displayUserFileVersion();
            QFile::remove(backupFileName);  // Remove because copy doesn't overwrite
660
            QFile::copy(settings.m_fileName.toString(), backupFileName);
661
662
        }
    }
663

664
665

    // Time to consider shared settings...
666
    SettingsData sharedSettings;
667
668
669
670
    fn = project()->property(m_sharedFileAcessor.id()).toString();
    if (fn.isEmpty())
        fn = project()->document()->fileName() + m_sharedFileAcessor.suffix();
    sharedSettings.m_fileName = Utils::FileName::fromString(fn);
671
    if (!sharedSettings.m_fileName.isEmpty() && m_sharedFileAcessor.readFile(&sharedSettings)) {
672
673
674
        bool useSharedSettings = true;
        if (sharedSettings.m_version != settings.m_version) {
            int baseFileVersion;
675
            if (sharedSettings.m_version > m_lastVersion + 1) {
676
677
678
679
680
681
682
683
                // The shared file version is newer than Creator... If we have valid user
                // settings we prompt the user whether we could try an *unsupported* update.
                // This makes sense since the merging operation will only replace shared settings
                // that perfectly match corresponding user ones. If we don't have valid user
                // settings to compare against, there's nothing we can do.
                if (!settings.isValid())
                    return QVariantMap();

684
685
686
687
688
                QMessageBox msgBox(
                            QMessageBox::Question,
                            QApplication::translate("ProjectExplorer::SettingsAccessor",
                                                    "Unsupported Shared Settings File"),
                            QApplication::translate("ProjectExplorer::SettingsAccessor",
689
                                                    "The version of your .shared file is not "
690
691
692
                                                    "supported by this Qt Creator version. "
                                                    "Only settings that are still compatible "
                                                    "will be taken into account.\n\n"
693
                                                    "Do you want to try loading it?"),
694
                            QMessageBox::Yes | QMessageBox::No,
hjk's avatar
hjk committed
695
                            Core::ICore::mainWindow());
696
697
698
699
700
                msgBox.setDefaultButton(QMessageBox::No);
                msgBox.setEscapeButton(QMessageBox::No);
                if (msgBox.exec() == QMessageBox::No)
                    useSharedSettings = false;
                else
701
                    baseFileVersion = m_lastVersion + 1;
702
            } else {
703
                baseFileVersion = qMax(settings.m_version, sharedSettings.m_version);
704
705
706
707
            }

            if (useSharedSettings) {
                // We now update the user and shared settings so they are compatible.
708
                for (int i = sharedSettings.m_version; i < baseFileVersion; ++i)
Tobias Hunger's avatar
Tobias Hunger committed
709
                    sharedSettings.m_map = m_handlers.value(i)->update(m_project, sharedSettings.m_map);
710

711
712
713
714
                if (settings.isValid()) {
                    for (int i = settings.m_version; i < baseFileVersion; ++i)
                        settings.m_map = m_handlers.value(i)->update(m_project, settings.m_map);
                    settings.m_version = baseFileVersion;
715
                }
716
717
            }
        }
718
719

        if (useSharedSettings) {
Tobias Hunger's avatar
Tobias Hunger committed
720
            m_project->setProperty(SHARED_SETTINGS, sharedSettings.m_map);
721
722
            if (settings.isValid())
                mergeSharedSettings(&settings.m_map, sharedSettings.m_map);
723
            if (!settings.isValid())
724
                settings = sharedSettings;
725
        }
Tobias Hunger's avatar
Tobias Hunger committed
726
    }
727

728
729
    if (!settings.isValid())
        return QVariantMap();
730

731
732
    // Update from the base version to Creator's version.
    for (int i = settings.m_version; i <= m_lastVersion; ++i)
Tobias Hunger's avatar
Tobias Hunger committed
733
        settings.m_map = m_handlers.value(i)->update(m_project, settings.m_map);
734

735
    return settings.m_map;
736
737
}

Tobias Hunger's avatar
Tobias Hunger committed
738
bool SettingsAccessor::saveSettings(const QVariantMap &map) const
739
{
Tobias Hunger's avatar
Tobias Hunger committed
740
    if (map.isEmpty())
741
742
        return false;

743
    SettingsData settings(map);
Tobias Hunger's avatar
Tobias Hunger committed
744
745
746
747
748
    QString fn = project()->property(m_userFileAcessor.id()).toString();
    if (fn.isEmpty())
        fn = project()->document()->fileName() + m_userFileAcessor.suffix();
    settings.m_fileName = Utils::FileName::fromString(fn);
    const QVariant &shared = m_project->property(SHARED_SETTINGS);
749
750
    if (shared.isValid())
        trackUserStickySettings(&settings.m_map, shared.toMap());
751

Tobias Hunger's avatar
Tobias Hunger committed
752
    return m_userFileAcessor.writeFile(&settings);
753
754
}

755
void SettingsAccessor::addVersionHandler(UserFileVersionHandler *handler)
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
{
    const int version(handler->userFileVersion());
    QTC_ASSERT(handler, return);
    QTC_ASSERT(version >= 0, return);
    QTC_ASSERT(!m_handlers.contains(version), return);
    QTC_ASSERT(m_handlers.isEmpty() ||
               (version == m_lastVersion + 1 || version == m_firstVersion - 1), return);

    if (m_handlers.isEmpty()) {
        m_firstVersion = version;
        m_lastVersion = version;
    } else {
        if (version < m_firstVersion)
            m_firstVersion = version;
        if (version > m_lastVersion)
            m_lastVersion = version;
    }

    m_handlers.insert(version, handler);

    // Postconditions:
    Q_ASSERT(m_lastVersion >= 0);
    Q_ASSERT(m_firstVersion >= 0);
    Q_ASSERT(m_lastVersion >= m_firstVersion);
    Q_ASSERT(m_handlers.count() == m_lastVersion - m_firstVersion + 1);
    for (int i = m_firstVersion; i < m_lastVersion; ++i)
        Q_ASSERT(m_handlers.contains(i));
}

785
786
787
788
789
790
791
792
793
// -------------------------------------------------------------------------
// SettingsData
// -------------------------------------------------------------------------
void SettingsAccessor::SettingsData::clear()
{
    m_version = -1;
    m_usingBackup = false;
    m_map.clear();
    m_fileName.clear();
794
    m_environmentId.clear();
795
796
797
798
799
800
801
}

bool SettingsAccessor::SettingsData::isValid() const
{
    return m_version > -1 && !m_fileName.isEmpty();
}

802
803
804
// -------------------------------------------------------------------------
// FileAcessor
// -------------------------------------------------------------------------
805
806
807
808

static const char VERSION_KEY[] = "ProjectExplorer.Project.Updater.FileVersion";
static const char ENVIRONMENT_ID_KEY[] = "ProjectExplorer.Project.Updater.EnvironmentId";

809
810
811
SettingsAccessor::FileAccessor::FileAccessor(const QByteArray &id,
                                             const QString &defaultSuffix,
                                             const QString &environmentSuffix,
812
                                             bool envSpecific,
Tobias Hunger's avatar
Tobias Hunger committed
813
                                             bool versionStrict, SettingsAccessor *accessor)
814
815
    : m_id(id)
    , m_environmentSpecific(envSpecific)
816
    , m_versionStrict(versionStrict)
Tobias Hunger's avatar
Tobias Hunger committed
817
818
    , m_accessor(accessor)
    , m_writer(0)
819
820
821
822
{
    assignSuffix(defaultSuffix, environmentSuffix);
}

Tobias Hunger's avatar
Tobias Hunger committed
823
824
825
826
827
SettingsAccessor::FileAccessor::~FileAccessor()
{
    delete m_writer;
}

828
829
830
831
832
void SettingsAccessor::FileAccessor::assignSuffix(const QString &defaultSuffix,
                                                  const QString &environmentSuffix)
{
    if (!environmentSuffix.isEmpty()) {
        m_suffix = environmentSuffix;
833
        m_suffix.replace(QRegExp(QLatin1String("[^a-zA-Z0-9_.-]")), QString(QLatin1Char('_'))); // replace fishy characters:
834
835
836
837
838
839
840
841
        m_suffix.prepend(QLatin1Char('.'));
    } else {
        m_suffix = defaultSuffix;
    }
}

bool SettingsAccessor::FileAccessor::findNewestCompatibleSetting(SettingsData *settings) const
{
842
    const QString baseFileName = settings->m_fileName.toString();
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
    const int baseVersion = settings->m_version;
    settings->m_fileName.clear();
    settings->m_version = -1;

    PersistentSettingsReader reader;

    QFileInfo fileInfo(baseFileName);
    QStringList fileNameFilter(fileInfo.fileName() + QLatin1String(".*"));
    const QStringList &entryList = fileInfo.absoluteDir().entryList(fileNameFilter);
    QStringList candidates;

    // First we try to identify the newest old version with a quick check.
    foreach (const QString &file, entryList) {
        const QString &suffix = file.mid(fileInfo.fileName().length() + 1);
        const QString &candidateFileName = baseFileName + QLatin1String(".") + suffix;
        candidates.append(candidateFileName);
Tobias Hunger's avatar
Tobias Hunger committed
859
860
        for (int candidateVersion = m_accessor->m_lastVersion;
             candidateVersion >= m_accessor->m_firstVersion;
861
             --candidateVersion) {
Tobias Hunger's avatar
Tobias Hunger committed
862
            if (suffix == m_accessor->m_handlers.value(candidateVersion)->displayUserFileVersion()) {
863
864
                if (candidateVersion > settings->m_version) {
                    settings->m_version = candidateVersion;
865
                    settings->m_fileName = Utils::FileName::fromString(candidateFileName);
866
867
868
869
870
871
872
873
874
875
876
                }
                break;
            }
        }
    }

    if (settings->m_version != -1) {
        if (reader.load(settings->m_fileName)) {
            settings->m_map = reader.restoreValues();
            return true;
        }
877
        qWarning() << "Unable to load file" << settings->m_fileName.toUserOutput();
878
879
880
881
882
883
    }

    // If we haven't identified a valid file or if it for any reason failed to load, we
    // try a more expensive check (which is actually needed to identify our own and newer
    // versions as we don't know what extensions will be assigned in the future).
    foreach (const QString &candidateFileName, candidates) {
884
885
        Utils::FileName fn = Utils::FileName::fromString(candidateFileName);
        if (settings->m_fileName == fn)
886
            continue; // This one has already failed to load.
887
        if (reader.load(fn)) {
888
889
            settings->m_map = reader.restoreValues();
            int candidateVersion = settings->m_map.value(QLatin1String(VERSION_KEY), 0).toInt();
Tobias Hunger's avatar
Tobias Hunger committed
890
            if (candidateVersion == m_accessor->m_lastVersion + 1) {
891
                settings->m_version = candidateVersion;
892
                settings->m_fileName = fn;
893
894
895
896
897
898
899
900
901
902
903
                return true;
            }
        }
    }

    // Nothing really worked...
    qWarning() << "File version" << baseVersion << "too new.";

    return false;
}

Tobias Hunger's avatar
Tobias Hunger committed
904
bool SettingsAccessor::FileAccessor::readFile(SettingsData *settings) const
905
906
907
908
909
910
911
{
    PersistentSettingsReader reader;
    if (!reader.load(settings->m_fileName))
        return false;

    settings->m_map = reader.restoreValues();

912
913
914
915
916
917
    // Get environment Id:
    if (m_environmentSpecific) {
        settings->m_environmentId = settings->m_map.value(QLatin1String(ENVIRONMENT_ID_KEY)).toByteArray();
        settings->m_map.remove(QLatin1String(ENVIRONMENT_ID_KEY));
    }

918
919
    // Get and verify file version
    settings->m_version = settings->m_map.value(QLatin1String(VERSION_KEY), 0).toInt();
920
921
922
    if (!m_versionStrict)
        return true;

Tobias Hunger's avatar
Tobias Hunger committed
923
    if (settings->m_version < m_accessor->m_firstVersion) {
924
925
926
927
        qWarning() << "Version" << settings->m_version << "in" << m_suffix << "too old.";
        return false;
    }

Tobias Hunger's avatar
Tobias Hunger committed
928
    if (settings->m_version > m_accessor->m_lastVersion + 1) {
929
930
931
932
        if (!findNewestCompatibleSetting(settings))
            return false;

        settings->m_usingBackup = true;
Tobias Hunger's avatar
Tobias Hunger committed
933
        m_accessor->project()->setProperty(m_id.constData(), settings->m_fileName.toString());
934
935
936
937
938
    }

    return true;
}

Tobias Hunger's avatar
Tobias Hunger committed
939
bool SettingsAccessor::FileAccessor::writeFile(const SettingsData *settings) const
940
{
Tobias Hunger's avatar
Tobias Hunger committed
941
942
943
944
945
    if (!m_writer || m_writer->fileName() != settings->m_fileName) {
        delete m_writer;
        m_writer = new Utils::PersistentSettingsWriter(settings->m_fileName, QLatin1String("QtCreatorProject"));
    }

946
947
    QVariantMap data;

948
949
950
    for (QVariantMap::const_iterator i = settings->m_map.constBegin();
         i != settings->m_map.constEnd();
         ++i) {
951
        data.insert(i.key(), i.value());
952
953
    }

954
    data.insert(QLatin1String(VERSION_KEY), m_accessor->m_lastVersion + 1);
955

Tobias Hunger's avatar
Tobias Hunger committed
956
    if (m_environmentSpecific)
957
958
959
        data.insert(QLatin1String(ENVIRONMENT_ID_KEY),
                    ProjectExplorerPlugin::instance()->projectExplorerSettings().environmentId.toString());
    return m_writer->save(data, Core::ICore::mainWindow());
960
}
961
962
963
964
965
966
967
968
969
970
971
972

// -------------------------------------------------------------------------
// Version0Handler
// -------------------------------------------------------------------------

QVariantMap Version0Handler::convertBuildConfigurations(Project *project, const QVariantMap &map)
{
    Q_ASSERT(project);
    QVariantMap result;

    // Find a valid Id to use:
    QString id;
973
    if (project->id() == "GenericProjectManager.GenericProject") {
974
        id = QLatin1String("GenericProjectManager.GenericBuildConfiguration");
975
    } else if (project->id() == "CMakeProjectManager.CMakeProject") {
976
        id = QLatin1String("CMakeProjectManager.CMakeBuildConfiguration");
977
    } else if (project->id() == "Qt4ProjectManager.Qt4Project") {
978
        result.insert(QLatin1String("Qt4ProjectManager.Qt4BuildConfiguration.NeedsV0Update"), QVariant());
979
        id = QLatin1String("Qt4ProjectManager.Qt4BuildConfiguration");
980
    } else {
981
982
        return QVariantMap(); // QmlProjects do not(/no longer) have BuildConfigurations,
                              // or we do not know how to handle this.
983
    }
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
    result.insert(QLatin1String("ProjectExplorer.ProjectConfiguration.Id"), id);

    for (QVariantMap::const_iterator i = map.constBegin(); i != map.constEnd(); ++i) {
        if (i.key() == QLatin1String("ProjectExplorer.BuildConfiguration.DisplayName")) {
            result.insert(QLatin1String("ProjectExplorer.ProjectConfiguration.DisplayName"),
                         i.value());
            continue;
        }

        if (id == QLatin1String("Qt4ProjectManager.Qt4BuildConfiguration") ||
            id.startsWith(QLatin1String("Qt4ProjectManager.Qt4BuildConfiguration."))) {
            // Qt4BuildConfiguration:
            if (i.key() == QLatin1String("QtVersionId")) {
                result.insert(QLatin1String("Qt4ProjectManager.Qt4BuildConfiguration.QtVersionId"),
                              i.value().toInt());
            } else if (i.key() == QLatin1String("ToolChain")) {
                result.insert(QLatin1String("Qt4ProjectManager.Qt4BuildConfiguration.ToolChain"),
                              i.value());
            } else if (i.key() == QLatin1String("buildConfiguration")) {
                result.insert(QLatin1String("Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration"),
                              i.value());
            } else if (i.key() == QLatin1String("userEnvironmentChanges")) {
                result.insert(QLatin1String("Qt4ProjectManager.Qt4BuildConfiguration.UserEnvironmentChanges"),
                              i.value());
            } else if (i.key() == QLatin1String("useShadowBuild")) {
                result.insert(QLatin1String("Qt4ProjectManager.Qt4BuildConfiguration.UseShadowBuild"),
                              i.value());
            } else if (i.key() == QLatin1String("clearSystemEnvironment")) {
                result.insert(QLatin1String("Qt4ProjectManager.Qt4BuildConfiguration.ClearSystemEnvironment"),
                              i.value());
            } else if (i.key() == QLatin1String("buildDirectory")) {
                result.insert(QLatin1String("Qt4ProjectManager.Qt4BuildConfiguration.BuildDirectory"),
                              i.value());
            } else {
                qWarning() << "Unknown Qt4BuildConfiguration Key found:" << i.key() << i.value();
            }
            continue;
        } else if (id == QLatin1String("CMakeProjectManager.CMakeBuildConfiguration")) {
            if (i.key() == QLatin1String("userEnvironmentChanges")) {
                result.insert(QLatin1String("CMakeProjectManager.CMakeBuildConfiguration.UserEnvironmentChanges"),
                              i.value());
            } else if (i.key() == QLatin1String("msvcVersion")) {
                result.insert(QLatin1String("CMakeProjectManager.CMakeBuildConfiguration.MsvcVersion"),
                              i.value());
            } else if (i.key() == QLatin1String("buildDirectory")) {
                result.insert(QLatin1String("CMakeProjectManager.CMakeBuildConfiguration.BuildDirectory"),
                              i.value());
            } else {
                qWarning() << "Unknown CMakeBuildConfiguration Key found:" << i.key() << i.value();
            }
            continue;
        } else if (id == QLatin1String("GenericProjectManager.GenericBuildConfiguration")) {
            if (i.key() == QLatin1String("buildDirectory")) {
                result.insert(QLatin1String("GenericProjectManager.GenericBuildConfiguration.BuildDirectory"),
                              i.value());
            } else {
                qWarning() << "Unknown GenericBuildConfiguration Key found:" << i.key() << i.value();
            }
            continue;
        }
        qWarning() << "Unknown BuildConfiguration Key found:" << i.key() << i.value();
        qWarning() << "BuildConfiguration Id is:" << id;
    }
    return result;
}

QVariantMap Version0Handler::convertRunConfigurations(Project *project, const QVariantMap &map)
{
    Q_UNUSED(project);
    QVariantMap result;
    QString id;

    // Convert Id:
    id = map.value(QLatin1String("Id")).toString();
    if (id.isEmpty())
        id = map.value(QLatin1String("type")).toString();
    if (id.isEmpty())
        return QVariantMap();

    if (QLatin1String("Qt4ProjectManager.DeviceRunConfiguration") == id)
        id = QLatin1String("Qt4ProjectManager.S60DeviceRunConfiguration");
    if (QLatin1String("Qt4ProjectManager.EmulatorRunConfiguration") == id)
        id = QLatin1String("Qt4ProjectManager.S60EmulatorRunConfiguration");
    // no need to change the CMakeRunConfiguration, CustomExecutableRunConfiguration,
    //                       MaemoRunConfiguration or Qt4RunConfiguration

    result.insert(QLatin1String("ProjectExplorer.ProjectConfiguration.Id"), id);

    // Convert everything else:
    for (QVariantMap::const_iterator i = map.constBegin(); i != map.constEnd(); ++i) {
        if (i.key() == QLatin1String("Id") || i.key() == QLatin1String("type"))
            continue;
        if (i.key() == QLatin1String("RunConfiguration.name")) {
            result.insert(QLatin1String("ProjectExplorer.ProjectConfiguration.DisplayName"),
                         i.value());
        } else if (QLatin1String("CMakeProjectManager.CMakeRunConfiguration") == id) {
            if (i.key() == QLatin1String("CMakeRunConfiguration.Target"))
                result.insert(QLatin1String("CMakeProjectManager.CMakeRunConfiguration.Target"), i.value());
            else if (i.key() == QLatin1String("CMakeRunConfiguration.WorkingDirectory"))
                result.insert(QLatin1String("CMakeProjectManager.CMakeRunConfiguration.WorkingDirectory"), i.value());
            else if (i.key() == QLatin1String("CMakeRunConfiguration.UserWorkingDirectory"))
                result.insert(QLatin1String("CMakeProjectManager.CMakeRunConfiguration.UserWorkingDirectory"), i.value());
            else if (i.key() == QLatin1String("CMakeRunConfiguration.UseTerminal"))
                result.insert(QLatin1String("CMakeProjectManager.CMakeRunConfiguration.UseTerminal"), i.value());
            else if (i.key() == QLatin1String("CMakeRunConfiguation.Title"))
                result.insert(QLatin1String("CMakeProjectManager.CMakeRunConfiguation.Title"), i.value());
            else if (i.key() == QLatin1String("CMakeRunConfiguration.Arguments"))
                result.insert(QLatin1String("CMakeProjectManager.CMakeRunConfiguration.Arguments"), i.value());
            else if (i.key() == QLatin1String("CMakeRunConfiguration.UserEnvironmentChanges"))
                result.insert(QLatin1String("CMakeProjectManager.CMakeRunConfiguration.UserEnvironmentChanges"), i.value());
            else if (i.key() == QLatin1String("BaseEnvironmentBase"))
                result.insert(QLatin1String("CMakeProjectManager.BaseEnvironmentBase"), i.value());
            else
                qWarning() << "Unknown CMakeRunConfiguration key found:" << i.key() << i.value();
        } else if (QLatin1String("Qt4ProjectManager.S60DeviceRunConfiguration") == id) {
            if (i.key() == QLatin1String("ProFile"))
                result.insert(QLatin1String("Qt4ProjectManager.S60DeviceRunConfiguration.ProFile"), i.value());
            else if (i.key() == QLatin1String("SigningMode"))
                result.insert(QLatin1String("Qt4ProjectManager.S60DeviceRunConfiguration.SigningMode"), i.value());
            else if (i.key() == QLatin1String("CustomSignaturePath"))
                result.insert(QLatin1String("Qt4ProjectManager.S60DeviceRunConfiguration.CustomSignaturePath"), i.value());
            else if (i.key() == QLatin1String("CustomKeyPath"))
                result.insert(QLatin1String("Qt4ProjectManager.S60DeviceRunConfiguration.CustomKeyPath"), i.value());
            else if (i.key() == QLatin1String("SerialPortName"))
                result.insert(QLatin1String("Qt4ProjectManager.S60DeviceRunConfiguration.SerialPortName"), i.value());
            else if (i.key() == QLatin1String("CommunicationType"))
                result.insert(QLatin1String("Qt4ProjectManager.S60DeviceRunConfiguration.CommunicationType"), i.value());
            else if (i.key() == QLatin1String("CommandLineArguments"))
                result.insert(QLatin1String("Qt4ProjectManager.S60DeviceRunConfiguration.CommandLineArguments"), i.value());
            else
                qWarning() << "Unknown S60DeviceRunConfiguration key found:" << i.key() << i.value();
        } else if (QLatin1String("Qt4ProjectManager.S60EmulatorRunConfiguration") == id) {
            if (i.key() == QLatin1String("ProFile"))
                result.insert(QLatin1String("Qt4ProjectManager.S60EmulatorRunConfiguration.ProFile"), i.value());
            else
                qWarning() << "Unknown S60EmulatorRunConfiguration key found:" << i.key() << i.value();
        } else if (QLatin1String("Qt4ProjectManager.Qt4RunConfiguration") == id) {
            if (i.key() == QLatin1String("ProFile"))
                result.insert(QLatin1String("Qt4ProjectManager.Qt4RunConfiguration.ProFile"), i.value());
            else if (i.key() == QLatin1String("CommandLineArguments"))
                result.insert(QLatin1String("Qt4ProjectManager.Qt4RunConfiguration.CommandLineArguments"), i.value());
            else if (i.key() == QLatin1String("UserSetName"))
                result.insert(QLatin1String("Qt4ProjectManager.Qt4RunConfiguration.UserSetName"), i.value());
            else if (i.key() == QLatin1String("UseTerminal"))
                result.insert(QLatin1String("Qt4ProjectManager.Qt4RunConfiguration.UseTerminal"), i.value());
            else if (i.key() == QLatin1String("UseDyldImageSuffix"))
                result.insert(QLatin1String("Qt4ProjectManager.Qt4RunConfiguration.UseDyldImageSuffix"), i.value());
            else if (i.key() == QLatin1String("UserEnvironmentChanges"))
                result.insert(QLatin1String("Qt4ProjectManager.Qt4RunConfiguration.UserEnvironmentChanges"), i.value());
            else if (i.key() == QLatin1String("BaseEnvironmentBase"))
                result.insert(QLatin1String("Qt4ProjectManager.Qt4RunConfiguration.BaseEnvironmentBase"), i.value());
            else if (i.key() == QLatin1String("UserSetWorkingDirectory"))
                result.insert(QLatin1String("Qt4ProjectManager.Qt4RunConfiguration.UserSetWorkingDirectory"), i.value());
            else if (i.key() == QLatin1String("UserWorkingDirectory"))
                result.insert(QLatin1String("Qt4ProjectManager.Qt4RunConfiguration.UserWorkingDirectory"), i.value());
            else
                qWarning() << "Unknown Qt4RunConfiguration key found:" << i.key() << i.value();
        } else if (QLatin1String("Qt4ProjectManager.MaemoRunConfiguration") == id) {
            if (i.key() == QLatin1String("ProFile"))
                result.insert(QLatin1String("Qt4ProjectManager.MaemoRunConfiguration.ProFile"), i.value());
            else if (i.key() == QLatin1String("Arguments"))
                result.insert(QLatin1String("Qt4ProjectManager.MaemoRunConfiguration.Arguments"), i.value());
            else if (i.key() == QLatin1String("Simulator"))
                result.insert(QLatin1String("Qt4ProjectManager.MaemoRunConfiguration.Simulator"), i.value());
            else if (i.key() == QLatin1String("DeviceId"))
                result.insert(QLatin1String("Qt4ProjectManager.MaemoRunConfiguration.DeviceId"), i.value());
            else if (i.key() == QLatin1String("LastDeployed"))
                result.insert(QLatin1String("Qt4ProjectManager.MaemoRunConfiguration.LastDeployed"), i.value());
            else if (i.key() == QLatin1String("DebuggingHelpersLastDeployed"))
                result.insert(QLatin1String("Qt4ProjectManager.MaemoRunConfiguration.DebuggingHelpersLastDeployed"), i.value());
            else
                qWarning() << "Unknown MaemoRunConfiguration key found:" << i.key() << i.value();
        } else if (QLatin1String("ProjectExplorer.CustomExecutableRunConfiguration") == id) {
            if (i.key() == QLatin1String("Executable"))
                result.insert(QLatin1String("ProjectExplorer.CustomExecutableRunConfiguration.Executable"), i.value());
            else if (i.key() == QLatin1String("Arguments"))
                result.insert(QLatin1String("ProjectExplorer.CustomExecutableRunConfiguration.Arguments"), i.value());
            else if (i.key() == QLatin1String("WorkingDirectory"))
                result.insert(QLatin1String("ProjectExplorer.CustomExecutableRunConfiguration.WorkingDirectory"), i.value());
            else if (i.key() == QLatin1String("UseTerminal"))
                result.insert(QLatin1String("ProjectExplorer.CustomExecutableRunConfiguration.UseTerminal"), i.value());
            else if (i.key() == QLatin1String("UserSetName"))
                result.insert(QLatin1String("ProjectExplorer.CustomExecutableRunConfiguration.UserSetName"), i.value());
            else if (i.key() == QLatin1String("UserName"))
                result.insert(QLatin1String("ProjectExplorer.CustomExecutableRunConfiguration.UserName"), i.value());
            else if (i.key() == QLatin1String("UserEnvironmentChanges"))
                result.insert(QLatin1String("ProjectExplorer.CustomExecutableRunConfiguration.UserEnvironmentChanges"), i.value());
            else if (i.key() == QLatin1String("BaseEnvironmentBase"))
                result.insert(QLatin1String("ProjectExplorer.CustomExecutableRunConfiguration.BaseEnvironmentBase"), i.value());
            else
                qWarning() << "Unknown CustomExecutableRunConfiguration key found:" << i.key() << i.value();
        } else {
            result.insert(i.key(), i.value());
        }
    }

    return result;
}

QVariantMap Version0Handler::convertBuildSteps(Project *project, const QVariantMap &map)
{
    Q_UNUSED(project);
    QVariantMap result;

    QString id(map.value(QLatin1String("Id")).toString());
    if (QLatin1String("GenericProjectManager.MakeStep") == id)
        id = QLatin1String("GenericProjectManager.GenericMakeStep");
    if (QLatin1String("projectexplorer.processstep") == id)
        id = QLatin1String("ProjectExplorer.ProcessStep");
    if (QLatin1String("trolltech.qt4projectmanager.make") == id)
        id = QLatin1String("Qt4ProjectManager.MakeStep");
    if (QLatin1String("trolltech.qt4projectmanager.qmake") == id)
        id = QLatin1String("QtProjectManager.QMakeBuildStep");
    // No need to change the CMake MakeStep.
    result.insert(QLatin1String("ProjectExplorer.ProjectConfiguration.Id"), id);

    for (QVariantMap::const_iterator i = map.constBegin(); i != map.constEnd(); ++i) {
        if (i.key() == QLatin1String("Id"))
            continue;
        if (i.key() == QLatin1String("ProjectExplorer.BuildConfiguration.DisplayName")) {
            // skip this: Not needed.
            continue;
        }

        if (QLatin1String("GenericProjectManager.GenericMakeStep") == id) {
            if (i.key() == QLatin1String("buildTargets"))
                result.insert(QLatin1String("GenericProjectManager.GenericMakeStep.BuildTargets"), i.value());
            else if (i.key() == QLatin1String("makeArguments"))
                result.insert(QLatin1String("GenericProjectManager.GenericMakeStep.MakeArguments"), i.value());
            else if (i.key() == QLatin1String("makeCommand"))
                result.insert(QLatin1String("GenericProjectManager.GenericMakeStep.MakeCommand"), i.value());
            else
                qWarning() << "Unknown GenericMakeStep value found:" << i.key() << i.value();
            continue;
        } else if (QLatin1String("ProjectExplorer.ProcessStep") == id) {
            if (i.key() == QLatin1String("ProjectExplorer.ProcessStep.DisplayName"))
                result.insert(QLatin1String("ProjectExplorer.ProjectConfiguration.DisplayName"), i.value());
            else if (i.key() == QLatin1String("abstractProcess.command"))
                result.insert(QLatin1String("ProjectExplorer.ProcessStep.Command"), i.value());
            else if ((i.key() == QLatin1String("abstractProcess.workingDirectory") ||
                      i.key() == QLatin1String("workingDirectory")) &&
                     !i.value().toString().isEmpty())
                    result.insert(QLatin1String("ProjectExplorer.ProcessStep.WorkingDirectory"), i.value());
            else if (i.key() == QLatin1String("abstractProcess.arguments"))
                result.insert(QLatin1String("ProjectExplorer.ProcessStep.Arguments"), i.value());
            else if (i.key() == QLatin1String("abstractProcess.enabled"))
                result.insert(QLatin1String("ProjectExplorer.ProcessStep.Enabled"), i.value());
            else
                qWarning() << "Unknown ProcessStep value found:" << i.key() << i.value();
        } else if (QLatin1String("Qt4ProjectManager.MakeStep") == id) {
            if (i.key() == QLatin1String("makeargs"))
                result.insert(QLatin1String("Qt4ProjectManager.MakeStep.MakeArguments"), i.value());
            else if (i.key() == QLatin1String("makeCmd"))
                result.insert(QLatin1String("Qt4ProjectManager.MakeStep.MakeCommand"), i.value());
            else if (i.key() == QLatin1String("clean"))
                result.insert(QLatin1String("Qt4ProjectManager.MakeStep.Clean"), i.value());
            else
                qWarning() << "Unknown Qt4MakeStep value found:" << i.key() << i.value();
        } else if (QLatin1String("QtProjectManager.QMakeBuildStep") == id) {
            if (i.key() == QLatin1String("qmakeArgs"))
                result.insert(QLatin1String("QtProjectManager.QMakeBuildStep.QMakeArguments"), i.value());
            else
                qWarning() << "Unknown Qt4QMakeStep value found:" << i.key() << i.value();
        } else if (QLatin1String("CMakeProjectManager.MakeStep") == id) {
            if (i.key() == QLatin1String("buildTargets"))
                result.insert(QLatin1String("CMakeProjectManager.MakeStep.BuildTargets"), i.value());
            else if (i.key() == QLatin1String("additionalArguments"))
                result.insert(QLatin1String("CMakeProjectManager.MakeStep.AdditionalArguments"), i.value());
            else if (i.key() == QLatin1String("clean"))
                result.insert(QLatin1String("CMakeProjectManager.MakeStep.Clean"), i.value());
            else
                qWarning() << "Unknown CMakeMakeStep value found:" << i.key() << i.value();
        } else {
            result.insert(i.key(), i.value());
        }
    }
    return result;
}

QVariantMap Version0Handler::update(Project *project, const QVariantMap &map)
{
    QVariantMap result;

    // "project": section is unused, just ignore it.

    // "buildconfigurations" and "buildConfiguration-":
    QStringList bcs(map.value(QLatin1String("buildconfigurations")).toStringList());
    QString active(map.value(QLatin1String("activebuildconfiguration")).toString());

    int count(0);
    foreach (const QString &bc, bcs) {
        // convert buildconfiguration:
        QString oldBcKey(QString::fromLatin1("buildConfiguration-") + bc);
        if (bc == active)
            result.insert(QLatin1String("ProjectExplorer.Project.ActiveBuildConfiguration"), count);

        QVariantMap tmp(map.value(oldBcKey).toMap());
        QVariantMap bcMap(convertBuildConfigurations(project, tmp));
        if (bcMap.isEmpty())
            continue;

        // buildsteps
        QStringList buildSteps(map.value(oldBcKey + QLatin1String("-buildsteps")).toStringList());
1287
1288
1289
1290

        if (buildSteps.isEmpty())
            // try lowercase version, too:-(
            buildSteps = map.value(QString::fromLatin1("buildconfiguration-") + bc + QLatin1String("-buildsteps")).toStringList();
1291
        if (buildSteps.isEmpty())
1292
            buildSteps = map.value(QLatin1String("buildsteps")).toStringList();
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320

        int pos(0);
        foreach (const QString &bs, buildSteps) {
            // Watch out: Capitalization differs from oldBcKey!
            const QString localKey(QLatin1String("buildconfiguration-") + bc + QString::fromLatin1("-buildstep") + QString::number(pos));
            const QString globalKey(QString::fromLatin1("buildstep") + QString::number(pos));

            QVariantMap local(map.value(localKey).toMap());
            QVariantMap global(map.value(globalKey).toMap());

            for (QVariantMap::const_iterator i = global.constBegin(); i != global.constEnd(); ++i) {
                if (!local.contains(i.key()))
                    local.insert(i.key(), i.value());
                if (i.key() == QLatin1String("ProjectExplorer.BuildConfiguration.DisplayName") &&
                    local.value(i.key()).toString().isEmpty())
                    local.insert(i.key(), i.value());
            }
            local.insert(QLatin1String("Id"), bs);

            bcMap.insert(QString::fromLatin1("ProjectExplorer.BuildConfiguration.BuildStep.") + QString::number(pos),
                         convertBuildSteps(project, local));
            ++pos;
        }
        bcMap.insert(QLatin1String("ProjectExplorer.BuildConfiguration.BuildStepsCount"), pos);

        // cleansteps
        QStringList cleanSteps(map.value(oldBcKey + QLatin1String("-cleansteps")).toStringList());
        if (cleanSteps.isEmpty())
1321
1322
1323
1324
            // try lowercase version, too:-(
            cleanSteps = map.value(QString::fromLatin1("buildconfiguration-") + bc + QLatin1String("-cleansteps")).toStringList();
        if (cleanSteps.isEmpty())
            cleanSteps = map.value(QLatin1String("cleansteps")).toStringList();
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358

        pos = 0;
        foreach (const QString &bs, cleanSteps) {
            // Watch out: Capitalization differs from oldBcKey!
            const QString localKey(QLatin1String("buildconfiguration-") + bc + QString::fromLatin1("-cleanstep") + QString::number(pos));
            const QString globalKey(QString::fromLatin1("cleanstep") + QString::number(pos));

            QVariantMap local(map.value(localKey).toMap());
            QVariantMap global(map.value(globalKey).toMap());

            for (QVariantMap::const_iterator i = global.constBegin(); i != global.constEnd(); ++i) {
                if (!local.contains(i.key()))
                    local.insert(i.key(), i.value());
                if (i.key() == QLatin1String("ProjectExplorer.BuildConfiguration.DisplayName") &&
                    local.value(i.key()).toString().isEmpty())
                    local.insert(i.key(), i.value());
            }
            local.insert(QLatin1String("Id"), bs);
            bcMap.insert(QString::fromLatin1("ProjectExplorer.BuildConfiguration.CleanStep.") + QString::number(pos),
                         convertBuildSteps(project, local));
            ++pos;
        }
        bcMap.insert(QLatin1String("ProjectExplorer.BuildConfiguration.CleanStepsCount"), pos);

        // Merge into result set:
        result.insert(QString::fromLatin1("ProjectExplorer.Project.BuildConfiguration.") + QString::number(count), bcMap);
        ++count;
    }
    result.insert(QLatin1String("ProjectExplorer.Project.BuildConfigurationCount"), count);

    // "RunConfiguration*":
    active = map.value(QLatin1String("activeRunConfiguration")).toString();
    count = 0;
    forever {
1359
        QString prefix(QLatin1String("RunConfiguration") + QString::number(count) + QLatin1Char('-'));
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
        QVariantMap rcMap;
        for (QVariantMap::const_iterator i = map.constBegin(); i != map.constEnd(); ++i) {
            if (!i.key().startsWith(prefix))
                continue;
            QString newKey(i.key().mid(prefix.size()));
            rcMap.insert(newKey, i.value());
        }
        if (rcMap.isEmpty()) {
            result.insert(QLatin1String("ProjectExplorer.Project.RunConfigurationCount"), count);
            break;
        }

        result.insert(QString::fromLatin1("ProjectExplorer.Project.RunConfiguration.") + QString::number(count),
                      convertRunConfigurations(project, rcMap));
        ++count;
    }

    // "defaultFileEncoding" (EditorSettings):
    QVariant codecVariant(map.value(QLatin1String("defaultFileEncoding")));
    if (codecVariant.isValid()) {
        QByteArray codec(codecVariant.toByteArray());
        QVariantMap editorSettingsMap;
        editorSettingsMap.insert(QLatin1String("EditorConfiguration.Codec"), codec);
        result.insert(QLatin1String("ProjectExplorer.Project.EditorSettings"),
                      editorSettingsMap);
    }

1387
    QVariant toolchain(map.value(QLatin1String("toolChain")));
1388
1389
1390
1391
1392
1393
    if (toolchain.isValid()) {
        bool ok;
        int type(toolchain.toInt(&ok));
        if (!ok) {
            QString toolChainName(toolchain.toString());
            if (toolChainName == QLatin1String("gcc"))
1394
                type = 0;
1395
            else if (toolChainName == QLatin1String("mingw"))
1396
                type = 2;
1397
            else if (toolChainName == QLatin1String("msvc"))
1398
                type = 3;