src/config.cpp

changeset 238
3b35f502a6c7
parent 199
10dd5909a50e
child 239
ea09eeba1c2b
equal deleted inserted replaced
237:ec77f6e9a19f 238:3b35f502a6c7
20 #include <time.h> 20 #include <time.h>
21 #include <QDir> 21 #include <QDir>
22 #include "common.h" 22 #include "common.h"
23 #include "config.h" 23 #include "config.h"
24 #include "misc.h" 24 #include "misc.h"
25 #include "gui.h"
25 26
26 std::vector<config*> g_configPointers; 27 std::vector<config*> g_configPointers;
27 28
28 // ============================================================================= 29 // =============================================================================
29 const char* g_WeekdayNames[7] = { 30 const char* g_WeekdayNames[7] = {
62 }; 63 };
63 64
64 // ============================================================================= 65 // =============================================================================
65 // Load the configuration from file 66 // Load the configuration from file
66 bool config::load () { 67 bool config::load () {
67 printf ("config::load: loading configuration file.\n"); 68 printf ("config::load: Loading configuration file...\n");
69 printf ("config::load: Path to configuration is %s\n", filepath ().chars ());
68 70
69 // Locale must be disabled for atof 71 // Locale must be disabled for atof
70 setlocale (LC_NUMERIC, "C"); 72 setlocale (LC_NUMERIC, "C");
71 73
72 FILE* fp = fopen (filepath().chars(), "r"); 74 FILE* fp = fopen (filepath ().chars(), "r");
73 char linedata[MAX_INI_LINE]; 75 char linedata[MAX_INI_LINE];
74 char* line; 76 char* line;
75 size_t ln = 0; 77 size_t ln = 0;
76 78
77 if (!fp) 79 if (!fp)
177 // The function will write floats, disable the locale now so that they 179 // The function will write floats, disable the locale now so that they
178 // are written properly. 180 // are written properly.
179 setlocale (LC_NUMERIC, "C"); 181 setlocale (LC_NUMERIC, "C");
180 182
181 // If the directory doesn't exist, create it now. 183 // If the directory doesn't exist, create it now.
182 if (QDir (dirpath ()).exists () == nope) { 184 if (QDir (dirpath ()).exists () == false) {
183 fprintf (stderr, "Creating config path %s...\n", dirpath().chars()); 185 fprintf (stderr, "Creating config path %s...\n", dirpath().chars());
184 if (!QDir ().mkpath (dirpath().chars())) { 186 if (!QDir ().mkpath (dirpath ().chars ())) {
185 logf (LOG_Warning, "Failed to create the directory. Configuration cannot be saved!\n"); 187 critical ("Failed to create the directory. Configuration cannot be saved!\n");
186 return false; // Couldn't create directory 188 return false; // Couldn't create directory
187 } 189 }
188 } 190 }
189 191
190 FILE* fp = fopen (filepath().chars(), "w"); 192 FILE* fp = fopen (filepath ().chars (), "w");
191 printf ("writing cfg to %s\n", filepath().chars()); 193 printf ("writing cfg to %s\n", filepath().chars());
192 194
193 if (!fp) { 195 if (!fp) {
194 printf ("Couldn't open %s for writing\n", filepath().chars()); 196 critical (fmt ("Cannot save configuration, cannot open %s for writing\n", filepath ().chars ()));
195 return false; 197 return false;
196 } 198 }
197 199
198 const time_t curtime = time (NULL); 200 const time_t curtime = time (NULL);
199 const struct tm* timeinfo = localtime (&curtime); 201 const struct tm* timeinfo = localtime (&curtime);
233 235
234 default: 236 default:
235 break; 237 break;
236 } 238 }
237 239
238 const char* sDefault = (cfg->getType() != CONFIG_keyseq) ? cfg->defaultstring : 240 const char* defstr = (cfg->getType() != CONFIG_keyseq) ? cfg->defaultstring :
239 static_cast<keyseqconfig*> (cfg)->defval.toString ().toUtf8 ().constData (); 241 qchars (static_cast<keyseqconfig*> (cfg)->defval.toString ());
240 242
241 // Write the entry now. 243 // Write the entry now.
242 writef (fp, "\n# [%s] default: %s\n", g_ConfigTypeNames[cfg->getType()], sDefault); 244 writef (fp, "\n# [%s] default: %s\n", g_ConfigTypeNames[cfg->getType()], defstr);
243 writef (fp, "%s=%s\n", cfg->name, valstring.chars()); 245 writef (fp, "%s=%s\n", cfg->name, valstring.chars());
244 } 246 }
245 247
246 fclose (fp); 248 fclose (fp);
247 return true; 249 return true;
261 return path; 263 return path;
262 } 264 }
263 265
264 // ============================================================================= 266 // =============================================================================
265 str config::dirpath () { 267 str config::dirpath () {
266 return fmt ("%s/.%s/", qchars (QDir::homePath ()), 268 #ifndef _WIN32
269 return fmt ("%s" DIRSLASH ".%s" DIRSLASH,
270 qchars (QDir::homePath ()),
267 str (APPNAME).lower ().chars ()); 271 str (APPNAME).lower ().chars ());
268 } 272 #else
273 return fmt ("%s" DIRSLASH APPNAME DIRSLASH, qchars (QDir::homePath ()));
274 #endif // _WIN32
275 }

mercurial