src/config.cpp

changeset 471
fc58b67d445c
parent 469
f2faed654633
child 493
16766ac1bbd9
equal deleted inserted replaced
470:e41b6e1e3299 471:fc58b67d445c
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details. 13 * GNU General Public License for more details.
14 * 14 *
15 * You should have received a copy of the GNU General Public License 15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 * =====================================================================
18 *
19 * config.cpp: Configuration management. I don't like how unsafe QSettings
20 * is so this implements a type-safer and idenitifer-safer wrapping system of
21 * configuration variables. QSettings is used underlyingly, this is a matter
22 * of interface.
17 */ 23 */
18 24
19 #include <errno.h> 25 #include <errno.h>
20 #include <QDir> 26 #include <QDir>
21 #include <QTextStream> 27 #include <QTextStream>
28 34
29 Config* g_configPointers[MAX_CONFIG]; 35 Config* g_configPointers[MAX_CONFIG];
30 static ushort g_cfgPointerCursor = 0; 36 static ushort g_cfgPointerCursor = 0;
31 37
32 // ============================================================================= 38 // =============================================================================
39 // Get the QSettings object. A portable build refers to a file in the current
40 // directory, a non-portable build to ~/.config/LDForge or to registry.
33 // ----------------------------------------------------------------------------- 41 // -----------------------------------------------------------------------------
34 static QSettings* getSettingsObject() { 42 static QSettings* getSettingsObject() {
35 #ifdef PORTABLE 43 #ifdef PORTABLE
36 # ifdef _WIN32 44 # ifdef _WIN32
37 # define EXTENSION ".ini" 45 # define EXTENSION ".ini"
46 54
47 Config::Config (const char* name, const char* defstring) : 55 Config::Config (const char* name, const char* defstring) :
48 name (name), m_defstring (defstring) {} 56 name (name), m_defstring (defstring) {}
49 57
50 // ============================================================================= 58 // =============================================================================
59 // Load the configuration from file
51 // ----------------------------------------------------------------------------- 60 // -----------------------------------------------------------------------------
52 // Load the configuration from file
53 bool Config::load() { 61 bool Config::load() {
54 QSettings* settings = getSettingsObject(); 62 QSettings* settings = getSettingsObject();
55 print ("config::load: Loading configuration file from %1...\n", settings->fileName()); 63 print ("config::load: Loading configuration file from %1\n", settings->fileName());
56 64
57 for (Config* cfg : g_configPointers) { 65 for (Config* cfg : g_configPointers) {
58 if (!cfg) 66 if (!cfg)
59 break; 67 break;
60 68
65 settings->deleteLater(); 73 settings->deleteLater();
66 return true; 74 return true;
67 } 75 }
68 76
69 // ============================================================================= 77 // =============================================================================
78 // Save the configuration to disk
70 // ----------------------------------------------------------------------------- 79 // -----------------------------------------------------------------------------
71 // Save the configuration to disk
72 bool Config::save() { 80 bool Config::save() {
73 QSettings* settings = getSettingsObject(); 81 QSettings* settings = getSettingsObject();
74 print ("Saving configuration to %1...\n", settings->fileName()); 82 print ("Saving configuration to %1...\n", settings->fileName());
75 83
76 for (Config* cfg : g_configPointers) { 84 for (Config* cfg : g_configPointers) {
87 settings->deleteLater(); 95 settings->deleteLater();
88 return true; 96 return true;
89 } 97 }
90 98
91 // ============================================================================= 99 // =============================================================================
100 // Reset configuration defaults.
92 // ----------------------------------------------------------------------------- 101 // -----------------------------------------------------------------------------
93 void Config::reset() { 102 void Config::reset() {
94 for (Config* cfg : g_configPointers) { 103 for (Config* cfg : g_configPointers) {
95 if (!cfg) 104 if (!cfg)
96 break; 105 break;
98 cfg->resetValue(); 107 cfg->resetValue();
99 } 108 }
100 } 109 }
101 110
102 // ============================================================================= 111 // =============================================================================
112 // Where is the configuration file located at? Note that the Windows build uses
113 // the registry so only use this with PORTABLE code.
103 // ----------------------------------------------------------------------------- 114 // -----------------------------------------------------------------------------
104 str Config::filepath (str file) { 115 str Config::filepath (str file) {
105 return Config::dirpath() + DIRSLASH + file; 116 return Config::dirpath() + DIRSLASH + file;
106 } 117 }
107 118
108 // ============================================================================= 119 // =============================================================================
120 // Directory of the configuration file. PORTABLE code here as well.
109 // ----------------------------------------------------------------------------- 121 // -----------------------------------------------------------------------------
110 str Config::dirpath() { 122 str Config::dirpath() {
111 QSettings* cfg = getSettingsObject(); 123 QSettings* cfg = getSettingsObject();
112 return dirname (cfg->fileName()); 124 return dirname (cfg->fileName());
113 }
114
115 // =============================================================================
116 // -----------------------------------------------------------------------------
117 str Config::defaultString() const {
118 str defstring = m_defstring;
119
120 // String types inevitably get extra quotes in their default string due to
121 // preprocessing stuff. We can only remove them now...
122 if (getType() == String) {
123 defstring.remove (0, 1);
124 defstring.chop (1);
125 }
126
127 return defstring;
128 } 125 }
129 126
130 // ============================================================================= 127 // =============================================================================
131 // We cannot just add config objects to a list or vector because that would rely 128 // We cannot just add config objects to a list or vector because that would rely
132 // on the vector's c-tor being called before the configs' c-tors. With global 129 // on the vector's c-tor being called before the configs' c-tors. With global

mercurial