fix red and remove tabs
[kugel-rb.git] / apps / plugins / disktidy.c
blobc06afeac704bacccd4b0052c037be4a1c3cad1ac
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 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
83 #define TIDY_STOP BUTTON_RC_REC
85 #elif CONFIG_KEYPAD == COWOND2_PAD
86 #define TIDY_STOP BUTTON_POWER
88 #else
89 #error No keymap defined!
90 #endif
93 void tidy_lcd_status(const char *name, int *removed)
95 char text[24]; /* "Cleaned up nnnnn items" */
97 /* display status text */
98 rb->lcd_clear_display();
99 rb->lcd_puts(0, 0, "Working ...");
100 rb->lcd_puts(0, 1, name);
101 rb->snprintf(text, 24, "Cleaned up %d items", *removed);
102 #ifdef HAVE_LCD_BITMAP
103 rb->lcd_puts(0, 2, text);
104 #endif
105 rb->lcd_update();
108 void tidy_get_absolute_path(struct dirent *entry, char *fullname,
109 const char* name)
111 /* gets absolute path using dirent and name */
112 rb->strcpy(fullname, name);
113 if (rb->strlen(name) > 1)
115 rb->strcat(fullname, "/");
117 rb->strcat(fullname, entry->d_name);
120 enum tidy_return tidy_removedir(const char *name, int *removed)
122 /* delete directory */
123 struct dirent *entry;
124 enum tidy_return status = TIDY_RETURN_OK;
125 int button;
126 DIR *dir;
127 char fullname[MAX_PATH];
129 /* display status text */
130 tidy_lcd_status(name, removed);
132 rb->yield();
134 dir = rb->opendir(name);
135 if (dir)
137 while((status == TIDY_RETURN_OK) && ((entry = rb->readdir(dir)) != 0))
138 /* walk directory */
140 /* check for user input and usb connect */
141 button = rb->button_get(false);
142 if (button == TIDY_STOP)
144 rb->closedir(dir);
145 return TIDY_RETURN_ABORT;
147 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
149 rb->closedir(dir);
150 return TIDY_RETURN_USB;
153 rb->yield();
155 /* get absolute path */
156 tidy_get_absolute_path(entry, fullname, name);
158 if (entry->attribute & ATTR_DIRECTORY)
160 /* dir ignore "." and ".." */
161 if ((rb->strcmp(entry->d_name, ".") != 0) && \
162 (rb->strcmp(entry->d_name, "..") != 0))
164 tidy_removedir(fullname, removed);
167 else
169 /* file */
170 *removed += 1;
171 rb->remove(fullname);
174 rb->closedir(dir);
175 /* rmdir */
176 *removed += 1;
177 rb->rmdir(name);
179 else
181 status = TIDY_RETURN_ERROR;
183 return status;
186 enum tidy_return tidy_clean(const char *name, int *removed, \
187 enum tidy_system system)
189 /* deletes junk files and dirs left by system */
190 struct dirent *entry;
191 enum tidy_return status = TIDY_RETURN_OK;
192 int button;
193 int del; /* has the item been deleted */
194 DIR *dir;
195 char fullname[MAX_PATH];
197 /* display status text */
198 tidy_lcd_status(name, removed);
200 rb->yield();
202 dir = rb->opendir(name);
203 if (dir)
205 while((status == TIDY_RETURN_OK) && ((entry = rb->readdir(dir)) != 0))
206 /* walk directory */
208 /* check for user input and usb connect */
209 button = rb->button_get(false);
210 if (button == TIDY_STOP)
212 rb->closedir(dir);
213 return TIDY_RETURN_ABORT;
215 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
217 rb->closedir(dir);
218 return TIDY_RETURN_USB;
221 rb->yield();
223 if (entry->attribute & ATTR_DIRECTORY)
225 /* directory ignore "." and ".." */
226 if ((rb->strcmp(entry->d_name, ".") != 0) && \
227 (rb->strcmp(entry->d_name, "..") != 0))
229 del = 0;
231 /* get absolute path */
232 tidy_get_absolute_path(entry, fullname, name);
234 /* check if we are in root directory "/" */
235 if (rb->strcmp(name, "/") == 0)
237 if ((system == TIDY_MAC) || (system == TIDY_BOTH))
239 /* mac directories */
240 if (rb->strcmp(entry->d_name, ".Trashes") == 0)
242 /* delete dir */
243 tidy_removedir(fullname, removed);
244 del = 1;
248 if (del == 0)
250 if ((system == TIDY_WIN) || (system == TIDY_BOTH))
252 /* windows directories */
253 if (rb->strcmp(entry->d_name, "Recycled") == 0 \
254 || rb->strcmp(entry->d_name, "System Volume Information") == 0)
256 /* delete dir */
257 tidy_removedir(fullname, removed);
258 del = 1;
264 if (del == 0)
266 /* dir not deleted so clean it */
267 status = tidy_clean(fullname, removed, system);
271 else
273 /* file */
274 del = 0;
276 if ((system == TIDY_MAC) || (system == TIDY_BOTH))
278 /* remove mac files */
279 if ((rb->strcmp(entry->d_name, ".DS_Store") == 0) || \
280 (rb->strncmp(entry->d_name, "._", 2) == 0))
282 *removed += 1; /* increment removed files counter */
284 /* get absolute path */
285 char fullname[MAX_PATH];
286 tidy_get_absolute_path(entry, fullname, name);
288 /* delete file */
289 rb->remove(fullname);
290 del = 1;
294 if (del == 0)
296 if ((system == TIDY_WIN) || (system == TIDY_BOTH))
298 /* remove windows files*/
299 if ((rb->strcmp(entry->d_name, "Thumbs.db") == 0))
301 *removed += 1; /* increment removed files counter */
303 /* get absolute path */
304 char fullname[MAX_PATH];
305 tidy_get_absolute_path(entry, fullname, name);
307 /* delete file */
308 rb->remove(fullname);
309 del = 1;
315 rb->closedir(dir);
316 return status;
318 else
320 return TIDY_RETURN_ERROR;
324 enum plugin_status tidy_do(enum tidy_system system)
326 /* clean disk and display num of items removed */
327 int removed = 0;
328 enum tidy_return status;
329 char text[24]; /* "Cleaned up nnnnn items" */
331 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
332 rb->cpu_boost(true);
333 #endif
335 status = tidy_clean("/", &removed, system);
337 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
338 rb->cpu_boost(false);
339 #endif
341 if ((status == TIDY_RETURN_OK) || (status == TIDY_RETURN_ABORT))
343 rb->lcd_clear_display();
344 rb->snprintf(text, 24, "Cleaned up %d items", removed);
345 if (status == TIDY_RETURN_ABORT)
347 rb->splash(HZ, "User aborted");
348 rb->lcd_clear_display();
350 rb->splash(HZ*2, text);
352 return status;
355 int tidy_lcd_menu(void)
357 int selection, ret = 2;
358 bool menu_quit = false;
360 MENUITEM_STRINGLIST(menu,"Disktidy Menu",NULL,"Start Cleaning",
361 "Files to Clean","Quit");
363 static const struct opt_items system_option[3] =
365 { "Mac", -1 },
366 { "Windows", -1 },
367 { "Both", -1 }
370 while (!menu_quit)
372 switch(rb->do_menu(&menu, &selection, NULL, false))
375 case 0:
376 menu_quit = true; /* start cleaning */
377 break;
379 case 1:
380 rb->set_option("Files to Clean", &ret, INT, system_option, 3, NULL);
381 break;
383 default:
384 ret = 99; /* exit plugin */
385 menu_quit = true;
386 break;
389 return ret;
392 /* this is the plugin entry point */
393 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
395 enum tidy_system system = TIDY_BOTH;
396 enum tidy_return status;
398 (void)parameter;
400 rb = api;
402 switch(tidy_lcd_menu())
404 case 0:
405 system = TIDY_MAC;
406 break;
407 case 1:
408 system = TIDY_WIN;
409 break;
410 case 2:
411 system = TIDY_BOTH;
412 break;
413 case 99:
414 return PLUGIN_OK;
415 default:
416 system = TIDY_BOTH;
419 while (true)
421 status = tidy_do(system);
423 switch (status)
425 case TIDY_RETURN_OK:
426 return PLUGIN_OK;
427 case TIDY_RETURN_ERROR:
428 return PLUGIN_ERROR;
429 case TIDY_RETURN_USB:
430 return PLUGIN_USB_CONNECTED;
431 case TIDY_RETURN_ABORT:
432 return PLUGIN_OK;
436 if (rb->default_event_handler(rb->button_get(false)) == SYS_USB_CONNECTED)
437 return PLUGIN_USB_CONNECTED;
439 rb->yield();
442 return PLUGIN_OK;