Check if a new version got installed after usb disconnect and ask if user wants to...
[maemo-rb.git] / apps / filetree.c
blob4c487107979d5208b7448480b36b6ac99a056216
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 "dircache.h"
42 #include "splash.h"
43 #include "yesno.h"
44 #include "cuesheet.h"
45 #ifdef HAVE_LCD_BITMAP
46 #include "keyboard.h"
47 #endif
49 #if CONFIG_TUNER
50 #include "radio.h"
51 #endif
53 #if LCD_DEPTH > 1
54 #include "backdrop.h"
55 #endif
57 int ft_build_playlist(struct tree_context* c, int start_index)
59 int i;
60 int start=start_index;
62 struct entry *dircache = c->dircache;
64 for(i = 0;i < c->filesindir;i++)
66 if((dircache[i].attr & TREE_ATTR_MASK) == TREE_ATTR_MPA)
68 DEBUGF("Adding %s\n", dircache[i].name);
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 /* walk a directory and check all dircache entries if a .talk file exists */
84 static void check_file_thumbnails(struct tree_context* c)
86 int i;
87 struct dircache_entry *entry;
88 struct entry* dircache = c->dircache;
89 DIRCACHED *dir;
91 dir = opendir_cached(c->currdir);
92 if(!dir)
93 return;
94 /* mark all files as non talking, except the .talk ones */
95 for (i=0; i < c->filesindir; i++)
97 if (dircache[i].attr & ATTR_DIRECTORY)
98 continue; /* we're not touching directories */
100 if (strcasecmp(file_thumbnail_ext,
101 &dircache[i].name[strlen(dircache[i].name)
102 - strlen(file_thumbnail_ext)]))
103 { /* no .talk file */
104 dircache[i].attr &= ~TREE_ATTR_THUMBNAIL; /* clear */
106 else
107 { /* .talk file, we later let them speak themselves */
108 dircache[i].attr |= TREE_ATTR_THUMBNAIL; /* set */
112 while((entry = readdir_cached(dir)) != 0) /* walk directory */
114 int ext_pos;
116 ext_pos = strlen((char *)entry->d_name) - strlen(file_thumbnail_ext);
117 if (ext_pos <= 0 /* too short to carry ".talk" */
118 || (entry->attribute & ATTR_DIRECTORY) /* no file */
119 || strcasecmp((char *)&entry->d_name[ext_pos], file_thumbnail_ext))
120 { /* or doesn't end with ".talk", no candidate */
121 continue;
124 /* terminate the (disposable) name in dir buffer,
125 this truncates off the ".talk" without needing an extra buffer */
126 entry->d_name[ext_pos] = '\0';
128 /* search corresponding file in dir cache */
129 for (i=0; i < c->filesindir; i++)
131 if (!strcasecmp(dircache[i].name, (char *)entry->d_name))
132 { /* match */
133 dircache[i].attr |= TREE_ATTR_THUMBNAIL; /* set the flag */
134 break; /* exit search loop, because we found it */
138 closedir_cached(dir);
141 /* support function for qsort() */
142 static int compare(const void* p1, const void* p2)
144 struct entry* e1 = (struct entry*)p1;
145 struct entry* e2 = (struct entry*)p2;
146 int criteria;
148 if (e1->attr & ATTR_DIRECTORY && e2->attr & ATTR_DIRECTORY)
149 { /* two directories */
150 criteria = global_settings.sort_dir;
152 #ifdef HAVE_MULTIVOLUME
153 if (e1->attr & ATTR_VOLUME || e2->attr & ATTR_VOLUME)
154 { /* a volume identifier is involved */
155 if (e1->attr & ATTR_VOLUME && e2->attr & ATTR_VOLUME)
156 criteria = 0; /* two volumes: sort alphabetically */
157 else /* only one is a volume: volume first */
158 return (e2->attr & ATTR_VOLUME) - (e1->attr & ATTR_VOLUME);
160 #endif
163 else if (!(e1->attr & ATTR_DIRECTORY) && !(e2->attr & ATTR_DIRECTORY))
164 { /* two files */
165 criteria = global_settings.sort_file;
167 else /* dir and file, dir goes first */
168 return ( e2->attr & ATTR_DIRECTORY ) - ( e1->attr & ATTR_DIRECTORY );
170 switch(criteria)
172 case 3: /* sort type */
174 int t1 = e1->attr & TREE_ATTR_MASK;
175 int t2 = e2->attr & TREE_ATTR_MASK;
177 if (!t1) /* unknown type */
178 t1 = INT_MAX; /* gets a high number, to sort after known */
179 if (!t2) /* unknown type */
180 t2 = INT_MAX; /* gets a high number, to sort after known */
182 if (t1 - t2) /* if different */
183 return t1 - t2;
184 /* else fall through to alphabetical sorting */
186 case 0: /* sort alphabetically asc */
187 if (global_settings.sort_case)
188 return strncmp(e1->name, e2->name, MAX_PATH);
189 else
190 return strncasecmp(e1->name, e2->name, MAX_PATH);
192 case 4: /* sort alphabetically desc */
193 if (global_settings.sort_case)
194 return strncmp(e2->name, e1->name, MAX_PATH);
195 else
196 return strncasecmp(e2->name, e1->name, MAX_PATH);
198 case 1: /* sort date */
199 return e1->time_write - e2->time_write;
201 case 2: /* sort date, newest first */
202 return e2->time_write - e1->time_write;
204 return 0; /* never reached */
207 /* load and sort directory into dircache. returns NULL on failure. */
208 int ft_load(struct tree_context* c, const char* tempdir)
210 int i;
211 int name_buffer_used = 0;
212 DIRCACHED *dir;
214 if (tempdir)
215 dir = opendir_cached(tempdir);
216 else
217 dir = opendir_cached(c->currdir);
218 if(!dir)
219 return -1; /* not a directory */
221 c->dirsindir = 0;
222 c->dirfull = false;
224 for ( i=0; i < global_settings.max_files_in_dir; i++ ) {
225 int len;
226 struct dircache_entry *entry = readdir_cached(dir);
227 struct entry* dptr =
228 (struct entry*)(c->dircache + i * sizeof(struct entry));
229 if (!entry)
230 break;
232 len = strlen((char *)entry->d_name);
234 /* skip directories . and .. */
235 if ((entry->attribute & ATTR_DIRECTORY) &&
236 (((len == 1) && (!strncmp((char *)entry->d_name, ".", 1))) ||
237 ((len == 2) && (!strncmp((char *)entry->d_name, "..", 2))))) {
238 i--;
239 continue;
242 /* Skip FAT volume ID */
243 if (entry->attribute & ATTR_VOLUME_ID) {
244 i--;
245 continue;
248 /* filter out dotfiles and hidden files */
249 if (*c->dirfilter != SHOW_ALL &&
250 ((entry->d_name[0]=='.') ||
251 (entry->attribute & ATTR_HIDDEN))) {
252 i--;
253 continue;
256 dptr->attr = entry->attribute;
258 /* check for known file types */
259 if ( !(dptr->attr & ATTR_DIRECTORY) )
260 dptr->attr |= filetype_get_attr((char *)entry->d_name);
262 /* filter out non-visible files */
263 if ((!(dptr->attr & ATTR_DIRECTORY) && (
264 (*c->dirfilter == SHOW_PLAYLIST &&
265 (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_M3U) ||
266 ((*c->dirfilter == SHOW_MUSIC &&
267 (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_MPA) &&
268 (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_M3U) ||
269 (*c->dirfilter == SHOW_SUPPORTED && !filetype_supported(dptr->attr)))) ||
270 (*c->dirfilter == SHOW_WPS && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_WPS) ||
271 #ifdef HAVE_REMOTE_LCD
272 (*c->dirfilter == SHOW_RWPS && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_RWPS) ||
273 #endif
274 #if CONFIG_TUNER
275 (*c->dirfilter == SHOW_FMR && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_FMR) ||
276 #endif
277 (*c->dirfilter == SHOW_CFG && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_CFG) ||
278 (*c->dirfilter == SHOW_LNG && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_LNG) ||
279 (*c->dirfilter == SHOW_MOD && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_MOD) ||
280 (*c->dirfilter == SHOW_FONT && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_FONT) ||
281 (*c->dirfilter == SHOW_PLUGINS && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_ROCK))
283 i--;
284 continue;
287 if (len > c->name_buffer_size - name_buffer_used - 1) {
288 /* Tell the world that we ran out of buffer space */
289 c->dirfull = true;
290 break;
292 dptr->name = &c->name_buffer[name_buffer_used];
293 dptr->time_write =
294 (long)entry->wrtdate<<16 |
295 (long)entry->wrttime; /* in one # */
296 strcpy(dptr->name, (char *)entry->d_name);
297 name_buffer_used += len + 1;
299 if (dptr->attr & ATTR_DIRECTORY) /* count the remaining dirs */
300 c->dirsindir++;
302 c->filesindir = i;
303 c->dirlength = i;
304 closedir_cached(dir);
306 qsort(c->dircache,i,sizeof(struct entry),compare);
308 /* If thumbnail talking is enabled, make an extra run to mark files with
309 associated thumbnails, so we don't do unsuccessful spinups later. */
310 if (global_settings.talk_file == 3)
311 check_file_thumbnails(c); /* map .talk to ours */
313 return 0;
316 int ft_enter(struct tree_context* c)
318 int rc = 0;
319 char buf[MAX_PATH];
320 struct entry *dircache = c->dircache;
321 struct entry* file = &dircache[c->selected_item];
322 bool reload_dir = false;
323 bool start_wps = false;
324 bool exit_func = false;
326 if (c->currdir[1])
327 snprintf(buf,sizeof(buf),"%s/%s",c->currdir, file->name);
328 else
329 snprintf(buf,sizeof(buf),"/%s",file->name);
331 if (file->attr & ATTR_DIRECTORY) {
332 memcpy(c->currdir, buf, sizeof(c->currdir));
333 if ( c->dirlevel < MAX_DIR_LEVELS )
334 c->selected_item_history[c->dirlevel] = c->selected_item;
335 c->dirlevel++;
336 c->selected_item=0;
338 else {
339 int seed = current_tick;
340 bool play = false;
341 int start_index=0;
343 switch ( file->attr & TREE_ATTR_MASK ) {
344 case TREE_ATTR_M3U:
345 if (global_settings.party_mode) {
346 gui_syncsplash(HZ, str(LANG_PARTY_MODE));
347 break;
350 if (bookmark_autoload(buf))
351 break;
353 gui_syncsplash(0, str(LANG_WAIT));
355 /* about to create a new current playlist...
356 allow user to cancel the operation */
357 if (global_settings.warnon_erase_dynplaylist &&
358 playlist_modified(NULL))
360 char *lines[]={str(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
361 struct text_message message={lines, 1};
363 if(gui_syncyesno_run(&message, NULL, NULL) != YESNO_YES)
364 break;
367 if (playlist_create(c->currdir, file->name) != -1)
369 if (global_settings.playlist_shuffle)
370 playlist_shuffle(seed, -1);
371 start_index = 0;
372 playlist_start(start_index,0);
373 play = true;
375 break;
377 case TREE_ATTR_MPA:
378 if (bookmark_autoload(c->currdir))
379 break;
381 gui_syncsplash(0, str(LANG_WAIT));
383 /* about to create a new current playlist...
384 allow user to cancel the operation */
385 if (global_settings.warnon_erase_dynplaylist &&
386 !global_settings.party_mode &&
387 playlist_modified(NULL))
389 char *lines[]={str(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
390 struct text_message message={lines, 1};
392 if(gui_syncyesno_run(&message, NULL, NULL) != YESNO_YES)
393 break;
396 if (global_settings.party_mode)
398 playlist_insert_track(NULL, buf,
399 PLAYLIST_INSERT_LAST, true, true);
400 gui_syncsplash(HZ, str(LANG_QUEUE_LAST));
402 else if (playlist_create(c->currdir, NULL) != -1)
404 start_index = ft_build_playlist(c, c->selected_item);
405 if (global_settings.playlist_shuffle)
407 start_index = playlist_shuffle(seed, start_index);
409 /* when shuffling dir.: play all files
410 even if the file selected by user is
411 not the first one */
412 if (!global_settings.play_selected)
413 start_index = 0;
416 playlist_start(start_index, 0);
417 play = true;
419 break;
421 #if CONFIG_TUNER
422 /* fmr preset file */
423 case TREE_ATTR_FMR:
425 gui_syncsplash(0, str(LANG_WAIT));
427 /* Preset inside the default folder. */
428 if(!strncasecmp(FMPRESET_PATH, buf, strlen(FMPRESET_PATH)))
430 set_file(buf, global_settings.fmr_file, MAX_FILENAME);
431 radio_load_presets(global_settings.fmr_file);
432 if(!in_radio_screen())
433 radio_screen();
436 * Preset outside default folder, we can choose such only
437 * if we are out of the radio screen, so the check for the
438 * radio status isn't neccessary
440 else
442 radio_load_presets(buf);
443 radio_screen();
446 break;
447 #endif
450 /* wps config file */
451 case TREE_ATTR_WPS:
452 gui_syncsplash(0, str(LANG_WAIT));
453 #if LCD_DEPTH > 1
454 unload_wps_backdrop();
455 #endif
456 wps_data_load(gui_wps[0].data, buf, true);
457 set_file(buf, (char *)global_settings.wps_file,
458 MAX_FILENAME);
459 break;
461 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
462 /* remote-wps config file */
463 case TREE_ATTR_RWPS:
464 gui_syncsplash(0, str(LANG_WAIT));
465 wps_data_load(gui_wps[1].data, buf, true);
466 set_file(buf, (char *)global_settings.rwps_file,
467 MAX_FILENAME);
468 break;
469 #endif
471 case TREE_ATTR_CFG:
472 gui_syncsplash(0, str(LANG_WAIT));
473 if (!settings_load_config(buf,true))
474 break;
475 gui_syncsplash(HZ, str(LANG_SETTINGS_LOADED));
476 break;
478 case TREE_ATTR_BMARK:
479 gui_syncsplash(0, str(LANG_WAIT));
480 bookmark_load(buf, false);
481 reload_dir = true;
482 break;
484 case TREE_ATTR_LNG:
485 gui_syncsplash(0, str(LANG_WAIT));
486 if(!lang_load(buf)) {
487 set_file(buf, (char *)global_settings.lang_file,
488 MAX_FILENAME);
489 talk_init(); /* use voice of same language */
490 gui_syncsplash(HZ, str(LANG_LANGUAGE_LOADED));
492 break;
494 #ifdef HAVE_LCD_BITMAP
495 case TREE_ATTR_FONT:
496 gui_syncsplash(0, str(LANG_WAIT));
497 font_load(buf);
498 set_file(buf, (char *)global_settings.font_file, MAX_FILENAME);
499 break;
501 case TREE_ATTR_KBD:
502 gui_syncsplash(0, str(LANG_WAIT));
503 if (!load_kbd(buf))
504 gui_syncsplash(HZ, str(LANG_KEYBOARD_LOADED));
505 set_file(buf, (char *)global_settings.kbd_file, MAX_FILENAME);
506 break;
507 #endif
509 #ifndef SIMULATOR
510 /* firmware file */
511 case TREE_ATTR_MOD:
512 gui_syncsplash(0, str(LANG_WAIT));
513 rolo_load(buf);
514 break;
515 #endif
517 /* plugin file */
518 case TREE_ATTR_ROCK:
519 if (global_settings.party_mode) {
520 gui_syncsplash(HZ, str(LANG_PARTY_MODE));
521 break;
524 gui_syncsplash(0, str(LANG_WAIT));
526 if (plugin_load(buf,NULL) == PLUGIN_USB_CONNECTED)
528 if(*c->dirfilter > NUM_FILTER_MODES)
529 /* leave sub-browsers after usb, doing
530 otherwise might be confusing to the user */
531 exit_func = true;
532 else
533 reload_dir = true;
535 break;
537 case TREE_ATTR_CUE:
538 display_cuesheet_content(buf);
539 break;
541 default:
543 char* plugin;
545 if (global_settings.party_mode) {
546 gui_syncsplash(HZ, str(LANG_PARTY_MODE));
547 break;
550 plugin = filetype_get_plugin(file);
551 if (plugin)
553 if (plugin_load(plugin,buf) == PLUGIN_USB_CONNECTED)
554 reload_dir = true;
556 break;
560 if ( play ) {
561 /* the resume_index must always be the index in the
562 shuffled list in case shuffle is enabled */
563 global_status.resume_index = start_index;
564 global_status.resume_offset = 0;
565 status_save();
567 start_wps = true;
569 else {
570 if (*c->dirfilter > NUM_FILTER_MODES &&
571 *c->dirfilter != SHOW_FONT &&
572 *c->dirfilter != SHOW_PLUGINS)
574 exit_func = true;
579 if (reload_dir)
580 rc = 1;
581 if (start_wps)
582 rc = 2;
583 if (exit_func)
584 rc = 3;
586 return rc;
589 int ft_exit(struct tree_context* c)
591 extern char lastfile[]; /* from tree.c */
592 char buf[MAX_PATH];
593 int rc = 0;
594 bool exit_func = false;
596 int i = strlen(c->currdir);
597 if (i>1) {
598 while (c->currdir[i-1]!='/')
599 i--;
600 strcpy(buf,&c->currdir[i]);
601 if (i==1)
602 c->currdir[i]=0;
603 else
604 c->currdir[i-1]=0;
606 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
607 exit_func = true;
609 c->dirlevel--;
610 if ( c->dirlevel < MAX_DIR_LEVELS )
611 c->selected_item=c->selected_item_history[c->dirlevel];
612 else
613 c->selected_item=0;
615 /* if undefined position */
616 if (c->selected_item == -1)
617 strcpy(lastfile, buf);
619 else
621 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
622 exit_func = true;
625 if (exit_func)
626 rc = 3;
628 return rc;