grub2: bring back build of aros-side grub2 tools
[AROS.git] / workbench / classes / zune / prefseditor / prefseditor.c
blobda5b0eafda92e28111c86b613c60ff6614dbed2f
1 /*
2 Copyright © 2004-2013, The AROS Development Team. All rights reserved.
3 This file is part of the PrefsEditor class, which is distributed under
4 the terms of version 2.1 of the GNU Lesser General Public License.
6 $Id$
7 */
9 #define MUIMASTER_YES_INLINE_STDARG
11 #include <utility/tagitem.h>
12 #include <libraries/mui.h>
13 #include <dos/dos.h>
15 #include <proto/exec.h>
16 #include <proto/muimaster.h>
17 #include <proto/intuition.h>
18 #include <proto/utility.h>
19 #include <proto/locale.h>
20 #include <proto/dos.h>
21 #include <proto/alib.h>
23 #include "support.h"
24 #include "prefseditor.h"
25 #include "prefseditor_private.h"
27 #include <aros/debug.h>
29 /*** Macros and defines *****************************************************/
30 #define SETUP_INST_DATA struct PrefsEditor_DATA *data = INST_DATA(CLASS, self)
32 #define ENV ((IPTR) "ENV:")
33 #define ENVARC ((IPTR) "ENVARC:")
35 /*** Methods ****************************************************************/
36 Object *PrefsEditor__OM_NEW
38 Class *CLASS, Object *self, struct opSet *message
41 self = (Object *) DoSuperMethodA(CLASS, self, (Msg)message);
43 if (self != NULL)
45 SETUP_INST_DATA;
47 /*-- Set defaults --------------------------------------------------*/
48 data->ped_CanSave = TRUE;
49 data->ped_CanTest = TRUE;
50 data->ped_CanUse = TRUE;
52 /*-- Handle initial attribute values -------------------------------*/
53 SetAttrsA(self, message->ops_AttrList);
57 return self;
60 IPTR PrefsEditor__MUIM_Setup
62 Class *CLASS, Object *self, struct MUIP_Setup *message
65 SETUP_INST_DATA;
66 BPTR lock;
68 if (!DoSuperMethodA(CLASS, self, (Msg) message)) return FALSE;
70 /*-- Load preferences --------------------------------------------------*/
71 if (!DoMethod(self, MUIM_PrefsEditor_ImportFromDirectory, ENV))
73 if (!DoMethod(self, MUIM_PrefsEditor_ImportFromDirectory, ENVARC))
75 if (DoMethod(self, MUIM_PrefsEditor_SetDefaults))
77 SET(self, MUIA_PrefsEditor_Changed, TRUE);
82 /*-- Store backup of initial preferences -------------------------------*/
83 data->ped_BackupFH = CreateTemporary(data->ped_BackupPath, BACKUP_PREFIX);
84 if (data->ped_BackupFH != BNULL)
88 !DoMethod
90 self, MUIM_PrefsEditor_ExportFH, (IPTR) data->ped_BackupFH
94 /* Remove the incomplete backup file */
95 Close(data->ped_BackupFH);
96 DeleteFile(data->ped_BackupPath);
97 data->ped_BackupFH = BNULL;
101 if (data->ped_BackupFH == BNULL)
103 data->ped_CanTest = FALSE;
106 /*-- Completely disable save if ENVARC: is write-protected -------------*/
107 lock = Lock("ENVARC:", SHARED_LOCK);
108 if (lock != BNULL)
110 struct InfoData id;
112 if (Info(lock, &id))
114 if (id.id_DiskState == ID_WRITE_PROTECTED)
116 data->ped_CanSave = FALSE;
120 UnLock(lock);
122 else
124 data->ped_CanSave = FALSE;
127 return TRUE;
130 IPTR PrefsEditor__MUIM_Cleanup
132 Class *CLASS, Object *self, struct MUIP_Cleanup *message
135 SETUP_INST_DATA;
137 if (data->ped_BackupFH != BNULL)
139 Close(data->ped_BackupFH);
140 DeleteFile(data->ped_BackupPath);
143 return DoSuperMethodA(CLASS, self, (Msg) message);
146 IPTR PrefsEditor__OM_DISPOSE
148 Class *CLASS, Object *self, Msg message
151 SETUP_INST_DATA;
153 if (data->ped_Name != NULL) FreeVec(data->ped_Name);
154 if (data->ped_Path != NULL) FreeVec(data->ped_Path);
156 return DoSuperMethodA(CLASS, self, message);
159 IPTR PrefsEditor__OM_SET
161 Class *CLASS, Object *self, struct opSet *message
164 SETUP_INST_DATA;
165 struct TagItem *tstate = message->ops_AttrList;
166 struct TagItem *tag,
167 noforward_attrs[] =
169 { MUIA_Group_Forward, FALSE },
170 { TAG_MORE, (IPTR)tstate }
172 struct opSet noforward_message = *message;
173 noforward_message.ops_AttrList = noforward_attrs;
175 while ((tag = NextTagItem(&tstate)) != NULL)
177 switch (tag->ti_Tag)
179 case MUIA_PrefsEditor_Changed:
180 data->ped_Changed = tag->ti_Data;
181 break;
183 case MUIA_PrefsEditor_Testing:
184 data->ped_Testing = tag->ti_Data;
185 break;
187 case MUIA_PrefsEditor_CanTest:
188 data->ped_CanTest = tag->ti_Data;
189 break;
191 case MUIA_PrefsEditor_Name:
192 if (data->ped_Name != NULL) FreeVec(data->ped_Name);
193 data->ped_Name = StrDup((CONST_STRPTR) tag->ti_Data);
194 break;
196 case MUIA_PrefsEditor_Path:
197 if (data->ped_Path != NULL) FreeVec(data->ped_Path);
198 data->ped_Path = StrDup((CONST_STRPTR) tag->ti_Data);
199 break;
201 case MUIA_PrefsEditor_IconTool:
202 if (data->ped_IconTool != NULL) FreeVec(data->ped_IconTool);
203 data->ped_IconTool = StrDup((CONST_STRPTR) tag->ti_Data);
204 break;
206 case MUIA_PrefsEditor_CanUse:
207 data->ped_CanUse = tag->ti_Data;
208 break;
212 return DoSuperMethodA(CLASS, self, (Msg) &noforward_message);
215 IPTR PrefsEditor__OM_GET
217 Class *CLASS, Object *self, struct opGet *message
220 SETUP_INST_DATA;
221 IPTR *store = message->opg_Storage;
222 IPTR rv = TRUE;
224 switch (message->opg_AttrID)
226 case MUIA_PrefsEditor_Changed:
227 *store = data->ped_Changed;
228 break;
230 case MUIA_PrefsEditor_Testing:
231 *store = data->ped_Testing;
232 break;
234 case MUIA_PrefsEditor_CanSave:
235 *store = data->ped_CanSave;
236 break;
238 case MUIA_PrefsEditor_CanTest:
239 *store = data->ped_CanTest;
240 break;
242 case MUIA_PrefsEditor_Name:
243 *store = (IPTR) data->ped_Name;
244 break;
246 case MUIA_PrefsEditor_Path:
247 *store = (IPTR) data->ped_Path;
248 break;
250 case MUIA_PrefsEditor_IconTool:
251 *store = (IPTR) data->ped_IconTool;
252 break;
254 case MUIA_PrefsEditor_CanUse:
255 *store = data->ped_CanUse;
256 break;
258 default:
259 rv = DoSuperMethodA(CLASS, self, (Msg) message);
262 return rv;
265 IPTR PrefsEditor__MUIM_PrefsEditor_ExportToDirectory
267 Class *CLASS, Object *self,
268 struct MUIP_PrefsEditor_ExportToDirectory *message
271 SETUP_INST_DATA;
272 BOOL success = FALSE;
273 BPTR lock = Lock(message->directory, ACCESS_READ);
275 if (lock != BNULL)
277 BPTR oldcd = CurrentDir(lock);
279 success = DoMethod
281 self, MUIM_PrefsEditor_Export, (IPTR) data->ped_Path
284 CurrentDir(oldcd);
286 UnLock(lock);
289 return success;
292 IPTR PrefsEditor__MUIM_PrefsEditor_ImportFromDirectory
294 Class *CLASS, Object *self,
295 struct MUIP_PrefsEditor_ImportFromDirectory *message
298 SETUP_INST_DATA;
299 BOOL success = FALSE;
300 BPTR lock = Lock(message->directory, ACCESS_READ);
302 if (lock != BNULL)
304 BPTR oldcd = CurrentDir(lock);
306 success = DoMethod
308 self, MUIM_PrefsEditor_Import, (IPTR) data->ped_Path
311 CurrentDir(oldcd);
313 UnLock(lock);
317 return success;
320 IPTR PrefsEditor__MUIM_PrefsEditor_Import
322 Class *CLASS, Object *self, struct MUIP_PrefsEditor_Import *message
325 BOOL success = FALSE;
326 BPTR fh;
328 if ((fh = Open(message->filename, MODE_OLDFILE)) != BNULL)
330 success = DoMethod(self, MUIM_PrefsEditor_ImportFH, (IPTR) fh);
331 Close(fh);
334 return success;
337 IPTR PrefsEditor__MUIM_PrefsEditor_Export
339 Class *CLASS, Object *self, struct MUIP_PrefsEditor_Export *message
342 BOOL success = FALSE;
343 BPTR fh;
345 fh = Open(message->filename, MODE_NEWFILE);
347 if (fh == BNULL && IoErr() == ERROR_OBJECT_NOT_FOUND)
349 /* Attempt to create missing directories */
350 /* MakeDirs() will modify the string in-place */
351 STRPTR tmp = StrDup(message->filename);
352 if (tmp != NULL)
354 MakeDirs(tmp);
355 fh = Open(message->filename, MODE_NEWFILE);
356 FreeVec(tmp);
360 if (fh != BNULL)
362 success = DoMethod(self, MUIM_PrefsEditor_ExportFH, (IPTR) fh);
363 Close(fh);
366 return success;
369 IPTR PrefsEditor__MUIM_PrefsEditor_Test
371 Class *CLASS, Object *self, Msg message
374 if (DoMethod(self, MUIM_PrefsEditor_ExportToDirectory, ENV))
376 SET(self, MUIA_PrefsEditor_Changed, FALSE);
377 SET(self, MUIA_PrefsEditor_Testing, TRUE);
379 return TRUE;
382 return FALSE;
385 IPTR PrefsEditor__MUIM_PrefsEditor_Revert
387 Class *CLASS, Object *self, Msg message
390 SETUP_INST_DATA;
392 if (Seek(data->ped_BackupFH, 0, OFFSET_BEGINNING) == -1)
393 return FALSE;//error
397 DoMethod(self, MUIM_PrefsEditor_ImportFH, (IPTR) data->ped_BackupFH)
398 && DoMethod(self, MUIM_PrefsEditor_ExportToDirectory, ENV)
401 SET(self, MUIA_PrefsEditor_Changed, FALSE);
402 SET(self, MUIA_PrefsEditor_Testing, FALSE);
404 return TRUE;
407 return FALSE;//error
411 IPTR PrefsEditor__MUIM_PrefsEditor_Save
413 Class *CLASS, Object *self, Msg message
418 DoMethod(self, MUIM_PrefsEditor_Use)
419 && DoMethod(self, MUIM_PrefsEditor_ExportToDirectory, ENVARC)
422 return TRUE;
425 return FALSE;
428 IPTR PrefsEditor__MUIM_PrefsEditor_Use
430 Class *CLASS, Object *self, Msg message
433 if (DoMethod(self, MUIM_PrefsEditor_ExportToDirectory, ENV))
435 SET(self, MUIA_PrefsEditor_Changed, FALSE);
436 SET(self, MUIA_PrefsEditor_Testing, FALSE);
438 return TRUE;
441 return FALSE;
444 IPTR PrefsEditor__MUIM_PrefsEditor_Cancel
446 Class *CLASS, Object *self, Msg message
449 if (XGET(self, MUIA_PrefsEditor_Testing))
451 return DoMethod(self, MUIM_PrefsEditor_Revert);
454 return TRUE;