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.
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 /*-- 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
);
60 IPTR PrefsEditor__MUIM_Setup
62 Class
*CLASS
, Object
*self
, struct MUIP_Setup
*message
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
)
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
);
114 if (id
.id_DiskState
== ID_WRITE_PROTECTED
)
116 data
->ped_CanSave
= FALSE
;
124 data
->ped_CanSave
= FALSE
;
130 IPTR PrefsEditor__MUIM_Cleanup
132 Class
*CLASS
, Object
*self
, struct MUIP_Cleanup
*message
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
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
165 struct TagItem
*tstate
= message
->ops_AttrList
;
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
)
179 case MUIA_PrefsEditor_Changed
:
180 data
->ped_Changed
= tag
->ti_Data
;
183 case MUIA_PrefsEditor_Testing
:
184 data
->ped_Testing
= tag
->ti_Data
;
187 case MUIA_PrefsEditor_CanTest
:
188 data
->ped_CanTest
= tag
->ti_Data
;
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
);
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
);
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
);
206 case MUIA_PrefsEditor_CanUse
:
207 data
->ped_CanUse
= tag
->ti_Data
;
212 return DoSuperMethodA(CLASS
, self
, (Msg
) &noforward_message
);
215 IPTR PrefsEditor__OM_GET
217 Class
*CLASS
, Object
*self
, struct opGet
*message
221 IPTR
*store
= message
->opg_Storage
;
224 switch (message
->opg_AttrID
)
226 case MUIA_PrefsEditor_Changed
:
227 *store
= data
->ped_Changed
;
230 case MUIA_PrefsEditor_Testing
:
231 *store
= data
->ped_Testing
;
234 case MUIA_PrefsEditor_CanSave
:
235 *store
= data
->ped_CanSave
;
238 case MUIA_PrefsEditor_CanTest
:
239 *store
= data
->ped_CanTest
;
242 case MUIA_PrefsEditor_Name
:
243 *store
= (IPTR
) data
->ped_Name
;
246 case MUIA_PrefsEditor_Path
:
247 *store
= (IPTR
) data
->ped_Path
;
250 case MUIA_PrefsEditor_IconTool
:
251 *store
= (IPTR
) data
->ped_IconTool
;
254 case MUIA_PrefsEditor_CanUse
:
255 *store
= data
->ped_CanUse
;
259 rv
= DoSuperMethodA(CLASS
, self
, (Msg
) message
);
265 IPTR PrefsEditor__MUIM_PrefsEditor_ExportToDirectory
267 Class
*CLASS
, Object
*self
,
268 struct MUIP_PrefsEditor_ExportToDirectory
*message
272 BOOL success
= FALSE
;
273 BPTR lock
= Lock(message
->directory
, ACCESS_READ
);
277 BPTR oldcd
= CurrentDir(lock
);
281 self
, MUIM_PrefsEditor_Export
, (IPTR
) data
->ped_Path
292 IPTR PrefsEditor__MUIM_PrefsEditor_ImportFromDirectory
294 Class
*CLASS
, Object
*self
,
295 struct MUIP_PrefsEditor_ImportFromDirectory
*message
299 BOOL success
= FALSE
;
300 BPTR lock
= Lock(message
->directory
, ACCESS_READ
);
304 BPTR oldcd
= CurrentDir(lock
);
308 self
, MUIM_PrefsEditor_Import
, (IPTR
) data
->ped_Path
320 IPTR PrefsEditor__MUIM_PrefsEditor_Import
322 Class
*CLASS
, Object
*self
, struct MUIP_PrefsEditor_Import
*message
325 BOOL success
= FALSE
;
328 if ((fh
= Open(message
->filename
, MODE_OLDFILE
)) != BNULL
)
330 success
= DoMethod(self
, MUIM_PrefsEditor_ImportFH
, (IPTR
) fh
);
337 IPTR PrefsEditor__MUIM_PrefsEditor_Export
339 Class
*CLASS
, Object
*self
, struct MUIP_PrefsEditor_Export
*message
342 BOOL success
= FALSE
;
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
);
355 fh
= Open(message
->filename
, MODE_NEWFILE
);
362 success
= DoMethod(self
, MUIM_PrefsEditor_ExportFH
, (IPTR
) fh
);
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
);
385 IPTR PrefsEditor__MUIM_PrefsEditor_Revert
387 Class
*CLASS
, Object
*self
, Msg message
392 if (Seek(data
->ped_BackupFH
, 0, OFFSET_BEGINNING
) == -1)
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
);
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
)
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
);
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
);