summaryrefslogtreecommitdiffstats
path: root/apps/plugins/properties.c
blob: 32bc8b9150ed116470e816fc601b807d9e008638 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2006 Peter D'Hoye
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "plugin.h"
#include "lib/mul_id3.h"

#if !defined(ARRAY_SIZE)
    #define ARRAY_SIZE(x) (sizeof((x)) / sizeof((x)[0]))
#endif

struct dir_stats {
    char dirname[MAX_PATH];
    int len;
    unsigned int dir_count;
    unsigned int file_count;
    unsigned long long byte_count;
};

enum props_types {
    PROPS_FILE = 0,
    PROPS_PLAYLIST,
    PROPS_ID3,
    PROPS_MUL_ID3,
    PROPS_DIR
};

static int props_type = PROPS_FILE;

static struct mp3entry id3;
static int mul_id3_count;
static int skipped_count;

static char str_filename[MAX_PATH];
static char str_dirname[MAX_PATH];
static char str_size[64];
static char str_dircount[64];
static char str_filecount[64];
static char str_date[64];
static char str_time[64];

static unsigned long nsize;
static int32_t size_unit;
static struct tm tm;

#define NUM_FILE_PROPERTIES 5
#define NUM_PLAYLIST_PROPERTIES 1 + NUM_FILE_PROPERTIES
static const unsigned char* const props_file[] =
{
    ID2P(LANG_PROPERTIES_PATH),       str_dirname,
    ID2P(LANG_PROPERTIES_FILENAME),   str_filename,
    ID2P(LANG_PROPERTIES_SIZE),       str_size,
    ID2P(LANG_PROPERTIES_DATE),       str_date,
    ID2P(LANG_PROPERTIES_TIME),       str_time,
    ID2P(LANG_MENU_SHOW_ID3_INFO),    "...",
};

#define NUM_DIR_PROPERTIES 4
static const unsigned char* const props_dir[] =
{
    ID2P(LANG_PROPERTIES_PATH),       str_dirname,
    ID2P(LANG_PROPERTIES_SUBDIRS),    str_dircount,
    ID2P(LANG_PROPERTIES_FILES),      str_filecount,
    ID2P(LANG_PROPERTIES_SIZE),       str_size,
};

static const int32_t units[] =
{
    LANG_BYTE,
    LANG_KIBIBYTE,
    LANG_MEBIBYTE,
    LANG_GIBIBYTE
};

static unsigned human_size_log(unsigned long long size)
{
    const size_t n = sizeof(units)/sizeof(units[0]);

    unsigned i;
    /* margin set at 10K boundary: 10239 B +1 => 10 KB */
    for(i=0; i < n-1 && size >= 10*1024; i++)
        size >>= 10; /* div by 1024 */

    return i;
}

static bool file_properties(const char* selected_file)
{
    bool found = false;
    DIR* dir;
    struct dirent* entry;

    dir = rb->opendir(str_dirname);
    if (dir)
    {
        while(0 != (entry = rb->readdir(dir)))
        {
            struct dirinfo info = rb->dir_get_info(dir, entry);
            if(!rb->strcmp(entry->d_name, str_filename))
            {
                unsigned log;
                log = human_size_log((unsigned long)info.size);
                nsize = ((unsigned long)info.size) >> (log*10);
                size_unit = units[log];
                rb->snprintf(str_size, sizeof str_size, "%lu %s",
                             nsize, rb->str(size_unit));
                rb->gmtime_r(&info.mtime, &tm);
                rb->snprintf(str_date, sizeof str_date, "%04d/%02d/%02d",
                    tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
                rb->snprintf(str_time, sizeof str_time, "%02d:%02d:%02d",
                    tm.tm_hour, tm.tm_min, tm.tm_sec);

                if (!rb->mp3info(&id3, selected_file))
                    props_type = PROPS_ID3;
                found = true;
                break;
            }
        }
        rb->closedir(dir);
    }
    return found;
}

static bool _dir_properties(struct dir_stats *stats)
{
    /* recursively scan directories in search of files
       and informs the user of the progress */
    bool result;
    static long lasttick=0;
    int dirlen;
    DIR* dir;
    struct dirent* entry;

    result = true;
    dirlen = rb->strlen(stats->dirname);
    dir = rb->opendir(stats->dirname);
    if (!dir)
    {
        rb->splashf(HZ*2, "%s", stats->dirname);
        return false; /* open error */
    }

    /* walk through the directory content */
    while(result && (0 != (entry = rb->readdir(dir))))
    {
        struct dirinfo info = rb->dir_get_info(dir, entry);
        /* append name to current directory */
        rb->snprintf(stats->dirname+dirlen, stats->len-dirlen, "/%s",
                     entry->d_name);

        if (info.attribute & ATTR_DIRECTORY)
        {
            if (!rb->strcmp((char *)entry->d_name, ".") ||
                !rb->strcmp((char *)entry->d_name, ".."))
                continue; /* skip these */

            stats->dir_count++; /* new directory */
            if (*rb->current_tick - lasttick > (HZ/8))
            {
                unsigned log;
                lasttick = *rb->current_tick;
                rb->lcd_clear_display();
                rb->lcd_puts(0,0,"SCANNING...");
                rb->lcd_puts(0,1,stats->dirname);
                rb->lcd_putsf(0,2,"Directories: %d", stats->dir_count);
                rb->lcd_putsf(0,3,"Files: %d", stats->file_count);
                log = human_size_log(stats->byte_count);
                rb->lcd_putsf(0,4,"Size: %lu %s",
                              (unsigned long)(stats->byte_count >> (10*log)),
                              rb->str(units[log]));
                rb->lcd_update();
            }

             /* recursion */
            result = _dir_properties(stats);
        }
        else
        {
            stats->file_count++; /* new file */
            stats->byte_count += info.size;
        }
        if(ACTION_STD_CANCEL == rb->get_action(CONTEXT_STD,TIMEOUT_NOBLOCK))
            result = false;
        rb->yield();
    }
    rb->closedir(dir);
    return result;
}

static bool dir_properties(const char* selected_file, struct dir_stats *stats)
{
    unsigned log;

    rb->strlcpy(stats->dirname, selected_file, MAX_PATH);

#ifdef HAVE_ADJUSTABLE_CPU_FREQ
    rb->cpu_boost(true);
#endif

    if (!_dir_properties(stats))
    {
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
        rb->cpu_boost(false);
#endif
        return false;
    }

#ifdef HAVE_ADJUSTABLE_CPU_FREQ
    rb->cpu_boost(false);
#endif

    rb->strlcpy(str_dirname, selected_file, MAX_PATH);
    rb->snprintf(str_dircount, sizeof str_dircount, "%d", stats->dir_count);
    rb->snprintf(str_filecount, sizeof str_filecount, "%d", stats->file_count);
    log = human_size_log(stats->byte_count);
    nsize = (long) (stats->byte_count >> (log*10));
    size_unit = units[log];
    rb->snprintf(str_size, sizeof str_size, "%ld %s", nsize, rb->str(size_unit));
    return true;
}

static const unsigned char* p2str(const unsigned char* p)
{
    int id = P2ID(p);
    return (id != -1) ? rb->str(id) : p;
}

static const char * get_props(int selected_item, void* data,
                              char *buffer, size_t buffer_len)
{
    (void)data;
    if (PROPS_DIR == props_type)
        rb->strlcpy(buffer, selected_item >= (int)(ARRAY_SIZE(props_dir)) ? "ERROR" :
                                (char *) p2str(props_dir[selected_item]), buffer_len);
    else
        rb->strlcpy(buffer, selected_item >= (int)(ARRAY_SIZE(props_file)) ? "ERROR" :
                                (char *) p2str(props_file[selected_item]), buffer_len);

    return buffer;
}

static int speak_property_selection(int selected_item, void *data)
{
    struct dir_stats *stats = data;
    int32_t id = P2ID((props_type == PROPS_DIR ?
                       props_dir : props_file)[selected_item]);
    rb->talk_id(id, false);
    switch (id)
    {
    case LANG_PROPERTIES_PATH:
        if (str_dirname[0] == '/')
        {
            char *start = str_dirname;
            char *ptr;
            while (0 != (ptr = rb->strchr(start, '/')))
            {
                *ptr = '\0';
                rb->talk_dir_or_spell(str_dirname, NULL, true);
                *ptr = '/';
                rb->talk_id(VOICE_CHAR_SLASH, true);
                start = ptr + 1;
            }
            if (*start)
                rb->talk_dir_or_spell(str_dirname, NULL, true);
        }
        else
        {
            rb->talk_spell(str_dirname, true);
        }
        break;
    case LANG_PROPERTIES_FILENAME:
        rb->talk_file_or_spell(str_dirname, str_filename, NULL, true);
        break;
    case LANG_PROPERTIES_SIZE:
        rb->talk_number(nsize, true);
        rb->talk_id(size_unit, true);
        break;
    case LANG_PROPERTIES_DATE:
        rb->talk_date(&tm, true);
        break;
    case LANG_PROPERTIES_TIME:
        rb->talk_time(&tm, true);
        break;
    case LANG_PROPERTIES_SUBDIRS:
        rb->talk_number(stats->dir_count, true);
        break;
    case LANG_PROPERTIES_FILES:
        rb->talk_number(stats->file_count, true);
        break;
    default:
        rb->talk_spell(props_file[selected_item + 1], true);
        break;
    }
    return 0;
}

static int browse_file_or_dir(struct dir_stats *stats)
{
    struct gui_synclist properties_lists;
    int button;

    rb->gui_synclist_init(&properties_lists, &get_props, stats, false, 2, NULL);
    rb->gui_synclist_set_title(&properties_lists,
                               rb->str(props_type == PROPS_DIR ?
                                                LANG_PROPERTIES_DIRECTORY_PROPERTIES :
                                                LANG_PROPERTIES_FILE_PROPERTIES),
                               NOICON);
    rb->gui_synclist_set_icon_callback(&properties_lists, NULL);
    if (rb->global_settings->talk_menu)
        rb->gui_synclist_set_voice_callback(&properties_lists, speak_property_selection);
    rb->gui_synclist_set_nb_items(&properties_lists,
                                  2 * (props_type == PROPS_FILE ? NUM_FILE_PROPERTIES :
                                       props_type == PROPS_PLAYLIST ? 
                                       NUM_PLAYLIST_PROPERTIES : NUM_DIR_PROPERTIES));
    rb->gui_synclist_select_item(&properties_lists, 0);
    rb->gui_synclist_draw(&properties_lists);
    rb->gui_synclist_speak_item(&properties_lists);

    while(true)
    {
        button = rb->get_action(CONTEXT_LIST, HZ);
        /* HZ so the status bar redraws corectly */
        if (rb->gui_synclist_do_button(&properties_lists,&button))
            continue;
        switch(button)
        {
            case ACTION_STD_OK:
                if (props_type == PROPS_PLAYLIST &&
                      rb->gui_synclist_get_sel_pos(&properties_lists)
                        == ARRAY_SIZE(props_file) - 2)
                    return -1;
                break;
            case ACTION_STD_CANCEL:
                return 0;
            default:
                if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
                    return 1;
                break;
        }
    }
}

static bool determine_file_or_dir(void)
{
    DIR* dir;
    struct dirent* entry;

    dir = rb->opendir(str_dirname);
    if (dir)
    {
        while(0 != (entry = rb->readdir(dir)))
        {
            if(!rb->strcmp(entry->d_name, str_filename))
            {
                struct dirinfo info = rb->dir_get_info(dir, entry);
                props_type = info.attribute & ATTR_DIRECTORY ? PROPS_DIR : PROPS_FILE;
                rb->closedir(dir);
                return true;
            }
        }
        rb->closedir(dir);
    }
    return false;
}

bool mul_id3_add(const char *file_name)
{
    if (!file_name || rb->mp3info(&id3, file_name))
        skipped_count++;
    else
    {
        collect_id3(&id3, mul_id3_count == 0);
        mul_id3_count++;
    }

    return true;
}

static bool has_pl_extension(const char* filename)
{
    char *dot = rb->strrchr(filename, '.');
    return (dot && (!rb->strcasecmp(dot, ".m3u") || !rb->strcasecmp(dot, ".m3u8")));
}

/* Assemble track info from a database table or the contents of a playlist file */
static bool assemble_track_info(const char *filename)
{
    props_type = PROPS_MUL_ID3;
    mul_id3_count = skipped_count = 0;

    if (     (filename && !rb->playlist_entries_iterate(filename, NULL, &mul_id3_add))
#ifdef HAVE_TAGCACHE
         || (!filename && !rb->tagtree_subentries_do_action(&mul_id3_add))
#endif
         || mul_id3_count == 0)
        return false;
    else if (mul_id3_count > 1) /* otherwise, the retrieved id3 can be used as-is */
        finalize_id3(&id3);

    if (skipped_count > 0)
        rb->splashf(HZ*2, "Skipped %d", skipped_count);

    return true;
}

enum plugin_status plugin_start(const void* parameter)
{
    int ret;
    static struct dir_stats stats =
    {
        .len = MAX_PATH,
        .dir_count  = 0,
        .file_count  = 0,
        .byte_count  = 0,
    };

    const char *file = parameter;
    if(!parameter)
        return PLUGIN_ERROR;

#ifdef HAVE_TOUCHSCREEN
    rb->touchscreen_set_mode(rb->global_settings->touch_mode);
#endif

    if (file[0] == '/') /* single file selected */
    {
        const char* file_name = rb->strrchr(file, '/') + 1;
        int dirlen = (file_name - file);

        rb->strlcpy(str_dirname, file, dirlen + 1);
        rb->snprintf(str_filename, sizeof str_filename, "%s", file+dirlen);

        if(!determine_file_or_dir())
        {
            /* weird: we couldn't find the entry. This Should Never Happen (TM) */
            rb->splashf(0, "File/Dir not found: %s", file);
            rb->action_userabort(TIMEOUT_BLOCK);
            return PLUGIN_OK;
        }

        if (props_type == PROPS_FILE && has_pl_extension(file))
            props_type = PROPS_PLAYLIST;

        /* get the info depending on its_a_dir */
        if(!(props_type == PROPS_DIR ?
             dir_properties(file, &stats) : file_properties(file)))
        {
            /* something went wrong (to do: tell user what it was (nesting,...) */
            rb->splash(0, ID2P(LANG_PROPERTIES_FAIL));
            rb->action_userabort(TIMEOUT_BLOCK);
            return PLUGIN_OK;
        }
    }
             /* database table selected */
    else if (rb->strcmp(file, MAKE_ACT_STR(ACTIVITY_DATABASEBROWSER))
             || !assemble_track_info(NULL))
        return PLUGIN_ERROR;

    FOR_NB_SCREENS(i)
        rb->viewportmanager_theme_enable(i, true, NULL);

    if (props_type == PROPS_ID3)
        ret = rb->browse_id3(&id3, 0, 0, &tm, 1);   /* Track Info for single file */
    else if (props_type == PROPS_MUL_ID3)
        ret = rb->browse_id3(&id3, 0, 0, NULL, mul_id3_count); /* database tracks */
    else if ((ret = browse_file_or_dir(&stats)) < 0)
        ret = assemble_track_info(file) ?                      /* playlist tracks */
                rb->browse_id3(&id3, 0, 0, NULL, mul_id3_count) : -1;

    FOR_NB_SCREENS(i)
        rb->viewportmanager_theme_undo(i, false);

    switch (ret)
    {
        case 1:
            return PLUGIN_USB_CONNECTED;
        case -1:
            return PLUGIN_ERROR;
        default:
            return PLUGIN_OK;
    }
}