Update Swedish translation.
[kugel-rb.git] / apps / filetree.c
blob8649dc46cc4d88a124cf951600f20a2d86493342
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Björn 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 <stdlib.h>
22 #include <file.h>
23 #include <dir.h>
24 #include <string.h>
25 #include <kernel.h>
26 #include <lcd.h>
27 #include <debug.h>
28 #include <font.h>
29 #include <limits.h>
30 #include "bookmark.h"
31 #include "tree.h"
32 #include "settings.h"
33 #include "filetypes.h"
34 #include "talk.h"
35 #include "playlist.h"
36 #include "gwps.h"
37 #include "lang.h"
38 #include "language.h"
39 #include "screens.h"
40 #include "plugin.h"
41 #include "rolo.h"
42 #include "sprintf.h"
43 #include "splash.h"
44 #include "yesno.h"
45 #include "cuesheet.h"
46 #include "filetree.h"
47 #include "misc.h"
48 #ifdef HAVE_LCD_BITMAP
49 #include "keyboard.h"
50 #endif
52 #if CONFIG_TUNER
53 #include "radio.h"
54 #endif
56 #include "backdrop.h"
58 int ft_build_playlist(struct tree_context* c, int start_index)
60 int i;
61 int start=start_index;
63 struct entry *dircache = c->dircache;
65 for(i = 0;i < c->filesindir;i++)
67 if((dircache[i].attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
69 if (playlist_add(dircache[i].name) < 0)
70 break;
72 else
74 /* Adjust the start index when se skip non-MP3 entries */
75 if(i < start)
76 start_index--;
80 return start_index;
83 /* Start playback of a playlist, checking for bookmark autoload, modified
84 * playlists, etc., as required. Returns false if playback wasn't started,
85 * or started via bookmark autoload, true otherwise.
87 * Pointers to both the full pathname and the separated parts needed to
88 * avoid allocating yet another path buffer on the stack (and save some
89 * code; the caller typically needs to create the full pathname anyway)...
91 bool ft_play_playlist(char* pathname, char* dirname, char* filename)
93 if (global_settings.party_mode)
95 gui_syncsplash(HZ, ID2P(LANG_PARTY_MODE));
96 return false;
99 if (bookmark_autoload(pathname))
101 return false;
104 gui_syncsplash(0, ID2P(LANG_WAIT));
106 /* about to create a new current playlist...
107 allow user to cancel the operation */
108 if (!warn_on_pl_erase())
109 return false;
111 if (playlist_create(dirname, filename) != -1)
113 if (global_settings.playlist_shuffle)
115 playlist_shuffle(current_tick, -1);
118 playlist_start(0, 0);
119 return true;
122 return false;
125 /* walk a directory and check all dircache entries if a .talk file exists */
126 static void check_file_thumbnails(struct tree_context* c)
128 int i;
129 struct dirent *entry;
130 struct entry* dircache = c->dircache;
131 DIR *dir;
133 dir = opendir(c->currdir);
134 if(!dir)
135 return;
136 /* mark all files as non talking, except the .talk ones */
137 for (i=0; i < c->filesindir; i++)
139 if (dircache[i].attr & ATTR_DIRECTORY)
140 continue; /* we're not touching directories */
142 if (strcasecmp(file_thumbnail_ext,
143 &dircache[i].name[strlen(dircache[i].name)
144 - strlen(file_thumbnail_ext)]))
145 { /* no .talk file */
146 dircache[i].attr &= ~FILE_ATTR_THUMBNAIL; /* clear */
148 else
149 { /* .talk file, we later let them speak themselves */
150 dircache[i].attr |= FILE_ATTR_THUMBNAIL; /* set */
154 while((entry = readdir(dir)) != 0) /* walk directory */
156 int ext_pos;
158 ext_pos = strlen((char *)entry->d_name) - strlen(file_thumbnail_ext);
159 if (ext_pos <= 0 /* too short to carry ".talk" */
160 || (entry->attribute & ATTR_DIRECTORY) /* no file */
161 || strcasecmp((char *)&entry->d_name[ext_pos], file_thumbnail_ext))
162 { /* or doesn't end with ".talk", no candidate */
163 continue;
166 /* terminate the (disposable) name in dir buffer,
167 this truncates off the ".talk" without needing an extra buffer */
168 entry->d_name[ext_pos] = '\0';
170 /* search corresponding file in dir cache */
171 for (i=0; i < c->filesindir; i++)
173 if (!strcasecmp(dircache[i].name, (char *)entry->d_name))
174 { /* match */
175 dircache[i].attr |= FILE_ATTR_THUMBNAIL; /* set the flag */
176 break; /* exit search loop, because we found it */
180 closedir(dir);
183 /* support function for qsort() */
184 static int compare(const void* p1, const void* p2)
186 struct entry* e1 = (struct entry*)p1;
187 struct entry* e2 = (struct entry*)p2;
188 int criteria;
190 if (e1->attr & ATTR_DIRECTORY && e2->attr & ATTR_DIRECTORY)
191 { /* two directories */
192 criteria = global_settings.sort_dir;
194 #ifdef HAVE_MULTIVOLUME
195 if (e1->attr & ATTR_VOLUME || e2->attr & ATTR_VOLUME)
196 { /* a volume identifier is involved */
197 if (e1->attr & ATTR_VOLUME && e2->attr & ATTR_VOLUME)
198 criteria = 0; /* two volumes: sort alphabetically */
199 else /* only one is a volume: volume first */
200 return (e2->attr & ATTR_VOLUME) - (e1->attr & ATTR_VOLUME);
202 #endif
205 else if (!(e1->attr & ATTR_DIRECTORY) && !(e2->attr & ATTR_DIRECTORY))
206 { /* two files */
207 criteria = global_settings.sort_file;
209 else /* dir and file, dir goes first */
210 return ( e2->attr & ATTR_DIRECTORY ) - ( e1->attr & ATTR_DIRECTORY );
212 switch(criteria)
214 case 3: /* sort type */
216 int t1 = e1->attr & FILE_ATTR_MASK;
217 int t2 = e2->attr & FILE_ATTR_MASK;
219 if (!t1) /* unknown type */
220 t1 = INT_MAX; /* gets a high number, to sort after known */
221 if (!t2) /* unknown type */
222 t2 = INT_MAX; /* gets a high number, to sort after known */
224 if (t1 - t2) /* if different */
225 return t1 - t2;
226 /* else fall through to alphabetical sorting */
228 case 0: /* sort alphabetically asc */
229 if (global_settings.sort_case)
230 return strncmp(e1->name, e2->name, MAX_PATH);
231 else
232 return strncasecmp(e1->name, e2->name, MAX_PATH);
234 case 4: /* sort alphabetically desc */
235 if (global_settings.sort_case)
236 return strncmp(e2->name, e1->name, MAX_PATH);
237 else
238 return strncasecmp(e2->name, e1->name, MAX_PATH);
240 case 1: /* sort date */
241 return e1->time_write - e2->time_write;
243 case 2: /* sort date, newest first */
244 return e2->time_write - e1->time_write;
246 return 0; /* never reached */
249 /* load and sort directory into dircache. returns NULL on failure. */
250 int ft_load(struct tree_context* c, const char* tempdir)
252 int i;
253 int name_buffer_used = 0;
254 DIR *dir;
256 if (tempdir)
257 dir = opendir(tempdir);
258 else
259 dir = opendir(c->currdir);
260 if(!dir)
261 return -1; /* not a directory */
263 c->dirsindir = 0;
264 c->dirfull = false;
266 for ( i=0; i < global_settings.max_files_in_dir; i++ ) {
267 int len;
268 struct dirent *entry = readdir(dir);
269 struct entry* dptr =
270 (struct entry*)(c->dircache + i * sizeof(struct entry));
271 if (!entry)
272 break;
274 len = strlen((char *)entry->d_name);
276 /* skip directories . and .. */
277 if ((entry->attribute & ATTR_DIRECTORY) &&
278 (((len == 1) && (!strncmp((char *)entry->d_name, ".", 1))) ||
279 ((len == 2) && (!strncmp((char *)entry->d_name, "..", 2))))) {
280 i--;
281 continue;
284 /* Skip FAT volume ID */
285 if (entry->attribute & ATTR_VOLUME_ID) {
286 i--;
287 continue;
290 /* filter out dotfiles and hidden files */
291 if (*c->dirfilter != SHOW_ALL &&
292 ((entry->d_name[0]=='.') ||
293 (entry->attribute & ATTR_HIDDEN))) {
294 i--;
295 continue;
298 dptr->attr = entry->attribute;
300 /* check for known file types */
301 if ( !(dptr->attr & ATTR_DIRECTORY) )
302 dptr->attr |= filetype_get_attr((char *)entry->d_name);
304 /* filter out non-visible files */
305 if ((!(dptr->attr & ATTR_DIRECTORY) && (
306 (*c->dirfilter == SHOW_PLAYLIST &&
307 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) ||
308 ((*c->dirfilter == SHOW_MUSIC &&
309 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO) &&
310 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) ||
311 (*c->dirfilter == SHOW_SUPPORTED && !filetype_supported(dptr->attr)))) ||
312 (*c->dirfilter == SHOW_WPS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_WPS) ||
313 #ifdef HAVE_REMOTE_LCD
314 (*c->dirfilter == SHOW_RWPS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_RWPS) ||
315 #endif
316 #if CONFIG_TUNER
317 (*c->dirfilter == SHOW_FMR && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_FMR) ||
318 #endif
319 (*c->dirfilter == SHOW_CFG && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_CFG) ||
320 (*c->dirfilter == SHOW_LNG && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_LNG) ||
321 (*c->dirfilter == SHOW_MOD && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_MOD) ||
322 (*c->dirfilter == SHOW_FONT && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_FONT) ||
323 (*c->dirfilter == SHOW_PLUGINS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_ROCK))
325 i--;
326 continue;
329 if (len > c->name_buffer_size - name_buffer_used - 1) {
330 /* Tell the world that we ran out of buffer space */
331 c->dirfull = true;
332 break;
334 dptr->name = &c->name_buffer[name_buffer_used];
335 dptr->time_write =
336 (long)entry->wrtdate<<16 |
337 (long)entry->wrttime; /* in one # */
338 strcpy(dptr->name, (char *)entry->d_name);
339 name_buffer_used += len + 1;
341 if (dptr->attr & ATTR_DIRECTORY) /* count the remaining dirs */
342 c->dirsindir++;
344 c->filesindir = i;
345 c->dirlength = i;
346 closedir(dir);
348 qsort(c->dircache,i,sizeof(struct entry),compare);
350 /* If thumbnail talking is enabled, make an extra run to mark files with
351 associated thumbnails, so we don't do unsuccessful spinups later. */
352 if (global_settings.talk_file_clip)
353 check_file_thumbnails(c); /* map .talk to ours */
355 return 0;
358 int ft_enter(struct tree_context* c)
360 int rc = 0;
361 char buf[MAX_PATH];
362 struct entry *dircache = c->dircache;
363 struct entry* file = &dircache[c->selected_item];
364 bool reload_dir = false;
365 bool start_wps = false;
366 bool exit_func = false;
368 if (c->currdir[1])
369 snprintf(buf,sizeof(buf),"%s/%s",c->currdir, file->name);
370 else
371 snprintf(buf,sizeof(buf),"/%s",file->name);
373 if (file->attr & ATTR_DIRECTORY) {
374 memcpy(c->currdir, buf, sizeof(c->currdir));
375 if ( c->dirlevel < MAX_DIR_LEVELS )
376 c->selected_item_history[c->dirlevel] = c->selected_item;
377 c->dirlevel++;
378 c->selected_item=0;
380 else {
381 int seed = current_tick;
382 bool play = false;
383 int start_index=0;
385 switch ( file->attr & FILE_ATTR_MASK ) {
386 case FILE_ATTR_M3U:
387 play = ft_play_playlist(buf, c->currdir, file->name);
389 if (play)
391 start_index = 0;
394 break;
396 case FILE_ATTR_AUDIO:
397 if (bookmark_autoload(c->currdir))
398 break;
400 gui_syncsplash(0, ID2P(LANG_WAIT));
402 /* about to create a new current playlist...
403 allow user to cancel the operation */
404 if (!warn_on_pl_erase())
405 break;
407 if (global_settings.party_mode)
409 playlist_insert_track(NULL, buf,
410 PLAYLIST_INSERT_LAST, true, true);
411 gui_syncsplash(HZ, ID2P(LANG_QUEUE_LAST));
413 else if (playlist_create(c->currdir, NULL) != -1)
415 start_index = ft_build_playlist(c, c->selected_item);
416 if (global_settings.playlist_shuffle)
418 start_index = playlist_shuffle(seed, start_index);
420 /* when shuffling dir.: play all files
421 even if the file selected by user is
422 not the first one */
423 if (!global_settings.play_selected)
424 start_index = 0;
427 playlist_start(start_index, 0);
428 play = true;
430 break;
432 #if CONFIG_TUNER
433 /* fmr preset file */
434 case FILE_ATTR_FMR:
436 gui_syncsplash(0, ID2P(LANG_WAIT));
438 /* Preset inside the default folder. */
439 if(!strncasecmp(FMPRESET_PATH, buf, strlen(FMPRESET_PATH)))
441 set_file(buf, global_settings.fmr_file, MAX_FILENAME);
442 radio_load_presets(global_settings.fmr_file);
443 if(!in_radio_screen())
444 radio_screen();
447 * Preset outside default folder, we can choose such only
448 * if we are out of the radio screen, so the check for the
449 * radio status isn't neccessary
451 else
453 radio_load_presets(buf);
454 radio_screen();
457 break;
458 #endif
461 /* wps config file */
462 case FILE_ATTR_WPS:
463 gui_syncsplash(0, ID2P(LANG_WAIT));
464 #if LCD_DEPTH > 1
465 unload_wps_backdrop();
466 #endif
467 wps_data_load(gui_wps[0].data, &screens[0], buf, true);
468 set_file(buf, (char *)global_settings.wps_file,
469 MAX_FILENAME);
470 break;
472 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
473 /* remote-wps config file */
474 case FILE_ATTR_RWPS:
475 gui_syncsplash(0, ID2P(LANG_WAIT));
476 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
477 unload_remote_wps_backdrop();
478 #endif
479 wps_data_load(gui_wps[1].data, &screens[1], buf, true);
480 set_file(buf, (char *)global_settings.rwps_file,
481 MAX_FILENAME);
482 break;
483 #endif
485 case FILE_ATTR_CFG:
486 gui_syncsplash(0, ID2P(LANG_WAIT));
487 if (!settings_load_config(buf,true))
488 break;
489 gui_syncsplash(HZ, ID2P(LANG_SETTINGS_LOADED));
490 break;
492 case FILE_ATTR_BMARK:
493 gui_syncsplash(0, ID2P(LANG_WAIT));
494 bookmark_load(buf, false);
495 reload_dir = true;
496 break;
498 case FILE_ATTR_LNG:
499 gui_syncsplash(0, ID2P(LANG_WAIT));
500 if(!lang_load(buf)) {
501 set_file(buf, (char *)global_settings.lang_file,
502 MAX_FILENAME);
503 talk_init(); /* use voice of same language */
504 gui_syncsplash(HZ, ID2P(LANG_LANGUAGE_LOADED));
506 break;
508 #ifdef HAVE_LCD_BITMAP
509 case FILE_ATTR_FONT:
510 gui_syncsplash(0, ID2P(LANG_WAIT));
511 font_load(buf);
512 set_file(buf, (char *)global_settings.font_file, MAX_FILENAME);
513 break;
515 case FILE_ATTR_KBD:
516 gui_syncsplash(0, ID2P(LANG_WAIT));
517 if (!load_kbd(buf))
518 gui_syncsplash(HZ, ID2P(LANG_KEYBOARD_LOADED));
519 set_file(buf, (char *)global_settings.kbd_file, MAX_FILENAME);
520 break;
521 #endif
523 #ifndef SIMULATOR
524 /* firmware file */
525 case FILE_ATTR_MOD:
526 gui_syncsplash(0, ID2P(LANG_WAIT));
527 rolo_load(buf);
528 break;
529 #endif
531 /* plugin file */
532 case FILE_ATTR_ROCK:
533 if (global_settings.party_mode) {
534 gui_syncsplash(HZ, ID2P(LANG_PARTY_MODE));
535 break;
538 gui_syncsplash(0, ID2P(LANG_WAIT));
540 if (plugin_load(buf,NULL) == PLUGIN_USB_CONNECTED)
542 if(*c->dirfilter > NUM_FILTER_MODES)
543 /* leave sub-browsers after usb, doing
544 otherwise might be confusing to the user */
545 exit_func = true;
546 else
547 reload_dir = true;
549 break;
551 case FILE_ATTR_CUE:
552 display_cuesheet_content(buf);
553 break;
555 default:
557 const char* plugin;
559 if (global_settings.party_mode) {
560 gui_syncsplash(HZ, ID2P(LANG_PARTY_MODE));
561 break;
564 plugin = filetype_get_plugin(file);
565 if (plugin)
567 if (plugin_load(plugin,buf) == PLUGIN_USB_CONNECTED)
568 reload_dir = true;
570 break;
574 if ( play ) {
575 /* the resume_index must always be the index in the
576 shuffled list in case shuffle is enabled */
577 global_status.resume_index = start_index;
578 global_status.resume_offset = 0;
579 status_save();
581 start_wps = true;
583 else {
584 if (*c->dirfilter > NUM_FILTER_MODES &&
585 *c->dirfilter != SHOW_FONT &&
586 *c->dirfilter != SHOW_PLUGINS)
588 exit_func = true;
593 if (reload_dir)
594 rc = 1;
595 if (start_wps)
596 rc = 2;
597 if (exit_func)
598 rc = 3;
600 return rc;
603 int ft_exit(struct tree_context* c)
605 extern char lastfile[]; /* from tree.c */
606 char buf[MAX_PATH];
607 int rc = 0;
608 bool exit_func = false;
610 int i = strlen(c->currdir);
611 if (i>1) {
612 while (c->currdir[i-1]!='/')
613 i--;
614 strcpy(buf,&c->currdir[i]);
615 if (i==1)
616 c->currdir[i]=0;
617 else
618 c->currdir[i-1]=0;
620 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
621 exit_func = true;
623 c->dirlevel--;
624 if ( c->dirlevel < MAX_DIR_LEVELS )
625 c->selected_item=c->selected_item_history[c->dirlevel];
626 else
627 c->selected_item=0;
629 /* if undefined position */
630 if (c->selected_item == -1)
631 strcpy(lastfile, buf);
633 else
635 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
636 exit_func = true;
639 if (exit_func)
640 rc = 3;
642 return rc;