2 Copyright © 2006, The AROS Development Team. All rights reserved.
6 #define MUIMASTER_YES_INLINE_STDARG
9 #include <aros/debug.h>
11 #include <proto/exec.h>
12 #include <proto/intuition.h>
13 #include <proto/muimaster.h>
14 #include <proto/workbench.h>
15 #include <proto/dos.h>
16 #include <proto/icon.h>
19 #include <libraries/mui.h>
20 #include <workbench/startup.h>
28 char versionstring
[] = "$VER: WBNewDrawer 0.5 (10.04.2006) ©2006 AROS Dev Team";
30 static STRPTR
AllocateNameFromLock(BPTR lock
);
31 static void bt_ok_hook_function(void);
32 static void Cleanup(STRPTR s
);
33 static BOOL
doNewDrawer(void);
34 static void MakeGUI(void);
35 static CONST_STRPTR
SelectDefaultName(STRPTR basename
);
37 static Object
*app
, *window
, *bt_ok
, *bt_cancel
, *cm_icons
, *str_name
;
38 static struct Hook bt_ok_hook
;
39 static BPTR dirlock
= (BPTR
)-1;
40 static BPTR oldlock
= (BPTR
)-1;
41 static STRPTR illegal_chars
= "/:";
44 int main(int argc
, char **argv
)
46 struct WBStartup
*startup
;
50 PutStr(_(MSG_WB_ONLY
));
53 startup
= (struct WBStartup
*) argv
;
55 D(bug("[NewDrawer] Args %d\n", startup
->sm_NumArgs
));
57 if (startup
->sm_NumArgs
!= 2)
58 Cleanup(_(MSG_NEEDS_MORE_ARGS
));
60 dirlock
= startup
->sm_ArgList
[1].wa_Lock
;
62 Cleanup(_(MSG_INVALID_LOCK
));
64 oldlock
= CurrentDir(dirlock
);
67 STRPTR fullname
= AllocateNameFromLock(dirlock
);
68 UpdateWorkbenchObject(fullname
, WBDRAWER
, TAG_DONE
);
76 static void MakeGUI(void)
78 CONST_STRPTR defname
= SelectDefaultName(_(MSG_BASENAME
));
79 bt_ok_hook
.h_Entry
= (APTR
)bt_ok_hook_function
;
80 app
= (Object
*)ApplicationObject
,
81 MUIA_Application_Title
, __(MSG_TITLE
),
82 MUIA_Application_Version
, (IPTR
) versionstring
,
83 MUIA_Application_Copyright
, __(MSG_COPYRIGHT
),
84 MUIA_Application_Author
, (IPTR
) "The AROS Development Team",
85 MUIA_Application_Description
, __(MSG_DESCRIPTION
),
86 MUIA_Application_Base
, (IPTR
) "NEWDRAWER",
87 MUIA_Application_UseCommodities
, FALSE
,
88 MUIA_Application_UseRexx
, FALSE
,
89 SubWindow
, (IPTR
)(window
= (Object
*)WindowObject
,
90 MUIA_Window_Title
, __(MSG_WINDOW_TITLE
),
91 MUIA_Window_NoMenus
, TRUE
,
92 MUIA_Window_CloseGadget
, FALSE
,
93 WindowContents
, (IPTR
) (VGroup
,
94 MUIA_Frame
, MUIV_Frame_Group
,
95 Child
, (IPTR
) (HGroup
,
96 Child
, (IPTR
) HVSpace
,
97 Child
, (IPTR
) Label2(__(MSG_LINE
)),
99 Child
, (IPTR
) (ColGroup(2),
100 Child
, (IPTR
) Label2(__(MSG_NAME
)),
101 Child
, (IPTR
)(str_name
= (Object
*)StringObject
,
103 MUIA_String_Contents
, (IPTR
) defname
,
104 MUIA_String_MaxLen
, MAXFILENAMELENGTH
,
105 MUIA_String_Reject
, (IPTR
) illegal_chars
,
106 MUIA_String_Columns
, -1,
107 MUIA_Frame
, MUIV_Frame_String
,
109 Child
, (IPTR
) Label2(__(MSG_ICON
)),
110 Child
, (IPTR
)(HGroup
,
111 Child
, (IPTR
) (cm_icons
= MUI_MakeObject(MUIO_Checkmark
, NULL
)),
112 Child
, (IPTR
) HVSpace
,
115 Child
, (IPTR
) (RectangleObject
,
116 MUIA_Rectangle_HBar
, TRUE
,
119 Child
, (IPTR
) (HGroup
,
120 Child
, (IPTR
) (bt_ok
= ImageButton(__(MSG_OK
), "THEME:Images/Gadgets/Prefs/Save")),
121 Child
, (IPTR
) (bt_cancel
= ImageButton(__(MSG_CANCEL
),"THEME:Images/Gadgets/Prefs/Cancel")),
126 FreeVec((APTR
)defname
);
129 Cleanup(_(MSG_FAILED_CREATE_APP
));
131 DoMethod(window
, MUIM_Notify
, MUIA_Window_CloseRequest
, TRUE
,
132 app
, 2, MUIM_Application_ReturnID
, MUIV_Application_ReturnID_Quit
);
133 DoMethod(bt_cancel
, MUIM_Notify
, MUIA_Pressed
, FALSE
,
134 app
, 2, MUIM_Application_ReturnID
, MUIV_Application_ReturnID_Quit
);
135 DoMethod(bt_ok
, MUIM_Notify
, MUIA_Pressed
, FALSE
,
136 app
, 2, MUIM_CallHook
, (IPTR
)&bt_ok_hook
);
137 set(cm_icons
, MUIA_CycleChain
, 1);
138 set(cm_icons
, MUIA_Selected
, TRUE
);
139 set(window
, MUIA_Window_Open
, TRUE
);
140 DoMethod(app
, MUIM_Application_Execute
);
144 static void bt_ok_hook_function(void)
148 DoMethod(app
, MUIM_Application_ReturnID
, MUIV_Application_ReturnID_Quit
);
153 static BOOL
doNewDrawer(void)
156 BOOL icon
= XGET(cm_icons
, MUIA_Selected
);
157 STRPTR name
= (STRPTR
)XGET(str_name
, MUIA_String_Contents
);
159 struct DiskObject
*dob
= NULL
;
160 D(bug("WBNewDrawer name %s icon %d\n", name
, icon
));
162 if (strpbrk(name
, illegal_chars
))
164 MUI_Request(app
, window
, 0, _(MSG_ERROR_TITLE
), _(MSG_OK
), _(MSG_ILLEGAL_CHARS
), name
);
168 test
= Lock(name
, ACCESS_READ
);
172 MUI_Request(app
, window
, 0, _(MSG_ERROR_TITLE
), _(MSG_OK
), _(MSG_ALREADY_EXIST
), name
);
176 dob
= GetDiskObject(name
);
177 // if icon exists it must be of type WBDRAWER
178 if (dob
&& (dob
->do_Type
!= WBDRAWER
))
180 MUI_Request(app
, window
, 0, _(MSG_ERROR_TITLE
), _(MSG_OK
), _(MSG_WRONG_ICON_TYPE
), name
);
186 if ((dstLock
= CreateDir(name
)))
190 MUI_Request(app
, window
, 0, _(MSG_ERROR_TITLE
), _(MSG_OK
), _(MSG_CANT_CREATE
), name
);
195 // if the icon already exists and is of right type then no actions happens.
196 if (icon
&& (dob
== NULL
))
198 dob
= GetDiskObjectNew(name
);
199 PutDiskObject(name
, dob
);
202 UpdateWorkbenchObject(name
, WBDRAWER
, TAG_DONE
);
212 * Try to find an unique file name.
213 * You have to free the return value with FreeVec.
216 static CONST_STRPTR
SelectDefaultName(STRPTR basename
)
218 if (basename
== NULL
)
219 basename
= "Rename_Me";
223 STRPTR buffer
= AllocVec(strlen(basename
) + 3, MEMF_ANY
);
230 strcpy(buffer
, basename
);
232 sprintf(buffer
,"%s_%d", basename
, number
);
234 test
= Lock(buffer
, ACCESS_READ
);
238 while ((number
< 10) && (test
!= NULL
));
243 static STRPTR
AllocateNameFromLock(BPTR lock
)
246 STRPTR buffer
= NULL
;
253 buffer
= AllocVec(length
, MEMF_ANY
);
256 if (NameFromLock(lock
, buffer
, length
))
263 if (IoErr() == ERROR_LINE_TOO_LONG
)
276 SetIoErr(ERROR_NO_FREE_STORE
);
293 static void Cleanup(STRPTR s
)
295 MUI_DisposeObject(app
);
297 if (oldlock
!= (BPTR
)-1)
304 struct EasyStruct es
;
305 es
.es_StructSize
= sizeof(struct EasyStruct
);
307 es
.es_Title
= _(MSG_ERROR_TITLE
);
308 es
.es_TextFormat
= s
;
309 es
.es_GadgetFormat
= _(MSG_OK
);
310 EasyRequest(NULL
, &es
, NULL
, NULL
);