revert between 56095 -> 55830 in arch
[AROS.git] / workbench / system / Wanderer / Tools / WBNewDrawer / main.c
blob70d0c497c8fb17bbc1c209627c23304fe0afaa7c
1 /*
2 Copyright © 2006-2011, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #define MUIMASTER_YES_INLINE_STDARG
8 //#define DEBUG 1
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>
19 #include <dos/dos.h>
20 #include <libraries/mui.h>
21 #include <workbench/startup.h>
23 #include "locale.h"
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
29 char versionstring[] = "$VER: WBNewDrawer 0.7 (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 doNewDrawer(void);
35 static void MakeGUI(void);
36 static CONST_STRPTR SelectDefaultName(STRPTR basename);
38 static Object *app, *window, *bt_ok, *bt_cancel, *cm_icons, *str_name;
39 static struct Hook bt_ok_hook;
40 static BPTR dirlock = (BPTR)-1;
41 static BPTR oldlock = (BPTR)-1;
42 static STRPTR illegal_chars = "/:";
44 int main(int argc, char **argv)
46 struct WBStartup *startup;
48 if (argc != 0)
50 PutStr(_(MSG_WB_ONLY));
51 Cleanup(NULL);
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;
61 if (dirlock == BNULL)
62 Cleanup(_(MSG_INVALID_LOCK));
64 oldlock = CurrentDir(dirlock);
65 MakeGUI();
67 STRPTR fullname = AllocateNameFromLock(dirlock);
68 UpdateWorkbenchObject(fullname, WBDRAWER, TAG_DONE);
69 FreeVec(fullname);
71 Cleanup(NULL);
72 return RETURN_OK;
76 static void MakeGUI(void)
78 CONST_STRPTR defname = SelectDefaultName("");
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)),
98 End),
99 Child, (IPTR) (ColGroup(2),
100 Child, (IPTR) Label2(__(MSG_NAME)),
101 Child, (IPTR)(str_name = (Object *)StringObject,
102 MUIA_CycleChain, 1,
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,
108 End),
109 Child, (IPTR) Label2(__(MSG_ICON)),
110 Child, (IPTR)(HGroup,
111 Child, (IPTR) (cm_icons = MUI_MakeObject(MUIO_Checkmark, NULL)),
112 Child, (IPTR) HVSpace,
113 End),
114 End),
115 Child, (IPTR) (RectangleObject,
116 MUIA_Rectangle_HBar, TRUE,
117 MUIA_FixHeight, 2,
118 End),
119 Child, (IPTR) (HGroup,
120 Child, (IPTR) (bt_ok = ImageButton(__(MSG_OK), "THEME:Images/Gadgets/OK")),
121 Child, (IPTR) (bt_cancel = ImageButton(__(MSG_CANCEL),"THEME:Images/Gadgets/Cancel")),
122 End),
123 End),
124 End),
125 End;
126 FreeVec((APTR) defname);
128 if (!app)
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(str_name, MUIA_CycleChain, 1);
140 set(window, MUIA_Window_Open, TRUE);
141 set(window, MUIA_Window_ActiveObject, str_name);
142 set(window, MUIA_Window_DefaultObject, bt_ok);
143 DoMethod(app, MUIM_Application_Execute);
147 static void bt_ok_hook_function(void)
149 if (doNewDrawer())
151 DoMethod(app, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
153 else
155 set(window, MUIA_Window_Activate, TRUE);
156 set(window, MUIA_Window_ActiveObject, str_name);
161 static BOOL doNewDrawer(void)
163 BOOL retval = FALSE;
164 BOOL icon = XGET(cm_icons, MUIA_Selected);
165 STRPTR name = (STRPTR) XGET(str_name, MUIA_String_Contents);
166 BPTR test;
167 struct DiskObject *dob = NULL;
168 D(bug("WBNewDrawer name %s icon %d\n", name, icon));
170 // No name specified
171 if(strlen(name) == 0)
173 return FALSE;
176 if (strpbrk(name, illegal_chars))
178 MUI_Request(app, window, 0, _(MSG_ERROR_TITLE), _(MSG_OK), _(MSG_ILLEGAL_CHARS), name);
179 goto end;
182 test = Lock(name, ACCESS_READ);
183 if (test)
185 UnLock(test);
186 MUI_Request(app, window, 0, _(MSG_ERROR_TITLE), _(MSG_OK), _(MSG_ALREADY_EXIST), name);
187 goto end;
190 dob = GetDiskObject(name);
191 // if icon exists it must be of type WBDRAWER
192 if (dob && (dob->do_Type != WBDRAWER))
194 MUI_Request(app, window, 0, _(MSG_ERROR_TITLE), _(MSG_OK), _(MSG_WRONG_ICON_TYPE), name);
195 goto end;
198 // create drawer
199 BPTR dstLock;
200 if ((dstLock = CreateDir(name)))
201 UnLock(dstLock);
202 else
204 MUI_Request(app, window, 0, _(MSG_ERROR_TITLE), _(MSG_OK), _(MSG_CANT_CREATE), name);
205 goto end;
208 // create icon
209 // if the icon already exists and is of right type then no actions happens.
210 if (icon && (dob == NULL))
212 dob = GetDiskObjectNew(name);
213 PutDiskObject(name, dob);
216 UpdateWorkbenchObject(name, WBDRAWER, TAG_DONE);
217 retval = TRUE;
219 end:
220 FreeDiskObject(dob);
221 return retval;
226 * Try to find an unique file name.
227 * You have to free the return value with FreeVec.
228 * */
230 static CONST_STRPTR SelectDefaultName(STRPTR basename)
232 if (basename == NULL)
233 basename = "Rename_Me";
235 BPTR test;
236 LONG number = 0;
237 STRPTR buffer = AllocVec(strlen(basename) + 3, MEMF_ANY);
238 if (!buffer)
239 return 0;
241 if(strlen(basename))
245 if (number == 0)
246 strcpy(buffer, basename);
247 else
248 sprintf(buffer,"%s_%d", basename, (int)number);
250 test = Lock(buffer, ACCESS_READ);
251 UnLock(test);
252 number++;
253 } while ((number < 10) && (test != BNULL));
255 else
257 // Empty buffer by default
258 sprintf(buffer,"%s", basename);
260 return buffer;
264 static STRPTR AllocateNameFromLock(BPTR lock)
266 ULONG length = 512;
267 STRPTR buffer = NULL;
268 BOOL done = FALSE;
270 while (!done)
272 FreeVec(buffer);
274 buffer = AllocVec(length, MEMF_ANY);
275 if (buffer != NULL)
277 if (NameFromLock(lock, buffer, length))
279 done = TRUE;
280 break;
282 else
284 if (IoErr() == ERROR_LINE_TOO_LONG)
286 length += 512;
287 continue;
289 else
291 break;
295 else
297 SetIoErr(ERROR_NO_FREE_STORE);
298 break;
302 if (done)
304 return buffer;
306 else
308 FreeVec(buffer);
309 return NULL;
314 static void Cleanup(STRPTR s)
316 MUI_DisposeObject(app);
318 if (oldlock != (BPTR)-1)
319 CurrentDir(oldlock);
321 if (s)
323 if (IntuitionBase)
325 struct EasyStruct es;
326 es.es_StructSize = sizeof(struct EasyStruct);
327 es.es_Flags = 0;
328 es.es_Title = _(MSG_ERROR_TITLE);
329 es.es_TextFormat = s;
330 es.es_GadgetFormat = _(MSG_OK);
331 EasyRequest(NULL, &es, NULL, NULL);
333 else
335 PutStr(s);
337 exit(RETURN_ERROR);
339 exit(RETURN_OK);