Updated PCI IDs to latest snapshot.
[tangerine.git] / workbench / classes / zune / prefseditor / prefseditor.c
blob0927c1ffbec0502c2dc1f507af9445f5e10bb5e2
1 /*
2 Copyright © 2004, 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 /*-- Handle initial attribute values -------------------------------*/
48 SetAttrsA(self, message->ops_AttrList);
50 /*-- Set defaults --------------------------------------------------*/
51 data->ped_CanSave = TRUE;
52 data->ped_CanTest = TRUE;
55 return self;
58 IPTR PrefsEditor__MUIM_Setup
60 Class *CLASS, Object *self, struct MUIP_Setup *message
63 SETUP_INST_DATA;
64 BPTR lock;
66 if (!DoSuperMethodA(CLASS, self, (Msg) message)) return FALSE;
68 /*-- Load preferences --------------------------------------------------*/
69 if (!DoMethod(self, MUIM_PrefsEditor_ImportFromDirectory, ENV))
71 DoMethod(self, MUIM_PrefsEditor_ImportFromDirectory, ENVARC);
74 /*-- Store backup of initial preferences -------------------------------*/
75 data->ped_BackupFH = CreateTemporary(data->ped_BackupPath, BACKUP_PREFIX);
76 if (data->ped_BackupFH != NULL)
80 !DoMethod
82 self, MUIM_PrefsEditor_ExportFH, (IPTR) data->ped_BackupFH
86 /* Remove the incomplete backup file */
87 Close(data->ped_BackupFH);
88 DeleteFile(data->ped_BackupPath);
89 data->ped_BackupFH = NULL;
93 if (data->ped_BackupFH == NULL)
95 data->ped_CanTest = FALSE;
98 /*-- Completely disable save if ENVARC: is write-protected -------------*/
99 lock = Lock("ENVARC:", SHARED_LOCK);
100 if (lock != NULL)
102 struct InfoData id;
104 if (Info(lock, &id))
106 if (id.id_DiskState == ID_WRITE_PROTECTED)
108 data->ped_CanSave = FALSE;
112 UnLock(lock);
114 else
116 data->ped_CanSave = FALSE;
119 return TRUE;
122 IPTR PrefsEditor__MUIM_Cleanup
124 Class *CLASS, Object *self, struct MUIP_Cleanup *message
127 SETUP_INST_DATA;
129 if (data->ped_BackupFH != NULL)
131 Close(data->ped_BackupFH);
132 DeleteFile(data->ped_BackupPath);
135 return DoSuperMethodA(CLASS, self, (Msg) message);
138 IPTR PrefsEditor__OM_DISPOSE
140 Class *CLASS, Object *self, Msg message
143 SETUP_INST_DATA;
145 if (data->ped_Name != NULL) FreeVec(data->ped_Name);
146 if (data->ped_Path != NULL) FreeVec(data->ped_Path);
148 return DoSuperMethodA(CLASS, self, message);
151 IPTR PrefsEditor__OM_SET
153 Class *CLASS, Object *self, struct opSet *message
156 SETUP_INST_DATA;
157 const struct TagItem *tstate = message->ops_AttrList;
158 struct TagItem *tag,
159 noforward_attrs[] =
161 { MUIA_Group_Forward, FALSE },
162 { TAG_MORE, (IPTR)tstate }
164 struct opSet noforward_message = *message;
165 noforward_message.ops_AttrList = noforward_attrs;
167 while ((tag = NextTagItem(&tstate)) != NULL)
169 switch (tag->ti_Tag)
171 case MUIA_PrefsEditor_Changed:
172 data->ped_Changed = tag->ti_Data;
173 break;
175 case MUIA_PrefsEditor_Testing:
176 data->ped_Testing = tag->ti_Data;
177 break;
179 case MUIA_PrefsEditor_Name:
180 if (data->ped_Name != NULL) FreeVec(data->ped_Name);
181 data->ped_Name = StrDup((CONST_STRPTR) tag->ti_Data);
182 break;
184 case MUIA_PrefsEditor_Path:
185 if (data->ped_Path != NULL) FreeVec(data->ped_Path);
186 data->ped_Path = StrDup((CONST_STRPTR) tag->ti_Data);
187 break;
191 return DoSuperMethodA(CLASS, self, (Msg) &noforward_message);
194 IPTR PrefsEditor__OM_GET
196 Class *CLASS, Object *self, struct opGet *message
199 SETUP_INST_DATA;
200 IPTR *store = message->opg_Storage;
201 IPTR rv = TRUE;
203 switch (message->opg_AttrID)
205 case MUIA_PrefsEditor_Changed:
206 *store = data->ped_Changed;
207 break;
209 case MUIA_PrefsEditor_Testing:
210 *store = data->ped_Testing;
211 break;
213 case MUIA_PrefsEditor_CanSave:
214 *store = data->ped_CanSave;
215 break;
217 case MUIA_PrefsEditor_CanTest:
218 *store = data->ped_CanTest;
219 break;
221 case MUIA_PrefsEditor_Name:
222 *store = (IPTR) data->ped_Name;
223 break;
225 case MUIA_PrefsEditor_Path:
226 *store = (IPTR) data->ped_Path;
227 break;
229 default:
230 rv = DoSuperMethodA(CLASS, self, (Msg) message);
233 return rv;
236 IPTR PrefsEditor__MUIM_PrefsEditor_ExportToDirectory
238 Class *CLASS, Object *self,
239 struct MUIP_PrefsEditor_ExportToDirectory *message
242 SETUP_INST_DATA;
243 BOOL success = FALSE;
244 BPTR lock = Lock(message->directory, ACCESS_READ);
246 if (lock != NULL)
248 BPTR oldcd = CurrentDir(lock);
250 success = DoMethod
252 self, MUIM_PrefsEditor_Export, (IPTR) data->ped_Path
255 CurrentDir(oldcd);
257 UnLock(lock);
260 return success;
263 IPTR PrefsEditor__MUIM_PrefsEditor_ImportFromDirectory
265 Class *CLASS, Object *self,
266 struct MUIP_PrefsEditor_ImportFromDirectory *message
269 SETUP_INST_DATA;
270 BOOL success = FALSE;
271 BPTR lock = Lock(message->directory, ACCESS_READ);
273 if (lock != NULL)
275 BPTR oldcd = CurrentDir(lock);
277 success = DoMethod
279 self, MUIM_PrefsEditor_Import, (IPTR) data->ped_Path
282 CurrentDir(oldcd);
284 UnLock(lock);
288 return success;
291 IPTR PrefsEditor__MUIM_PrefsEditor_Import
293 Class *CLASS, Object *self, struct MUIP_PrefsEditor_Import *message
296 BOOL success = FALSE;
297 BPTR fh;
299 if ((fh = Open(message->filename, MODE_OLDFILE)) != NULL)
301 success = DoMethod(self, MUIM_PrefsEditor_ImportFH, (IPTR) fh);
302 Close(fh);
305 return success;
308 IPTR PrefsEditor__MUIM_PrefsEditor_Export
310 Class *CLASS, Object *self, struct MUIP_PrefsEditor_Export *message
313 BOOL success = FALSE;
314 BPTR fh;
316 fh = Open(message->filename, MODE_NEWFILE);
318 if (fh == NULL && IoErr() == ERROR_OBJECT_NOT_FOUND)
320 /* Attempt to create missing directories */
321 /* MakeDirs() will modify the string in-place */
322 STRPTR tmp = StrDup(message->filename);
323 if (tmp != NULL)
325 MakeDirs(tmp);
326 fh = Open(message->filename, MODE_NEWFILE);
327 FreeVec(tmp);
331 if (fh != NULL)
333 success = DoMethod(self, MUIM_PrefsEditor_ExportFH, (IPTR) fh);
334 Close(fh);
337 return success;
340 IPTR PrefsEditor__MUIM_PrefsEditor_Test
342 Class *CLASS, Object *self, Msg message
345 if (DoMethod(self, MUIM_PrefsEditor_ExportToDirectory, ENV))
347 SET(self, MUIA_PrefsEditor_Changed, FALSE);
348 SET(self, MUIA_PrefsEditor_Testing, TRUE);
350 return TRUE;
353 return FALSE;
356 IPTR PrefsEditor__MUIM_PrefsEditor_Revert
358 Class *CLASS, Object *self, Msg message
361 SETUP_INST_DATA;
363 if (Seek(data->ped_BackupFH, 0, OFFSET_BEGINNING) == -1)
364 return FALSE;//error
368 DoMethod(self, MUIM_PrefsEditor_ImportFH, (IPTR) data->ped_BackupFH)
369 && DoMethod(self, MUIM_PrefsEditor_ExportToDirectory, ENV)
372 SET(self, MUIA_PrefsEditor_Changed, FALSE);
373 SET(self, MUIA_PrefsEditor_Testing, FALSE);
375 return TRUE;
378 return FALSE;//error
382 IPTR PrefsEditor__MUIM_PrefsEditor_Save
384 Class *CLASS, Object *self, Msg message
389 DoMethod(self, MUIM_PrefsEditor_Use)
390 && DoMethod(self, MUIM_PrefsEditor_ExportToDirectory, ENVARC)
393 return TRUE;
396 return FALSE;
399 IPTR PrefsEditor__MUIM_PrefsEditor_Use
401 Class *CLASS, Object *self, Msg message
404 if (DoMethod(self, MUIM_PrefsEditor_ExportToDirectory, ENV))
406 SET(self, MUIA_PrefsEditor_Changed, FALSE);
407 SET(self, MUIA_PrefsEditor_Testing, FALSE);
409 return TRUE;
412 return FALSE;
415 IPTR PrefsEditor__MUIM_PrefsEditor_Cancel
417 Class *CLASS, Object *self, Msg message
420 if (XGET(self, MUIA_PrefsEditor_Testing))
422 return DoMethod(self, MUIM_PrefsEditor_Revert);
425 return TRUE;