Some compiler warnings removed.
[cake.git] / workbench / system / Wanderer / Tools / WBNewDrawer / main.c
blob08e2702813f9375f915e067f524496e5a814de9b
1 /*
2 Copyright © 2006, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #define MUIMASTER_YES_INLINE_STDARG
8 #define DEBUG 0
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>
18 #include <dos/dos.h>
19 #include <libraries/mui.h>
20 #include <workbench/startup.h>
22 #include "locale.h"
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.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;
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 == NULL)
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(_(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)),
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/Prefs/Save")),
121 Child, (IPTR) (bt_cancel = ImageButton(__(MSG_CANCEL),"THEME:Images/Gadgets/Prefs/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(window, MUIA_Window_Open, TRUE);
140 DoMethod(app, MUIM_Application_Execute);
144 static void bt_ok_hook_function(void)
146 if (doNewDrawer())
148 DoMethod(app, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
153 static BOOL doNewDrawer(void)
155 BOOL retval = FALSE;
156 BOOL icon = XGET(cm_icons, MUIA_Selected);
157 STRPTR name = (STRPTR)XGET(str_name, MUIA_String_Contents);
158 BPTR test;
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);
165 goto end;
168 test = Lock(name, ACCESS_READ);
169 if (test)
171 UnLock(test);
172 MUI_Request(app, window, 0, _(MSG_ERROR_TITLE), _(MSG_OK), _(MSG_ALREADY_EXIST), name);
173 goto end;
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);
181 goto end;
184 // create drawer
185 BPTR dstLock;
186 if ((dstLock = CreateDir(name)))
187 UnLock(dstLock);
188 else
190 MUI_Request(app, window, 0, _(MSG_ERROR_TITLE), _(MSG_OK), _(MSG_CANT_CREATE), name);
191 goto end;
194 // create icon
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);
203 retval = TRUE;
205 end:
206 FreeDiskObject(dob);
207 return retval;
212 * Try to find an unique file name.
213 * You have to free the return value with FreeVec.
214 * */
216 static CONST_STRPTR SelectDefaultName(STRPTR basename)
218 if (basename == NULL)
219 basename = "Rename_Me";
221 BPTR test;
222 LONG number = 0;
223 STRPTR buffer = AllocVec(strlen(basename) + 3, MEMF_ANY);
224 if (!buffer)
225 return 0;
229 if (number == 0)
230 strcpy(buffer, basename);
231 else
232 sprintf(buffer,"%s_%d", basename, number);
234 test = Lock(buffer, ACCESS_READ);
235 UnLock(test);
236 number++;
238 while ((number < 10) && (test != NULL));
239 return buffer;
243 static STRPTR AllocateNameFromLock(BPTR lock)
245 ULONG length = 512;
246 STRPTR buffer = NULL;
247 BOOL done = FALSE;
249 while (!done)
251 FreeVec(buffer);
253 buffer = AllocVec(length, MEMF_ANY);
254 if (buffer != NULL)
256 if (NameFromLock(lock, buffer, length))
258 done = TRUE;
259 break;
261 else
263 if (IoErr() == ERROR_LINE_TOO_LONG)
265 length += 512;
266 continue;
268 else
270 break;
274 else
276 SetIoErr(ERROR_NO_FREE_STORE);
277 break;
281 if (done)
283 return buffer;
285 else
287 FreeVec(buffer);
288 return NULL;
293 static void Cleanup(STRPTR s)
295 MUI_DisposeObject(app);
297 if (oldlock != (BPTR)-1)
298 CurrentDir(oldlock);
300 if (s)
302 if (IntuitionBase)
304 struct EasyStruct es;
305 es.es_StructSize = sizeof(struct EasyStruct);
306 es.es_Flags = 0;
307 es.es_Title = _(MSG_ERROR_TITLE);
308 es.es_TextFormat = s;
309 es.es_GadgetFormat = _(MSG_OK);
310 EasyRequest(NULL, &es, NULL, NULL);
312 else
314 PutStr(s);
316 exit(RETURN_ERROR);
318 exit(RETURN_OK);