2 Copyright © 2006-2011, The AROS Development Team. All rights reserved.
6 #define MUIMASTER_YES_INLINE_STDARG
9 #include <aros/debug.h>
11 #include <proto/alib.h>
12 #include <proto/exec.h>
13 #include <proto/intuition.h>
14 #include <proto/muimaster.h>
15 #include <proto/workbench.h>
16 #include <proto/dos.h>
17 #include <proto/icon.h>
20 #include <libraries/mui.h>
21 #include <workbench/startup.h>
29 char versionstring
[] = "$VER: WBRename 0.4 (6.4.2011) ©2006-2011 AROS Dev Team";
31 static STRPTR
AllocateNameFromLock(BPTR lock
);
32 static void bt_ok_hook_function(void);
33 static void Cleanup(STRPTR s
);
34 static BOOL
doRename(const STRPTR oldname
, const STRPTR newname
);
35 static void MakeGUI(void);
37 static Object
*app
, *window
, *bt_ok
, *bt_cancel
, *str_name
;
38 static struct Hook bt_ok_hook
;
39 static BPTR parentlock
= (BPTR
)-1;
40 static STRPTR oldname
;
41 static TEXT str_line
[256];
42 static BPTR oldlock
= (BPTR
)-1;
43 static STRPTR illegal_chars
= "/:";
46 int main(int argc
, char **argv
)
48 struct WBStartup
*startup
;
53 PutStr(_(MSG_WB_ONLY
));
56 startup
= (struct WBStartup
*) argv
;
58 D(bug("[WBRename] Args %d\n", startup
->sm_NumArgs
));
60 if (startup
->sm_NumArgs
!= 2)
61 Cleanup(_(MSG_NEEDS_MORE_ARGS
));
63 parentlock
= startup
->sm_ArgList
[1].wa_Lock
;
64 oldname
= startup
->sm_ArgList
[1].wa_Name
;
65 if ((parentlock
== BNULL
) || (oldname
== NULL
))
66 Cleanup(_(MSG_INVALID_LOCK
));
68 oldlock
= CurrentDir(parentlock
);
71 DoMethod(app
, MUIM_Application_Execute
);
73 fullname
= AllocateNameFromLock(parentlock
);
74 UpdateWorkbenchObject(fullname
, WBDRAWER
, TAG_DONE
);
82 static void MakeGUI(void)
84 bt_ok_hook
.h_Entry
= (APTR
)bt_ok_hook_function
;
85 sprintf(str_line
, _(MSG_LINE
), oldname
);
86 app
= (Object
*)ApplicationObject
,
87 MUIA_Application_Title
, __(MSG_TITLE
),
88 MUIA_Application_Version
, (IPTR
) versionstring
,
89 MUIA_Application_Copyright
, __(MSG_COPYRIGHT
),
90 MUIA_Application_Author
, (IPTR
) "The AROS Development Team",
91 MUIA_Application_Description
, __(MSG_DESCRIPTION
),
92 MUIA_Application_Base
, (IPTR
) "WBRENAME",
93 MUIA_Application_UseCommodities
, FALSE
,
94 MUIA_Application_UseRexx
, FALSE
,
95 SubWindow
, (IPTR
)(window
= (Object
*)WindowObject
,
96 MUIA_Window_Title
, __(MSG_WINDOW_TITLE
),
97 MUIA_Window_ID
, MAKE_ID('W','B','R','N'),
98 MUIA_Window_Width
, MUIV_Window_Width_Visible(33), // Set width at least as 33% of visible screen
99 MUIA_Window_NoMenus
, TRUE
,
100 MUIA_Window_CloseGadget
, FALSE
,
101 WindowContents
, (IPTR
) (VGroup
,
102 MUIA_Frame
, MUIV_Frame_Group
,
103 Child
, (IPTR
) (HGroup
,
104 Child
, (IPTR
) HVSpace
,
105 Child
, (IPTR
) TextObject
,
106 MUIA_Text_PreParse
, (IPTR
) "\33r",
107 MUIA_Text_Contents
, (IPTR
) str_line
,
110 Child
, (IPTR
) (HGroup
,
111 Child
, (IPTR
) Label2(__(MSG_NAME
)),
112 Child
, (IPTR
)(str_name
= (Object
*)StringObject
,
114 MUIA_String_Contents
, (IPTR
) oldname
,
115 MUIA_String_MaxLen
, MAXFILENAMELENGTH
,
116 MUIA_String_Reject
, (IPTR
) illegal_chars
, // Doesn't work :-/
117 MUIA_String_Columns
, -1,
118 MUIA_Frame
, MUIV_Frame_String
,
121 Child
, (IPTR
) (RectangleObject
,
122 MUIA_Rectangle_HBar
, TRUE
,
125 Child
, (IPTR
) (HGroup
,
126 Child
, (IPTR
) (bt_ok
= ImageButton(__(MSG_OK
), "THEME:Images/Gadgets/OK")),
127 Child
, (IPTR
) (bt_cancel
= ImageButton(__(MSG_CANCEL
),"THEME:Images/Gadgets/Cancel")),
134 Cleanup(_(MSG_FAILED_CREATE_APP
));
136 DoMethod(window
, MUIM_Notify
, MUIA_Window_CloseRequest
, TRUE
,
137 app
, 2, MUIM_Application_ReturnID
, MUIV_Application_ReturnID_Quit
);
138 DoMethod(bt_cancel
, MUIM_Notify
, MUIA_Pressed
, FALSE
,
139 app
, 2, MUIM_Application_ReturnID
, MUIV_Application_ReturnID_Quit
);
140 DoMethod(bt_ok
, MUIM_Notify
, MUIA_Pressed
, FALSE
,
141 app
, 2, MUIM_CallHook
, (IPTR
)&bt_ok_hook
);
142 set(window
, MUIA_Window_Open
, TRUE
);
143 set(window
, MUIA_Window_ActiveObject
, str_name
);
144 set(window
, MUIA_Window_DefaultObject
, bt_ok
);
148 static void bt_ok_hook_function(void)
150 STRPTR newname
= (STRPTR
)XGET(str_name
, MUIA_String_Contents
);
151 D(bug("WBRename oldname %s newname %s \n", oldname
, newname
));
153 if (doRename(oldname
, newname
))
155 DoMethod(app
, MUIM_Application_ReturnID
, MUIV_Application_ReturnID_Quit
);
159 set(window
, MUIA_Window_Activate
, TRUE
);
160 set(window
, MUIA_Window_ActiveObject
, str_name
);
166 static BOOL
doRename(const STRPTR oldname
, const STRPTR newname
)
169 if (( oldname
== NULL
) || (newname
== NULL
))
172 STRPTR oldinfoname
= NULL
;
173 STRPTR newinfoname
= NULL
;
174 BOOL infoexists
=FALSE
;
177 oldinfoname
= AllocVec(strlen(oldname
) + 6, MEMF_ANY
);
180 MUI_Request(app
, window
, 0, _(MSG_ERROR_TITLE
), _(MSG_OK
), _(MSG_OUTOFMEMORY
));
183 strcpy(oldinfoname
, oldname
);
184 strcat(oldinfoname
, ".info");
186 newinfoname
= AllocVec(strlen(newname
) + 6, MEMF_ANY
);
189 MUI_Request(app
, window
, 0, _(MSG_ERROR_TITLE
), _(MSG_OK
), _(MSG_OUTOFMEMORY
));
192 strcpy(newinfoname
, newname
);
193 strcat(newinfoname
, ".info");
195 if (strpbrk(newname
, illegal_chars
))
197 MUI_Request(app
, window
, 0, _(MSG_ERROR_TITLE
), _(MSG_OK
), _(MSG_ILLEGAL_CHARS
), newname
);
201 if ((test
= Lock(newname
, ACCESS_READ
)))
204 MUI_Request(app
, window
, 0, _(MSG_ERROR_TITLE
), _(MSG_OK
), _(MSG_ALREADY_EXIST
), newname
);
208 if ((test
= Lock(oldinfoname
, ACCESS_READ
)))
211 infoexists
= TRUE
; // we have an .info file
212 test
= Lock(newinfoname
, ACCESS_READ
);
216 MUI_Request(app
, window
, 0, _(MSG_ERROR_TITLE
), _(MSG_OK
), _(MSG_ALREADY_EXIST
), newinfoname
);
221 if (Rename(oldname
, newname
) == DOSFALSE
)
223 MUI_Request(app
, window
, 0, _(MSG_ERROR_TITLE
), _(MSG_OK
), _(MSG_FAILED
), oldname
, GetDosErrorString(IoErr()));
229 if ( Rename(oldinfoname
, newinfoname
) == DOSFALSE
)
231 MUI_Request(app
, window
, 0, _(MSG_ERROR_TITLE
), _(MSG_OK
), _(MSG_FAILED
), oldinfoname
, GetDosErrorString(IoErr()));
239 FreeVec(oldinfoname
);
240 FreeVec(newinfoname
);
245 static STRPTR
AllocateNameFromLock(BPTR lock
)
248 STRPTR buffer
= NULL
;
255 buffer
= AllocVec(length
, MEMF_ANY
);
258 if (NameFromLock(lock
, buffer
, length
))
265 if (IoErr() == ERROR_LINE_TOO_LONG
)
278 SetIoErr(ERROR_NO_FREE_STORE
);
295 static void Cleanup(STRPTR s
)
297 MUI_DisposeObject(app
);
299 if (oldlock
!= (BPTR
)-1)
306 struct EasyStruct es
;
307 es
.es_StructSize
= sizeof(struct EasyStruct
);
309 es
.es_Title
= _(MSG_ERROR_TITLE
);
310 es
.es_TextFormat
= s
;
311 es
.es_GadgetFormat
= _(MSG_OK
);
312 EasyRequest(NULL
, &es
, NULL
, NULL
);