M:Robe 100: add button definition/bitmaps to plugins and enable compilation
[Rockbox.git] / apps / plugins / disktidy.c
blobbe3407e13ed6a72ac555122e5b30c50bcbb1881a
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 David Dent
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "plugin.h"
21 PLUGIN_HEADER
22 static struct plugin_api* rb;
24 /* function return values */
25 enum tidy_return
27 TIDY_RETURN_OK = 0,
28 TIDY_RETURN_ERROR = 1,
29 TIDY_RETURN_USB = 2,
30 TIDY_RETURN_ABORT = 3,
33 /* Which systems junk are we removing */
34 enum tidy_system
36 TIDY_MAC = 0,
37 TIDY_WIN = 1,
38 TIDY_BOTH = 2,
41 /* variable button definitions */
42 #if CONFIG_KEYPAD == PLAYER_PAD
43 #define TIDY_STOP BUTTON_STOP
45 #elif CONFIG_KEYPAD == RECORDER_PAD
46 #define TIDY_STOP BUTTON_OFF
48 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
49 #define TIDY_STOP BUTTON_OFF
51 #elif CONFIG_KEYPAD == ONDIO_PAD
52 #define TIDY_STOP BUTTON_OFF
54 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
55 (CONFIG_KEYPAD == IRIVER_H300_PAD)
56 #define TIDY_STOP BUTTON_OFF
58 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
59 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
60 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
61 #define TIDY_STOP BUTTON_MENU
63 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
64 #define TIDY_STOP BUTTON_POWER
66 #elif CONFIG_KEYPAD == GIGABEAT_PAD
67 #define TIDY_STOP BUTTON_POWER
69 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
70 (CONFIG_KEYPAD == SANSA_C200_PAD)
71 #define TIDY_STOP BUTTON_POWER
73 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
74 #define TIDY_STOP BUTTON_POWER
76 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
77 #define TIDY_STOP BUTTON_BACK
79 #elif CONFIG_KEYPAD == MROBE100_PAD
80 #define TIDY_STOP BUTTON_POWER
82 #else
83 #error No keymap defined!
84 #endif
87 void tidy_lcd_status(const char *name, int *removed)
89 char text[24]; /* "Cleaned up nnnnn items" */
91 /* display status text */
92 rb->lcd_clear_display();
93 rb->lcd_puts(0, 0, "Working ...");
94 rb->lcd_puts(0, 1, name);
95 rb->snprintf(text, 24, "Cleaned up %d items", *removed);
96 #ifdef HAVE_LCD_BITMAP
97 rb->lcd_puts(0, 2, text);
98 #endif
99 rb->lcd_update();
102 void tidy_get_absolute_path(struct dirent *entry, char *fullname,
103 const char* name)
105 /* gets absolute path using dirent and name */
106 rb->strcpy(fullname, name);
107 if (rb->strlen(name) > 1)
109 rb->strcat(fullname, "/");
111 rb->strcat(fullname, entry->d_name);
114 enum tidy_return tidy_removedir(const char *name, int *removed)
116 /* delete directory */
117 struct dirent *entry;
118 enum tidy_return status = TIDY_RETURN_OK;
119 int button;
120 DIR *dir;
121 char fullname[MAX_PATH];
123 /* display status text */
124 tidy_lcd_status(name, removed);
126 rb->yield();
128 dir = rb->opendir(name);
129 if (dir)
131 while((status == TIDY_RETURN_OK) && ((entry = rb->readdir(dir)) != 0))
132 /* walk directory */
134 /* check for user input and usb connect */
135 button = rb->button_get(false);
136 if (button == TIDY_STOP)
138 rb->closedir(dir);
139 return TIDY_RETURN_ABORT;
141 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
143 rb->closedir(dir);
144 return TIDY_RETURN_USB;
147 rb->yield();
149 /* get absolute path */
150 tidy_get_absolute_path(entry, fullname, name);
152 if (entry->attribute & ATTR_DIRECTORY)
154 /* dir ignore "." and ".." */
155 if ((rb->strcmp(entry->d_name, ".") != 0) && \
156 (rb->strcmp(entry->d_name, "..") != 0))
158 tidy_removedir(fullname, removed);
161 else
163 /* file */
164 *removed += 1;
165 rb->remove(fullname);
168 rb->closedir(dir);
169 /* rmdir */
170 *removed += 1;
171 rb->rmdir(name);
173 else
175 status = TIDY_RETURN_ERROR;
177 return status;
180 enum tidy_return tidy_clean(const char *name, int *removed, \
181 enum tidy_system system)
183 /* deletes junk files and dirs left by system */
184 struct dirent *entry;
185 enum tidy_return status = TIDY_RETURN_OK;
186 int button;
187 int del; /* has the item been deleted */
188 DIR *dir;
189 char fullname[MAX_PATH];
191 /* display status text */
192 tidy_lcd_status(name, removed);
194 rb->yield();
196 dir = rb->opendir(name);
197 if (dir)
199 while((status == TIDY_RETURN_OK) && ((entry = rb->readdir(dir)) != 0))
200 /* walk directory */
202 /* check for user input and usb connect */
203 button = rb->button_get(false);
204 if (button == TIDY_STOP)
206 rb->closedir(dir);
207 return TIDY_RETURN_ABORT;
209 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
211 rb->closedir(dir);
212 return TIDY_RETURN_USB;
215 rb->yield();
217 if (entry->attribute & ATTR_DIRECTORY)
219 /* directory ignore "." and ".." */
220 if ((rb->strcmp(entry->d_name, ".") != 0) && \
221 (rb->strcmp(entry->d_name, "..") != 0))
223 del = 0;
225 /* get absolute path */
226 tidy_get_absolute_path(entry, fullname, name);
228 /* check if we are in root directory "/" */
229 if (rb->strcmp(name, "/") == 0)
231 if ((system == TIDY_MAC) || (system == TIDY_BOTH))
233 /* mac directories */
234 if (rb->strcmp(entry->d_name, ".Trashes") == 0)
236 /* delete dir */
237 tidy_removedir(fullname, removed);
238 del = 1;
242 if (del == 0)
244 if ((system == TIDY_WIN) || (system == TIDY_BOTH))
246 /* windows directories */
247 if (rb->strcmp(entry->d_name, "Recycled") == 0 \
248 || rb->strcmp(entry->d_name, "System Volume Information") == 0)
250 /* delete dir */
251 tidy_removedir(fullname, removed);
252 del = 1;
258 if (del == 0)
260 /* dir not deleted so clean it */
261 status = tidy_clean(fullname, removed, system);
265 else
267 /* file */
268 del = 0;
270 if ((system == TIDY_MAC) || (system == TIDY_BOTH))
272 /* remove mac files */
273 if ((rb->strcmp(entry->d_name, ".DS_Store") == 0) || \
274 (rb->strncmp(entry->d_name, "._", 2) == 0))
276 *removed += 1; /* increment removed files counter */
278 /* get absolute path */
279 char fullname[MAX_PATH];
280 tidy_get_absolute_path(entry, fullname, name);
282 /* delete file */
283 rb->remove(fullname);
284 del = 1;
288 if (del == 0)
290 if ((system == TIDY_WIN) || (system == TIDY_BOTH))
292 /* remove windows files*/
293 if ((rb->strcmp(entry->d_name, "Thumbs.db") == 0))
295 *removed += 1; /* increment removed files counter */
297 /* get absolute path */
298 char fullname[MAX_PATH];
299 tidy_get_absolute_path(entry, fullname, name);
301 /* delete file */
302 rb->remove(fullname);
303 del = 1;
309 rb->closedir(dir);
310 return status;
312 else
314 return TIDY_RETURN_ERROR;
318 enum plugin_status tidy_do(enum tidy_system system)
320 /* clean disk and display num of items removed */
321 int removed = 0;
322 enum tidy_return status;
323 char text[24]; /* "Cleaned up nnnnn items" */
325 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
326 rb->cpu_boost(true);
327 #endif
329 status = tidy_clean("/", &removed, system);
331 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
332 rb->cpu_boost(false);
333 #endif
335 if ((status == TIDY_RETURN_OK) || (status == TIDY_RETURN_ABORT))
337 rb->lcd_clear_display();
338 rb->snprintf(text, 24, "Cleaned up %d items", removed);
339 if (status == TIDY_RETURN_ABORT)
341 rb->splash(HZ, "User aborted");
342 rb->lcd_clear_display();
344 rb->splash(HZ*2, text);
346 return status;
349 int tidy_lcd_menu(void)
351 int selection, ret = 2;
352 bool menu_quit = false;
354 MENUITEM_STRINGLIST(menu,"Disktidy Menu",NULL,"Start Cleaning",
355 "Files to Clean","Quit");
357 static const struct opt_items system_option[3] =
359 { "Mac", -1 },
360 { "Windows", -1 },
361 { "Both", -1 }
364 while (!menu_quit)
366 switch(rb->do_menu(&menu, &selection))
369 case 0:
370 menu_quit = true; /* start cleaning */
371 break;
373 case 1:
374 rb->set_option("Files to Clean", &ret, INT, system_option, 3, NULL);
375 break;
377 default:
378 ret = 99; /* exit plugin */
379 menu_quit = true;
380 break;
383 return ret;
386 /* this is the plugin entry point */
387 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
389 enum tidy_system system = TIDY_BOTH;
390 enum tidy_return status;
392 (void)parameter;
394 rb = api;
396 switch(tidy_lcd_menu())
398 case 0:
399 system = TIDY_MAC;
400 break;
401 case 1:
402 system = TIDY_WIN;
403 break;
404 case 2:
405 system = TIDY_BOTH;
406 break;
407 case 99:
408 return PLUGIN_OK;
409 default:
410 system = TIDY_BOTH;
413 while (true)
415 status = tidy_do(system);
417 switch (status)
419 case TIDY_RETURN_OK:
420 return PLUGIN_OK;
421 case TIDY_RETURN_ERROR:
422 return PLUGIN_ERROR;
423 case TIDY_RETURN_USB:
424 return PLUGIN_USB_CONNECTED;
425 case TIDY_RETURN_ABORT:
426 return PLUGIN_OK;
430 if (rb->default_event_handler(rb->button_get(false)) == SYS_USB_CONNECTED)
431 return PLUGIN_USB_CONNECTED;
433 rb->yield();
436 return PLUGIN_OK;