Small '#ifdef HAVE_TAGCACHE' fix
[Rockbox.git] / apps / filetree.c
blobedf4fa0b6374ce55e0244085d1429d1811f8a3b2
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Björn Stenberg
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 <stdlib.h>
20 #include <file.h>
21 #include <dir.h>
22 #include <string.h>
23 #include <kernel.h>
24 #include <lcd.h>
25 #include <debug.h>
26 #include <font.h>
27 #include <limits.h>
28 #include "bookmark.h"
29 #include "tree.h"
30 #include "settings.h"
31 #include "filetypes.h"
32 #include "talk.h"
33 #include "playlist.h"
34 #include "gwps.h"
35 #include "lang.h"
36 #include "language.h"
37 #include "screens.h"
38 #include "plugin.h"
39 #include "rolo.h"
40 #include "sprintf.h"
41 #include "dircache.h"
42 #include "splash.h"
43 #include "yesno.h"
44 #include "cuesheet.h"
45 #ifdef HAVE_LCD_BITMAP
46 #include "keyboard.h"
47 #endif
49 #if CONFIG_TUNER
50 #include "radio.h"
51 #endif
53 #ifndef SIMULATOR
54 static int boot_size = 0;
55 static int boot_cluster;
56 #endif
58 #if LCD_DEPTH > 1
59 #include "backdrop.h"
60 #endif
62 extern bool boot_changed;
64 int ft_build_playlist(struct tree_context* c, int start_index)
66 int i;
67 int start=start_index;
69 struct entry *dircache = c->dircache;
71 for(i = 0;i < c->filesindir;i++)
73 if((dircache[i].attr & TREE_ATTR_MASK) == TREE_ATTR_MPA)
75 DEBUGF("Adding %s\n", dircache[i].name);
76 if (playlist_add(dircache[i].name) < 0)
77 break;
79 else
81 /* Adjust the start index when se skip non-MP3 entries */
82 if(i < start)
83 start_index--;
87 return start_index;
90 /* walk a directory and check all dircache entries if a .talk file exists */
91 static void check_file_thumbnails(struct tree_context* c)
93 int i;
94 struct dircache_entry *entry;
95 struct entry* dircache = c->dircache;
96 DIRCACHED *dir;
98 dir = opendir_cached(c->currdir);
99 if(!dir)
100 return;
101 /* mark all files as non talking, except the .talk ones */
102 for (i=0; i < c->filesindir; i++)
104 if (dircache[i].attr & ATTR_DIRECTORY)
105 continue; /* we're not touching directories */
107 if (strcasecmp(file_thumbnail_ext,
108 &dircache[i].name[strlen(dircache[i].name)
109 - strlen(file_thumbnail_ext)]))
110 { /* no .talk file */
111 dircache[i].attr &= ~TREE_ATTR_THUMBNAIL; /* clear */
113 else
114 { /* .talk file, we later let them speak themselves */
115 dircache[i].attr |= TREE_ATTR_THUMBNAIL; /* set */
119 while((entry = readdir_cached(dir)) != 0) /* walk directory */
121 int ext_pos;
123 ext_pos = strlen((char *)entry->d_name) - strlen(file_thumbnail_ext);
124 if (ext_pos <= 0 /* too short to carry ".talk" */
125 || (entry->attribute & ATTR_DIRECTORY) /* no file */
126 || strcasecmp((char *)&entry->d_name[ext_pos], file_thumbnail_ext))
127 { /* or doesn't end with ".talk", no candidate */
128 continue;
131 /* terminate the (disposable) name in dir buffer,
132 this truncates off the ".talk" without needing an extra buffer */
133 entry->d_name[ext_pos] = '\0';
135 /* search corresponding file in dir cache */
136 for (i=0; i < c->filesindir; i++)
138 if (!strcasecmp(dircache[i].name, (char *)entry->d_name))
139 { /* match */
140 dircache[i].attr |= TREE_ATTR_THUMBNAIL; /* set the flag */
141 break; /* exit search loop, because we found it */
145 closedir_cached(dir);
148 /* support function for qsort() */
149 static int compare(const void* p1, const void* p2)
151 struct entry* e1 = (struct entry*)p1;
152 struct entry* e2 = (struct entry*)p2;
153 int criteria;
155 if (e1->attr & ATTR_DIRECTORY && e2->attr & ATTR_DIRECTORY)
156 { /* two directories */
157 criteria = global_settings.sort_dir;
159 if (e1->attr & ATTR_VOLUME || e2->attr & ATTR_VOLUME)
160 { /* a volume identifier is involved */
161 if (e1->attr & ATTR_VOLUME && e2->attr & ATTR_VOLUME)
162 criteria = 0; /* two volumes: sort alphabetically */
163 else /* only one is a volume: volume first */
164 return (e2->attr & ATTR_VOLUME) - (e1->attr & ATTR_VOLUME);
167 else if (!(e1->attr & ATTR_DIRECTORY) && !(e2->attr & ATTR_DIRECTORY))
168 { /* two files */
169 criteria = global_settings.sort_file;
171 else /* dir and file, dir goes first */
172 return ( e2->attr & ATTR_DIRECTORY ) - ( e1->attr & ATTR_DIRECTORY );
174 switch(criteria)
176 case 3: /* sort type */
178 int t1 = e1->attr & TREE_ATTR_MASK;
179 int t2 = e2->attr & TREE_ATTR_MASK;
181 if (!t1) /* unknown type */
182 t1 = INT_MAX; /* gets a high number, to sort after known */
183 if (!t2) /* unknown type */
184 t2 = INT_MAX; /* gets a high number, to sort after known */
186 if (t1 - t2) /* if different */
187 return t1 - t2;
188 /* else fall through to alphabetical sorting */
190 case 0: /* sort alphabetically asc */
191 if (global_settings.sort_case)
192 return strncmp(e1->name, e2->name, MAX_PATH);
193 else
194 return strncasecmp(e1->name, e2->name, MAX_PATH);
196 case 4: /* sort alphabetically desc */
197 if (global_settings.sort_case)
198 return strncmp(e2->name, e1->name, MAX_PATH);
199 else
200 return strncasecmp(e2->name, e1->name, MAX_PATH);
202 case 1: /* sort date */
203 return e1->time_write - e2->time_write;
205 case 2: /* sort date, newest first */
206 return e2->time_write - e1->time_write;
208 return 0; /* never reached */
211 /* load and sort directory into dircache. returns NULL on failure. */
212 int ft_load(struct tree_context* c, const char* tempdir)
214 int i;
215 int name_buffer_used = 0;
216 DIRCACHED *dir;
218 if (tempdir)
219 dir = opendir_cached(tempdir);
220 else
221 dir = opendir_cached(c->currdir);
222 if(!dir)
223 return -1; /* not a directory */
225 c->dirsindir = 0;
226 c->dirfull = false;
228 for ( i=0; i < global_settings.max_files_in_dir; i++ ) {
229 int len;
230 struct dircache_entry *entry = readdir_cached(dir);
231 struct entry* dptr =
232 (struct entry*)(c->dircache + i * sizeof(struct entry));
233 if (!entry)
234 break;
236 len = strlen((char *)entry->d_name);
238 /* skip directories . and .. */
239 if ((entry->attribute & ATTR_DIRECTORY) &&
240 (((len == 1) && (!strncmp((char *)entry->d_name, ".", 1))) ||
241 ((len == 2) && (!strncmp((char *)entry->d_name, "..", 2))))) {
242 i--;
243 continue;
246 /* Skip FAT volume ID */
247 if (entry->attribute & ATTR_VOLUME_ID) {
248 i--;
249 continue;
252 /* filter out dotfiles and hidden files */
253 if (*c->dirfilter != SHOW_ALL &&
254 ((entry->d_name[0]=='.') ||
255 (entry->attribute & ATTR_HIDDEN))) {
256 i--;
257 continue;
260 dptr->attr = entry->attribute;
262 /* check for known file types */
263 if ( !(dptr->attr & ATTR_DIRECTORY) )
264 dptr->attr |= filetype_get_attr((char *)entry->d_name);
266 #ifdef BOOTFILE
267 /* memorize/compare details about the boot file */
268 if ((c->currdir[1] == 0) && !strcasecmp(entry->d_name, BOOTFILE)) {
269 if (boot_size) {
270 if ((entry->size != boot_size) ||
271 (entry->startcluster != boot_cluster))
272 boot_changed = true;
274 boot_size = entry->size;
275 boot_cluster = entry->startcluster;
277 #endif
279 /* filter out non-visible files */
280 if ((!(dptr->attr & ATTR_DIRECTORY) && (
281 (*c->dirfilter == SHOW_PLAYLIST &&
282 (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_M3U) ||
283 ((*c->dirfilter == SHOW_MUSIC &&
284 (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_MPA) &&
285 (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_M3U) ||
286 (*c->dirfilter == SHOW_SUPPORTED && !filetype_supported(dptr->attr)))) ||
287 (*c->dirfilter == SHOW_WPS && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_WPS) ||
288 #ifdef HAVE_REMOTE_LCD
289 (*c->dirfilter == SHOW_RWPS && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_RWPS) ||
290 #endif
291 #if CONFIG_TUNER
292 (*c->dirfilter == SHOW_FMR && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_FMR) ||
293 #endif
294 (*c->dirfilter == SHOW_CFG && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_CFG) ||
295 (*c->dirfilter == SHOW_LNG && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_LNG) ||
296 (*c->dirfilter == SHOW_MOD && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_MOD) ||
297 (*c->dirfilter == SHOW_FONT && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_FONT) ||
298 (*c->dirfilter == SHOW_PLUGINS && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_ROCK))
300 i--;
301 continue;
304 if (len > c->name_buffer_size - name_buffer_used - 1) {
305 /* Tell the world that we ran out of buffer space */
306 c->dirfull = true;
307 break;
309 dptr->name = &c->name_buffer[name_buffer_used];
310 dptr->time_write =
311 (long)entry->wrtdate<<16 |
312 (long)entry->wrttime; /* in one # */
313 strcpy(dptr->name, (char *)entry->d_name);
314 name_buffer_used += len + 1;
316 if (dptr->attr & ATTR_DIRECTORY) /* count the remaining dirs */
317 c->dirsindir++;
319 c->filesindir = i;
320 c->dirlength = i;
321 closedir_cached(dir);
323 qsort(c->dircache,i,sizeof(struct entry),compare);
325 /* If thumbnail talking is enabled, make an extra run to mark files with
326 associated thumbnails, so we don't do unsuccessful spinups later. */
327 if (global_settings.talk_file == 3)
328 check_file_thumbnails(c); /* map .talk to ours */
330 return 0;
333 int ft_enter(struct tree_context* c)
335 int rc = 0;
336 char buf[MAX_PATH];
337 struct entry *dircache = c->dircache;
338 struct entry* file = &dircache[c->selected_item];
339 bool reload_dir = false;
340 bool start_wps = false;
341 bool exit_func = false;
343 if (c->currdir[1])
344 snprintf(buf,sizeof(buf),"%s/%s",c->currdir, file->name);
345 else
346 snprintf(buf,sizeof(buf),"/%s",file->name);
348 if (file->attr & ATTR_DIRECTORY) {
349 memcpy(c->currdir, buf, sizeof(c->currdir));
350 if ( c->dirlevel < MAX_DIR_LEVELS )
351 c->selected_item_history[c->dirlevel] = c->selected_item;
352 c->dirlevel++;
353 c->selected_item=0;
355 else {
356 int seed = current_tick;
357 bool play = false;
358 int start_index=0;
360 switch ( file->attr & TREE_ATTR_MASK ) {
361 case TREE_ATTR_M3U:
362 if (global_settings.party_mode) {
363 gui_syncsplash(HZ, true, str(LANG_PARTY_MODE));
364 break;
367 if (bookmark_autoload(buf))
368 break;
370 gui_syncsplash(0, true, str(LANG_WAIT));
372 /* about to create a new current playlist...
373 allow user to cancel the operation */
374 if (global_settings.warnon_erase_dynplaylist &&
375 playlist_modified(NULL))
377 char *lines[]={str(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
378 struct text_message message={lines, 1};
380 if(gui_syncyesno_run(&message, NULL, NULL) != YESNO_YES)
381 break;
384 if (playlist_create(c->currdir, file->name) != -1)
386 if (global_settings.playlist_shuffle)
387 playlist_shuffle(seed, -1);
388 start_index = 0;
389 playlist_start(start_index,0);
390 play = true;
392 break;
394 case TREE_ATTR_MPA:
395 if (bookmark_autoload(c->currdir))
396 break;
398 gui_syncsplash(0, true, str(LANG_WAIT));
400 /* about to create a new current playlist...
401 allow user to cancel the operation */
402 if (global_settings.warnon_erase_dynplaylist &&
403 !global_settings.party_mode &&
404 playlist_modified(NULL))
406 char *lines[]={str(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
407 struct text_message message={lines, 1};
409 if(gui_syncyesno_run(&message, NULL, NULL) != YESNO_YES)
410 break;
413 if (global_settings.party_mode)
415 playlist_insert_track(NULL, buf,
416 PLAYLIST_INSERT_LAST, true, true);
417 gui_syncsplash(HZ, true, str(LANG_QUEUE_LAST));
419 else if (playlist_create(c->currdir, NULL) != -1)
421 start_index = ft_build_playlist(c, c->selected_item);
422 if (global_settings.playlist_shuffle)
424 start_index = playlist_shuffle(seed, start_index);
426 /* when shuffling dir.: play all files
427 even if the file selected by user is
428 not the first one */
429 if (!global_settings.play_selected)
430 start_index = 0;
433 playlist_start(start_index, 0);
434 play = true;
436 break;
438 #if CONFIG_TUNER
439 /* fmr preset file */
440 case TREE_ATTR_FMR:
442 gui_syncsplash(0, true, str(LANG_WAIT));
444 /* Preset inside the default folder. */
445 if(!strncasecmp(FMPRESET_PATH, buf, strlen(FMPRESET_PATH)))
447 set_file(buf, global_settings.fmr_file, MAX_FILENAME);
448 radio_load_presets(global_settings.fmr_file);
449 if(!in_radio_screen())
450 radio_screen();
453 * Preset outside default folder, we can choose such only
454 * if we are out of the radio screen, so the check for the
455 * radio status isn't neccessary
457 else
459 radio_load_presets(buf);
460 radio_screen();
463 break;
464 #endif
467 /* wps config file */
468 case TREE_ATTR_WPS:
469 gui_syncsplash(0, true, str(LANG_WAIT));
470 #if LCD_DEPTH > 1
471 unload_wps_backdrop();
472 #endif
473 wps_data_load(gui_wps[0].data, buf, true);
474 set_file(buf, (char *)global_settings.wps_file,
475 MAX_FILENAME);
476 break;
478 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
479 /* remote-wps config file */
480 case TREE_ATTR_RWPS:
481 gui_syncsplash(0, true, str(LANG_WAIT));
482 wps_data_load(gui_wps[1].data, buf, true);
483 set_file(buf, (char *)global_settings.rwps_file,
484 MAX_FILENAME);
485 break;
486 #endif
488 case TREE_ATTR_CFG:
489 gui_syncsplash(0, true, str(LANG_WAIT));
490 if (!settings_load_config(buf,true))
491 break;
492 gui_syncsplash(HZ, true, str(LANG_SETTINGS_LOADED));
493 break;
495 case TREE_ATTR_BMARK:
496 gui_syncsplash(0, true, str(LANG_WAIT));
497 bookmark_load(buf, false);
498 reload_dir = true;
499 break;
501 case TREE_ATTR_LNG:
502 gui_syncsplash(0, true, str(LANG_WAIT));
503 if(!lang_load(buf)) {
504 set_file(buf, (char *)global_settings.lang_file,
505 MAX_FILENAME);
506 talk_init(); /* use voice of same language */
507 gui_syncsplash(HZ, true, str(LANG_LANGUAGE_LOADED));
509 break;
511 #ifdef HAVE_LCD_BITMAP
512 case TREE_ATTR_FONT:
513 gui_syncsplash(0, true, str(LANG_WAIT));
514 font_load(buf);
515 set_file(buf, (char *)global_settings.font_file, MAX_FILENAME);
516 break;
518 case TREE_ATTR_KBD:
519 gui_syncsplash(0, true, str(LANG_WAIT));
520 if (!load_kbd(buf))
521 gui_syncsplash(HZ, true, str(LANG_KEYBOARD_LOADED));
522 set_file(buf, (char *)global_settings.kbd_file, MAX_FILENAME);
523 break;
524 #endif
526 #ifndef SIMULATOR
527 /* firmware file */
528 case TREE_ATTR_MOD:
529 gui_syncsplash(0, true, str(LANG_WAIT));
530 rolo_load(buf);
531 break;
532 #endif
534 /* plugin file */
535 case TREE_ATTR_ROCK:
536 if (global_settings.party_mode) {
537 gui_syncsplash(HZ, true, str(LANG_PARTY_MODE));
538 break;
541 gui_syncsplash(0, true, str(LANG_WAIT));
543 if (plugin_load(buf,NULL) == PLUGIN_USB_CONNECTED)
545 if(*c->dirfilter > NUM_FILTER_MODES)
546 /* leave sub-browsers after usb, doing
547 otherwise might be confusing to the user */
548 exit_func = true;
549 else
550 reload_dir = true;
552 break;
554 case TREE_ATTR_CUE:
555 display_cuesheet_content(buf);
556 break;
558 default:
560 char* plugin;
562 if (global_settings.party_mode) {
563 gui_syncsplash(HZ, true, str(LANG_PARTY_MODE));
564 break;
567 plugin = filetype_get_plugin(file);
568 if (plugin)
570 if (plugin_load(plugin,buf) == PLUGIN_USB_CONNECTED)
571 reload_dir = true;
573 break;
577 if ( play ) {
578 /* the resume_index must always be the index in the
579 shuffled list in case shuffle is enabled */
580 global_status.resume_index = start_index;
581 global_status.resume_offset = 0;
582 status_save();
584 start_wps = true;
586 else {
587 if (*c->dirfilter > NUM_FILTER_MODES &&
588 *c->dirfilter != SHOW_FONT &&
589 *c->dirfilter != SHOW_PLUGINS)
591 exit_func = true;
596 if (reload_dir)
597 rc = 1;
598 if (start_wps)
599 rc = 2;
600 if (exit_func)
601 rc = 3;
603 return rc;
606 int ft_exit(struct tree_context* c)
608 extern char lastfile[]; /* from tree.c */
609 char buf[MAX_PATH];
610 int rc = 0;
611 bool exit_func = false;
613 int i = strlen(c->currdir);
614 if (i>1) {
615 while (c->currdir[i-1]!='/')
616 i--;
617 strcpy(buf,&c->currdir[i]);
618 if (i==1)
619 c->currdir[i]=0;
620 else
621 c->currdir[i-1]=0;
623 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
624 exit_func = true;
626 c->dirlevel--;
627 if ( c->dirlevel < MAX_DIR_LEVELS )
628 c->selected_item=c->selected_item_history[c->dirlevel];
629 else
630 c->selected_item=0;
632 /* if undefined position */
633 if (c->selected_item == -1)
634 strcpy(lastfile, buf);
636 else
638 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
639 exit_func = true;
642 if (exit_func)
643 rc = 3;
645 return rc;