2 Copyright © 2003-2019, The AROS Development Team. All rights reserved.
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>
21 #include "registertab.h"
24 #include "page_language.h"
25 #include "page_region.h"
26 #include "page_timezone.h"
28 /*** Instance Data **********************************************************/
30 struct LocaleRegister_DATA
39 const char *LocaleRegisterLabels
[3];
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
;
59 D(bug("[Register class] %s\n", error
));
64 data
= INST_DATA(cl
, obj
);
68 D(bug("[Register class] DisposeObject(data->language);\n"));
69 DisposeObject(data
->language
);
70 data
->language
= NULL
;
75 D(bug("[Register class] DisposeObject(data->region);\n"));
76 DisposeObject(data
->region
);
82 D(bug("[Register class] DisposeObject(data->timezone);\n"));
83 DisposeObject(data
->timezone
);
84 data
->timezone
= NULL
;
89 FreeVec(data
->tab_label
);
90 data
->tab_label
= NULL
;
93 CoerceMethod(cl
, obj
, OM_DISPOSE
);
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
109 MUIA_PrefsEditor_Name
, _(MSG_WINTITLE
),
110 MUIA_PrefsEditor_Path
, (IPTR
) "SYS/locale.prefs",
111 MUIA_PrefsEditor_IconTool
, (IPTR
) "SYS:Prefs/Locale",
116 return handle_New_error(self
, CLASS
, "ERROR: Unable to create register object!\n");
121 data
->language
= NewObject(Language_CLASS
->mcc_Class
, NULL
,
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,
135 return handle_New_error(self
, CLASS
, "ERROR: Unable to create region object!\n");
137 data
->timezone
= NewObject(Timezone_CLASS
->mcc_Class
, NULL
,
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
)) +
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
,
170 MUIA_Group_SameSize
, TRUE
,
172 MUIA_Frame
, MUIV_Frame_Group
,
173 MUIA_FrameTitle
, _(MSG_GAD_TAB_REGION
),
177 MUIA_Frame
, MUIV_Frame_Group
,
178 MUIA_FrameTitle
, _(MSG_GAD_TAB_LANGUAGE
),
179 Child
, data
->language
,
182 Child
, data
->timezone
,
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
);
198 * update struct localprefs with actual data selected in gadgets:
202 * struct LocalePrefs {
203 * char lp_CountryName[32];
204 * char lp_PreferredLanguages[10][30];
208 * struct CountryPrefs lp_CountryData;
212 STATIC VOID
Gadgets2LocalePrefs (struct LocaleRegister_DATA
*data
)
217 char **preferred
= NULL
;
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
++)
234 strncpy(localeprefs
.lp_PreferredLanguages
[i
], preferred
[i
], 30);
238 localeprefs
.lp_PreferredLanguages
[i
][0] = 0;
242 GetAttr(MUIA_Language_Characterset
, data
->language
, (IPTR
*)&tmp
);
244 strcpy(character_set
, tmp
);
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
);
254 newflags
|= LOCF_GMT_CLOCK
;
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
263 if ((newflags
^ localeprefs
.lp_Flags
) & LOCF_GMT_CLOCK
)
266 /* Do the same if 'GMT clock' is active and we change the time zone */
267 if (gmtclock
&& (newtz
!= localeprefs
.lp_GMTOffset
))
272 struct Library
*BattClockBase
= OpenResource("battclock.resource");
276 struct MsgPort
*mp
= CreateMsgPort();
280 struct timerequest
*tr
= (struct timerequest
*)CreateIORequest(mp
, sizeof(struct timerequest
));
284 if (OpenDevice("timer.device", UNIT_VBLANK
, &tr
->tr_node
, 0) == 0)
286 ULONG time
= ReadBattClock();
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
;
301 CloseDevice(&tr
->tr_node
);
303 DeleteIORequest(&tr
->tr_node
);
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
337 D(bug("[localeregister class] LocaleRegister Class Import\n"));
339 success
= Prefs_ImportFH(message
->fh
);
340 if (success
) LocalePrefs2Gadgets(data
);
345 IPTR LocaleRegister__MUIM_PrefsEditor_ExportFH
347 Class
*CLASS
, Object
*self
,
348 struct MUIP_PrefsEditor_ExportFH
*message
354 D(bug("[localeregister class] LocaleRegister Class Export\n"));
356 Gadgets2LocalePrefs(data
);
357 success
= Prefs_ExportFH(message
->fh
);
362 IPTR LocaleRegister__MUIM_PrefsEditor_SetDefaults
364 Class
*CLASS
, Object
*self
, Msg message
370 D(bug("[localeregister class] LocaleRegister Class SetDefaults\n"));
372 success
= Prefs_Default();
373 if (success
) LocalePrefs2Gadgets(data
);
378 IPTR
LocaleRegister__OM_DISPOSE(Class
*CLASS
, Object
*self
, Msg message
)
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
)
394 struct LocaleRegister_DATA
*data
= INST_DATA(cl
, obj
);
396 /* We set this flag in order not to call Prefs_SaveCharset() twice */
398 res
= DoSuperMethodA(cl
, obj
, msg
);
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
))
414 return Prefs_SaveCharset(FALSE
);
417 /*** Setup ******************************************************************/
420 LocaleRegister
, NULL
, MUIC_PrefsEditor
, NULL
,
421 OM_NEW
, struct opSet
*,
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