revert between 56095 -> 55830 in arch
[AROS.git] / workbench / prefs / locale / registertab.c
blobff8af6765ed178ea3d50aeffb642ddf94e279a78
1 /*
2 Copyright © 2003-2019, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <aros/debug.h>
8 #include <proto/alib.h>
9 #include <proto/battclock.h>
10 #include <proto/intuition.h>
11 #include <proto/muimaster.h>
13 #include <devices/timer.h>
14 #include <libraries/locale.h>
15 #include <zune/customclasses.h>
16 #include <zune/prefseditor.h>
18 #include <stdio.h>
20 #include "locale.h"
21 #include "registertab.h"
22 #include "misc.h"
23 #include "prefs.h"
24 #include "page_language.h"
25 #include "page_region.h"
26 #include "page_timezone.h"
28 /*** Instance Data **********************************************************/
30 struct LocaleRegister_DATA
32 int i;
34 Object *child;
35 Object *language;
36 Object *region;
37 Object *timezone;
39 const char *LocaleRegisterLabels[3];
40 char *tab_label;
42 BOOL save;
45 STATIC VOID LocalePrefs2Gadgets(struct LocaleRegister_DATA *data);
46 STATIC VOID Gadgets2LocalePrefs(struct LocaleRegister_DATA *data);
48 /*** Macros *****************************************************************/
49 #define SETUP_INST_DATA struct LocaleRegister_DATA *data = INST_DATA(CLASS, self)
51 /*** Methods ****************************************************************/
54 static Object *handle_New_error(Object *obj, struct IClass *cl, char *error)
56 struct LocaleRegister_DATA *data;
58 ShowMessage(error);
59 D(bug("[Register class] %s\n", error));
61 if(!obj)
62 return NULL;
64 data = INST_DATA(cl, obj);
66 if(data->language)
68 D(bug("[Register class] DisposeObject(data->language);\n"));
69 DisposeObject(data->language);
70 data->language = NULL;
73 if(data->region)
75 D(bug("[Register class] DisposeObject(data->region);\n"));
76 DisposeObject(data->region);
77 data->region = NULL;
80 if(data->timezone)
82 D(bug("[Register class] DisposeObject(data->timezone);\n"));
83 DisposeObject(data->timezone);
84 data->timezone = NULL;
87 if(data->tab_label)
89 FreeVec(data->tab_label);
90 data->tab_label = NULL;
93 CoerceMethod(cl, obj, OM_DISPOSE);
94 return NULL;
97 Object *LocaleRegister__OM_NEW(Class *CLASS, Object *self, struct opSet *message)
99 D(bug("[register class] LocaleRegister Class New\n"));
102 * we create self first and then create the child,
103 * so we have self->data available already
106 self = (Object *) DoSuperNewTags
108 CLASS, self, NULL,
109 MUIA_PrefsEditor_Name, _(MSG_WINTITLE),
110 MUIA_PrefsEditor_Path, (IPTR) "SYS/locale.prefs",
111 MUIA_PrefsEditor_IconTool, (IPTR) "SYS:Prefs/Locale",
112 TAG_DONE
114 if (self == NULL)
116 return handle_New_error(self, CLASS, "ERROR: Unable to create register object!\n");
119 SETUP_INST_DATA;
121 data->language = NewObject(Language_CLASS->mcc_Class, NULL,
122 MUIA_UserData, self,
123 TAG_DONE);
125 if(!data->language)
126 return handle_New_error(self, CLASS, "ERROR: Unable to create language object!\n");
128 data->region = ListviewObject, MUIA_Listview_List,
129 NewObject(Region_CLASS->mcc_Class, 0,
130 MUIA_UserData, self,
131 TAG_DONE),
132 End;
134 if(!data->region)
135 return handle_New_error(self, CLASS, "ERROR: Unable to create region object!\n");
137 data->timezone = NewObject(Timezone_CLASS->mcc_Class, NULL,
138 MUIA_UserData, self,
139 TAG_DONE);
141 if(!data->timezone)
143 D(bug("icall register handle error\n"));
144 return handle_New_error(self, CLASS,"ERROR: Unable to create timezone object!\n");
149 * Maybe, it would be easier to change the catalog,
150 * but for me it's easier this way ;)
152 data->tab_label = AllocVec( strlen(_(MSG_GAD_TAB_LANGUAGE)) +
153 strlen(_(MSG_GAD_TAB_REGION)) +
154 strlen(" / ") + 1,
155 MEMF_ANY);
157 if(!data->tab_label)
158 return handle_New_error(self, CLASS, "ERROR: Unable to allocate tab_label!\n");
160 sprintf(data->tab_label, "%s / %s", _(MSG_GAD_TAB_REGION),
161 _(MSG_GAD_TAB_LANGUAGE));
163 data->LocaleRegisterLabels[0] = data->tab_label;
164 data->LocaleRegisterLabels[1] = _(MSG_GAD_TAB_TIMEZONE);
165 data->LocaleRegisterLabels[2] = NULL;
167 data->child = RegisterGroup(data->LocaleRegisterLabels),
168 MUIA_Register_Frame, TRUE,
169 Child, HGroup,
170 MUIA_Group_SameSize, TRUE,
171 Child, HGroup,
172 MUIA_Frame, MUIV_Frame_Group,
173 MUIA_FrameTitle, _(MSG_GAD_TAB_REGION),
174 Child, data->region,
175 End,
176 Child, HGroup,
177 MUIA_Frame, MUIV_Frame_Group,
178 MUIA_FrameTitle, _(MSG_GAD_TAB_LANGUAGE),
179 Child, data->language,
180 End,
181 End,
182 Child, data->timezone,
183 End;
185 if(!data->child)
186 return handle_New_error(self, CLASS, "ERROR: unable to create registergroup\n");
188 DoMethod(self, OM_ADDMEMBER, data->child);
190 DoMethod(data->region, MUIM_Region_Fill);
192 LocalePrefs2Gadgets(data);
194 return self;
198 * update struct localprefs with actual data selected in gadgets:
200 * see prefs/locale.h
202 * struct LocalePrefs {
203 * char lp_CountryName[32];
204 * char lp_PreferredLanguages[10][30];
205 * LONG lp_GMTOffset;
206 * ULONG lp_Flags;
208 * struct CountryPrefs lp_CountryData;
209 * };
212 STATIC VOID Gadgets2LocalePrefs (struct LocaleRegister_DATA *data)
214 char *tmp;
215 IPTR newtz;
216 IPTR gmtclock;
217 char **preferred = NULL;
218 ULONG i;
219 ULONG newflags;
220 BOOL sync_clock = FALSE;
222 if (GetAttr(MUIA_Region_Regionname, data->region, (IPTR *)&tmp))
224 strncpy(localeprefs.lp_CountryName, tmp, sizeof(localeprefs.lp_CountryName) - 1);
225 Prefs_LoadRegion(localeprefs.lp_CountryName, &localeprefs.lp_CountryData);
228 if(GET(data->language, MUIA_Language_Preferred, &preferred))
230 for(i = 0; i < 10; i++)
232 if(preferred[i])
234 strncpy(localeprefs.lp_PreferredLanguages[i], preferred[i], 30);
236 else
238 localeprefs.lp_PreferredLanguages[i][0] = 0;
242 GetAttr(MUIA_Language_Characterset, data->language, (IPTR *)&tmp);
243 if (tmp)
244 strcpy(character_set, tmp);
245 else
246 character_set[0] = 0;
247 D(bug("[locale prefs] New character set is %s\n", character_set));
249 GetAttr(MUIA_Timezone_Timeoffset, data->timezone, &newtz);
251 newflags = localeprefs.lp_Flags;
252 GetAttr(MUIA_Timezone_GMTClock, data->timezone, &gmtclock);
253 if (gmtclock)
254 newflags |= LOCF_GMT_CLOCK;
255 else
256 newflags &= ~LOCF_GMT_CLOCK;
259 * If we change "GMT clock" setting, update system time from hardware clock.
260 * We do it in order to prevent time de-synchronization and clobbering
261 * hardware clock.
263 if ((newflags ^ localeprefs.lp_Flags) & LOCF_GMT_CLOCK)
264 sync_clock = TRUE;
266 /* Do the same if 'GMT clock' is active and we change the time zone */
267 if (gmtclock && (newtz != localeprefs.lp_GMTOffset))
268 sync_clock = TRUE;
270 if (sync_clock)
272 struct Library *BattClockBase = OpenResource("battclock.resource");
274 if (BattClockBase)
276 struct MsgPort *mp = CreateMsgPort();
278 if (mp)
280 struct timerequest *tr = (struct timerequest *)CreateIORequest(mp, sizeof(struct timerequest));
282 if (tr)
284 if (OpenDevice("timer.device", UNIT_VBLANK, &tr->tr_node, 0) == 0)
286 ULONG time = ReadBattClock();
288 if (tmp)
290 /* loc_GMTOffset actually expresses difference from local time to GMT */
291 time -= localeprefs.lp_GMTOffset * 60;
294 /* Set timer.device clock */
295 tr->tr_node.io_Command = TR_SETSYSTIME;
296 tr->tr_time.tv_secs = time;
297 tr->tr_time.tv_micro = 0;
298 tr->tr_node.io_Flags = IOF_QUICK;
299 DoIO(&tr->tr_node);
301 CloseDevice(&tr->tr_node);
303 DeleteIORequest(&tr->tr_node);
305 DeleteMsgPort(mp);
309 localeprefs.lp_GMTOffset = newtz;
310 localeprefs.lp_Flags = newflags;
314 * update gadgets with values of struct localeprefs
316 STATIC VOID LocalePrefs2Gadgets(struct LocaleRegister_DATA *data)
319 SET(data->region, MUIA_Region_Regionname, localeprefs.lp_CountryName);
321 SET(data->language, MUIA_Language_Preferred, TRUE);
322 SET(data->language, MUIA_Language_Characterset, character_set);
324 SET(data->timezone, MUIA_Timezone_Timeoffset, -localeprefs.lp_GMTOffset);
325 SET(data->timezone, MUIA_Timezone_GMTClock, localeprefs.lp_Flags & LOCF_GMT_CLOCK);
328 IPTR LocaleRegister__MUIM_PrefsEditor_ImportFH
330 Class *CLASS, Object *self,
331 struct MUIP_PrefsEditor_ImportFH *message
334 SETUP_INST_DATA;
335 BOOL success = TRUE;
337 D(bug("[localeregister class] LocaleRegister Class Import\n"));
339 success = Prefs_ImportFH(message->fh);
340 if (success) LocalePrefs2Gadgets(data);
342 return success;
345 IPTR LocaleRegister__MUIM_PrefsEditor_ExportFH
347 Class *CLASS, Object *self,
348 struct MUIP_PrefsEditor_ExportFH *message
351 SETUP_INST_DATA;
352 BOOL success = TRUE;
354 D(bug("[localeregister class] LocaleRegister Class Export\n"));
356 Gadgets2LocalePrefs(data);
357 success = Prefs_ExportFH(message->fh);
359 return success;
362 IPTR LocaleRegister__MUIM_PrefsEditor_SetDefaults
364 Class *CLASS, Object *self, Msg message
367 SETUP_INST_DATA;
368 BOOL success = TRUE;
370 D(bug("[localeregister class] LocaleRegister Class SetDefaults\n"));
372 success = Prefs_Default();
373 if (success) LocalePrefs2Gadgets(data);
375 return success;
378 IPTR LocaleRegister__OM_DISPOSE(Class *CLASS, Object *self, Msg message)
380 SETUP_INST_DATA;
382 if(data->tab_label)
384 FreeVec(data->tab_label);
385 data->tab_label = NULL;
388 return DoSuperMethodA(CLASS, self, message);
391 IPTR LocaleRegister__MUIM_PrefsEditor_Save(Class *cl, Object *obj, Msg msg)
393 IPTR res;
394 struct LocaleRegister_DATA *data = INST_DATA(cl, obj);
396 /* We set this flag in order not to call Prefs_SaveCharset() twice */
397 data->save = TRUE;
398 res = DoSuperMethodA(cl, obj, msg);
399 data->save = FALSE;
401 return res && Prefs_SaveCharset(TRUE);
404 IPTR LocaleRegister__MUIM_PrefsEditor_Use(Class *cl, Object *obj, Msg msg)
406 struct LocaleRegister_DATA *data = INST_DATA(cl, obj);
408 if (!DoSuperMethodA(cl, obj, msg))
409 return FALSE;
411 if (data->save)
412 return TRUE;
414 return Prefs_SaveCharset(FALSE);
417 /*** Setup ******************************************************************/
418 ZUNE_CUSTOMCLASS_7
420 LocaleRegister, NULL, MUIC_PrefsEditor, NULL,
421 OM_NEW, struct opSet *,
422 OM_DISPOSE, Msg,
423 MUIM_PrefsEditor_ImportFH, struct MUIP_PrefsEditor_ImportFH *,
424 MUIM_PrefsEditor_ExportFH, struct MUIP_PrefsEditor_ExportFH *,
425 MUIM_PrefsEditor_SetDefaults, Msg,
426 MUIM_PrefsEditor_Save, Msg,
427 MUIM_PrefsEditor_Use, Msg