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.
9 #define MUIMASTER_YES_INLINE_STDARG
11 #include <utility/tagitem.h>
12 #include <libraries/mui.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>
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
);
47 /*-- Handle initial attribute values -------------------------------*/
48 SetAttrsA(self
, message
->ops_AttrList
);
50 /*-- Set defaults --------------------------------------------------*/
51 data
->ped_CanSave
= TRUE
;
52 data
->ped_CanTest
= TRUE
;
58 IPTR PrefsEditor__MUIM_Setup
60 Class
*CLASS
, Object
*self
, struct MUIP_Setup
*message
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
)
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
);
106 if (id
.id_DiskState
== ID_WRITE_PROTECTED
)
108 data
->ped_CanSave
= FALSE
;
116 data
->ped_CanSave
= FALSE
;
122 IPTR PrefsEditor__MUIM_Cleanup
124 Class
*CLASS
, Object
*self
, struct MUIP_Cleanup
*message
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
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
157 const struct TagItem
*tstate
= message
->ops_AttrList
;
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
)
171 case MUIA_PrefsEditor_Changed
:
172 data
->ped_Changed
= tag
->ti_Data
;
175 case MUIA_PrefsEditor_Testing
:
176 data
->ped_Testing
= tag
->ti_Data
;
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
);
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
);
191 return DoSuperMethodA(CLASS
, self
, (Msg
) &noforward_message
);
194 IPTR PrefsEditor__OM_GET
196 Class
*CLASS
, Object
*self
, struct opGet
*message
200 IPTR
*store
= message
->opg_Storage
;
203 switch (message
->opg_AttrID
)
205 case MUIA_PrefsEditor_Changed
:
206 *store
= data
->ped_Changed
;
209 case MUIA_PrefsEditor_Testing
:
210 *store
= data
->ped_Testing
;
213 case MUIA_PrefsEditor_CanSave
:
214 *store
= data
->ped_CanSave
;
217 case MUIA_PrefsEditor_CanTest
:
218 *store
= data
->ped_CanTest
;
221 case MUIA_PrefsEditor_Name
:
222 *store
= (IPTR
) data
->ped_Name
;
225 case MUIA_PrefsEditor_Path
:
226 *store
= (IPTR
) data
->ped_Path
;
230 rv
= DoSuperMethodA(CLASS
, self
, (Msg
) message
);
236 IPTR PrefsEditor__MUIM_PrefsEditor_ExportToDirectory
238 Class
*CLASS
, Object
*self
,
239 struct MUIP_PrefsEditor_ExportToDirectory
*message
243 BOOL success
= FALSE
;
244 BPTR lock
= Lock(message
->directory
, ACCESS_READ
);
248 BPTR oldcd
= CurrentDir(lock
);
252 self
, MUIM_PrefsEditor_Export
, (IPTR
) data
->ped_Path
263 IPTR PrefsEditor__MUIM_PrefsEditor_ImportFromDirectory
265 Class
*CLASS
, Object
*self
,
266 struct MUIP_PrefsEditor_ImportFromDirectory
*message
270 BOOL success
= FALSE
;
271 BPTR lock
= Lock(message
->directory
, ACCESS_READ
);
275 BPTR oldcd
= CurrentDir(lock
);
279 self
, MUIM_PrefsEditor_Import
, (IPTR
) data
->ped_Path
291 IPTR PrefsEditor__MUIM_PrefsEditor_Import
293 Class
*CLASS
, Object
*self
, struct MUIP_PrefsEditor_Import
*message
296 BOOL success
= FALSE
;
299 if ((fh
= Open(message
->filename
, MODE_OLDFILE
)) != NULL
)
301 success
= DoMethod(self
, MUIM_PrefsEditor_ImportFH
, (IPTR
) fh
);
308 IPTR PrefsEditor__MUIM_PrefsEditor_Export
310 Class
*CLASS
, Object
*self
, struct MUIP_PrefsEditor_Export
*message
313 BOOL success
= FALSE
;
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
);
326 fh
= Open(message
->filename
, MODE_NEWFILE
);
333 success
= DoMethod(self
, MUIM_PrefsEditor_ExportFH
, (IPTR
) fh
);
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
);
356 IPTR PrefsEditor__MUIM_PrefsEditor_Revert
358 Class
*CLASS
, Object
*self
, Msg message
363 if (Seek(data
->ped_BackupFH
, 0, OFFSET_BEGINNING
) == -1)
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
);
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
)
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
);
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
);