settingsaccessor.h 3.57 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>
Tobias Hunger's avatar
Tobias Hunger committed
35
#include <utils/persistentsettings.h>
36

37
38
39
40
namespace ProjectExplorer {

class Project;

41
42
43
namespace Internal {
class UserFileVersionHandler;
}
44

45
class SettingsAccessor
46
47
{
public:
Tobias Hunger's avatar
Tobias Hunger committed
48
    SettingsAccessor(Project *project);
49
    ~SettingsAccessor();
50

Tobias Hunger's avatar
Tobias Hunger committed
51
    Project *project() const;
52

Tobias Hunger's avatar
Tobias Hunger committed
53
54
    QVariantMap restoreSettings() const;
    bool saveSettings(const QVariantMap &map) const;
55
56

private:
57

58
    // Takes ownership of the handler!
59
60
61
62
63
64
65
66
67
    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) {}

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

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

    // The entity which actually reads/writes to the settings file.
    class FileAccessor
    {
    public:
        FileAccessor(const QByteArray &id,
                     const QString &defaultSuffix,
                     const QString &environmentSuffix,
84
                     bool envSpecific,
Tobias Hunger's avatar
Tobias Hunger committed
85
86
87
88
89
90
                     bool versionStrict,
                     SettingsAccessor *accessor);
        ~FileAccessor();

        bool readFile(SettingsData *settings) const;
        bool writeFile(const SettingsData *settings) const;
91

Tobias Hunger's avatar
Tobias Hunger committed
92
93
        QString suffix() const { return m_suffix; }
        QByteArray id() const { return m_id; }
94
95
96
97
98
99
100
101
102

    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;
103
        bool m_versionStrict;
Tobias Hunger's avatar
Tobias Hunger committed
104
105
        SettingsAccessor *m_accessor;
        mutable Utils::PersistentSettingsWriter *m_writer;
106
107
108
    };

    static bool verifyEnvironmentId(const QString &id);
109

110
    QMap<int, Internal::UserFileVersionHandler *> m_handlers;
111
112
    int m_firstVersion;
    int m_lastVersion;
113
    const FileAccessor m_userFileAcessor;
114
    const FileAccessor m_sharedFileAcessor;
Tobias Hunger's avatar
Tobias Hunger committed
115
116

    Project *m_project;
117
118
119
120
121
};

} // namespace ProjectExplorer

#endif // PROJECTMANAGER_USERFILEACCESSOR_H