settingsaccessor.h 3.34 KB
Newer Older
1
2
3
4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
6
**
Eike Ziller's avatar
Eike Ziller committed
7
** Contact: http://www.qt-project.org/
8
9
10
11
**
**
** GNU Lesser General Public License Usage
**
hjk's avatar
hjk committed
12
13
14
15
16
17
** 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.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21
22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23
24
25
26
27
** 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.
**
28
29
30
31
32
33
**
**************************************************************************/

#ifndef PROJECTMANAGER_USERFILEACCESSOR_H
#define PROJECTMANAGER_USERFILEACCESSOR_H

34
#include <utils/fileutils.h>
35

36
#include <QVariantMap>
37

38
39
40
41
namespace ProjectExplorer {

class Project;

42
43
44
namespace Internal {
class UserFileVersionHandler;
}
45

46
class SettingsAccessor
47
48
{
public:
49
    ~SettingsAccessor();
50

51
    static SettingsAccessor *instance();
52

53
54
    QVariantMap restoreSettings(Project *project) const;
    bool saveSettings(const Project *project, const QVariantMap &map) const;
55
56

private:
57
58
    SettingsAccessor();

59
    // Takes ownership of the handler!
60
61
62
63
64
65
66
67
68
    void addVersionHandler(Internal::UserFileVersionHandler *handler);

    // The relevant data from the settings currently in use.
    class SettingsData
    {
    public:
        SettingsData() : m_version(-1), m_usingBackup(false) {}
        SettingsData(const QVariantMap &map) : m_version(-1), m_usingBackup(false), m_map(map) {}

69
70
71
        void clear();
        bool isValid() const;

72
73
74
        int m_version;
        bool m_usingBackup;
        QVariantMap m_map;
75
        Utils::FileName m_fileName;
76
77
78
79
80
81
82
83
84
    };

    // The entity which actually reads/writes to the settings file.
    class FileAccessor
    {
    public:
        FileAccessor(const QByteArray &id,
                     const QString &defaultSuffix,
                     const QString &environmentSuffix,
85
86
                     bool envSpecific,
                     bool versionStrict);
87
88
89
90
91
92
93
94
95
96
97
98

        bool readFile(Project *project, SettingsData *settings) const;
        bool writeFile(const Project *project, const SettingsData *settings) const;

    private:
        void assignSuffix(const QString &defaultSuffix, const QString &environmentSuffix);
        QString assembleFileName(const Project *project) const;
        bool findNewestCompatibleSetting(SettingsData *settings) const;

        QByteArray m_id;
        QString m_suffix;
        bool m_environmentSpecific;
99
        bool m_versionStrict;
100
101
102
    };

    static bool verifyEnvironmentId(const QString &id);
103

104
    QMap<int, Internal::UserFileVersionHandler *> m_handlers;
105
106
    int m_firstVersion;
    int m_lastVersion;
107
    const FileAccessor m_userFileAcessor;
108
    const FileAccessor m_sharedFileAcessor;
109
110
111
112
113
};

} // namespace ProjectExplorer

#endif // PROJECTMANAGER_USERFILEACCESSOR_H