56 name (name), m_defstring (defstring) {} |
57 name (name), m_defstring (defstring) {} |
57 |
58 |
58 // ============================================================================= |
59 // ============================================================================= |
59 // Load the configuration from file |
60 // Load the configuration from file |
60 // ----------------------------------------------------------------------------- |
61 // ----------------------------------------------------------------------------- |
61 bool Config::load() { |
62 bool Config::load() |
62 QSettings* settings = getSettingsObject(); |
63 { QSettings* settings = getSettingsObject(); |
63 print ("config::load: Loading configuration file from %1\n", settings->fileName()); |
64 log ("config::load: Loading configuration file from %1\n", settings->fileName()); |
64 |
65 |
65 for (Config* cfg : g_configPointers) { |
66 for (Config* cfg : g_configPointers) |
66 if (!cfg) |
67 { if (!cfg) |
67 break; |
68 break; |
68 |
69 |
69 QVariant val = settings->value (cfg->name, cfg->defaultVariant()); |
70 QVariant val = settings->value (cfg->name, cfg->defaultVariant()); |
70 cfg->loadFromVariant (val); |
71 cfg->loadFromVariant (val); |
71 } |
72 } |
72 |
73 |
73 settings->deleteLater(); |
74 settings->deleteLater(); |
74 return true; |
75 return true; |
75 } |
76 } |
76 |
77 |
77 // ============================================================================= |
78 // ============================================================================= |
78 // Save the configuration to disk |
79 // Save the configuration to disk |
79 // ----------------------------------------------------------------------------- |
80 // ----------------------------------------------------------------------------- |
80 bool Config::save() { |
81 bool Config::save() |
81 QSettings* settings = getSettingsObject(); |
82 { QSettings* settings = getSettingsObject(); |
82 print ("Saving configuration to %1...\n", settings->fileName()); |
83 log ("Saving configuration to %1...\n", settings->fileName()); |
83 |
84 |
84 for (Config* cfg : g_configPointers) { |
85 for (Config* cfg : g_configPointers) |
85 if (!cfg) |
86 { if (!cfg) |
86 break; |
87 break; |
87 |
88 |
88 if (cfg->isDefault()) |
89 if (cfg->isDefault()) |
89 continue; |
90 continue; |
90 |
91 |
91 settings->setValue (cfg->name, cfg->toVariant()); |
92 settings->setValue (cfg->name, cfg->toVariant()); |
92 } |
93 } |
93 |
94 |
94 settings->sync(); |
95 settings->sync(); |
95 settings->deleteLater(); |
96 settings->deleteLater(); |
96 return true; |
97 return true; |
97 } |
98 } |
98 |
99 |
99 // ============================================================================= |
100 // ============================================================================= |
100 // Reset configuration defaults. |
101 // Reset configuration defaults. |
101 // ----------------------------------------------------------------------------- |
102 // ----------------------------------------------------------------------------- |
102 void Config::reset() { |
103 void Config::reset() |
103 for (Config* cfg : g_configPointers) { |
104 { for (Config * cfg : g_configPointers) |
104 if (!cfg) |
105 { if (!cfg) |
105 break; |
106 break; |
106 |
107 |
107 cfg->resetValue(); |
108 cfg->resetValue(); |
108 } |
109 } |
109 } |
110 } |
110 |
111 |
111 // ============================================================================= |
112 // ============================================================================= |
112 // Where is the configuration file located at? Note that the Windows build uses |
113 // Where is the configuration file located at? Note that the Windows build uses |
113 // the registry so only use this with PORTABLE code. |
114 // the registry so only use this with PORTABLE code. |
114 // ----------------------------------------------------------------------------- |
115 // ----------------------------------------------------------------------------- |
115 str Config::filepath (str file) { |
116 str Config::filepath (str file) |
116 return Config::dirpath() + DIRSLASH + file; |
117 { return Config::dirpath() + DIRSLASH + file; |
117 } |
118 } |
118 |
119 |
119 // ============================================================================= |
120 // ============================================================================= |
120 // Directory of the configuration file. PORTABLE code here as well. |
121 // Directory of the configuration file. PORTABLE code here as well. |
121 // ----------------------------------------------------------------------------- |
122 // ----------------------------------------------------------------------------- |
122 str Config::dirpath() { |
123 str Config::dirpath() |
123 QSettings* cfg = getSettingsObject(); |
124 { QSettings* cfg = getSettingsObject(); |
124 return dirname (cfg->fileName()); |
125 return dirname (cfg->fileName()); |
125 } |
126 } |
126 |
127 |
127 // ============================================================================= |
128 // ============================================================================= |
128 // We cannot just add config objects to a list or vector because that would rely |
129 // We cannot just add config objects to a list or vector because that would rely |
129 // on the vector's c-tor being called before the configs' c-tors. With global |
130 // on the vector's c-tor being called before the configs' c-tors. With global |
130 // variables we cannot assume that!! Therefore we need to use a C-style array here. |
131 // variables we cannot assume that!! Therefore we need to use a C-style array here. |
131 // ----------------------------------------------------------------------------- |
132 // ----------------------------------------------------------------------------- |
132 void Config::addToArray (Config* ptr) { |
133 void Config::addToArray (Config* ptr) |
133 if (g_cfgPointerCursor == 0) |
134 { if (g_cfgPointerCursor == 0) |
134 memset (g_configPointers, 0, sizeof g_configPointers); |
135 memset (g_configPointers, 0, sizeof g_configPointers); |
135 |
136 |
136 assert (g_cfgPointerCursor < MAX_CONFIG); |
137 assert (g_cfgPointerCursor < MAX_CONFIG); |
137 g_configPointers[g_cfgPointerCursor++] = ptr; |
138 g_configPointers[g_cfgPointerCursor++] = ptr; |
138 } |
139 } |