1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 David Dent
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
24 static const struct plugin_api
* rb
;
25 MEM_FUNCTION_WRAPPERS(rb
)
27 /* function return values */
31 TIDY_RETURN_ERROR
= 1,
33 TIDY_RETURN_ABORT
= 3,
41 } tidy_types
[MAX_TYPES
];
43 bool tidy_loaded_and_changed
= false;
45 #define DEFAULT_FILES PLUGIN_APPS_DIR "/disktidy.config"
46 #define CUSTOM_FILES PLUGIN_APPS_DIR "/disktidy_custom.config"
47 void add_item(const char* name
, int index
)
49 rb
->strcpy(tidy_types
[index
].filestring
, name
);
50 if (name
[rb
->strlen(name
)-1] == '/')
52 tidy_types
[index
].directory
= true;
53 tidy_types
[index
].filestring
[rb
->strlen(name
)-1] = '\0';
56 tidy_types
[index
].directory
= false;
58 static int find_file_string(const char *file
, char *last_group
)
61 int i
= 0, idx_last_group
= -1;
63 rb
->strcpy(temp
, file
);
64 if (temp
[rb
->strlen(temp
)-1] == '/')
67 temp
[rb
->strlen(temp
)-1] = '\0';
69 while (i
<tidy_type_count
)
71 if (!rb
->strcmp(tidy_types
[i
].filestring
, temp
) &&
72 folder
== tidy_types
[i
].directory
)
74 else if (!rb
->strcmp(tidy_types
[i
].filestring
, last_group
))
78 /* not found, so insert it into its group */
79 if (file
[0] != '<' && idx_last_group
!= -1)
81 for (i
=idx_last_group
; i
<tidy_type_count
; i
++)
83 if (tidy_types
[i
].filestring
[0] == '<')
89 /* shift items up one */
90 for (i
=tidy_type_count
;i
>idx_last_group
;i
--)
92 rb
->strcpy(tidy_types
[i
].filestring
, tidy_types
[i
-1].filestring
);
93 tidy_types
[i
].directory
= tidy_types
[i
-1].directory
;
94 tidy_types
[i
].remove
= tidy_types
[i
-1].remove
;
97 add_item(file
, idx_last_group
+1);
98 return idx_last_group
+1;
103 bool tidy_load_file(const char* file
)
105 int fd
= rb
->open(file
, O_RDONLY
), i
;
106 char buf
[MAX_PATH
], *str
, *remove
;
107 char last_group
[MAX_PATH
] = "";
111 while ((tidy_type_count
< MAX_TYPES
) &&
112 rb
->read_line(fd
, buf
, MAX_PATH
))
114 if (rb
->settings_parseline(buf
, &str
, &remove
))
116 i
= find_file_string(str
, last_group
);
117 new = (i
>= tidy_type_count
);
118 if (!rb
->strcmp(remove
, "yes"))
119 tidy_types
[i
].remove
= true;
120 else tidy_types
[i
].remove
= false;
128 rb
->strcpy(last_group
, str
);
135 bool tidy_remove_item(char *item
, int attr
)
139 bool ret
= false, rem
= false;
140 for (i
=0; ret
== false && i
< tidy_type_count
; i
++)
142 file
= tidy_types
[i
].filestring
;
143 if (file
[rb
->strlen(file
)-1] == '*')
145 if (!rb
->strncmp(file
, item
, rb
->strlen(file
)-1))
148 else if (!rb
->strcmp(file
, item
))
152 if (!tidy_types
[i
].remove
)
154 if (attr
&ATTR_DIRECTORY
)
155 ret
= tidy_types
[i
].directory
;
162 void tidy_lcd_status(const char *name
, int *removed
)
164 char text
[24]; /* "Cleaned up nnnnn items" */
166 /* display status text */
167 rb
->lcd_clear_display();
168 rb
->lcd_puts(0, 0, "Working ...");
169 rb
->lcd_puts(0, 1, name
);
170 rb
->snprintf(text
, 24, "Cleaned up %d items", *removed
);
171 #ifdef HAVE_LCD_BITMAP
172 rb
->lcd_puts(0, 2, text
);
177 void tidy_get_absolute_path(struct dirent
*entry
, char *fullname
,
180 /* gets absolute path using dirent and name */
181 rb
->strcpy(fullname
, name
);
182 if (rb
->strlen(name
) > 1)
184 rb
->strcat(fullname
, "/");
186 rb
->strcat(fullname
, entry
->d_name
);
189 enum tidy_return
tidy_removedir(const char *name
, int *removed
)
191 /* delete directory */
192 struct dirent
*entry
;
193 enum tidy_return status
= TIDY_RETURN_OK
;
196 char fullname
[MAX_PATH
];
198 /* display status text */
199 tidy_lcd_status(name
, removed
);
203 dir
= rb
->opendir(name
);
206 while((status
== TIDY_RETURN_OK
) && ((entry
= rb
->readdir(dir
)) != 0))
209 /* check for user input and usb connect */
210 button
= rb
->get_action(CONTEXT_STD
, TIMEOUT_NOBLOCK
);
211 if (button
== ACTION_STD_CANCEL
)
214 return TIDY_RETURN_ABORT
;
216 if (rb
->default_event_handler(button
) == SYS_USB_CONNECTED
)
219 return TIDY_RETURN_USB
;
224 /* get absolute path */
225 tidy_get_absolute_path(entry
, fullname
, name
);
227 if (entry
->attribute
& ATTR_DIRECTORY
)
229 /* dir ignore "." and ".." */
230 if ((rb
->strcmp(entry
->d_name
, ".") != 0) && \
231 (rb
->strcmp(entry
->d_name
, "..") != 0))
233 tidy_removedir(fullname
, removed
);
240 rb
->remove(fullname
);
250 status
= TIDY_RETURN_ERROR
;
255 enum tidy_return
tidy_clean(const char *name
, int *removed
)
257 /* deletes junk files and dirs left by system */
258 struct dirent
*entry
;
259 enum tidy_return status
= TIDY_RETURN_OK
;
261 int del
; /* has the item been deleted */
263 char fullname
[MAX_PATH
];
265 /* display status text */
266 tidy_lcd_status(name
, removed
);
270 dir
= rb
->opendir(name
);
273 while((status
== TIDY_RETURN_OK
) && ((entry
= rb
->readdir(dir
)) != 0))
276 /* check for user input and usb connect */
277 button
= rb
->get_action(CONTEXT_STD
, TIMEOUT_NOBLOCK
);
278 if (button
== ACTION_STD_CANCEL
)
281 return TIDY_RETURN_ABORT
;
283 if (rb
->default_event_handler(button
) == SYS_USB_CONNECTED
)
286 return TIDY_RETURN_USB
;
291 if (entry
->attribute
& ATTR_DIRECTORY
)
293 /* directory ignore "." and ".." */
294 if ((rb
->strcmp(entry
->d_name
, ".") != 0) && \
295 (rb
->strcmp(entry
->d_name
, "..") != 0))
299 /* get absolute path */
300 tidy_get_absolute_path(entry
, fullname
, name
);
302 if (tidy_remove_item(entry
->d_name
, entry
->attribute
))
305 tidy_removedir(fullname
, removed
);
311 /* dir not deleted so clean it */
312 status
= tidy_clean(fullname
, removed
);
320 if (tidy_remove_item(entry
->d_name
, entry
->attribute
))
322 *removed
+= 1; /* increment removed files counter */
324 /* get absolute path */
325 char fullname
[MAX_PATH
];
326 tidy_get_absolute_path(entry
, fullname
, name
);
329 rb
->remove(fullname
);
339 return TIDY_RETURN_ERROR
;
343 enum plugin_status
tidy_do(void)
345 /* clean disk and display num of items removed */
347 enum tidy_return status
;
348 char text
[24]; /* "Cleaned up nnnnn items" */
350 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
354 status
= tidy_clean("/", &removed
);
356 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
357 rb
->cpu_boost(false);
360 if ((status
== TIDY_RETURN_OK
) || (status
== TIDY_RETURN_ABORT
))
362 rb
->lcd_clear_display();
363 rb
->snprintf(text
, 24, "Cleaned up %d items", removed
);
364 if (status
== TIDY_RETURN_ABORT
)
366 rb
->splash(HZ
, "User aborted");
367 rb
->lcd_clear_display();
369 rb
->splash(HZ
*2, text
);
374 enum themable_icons
get_icon(int item
, void * data
)
377 if (tidy_types
[item
].filestring
[0] == '<') /* special type */
379 else if (tidy_types
[item
].remove
)
385 char * get_name(int selected_item
, void * data
,
386 char * buffer
, size_t buffer_len
)
389 if (tidy_types
[selected_item
].directory
)
391 rb
->snprintf(buffer
, buffer_len
, "%s/",
392 tidy_types
[selected_item
].filestring
);
395 return tidy_types
[selected_item
].filestring
;
398 int list_action_callback(int action
, struct gui_synclist
*lists
)
400 if (action
== ACTION_STD_OK
)
402 int selection
= rb
->gui_synclist_get_sel_pos(lists
);
403 if (tidy_types
[selection
].filestring
[0] == '<')
406 if (!rb
->strcmp(tidy_types
[selection
].filestring
, "< ALL >"))
408 for (i
=0; i
<tidy_type_count
; i
++)
410 if (tidy_types
[i
].filestring
[0] != '<')
411 tidy_types
[i
].remove
= true;
414 else if (!rb
->strcmp(tidy_types
[selection
].filestring
, "< NONE >"))
416 for (i
=0; i
<tidy_type_count
; i
++)
418 if (tidy_types
[i
].filestring
[0] != '<')
419 tidy_types
[i
].remove
= false;
422 else /* toggle all untill the next <> */
425 while (selection
< tidy_type_count
&&
426 tidy_types
[selection
].filestring
[0] != '<')
428 tidy_types
[selection
].remove
= !tidy_types
[selection
].remove
;
434 tidy_types
[selection
].remove
= !tidy_types
[selection
].remove
;
435 tidy_loaded_and_changed
= true;
436 return ACTION_REDRAW
;
441 int tidy_lcd_menu(void)
443 int selection
, ret
= 3;
444 bool menu_quit
= false;
446 MENUITEM_STRINGLIST(menu
,"Disktidy Menu",NULL
,"Start Cleaning",
447 "Files to Clean","Quit");
451 switch(rb
->do_menu(&menu
, &selection
, NULL
, false))
455 menu_quit
= true; /* start cleaning */
460 bool show_icons
= rb
->global_settings
->show_icons
;
461 struct simplelist_info list
;
462 rb
->global_settings
->show_icons
= true; /* force the icons so its readable */
463 rb
->simplelist_info_init(&list
, "Files to Clean", tidy_type_count
, NULL
);
464 list
.get_icon
= get_icon
;
465 list
.get_name
= get_name
;
466 list
.action_callback
= list_action_callback
;
467 rb
->simplelist_show_list(&list
);
468 rb
->global_settings
->show_icons
= show_icons
;
473 ret
= 99; /* exit plugin */
481 /* this is the plugin entry point */
482 enum plugin_status
plugin_start(const struct plugin_api
* api
, const void* parameter
)
484 enum tidy_return status
;
490 tidy_load_file(DEFAULT_FILES
);
491 tidy_load_file(CUSTOM_FILES
);
492 if (tidy_type_count
== 0)
494 rb
->splash(3*HZ
, "Missing disktidy.config file");
497 ret
= tidy_lcd_menu();
498 if (tidy_loaded_and_changed
)
500 int fd
= rb
->creat(CUSTOM_FILES
);
504 for(i
=0;i
<tidy_type_count
;i
++)
506 rb
->fdprintf(fd
, "%s%c: %s\n", tidy_types
[i
].filestring
,
507 tidy_types
[i
].directory
?'/':'\0',
508 tidy_types
[i
].remove
?"yes":"no");
523 case TIDY_RETURN_ERROR
:
525 case TIDY_RETURN_USB
:
526 return PLUGIN_USB_CONNECTED
;
527 case TIDY_RETURN_ABORT
:
532 if (rb
->default_event_handler(rb
->button_get(false)) == SYS_USB_CONNECTED
)
533 return PLUGIN_USB_CONNECTED
;