Remove the (part of the) check that is actually not needed since the 'properties...
[kugel-rb/myfork.git] / apps / tree.c
blob7ae99051f99d6b74da62adecf89f4131a4d343e1
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Daniel Stenberg
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 ****************************************************************************/
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <stdbool.h>
26 #include "applimits.h"
27 #include "dir.h"
28 #include "file.h"
29 #include "lcd.h"
30 #include "font.h"
31 #include "button.h"
32 #include "kernel.h"
33 #include "usb.h"
34 #include "tree.h"
35 #include "sprintf.h"
36 #include "audio.h"
37 #include "playlist.h"
38 #include "menu.h"
39 #include "gwps.h"
40 #include "settings.h"
41 #include "debug.h"
42 #include "storage.h"
43 #include "rolo.h"
44 #include "icons.h"
45 #include "lang.h"
46 #include "screens.h"
47 #include "keyboard.h"
48 #include "bookmark.h"
49 #include "onplay.h"
50 #include "buffer.h"
51 #include "power.h"
52 #include "action.h"
53 #include "talk.h"
54 #include "filetypes.h"
55 #include "misc.h"
56 #include "filetree.h"
57 #include "tagtree.h"
58 #ifdef HAVE_RECORDING
59 #include "recorder/recording.h"
60 #endif
61 #include "rtc.h"
62 #include "dircache.h"
63 #ifdef HAVE_TAGCACHE
64 #include "tagcache.h"
65 #endif
66 #include "yesno.h"
67 #include "gwps-common.h"
68 #include "eeprom_settings.h"
69 #include "playlist_catalog.h"
71 /* gui api */
72 #include "list.h"
73 #include "statusbar.h"
74 #include "splash.h"
75 #include "buttonbar.h"
76 #include "action.h"
77 #include "quickscreen.h"
79 #include "root_menu.h"
81 static const struct filetype *filetypes;
82 static int filetypes_count;
84 struct gui_synclist tree_lists;
86 /* I put it here because other files doesn't use it yet,
87 * but should be elsewhere since it will be used mostly everywhere */
88 #ifdef HAVE_BUTTONBAR
89 struct gui_buttonbar tree_buttonbar;
90 #endif
91 static struct tree_context tc;
93 bool boot_changed = false;
95 char lastfile[MAX_PATH];
96 static char lastdir[MAX_PATH];
97 #ifdef HAVE_TAGCACHE
98 static int lasttable, lastextra, lastfirstpos;
99 #endif
100 static int max_files = 0;
102 static bool reload_dir = false;
104 static bool start_wps = false;
105 static int curr_context = false;/* id3db or tree*/
107 static int dirbrowse(void);
108 static int ft_play_dirname(char* name);
109 static void ft_play_filename(char *dir, char *file);
110 static void say_filetype(int attr);
112 static char * tree_get_filename(int selected_item, void *data,
113 char *buffer, size_t buffer_len)
115 struct tree_context * local_tc=(struct tree_context *)data;
116 char *name;
117 int attr=0;
118 bool stripit = false;
119 #ifdef HAVE_TAGCACHE
120 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
122 if (id3db)
124 return tagtree_get_entry(&tc, selected_item)->name;
126 else
127 #endif
129 struct entry* dc = local_tc->dircache;
130 struct entry* e = &dc[selected_item];
131 name = e->name;
132 attr = e->attr;
135 if(!(attr & ATTR_DIRECTORY))
137 switch(global_settings.show_filename_ext)
139 case 0:
140 /* show file extension: off */
141 stripit = true;
142 break;
143 case 1:
144 /* show file extension: on */
145 break;
146 case 2:
147 /* show file extension: only unknown types */
148 stripit = filetype_supported(attr);
149 break;
150 case 3:
151 default:
152 /* show file extension: only when viewing all */
153 stripit = (*(local_tc->dirfilter) != SHOW_ID3DB) &&
154 (*(local_tc->dirfilter) != SHOW_ALL);
155 break;
159 if(stripit)
161 return(strip_extension(buffer, buffer_len, name));
163 return(name);
166 #ifdef HAVE_LCD_COLOR
167 static int tree_get_filecolor(int selected_item, void * data)
169 if (*tc.dirfilter == SHOW_ID3DB)
170 return -1;
171 struct tree_context * local_tc=(struct tree_context *)data;
172 struct entry* dc = local_tc->dircache;
173 struct entry* e = &dc[selected_item];
174 return filetype_get_color(e->name, e->attr);
176 #endif
178 static int tree_get_fileicon(int selected_item, void * data)
180 struct tree_context * local_tc=(struct tree_context *)data;
181 #ifdef HAVE_TAGCACHE
182 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
183 if (id3db) {
184 return tagtree_get_icon(&tc);
186 else
187 #endif
189 struct entry* dc = local_tc->dircache;
190 struct entry* e = &dc[selected_item];
191 return filetype_get_icon(e->attr);
195 static int tree_voice_cb(int selected_item, void * data)
197 struct tree_context * local_tc=(struct tree_context *)data;
198 char *name;
199 int attr=0;
200 #ifdef HAVE_TAGCACHE
201 bool id3db = *(local_tc->dirfilter) == SHOW_ID3DB;
203 if (id3db)
205 attr = tagtree_get_attr(local_tc);
206 name = tagtree_get_entry(local_tc, selected_item)->name;
208 else
209 #endif
211 struct entry* dc = local_tc->dircache;
212 struct entry* e = &dc[selected_item];
213 name = e->name;
214 attr = e->attr;
216 bool is_dir = (attr & ATTR_DIRECTORY);
217 bool did_clip = false;
218 /* First the .talk clip case */
219 if(is_dir)
221 if(global_settings.talk_dir_clip)
223 did_clip = true;
224 if(ft_play_dirname(name) <0)
225 /* failed, not existing */
226 did_clip = false;
228 } else { /* it's a file */
229 if (global_settings.talk_file_clip && (attr & FILE_ATTR_THUMBNAIL))
231 did_clip = true;
232 ft_play_filename(local_tc->currdir, name);
235 if(!did_clip)
237 /* say the number or spell if required or as a fallback */
238 switch (is_dir ? global_settings.talk_dir : global_settings.talk_file)
240 case 1: /* as numbers */
241 talk_id(is_dir ? VOICE_DIR : VOICE_FILE, false);
242 talk_number(selected_item+1 - (is_dir ? 0 : local_tc->dirsindir),
243 true);
244 if(global_settings.talk_filetype
245 && !is_dir && *local_tc->dirfilter < NUM_FILTER_MODES)
246 say_filetype(attr);
247 break;
248 case 2: /* spelled */
249 talk_shutup();
250 if(global_settings.talk_filetype)
252 if(is_dir)
253 talk_id(VOICE_DIR, true);
254 else if(*local_tc->dirfilter < NUM_FILTER_MODES)
255 say_filetype(attr);
257 talk_spell(name, true);
258 break;
261 return 0;
264 bool check_rockboxdir(void)
266 if(!dir_exists(ROCKBOX_DIR))
267 { /* No need to localise this message.
268 If .rockbox is missing, it wouldn't work anyway */
269 int i;
270 FOR_NB_SCREENS(i)
271 screens[i].clear_display();
272 splash(HZ*2, "No .rockbox directory");
273 FOR_NB_SCREENS(i)
274 screens[i].clear_display();
275 splash(HZ*2, "Installation incomplete");
276 return false;
278 return true;
281 /* do this really late in the init sequence */
282 void tree_gui_init(void)
284 check_rockboxdir();
286 strcpy(tc.currdir, "/");
288 #ifdef HAVE_LCD_CHARCELLS
289 int i;
290 FOR_NB_SCREENS(i)
291 screens[i].double_height(false);
292 #endif
293 #ifdef HAVE_BUTTONBAR
294 gui_buttonbar_init(&tree_buttonbar);
295 /* since archos only have one screen, no need to create more than that */
296 gui_buttonbar_set_display(&tree_buttonbar, &(screens[SCREEN_MAIN]) );
297 #endif
298 gui_synclist_init(&tree_lists, &tree_get_filename, &tc, false, 1, NULL);
299 gui_synclist_set_voice_callback(&tree_lists, tree_voice_cb);
300 gui_synclist_set_icon_callback(&tree_lists, &tree_get_fileicon);
301 #ifdef HAVE_LCD_COLOR
302 gui_synclist_set_color_callback(&tree_lists, &tree_get_filecolor);
303 #endif
308 struct tree_context* tree_get_context(void)
310 return &tc;
314 * Returns the position of a given file in the current directory
315 * returns -1 if not found
317 static int tree_get_file_position(char * filename)
319 int i;
321 /* use lastfile to determine the selected item (default=0) */
322 for (i=0; i < tc.filesindir; i++)
324 struct entry* dc = tc.dircache;
325 struct entry* e = &dc[i];
326 if (!strcasecmp(e->name, filename))
327 return(i);
329 return(-1);/* no file can match, returns undefined */
333 * Called when a new dir is loaded (for example when returning from other apps ...)
334 * also completely redraws the tree
336 static int update_dir(void)
338 bool changed = false;
339 #ifdef HAVE_TAGCACHE
340 bool id3db = *tc.dirfilter == SHOW_ID3DB;
341 /* Checks for changes */
342 if (id3db) {
343 if (tc.currtable != lasttable ||
344 tc.currextra != lastextra ||
345 tc.firstpos != lastfirstpos ||
346 reload_dir)
348 if (tagtree_load(&tc) < 0)
349 return -1;
351 lasttable = tc.currtable;
352 lastextra = tc.currextra;
353 lastfirstpos = tc.firstpos;
354 changed = true;
357 else
358 #endif
360 /* if the tc.currdir has been changed, reload it ...*/
361 if (strncmp(tc.currdir, lastdir, sizeof(lastdir)) || reload_dir)
363 if (ft_load(&tc, NULL) < 0)
364 return -1;
365 strcpy(lastdir, tc.currdir);
366 changed = true;
369 /* if selected item is undefined */
370 if (tc.selected_item == -1)
372 /* use lastfile to determine the selected item */
373 tc.selected_item = tree_get_file_position(lastfile);
375 /* If the file doesn't exists, select the first one (default) */
376 if(tc.selected_item < 0)
377 tc.selected_item = 0;
378 changed = true;
380 if (changed)
383 #ifdef HAVE_TAGCACHE
384 !id3db &&
385 #endif
386 (tc.dirfull || tc.filesindir == global_settings.max_files_in_dir) )
388 splash(HZ, ID2P(LANG_SHOWDIR_BUFFER_FULL));
391 #ifdef HAVE_TAGCACHE
392 if (id3db)
394 #ifdef HAVE_LCD_BITMAP
395 if (global_settings.show_path_in_browser == SHOW_PATH_FULL
396 || global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
398 gui_synclist_set_title(&tree_lists, tagtree_get_title(&tc),
399 filetype_get_icon(ATTR_DIRECTORY));
401 else
403 /* Must clear the title as the list is reused */
404 gui_synclist_set_title(&tree_lists, NULL, NOICON);
406 #endif
408 else
409 #endif
411 #ifdef HAVE_LCD_BITMAP
412 if (global_settings.show_path_in_browser &&
413 *(tc.dirfilter) == SHOW_PLUGINS)
415 char *title;
416 if (!strcmp(tc.currdir, PLUGIN_GAMES_DIR))
417 title = str(LANG_PLUGIN_GAMES);
418 else if (!strcmp(tc.currdir, PLUGIN_APPS_DIR))
419 title = str(LANG_PLUGIN_APPS);
420 else if (!strcmp(tc.currdir, PLUGIN_DEMOS_DIR))
421 title = str(LANG_PLUGIN_DEMOS);
422 else title = str(LANG_PLUGINS);
423 gui_synclist_set_title(&tree_lists, title, Icon_Plugin);
425 else if (global_settings.show_path_in_browser == SHOW_PATH_FULL)
427 gui_synclist_set_title(&tree_lists, tc.currdir,
428 filetype_get_icon(ATTR_DIRECTORY));
430 else if (global_settings.show_path_in_browser == SHOW_PATH_CURRENT)
432 char *title = strrchr(tc.currdir, '/') + 1;
433 if (*title == '\0')
435 /* Display "Files" for the root dir */
436 gui_synclist_set_title(&tree_lists, str(LANG_DIR_BROWSER),
437 filetype_get_icon(ATTR_DIRECTORY));
439 else
440 gui_synclist_set_title(&tree_lists, title,
441 filetype_get_icon(ATTR_DIRECTORY));
443 else
445 /* Must clear the title as the list is reused */
446 gui_synclist_set_title(&tree_lists, NULL, NOICON);
448 #endif
451 gui_synclist_set_nb_items(&tree_lists, tc.filesindir);
452 gui_synclist_set_icon_callback(&tree_lists, tree_get_fileicon);
453 if( tc.selected_item >= tc.filesindir)
454 tc.selected_item=tc.filesindir-1;
456 gui_synclist_select_item(&tree_lists, tc.selected_item);
457 #ifdef HAVE_BUTTONBAR
458 if (global_settings.buttonbar) {
459 if (*tc.dirfilter < NUM_FILTER_MODES)
460 gui_buttonbar_set(&tree_buttonbar, str(LANG_SYSFONT_DIRBROWSE_F1),
461 str(LANG_SYSFONT_DIRBROWSE_F2),
462 str(LANG_SYSFONT_DIRBROWSE_F3));
463 else
464 gui_buttonbar_set(&tree_buttonbar, "<<<", "", "");
465 gui_buttonbar_draw(&tree_buttonbar);
467 #endif
468 gui_synclist_draw(&tree_lists);
469 gui_synclist_speak_item(&tree_lists);
470 return tc.filesindir;
473 /* load tracks from specified directory to resume play */
474 void resume_directory(const char *dir)
476 int dirfilter = *tc.dirfilter;
477 int ret;
478 #ifdef HAVE_TAGCACHE
479 bool id3db = *tc.dirfilter == SHOW_ID3DB;
480 #endif
481 /* make sure the dirfilter is sane. The only time it should be possible
482 * thats its not is when resume playlist is called from a plugin
484 #ifdef HAVE_TAGCACHE
485 if (!id3db)
486 #endif
487 *tc.dirfilter = global_settings.dirfilter;
488 ret = ft_load(&tc, dir);
489 *tc.dirfilter = dirfilter;
490 if (ret < 0)
491 return;
492 lastdir[0] = 0;
494 ft_build_playlist(&tc, 0);
496 #ifdef HAVE_TAGCACHE
497 if (id3db)
498 tagtree_load(&tc);
499 #endif
502 /* Returns the current working directory and also writes cwd to buf if
503 non-NULL. In case of error, returns NULL. */
504 char *getcwd(char *buf, int size)
506 if (!buf)
507 return tc.currdir;
508 else if (size > 0)
510 strncpy(buf, tc.currdir, size);
511 return buf;
513 else
514 return NULL;
517 /* Force a reload of the directory next time directory browser is called */
518 void reload_directory(void)
520 reload_dir = true;
523 void get_current_file(char* buffer, int buffer_len)
525 #ifdef HAVE_TAGCACHE
526 /* in ID3DB mode it is a bad idea to call this function */
527 /* (only happens with `follow playlist') */
528 if( *tc.dirfilter == SHOW_ID3DB )
529 return;
530 #endif
532 struct entry* dc = tc.dircache;
533 struct entry* e = &dc[tc.selected_item];
534 snprintf(buffer, buffer_len, "%s/%s", getcwd(NULL,0),
535 tc.dirlength ? e->name : "");
538 /* Allow apps to change our dirfilter directly (required for sub browsers)
539 if they're suddenly going to become a file browser for example */
540 void set_dirfilter(int l_dirfilter)
542 *tc.dirfilter = l_dirfilter;
545 /* Selects a file and update tree context properly */
546 void set_current_file(char *path)
548 char *name;
549 int i;
551 #ifdef HAVE_TAGCACHE
552 /* in ID3DB mode it is a bad idea to call this function */
553 /* (only happens with `follow playlist') */
554 if( *tc.dirfilter == SHOW_ID3DB )
555 return;
556 #endif
558 /* separate directory from filename */
559 /* gets the directory's name and put it into tc.currdir */
560 name = strrchr(path+1,'/');
561 if (name)
563 *name = 0;
564 strcpy(tc.currdir, path);
565 *name = '/';
566 name++;
568 else
570 strcpy(tc.currdir, "/");
571 name = path+1;
574 strcpy(lastfile, name);
577 /* If we changed dir we must recalculate the dirlevel
578 and adjust the selected history properly */
579 if (strncmp(tc.currdir,lastdir,sizeof(lastdir)))
581 tc.dirlevel = 0;
582 tc.selected_item_history[tc.dirlevel] = -1;
584 /* use '/' to calculate dirlevel */
585 for (i = 1; path[i] != '\0'; i++)
587 if (path[i] == '/')
589 tc.dirlevel++;
590 tc.selected_item_history[tc.dirlevel] = -1;
594 if (ft_load(&tc, NULL) >= 0)
596 tc.selected_item = tree_get_file_position(lastfile);
601 /* main loop, handles key events */
602 static int dirbrowse()
604 int numentries=0;
605 char buf[MAX_PATH];
606 int button, oldbutton;
607 bool reload_root = false;
608 int lastfilter = *tc.dirfilter;
609 bool lastsortcase = global_settings.sort_case;
610 bool exit_func = false;
612 char* currdir = tc.currdir; /* just a shortcut */
613 #ifdef HAVE_TAGCACHE
614 bool id3db = *tc.dirfilter == SHOW_ID3DB;
616 if (id3db)
617 curr_context=CONTEXT_ID3DB;
618 else
619 #endif
620 curr_context=CONTEXT_TREE;
621 if (tc.selected_item < 0)
622 tc.selected_item = 0;
623 #ifdef HAVE_TAGCACHE
624 tc.firstpos = 0;
625 lasttable = -1;
626 lastextra = -1;
627 lastfirstpos = 0;
628 #endif
630 start_wps = false;
631 numentries = update_dir();
632 reload_dir = false;
633 if (numentries == -1)
634 return GO_TO_PREVIOUS; /* currdir is not a directory */
636 if (*tc.dirfilter > NUM_FILTER_MODES && numentries==0)
638 splash(HZ*2, ID2P(LANG_NO_FILES));
639 return GO_TO_PREVIOUS; /* No files found for rockbox_browser() */
642 gui_synclist_draw(&tree_lists);
643 while(1) {
644 struct entry *dircache = tc.dircache;
645 bool restore = false;
646 if (tc.dirlevel < 0)
647 tc.dirlevel = 0; /* shouldnt be needed.. this code needs work! */
648 #ifdef BOOTFILE
649 if (boot_changed) {
650 static const char *lines[]={ID2P(LANG_BOOT_CHANGED), ID2P(LANG_REBOOT_NOW)};
651 static const struct text_message message={lines, 2};
652 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
653 rolo_load("/" BOOTFILE);
654 restore = true;
655 boot_changed = false;
657 #endif
658 button = get_action(CONTEXT_TREE,
659 list_do_action_timeout(&tree_lists, HZ/2));
660 oldbutton = button;
661 gui_synclist_do_button(&tree_lists, &button,LIST_WRAP_UNLESS_HELD);
662 tc.selected_item = gui_synclist_get_sel_pos(&tree_lists);
663 switch ( button ) {
664 case ACTION_STD_OK:
665 /* nothing to do if no files to display */
666 if ( numentries == 0 )
667 break;
669 #ifdef HAVE_TAGCACHE
670 switch (id3db?tagtree_enter(&tc):ft_enter(&tc))
671 #else
672 switch (ft_enter(&tc))
673 #endif
675 case 1: reload_dir = true; break;
676 case 2: start_wps = true; break;
677 case 3: exit_func = true; break;
678 default: break;
680 restore = true;
681 break;
683 case ACTION_STD_CANCEL:
684 if (*tc.dirfilter > NUM_FILTER_MODES && tc.dirlevel < 1) {
685 exit_func = true;
686 break;
688 if ((*tc.dirfilter == SHOW_ID3DB && tc.dirlevel == 0) ||
689 ((*tc.dirfilter != SHOW_ID3DB && !strcmp(currdir,"/"))))
691 #ifdef HAVE_LCD_BITMAP /* charcell doesnt have ACTION_TREE_PGLEFT so this isnt needed */
692 if (oldbutton == ACTION_TREE_PGLEFT)
693 break;
694 else
695 #endif
696 return GO_TO_ROOT;
699 #ifdef HAVE_TAGCACHE
700 if (id3db)
701 tagtree_exit(&tc);
702 else
703 #endif
704 if (ft_exit(&tc) == 3)
705 exit_func = true;
707 restore = true;
708 break;
710 case ACTION_TREE_STOP:
711 if (list_stop_handler())
712 restore = true;
713 break;
715 case ACTION_STD_MENU:
716 return GO_TO_ROOT;
717 break;
719 #ifdef HAVE_RECORDING
720 case ACTION_STD_REC:
721 return GO_TO_RECSCREEN;
722 #endif
724 case ACTION_TREE_WPS:
725 return GO_TO_PREVIOUS_MUSIC;
726 break;
727 #ifdef HAVE_QUICKSCREEN
728 case ACTION_STD_QUICKSCREEN:
729 /* don't enter f2 from plugin browser */
730 if (*tc.dirfilter < NUM_FILTER_MODES)
732 if (quick_screen_quick(button))
733 reload_dir = true;
734 restore = true;
736 break;
737 #endif
738 #ifdef BUTTON_F3
739 case ACTION_F3:
740 /* don't enter f3 from plugin browser */
741 if (*tc.dirfilter < NUM_FILTER_MODES)
743 if (quick_screen_f3(ACTION_F3))
744 reload_dir = true;
745 restore = true;
747 break;
748 #endif
750 case ACTION_STD_CONTEXT:
752 int onplay_result;
753 int attr = 0;
755 if(!numentries)
756 onplay_result = onplay(NULL, 0, curr_context);
757 else {
758 #ifdef HAVE_TAGCACHE
759 if (id3db)
761 if (tagtree_get_attr(&tc) == FILE_ATTR_AUDIO)
763 attr = FILE_ATTR_AUDIO;
764 tagtree_get_filename(&tc, buf, sizeof(buf));
766 else
767 attr = ATTR_DIRECTORY;
769 else
770 #endif
772 attr = dircache[tc.selected_item].attr;
774 if (currdir[1]) /* Not in / */
775 snprintf(buf, sizeof buf, "%s/%s",
776 currdir,
777 dircache[tc.selected_item].name);
778 else /* In / */
779 snprintf(buf, sizeof buf, "/%s",
780 dircache[tc.selected_item].name);
782 onplay_result = onplay(buf, attr, curr_context);
784 switch (onplay_result)
786 case ONPLAY_MAINMENU:
787 return GO_TO_ROOT;
789 case ONPLAY_OK:
790 restore = true;
791 break;
793 case ONPLAY_RELOAD_DIR:
794 reload_dir = true;
795 break;
797 case ONPLAY_START_PLAY:
798 return GO_TO_WPS;
799 break;
801 break;
804 #ifdef HAVE_HOTSWAP
805 case SYS_FS_CHANGED:
806 #ifdef HAVE_TAGCACHE
807 if (!id3db)
808 #endif
809 reload_dir = true;
810 /* The 'dir no longer valid' situation will be caught later
811 * by checking the showdir() result. */
812 break;
813 #endif
815 default:
816 if (default_event_handler(button) == SYS_USB_CONNECTED)
818 if(*tc.dirfilter > NUM_FILTER_MODES)
819 /* leave sub-browsers after usb, doing otherwise
820 might be confusing to the user */
821 exit_func = true;
822 else
823 reload_dir = true;
825 break;
827 if (start_wps)
828 return GO_TO_WPS;
829 if (button && !IS_SYSEVENT(button))
831 storage_spin();
835 check_rescan:
836 /* do we need to rescan dir? */
837 if (reload_dir || reload_root ||
838 lastfilter != *tc.dirfilter ||
839 lastsortcase != global_settings.sort_case)
841 if (reload_root) {
842 strcpy(currdir, "/");
843 tc.dirlevel = 0;
844 #ifdef HAVE_TAGCACHE
845 tc.currtable = 0;
846 tc.currextra = 0;
847 lasttable = -1;
848 lastextra = -1;
849 #endif
850 reload_root = false;
853 if (!reload_dir)
855 gui_synclist_select_item(&tree_lists, 0);
856 gui_synclist_draw(&tree_lists);
857 tc.selected_item = 0;
858 lastdir[0] = 0;
861 lastfilter = *tc.dirfilter;
862 lastsortcase = global_settings.sort_case;
863 restore = true;
866 if (exit_func)
867 return GO_TO_PREVIOUS;
869 if (restore || reload_dir) {
870 /* restore display */
871 numentries = update_dir();
872 reload_dir = false;
873 if (currdir[1] && (numentries < 0))
874 { /* not in root and reload failed */
875 reload_root = true; /* try root */
876 goto check_rescan;
880 return true;
883 bool create_playlist(void)
885 char filename[MAX_PATH];
887 snprintf(filename, sizeof filename, "%s.m3u8",
888 tc.currdir[1] ? tc.currdir : "/root");
889 splashf(0, "%s %s", str(LANG_CREATING), filename);
891 trigger_cpu_boost();
892 catalog_add_to_a_playlist(tc.currdir, ATTR_DIRECTORY, true, filename);
893 cancel_cpu_boost();
895 return true;
898 int rockbox_browse(const char *root, int dirfilter)
900 int ret_val = 0;
901 int *last_filter = tc.dirfilter;
902 tc.dirfilter = &dirfilter;
903 tc.sort_dir = global_settings.sort_dir;
905 reload_dir = true;
906 if (dirfilter >= NUM_FILTER_MODES)
908 static struct tree_context backup;
909 int last_context;
911 backup = tc;
912 tc.selected_item = 0;
913 tc.dirlevel = 0;
914 memcpy(tc.currdir, root, sizeof(tc.currdir));
915 start_wps = false;
916 last_context = curr_context;
918 ret_val = dirbrowse();
919 tc = backup;
920 curr_context = last_context;
922 else
924 static char buf[MAX_PATH];
925 if (dirfilter != SHOW_ID3DB)
926 tc.dirfilter = &global_settings.dirfilter;
927 strcpy(buf,root);
928 set_current_file(buf);
929 ret_val = dirbrowse();
931 tc.dirfilter = last_filter;
932 return ret_val;
935 void tree_mem_init(void)
937 /* We copy the settings value in case it is changed by the user. We can't
938 use it until the next reboot. */
939 max_files = global_settings.max_files_in_dir;
941 /* initialize tree context struct */
942 memset(&tc, 0, sizeof(tc));
943 tc.dirfilter = &global_settings.dirfilter;
944 tc.sort_dir = global_settings.sort_dir;
946 tc.name_buffer_size = AVERAGE_FILENAME_LENGTH * max_files;
947 tc.name_buffer = buffer_alloc(tc.name_buffer_size);
949 tc.dircache_size = max_files * sizeof(struct entry);
950 tc.dircache = buffer_alloc(tc.dircache_size);
951 tree_get_filetypes(&filetypes, &filetypes_count);
954 bool bookmark_play(char *resume_file, int index, int offset, int seed,
955 char *filename)
957 int i;
958 char* suffix = strrchr(resume_file, '.');
959 bool started = false;
961 if (suffix != NULL &&
962 (!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
964 /* Playlist playback */
965 char* slash;
966 /* check that the file exists */
967 if(!file_exists(resume_file))
968 return false;
970 slash = strrchr(resume_file,'/');
971 if (slash)
973 char* cp;
974 *slash=0;
976 cp=resume_file;
977 if (!cp[0])
978 cp="/";
980 if (playlist_create(cp, slash+1) != -1)
982 if (global_settings.playlist_shuffle)
983 playlist_shuffle(seed, -1);
984 playlist_start(index,offset);
985 started = true;
987 *slash='/';
990 else
992 /* Directory playback */
993 lastdir[0]='\0';
994 if (playlist_create(resume_file, NULL) != -1)
996 char* peek_filename;
997 resume_directory(resume_file);
998 if (global_settings.playlist_shuffle)
999 playlist_shuffle(seed, -1);
1001 /* Check if the file is at the same spot in the directory,
1002 else search for it */
1003 peek_filename = playlist_peek(index);
1005 if (peek_filename == NULL)
1006 return false;
1008 if (strcmp(strrchr(peek_filename, '/') + 1, filename))
1010 for ( i=0; i < playlist_amount(); i++ )
1012 peek_filename = playlist_peek(i);
1014 if (peek_filename == NULL)
1015 return false;
1017 if (!strcmp(strrchr(peek_filename, '/') + 1, filename))
1018 break;
1020 if (i < playlist_amount())
1021 index = i;
1022 else
1023 return false;
1025 playlist_start(index,offset);
1026 started = true;
1030 if (started)
1031 start_wps = true;
1032 return started;
1035 static void say_filetype(int attr)
1037 /* try to find a voice ID for the extension, if known */
1038 int j;
1039 attr &= FILE_ATTR_MASK; /* file type */
1040 for (j=0; j<filetypes_count; j++)
1041 if (attr == filetypes[j].tree_attr)
1043 talk_id(filetypes[j].voiceclip, true);
1044 return;
1048 static int ft_play_dirname(char* name)
1050 #if CONFIG_CODEC != SWCODEC
1051 if (audio_status() & AUDIO_STATUS_PLAY)
1052 return 0;
1053 #endif
1055 if(talk_file(tc.currdir, name, dir_thumbnail_name, NULL,
1056 NULL, false))
1058 if(global_settings.talk_filetype)
1059 talk_id(VOICE_DIR, true);
1060 return 1;
1062 else
1063 return -1;
1066 static void ft_play_filename(char *dir, char *file)
1068 #if CONFIG_CODEC != SWCODEC
1069 if (audio_status() & AUDIO_STATUS_PLAY)
1070 return;
1071 #endif
1073 if (strlen(file) >= strlen(file_thumbnail_ext)
1074 && strcasecmp(&file[strlen(file) - strlen(file_thumbnail_ext)],
1075 file_thumbnail_ext))
1076 /* file has no .talk extension */
1077 talk_file(dir, NULL, file, file_thumbnail_ext,
1078 NULL, false);
1079 else
1080 /* it already is a .talk file, play this directly, but prefix it. */
1081 talk_file(dir, NULL, file, NULL,
1082 TALK_IDARRAY(LANG_VOICE_DIR_HOVER), false);
1085 /* These two functions are called by the USB and shutdown handlers */
1086 void tree_flush(void)
1088 #ifdef HAVE_TAGCACHE
1089 tagcache_shutdown();
1090 #endif
1092 #ifdef HAVE_TC_RAMCACHE
1093 tagcache_unload_ramcache();
1094 #endif
1096 #ifdef HAVE_DIRCACHE
1098 int old_val = global_status.dircache_size;
1099 if (global_settings.dircache)
1101 if (!dircache_is_initializing())
1102 global_status.dircache_size = dircache_get_cache_size();
1103 # ifdef HAVE_EEPROM_SETTINGS
1104 if (firmware_settings.initialized)
1105 dircache_save();
1106 # endif
1107 dircache_disable();
1109 else
1111 global_status.dircache_size = 0;
1113 if (old_val != global_status.dircache_size)
1114 status_save();
1116 #endif
1119 void tree_restore(void)
1121 #ifdef HAVE_EEPROM_SETTINGS
1122 firmware_settings.disk_clean = false;
1123 #endif
1125 #ifdef HAVE_TC_RAMCACHE
1126 remove(TAGCACHE_STATEFILE);
1127 #endif
1129 #ifdef HAVE_DIRCACHE
1130 remove(DIRCACHE_FILE);
1131 if (global_settings.dircache)
1133 /* Print "Scanning disk..." to the display. */
1134 splash(0, str(LANG_SCANNING_DISK));
1136 dircache_build(global_status.dircache_size);
1138 #endif
1139 #ifdef HAVE_TAGCACHE
1140 tagcache_start_scan();
1141 #endif