1 /** @file configprofiles.h  Configuration setting profiles.
2  *
3  * @authors Copyright (c) 2013-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
4  *
5  * @par License
6  * GPL: http://www.gnu.org/licenses/gpl.html
7  *
8  * <small>This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; either version 2 of the License, or (at your
11  * option) any later version. This program is distributed in the hope that it
12  * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14  * Public License for more details. You should have received a copy of the GNU
15  * General Public License along with this program; if not, see:
16  * http://www.gnu.org/licenses</small>
17  */
18 
19 #ifndef DENG_CONFIGPROFILES_H
20 #define DENG_CONFIGPROFILES_H
21 
22 #include <de/String>
23 #include <de/Observers>
24 #include <de/Profiles>
25 #include <QVariant>
26 #include <QList>
27 
28 /**
29  * Collection of settings (cvars, Config variables) of which there can be
30  * several alternative profiles. When a register is created, it automatically
31  * gets a profile called "User".
32  *
33  * The default values are stored separately, so that any profile can be reseted
34  * back to the default values.
35  *
36  * All settings of a register should be defined before it gets used.
37  *
38  * The current profile is simply whatever values the identified cvars/variables
39  * presently hold. These current values get stored persistently in the app's
40  * Config (and via con_config) as usual. ConfigProfiles is responsible for
41  * storing the non-current profiles persistently. The (de)serialization occurs
42  * whenever the game is (un)loaded, as all cvars are presently game-specific.
43  *
44  * It is possible to install new profiles via resource packs. The profiles should
45  * be placed to /data/profiles/(persistentName)/.
46  */
47 class ConfigProfiles : public de::Profiles
48 {
49 public:
50     enum SettingType {
51         IntCVar,
52         FloatCVar,
53         StringCVar,
54         ConfigVariable      ///< Default value gotten from Config.setDefaults().
55     };
56 
57     /// Notified when the current profile has changed.
58     DENG2_DEFINE_AUDIENCE(ProfileChange, void currentProfileChanged(de::String const &name))
59 
60 public:
61     ConfigProfiles();
62 
63     /**
64      * Defines a new setting in the profile.
65      *
66      * The default values of Config variables are taken from Config (so it must already
67      * be initialized).
68      *
69      * @param type          Type of setting.
70      * @param settingName   Name of the setting.
71      * @param defaultValue  Default value of the setting (for cvars).
72      *
73      * @return Reference to this instance.
74      */
75     ConfigProfiles &define(SettingType type,
76                            de::String const &settingName,
77                            QVariant const &defaultValue = QVariant());
78 
79     de::String currentProfile() const;
80 
81     /**
82      * Current values of the settings are saved as a profile. If there is a
83      * profile with the same name, it will be replaced. The current profile is
84      * not changed.
85      *
86      * @param name  Name of the profile.
87      *
88      * @return @c true if a new profile was created. @c false, if the operation
89      * failed (e.g., name already in use).
90      */
91     bool saveAsProfile(de::String const &name);
92 
93     /**
94      * Changes the current settings profile.
95      *
96      * @param name  Name of the profile to use.
97      */
98     void setProfile(de::String const &name);
99 
100     /**
101      * Resets the current profile to default values.
102      */
103     void resetToDefaults();
104 
105     /**
106      * Resets one setting in the current profile to its default value.
107      *
108      * @param settingName  Name of the setting.
109      */
110     void resetSettingToDefaults(de::String const &settingName);
111 
112     /**
113      * Renames the current profile.
114      *
115      * @param name  New name of the profile.
116      *
117      * @return @c true, if renamed successfully.
118      */
119     bool rename(de::String const &name);
120 
121     /**
122      * Deletes a profile. The current profile cannot be deleted.
123      *
124      * @param name  Name of the profile to delete.
125      */
126     void deleteProfile(de::String const &name);
127 
128 protected:
129     AbstractProfile *profileFromInfoBlock(
130             de::Info::BlockElement const &block) override;
131 
132 private:
133     DENG2_PRIVATE(d)
134 };
135 
136 #endif // DENG_CONFIGPROFILES_H
137