Fixed binary search: no more infinite loops when vendor is unknown.
[tangerine.git] / workbench / prefs / locale / registertab.c
blob84ac2e6b168cef8ef60825d2eaee02915f64f9bd
1 /*
2 Copyright 2003-2008, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 // #define MUIMASTER_YES_INLINE_STDARG
8 #include <exec/types.h>
9 #include <utility/tagitem.h>
10 #include <libraries/asl.h>
11 #include <libraries/mui.h>
12 #include <prefs/locale.h>
13 #include <prefs/prefhdr.h>
14 //#define DEBUG 1
15 #include <zune/customclasses.h>
16 #include <zune/prefseditor.h>
18 #include <proto/exec.h>
19 #include <proto/intuition.h>
20 #include <proto/utility.h>
21 #include <proto/muimaster.h>
22 #include <proto/dos.h>
23 #include <proto/iffparse.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <stdlib.h>
29 #include <aros/debug.h>
31 #include "global.h"
32 #include "locale.h"
33 #include "registertab.h"
34 #include "page_language.h"
35 #include "page_country.h"
36 #include "page_timezone.h"
38 /*** Instance Data **********************************************************/
40 struct LocaleRegister_DATA
42 int i;
44 Object *child;
45 Object *language;
46 Object *country;
47 Object *timezone;
49 char *LocaleRegisterLabels[3];
50 char *tab_label;
53 STATIC VOID LocalePrefs2Gadgets(struct LocaleRegister_DATA *data);
54 STATIC VOID Gadgets2LocalePrefs(struct LocaleRegister_DATA *data);
55 VOID ShowMsg(char *msg);
58 /*** Macros *****************************************************************/
59 #define SETUP_INST_DATA struct LocaleRegister_DATA *data = INST_DATA(CLASS, self)
61 /*** Methods ****************************************************************/
64 static Object *handle_New_error(Object *obj, struct IClass *cl, char *error)
66 struct LocaleRegister_DATA *data;
68 ShowMsg(error);
69 D(bug("[Register class] %s\n",error));
71 if(!obj)
72 return NULL;
74 data = INST_DATA(cl, obj);
76 if(data->language)
78 D(bug("[Register class] DisposeObject(data->language);\n"));
79 DisposeObject(data->language);
80 data->language=NULL;
83 if(data->country)
85 D(bug("[Register class] DisposeObject(data->country);\n"));
86 DisposeObject(data->country);
87 data->country=NULL;
90 if(data->timezone)
92 D(bug("[Register class] DisposeObject(data->timezone);\n"));
93 DisposeObject(data->timezone);
94 data->timezone=NULL;
97 if(data->tab_label)
99 FreeVec(data->tab_label);
100 data->tab_label=NULL;
103 CoerceMethod(cl, obj, OM_DISPOSE);
104 return NULL;
107 Object *LocaleRegister__OM_NEW(Class *CLASS, Object *self, struct opSet *message)
109 D(bug("[register class] LocaleRegister Class New\n"));
112 * we create self first and then create the child,
113 * so we have self->data available already
116 self = (Object *) DoSuperNewTags(
117 CLASS, self, NULL,
118 MUIA_PrefsEditor_Name, MSG(MSG_WINTITLE),
119 MUIA_PrefsEditor_Path, (IPTR) "SYS/locale.prefs",
120 TAG_DONE
122 if (self == NULL)
124 return handle_New_error(self, CLASS, "ERROR: Unable to create register object!\n");
127 SETUP_INST_DATA;
129 data->language=NewObject(Language_CLASS->mcc_Class,NULL,
130 MA_PrefsObject, (ULONG) self,
131 TAG_DONE);
133 if(!data->language)
134 return handle_New_error(self,CLASS,"ERROR: Unable to create language object!\n");
136 data->country= ListviewObject, MUIA_Listview_List,
137 NewObject(Country_CLASS->mcc_Class,0,
138 MA_PrefsObject, (ULONG) self,
139 TAG_DONE),
140 End;
142 if(!data->country)
143 return handle_New_error(self,CLASS,"ERROR: Unable to create country object!\n");
145 data->timezone=NewObject(Timezone_CLASS->mcc_Class,NULL,
146 MA_PrefsObject, (ULONG) self,
147 TAG_DONE);
149 if(!data->timezone)
151 D(bug("icall register handle error\n"));
152 return handle_New_error(self,CLASS,"ERROR: Unable to create timezone object!\n");
157 * Maybe, it would be easier to change the catalog,
158 * but for me it's easier this way ;)
160 data->tab_label=AllocVec( strlen(MSG(MSG_GAD_TAB_LANGUAGE)) +
161 strlen(MSG(MSG_GAD_TAB_COUNTRY)) +
162 strlen(" / ") + 1,
163 MEMF_ANY);
165 if(!data->tab_label)
166 return handle_New_error(self,CLASS,"ERROR: Unable to allocate tab_label!\n");
168 sprintf(data->tab_label,"%s / %s",MSG(MSG_GAD_TAB_COUNTRY),
169 MSG(MSG_GAD_TAB_LANGUAGE));
171 data->LocaleRegisterLabels[0]=data->tab_label;
172 data->LocaleRegisterLabels[1]=MSG(MSG_GAD_TAB_TIMEZONE);
173 data->LocaleRegisterLabels[2]=NULL;
175 data->child=
176 RegisterGroup(data->LocaleRegisterLabels),
177 MUIA_Register_Frame, TRUE,
178 Child,
179 HGroup,
180 MUIA_Group_SameSize, TRUE,
181 Child,
182 HGroup,
183 MUIA_Frame, MUIV_Frame_Group,
184 MUIA_FrameTitle, MSG(MSG_GAD_TAB_COUNTRY),
185 Child,
186 data->country,
187 End,
188 Child,
189 HGroup,
190 MUIA_Frame, MUIV_Frame_Group,
191 MUIA_FrameTitle, MSG(MSG_GAD_TAB_LANGUAGE),
192 Child,
193 data->language,
194 End,
195 End,
196 Child,
197 data->timezone,
198 End;
200 if(!data->child)
201 return handle_New_error(self, CLASS, "ERROR: unable to create registergroup\n");
203 DoMethod(self,OM_ADDMEMBER,(ULONG) data->child);
205 DoMethod(data->country,COUNTRY_FILL);
207 LocalePrefs2Gadgets(data);
209 return self;
213 * update struct localprefs with actual data selected in gadgets:
215 * see prefs/locale.h
217 * struct LocalePrefs {
218 * char lp_CountryName[32];
219 * char lp_PreferredLanguages[10][30];
220 * LONG lp_GMTOffset;
221 * ULONG lp_Flags;
223 * struct CountryPrefs lp_CountryData;
224 * };
227 STATIC VOID Gadgets2LocalePrefs (struct LocaleRegister_DATA *data)
229 char *tmp;
230 char **preferred;
231 ULONG i;
233 if(get(data->country,MA_CountryName,&tmp))
235 strncpy(localeprefs.lp_CountryName,tmp,32);
238 if(get(data->language,MA_Preferred,&preferred))
240 for(i=0;i<10;i++)
242 if(preferred[i])
244 strncpy(localeprefs.lp_PreferredLanguages[i],preferred[i],30);
246 else
248 localeprefs.lp_PreferredLanguages[i][0]=(char) 0;
253 get(data->timezone, MA_TimeOffset, &localeprefs.lp_GMTOffset);
257 * update gadgets with values of struct localeprefs
259 STATIC VOID LocalePrefs2Gadgets(struct LocaleRegister_DATA *data)
262 set(data->country, MA_CountryName, localeprefs.lp_CountryName);
264 set(data->language, MA_Preferred, TRUE);
266 set(data->timezone, MA_TimeOffset, -localeprefs.lp_GMTOffset);
270 IPTR LocaleRegister__MUIM_PrefsEditor_ImportFH (
271 Class *CLASS, Object *self,
272 struct MUIP_PrefsEditor_ImportFH *message
275 SETUP_INST_DATA;
277 if (!LoadPrefsFH(message->fh)) {
278 D(bug("[register class] LocaleRegister__MUIM_PrefsEditor_ImportFH failed\n"));
279 return FALSE;
282 BackupPrefs();
283 LocalePrefs2Gadgets(data);
284 SET(self, MUIA_PrefsEditor_Changed, FALSE);
285 SET(self, MUIA_PrefsEditor_Testing, FALSE);
287 return TRUE;
290 IPTR LocaleRegister__MUIM_PrefsEditor_ExportFH
292 Class *CLASS, Object *self,
293 struct MUIP_PrefsEditor_ExportFH *message
296 SETUP_INST_DATA;
297 D(bug("[register class] SerEdit Class Export\n"));
299 Gadgets2LocalePrefs(data);
300 return SavePrefsFH(message->fh);
303 IPTR LocaleRegister__MUIM_PrefsEditor_Test
305 Class *CLASS, Object *self, Msg message
308 SETUP_INST_DATA;
309 BOOL result;
311 D(bug("[register class] SerEdit Class Test\n"));
313 Gadgets2LocalePrefs(data);
315 result=SaveEnv();
317 if(result) { /* TRUE -> success */
318 SET(self, MUIA_PrefsEditor_Changed, FALSE);
319 SET(self, MUIA_PrefsEditor_Testing, TRUE);
322 return result;
325 IPTR LocaleRegister__MUIM_PrefsEditor_Revert
327 Class *CLASS, Object *self, Msg message
330 SETUP_INST_DATA;
331 BOOL result;
333 D(bug("[register class] SerEdit Class Revert\n"));
335 RestorePrefs();
336 LocalePrefs2Gadgets(data);
338 result=SaveEnv();
340 if(result) {
341 SET(self, MUIA_PrefsEditor_Changed, FALSE);
342 SET(self, MUIA_PrefsEditor_Testing, FALSE);
345 return result;
348 IPTR LocaleRegister__OM_DISPOSE(Class *CLASS, Object *self, Msg message)
350 SETUP_INST_DATA;
352 if(data->tab_label)
354 FreeVec(data->tab_label);
355 data->tab_label=NULL;
358 return DoSuperMethodA(CLASS, self, message);
361 /*** Setup ******************************************************************/
362 ZUNE_CUSTOMCLASS_6
364 LocaleRegister, NULL, MUIC_PrefsEditor, NULL,
365 OM_NEW, struct opSet *,
366 OM_DISPOSE, Msg,
367 MUIM_PrefsEditor_ImportFH, struct MUIP_PrefsEditor_ImportFH *,
368 MUIM_PrefsEditor_ExportFH, struct MUIP_PrefsEditor_ExportFH *,
369 MUIM_PrefsEditor_Test, Msg,
370 MUIM_PrefsEditor_Revert, Msg