bump version to m1.0.5 release
[Rockbox.git] / apps / filetree.c
blobe8fb459d5256c4a4a6df7c97210b9092a728b424
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 if (playlist_add(dircache[i].name) < 0)
66 break;
68 else
70 /* Adjust the start index when se skip non-MP3 entries */
71 if(i < start)
72 start_index--;
76 return start_index;
79 /* Start playback of a playlist, checking for bookmark autoload, modified
80 * playlists, etc., as required. Returns false if playback wasn't started,
81 * or started via bookmark autoload, true otherwise.
83 * Pointers to both the full pathname and the separated parts needed to
84 * avoid allocating yet another path buffer on the stack (and save some
85 * code; the caller typically needs to create the full pathname anyway)...
87 bool ft_play_playlist(char* pathname, char* dirname, char* filename)
89 if (global_settings.party_mode)
91 gui_syncsplash(HZ, ID2P(LANG_PARTY_MODE));
92 return false;
95 if (bookmark_autoload(pathname))
97 return false;
100 gui_syncsplash(0, ID2P(LANG_WAIT));
102 /* about to create a new current playlist...
103 allow user to cancel the operation */
104 if (global_settings.warnon_erase_dynplaylist &&
105 playlist_modified(NULL))
107 char *lines[] = {ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
108 struct text_message message = {lines, 1};
110 if (gui_syncyesno_run(&message, NULL, NULL) != YESNO_YES)
111 return false;
114 if (playlist_create(dirname, filename) != -1)
116 if (global_settings.playlist_shuffle)
118 playlist_shuffle(current_tick, -1);
121 playlist_start(0, 0);
122 return true;
125 return false;
128 /* walk a directory and check all dircache entries if a .talk file exists */
129 static void check_file_thumbnails(struct tree_context* c)
131 int i;
132 struct dirent *entry;
133 struct entry* dircache = c->dircache;
134 DIR *dir;
136 dir = opendir(c->currdir);
137 if(!dir)
138 return;
139 /* mark all files as non talking, except the .talk ones */
140 for (i=0; i < c->filesindir; i++)
142 if (dircache[i].attr & ATTR_DIRECTORY)
143 continue; /* we're not touching directories */
145 if (strcasecmp(file_thumbnail_ext,
146 &dircache[i].name[strlen(dircache[i].name)
147 - strlen(file_thumbnail_ext)]))
148 { /* no .talk file */
149 dircache[i].attr &= ~FILE_ATTR_THUMBNAIL; /* clear */
151 else
152 { /* .talk file, we later let them speak themselves */
153 dircache[i].attr |= FILE_ATTR_THUMBNAIL; /* set */
157 while((entry = readdir(dir)) != 0) /* walk directory */
159 int ext_pos;
161 ext_pos = strlen((char *)entry->d_name) - strlen(file_thumbnail_ext);
162 if (ext_pos <= 0 /* too short to carry ".talk" */
163 || (entry->attribute & ATTR_DIRECTORY) /* no file */
164 || strcasecmp((char *)&entry->d_name[ext_pos], file_thumbnail_ext))
165 { /* or doesn't end with ".talk", no candidate */
166 continue;
169 /* terminate the (disposable) name in dir buffer,
170 this truncates off the ".talk" without needing an extra buffer */
171 entry->d_name[ext_pos] = '\0';
173 /* search corresponding file in dir cache */
174 for (i=0; i < c->filesindir; i++)
176 if (!strcasecmp(dircache[i].name, (char *)entry->d_name))
177 { /* match */
178 dircache[i].attr |= FILE_ATTR_THUMBNAIL; /* set the flag */
179 break; /* exit search loop, because we found it */
183 closedir(dir);
186 /* support function for qsort() */
187 static int compare(const void* p1, const void* p2)
189 struct entry* e1 = (struct entry*)p1;
190 struct entry* e2 = (struct entry*)p2;
191 int criteria;
193 if (e1->attr & ATTR_DIRECTORY && e2->attr & ATTR_DIRECTORY)
194 { /* two directories */
195 criteria = global_settings.sort_dir;
197 #ifdef HAVE_MULTIVOLUME
198 if (e1->attr & ATTR_VOLUME || e2->attr & ATTR_VOLUME)
199 { /* a volume identifier is involved */
200 if (e1->attr & ATTR_VOLUME && e2->attr & ATTR_VOLUME)
201 criteria = 0; /* two volumes: sort alphabetically */
202 else /* only one is a volume: volume first */
203 return (e2->attr & ATTR_VOLUME) - (e1->attr & ATTR_VOLUME);
205 #endif
208 else if (!(e1->attr & ATTR_DIRECTORY) && !(e2->attr & ATTR_DIRECTORY))
209 { /* two files */
210 criteria = global_settings.sort_file;
212 else /* dir and file, dir goes first */
213 return ( e2->attr & ATTR_DIRECTORY ) - ( e1->attr & ATTR_DIRECTORY );
215 switch(criteria)
217 case 3: /* sort type */
219 int t1 = e1->attr & FILE_ATTR_MASK;
220 int t2 = e2->attr & FILE_ATTR_MASK;
222 if (!t1) /* unknown type */
223 t1 = INT_MAX; /* gets a high number, to sort after known */
224 if (!t2) /* unknown type */
225 t2 = INT_MAX; /* gets a high number, to sort after known */
227 if (t1 - t2) /* if different */
228 return t1 - t2;
229 /* else fall through to alphabetical sorting */
231 case 0: /* sort alphabetically asc */
232 if (global_settings.sort_case)
233 return strncmp(e1->name, e2->name, MAX_PATH);
234 else
235 return strncasecmp(e1->name, e2->name, MAX_PATH);
237 case 4: /* sort alphabetically desc */
238 if (global_settings.sort_case)
239 return strncmp(e2->name, e1->name, MAX_PATH);
240 else
241 return strncasecmp(e2->name, e1->name, MAX_PATH);
243 case 1: /* sort date */
244 return e1->time_write - e2->time_write;
246 case 2: /* sort date, newest first */
247 return e2->time_write - e1->time_write;
249 return 0; /* never reached */
252 /* load and sort directory into dircache. returns NULL on failure. */
253 int ft_load(struct tree_context* c, const char* tempdir)
255 int i;
256 int name_buffer_used = 0;
257 DIR *dir;
259 if (tempdir)
260 dir = opendir(tempdir);
261 else
262 dir = opendir(c->currdir);
263 if(!dir)
264 return -1; /* not a directory */
266 c->dirsindir = 0;
267 c->dirfull = false;
269 for ( i=0; i < global_settings.max_files_in_dir; i++ ) {
270 int len;
271 struct dirent *entry = readdir(dir);
272 struct entry* dptr =
273 (struct entry*)(c->dircache + i * sizeof(struct entry));
274 if (!entry)
275 break;
277 len = strlen((char *)entry->d_name);
279 /* skip directories . and .. */
280 if ((entry->attribute & ATTR_DIRECTORY) &&
281 (((len == 1) && (!strncmp((char *)entry->d_name, ".", 1))) ||
282 ((len == 2) && (!strncmp((char *)entry->d_name, "..", 2))))) {
283 i--;
284 continue;
287 /* Skip FAT volume ID */
288 if (entry->attribute & ATTR_VOLUME_ID) {
289 i--;
290 continue;
293 /* filter out dotfiles and hidden files */
294 if (*c->dirfilter != SHOW_ALL &&
295 ((entry->d_name[0]=='.') ||
296 (entry->attribute & ATTR_HIDDEN))) {
297 i--;
298 continue;
301 dptr->attr = entry->attribute;
303 /* check for known file types */
304 if ( !(dptr->attr & ATTR_DIRECTORY) )
305 dptr->attr |= filetype_get_attr((char *)entry->d_name);
307 /* filter out non-visible files */
308 if ((!(dptr->attr & ATTR_DIRECTORY) && (
309 (*c->dirfilter == SHOW_PLAYLIST &&
310 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) ||
311 ((*c->dirfilter == SHOW_MUSIC &&
312 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO) &&
313 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) ||
314 (*c->dirfilter == SHOW_SUPPORTED && !filetype_supported(dptr->attr)))) ||
315 (*c->dirfilter == SHOW_WPS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_WPS) ||
316 #ifdef HAVE_REMOTE_LCD
317 (*c->dirfilter == SHOW_RWPS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_RWPS) ||
318 #endif
319 #if CONFIG_TUNER
320 (*c->dirfilter == SHOW_FMR && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_FMR) ||
321 #endif
322 (*c->dirfilter == SHOW_CFG && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_CFG) ||
323 (*c->dirfilter == SHOW_LNG && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_LNG) ||
324 (*c->dirfilter == SHOW_MOD && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_MOD) ||
325 (*c->dirfilter == SHOW_FONT && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_FONT) ||
326 (*c->dirfilter == SHOW_PLUGINS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_ROCK))
328 i--;
329 continue;
332 if (len > c->name_buffer_size - name_buffer_used - 1) {
333 /* Tell the world that we ran out of buffer space */
334 c->dirfull = true;
335 break;
337 dptr->name = &c->name_buffer[name_buffer_used];
338 dptr->time_write =
339 (long)entry->wrtdate<<16 |
340 (long)entry->wrttime; /* in one # */
341 strcpy(dptr->name, (char *)entry->d_name);
342 name_buffer_used += len + 1;
344 if (dptr->attr & ATTR_DIRECTORY) /* count the remaining dirs */
345 c->dirsindir++;
347 c->filesindir = i;
348 c->dirlength = i;
349 closedir(dir);
351 qsort(c->dircache,i,sizeof(struct entry),compare);
353 /* If thumbnail talking is enabled, make an extra run to mark files with
354 associated thumbnails, so we don't do unsuccessful spinups later. */
355 if (global_settings.talk_file_clip)
356 check_file_thumbnails(c); /* map .talk to ours */
358 return 0;
361 int ft_enter(struct tree_context* c)
363 int rc = 0;
364 char buf[MAX_PATH];
365 struct entry *dircache = c->dircache;
366 struct entry* file = &dircache[c->selected_item];
367 bool reload_dir = false;
368 bool start_wps = false;
369 bool exit_func = false;
371 if (c->currdir[1])
372 snprintf(buf,sizeof(buf),"%s/%s",c->currdir, file->name);
373 else
374 snprintf(buf,sizeof(buf),"/%s",file->name);
376 if (file->attr & ATTR_DIRECTORY) {
377 memcpy(c->currdir, buf, sizeof(c->currdir));
378 if ( c->dirlevel < MAX_DIR_LEVELS )
379 c->selected_item_history[c->dirlevel] = c->selected_item;
380 c->dirlevel++;
381 c->selected_item=0;
383 else {
384 int seed = current_tick;
385 bool play = false;
386 int start_index=0;
388 switch ( file->attr & FILE_ATTR_MASK ) {
389 case FILE_ATTR_M3U:
390 play = ft_play_playlist(buf, c->currdir, file->name);
392 if (play)
394 start_index = 0;
397 break;
399 case FILE_ATTR_AUDIO:
400 if (bookmark_autoload(c->currdir))
401 break;
403 gui_syncsplash(0, ID2P(LANG_WAIT));
405 /* about to create a new current playlist...
406 allow user to cancel the operation */
407 if (global_settings.warnon_erase_dynplaylist &&
408 !global_settings.party_mode &&
409 playlist_modified(NULL))
411 char *lines[]={ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
412 struct text_message message={lines, 1};
414 if(gui_syncyesno_run(&message, NULL, NULL) != YESNO_YES)
415 break;
418 if (global_settings.party_mode)
420 playlist_insert_track(NULL, buf,
421 PLAYLIST_INSERT_LAST, true, true);
422 gui_syncsplash(HZ, ID2P(LANG_QUEUE_LAST));
424 else if (playlist_create(c->currdir, NULL) != -1)
426 start_index = ft_build_playlist(c, c->selected_item);
427 if (global_settings.playlist_shuffle)
429 start_index = playlist_shuffle(seed, start_index);
431 /* when shuffling dir.: play all files
432 even if the file selected by user is
433 not the first one */
434 if (!global_settings.play_selected)
435 start_index = 0;
438 playlist_start(start_index, 0);
439 play = true;
441 break;
443 #if CONFIG_TUNER
444 /* fmr preset file */
445 case FILE_ATTR_FMR:
447 gui_syncsplash(0, ID2P(LANG_WAIT));
449 /* Preset inside the default folder. */
450 if(!strncasecmp(FMPRESET_PATH, buf, strlen(FMPRESET_PATH)))
452 set_file(buf, global_settings.fmr_file, MAX_FILENAME);
453 radio_load_presets(global_settings.fmr_file);
454 if(!in_radio_screen())
455 radio_screen();
458 * Preset outside default folder, we can choose such only
459 * if we are out of the radio screen, so the check for the
460 * radio status isn't neccessary
462 else
464 radio_load_presets(buf);
465 radio_screen();
468 break;
469 #endif
472 /* wps config file */
473 case FILE_ATTR_WPS:
474 gui_syncsplash(0, ID2P(LANG_WAIT));
475 #if LCD_DEPTH > 1
476 unload_wps_backdrop();
477 #endif
478 wps_data_load(gui_wps[0].data, &screens[0], buf, true);
479 set_file(buf, (char *)global_settings.wps_file,
480 MAX_FILENAME);
481 break;
483 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
484 /* remote-wps config file */
485 case FILE_ATTR_RWPS:
486 gui_syncsplash(0, ID2P(LANG_WAIT));
487 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
488 unload_remote_wps_backdrop();
489 #endif
490 wps_data_load(gui_wps[1].data, &screens[1], buf, true);
491 set_file(buf, (char *)global_settings.rwps_file,
492 MAX_FILENAME);
493 break;
494 #endif
496 case FILE_ATTR_CFG:
497 gui_syncsplash(0, ID2P(LANG_WAIT));
498 if (!settings_load_config(buf,true))
499 break;
500 gui_syncsplash(HZ, ID2P(LANG_SETTINGS_LOADED));
501 break;
503 case FILE_ATTR_BMARK:
504 gui_syncsplash(0, ID2P(LANG_WAIT));
505 bookmark_load(buf, false);
506 reload_dir = true;
507 break;
509 case FILE_ATTR_LNG:
510 gui_syncsplash(0, ID2P(LANG_WAIT));
511 if(!lang_load(buf)) {
512 set_file(buf, (char *)global_settings.lang_file,
513 MAX_FILENAME);
514 talk_init(); /* use voice of same language */
515 gui_syncsplash(HZ, ID2P(LANG_LANGUAGE_LOADED));
517 break;
519 #ifdef HAVE_LCD_BITMAP
520 case FILE_ATTR_FONT:
521 gui_syncsplash(0, ID2P(LANG_WAIT));
522 font_load(buf);
523 set_file(buf, (char *)global_settings.font_file, MAX_FILENAME);
524 break;
526 case FILE_ATTR_KBD:
527 gui_syncsplash(0, ID2P(LANG_WAIT));
528 if (!load_kbd(buf))
529 gui_syncsplash(HZ, ID2P(LANG_KEYBOARD_LOADED));
530 set_file(buf, (char *)global_settings.kbd_file, MAX_FILENAME);
531 break;
532 #endif
534 #ifndef SIMULATOR
535 /* firmware file */
536 case FILE_ATTR_MOD:
537 gui_syncsplash(0, ID2P(LANG_WAIT));
538 rolo_load(buf);
539 break;
540 #endif
542 /* plugin file */
543 case FILE_ATTR_ROCK:
544 if (global_settings.party_mode) {
545 gui_syncsplash(HZ, ID2P(LANG_PARTY_MODE));
546 break;
549 gui_syncsplash(0, ID2P(LANG_WAIT));
551 if (plugin_load(buf,NULL) == PLUGIN_USB_CONNECTED)
553 if(*c->dirfilter > NUM_FILTER_MODES)
554 /* leave sub-browsers after usb, doing
555 otherwise might be confusing to the user */
556 exit_func = true;
557 else
558 reload_dir = true;
560 break;
562 case FILE_ATTR_CUE:
563 display_cuesheet_content(buf);
564 break;
566 default:
568 char* plugin;
570 if (global_settings.party_mode) {
571 gui_syncsplash(HZ, ID2P(LANG_PARTY_MODE));
572 break;
575 plugin = filetype_get_plugin(file);
576 if (plugin)
578 if (plugin_load(plugin,buf) == PLUGIN_USB_CONNECTED)
579 reload_dir = true;
581 break;
585 if ( play ) {
586 /* the resume_index must always be the index in the
587 shuffled list in case shuffle is enabled */
588 global_status.resume_index = start_index;
589 global_status.resume_offset = 0;
590 status_save();
592 start_wps = true;
594 else {
595 if (*c->dirfilter > NUM_FILTER_MODES &&
596 *c->dirfilter != SHOW_FONT &&
597 *c->dirfilter != SHOW_PLUGINS)
599 exit_func = true;
604 if (reload_dir)
605 rc = 1;
606 if (start_wps)
607 rc = 2;
608 if (exit_func)
609 rc = 3;
611 return rc;
614 int ft_exit(struct tree_context* c)
616 extern char lastfile[]; /* from tree.c */
617 char buf[MAX_PATH];
618 int rc = 0;
619 bool exit_func = false;
621 int i = strlen(c->currdir);
622 if (i>1) {
623 while (c->currdir[i-1]!='/')
624 i--;
625 strcpy(buf,&c->currdir[i]);
626 if (i==1)
627 c->currdir[i]=0;
628 else
629 c->currdir[i-1]=0;
631 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
632 exit_func = true;
634 c->dirlevel--;
635 if ( c->dirlevel < MAX_DIR_LEVELS )
636 c->selected_item=c->selected_item_history[c->dirlevel];
637 else
638 c->selected_item=0;
640 /* if undefined position */
641 if (c->selected_item == -1)
642 strcpy(lastfile, buf);
644 else
646 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
647 exit_func = true;
650 if (exit_func)
651 rc = 3;
653 return rc;