summaryrefslogtreecommitdiffstats
path: root/apps/filetree.c
blob: b875e81d48dfa13a72284b13b3e27d99a7bb1756 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2005 by Björn Stenberg
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include <stdlib.h>
#include <file.h>
#include <dir.h>
#include <string.h>
#include <kernel.h>
#include <lcd.h>
#include <debug.h>
#include <font.h>
#include <limits.h>
#include "bookmark.h"
#include "tree.h"
#include "settings.h"
#include "filetypes.h"
#include "talk.h"
#include "playlist.h"
#include "gwps.h"
#include "lang.h"
#include "language.h"
#include "screens.h"
#include "plugin.h"
#include "rolo.h"
#include "sprintf.h"
#include "dircache.h"
#include "splash.h"
#include "yesno.h"
#include "cuesheet.h"
#ifdef HAVE_LCD_BITMAP
#include "keyboard.h"
#endif

#if CONFIG_TUNER
#include "radio.h"
#endif

#ifndef SIMULATOR
static int boot_size = 0;
static int boot_cluster;
#endif

#if LCD_DEPTH > 1
#include "backdrop.h"
#endif

extern bool boot_changed;

int ft_build_playlist(struct tree_context* c, int start_index)
{
    int i;
    int start=start_index;

    struct entry *dircache = c->dircache;

    for(i = 0;i < c->filesindir;i++)
    {
        if((dircache[i].attr & TREE_ATTR_MASK) == TREE_ATTR_MPA)
        {
            DEBUGF("Adding %s\n", dircache[i].name);
            if (playlist_add(dircache[i].name) < 0)
                break;
        }
        else
        {
            /* Adjust the start index when se skip non-MP3 entries */
            if(i < start)
                start_index--;
        }
    }

    return start_index;
}

/* walk a directory and check all dircache entries if a .talk file exists */
static void check_file_thumbnails(struct tree_context* c)
{
    int i;
    struct dircache_entry *entry;
    struct entry* dircache = c->dircache;
    DIRCACHED *dir;

    dir = opendir_cached(c->currdir);
    if(!dir)
        return;
    /* mark all files as non talking, except the .talk ones */
    for (i=0; i < c->filesindir; i++)
    {
        if (dircache[i].attr & ATTR_DIRECTORY)
            continue; /* we're not touching directories */

        if (strcasecmp(file_thumbnail_ext,
            &dircache[i].name[strlen(dircache[i].name)
                              - strlen(file_thumbnail_ext)]))
        {   /* no .talk file */
            dircache[i].attr &= ~TREE_ATTR_THUMBNAIL; /* clear */
        }
        else
        {   /* .talk file, we later let them speak themselves */
            dircache[i].attr |= TREE_ATTR_THUMBNAIL; /* set */
        }
    }

    while((entry = readdir_cached(dir)) != 0) /* walk directory */
    {
        int ext_pos;

        ext_pos = strlen((char *)entry->d_name) - strlen(file_thumbnail_ext);
        if (ext_pos <= 0 /* too short to carry ".talk" */
            || (entry->attribute & ATTR_DIRECTORY) /* no file */
            || strcasecmp((char *)&entry->d_name[ext_pos], file_thumbnail_ext))
        {   /* or doesn't end with ".talk", no candidate */
            continue;
        }

        /* terminate the (disposable) name in dir buffer,
           this truncates off the ".talk" without needing an extra buffer */
        entry->d_name[ext_pos] = '\0';

        /* search corresponding file in dir cache */
        for (i=0; i < c->filesindir; i++)
        {
            if (!strcasecmp(dircache[i].name, (char *)entry->d_name))
            {   /* match */
                dircache[i].attr |= TREE_ATTR_THUMBNAIL; /* set the flag */
                break; /* exit search loop, because we found it */
            }
        }
    }
    closedir_cached(dir);
}

/* support function for qsort() */
static int compare(const void* p1, const void* p2)
{
    struct entry* e1 = (struct entry*)p1;
    struct entry* e2 = (struct entry*)p2;
    int criteria;

    if (e1->attr & ATTR_DIRECTORY && e2->attr & ATTR_DIRECTORY)
    {   /* two directories */
        criteria = global_settings.sort_dir;

        if (e1->attr & ATTR_VOLUME || e2->attr & ATTR_VOLUME)
        {   /* a volume identifier is involved */
            if (e1->attr & ATTR_VOLUME && e2->attr & ATTR_VOLUME)
                criteria = 0; /* two volumes: sort alphabetically */
            else /* only one is a volume: volume first */
                return (e2->attr & ATTR_VOLUME) - (e1->attr & ATTR_VOLUME);
        }
    }
    else if (!(e1->attr & ATTR_DIRECTORY) && !(e2->attr & ATTR_DIRECTORY))
    {   /* two files */
        criteria = global_settings.sort_file;
    }
    else /* dir and file, dir goes first */
        return ( e2->attr & ATTR_DIRECTORY ) - ( e1->attr & ATTR_DIRECTORY );

    switch(criteria)
    {
        case 3: /* sort type */
        {
            int t1 = e1->attr & TREE_ATTR_MASK;
            int t2 = e2->attr & TREE_ATTR_MASK;

            if (!t1) /* unknown type */
                t1 = INT_MAX; /* gets a high number, to sort after known */
            if (!t2) /* unknown type */
                t2 = INT_MAX; /* gets a high number, to sort after known */

            if (t1 - t2) /* if different */
                return t1 - t2;
            /* else fall through to alphabetical sorting */
        }
        case 0: /* sort alphabetically asc */
            if (global_settings.sort_case)
                return strncmp(e1->name, e2->name, MAX_PATH);
            else
                return strncasecmp(e1->name, e2->name, MAX_PATH);

        case 4: /* sort alphabetically desc */
            if (global_settings.sort_case)
                return strncmp(e2->name, e1->name, MAX_PATH);
            else
                return strncasecmp(e2->name, e1->name, MAX_PATH);

        case 1: /* sort date */
            return e1->time_write - e2->time_write;

        case 2: /* sort date, newest first */
            return e2->time_write - e1->time_write;
    }
    return 0; /* never reached */
}

/* load and sort directory into dircache. returns NULL on failure. */
int ft_load(struct tree_context* c, const char* tempdir)
{
    int i;
    int name_buffer_used = 0;
    DIRCACHED *dir;

    if (tempdir)
        dir = opendir_cached(tempdir);
    else
        dir = opendir_cached(c->currdir);
    if(!dir)
        return -1; /* not a directory */

    c->dirsindir = 0;
    c->dirfull = false;

    for ( i=0; i < global_settings.max_files_in_dir; i++ ) {
        int len;
        struct dircache_entry *entry = readdir_cached(dir);
        struct entry* dptr =
            (struct entry*)(c->dircache + i * sizeof(struct entry));
        if (!entry)
            break;

        len = strlen((char *)entry->d_name);

        /* skip directories . and .. */
        if ((entry->attribute & ATTR_DIRECTORY) &&
            (((len == 1) && (!strncmp((char *)entry->d_name, ".", 1))) ||
             ((len == 2) && (!strncmp((char *)entry->d_name, "..", 2))))) {
            i--;
            continue;
        }

        /* Skip FAT volume ID */
        if (entry->attribute & ATTR_VOLUME_ID) {
            i--;
            continue;
        }

        /* filter out dotfiles and hidden files */
        if (*c->dirfilter != SHOW_ALL &&
            ((entry->d_name[0]=='.') ||
            (entry->attribute & ATTR_HIDDEN))) {
            i--;
            continue;
        }

        dptr->attr = entry->attribute;

        /* check for known file types */
        if ( !(dptr->attr & ATTR_DIRECTORY) )
            dptr->attr |= filetype_get_attr((char *)entry->d_name);

#ifdef BOOTFILE
        /* memorize/compare details about the boot file */
        if ((c->currdir[1] == 0) && !strcasecmp(entry->d_name, BOOTFILE)) {
            if (boot_size) {
                if ((entry->size != boot_size) ||
                    (entry->startcluster != boot_cluster))
                    boot_changed = true;
            }
            boot_size = entry->size;
            boot_cluster = entry->startcluster;
        }
#endif

        /* filter out non-visible files */
        if ((!(dptr->attr & ATTR_DIRECTORY) && (
            (*c->dirfilter == SHOW_PLAYLIST &&
             (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_M3U) ||
            ((*c->dirfilter == SHOW_MUSIC &&
             (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_MPA) &&
             (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_M3U) ||
            (*c->dirfilter == SHOW_SUPPORTED && !filetype_supported(dptr->attr)))) ||
            (*c->dirfilter == SHOW_WPS && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_WPS) ||
#ifdef HAVE_REMOTE_LCD
            (*c->dirfilter == SHOW_RWPS && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_RWPS) ||
#endif
#if CONFIG_TUNER
            (*c->dirfilter == SHOW_FMR && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_FMR) ||
#endif
            (*c->dirfilter == SHOW_CFG && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_CFG) ||
            (*c->dirfilter == SHOW_LNG && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_LNG) ||
            (*c->dirfilter == SHOW_MOD && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_MOD) ||
            (*c->dirfilter == SHOW_FONT && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_FONT) ||
            (*c->dirfilter == SHOW_PLUGINS && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_ROCK))
        {
            i--;
            continue;
        }

        if (len > c->name_buffer_size - name_buffer_used - 1) {
            /* Tell the world that we ran out of buffer space */
            c->dirfull = true;
            break;
        }
        dptr->name = &c->name_buffer[name_buffer_used];
        dptr->time_write =
            (long)entry->wrtdate<<16 |
            (long)entry->wrttime; /* in one # */
        strcpy(dptr->name, (char *)entry->d_name);
        name_buffer_used += len + 1;

        if (dptr->attr & ATTR_DIRECTORY) /* count the remaining dirs */
            c->dirsindir++;
    }
    c->filesindir = i;
    c->dirlength = i;
    closedir_cached(dir);

    qsort(c->dircache,i,sizeof(struct entry),compare);

    /* If thumbnail talking is enabled, make an extra run to mark files with
       associated thumbnails, so we don't do unsuccessful spinups later. */
    if (global_settings.talk_file == 3)
        check_file_thumbnails(c); /* map .talk to ours */

    return 0;
}

int ft_enter(struct tree_context* c)
{
    int rc = 0;
    char buf[MAX_PATH];
    struct entry *dircache = c->dircache;
    struct entry* file = &dircache[c->selected_item];
    bool reload_dir = false;
    bool start_wps = false;
    bool exit_func = false;

    if (c->currdir[1])
        snprintf(buf,sizeof(buf),"%s/%s",c->currdir, file->name);
    else
        snprintf(buf,sizeof(buf),"/%s",file->name);

    if (file->attr & ATTR_DIRECTORY) {
        memcpy(c->currdir, buf, sizeof(c->currdir));
        if ( c->dirlevel < MAX_DIR_LEVELS )
            c->selected_item_history[c->dirlevel] = c->selected_item;
        c->dirlevel++;
        c->selected_item=0;
    }
    else {
        int seed = current_tick;
        bool play = false;
        int start_index=0;

        switch ( file->attr & TREE_ATTR_MASK ) {
            case TREE_ATTR_M3U:
                if (global_settings.party_mode) {
                    gui_syncsplash(HZ, str(LANG_PARTY_MODE));
                    break;
                }

                if (bookmark_autoload(buf))
                    break;

                gui_syncsplash(0, str(LANG_WAIT));

                /* about to create a new current playlist...
                   allow user to cancel the operation */
                if (global_settings.warnon_erase_dynplaylist &&
                    playlist_modified(NULL))
                {
                    char *lines[]={str(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
                    struct text_message message={lines, 1};

                    if(gui_syncyesno_run(&message, NULL, NULL) != YESNO_YES)
                        break;
                }

                if (playlist_create(c->currdir, file->name) != -1)
                {
                    if (global_settings.playlist_shuffle)
                        playlist_shuffle(seed, -1);
                    start_index = 0;
                    playlist_start(start_index,0);
                    play = true;
                }
                break;

            case TREE_ATTR_MPA:
                if (bookmark_autoload(c->currdir))
                    break;

                gui_syncsplash(0, str(LANG_WAIT));

                /* about to create a new current playlist...
                   allow user to cancel the operation */
                if (global_settings.warnon_erase_dynplaylist &&
                    !global_settings.party_mode &&
                    playlist_modified(NULL))
                {
                    char *lines[]={str(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
                    struct text_message message={lines, 1};

                    if(gui_syncyesno_run(&message, NULL, NULL) != YESNO_YES)
                        break;
                }

                if (global_settings.party_mode) 
                {
                    playlist_insert_track(NULL, buf,
                                          PLAYLIST_INSERT_LAST, true, true);
                    gui_syncsplash(HZ, str(LANG_QUEUE_LAST));
                }
                else if (playlist_create(c->currdir, NULL) != -1)
                {
                    start_index = ft_build_playlist(c, c->selected_item);
                    if (global_settings.playlist_shuffle)
                    {
                        start_index = playlist_shuffle(seed, start_index);

                        /* when shuffling dir.: play all files
                           even if the file selected by user is
                           not the first one */
                        if (!global_settings.play_selected)
                            start_index = 0;
                    }

                    playlist_start(start_index, 0);
                    play = true;
                }
                break;

#if CONFIG_TUNER
                /* fmr preset file */
            case TREE_ATTR_FMR:

                gui_syncsplash(0, str(LANG_WAIT));

                /* Preset inside the default folder. */
                if(!strncasecmp(FMPRESET_PATH, buf, strlen(FMPRESET_PATH)))
                {
                    set_file(buf, global_settings.fmr_file, MAX_FILENAME);
                    radio_load_presets(global_settings.fmr_file);
                    if(!in_radio_screen())
                        radio_screen();
                }
                /*
                 * Preset outside default folder, we can choose such only
                 * if we are out of the radio screen, so the check for the
                 * radio status isn't neccessary
                 */
                else
                {
                    radio_load_presets(buf);
                    radio_screen();
                }

                break;
#endif


                /* wps config file */
            case TREE_ATTR_WPS:
                gui_syncsplash(0, str(LANG_WAIT));
#if LCD_DEPTH > 1
                unload_wps_backdrop();
#endif
                wps_data_load(gui_wps[0].data, buf, true);
                set_file(buf, (char *)global_settings.wps_file,
                         MAX_FILENAME);
                break;

#if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
                /* remote-wps config file */
            case TREE_ATTR_RWPS:
                gui_syncsplash(0, str(LANG_WAIT));
                wps_data_load(gui_wps[1].data, buf, true);
                set_file(buf, (char *)global_settings.rwps_file,
                         MAX_FILENAME);
                break;
#endif

            case TREE_ATTR_CFG:
                gui_syncsplash(0, str(LANG_WAIT));
                if (!settings_load_config(buf,true))
                    break;
                gui_syncsplash(HZ, str(LANG_SETTINGS_LOADED));
                break;

            case TREE_ATTR_BMARK:
                gui_syncsplash(0, str(LANG_WAIT));
                bookmark_load(buf, false);
                reload_dir = true;
                break;

            case TREE_ATTR_LNG:
                gui_syncsplash(0, str(LANG_WAIT));
                if(!lang_load(buf)) {
                    set_file(buf, (char *)global_settings.lang_file,
                             MAX_FILENAME);
                    talk_init(); /* use voice of same language */
                    gui_syncsplash(HZ, str(LANG_LANGUAGE_LOADED));
                }
                break;

#ifdef HAVE_LCD_BITMAP
            case TREE_ATTR_FONT:
                gui_syncsplash(0, str(LANG_WAIT));
                font_load(buf);
                set_file(buf, (char *)global_settings.font_file, MAX_FILENAME);
                break;

            case TREE_ATTR_KBD:
                gui_syncsplash(0, str(LANG_WAIT));
                if (!load_kbd(buf))
                    gui_syncsplash(HZ, str(LANG_KEYBOARD_LOADED));
                set_file(buf, (char *)global_settings.kbd_file, MAX_FILENAME);
                break;
#endif

#ifndef SIMULATOR
                /* firmware file */
            case TREE_ATTR_MOD:
                gui_syncsplash(0, str(LANG_WAIT));
                rolo_load(buf);
                break;
#endif

                /* plugin file */
            case TREE_ATTR_ROCK:
                if (global_settings.party_mode) {
                    gui_syncsplash(HZ, str(LANG_PARTY_MODE));
                    break;
                }

                gui_syncsplash(0, str(LANG_WAIT));

                if (plugin_load(buf,NULL) == PLUGIN_USB_CONNECTED)
                {
                    if(*c->dirfilter > NUM_FILTER_MODES)
                        /* leave sub-browsers after usb, doing
                           otherwise might be confusing to the user */
                        exit_func = true;
                    else
                        reload_dir = true;
                }
                break;

            case TREE_ATTR_CUE:
                display_cuesheet_content(buf);
                break;

            default:
            {
                char* plugin;

                if (global_settings.party_mode) {
                    gui_syncsplash(HZ, str(LANG_PARTY_MODE));
                    break;
                }

                plugin = filetype_get_plugin(file);
                if (plugin)
                {
                    if (plugin_load(plugin,buf) == PLUGIN_USB_CONNECTED)
                        reload_dir = true;
                }
                break;
            }
        }

        if ( play ) {
            /* the resume_index must always be the index in the
               shuffled list in case shuffle is enabled */
            global_status.resume_index = start_index;
            global_status.resume_offset = 0;
            status_save();

            start_wps = true;
        }
        else {
            if (*c->dirfilter > NUM_FILTER_MODES &&
                *c->dirfilter != SHOW_FONT &&
                *c->dirfilter != SHOW_PLUGINS)
            {
                exit_func = true;
            }
        }
    }

    if (reload_dir)
        rc = 1;
    if (start_wps)
        rc = 2;
    if (exit_func)
        rc = 3;

    return rc;
}

int ft_exit(struct tree_context* c)
{
    extern char lastfile[]; /* from tree.c */
    char buf[MAX_PATH];
    int rc = 0;
    bool exit_func = false;

    int i = strlen(c->currdir);
    if (i>1) {
        while (c->currdir[i-1]!='/')
            i--;
        strcpy(buf,&c->currdir[i]);
        if (i==1)
            c->currdir[i]=0;
        else
            c->currdir[i-1]=0;

        if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
            exit_func = true;

        c->dirlevel--;
        if ( c->dirlevel < MAX_DIR_LEVELS )
            c->selected_item=c->selected_item_history[c->dirlevel];
        else
            c->selected_item=0;

        /* if undefined position */
        if (c->selected_item == -1)
            strcpy(lastfile, buf);
    }
    else
    {
        if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
            exit_func = true;
    }

    if (exit_func)
        rc = 3;

    return rc;
}