2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
9 /*********************************************************************************************/
12 #include <aros/macros.h>
15 #include <aros/debug.h>
21 /*********************************************************************************************/
30 /*********************************************************************************************/
32 static struct LocalePrefs restore_prefs
;
35 /*********************************************************************************************/
37 static void ScanDirectory(STRPTR pattern
, struct List
*list
, LONG entrysize
)
40 struct ListviewEntry
*entry
;
44 memset(&ap
, 0, sizeof(ap
));
46 error
= MatchFirst(pattern
, &ap
);
49 if (ap
.ap_Info
.fib_DirEntryType
< 0)
51 entry
= (struct ListviewEntry
*)AllocPooled(mempool
, entrysize
);
54 entry
->node
.ln_Name
= entry
->name
;
55 strncpy(entry
->name
, ap
.ap_Info
.fib_FileName
, sizeof(entry
->name
));
57 entry
->name
[0] = ToUpper(entry
->name
[0]);
58 sp
= strchr(entry
->name
, '.');
61 strcpy(entry
->realname
, entry
->name
);
64 while((sp
= strchr(sp
, '_')))
69 /* Make char after underscore uppercase only if no
70 more underscores follow */
71 if (strchr(sp
, '_') == 0)
73 sp
[1] = ToUpper(sp
[1]);
77 SortInNode(list
, &entry
->node
);
80 error
= MatchNext(&ap
);
85 /*********************************************************************************************/
87 static BOOL
GetCountryFromDefLocale(struct CountryPrefs
*country
)
93 struct Locale
*loc
= OpenLocale(NULL
);
101 /*********************************************************************************************/
103 void InitPrefs(STRPTR filename
, BOOL use
, BOOL save
)
105 struct LanguageEntry
*entry
;
107 NewList(&country_list
);
108 NewList(&language_list
);
109 NewList(&pref_language_list
);
111 mempool
= CreatePool(MEMF_PUBLIC
| MEMF_CLEAR
, 2048, 2048);
112 if (!mempool
) Cleanup("Out of memory!");
114 ScanDirectory("LOCALE:Countries/~(#?.info)", &country_list
, sizeof(struct CountryEntry
));
115 ScanDirectory("LOCALE:Languages/#?.language", &language_list
, sizeof(struct LanguageEntry
));
117 /* English language is always available */
119 if ((entry
= AllocPooled(mempool
, sizeof(struct LanguageEntry
))))
121 strcpy(entry
->lve
.name
, "English");
122 strcpy(entry
->lve
.realname
, "English");
123 entry
->lve
.node
.ln_Name
= entry
->lve
.name
;
125 SortInNode(&language_list
, &entry
->lve
.node
);
128 if (!LoadPrefs(filename
))
132 if (!GetCountryFromDefLocale(&localeprefs
.lp_CountryData
))
134 Cleanup("Panic! Cannot setup default prefs!");
139 restore_prefs
= localeprefs
;
143 SavePrefs(CONFIGNAME_ENV
);
148 SavePrefs(CONFIGNAME_ENVARC
);
151 if (use
|| save
) Cleanup(NULL
);
154 /*********************************************************************************************/
156 void CleanupPrefs(void)
158 if (mempool
) DeletePool(mempool
);
161 /*********************************************************************************************/
164 static void FixCountryEndianess(struct CountryPrefs
*country
)
166 country
->cp_Reserved
[0] = AROS_BE2LONG(country
->cp_Reserved
[0]);
167 country
->cp_Reserved
[1] = AROS_BE2LONG(country
->cp_Reserved
[1]);
168 country
->cp_Reserved
[2] = AROS_BE2LONG(country
->cp_Reserved
[2]);
169 country
->cp_Reserved
[3] = AROS_BE2LONG(country
->cp_Reserved
[3]);
170 country
->cp_CountryCode
= AROS_BE2LONG(country
->cp_CountryCode
);
174 /*********************************************************************************************/
177 static void FixLocaleEndianess(struct LocalePrefs
*localeprefs
)
179 localeprefs
->lp_Reserved
[0] = AROS_BE2LONG(localeprefs
->lp_Reserved
[0]);
180 localeprefs
->lp_Reserved
[1] = AROS_BE2LONG(localeprefs
->lp_Reserved
[1]);
181 localeprefs
->lp_Reserved
[2] = AROS_BE2LONG(localeprefs
->lp_Reserved
[2]);
182 localeprefs
->lp_Reserved
[3] = AROS_BE2LONG(localeprefs
->lp_Reserved
[3]);
183 localeprefs
->lp_GMTOffset
= AROS_BE2LONG(localeprefs
->lp_GMTOffset
);
184 localeprefs
->lp_Flags
= AROS_BE2LONG(localeprefs
->lp_Flags
);
188 /*********************************************************************************************/
190 BOOL
LoadCountry(STRPTR name
, struct CountryPrefs
*country
)
192 static struct CountryPrefs loadcountry
;
193 struct IFFHandle
*iff
;
197 strcpy(fullname
, "LOCALE:Countries");
198 AddPart(fullname
, name
, 100);
199 strcat(fullname
, ".country");
201 D(bug("LoadCountry: Trying to open \"%s\"\n", fullname
));
203 if ((iff
= AllocIFF()))
205 if ((iff
->iff_Stream
= (IPTR
)Open(fullname
, MODE_OLDFILE
)))
207 D(bug("LoadCountry: stream opened.\n"));
211 if (!OpenIFF(iff
, IFFF_READ
))
213 D(bug("LoadCountry: OpenIFF okay.\n"));
215 if (!StopChunk(iff
, ID_PREF
, ID_CTRY
))
217 D(bug("LoadCountry: StopChunk okay.\n"));
219 if (!ParseIFF(iff
, IFFPARSE_SCAN
))
221 struct ContextNode
*cn
;
223 D(bug("LoadCountry: ParseIFF okay.\n"));
225 cn
= CurrentChunk(iff
);
227 if (cn
->cn_Size
== sizeof(struct CountryPrefs
))
229 D(bug("LoadCountry: ID_CTRY chunk size okay.\n"));
231 if (ReadChunkBytes(iff
, &loadcountry
, sizeof(struct CountryPrefs
)) == sizeof(struct CountryPrefs
))
233 D(bug("LoadCountry: Reading chunk successful.\n"));
235 *country
= loadcountry
;
238 FixCountryEndianess(country
);
241 D(bug("LoadCountry: Everything okay :-)\n"));
247 } /* if (!ParseIFF(iff, IFFPARSE_SCAN)) */
249 } /* if (!StopChunk(iff, ID_PREF, ID_CTRY)) */
253 } /* if (!OpenIFF(iff, IFFF_READ)) */
255 Close((BPTR
)iff
->iff_Stream
);
257 } /* if ((iff->iff_Stream = (IPTR)Open(fullname, MODE_OLDFILE))) */
261 } /* if ((iff = AllocIFF())) */
266 /*********************************************************************************************/
268 BOOL
LoadPrefs(STRPTR filename
)
270 static struct LocalePrefs loadprefs
;
271 struct IFFHandle
*iff
;
274 D(bug("LoadPrefs: Trying to open \"%s\"\n", filename
));
276 if ((iff
= AllocIFF()))
278 if ((iff
->iff_Stream
= (IPTR
)Open(filename
, MODE_OLDFILE
)))
280 D(bug("LoadPrefs: stream opened.\n"));
284 if (!OpenIFF(iff
, IFFF_READ
))
286 D(bug("LoadPrefs: OpenIFF okay.\n"));
288 if (!StopChunk(iff
, ID_PREF
, ID_LCLE
))
290 D(bug("LoadPrefs: StopChunk okay.\n"));
292 if (!ParseIFF(iff
, IFFPARSE_SCAN
))
294 struct ContextNode
*cn
;
296 D(bug("LoadPrefs: ParseIFF okay.\n"));
298 cn
= CurrentChunk(iff
);
300 if (cn
->cn_Size
== sizeof(struct LocalePrefs
))
302 D(bug("LoadPrefs: ID_LCLE chunk size okay.\n"));
304 if (ReadChunkBytes(iff
, &loadprefs
, sizeof(struct LocalePrefs
)) == sizeof(struct LocalePrefs
))
306 D(bug("LoadPrefs: Reading chunk successful.\n"));
308 TellGUI(PAGECMD_PREFS_CHANGING
);
310 localeprefs
= loadprefs
;
313 FixLocaleEndianess(&localeprefs
);
314 FixCountryEndianess(&localeprefs
.lp_CountryData
);
316 TellGUI(PAGECMD_PREFS_CHANGED
);
318 D(bug("LoadPrefs: Everything okay :-)\n"));
324 } /* if (!ParseIFF(iff, IFFPARSE_SCAN)) */
326 } /* if (!StopChunk(iff, ID_PREF, ID_CTRY)) */
330 } /* if (!OpenIFF(iff, IFFF_READ)) */
332 Close((BPTR
)iff
->iff_Stream
);
334 } /* if ((iff->iff_Stream = (IPTR)Open(filename, MODE_OLDFILE))) */
338 } /* if ((iff = AllocIFF())) */
343 /*********************************************************************************************/
345 BOOL
SavePrefs(STRPTR filename
)
347 static struct LocalePrefs saveprefs
;
348 struct IFFHandle
*iff
;
350 BOOL retval
= FALSE
, delete_if_error
= FALSE
;
352 saveprefs
= localeprefs
;
354 FixLocaleEndianess(&saveprefs
);
355 FixCountryEndianess(&saveprefs
.lp_CountryData
);
358 for(i
= 0; i
< 10; i
++)
360 saveprefs
.lp_PreferredLanguages
[i
][0] = ToLower(saveprefs
.lp_PreferredLanguages
[i
][0]);
363 D(bug("SavePrefs: Trying to open \"%s\"\n", filename
));
365 if ((iff
= AllocIFF()))
367 if ((iff
->iff_Stream
= (IPTR
)Open(filename
, MODE_NEWFILE
)))
369 D(bug("SavePrefs: stream opened.\n"));
371 delete_if_error
= TRUE
;
375 if (!OpenIFF(iff
, IFFF_WRITE
))
377 D(bug("SavePrefs: OpenIFF okay.\n"));
379 if (!PushChunk(iff
, ID_PREF
, ID_FORM
, IFFSIZE_UNKNOWN
))
381 D(bug("SavePrefs: PushChunk(FORM) okay.\n"));
383 if (!PushChunk(iff
, ID_PREF
, ID_PRHD
, sizeof(struct FilePrefHeader
)))
385 struct FilePrefHeader head
;
387 D(bug("SavePrefs: PushChunk(PRHD) okay.\n"));
389 head
.ph_Version
= 0; // FIXME: shouold be PHV_CURRENT, but see <prefs/prefhdr.h>
394 head
.ph_Flags
[3] = 0;
396 if (WriteChunkBytes(iff
, &head
, sizeof(head
)) == sizeof(head
))
398 D(bug("SavePrefs: WriteChunkBytes(PRHD) okay.\n"));
402 if (!PushChunk(iff
, ID_PREF
, ID_LCLE
, sizeof(struct LocalePrefs
)))
404 D(bug("SavePrefs: PushChunk(LCLE) okay.\n"));
406 if (WriteChunkBytes(iff
, &saveprefs
, sizeof(saveprefs
)) == sizeof(saveprefs
))
408 D(bug("SavePrefs: WriteChunkBytes(LCLE) okay.\n"));
409 D(bug("SavePrefs: Everything okay :-)\n"));
416 } /* if (!PushChunk(iff, ID_PREF, ID_LCLE, sizeof(struct LocalePrefs))) */
418 } /* if (WriteChunkBytes(iff, &head, sizeof(head)) == sizeof(head)) */
424 } /* if (!PushChunk(iff, ID_PREF, ID_PRHD, sizeof(struct PrefHeader))) */
428 } /* if (!PushChunk(iff, ID_PREF, ID_FORM, IFFSIZE_UNKNOWN)) */
432 } /* if (!OpenIFF(iff, IFFFWRITE)) */
434 Close((BPTR
)iff
->iff_Stream
);
436 } /* if ((iff->iff_Stream = (IPTR)Open(filename, MODE_NEWFILE))) */
440 } /* if ((iff = AllocIFF())) */
442 if (!retval
&& delete_if_error
)
444 DeleteFile(filename
);
450 /*********************************************************************************************/
452 BOOL
DefaultPrefs(void)
457 TellGUI(PAGECMD_PREFS_CHANGING
);
459 localeprefs
.lp_Reserved
[0] = 0;
460 localeprefs
.lp_Reserved
[1] = 0;
461 localeprefs
.lp_Reserved
[2] = 0;
462 localeprefs
.lp_Reserved
[3] = 0;
464 strcpy(localeprefs
.lp_CountryName
, "united_states");
466 for(i
= 0; i
< 10; i
++)
468 memset(localeprefs
.lp_PreferredLanguages
[i
], 0, sizeof(localeprefs
.lp_PreferredLanguages
[i
]));
470 localeprefs
.lp_GMTOffset
= 5 * 60;
471 localeprefs
.lp_Flags
= 0;
473 if (LoadCountry("united_states", &localeprefs
.lp_CountryData
))
478 TellGUI(PAGECMD_PREFS_CHANGED
);
483 /*********************************************************************************************/
485 void RestorePrefs(void)
487 TellGUI(PAGECMD_PREFS_CHANGING
);
488 localeprefs
= restore_prefs
;
489 TellGUI(PAGECMD_PREFS_CHANGED
);
492 /*********************************************************************************************/