Added support for very large tables in ID3 database.
[kugel-rb.git] / apps / filetree.c
bloba46ffbb2bc54444c147add9c42eed8ec2581c4d0
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 "bookmark.h"
28 #include "tree.h"
29 #include "settings.h"
30 #include "filetypes.h"
31 #include "talk.h"
32 #include "playlist.h"
33 #include "wps-display.h"
34 #include "lang.h"
35 #include "language.h"
36 #include "screens.h"
37 #include "plugin.h"
38 #include "rolo.h"
39 #include "sprintf.h"
41 static int boot_size = 0;
42 static int boot_cluster;
43 extern bool boot_changed;
45 int ft_build_playlist(struct tree_context* c, int start_index)
47 int i;
48 int start=start_index;
50 struct entry *dircache = c->dircache;
52 for(i = 0;i < c->filesindir;i++)
54 if((dircache[i].attr & TREE_ATTR_MASK) == TREE_ATTR_MPA)
56 DEBUGF("Adding %s\n", dircache[i].name);
57 if (playlist_add(dircache[i].name) < 0)
58 break;
60 else
62 /* Adjust the start index when se skip non-MP3 entries */
63 if(i < start)
64 start_index--;
68 return start_index;
71 /* walk a directory and check all dircache entries if a .talk file exists */
72 static void check_file_thumbnails(struct tree_context* c)
74 int i;
75 struct dirent *entry;
76 struct entry* dircache = c->dircache;
77 DIR *dir;
79 dir = opendir(c->currdir);
80 if(!dir)
81 return;
83 for (i=0; i < c->filesindir; i++) /* mark all files as non talking, except the .talk ones */
85 if (dircache[i].attr & ATTR_DIRECTORY)
86 continue; /* we're not touching directories */
88 if (strcasecmp(file_thumbnail_ext,
89 &dircache[i].name[strlen(dircache[i].name)
90 - strlen(file_thumbnail_ext)]))
91 { /* no .talk file */
92 dircache[i].attr &= ~TREE_ATTR_THUMBNAIL; /* clear */
94 else
95 { /* .talk file, we later let them speak themselves */
96 dircache[i].attr |= TREE_ATTR_THUMBNAIL; /* set */
100 while((entry = readdir(dir)) != 0) /* walk directory */
102 int ext_pos;
104 ext_pos = strlen(entry->d_name) - strlen(file_thumbnail_ext);
105 if (ext_pos <= 0 /* too short to carry ".talk" */
106 || (entry->attribute & ATTR_DIRECTORY) /* no file */
107 || strcasecmp(&entry->d_name[ext_pos], file_thumbnail_ext))
108 { /* or doesn't end with ".talk", no candidate */
109 continue;
112 /* terminate the (disposable) name in dir buffer,
113 this truncates off the ".talk" without needing an extra buffer */
114 entry->d_name[ext_pos] = '\0';
116 /* search corresponding file in dir cache */
117 for (i=0; i < c->filesindir; i++)
119 if (!strcasecmp(dircache[i].name, entry->d_name))
120 { /* match */
121 dircache[i].attr |= TREE_ATTR_THUMBNAIL; /* set the flag */
122 break; /* exit search loop, because we found it */
126 closedir(dir);
129 /* support function for qsort() */
130 static int compare(const void* p1, const void* p2)
132 struct entry* e1 = (struct entry*)p1;
133 struct entry* e2 = (struct entry*)p2;
134 int criteria;
136 if (e1->attr & ATTR_DIRECTORY && e2->attr & ATTR_DIRECTORY)
137 { /* two directories */
138 criteria = global_settings.sort_dir;
140 if (e1->attr & ATTR_VOLUME || e2->attr & ATTR_VOLUME)
141 { /* a volume identifier is involved */
142 if (e1->attr & ATTR_VOLUME && e2->attr & ATTR_VOLUME)
143 criteria = 0; /* two volumes: sort alphabetically */
144 else /* only one is a volume: volume first */
145 return (e2->attr & ATTR_VOLUME) - (e1->attr & ATTR_VOLUME);
148 else if (!(e1->attr & ATTR_DIRECTORY) && !(e2->attr & ATTR_DIRECTORY))
149 { /* two files */
150 criteria = global_settings.sort_file;
152 else /* dir and file, dir goes first */
153 return ( e2->attr & ATTR_DIRECTORY ) - ( e1->attr & ATTR_DIRECTORY );
155 switch(criteria)
157 case 3: /* sort type */
159 int t1 = e1->attr & TREE_ATTR_MASK;
160 int t2 = e2->attr & TREE_ATTR_MASK;
162 if (!t1) /* unknown type */
163 t1 = 0x7FFFFFFF; /* gets a high number, to sort after known */
164 if (!t2) /* unknown type */
165 t2 = 0x7FFFFFFF; /* gets a high number, to sort after known */
167 if (t1 - t2) /* if different */
168 return t1 - t2;
169 /* else fall through to alphabetical sorting */
171 case 0: /* sort alphabetically */
172 if (global_settings.sort_case)
173 return strncmp(e1->name, e2->name, MAX_PATH);
174 else
175 return strncasecmp(e1->name, e2->name, MAX_PATH);
177 case 1: /* sort date */
178 return e1->time_write - e2->time_write;
180 case 2: /* sort date, newest first */
181 return e2->time_write - e1->time_write;
183 return 0; /* never reached */
186 /* load and sort directory into dircache. returns NULL on failure. */
187 int ft_load(struct tree_context* c)
189 int i;
190 int name_buffer_used = 0;
191 DIR *dir = opendir(c->currdir);
192 if(!dir)
193 return -1; /* not a directory */
195 c->dirsindir = 0;
196 c->dirfull = false;
198 for ( i=0; i < global_settings.max_files_in_dir; i++ ) {
199 int len;
200 struct dirent *entry = readdir(dir);
201 struct entry* dptr =
202 (struct entry*)(c->dircache + i * sizeof(struct entry));
203 if (!entry)
204 break;
206 len = strlen(entry->d_name);
208 /* skip directories . and .. */
209 if ((entry->attribute & ATTR_DIRECTORY) &&
210 (((len == 1) &&
211 (!strncmp(entry->d_name, ".", 1))) ||
212 ((len == 2) &&
213 (!strncmp(entry->d_name, "..", 2))))) {
214 i--;
215 continue;
218 /* Skip FAT volume ID */
219 if (entry->attribute & ATTR_VOLUME_ID) {
220 i--;
221 continue;
224 /* filter out dotfiles and hidden files */
225 if (*c->dirfilter != SHOW_ALL &&
226 ((entry->d_name[0]=='.') ||
227 (entry->attribute & ATTR_HIDDEN))) {
228 i--;
229 continue;
232 dptr->attr = entry->attribute;
234 /* check for known file types */
235 if ( !(dptr->attr & ATTR_DIRECTORY) && (len > 4) )
236 dptr->attr |= filetype_get_attr(entry->d_name);
238 /* memorize/compare details about the boot file */
239 if ((c->currdir[1] == 0) && !strcasecmp(entry->d_name, BOOTFILE)) {
240 if (boot_size) {
241 if ((entry->size != boot_size) ||
242 (entry->startcluster != boot_cluster))
243 boot_changed = true;
245 boot_size = entry->size;
246 boot_cluster = entry->startcluster;
249 /* filter out non-visible files */
250 if (!(dptr->attr & ATTR_DIRECTORY) && (
251 (*c->dirfilter == SHOW_PLAYLIST &&
252 (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_M3U) ||
253 ((*c->dirfilter == SHOW_MUSIC &&
254 (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_MPA) &&
255 (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_M3U) ||
256 (*c->dirfilter == SHOW_SUPPORTED && !filetype_supported(dptr->attr)) ||
257 (*c->dirfilter == SHOW_WPS && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_WPS) ||
258 (*c->dirfilter == SHOW_CFG && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_CFG) ||
259 (*c->dirfilter == SHOW_LNG && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_LNG) ||
260 (*c->dirfilter == SHOW_MOD && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_MOD) ||
261 (*c->dirfilter == SHOW_FONT && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_FONT) ||
262 (*c->dirfilter == SHOW_PLUGINS && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_ROCK)))
264 i--;
265 continue;
268 if (len > c->name_buffer_size - name_buffer_used - 1) {
269 /* Tell the world that we ran out of buffer space */
270 c->dirfull = true;
271 break;
273 dptr->name = &c->name_buffer[name_buffer_used];
274 dptr->time_write = entry->wrtdate<<16 | entry->wrttime; /* in one # */
275 strcpy(dptr->name,entry->d_name);
276 name_buffer_used += len + 1;
278 if (dptr->attr & ATTR_DIRECTORY) /* count the remaining dirs */
279 c->dirsindir++;
281 c->filesindir = i;
282 c->dirlength = i;
283 closedir(dir);
285 qsort(c->dircache,i,sizeof(struct entry),compare);
287 /* If thumbnail talking is enabled, make an extra run to mark files with
288 associated thumbnails, so we don't do unsuccessful spinups later. */
289 if (global_settings.talk_file == 3)
290 check_file_thumbnails(c); /* map .talk to ours */
292 return 0;
295 int ft_enter(struct tree_context* c)
297 int rc = 0;
298 char buf[MAX_PATH];
299 struct entry *dircache = c->dircache;
300 struct entry* file = &dircache[c->dircursor + c->dirstart];
301 bool reload_root = false;
302 bool reload_dir = false;
303 bool start_wps = false;
304 bool exit_func = false;
306 if (c->currdir[1])
307 snprintf(buf,sizeof(buf),"%s/%s",c->currdir, file->name);
308 else
309 snprintf(buf,sizeof(buf),"/%s",file->name);
311 if (file->attr & ATTR_DIRECTORY) {
312 memcpy(c->currdir, buf, sizeof(c->currdir));
313 if ( c->dirlevel < MAX_DIR_LEVELS ) {
314 c->dirpos[c->dirlevel] = c->dirstart;
315 c->cursorpos[c->dirlevel] = c->dircursor;
317 c->dirlevel++;
318 c->dircursor=0;
319 c->dirstart=0;
321 else {
322 int seed = current_tick;
323 bool play = false;
324 int start_index=0;
326 lcd_stop_scroll();
327 switch ( file->attr & TREE_ATTR_MASK ) {
328 case TREE_ATTR_M3U:
329 if (bookmark_autoload(buf))
330 break;
332 if (playlist_create(c->currdir, file->name) != -1)
334 if (global_settings.playlist_shuffle)
335 playlist_shuffle(seed, -1);
336 start_index = 0;
337 playlist_start(start_index,0);
338 play = true;
340 break;
342 case TREE_ATTR_MPA:
343 if (bookmark_autoload(c->currdir))
344 break;
346 if (playlist_create(c->currdir, NULL) != -1)
348 start_index =
349 ft_build_playlist(c, c->dircursor + c->dirstart);
350 if (global_settings.playlist_shuffle)
352 start_index = playlist_shuffle(seed, start_index);
354 /* when shuffling dir.: play all files
355 even if the file selected by user is
356 not the first one */
357 if (!global_settings.play_selected)
358 start_index = 0;
361 playlist_start(start_index, 0);
362 play = true;
364 break;
366 /* wps config file */
367 case TREE_ATTR_WPS:
368 wps_load(buf,true);
369 set_file(buf, global_settings.wps_file,
370 MAX_FILENAME);
371 break;
373 case TREE_ATTR_CFG:
374 if (!settings_load_config(buf))
375 break;
376 lcd_clear_display();
377 lcd_puts(0,0,str(LANG_SETTINGS_LOADED1));
378 lcd_puts(0,1,str(LANG_SETTINGS_LOADED2));
379 #ifdef HAVE_LCD_BITMAP
380 lcd_update();
381 #endif
382 sleep(HZ/2);
383 break;
385 case TREE_ATTR_BMARK:
386 bookmark_load(buf, false);
387 reload_dir = true;
388 break;
390 case TREE_ATTR_LNG:
391 if(!lang_load(buf)) {
392 set_file(buf, global_settings.lang_file,
393 MAX_FILENAME);
394 talk_init(); /* use voice of same language */
395 splash(HZ, true, str(LANG_LANGUAGE_LOADED));
397 break;
399 #ifdef HAVE_LCD_BITMAP
400 case TREE_ATTR_FONT:
401 font_load(buf);
402 set_file(buf, global_settings.font_file, MAX_FILENAME);
403 break;
404 #endif
406 #ifndef SIMULATOR
407 /* firmware file */
408 case TREE_ATTR_MOD:
409 rolo_load(buf);
410 break;
411 #endif
413 /* plugin file */
414 case TREE_ATTR_ROCK:
415 if (plugin_load(buf,NULL) == PLUGIN_USB_CONNECTED)
417 if(*c->dirfilter > NUM_FILTER_MODES)
418 /* leave sub-browsers after usb, doing
419 otherwise might be confusing to the user */
420 exit_func = true;
421 else
422 reload_root = true;
424 break;
426 default:
428 char* plugin = filetype_get_plugin(file);
429 if (plugin)
431 if (plugin_load(plugin,buf) == PLUGIN_USB_CONNECTED)
432 reload_root = true;
434 break;
438 if ( play ) {
439 if ( global_settings.resume ) {
440 /* the resume_index must always be the index in the
441 shuffled list in case shuffle is enabled */
442 global_settings.resume_index = start_index;
443 global_settings.resume_offset = 0;
444 settings_save();
447 start_wps = true;
449 else {
450 if (*c->dirfilter > NUM_FILTER_MODES &&
451 *c->dirfilter != SHOW_FONT &&
452 *c->dirfilter != SHOW_PLUGINS)
454 exit_func = true;
459 if (reload_dir)
460 rc = 1;
461 if (reload_root)
462 rc = 2;
463 if (start_wps)
464 rc = 3;
465 if (exit_func)
466 rc = 4;
468 return rc;
471 int ft_exit(struct tree_context* c)
473 extern char lastfile[]; /* from tree.c */
474 char buf[MAX_PATH];
475 int rc = 0;
476 bool exit_func = false;
478 int i = strlen(c->currdir);
479 if (i>1) {
480 while (c->currdir[i-1]!='/')
481 i--;
482 strcpy(buf,&c->currdir[i]);
483 if (i==1)
484 c->currdir[i]=0;
485 else
486 c->currdir[i-1]=0;
488 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
489 exit_func = true;
491 c->dirlevel--;
492 if ( c->dirlevel < MAX_DIR_LEVELS ) {
493 c->dirstart = c->dirpos[c->dirlevel];
494 c->dircursor = c->cursorpos[c->dirlevel];
496 else
497 c->dirstart = c->dircursor = 0;
499 if (c->dirstart == -1)
500 strcpy(lastfile, buf);
502 else
504 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
505 exit_func = true;
508 if (exit_func)
509 rc = 4;
511 return rc;