Revert... stupid svn doesn't complain when passing -m twice :(
[Rockbox.git] / apps / filetree.c
blob97732ccc82a9ce1dfee96cd14a27aafd9b6f53f7
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 #include "backdrop.h"
54 int ft_build_playlist(struct tree_context* c, int start_index)
56 int i;
57 int start=start_index;
59 struct entry *dircache = c->dircache;
61 for(i = 0;i < c->filesindir;i++)
63 if((dircache[i].attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
65 DEBUGF("Adding %s\n", dircache[i].name);
66 if (playlist_add(dircache[i].name) < 0)
67 break;
69 else
71 /* Adjust the start index when se skip non-MP3 entries */
72 if(i < start)
73 start_index--;
77 return start_index;
80 /* Start playback of a playlist, checking for bookmark autoload, modified
81 * playlists, etc., as required. Returns false if playback wasn't started,
82 * or started via bookmark autoload, true otherwise.
84 * Pointers to both the full pathname and the separated parts needed to
85 * avoid allocating yet another path buffer on the stack (and save some
86 * code; the caller typically needs to create the full pathname anyway)...
88 bool ft_play_playlist(char* pathname, char* dirname, char* filename)
90 if (global_settings.party_mode)
92 gui_syncsplash(HZ, ID2P(LANG_PARTY_MODE));
93 return false;
96 if (bookmark_autoload(pathname))
98 return false;
101 gui_syncsplash(0, ID2P(LANG_WAIT));
103 /* about to create a new current playlist...
104 allow user to cancel the operation */
105 if (global_settings.warnon_erase_dynplaylist &&
106 playlist_modified(NULL))
108 char *lines[] = {ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
109 struct text_message message = {lines, 1};
111 if (gui_syncyesno_run(&message, NULL, NULL) != YESNO_YES)
112 return false;
115 if (playlist_create(dirname, filename) != -1)
117 if (global_settings.playlist_shuffle)
119 playlist_shuffle(current_tick, -1);
122 playlist_start(0, 0);
123 return true;
126 return false;
129 /* walk a directory and check all dircache entries if a .talk file exists */
130 static void check_file_thumbnails(struct tree_context* c)
132 int i;
133 struct dirent *entry;
134 struct entry* dircache = c->dircache;
135 DIR *dir;
137 dir = opendir(c->currdir);
138 if(!dir)
139 return;
140 /* mark all files as non talking, except the .talk ones */
141 for (i=0; i < c->filesindir; i++)
143 if (dircache[i].attr & ATTR_DIRECTORY)
144 continue; /* we're not touching directories */
146 if (strcasecmp(file_thumbnail_ext,
147 &dircache[i].name[strlen(dircache[i].name)
148 - strlen(file_thumbnail_ext)]))
149 { /* no .talk file */
150 dircache[i].attr &= ~FILE_ATTR_THUMBNAIL; /* clear */
152 else
153 { /* .talk file, we later let them speak themselves */
154 dircache[i].attr |= FILE_ATTR_THUMBNAIL; /* set */
158 while((entry = readdir(dir)) != 0) /* walk directory */
160 int ext_pos;
162 ext_pos = strlen((char *)entry->d_name) - strlen(file_thumbnail_ext);
163 if (ext_pos <= 0 /* too short to carry ".talk" */
164 || (entry->attribute & ATTR_DIRECTORY) /* no file */
165 || strcasecmp((char *)&entry->d_name[ext_pos], file_thumbnail_ext))
166 { /* or doesn't end with ".talk", no candidate */
167 continue;
170 /* terminate the (disposable) name in dir buffer,
171 this truncates off the ".talk" without needing an extra buffer */
172 entry->d_name[ext_pos] = '\0';
174 /* search corresponding file in dir cache */
175 for (i=0; i < c->filesindir; i++)
177 if (!strcasecmp(dircache[i].name, (char *)entry->d_name))
178 { /* match */
179 dircache[i].attr |= FILE_ATTR_THUMBNAIL; /* set the flag */
180 break; /* exit search loop, because we found it */
184 closedir(dir);
187 /* support function for qsort() */
188 static int compare(const void* p1, const void* p2)
190 struct entry* e1 = (struct entry*)p1;
191 struct entry* e2 = (struct entry*)p2;
192 int criteria;
194 if (e1->attr & ATTR_DIRECTORY && e2->attr & ATTR_DIRECTORY)
195 { /* two directories */
196 criteria = global_settings.sort_dir;
198 #ifdef HAVE_MULTIVOLUME
199 if (e1->attr & ATTR_VOLUME || e2->attr & ATTR_VOLUME)
200 { /* a volume identifier is involved */
201 if (e1->attr & ATTR_VOLUME && e2->attr & ATTR_VOLUME)
202 criteria = 0; /* two volumes: sort alphabetically */
203 else /* only one is a volume: volume first */
204 return (e2->attr & ATTR_VOLUME) - (e1->attr & ATTR_VOLUME);
206 #endif
209 else if (!(e1->attr & ATTR_DIRECTORY) && !(e2->attr & ATTR_DIRECTORY))
210 { /* two files */
211 criteria = global_settings.sort_file;
213 else /* dir and file, dir goes first */
214 return ( e2->attr & ATTR_DIRECTORY ) - ( e1->attr & ATTR_DIRECTORY );
216 switch(criteria)
218 case 3: /* sort type */
220 int t1 = e1->attr & FILE_ATTR_MASK;
221 int t2 = e2->attr & FILE_ATTR_MASK;
223 if (!t1) /* unknown type */
224 t1 = INT_MAX; /* gets a high number, to sort after known */
225 if (!t2) /* unknown type */
226 t2 = INT_MAX; /* gets a high number, to sort after known */
228 if (t1 - t2) /* if different */
229 return t1 - t2;
230 /* else fall through to alphabetical sorting */
232 case 0: /* sort alphabetically asc */
233 if (global_settings.sort_case)
234 return strncmp(e1->name, e2->name, MAX_PATH);
235 else
236 return strncasecmp(e1->name, e2->name, MAX_PATH);
238 case 4: /* sort alphabetically desc */
239 if (global_settings.sort_case)
240 return strncmp(e2->name, e1->name, MAX_PATH);
241 else
242 return strncasecmp(e2->name, e1->name, MAX_PATH);
244 case 1: /* sort date */
245 return e1->time_write - e2->time_write;
247 case 2: /* sort date, newest first */
248 return e2->time_write - e1->time_write;
250 return 0; /* never reached */
253 /* load and sort directory into dircache. returns NULL on failure. */
254 int ft_load(struct tree_context* c, const char* tempdir)
256 int i;
257 int name_buffer_used = 0;
258 DIR *dir;
260 if (tempdir)
261 dir = opendir(tempdir);
262 else
263 dir = opendir(c->currdir);
264 if(!dir)
265 return -1; /* not a directory */
267 c->dirsindir = 0;
268 c->dirfull = false;
270 for ( i=0; i < global_settings.max_files_in_dir; i++ ) {
271 int len;
272 struct dirent *entry = readdir(dir);
273 struct entry* dptr =
274 (struct entry*)(c->dircache + i * sizeof(struct entry));
275 if (!entry)
276 break;
278 len = strlen((char *)entry->d_name);
280 /* skip directories . and .. */
281 if ((entry->attribute & ATTR_DIRECTORY) &&
282 (((len == 1) && (!strncmp((char *)entry->d_name, ".", 1))) ||
283 ((len == 2) && (!strncmp((char *)entry->d_name, "..", 2))))) {
284 i--;
285 continue;
288 /* Skip FAT volume ID */
289 if (entry->attribute & ATTR_VOLUME_ID) {
290 i--;
291 continue;
294 /* filter out dotfiles and hidden files */
295 if (*c->dirfilter != SHOW_ALL &&
296 ((entry->d_name[0]=='.') ||
297 (entry->attribute & ATTR_HIDDEN))) {
298 i--;
299 continue;
302 dptr->attr = entry->attribute;
304 /* check for known file types */
305 if ( !(dptr->attr & ATTR_DIRECTORY) )
306 dptr->attr |= filetype_get_attr((char *)entry->d_name);
308 /* filter out non-visible files */
309 if ((!(dptr->attr & ATTR_DIRECTORY) && (
310 (*c->dirfilter == SHOW_PLAYLIST &&
311 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) ||
312 ((*c->dirfilter == SHOW_MUSIC &&
313 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO) &&
314 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) ||
315 (*c->dirfilter == SHOW_SUPPORTED && !filetype_supported(dptr->attr)))) ||
316 (*c->dirfilter == SHOW_WPS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_WPS) ||
317 #ifdef HAVE_REMOTE_LCD
318 (*c->dirfilter == SHOW_RWPS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_RWPS) ||
319 #endif
320 #if CONFIG_TUNER
321 (*c->dirfilter == SHOW_FMR && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_FMR) ||
322 #endif
323 (*c->dirfilter == SHOW_CFG && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_CFG) ||
324 (*c->dirfilter == SHOW_LNG && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_LNG) ||
325 (*c->dirfilter == SHOW_MOD && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_MOD) ||
326 (*c->dirfilter == SHOW_FONT && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_FONT) ||
327 (*c->dirfilter == SHOW_PLUGINS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_ROCK))
329 i--;
330 continue;
333 if (len > c->name_buffer_size - name_buffer_used - 1) {
334 /* Tell the world that we ran out of buffer space */
335 c->dirfull = true;
336 break;
338 dptr->name = &c->name_buffer[name_buffer_used];
339 dptr->time_write =
340 (long)entry->wrtdate<<16 |
341 (long)entry->wrttime; /* in one # */
342 strcpy(dptr->name, (char *)entry->d_name);
343 name_buffer_used += len + 1;
345 if (dptr->attr & ATTR_DIRECTORY) /* count the remaining dirs */
346 c->dirsindir++;
348 c->filesindir = i;
349 c->dirlength = i;
350 closedir(dir);
352 qsort(c->dircache,i,sizeof(struct entry),compare);
354 /* If thumbnail talking is enabled, make an extra run to mark files with
355 associated thumbnails, so we don't do unsuccessful spinups later. */
356 if (global_settings.talk_file_clip)
357 check_file_thumbnails(c); /* map .talk to ours */
359 return 0;
362 int ft_enter(struct tree_context* c)
364 int rc = 0;
365 char buf[MAX_PATH];
366 struct entry *dircache = c->dircache;
367 struct entry* file = &dircache[c->selected_item];
368 bool reload_dir = false;
369 bool start_wps = false;
370 bool exit_func = false;
372 if (c->currdir[1])
373 snprintf(buf,sizeof(buf),"%s/%s",c->currdir, file->name);
374 else
375 snprintf(buf,sizeof(buf),"/%s",file->name);
377 if (file->attr & ATTR_DIRECTORY) {
378 memcpy(c->currdir, buf, sizeof(c->currdir));
379 if ( c->dirlevel < MAX_DIR_LEVELS )
380 c->selected_item_history[c->dirlevel] = c->selected_item;
381 c->dirlevel++;
382 c->selected_item=0;
384 else {
385 int seed = current_tick;
386 bool play = false;
387 int start_index=0;
389 switch ( file->attr & FILE_ATTR_MASK ) {
390 case FILE_ATTR_M3U:
391 play = ft_play_playlist(buf, c->currdir, file->name);
393 if (play)
395 start_index = 0;
398 break;
400 case FILE_ATTR_AUDIO:
401 if (bookmark_autoload(c->currdir))
402 break;
404 gui_syncsplash(0, ID2P(LANG_WAIT));
406 /* about to create a new current playlist...
407 allow user to cancel the operation */
408 if (global_settings.warnon_erase_dynplaylist &&
409 !global_settings.party_mode &&
410 playlist_modified(NULL))
412 char *lines[]={ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
413 struct text_message message={lines, 1};
415 if(gui_syncyesno_run(&message, NULL, NULL) != YESNO_YES)
416 break;
419 if (global_settings.party_mode)
421 playlist_insert_track(NULL, buf,
422 PLAYLIST_INSERT_LAST, true, true);
423 gui_syncsplash(HZ, ID2P(LANG_QUEUE_LAST));
425 else if (playlist_create(c->currdir, NULL) != -1)
427 start_index = ft_build_playlist(c, c->selected_item);
428 if (global_settings.playlist_shuffle)
430 start_index = playlist_shuffle(seed, start_index);
432 /* when shuffling dir.: play all files
433 even if the file selected by user is
434 not the first one */
435 if (!global_settings.play_selected)
436 start_index = 0;
439 playlist_start(start_index, 0);
440 play = true;
442 break;
444 #if CONFIG_TUNER
445 /* fmr preset file */
446 case FILE_ATTR_FMR:
448 gui_syncsplash(0, ID2P(LANG_WAIT));
450 /* Preset inside the default folder. */
451 if(!strncasecmp(FMPRESET_PATH, buf, strlen(FMPRESET_PATH)))
453 set_file(buf, global_settings.fmr_file, MAX_FILENAME);
454 radio_load_presets(global_settings.fmr_file);
455 if(!in_radio_screen())
456 radio_screen();
459 * Preset outside default folder, we can choose such only
460 * if we are out of the radio screen, so the check for the
461 * radio status isn't neccessary
463 else
465 radio_load_presets(buf);
466 radio_screen();
469 break;
470 #endif
473 /* wps config file */
474 case FILE_ATTR_WPS:
475 gui_syncsplash(0, ID2P(LANG_WAIT));
476 #if LCD_DEPTH > 1
477 unload_wps_backdrop();
478 #endif
479 wps_data_load(gui_wps[0].data, &screens[0], buf, true);
480 set_file(buf, (char *)global_settings.wps_file,
481 MAX_FILENAME);
482 break;
484 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
485 /* remote-wps config file */
486 case FILE_ATTR_RWPS:
487 gui_syncsplash(0, ID2P(LANG_WAIT));
488 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
489 unload_remote_wps_backdrop();
490 #endif
491 wps_data_load(gui_wps[1].data, &screens[1], buf, true);
492 set_file(buf, (char *)global_settings.rwps_file,
493 MAX_FILENAME);
494 break;
495 #endif
497 case FILE_ATTR_CFG:
498 gui_syncsplash(0, ID2P(LANG_WAIT));
499 if (!settings_load_config(buf,true))
500 break;
501 gui_syncsplash(HZ, ID2P(LANG_SETTINGS_LOADED));
502 break;
504 case FILE_ATTR_BMARK:
505 gui_syncsplash(0, ID2P(LANG_WAIT));
506 bookmark_load(buf, false);
507 reload_dir = true;
508 break;
510 case FILE_ATTR_LNG:
511 gui_syncsplash(0, ID2P(LANG_WAIT));
512 if(!lang_load(buf)) {
513 set_file(buf, (char *)global_settings.lang_file,
514 MAX_FILENAME);
515 talk_init(); /* use voice of same language */
516 gui_syncsplash(HZ, ID2P(LANG_LANGUAGE_LOADED));
518 break;
520 #ifdef HAVE_LCD_BITMAP
521 case FILE_ATTR_FONT:
522 gui_syncsplash(0, ID2P(LANG_WAIT));
523 font_load(buf);
524 set_file(buf, (char *)global_settings.font_file, MAX_FILENAME);
525 break;
527 case FILE_ATTR_KBD:
528 gui_syncsplash(0, ID2P(LANG_WAIT));
529 if (!load_kbd(buf))
530 gui_syncsplash(HZ, ID2P(LANG_KEYBOARD_LOADED));
531 set_file(buf, (char *)global_settings.kbd_file, MAX_FILENAME);
532 break;
533 #endif
535 #ifndef SIMULATOR
536 /* firmware file */
537 case FILE_ATTR_MOD:
538 gui_syncsplash(0, ID2P(LANG_WAIT));
539 rolo_load(buf);
540 break;
541 #endif
543 /* plugin file */
544 case FILE_ATTR_ROCK:
545 if (global_settings.party_mode) {
546 gui_syncsplash(HZ, ID2P(LANG_PARTY_MODE));
547 break;
550 gui_syncsplash(0, ID2P(LANG_WAIT));
552 if (plugin_load(buf,NULL) == PLUGIN_USB_CONNECTED)
554 if(*c->dirfilter > NUM_FILTER_MODES)
555 /* leave sub-browsers after usb, doing
556 otherwise might be confusing to the user */
557 exit_func = true;
558 else
559 reload_dir = true;
561 break;
563 case FILE_ATTR_CUE:
564 display_cuesheet_content(buf);
565 break;
567 default:
569 char* plugin;
571 if (global_settings.party_mode) {
572 gui_syncsplash(HZ, ID2P(LANG_PARTY_MODE));
573 break;
576 plugin = filetype_get_plugin(file);
577 if (plugin)
579 if (plugin_load(plugin,buf) == PLUGIN_USB_CONNECTED)
580 reload_dir = true;
582 break;
586 if ( play ) {
587 /* the resume_index must always be the index in the
588 shuffled list in case shuffle is enabled */
589 global_status.resume_index = start_index;
590 global_status.resume_offset = 0;
591 status_save();
593 start_wps = true;
595 else {
596 if (*c->dirfilter > NUM_FILTER_MODES &&
597 *c->dirfilter != SHOW_FONT &&
598 *c->dirfilter != SHOW_PLUGINS)
600 exit_func = true;
605 if (reload_dir)
606 rc = 1;
607 if (start_wps)
608 rc = 2;
609 if (exit_func)
610 rc = 3;
612 return rc;
615 int ft_exit(struct tree_context* c)
617 extern char lastfile[]; /* from tree.c */
618 char buf[MAX_PATH];
619 int rc = 0;
620 bool exit_func = false;
622 int i = strlen(c->currdir);
623 if (i>1) {
624 while (c->currdir[i-1]!='/')
625 i--;
626 strcpy(buf,&c->currdir[i]);
627 if (i==1)
628 c->currdir[i]=0;
629 else
630 c->currdir[i-1]=0;
632 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
633 exit_func = true;
635 c->dirlevel--;
636 if ( c->dirlevel < MAX_DIR_LEVELS )
637 c->selected_item=c->selected_item_history[c->dirlevel];
638 else
639 c->selected_item=0;
641 /* if undefined position */
642 if (c->selected_item == -1)
643 strcpy(lastfile, buf);
645 else
647 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
648 exit_func = true;
651 if (exit_func)
652 rc = 3;
654 return rc;