Tabs to spaces, more consistent formatting.
[AROS.git] / workbench / libs / muimaster / classes / volumelist.c
blob1477f7846bda44014ebb2a39f7f2385542ce9c62
1 /*
2 Copyright © 2002-2011, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #define MUIMASTER_YES_INLINE_STDARG
8 #include <exec/memory.h>
9 #include <clib/alib_protos.h>
10 #include <proto/exec.h>
11 #include <proto/dos.h>
12 #include <proto/intuition.h>
13 #include <proto/utility.h>
14 #include <proto/muimaster.h>
16 #include <string.h>
17 #include <stdio.h>
19 #include "mui.h"
20 #include "muimaster_intern.h"
21 #include "support.h"
22 #include "support_classes.h"
23 #include "volumelist_private.h"
25 extern struct Library *MUIMasterBase;
28 static void printSize(STRPTR string, size_t bufsize, UQUAD size)
30 char unit = 'B';
32 if (size >= 9999999999ULL)
34 size = size >> 30;
35 unit = 'G';
37 else if (size >= 9999999UL)
39 size = size >> 20;
40 unit = 'M';
42 else if (size > 9999)
44 size = size >> 10;
45 unit = 'K';
48 snprintf(string, bufsize, "%u%c", (unsigned int)size, unit);
49 string[bufsize - 1] = '\0';
52 AROS_UFH3S(APTR, construct_func,
53 AROS_UFHA(struct Hook *, hook, A0),
54 AROS_UFHA(APTR, pool, A2),
55 AROS_UFHA(struct Volumelist_Entry *, entry, A1))
57 AROS_USERFUNC_INIT
59 struct Volumelist_Entry *new;
61 if ((new = AllocPooled(pool, sizeof(*new))))
63 *new = *entry;
65 return new;
67 AROS_USERFUNC_EXIT
70 AROS_UFH3S(void, destruct_func,
71 AROS_UFHA(struct Hook *, hook, A0),
72 AROS_UFHA(APTR, pool, A2),
73 AROS_UFHA(struct Volumelist_Entry *, entry, A1))
75 AROS_USERFUNC_INIT
77 FreePooled(pool, entry, sizeof(struct Volumelist_Entry));
79 AROS_USERFUNC_EXIT
82 AROS_UFH3S(LONG, display_func,
83 AROS_UFHA(struct Hook *, hook, A0),
84 AROS_UFHA(char **, array, A2),
85 AROS_UFHA(struct Volumelist_Entry *, entry, A1))
87 AROS_USERFUNC_INIT
89 /* MUI: logo | devicename | %-used | bytes free | bytes used */
91 if (entry)
93 if (entry->type == DLT_DEVICE)
95 *array++ = "\33I[6:24]";
97 else if (entry->type == DLT_VOLUME)
99 *array++ = "\33I[6:26]";
101 else
103 *array++ = "\33I[6:29]";
106 *array++ = entry->name;
107 *array++ = entry->full;
108 *array++ = entry->free;
109 *array = entry->used;
111 else
113 *array++ = "";
114 *array++ = "Name";
115 *array++ = "full";
116 *array++ = "free";
117 *array = "used";
120 return 0;
122 AROS_USERFUNC_EXIT
126 IPTR Volumelist__OM_NEW(struct IClass *cl, Object *obj,
127 struct opSet *msg)
129 struct DosList *dl, *actdl;
131 STRPTR format =
132 (STRPTR) GetTagData(MUIA_List_Format, 0, msg->ops_AttrList);
134 obj = (Object *)DoSuperNewTags
136 cl, obj, NULL,
137 MUIA_List_Format, format
138 ? TAG_IGNORE
139 : (IPTR)",,P=\33r,P=\33r,P=\33r",
140 TAG_MORE, (IPTR) msg->ops_AttrList
143 if (obj)
145 struct Volumelist_DATA *data = INST_DATA(cl, obj);
147 data->construct_hook.h_Entry = (HOOKFUNC) construct_func;
148 data->destruct_hook.h_Entry = (HOOKFUNC) destruct_func;
149 data->display_hook.h_Entry = (HOOKFUNC) display_func;
151 SetAttrs(obj, MUIA_List_ConstructHook,
152 (IPTR) & data->construct_hook, MUIA_List_DestructHook,
153 (IPTR) & data->destruct_hook, MUIA_List_DisplayHook,
154 (IPTR) & data->display_hook, TAG_DONE);
156 dl = LockDosList(LDF_READ | LDF_VOLUMES | LDF_ASSIGNS |
157 LDF_DEVICES);
159 actdl = dl;
160 while ((actdl = NextDosEntry(actdl, LDF_DEVICES)))
162 struct Volumelist_Entry entry;
164 entry.full[0] = '\0';
165 entry.free[0] = '\0';
166 entry.used[0] = '\0';
168 entry.type = DLT_DEVICE;
170 strncpy(entry.name, AROS_BSTR_ADDR(actdl->dol_Name),
171 sizeof(entry.name));
172 entry.name[sizeof(entry.name) - 2] = '\0';
173 strcat(entry.name, ":");
175 DoMethod(obj, MUIM_List_InsertSingle, (IPTR) & entry,
176 MUIV_List_Insert_Bottom);
179 actdl = dl;
180 while ((actdl = NextDosEntry(actdl, LDF_VOLUMES)))
182 struct Volumelist_Entry entry;
183 struct InfoData diskinfo;
184 BPTR lock;
185 UQUAD free;
186 UQUAD used;
188 entry.full[0] = '\0';
189 entry.free[0] = '\0';
190 entry.used[0] = '\0';
192 entry.type = DLT_VOLUME;
194 strncpy(entry.name, AROS_BSTR_ADDR(actdl->dol_Name),
195 sizeof(entry.name));
196 entry.name[sizeof(entry.name) - 2] = '\0';
197 strcat(entry.name, ":");
199 if ((lock = Lock(entry.name, SHARED_LOCK)) != BNULL)
201 if (Info(lock, &diskinfo) != DOSFALSE)
203 snprintf
204 (entry.full,
205 sizeof(entry.full),
206 "%ld%%",
207 (long)(100 * diskinfo.id_NumBlocksUsed /
208 diskinfo.id_NumBlocks));
209 entry.full[sizeof(entry.full) - 1] = '\0';
211 used =
212 (UQUAD) diskinfo.id_NumBlocksUsed *
213 diskinfo.id_BytesPerBlock;
214 free =
215 (UQUAD) diskinfo.id_NumBlocks *
216 diskinfo.id_BytesPerBlock - used;
217 printSize(entry.free, sizeof entry.free, free);
218 printSize(entry.used, sizeof entry.used, used);
220 UnLock(lock);
223 DoMethod(obj, MUIM_List_InsertSingle, (IPTR) & entry,
224 MUIV_List_Insert_Bottom);
227 actdl = dl;
228 while ((actdl = NextDosEntry(actdl, LDF_ASSIGNS)))
230 struct Volumelist_Entry entry;
232 entry.full[0] = '\0';
233 entry.free[0] = '\0';
234 entry.used[0] = '\0';
236 entry.type = DLT_DIRECTORY;
238 strncpy(entry.name, AROS_BSTR_ADDR(actdl->dol_Name),
239 sizeof(entry.name));
240 entry.name[sizeof(entry.name) - 2] = '\0';
241 strcat(entry.name, ":");
243 DoMethod(obj, MUIM_List_InsertSingle, (IPTR) & entry,
244 MUIV_List_Insert_Bottom);
247 UnLockDosList(LDF_READ | LDF_VOLUMES | LDF_ASSIGNS | LDF_DEVICES);
250 return (IPTR) obj;
254 #if ZUNE_BUILTIN_VOLUMELIST
255 BOOPSI_DISPATCHER(IPTR, Volumelist_Dispatcher, cl, obj, msg)
257 switch (msg->MethodID)
259 case OM_NEW:
260 return Volumelist__OM_NEW(cl, obj, (struct opSet *)msg);
262 default:
263 return DoSuperMethodA(cl, obj, msg);
266 BOOPSI_DISPATCHER_END
268 const struct __MUIBuiltinClass _MUI_Volumelist_desc =
270 MUIC_Volumelist,
271 MUIC_List,
272 sizeof(struct Volumelist_DATA),
273 (void *) Volumelist_Dispatcher
275 #endif /* ZUNE_BUILTIN_VOLUMELIST */