Fix wps image tags description.
[Rockbox.git] / apps / filetree.c
blob34bed714996564c5008a2b4c513efecc3c1f2117
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 "splash.h"
42 #include "yesno.h"
43 #include "cuesheet.h"
44 #ifdef HAVE_LCD_BITMAP
45 #include "keyboard.h"
46 #endif
48 #if CONFIG_TUNER
49 #include "radio.h"
50 #endif
52 #if (LCD_DEPTH > 1) || (defined(HAVE_LCD_REMOTE) && (LCD_REMOTE_DEPTH > 1))
53 #include "backdrop.h"
54 #endif
56 int ft_build_playlist(struct tree_context* c, int start_index)
58 int i;
59 int start=start_index;
61 struct entry *dircache = c->dircache;
63 for(i = 0;i < c->filesindir;i++)
65 if((dircache[i].attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
67 DEBUGF("Adding %s\n", dircache[i].name);
68 if (playlist_add(dircache[i].name) < 0)
69 break;
71 else
73 /* Adjust the start index when se skip non-MP3 entries */
74 if(i < start)
75 start_index--;
79 return start_index;
82 /* Start playback of a playlist, checking for bookmark autoload, modified
83 * playlists, etc., as required. Returns false if playback wasn't started,
84 * or started via bookmark autoload, true otherwise.
86 * Pointers to both the full pathname and the separated parts needed to
87 * avoid allocating yet another path buffer on the stack (and save some
88 * code; the caller typically needs to create the full pathname anyway)...
90 bool ft_play_playlist(char* pathname, char* dirname, char* filename)
92 if (global_settings.party_mode)
94 gui_syncsplash(HZ, ID2P(LANG_PARTY_MODE));
95 return false;
98 if (bookmark_autoload(pathname))
100 return false;
103 gui_syncsplash(0, ID2P(LANG_WAIT));
105 /* about to create a new current playlist...
106 allow user to cancel the operation */
107 if (global_settings.warnon_erase_dynplaylist &&
108 playlist_modified(NULL))
110 char *lines[] = {ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
111 struct text_message message = {lines, 1};
113 if (gui_syncyesno_run(&message, NULL, NULL) != YESNO_YES)
114 return false;
117 if (playlist_create(dirname, filename) != -1)
119 if (global_settings.playlist_shuffle)
121 playlist_shuffle(current_tick, -1);
124 playlist_start(0, 0);
125 return true;
128 return false;
131 /* walk a directory and check all dircache entries if a .talk file exists */
132 static void check_file_thumbnails(struct tree_context* c)
134 int i;
135 struct dirent *entry;
136 struct entry* dircache = c->dircache;
137 DIR *dir;
139 dir = opendir(c->currdir);
140 if(!dir)
141 return;
142 /* mark all files as non talking, except the .talk ones */
143 for (i=0; i < c->filesindir; i++)
145 if (dircache[i].attr & ATTR_DIRECTORY)
146 continue; /* we're not touching directories */
148 if (strcasecmp(file_thumbnail_ext,
149 &dircache[i].name[strlen(dircache[i].name)
150 - strlen(file_thumbnail_ext)]))
151 { /* no .talk file */
152 dircache[i].attr &= ~FILE_ATTR_THUMBNAIL; /* clear */
154 else
155 { /* .talk file, we later let them speak themselves */
156 dircache[i].attr |= FILE_ATTR_THUMBNAIL; /* set */
160 while((entry = readdir(dir)) != 0) /* walk directory */
162 int ext_pos;
164 ext_pos = strlen((char *)entry->d_name) - strlen(file_thumbnail_ext);
165 if (ext_pos <= 0 /* too short to carry ".talk" */
166 || (entry->attribute & ATTR_DIRECTORY) /* no file */
167 || strcasecmp((char *)&entry->d_name[ext_pos], file_thumbnail_ext))
168 { /* or doesn't end with ".talk", no candidate */
169 continue;
172 /* terminate the (disposable) name in dir buffer,
173 this truncates off the ".talk" without needing an extra buffer */
174 entry->d_name[ext_pos] = '\0';
176 /* search corresponding file in dir cache */
177 for (i=0; i < c->filesindir; i++)
179 if (!strcasecmp(dircache[i].name, (char *)entry->d_name))
180 { /* match */
181 dircache[i].attr |= FILE_ATTR_THUMBNAIL; /* set the flag */
182 break; /* exit search loop, because we found it */
186 closedir(dir);
189 /* support function for qsort() */
190 static int compare(const void* p1, const void* p2)
192 struct entry* e1 = (struct entry*)p1;
193 struct entry* e2 = (struct entry*)p2;
194 int criteria;
196 if (e1->attr & ATTR_DIRECTORY && e2->attr & ATTR_DIRECTORY)
197 { /* two directories */
198 criteria = global_settings.sort_dir;
200 #ifdef HAVE_MULTIVOLUME
201 if (e1->attr & ATTR_VOLUME || e2->attr & ATTR_VOLUME)
202 { /* a volume identifier is involved */
203 if (e1->attr & ATTR_VOLUME && e2->attr & ATTR_VOLUME)
204 criteria = 0; /* two volumes: sort alphabetically */
205 else /* only one is a volume: volume first */
206 return (e2->attr & ATTR_VOLUME) - (e1->attr & ATTR_VOLUME);
208 #endif
211 else if (!(e1->attr & ATTR_DIRECTORY) && !(e2->attr & ATTR_DIRECTORY))
212 { /* two files */
213 criteria = global_settings.sort_file;
215 else /* dir and file, dir goes first */
216 return ( e2->attr & ATTR_DIRECTORY ) - ( e1->attr & ATTR_DIRECTORY );
218 switch(criteria)
220 case 3: /* sort type */
222 int t1 = e1->attr & FILE_ATTR_MASK;
223 int t2 = e2->attr & FILE_ATTR_MASK;
225 if (!t1) /* unknown type */
226 t1 = INT_MAX; /* gets a high number, to sort after known */
227 if (!t2) /* unknown type */
228 t2 = INT_MAX; /* gets a high number, to sort after known */
230 if (t1 - t2) /* if different */
231 return t1 - t2;
232 /* else fall through to alphabetical sorting */
234 case 0: /* sort alphabetically asc */
235 if (global_settings.sort_case)
236 return strncmp(e1->name, e2->name, MAX_PATH);
237 else
238 return strncasecmp(e1->name, e2->name, MAX_PATH);
240 case 4: /* sort alphabetically desc */
241 if (global_settings.sort_case)
242 return strncmp(e2->name, e1->name, MAX_PATH);
243 else
244 return strncasecmp(e2->name, e1->name, MAX_PATH);
246 case 1: /* sort date */
247 return e1->time_write - e2->time_write;
249 case 2: /* sort date, newest first */
250 return e2->time_write - e1->time_write;
252 return 0; /* never reached */
255 /* load and sort directory into dircache. returns NULL on failure. */
256 int ft_load(struct tree_context* c, const char* tempdir)
258 int i;
259 int name_buffer_used = 0;
260 DIR *dir;
262 if (tempdir)
263 dir = opendir(tempdir);
264 else
265 dir = opendir(c->currdir);
266 if(!dir)
267 return -1; /* not a directory */
269 c->dirsindir = 0;
270 c->dirfull = false;
272 for ( i=0; i < global_settings.max_files_in_dir; i++ ) {
273 int len;
274 struct dirent *entry = readdir(dir);
275 struct entry* dptr =
276 (struct entry*)(c->dircache + i * sizeof(struct entry));
277 if (!entry)
278 break;
280 len = strlen((char *)entry->d_name);
282 /* skip directories . and .. */
283 if ((entry->attribute & ATTR_DIRECTORY) &&
284 (((len == 1) && (!strncmp((char *)entry->d_name, ".", 1))) ||
285 ((len == 2) && (!strncmp((char *)entry->d_name, "..", 2))))) {
286 i--;
287 continue;
290 /* Skip FAT volume ID */
291 if (entry->attribute & ATTR_VOLUME_ID) {
292 i--;
293 continue;
296 /* filter out dotfiles and hidden files */
297 if (*c->dirfilter != SHOW_ALL &&
298 ((entry->d_name[0]=='.') ||
299 (entry->attribute & ATTR_HIDDEN))) {
300 i--;
301 continue;
304 dptr->attr = entry->attribute;
306 /* check for known file types */
307 if ( !(dptr->attr & ATTR_DIRECTORY) )
308 dptr->attr |= filetype_get_attr((char *)entry->d_name);
310 /* filter out non-visible files */
311 if ((!(dptr->attr & ATTR_DIRECTORY) && (
312 (*c->dirfilter == SHOW_PLAYLIST &&
313 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) ||
314 ((*c->dirfilter == SHOW_MUSIC &&
315 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO) &&
316 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) ||
317 (*c->dirfilter == SHOW_SUPPORTED && !filetype_supported(dptr->attr)))) ||
318 (*c->dirfilter == SHOW_WPS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_WPS) ||
319 #ifdef HAVE_REMOTE_LCD
320 (*c->dirfilter == SHOW_RWPS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_RWPS) ||
321 #endif
322 #if CONFIG_TUNER
323 (*c->dirfilter == SHOW_FMR && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_FMR) ||
324 #endif
325 (*c->dirfilter == SHOW_CFG && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_CFG) ||
326 (*c->dirfilter == SHOW_LNG && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_LNG) ||
327 (*c->dirfilter == SHOW_MOD && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_MOD) ||
328 (*c->dirfilter == SHOW_FONT && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_FONT) ||
329 (*c->dirfilter == SHOW_PLUGINS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_ROCK))
331 i--;
332 continue;
335 if (len > c->name_buffer_size - name_buffer_used - 1) {
336 /* Tell the world that we ran out of buffer space */
337 c->dirfull = true;
338 break;
340 dptr->name = &c->name_buffer[name_buffer_used];
341 dptr->time_write =
342 (long)entry->wrtdate<<16 |
343 (long)entry->wrttime; /* in one # */
344 strcpy(dptr->name, (char *)entry->d_name);
345 name_buffer_used += len + 1;
347 if (dptr->attr & ATTR_DIRECTORY) /* count the remaining dirs */
348 c->dirsindir++;
350 c->filesindir = i;
351 c->dirlength = i;
352 closedir(dir);
354 qsort(c->dircache,i,sizeof(struct entry),compare);
356 /* If thumbnail talking is enabled, make an extra run to mark files with
357 associated thumbnails, so we don't do unsuccessful spinups later. */
358 if (global_settings.talk_file_clip)
359 check_file_thumbnails(c); /* map .talk to ours */
361 return 0;
364 int ft_enter(struct tree_context* c)
366 int rc = 0;
367 char buf[MAX_PATH];
368 struct entry *dircache = c->dircache;
369 struct entry* file = &dircache[c->selected_item];
370 bool reload_dir = false;
371 bool start_wps = false;
372 bool exit_func = false;
374 if (c->currdir[1])
375 snprintf(buf,sizeof(buf),"%s/%s",c->currdir, file->name);
376 else
377 snprintf(buf,sizeof(buf),"/%s",file->name);
379 if (file->attr & ATTR_DIRECTORY) {
380 memcpy(c->currdir, buf, sizeof(c->currdir));
381 if ( c->dirlevel < MAX_DIR_LEVELS )
382 c->selected_item_history[c->dirlevel] = c->selected_item;
383 c->dirlevel++;
384 c->selected_item=0;
386 else {
387 int seed = current_tick;
388 bool play = false;
389 int start_index=0;
391 switch ( file->attr & FILE_ATTR_MASK ) {
392 case FILE_ATTR_M3U:
393 play = ft_play_playlist(buf, c->currdir, file->name);
395 if (play)
397 start_index = 0;
400 break;
402 case FILE_ATTR_AUDIO:
403 if (bookmark_autoload(c->currdir))
404 break;
406 gui_syncsplash(0, ID2P(LANG_WAIT));
408 /* about to create a new current playlist...
409 allow user to cancel the operation */
410 if (global_settings.warnon_erase_dynplaylist &&
411 !global_settings.party_mode &&
412 playlist_modified(NULL))
414 char *lines[]={ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
415 struct text_message message={lines, 1};
417 if(gui_syncyesno_run(&message, NULL, NULL) != YESNO_YES)
418 break;
421 if (global_settings.party_mode)
423 playlist_insert_track(NULL, buf,
424 PLAYLIST_INSERT_LAST, true, true);
425 gui_syncsplash(HZ, ID2P(LANG_QUEUE_LAST));
427 else if (playlist_create(c->currdir, NULL) != -1)
429 start_index = ft_build_playlist(c, c->selected_item);
430 if (global_settings.playlist_shuffle)
432 start_index = playlist_shuffle(seed, start_index);
434 /* when shuffling dir.: play all files
435 even if the file selected by user is
436 not the first one */
437 if (!global_settings.play_selected)
438 start_index = 0;
441 playlist_start(start_index, 0);
442 play = true;
444 break;
446 #if CONFIG_TUNER
447 /* fmr preset file */
448 case FILE_ATTR_FMR:
450 gui_syncsplash(0, ID2P(LANG_WAIT));
452 /* Preset inside the default folder. */
453 if(!strncasecmp(FMPRESET_PATH, buf, strlen(FMPRESET_PATH)))
455 set_file(buf, global_settings.fmr_file, MAX_FILENAME);
456 radio_load_presets(global_settings.fmr_file);
457 if(!in_radio_screen())
458 radio_screen();
461 * Preset outside default folder, we can choose such only
462 * if we are out of the radio screen, so the check for the
463 * radio status isn't neccessary
465 else
467 radio_load_presets(buf);
468 radio_screen();
471 break;
472 #endif
475 /* wps config file */
476 case FILE_ATTR_WPS:
477 gui_syncsplash(0, ID2P(LANG_WAIT));
478 #if LCD_DEPTH > 1
479 unload_wps_backdrop();
480 #endif
481 wps_data_load(gui_wps[0].data, &screens[0], buf, true);
482 set_file(buf, (char *)global_settings.wps_file,
483 MAX_FILENAME);
484 break;
486 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
487 /* remote-wps config file */
488 case FILE_ATTR_RWPS:
489 gui_syncsplash(0, ID2P(LANG_WAIT));
490 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
491 unload_remote_wps_backdrop();
492 #endif
493 wps_data_load(gui_wps[1].data, &screens[1], buf, true);
494 set_file(buf, (char *)global_settings.rwps_file,
495 MAX_FILENAME);
496 break;
497 #endif
499 case FILE_ATTR_CFG:
500 gui_syncsplash(0, ID2P(LANG_WAIT));
501 if (!settings_load_config(buf,true))
502 break;
503 gui_syncsplash(HZ, ID2P(LANG_SETTINGS_LOADED));
504 break;
506 case FILE_ATTR_BMARK:
507 gui_syncsplash(0, ID2P(LANG_WAIT));
508 bookmark_load(buf, false);
509 reload_dir = true;
510 break;
512 case FILE_ATTR_LNG:
513 gui_syncsplash(0, ID2P(LANG_WAIT));
514 if(!lang_load(buf)) {
515 set_file(buf, (char *)global_settings.lang_file,
516 MAX_FILENAME);
517 talk_init(); /* use voice of same language */
518 gui_syncsplash(HZ, ID2P(LANG_LANGUAGE_LOADED));
520 break;
522 #ifdef HAVE_LCD_BITMAP
523 case FILE_ATTR_FONT:
524 gui_syncsplash(0, ID2P(LANG_WAIT));
525 font_load(buf);
526 set_file(buf, (char *)global_settings.font_file, MAX_FILENAME);
527 break;
529 case FILE_ATTR_KBD:
530 gui_syncsplash(0, ID2P(LANG_WAIT));
531 if (!load_kbd(buf))
532 gui_syncsplash(HZ, ID2P(LANG_KEYBOARD_LOADED));
533 set_file(buf, (char *)global_settings.kbd_file, MAX_FILENAME);
534 break;
535 #endif
537 #ifndef SIMULATOR
538 /* firmware file */
539 case FILE_ATTR_MOD:
540 gui_syncsplash(0, ID2P(LANG_WAIT));
541 rolo_load(buf);
542 break;
543 #endif
545 /* plugin file */
546 case FILE_ATTR_ROCK:
547 if (global_settings.party_mode) {
548 gui_syncsplash(HZ, ID2P(LANG_PARTY_MODE));
549 break;
552 gui_syncsplash(0, ID2P(LANG_WAIT));
554 if (plugin_load(buf,NULL) == PLUGIN_USB_CONNECTED)
556 if(*c->dirfilter > NUM_FILTER_MODES)
557 /* leave sub-browsers after usb, doing
558 otherwise might be confusing to the user */
559 exit_func = true;
560 else
561 reload_dir = true;
563 break;
565 case FILE_ATTR_CUE:
566 display_cuesheet_content(buf);
567 break;
569 default:
571 char* plugin;
573 if (global_settings.party_mode) {
574 gui_syncsplash(HZ, ID2P(LANG_PARTY_MODE));
575 break;
578 plugin = filetype_get_plugin(file);
579 if (plugin)
581 if (plugin_load(plugin,buf) == PLUGIN_USB_CONNECTED)
582 reload_dir = true;
584 break;
588 if ( play ) {
589 /* the resume_index must always be the index in the
590 shuffled list in case shuffle is enabled */
591 global_status.resume_index = start_index;
592 global_status.resume_offset = 0;
593 status_save();
595 start_wps = true;
597 else {
598 if (*c->dirfilter > NUM_FILTER_MODES &&
599 *c->dirfilter != SHOW_FONT &&
600 *c->dirfilter != SHOW_PLUGINS)
602 exit_func = true;
607 if (reload_dir)
608 rc = 1;
609 if (start_wps)
610 rc = 2;
611 if (exit_func)
612 rc = 3;
614 return rc;
617 int ft_exit(struct tree_context* c)
619 extern char lastfile[]; /* from tree.c */
620 char buf[MAX_PATH];
621 int rc = 0;
622 bool exit_func = false;
624 int i = strlen(c->currdir);
625 if (i>1) {
626 while (c->currdir[i-1]!='/')
627 i--;
628 strcpy(buf,&c->currdir[i]);
629 if (i==1)
630 c->currdir[i]=0;
631 else
632 c->currdir[i-1]=0;
634 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
635 exit_func = true;
637 c->dirlevel--;
638 if ( c->dirlevel < MAX_DIR_LEVELS )
639 c->selected_item=c->selected_item_history[c->dirlevel];
640 else
641 c->selected_item=0;
643 /* if undefined position */
644 if (c->selected_item == -1)
645 strcpy(lastfile, buf);
647 else
649 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
650 exit_func = true;
653 if (exit_func)
654 rc = 3;
656 return rc;