40 # define EXTENSION ".cfg" |
40 # define EXTENSION ".cfg" |
41 #endif // _WIN32 |
41 #endif // _WIN32 |
42 |
42 |
43 #define MAX_CONFIG 512 |
43 #define MAX_CONFIG 512 |
44 |
44 |
45 static QMap<QString, ConfigEntry*> g_configsByName; |
45 static QMap<QString, AbstractConfigEntry*> EntriesByName; |
46 static QList<ConfigEntry*> g_configs; |
46 static QList<AbstractConfigEntry*> ConfigurationEntries; |
47 |
47 |
48 ConfigEntry::ConfigEntry (QString name) : |
48 AbstractConfigEntry::AbstractConfigEntry (QString name) : |
49 m_name (name) {} |
49 m_name (name) {} |
50 |
50 |
51 void Config::init() |
51 void Config::Initialize() |
52 { |
52 { |
53 setupConfigurationLists(); |
53 SetupConfigurationLists(); |
54 print ("Configuration initialized with %1 entries\n", g_configs.size()); |
54 print ("Configuration initialized with %1 entries\n", ConfigurationEntries.size()); |
55 } |
55 } |
56 |
56 |
57 static void initConfigurationEntry (ConfigEntry* entry) |
57 static void InitConfigurationEntry (AbstractConfigEntry* entry) |
58 { |
58 { |
59 g_configs << entry; |
59 ConfigurationEntries << entry; |
60 g_configsByName[entry->name()] = entry; |
60 EntriesByName[entry->name()] = entry; |
61 } |
61 } |
62 |
62 |
63 // |
63 // |
64 // Load the configuration from file |
64 // Load the configuration from file |
65 // |
65 // |
66 bool Config::load() |
66 bool Config::Load() |
67 { |
67 { |
68 QSettings* settings = settingsObject(); |
68 QSettings* settings = SettingsObject(); |
69 print ("Loading configuration file from %1\n", settings->fileName()); |
69 print ("Loading configuration file from %1\n", settings->fileName()); |
70 |
70 |
71 for (ConfigEntry* cfg : g_configs) |
71 for (AbstractConfigEntry* cfg : ConfigurationEntries) |
72 { |
72 { |
73 if (cfg == null) |
73 if (cfg == null) |
74 break; |
74 break; |
75 |
75 |
76 QVariant val = settings->value (cfg->name(), cfg->getDefaultAsVariant()); |
76 QVariant val = settings->value (cfg->name(), cfg->getDefaultAsVariant()); |
85 } |
85 } |
86 |
86 |
87 // |
87 // |
88 // Save the configuration to disk |
88 // Save the configuration to disk |
89 // |
89 // |
90 bool Config::save() |
90 bool Config::Save() |
91 { |
91 { |
92 QSettings* settings = settingsObject(); |
92 QSettings* settings = SettingsObject(); |
93 |
93 |
94 for (ConfigEntry* cfg : g_configs) |
94 for (AbstractConfigEntry* cfg : ConfigurationEntries) |
95 { |
95 { |
96 if (not cfg->isDefault()) |
96 if (not cfg->isDefault()) |
97 settings->setValue (cfg->name(), cfg->toVariant()); |
97 settings->setValue (cfg->name(), cfg->toVariant()); |
98 else |
98 else |
99 settings->remove (cfg->name()); |
99 settings->remove (cfg->name()); |
109 } |
109 } |
110 |
110 |
111 // |
111 // |
112 // Reset configuration to defaults. |
112 // Reset configuration to defaults. |
113 // |
113 // |
114 void Config::reset() |
114 void Config::ResetToDefaults() |
115 { |
115 { |
116 for (ConfigEntry* cfg : g_configs) |
116 for (AbstractConfigEntry* cfg : ConfigurationEntries) |
117 cfg->resetValue(); |
117 cfg->resetValue(); |
118 } |
118 } |
119 |
119 |
120 // |
120 // |
121 // Where is the configuration file located at? |
121 // Where is the configuration file located at? |
122 // |
122 // |
123 QString Config::filepath (QString file) |
123 QString Config::FilePath (QString file) |
124 { |
124 { |
125 return Config::dirpath() + DIRSLASH + file; |
125 return Config::DirectoryPath() + DIRSLASH + file; |
126 } |
126 } |
127 |
127 |
128 // |
128 // |
129 // Directory of the configuration file. |
129 // Directory of the configuration file. |
130 // |
130 // |
131 QString Config::dirpath() |
131 QString Config::DirectoryPath() |
132 { |
132 { |
133 QSettings* settings = settingsObject(); |
133 QSettings* settings = SettingsObject(); |
134 QString result = dirname (settings->fileName()); |
134 QString result = dirname (settings->fileName()); |
135 delete settings; |
135 delete settings; |
136 return result; |
136 return result; |
137 } |
137 } |
138 |
138 |
139 // |
139 // |
140 // Accessor to the settings object |
140 // Accessor to the settings object |
141 // |
141 // |
142 QSettings* Config::settingsObject() |
142 QSettings* Config::SettingsObject() |
143 { |
143 { |
144 QString path = qApp->applicationDirPath() + "/" UNIXNAME EXTENSION; |
144 QString path = qApp->applicationDirPath() + "/" UNIXNAME EXTENSION; |
145 return new QSettings (path, QSettings::IniFormat); |
145 return new QSettings (path, QSettings::IniFormat); |
146 } |
146 } |
147 |
147 |
148 template<typename T> |
148 template<typename T> |
149 T* getConfigByName (QString name, ConfigEntry::Type type) |
149 static T* GetConfigByName (QString name, AbstractConfigEntry::Type type) |
150 { |
150 { |
151 auto it = g_configsByName.find (name); |
151 auto it = EntriesByName.find (name); |
152 |
152 |
153 if (it == g_configsByName.end()) |
153 if (it == EntriesByName.end()) |
154 return null; |
154 return null; |
155 |
155 |
156 ConfigEntry* cfg = it.value(); |
156 AbstractConfigEntry* cfg = it.value(); |
157 |
157 |
158 if (cfg->getType() != type) |
158 if (cfg->getType() != type) |
159 { |
159 { |
160 fprint (stderr, "type of %1 is %2, not %3\n", name, cfg->getType(), type); |
160 fprint (stderr, "type of %1 is %2, not %3\n", name, cfg->getType(), type); |
161 abort(); |
161 abort(); |
167 #undef IMPLEMENT_CONFIG |
167 #undef IMPLEMENT_CONFIG |
168 |
168 |
169 #define IMPLEMENT_CONFIG(NAME) \ |
169 #define IMPLEMENT_CONFIG(NAME) \ |
170 NAME##ConfigEntry* NAME##ConfigEntry::getByName (QString name) \ |
170 NAME##ConfigEntry* NAME##ConfigEntry::getByName (QString name) \ |
171 { \ |
171 { \ |
172 return getConfigByName<NAME##ConfigEntry> (name, E##NAME##Type); \ |
172 return GetConfigByName<NAME##ConfigEntry> (name, E##NAME##Type); \ |
173 } |
173 } |
174 |
174 |
175 IMPLEMENT_CONFIG (Int) |
175 IMPLEMENT_CONFIG (Int) |
176 IMPLEMENT_CONFIG (String) |
176 IMPLEMENT_CONFIG (String) |
177 IMPLEMENT_CONFIG (Bool) |
177 IMPLEMENT_CONFIG (Bool) |