summaryrefslogtreecommitdiffstats
path: root/apps/plugins/tagcache/tagcache.c
blob: 23651a44710a85fcdb34b1bafa266c5152969769 (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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2023 by William Wilgus
 *
 * 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.
 *
 ****************************************************************************/

/*Plugin Includes*/

#include "plugin.h"
#include "errno.h"

/* Redefinitons of ANSI C functions. */
#include "lib/wrappers.h"
#include "lib/helper.h"

static void thread_create(void);
static void thread(void); /* the thread running commit*/
static void allocate_tempbuf(void);
static void free_tempbuf(void);
static bool do_timed_yield(void);
static void _log(const char *fmt, ...);
static bool logdump(bool append);
/*Aliases*/
#if 0
#ifdef ROCKBOX_HAS_LOGF
    #define logf         rb->logf
#else
    #define logf(...)   {}
#endif
#endif

#define logf _log
#define sleep rb->sleep
#define qsort rb->qsort

#define write(x,y,z)   rb->write(x,y,z)
#define ftruncate rb->ftruncate
#define remove rb->remove
#define rename rb->rename

#define vsnprintf    rb->vsnprintf
#define mkdir        rb->mkdir
#define filesize     rb->filesize

#define strtok_r     rb->strtok_r
#define strncasecmp  rb->strncasecmp
#define strcasecmp   rb->strcasecmp

#define current_tick (*rb->current_tick)
#define crc_32(x,y,z)   rb->crc_32(x,y,z)
#define plugin_get_buffer rb->plugin_get_buffer

#define MAX_LOG_SIZE 16384

#define EV_EXIT        MAKE_SYS_EVENT(SYS_EVENT_CLS_PRIVATE, 0xFF)
#define EV_STARTUP     MAKE_SYS_EVENT(SYS_EVENT_CLS_PRIVATE, 0x01)

#define BACKUP_DIRECTORY PLUGIN_APPS_DATA_DIR "/db_commit"

#define RC_SUCCESS 0
#define RC_USER_CANCEL 2

enum fcpy_op_flags
{
    FCPY_MOVE       = 0x00, /* Is a move operation (default) */
    FCPY_COPY      = 0x01, /* Is a copy operation */
    FCPY_OVERWRITE = 0x02, /* Overwrite destination */
    FCPY_EXDEV     = 0x04, /* Actually copy/move across volumes */
};

/* communication to the worker thread */
static struct
{
    int user_index;
    bool exiting; /* signal to the thread that we want to exit */
    bool resume;
    unsigned int id; /* worker thread id */
    struct  event_queue queue; /* thread event queue */
    long    last_useraction_tick;
} gThread;

/* status of db and commit */
static struct
{
    long last_check;
    bool do_commit;
    bool auto_commit;
    bool commit_ready;
    bool db_exists;
    bool have_backup;
    bool bu_exists;
} gStatus;

static unsigned char logbuffer[MAX_LOG_SIZE + 1];
static int log_font_h = -1;
static int logindex;
static bool logwrap;
static bool logenabled = true;

/*Support Fns*/
/* open but with a builtin printf for assembling the path */
int open_pathfmt(char *buf, size_t size, int oflag, const char *pathfmt, ...)
{
    va_list ap;
    va_start(ap, pathfmt);
    vsnprintf(buf, size, pathfmt, ap);
    va_end(ap);
    if ((oflag & O_PATH) == O_PATH)
        return -1;
    int handle = open(buf, oflag, 0666);
    //logf("Open: %s %d flag: %x", buf, handle, oflag);
    return handle;
}

static void sleep_yield(void)
{
    sleep(1);
    #undef yield
    rb->yield();
    #define yield sleep_yield
}

/* make sure tag can be displayed by font pf*/
static bool text_is_displayable(struct font *pf, unsigned char *src)
{
    unsigned short code;
    const unsigned char *ptr = src;
    while(*ptr)
    {
        ptr = rb->utf8decode(ptr, &code);

        if(!rb->font_get_bits(pf, code))
        {
            return false;
        }
    }
    return true;
}

/* callback for each tag if false returned tag will not be added */
bool user_check_tag(int index_type, char* build_idx_buf)
{
    static struct font *pf = NULL;
    if (!pf)
        pf = rb->font_get(FONT_UI);

    if (index_type == tag_artist || index_type == tag_album ||
        index_type == tag_genre || index_type == tag_title ||
        index_type == tag_composer || index_type == tag_comment ||
        index_type == tag_albumartist || index_type == tag_virt_canonicalartist)
    {
        /* this could be expanded with more rules / transformations */
        if (rb->utf8length(build_idx_buf) != strlen(build_idx_buf))
        {
            if (!text_is_displayable(pf, build_idx_buf))
            {
                logf("Can't display (%d) %s", index_type, build_idx_buf);
            }
        }
    }
    return true;
}

/* undef features we don't need */
#undef HAVE_DIRCACHE
#undef HAVE_TC_RAMCACHE
#undef HAVE_EEPROM_SETTINGS
/* paste the whole tagcache.c file */
#include "../tagcache.c"

static void check_logindex(void)
{
    if(logindex >= MAX_LOG_SIZE)
    {
        logdump(true);
        //logwrap = true;
        logindex = 0;
    }
}

static int log_push(void *userp, int c)
{
    (void)userp;

    logbuffer[logindex++] = c;
    check_logindex();

    return 1;
}

/* our logf function */
static void _log(const char *fmt, ...)
{
    if (!logenabled)
    {
        rb->splash(10, "log not enabled");
        return;
    }

    #ifdef USB_ENABLE_SERIAL
    int old_logindex = logindex;
    #endif
    va_list ap;

    va_start(ap, fmt);

#if (CONFIG_PLATFORM & PLATFORM_HOSTED)
    char buf[1024];
    vsnprintf(buf, sizeof buf, fmt, ap);
    DEBUGF("%s\n", buf);
    /* restart va_list otherwise the result if undefined when vuprintf is called */
    va_end(ap);
    va_start(ap, fmt);
#endif

    rb->vuprintf(log_push, NULL, fmt, ap);
    va_end(ap);

    /* add trailing zero */
    log_push(NULL, '\0');
}


int compute_nb_lines(int w, struct font* font)
{
    int i, nb_lines;
    int cur_x, delta_x;

    if(logindex>= MAX_LOG_SIZE || (logindex == 0 && !logwrap))
        return 0;

    if(logwrap)
        i = logindex;
    else
        i = 0;

    cur_x = 0;
    nb_lines = 0;

    do {
        if(logbuffer[i] == '\0')
        {
            cur_x = 0;
            nb_lines++;
        }
        else
        {
            /* does character fit on this line ? */
            delta_x = rb->font_get_width(font, logbuffer[i]);

            if(cur_x + delta_x > w)
            {
                cur_x = 0;
                nb_lines++;
            }

            /* update pointer */
            cur_x += delta_x;
        }

        i++;
        if(i >= MAX_LOG_SIZE)
            i = 0;
    } while(i != logindex);

    return nb_lines;
}

static bool logdisplay(void)
{

    int w, h, i, index;
    int fontnr;
    int cur_x, cur_y, delta_x;
    struct font* font;

    char buf[2];

    fontnr = FONT_FIRSTUSERFONT;
    font = rb->font_get(fontnr);
    buf[1] = '\0';
    w = LCD_WIDTH;
    h = LCD_HEIGHT;

    if (log_font_h < 0) /* init, get the horizontal size of each line */
    {
        rb->font_getstringsize("A", NULL, &log_font_h, fontnr);
        /* start at the end of the log */
        gThread.user_index = compute_nb_lines(w, font) - h/log_font_h -1;
        /* user_index will be number of the first line to display (warning: line!=log entry) */
        /* if negative, will be set 0 to zero later */
    }

    rb->lcd_clear_display();

    if(gThread.user_index < 0 || gThread.user_index >= MAX_LOG_SIZE)
        gThread.user_index = 0;

    if(logwrap)
        i = logindex;
    else
        i = 0;

    index = 0;
    cur_x = 0;
    cur_y = 0;

    /* nothing to print ? */
    if(logindex == 0 && !logwrap)
        goto end_print;

    do {
        if(logbuffer[i] == '\0')
        {
            /* should be display a newline ? */
            if(index >= gThread.user_index)
                cur_y += log_font_h;
            cur_x = 0;
            index++;
        }
        else
        {
            /* does character fit on this line ? */
            delta_x = rb->font_get_width(font, logbuffer[i]);

            if(cur_x + delta_x > w)
            {
                /* should be display a newline ? */
                if(index >= gThread.user_index)
                    cur_y += log_font_h;
                cur_x = 0;
                index++;
            }

            /* should we print character ? */
            if(index >= gThread.user_index)
            {
                buf[0] = logbuffer[i];
                rb->lcd_putsxy(cur_x, cur_y, buf);
            }

            /* update pointer */
            cur_x += delta_x;
        }
        i++;
        /* did we fill the screen ? */
        if(cur_y > h - log_font_h)
        {
            if (TIME_AFTER(current_tick, gThread.last_useraction_tick + HZ))
                gThread.user_index++;
            break;
        }

        if(i >= MAX_LOG_SIZE)
            i = 0;
    } while(i != logindex);

    end_print:
    rb->lcd_update();

    return false;
}

static bool logdump(bool append)
{
    int fd;
    int flags = O_CREAT|O_WRONLY|O_TRUNC;
    /* nothing to print ? */
    if(!logenabled || (logindex == 0 && !logwrap))
    {
        /* nothing is logged just yet */
        return false;
    }
    if (append)
    {
        flags = O_CREAT|O_WRONLY|O_APPEND;
    }

    fd = open(ROCKBOX_DIR "/db_commit_log.txt", flags, 0666);
    logenabled = false;
    if(-1 != fd) {
        int i;

        if(logwrap)
            i = logindex;
        else
            i = 0;

        do {
            if(logbuffer[i]=='\0')
                rb->fdprintf(fd, "\n");
            else
                rb->fdprintf(fd, "%c", logbuffer[i]);

            i++;
            if(i >= MAX_LOG_SIZE)
                i = 0;
        } while(i != logindex);

        close(fd);
    }

    logenabled = true;

    return false;
}

static void allocate_tempbuf(void)
{
    tempbuf_size = 0;
    tempbuf = rb->plugin_get_audio_buffer(&tempbuf_size);
    tempbuf_size &= ~0x03;

}

static void free_tempbuf(void)
{
    if (tempbuf_size == 0)
        return ;

    rb->plugin_release_audio_buffer();
    tempbuf = NULL;
    tempbuf_size = 0;
}

static bool do_timed_yield(void)
{
    /* Sorting can lock up for quite a while, so yield occasionally */
    static long wakeup_tick = 0;
    if (TIME_AFTER(current_tick, wakeup_tick))
    {
        yield();

        wakeup_tick = current_tick + (HZ/5);
        return true;
    }
    return false;
}

/* copy/move a file */
static int fcpy(const char *src, const char *target,
                unsigned int flags, bool (*poll_cancel)(const char *))
{
    int rc = -1;

    while (!(flags & (FCPY_COPY | FCPY_EXDEV))) {
        if ((flags & FCPY_OVERWRITE) || !file_exists(target)) {
            /* Rename and possibly overwrite the file */
            if (poll_cancel && poll_cancel(src)) {
                rc = RC_USER_CANCEL;
            } else {
                rc = rename(src, target);
            }

        #ifdef HAVE_MULTIVOLUME
            if (rc < 0 && errno == EXDEV) {
                /* Failed because cross volume rename doesn't work; force
                   a move instead */
                flags |= FCPY_EXDEV;
                break;
            }
        #endif /* HAVE_MULTIVOLUME */
        }

        return rc;
    }

    /* See if we can get the plugin buffer for the file copy buffer */
    size_t buffersize;
    char *buffer = (char *) plugin_get_buffer(&buffersize);
    if (buffer == NULL || buffersize < 512) {
        /* Not large enough, try for a disk sector worth of stack
           instead */
        buffersize = 512;
        buffer = (char *)alloca(buffersize);
    }

    if (buffer == NULL) {
        return -1;
    }

    buffersize &= ~0x1ff;  /* Round buffer size to multiple of sector
                              size */

    int src_fd = open(src, O_RDONLY);
    if (src_fd >= 0) {
        int oflag = O_WRONLY|O_CREAT;

        if (!(flags & FCPY_OVERWRITE)) {
            oflag |= O_EXCL;
        }

        int target_fd = open(target, oflag, 0666);
        if (target_fd >= 0) {
            off_t total_size = 0;
            off_t next_cancel_test = 0; /* No excessive button polling */

            rc = RC_SUCCESS;

            while (rc == RC_SUCCESS) {
                if (total_size >= next_cancel_test) {
                    next_cancel_test = total_size + 0x10000;
                    if (poll_cancel && poll_cancel(src)) {
                       rc = RC_USER_CANCEL;
                       break;
                    }
                }

                ssize_t bytesread = read(src_fd, buffer, buffersize);
                if (bytesread <= 0) {
                    if (bytesread < 0) {
                        rc = -1;
                    }
                    /* else eof on buffer boundary; nothing to write */
                    break;
                }

                ssize_t byteswritten = write(target_fd, buffer, bytesread);
                if (byteswritten < bytesread) {
                    /* Some I/O error */
                    rc = -1;
                    break;
                }

                total_size += byteswritten;

                if (bytesread < (ssize_t)buffersize) {
                    /* EOF with trailing bytes */
                    break;
                }
            }

            if (rc == RC_SUCCESS) {
                /* If overwriting, set the correct length if original was
                   longer */
                rc = ftruncate(target_fd, total_size);
            }

            close(target_fd);

            if (rc != RC_SUCCESS) {
                /* Copy failed. Cleanup. */
                remove(target);
            }
        }

        close(src_fd);
    }

    if (rc == RC_SUCCESS && !(flags & FCPY_COPY)) {
        /* Remove the source file */
        rc = remove(src);
    }

    return rc;
}

static bool backup_restore_tagcache(bool backup)
{
    struct master_header   tcmh;
    char path[MAX_PATH];
    char bu_path[MAX_PATH];
    int fd;
    int rc;

    if (backup)
    {
        if (!rb->dir_exists(BACKUP_DIRECTORY))
            mkdir(BACKUP_DIRECTORY);
        snprintf(path, sizeof(path), "%s/"TAGCACHE_FILE_MASTER, tc_stat.db_path);
        snprintf(bu_path, sizeof(bu_path), "%s/"TAGCACHE_FILE_MASTER, BACKUP_DIRECTORY);
    }
    else
    {
        if (!rb->dir_exists(BACKUP_DIRECTORY))
            return false;
        snprintf(path, sizeof(path), "%s/"TAGCACHE_FILE_MASTER, BACKUP_DIRECTORY);
        snprintf(bu_path, sizeof(bu_path), "%s/"TAGCACHE_FILE_MASTER, tc_stat.db_path);
    }

    fd = open(path, O_RDONLY, 0666);

    if (fd >= 0)
    {
        rc = read(fd, &tcmh, sizeof(struct master_header));
        close(fd);
        if (rc != sizeof(struct master_header))
        {
            logf("master file read failed");
            return false;
        }
        int entries = tcmh.tch.entry_count;

        logf("master file %d entries", entries);
        if (backup)
            logf("backup db to %s", BACKUP_DIRECTORY);
        else
            logf("restore db to %s", tc_stat.db_path);

        if (entries > 0)
        {
            logf("%s", path);
            fcpy(path, bu_path, FCPY_COPY|FCPY_OVERWRITE, NULL);

            for (int i = 0; i < TAG_COUNT; i++)
            {
                if (TAGCACHE_IS_NUMERIC(i))
                    continue;
                snprintf(path, sizeof(path),
                         "%s/"TAGCACHE_FILE_INDEX, tc_stat.db_path, i);

                snprintf(bu_path, sizeof(bu_path),
                         "%s/"TAGCACHE_FILE_INDEX, BACKUP_DIRECTORY, i);
                /* Note: above we swapped paths in the snprintf call here we swap variables */
                if (backup)
                {
                    logf("%s", path);
                    if (fcpy(path, bu_path, FCPY_COPY|FCPY_OVERWRITE, NULL) < 0)
                        goto failed;
                    gStatus.have_backup = true;
                }
                else
                {
                    logf("%s", bu_path);
                    if (fcpy(bu_path, path, FCPY_COPY|FCPY_OVERWRITE, NULL) < 0)
                        goto failed;
                }
            }
        }
        return true;
    }
failed:
    if (backup)
    {
        logf("failed backup");
    }

    return false;
}

/* asks the user if they wish to quit */
static bool confirm_quit(void)
{
    const struct text_message prompt =
       { (const char*[]) {"Are you sure?", "This will abort commit."}, 2};
    enum yesno_res response = rb->gui_syncyesno_run(&prompt, NULL, NULL);
    return (response == YESNO_YES);
}

/* asks the user if they wish to backup/restore */
static bool prompt_backup_restore(bool backup)
{
    const struct text_message bu_prompt = { (const char*[]) {"Backup database?"}, 1};
    const struct text_message re_prompt =
        { (const char*[]) {"Error committing,", "Restore database?"}, 2};
    enum yesno_res response =
              rb->gui_syncyesno_run(backup ? &bu_prompt:&re_prompt, NULL, NULL);
    if(response == YESNO_YES)
        return backup_restore_tagcache(backup);
    return true;
}

static const char* list_get_name_cb(int selected_item, void* data,
                                    char* buf, size_t buf_len)
{
    (void) data;
    (void) buf;
    (void) buf_len;

    /* buf supplied isn't used so lets use it for a filename buffer */
    if (TIME_AFTER(current_tick, gStatus.last_check))
    {
        snprintf(buf, buf_len, "%s/"TAGCACHE_FILE_NOCOMMIT, tc_stat.db_path);
        gStatus.auto_commit = !file_exists(buf);
        snprintf(buf, buf_len, "%s/"TAGCACHE_FILE_TEMP, tc_stat.db_path);
        gStatus.commit_ready = file_exists(buf);
        snprintf(buf, buf_len, "%s/"TAGCACHE_FILE_MASTER, tc_stat.db_path);
        gStatus.db_exists = file_exists(buf);
        snprintf(buf, buf_len, "%s/"TAGCACHE_FILE_MASTER, BACKUP_DIRECTORY);
        gStatus.bu_exists = file_exists(buf);
        gStatus.last_check = current_tick + HZ;
        buf[0] = '\0';
    }

    switch(selected_item)
    {

        case 0: /* exit */
            return ID2P(LANG_MENU_QUIT);
        case 1: /*sep*/
            return ID2P(VOICE_BLANK);
        case 2: /*backup*/
            if (!gStatus.db_exists)
                return ID2P(VOICE_BLANK);
            return "Backup";
        case 3: /*restore*/
            if (!gStatus.bu_exists)
                return ID2P(VOICE_BLANK);
            return "Restore";
        case 4: /*sep*/
            return ID2P(VOICE_BLANK);
        case 5: /*auto commit*/
            if (gStatus.auto_commit)
                return "Disable auto commit";
            else
                return "Enable auto commit";
        case 6: /*destroy*/
            if (gStatus.db_exists)
                return "Delete database";
            /*fall through*/
        case 7: /*sep*/
            return ID2P(VOICE_BLANK);
        case 8: /*commit*/
            if (gStatus.commit_ready)
                return "Commit";
            else
                return "Nothing to commit";
        default:
            return "?";
    }
}

static int list_voice_cb(int list_index, void* data)
{
    #define MAX_MENU_NAME 32
    if (!rb->global_settings->talk_menu)
        return -1;
    else
    {
        char buf[MAX_MENU_NAME];
        const char* name = list_get_name_cb(list_index, data, buf, sizeof(buf));
        long id = P2ID((const unsigned char *)name);
        if(id>=0)
            rb->talk_id(id, true);
        else
            rb->talk_spell(name, true);
    }
    return 0;
}

static int commit_menu(void)
{
    struct gui_synclist lists;
    bool exit = false;
    int button,i;
    int selection, ret = 0;

    rb->gui_synclist_init(&lists,list_get_name_cb,0, false, 1, NULL);
    rb->gui_synclist_set_nb_items(&lists, 9);
    rb->gui_synclist_select_item(&lists, 0);

    while (!exit)
    {
        rb->gui_synclist_draw(&lists);
        button = rb->get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
        if (rb->gui_synclist_do_button(&lists, &button))
            continue;
        selection = rb->gui_synclist_get_sel_pos(&lists);

        if (button ==  ACTION_STD_CANCEL)
                return 0;
        else if (button ==  ACTION_STD_OK)
        {
            switch(selection)
            {
                case 0: /* exit */
                    exit = true;
                    break;
                case 1: /*sep*/
                    continue;
                case 2: /*backup*/
                    if (!gStatus.db_exists)
                        break;
                    if (!backup_restore_tagcache(true))
                        rb->splash(HZ, "Backup failed!");
                    else
                    {
                        rb->splash(HZ, "Backup success!");
                        gStatus.bu_exists = true;
                    }
                    break;
                case 3: /*restore*/
                    if (!gStatus.bu_exists)
                        break;
                    if (!backup_restore_tagcache(false))
                        rb->splash(HZ, "Restore failed!");
                    else
                        rb->splash(HZ, "Restore success!");
                    break;
                case 4: /*sep*/
                    continue;
                case 5: /*auto commit*/
                {
                    /* build_idx_buf supplied by tagcache.c isn't being used
                     * so lets use it for a filename buffer */
                    snprintf(build_idx_buf, build_idx_bufsz,
                            "%s/" TAGCACHE_FILE_NOCOMMIT, tc_stat.db_path);
                    if(gStatus.auto_commit)
                         close(open(build_idx_buf, O_WRONLY | O_CREAT | O_TRUNC, 0666));
                    else
                        remove(build_idx_buf);
                    gStatus.auto_commit = !file_exists(build_idx_buf);
                    break;
                }
                case 6: /*destroy*/
                {
                    if (!gStatus.db_exists)
                        break;
                    const struct text_message prompt =
                     { (const char*[]) {"Are you sure?", "This will destroy database."}, 2};
                    if (rb->gui_syncyesno_run(&prompt, NULL, NULL) == YESNO_YES)
                        remove_files();
                    break;
                }
                case 7: /*sep*/
                    continue;
                case 8: /*commit*/
                    if (gStatus.commit_ready)
                    {
                        gStatus.do_commit = true;
                        exit = true;
                    }
                    break;

                case MENU_ATTACHED_USB:
                    return PLUGIN_USB_CONNECTED;
                default:
                    return 0;
            }
        }
    } /*while*/
    return ret;
}

/*-----------------------------------------------------------------------------*/
/******* plugin_start *******                                                  */
/*-----------------------------------------------------------------------------*/

enum plugin_status plugin_start(const void* parameter)
{
    (void) parameter;

    /* Turn off backlight timeout */
    backlight_ignore_timeout();

    memset(&gThread, 0, sizeof(gThread));
    memset(&gStatus, 0, sizeof(gStatus));
    memset(&tc_stat, 0, sizeof(struct tagcache_stat));
    memset(&current_tcmh, 0, sizeof(struct master_header));
    filenametag_fd = -1;

    strlcpy(tc_stat.db_path, rb->global_settings->tagcache_db_path, sizeof(tc_stat.db_path));
    if (!rb->dir_exists(tc_stat.db_path)) /* on fail use default DB path */
        strlcpy(tc_stat.db_path, ROCKBOX_DIR, sizeof(tc_stat.db_path));
    tc_stat.initialized = true;
    tc_stat.commit_step = -1;

    logf("started");

    int result = commit_menu();

    if (!gStatus.do_commit)
    {
        /* Turn on backlight timeout (revert to settings) */
        backlight_use_settings();
        return PLUGIN_OK;
    }

    logdump(false);
    allocate_tempbuf();
    if (gStatus.db_exists && !gStatus.have_backup && !prompt_backup_restore(true))
        rb->splash(HZ, "Backup failed!");
    thread_create();
    gThread.user_index = 0;
    logdisplay(); /* get something on the screen while user waits */

    while (!gThread.exiting)
    {
        logdisplay();

        int action = rb->get_action(CONTEXT_STD, HZ/20);

        switch( action )
        {
            case ACTION_NONE:
                break;
            case ACTION_STD_NEXT:
            case ACTION_STD_NEXTREPEAT:
                gThread.last_useraction_tick = current_tick;
                gThread.user_index++;
                break;
            case ACTION_STD_PREV:
            case ACTION_STD_PREVREPEAT:
                gThread.last_useraction_tick = current_tick;
                gThread.user_index--;
                break;
            case ACTION_STD_OK:
                gThread.last_useraction_tick = current_tick;
                gThread.user_index = 0;
                break;
            case SYS_POWEROFF:
            case ACTION_STD_CANCEL:
                if (tc_stat.commit_step >= 0 && !tc_stat.ready)
                {
                    if (!confirm_quit())
                        break;
                    tc_stat.commit_delayed = true; /* Cancel the commit */
                }
                rb->queue_remove_from_head(&gThread.queue, EV_EXIT);
                rb->queue_post(&gThread.queue, EV_EXIT, 0);
                break;
#ifdef HAVE_TOUCHSCREEN
            case ACTION_TOUCHSCREEN:
            {
                gThread.last_useraction_tick = current_tick;
                short x, y;
                static int prev_y;

                action = rb->action_get_touchscreen_press(&x, &y);

                if(action & BUTTON_REL)
                    prev_y = 0;
                else
                {
                    if(prev_y != 0)
                        gThread.user_index += (prev_y - y) / log_font_h;

                    prev_y = y;
                }
            }
#endif
            default:
                break;
        }
        yield();
    }

    rb->thread_wait(gThread.id);
    rb->queue_delete(&gThread.queue);
    free_tempbuf();

    if (tc_stat.commit_delayed || !tc_stat.ready)
    {
        remove_files();
        if (gStatus.bu_exists && !prompt_backup_restore(false))
            rb->splash(HZ, "Restore failed!");
    }

    if (tc_stat.ready)
        rb->tagcache_commit_finalize();

    /* Turn on backlight timeout (revert to settings) */
    backlight_use_settings();
    return PLUGIN_OK;
}

/****************** main thread + helper ******************/
static void thread(void)
{
    struct queue_event ev;
    while (!gThread.exiting)
    {
        rb->queue_wait_w_tmo(&gThread.queue, &ev, 1);
        switch (ev.id)
        {
            case SYS_USB_CONNECTED:
                rb->usb_acknowledge(SYS_USB_CONNECTED_ACK);
                logenabled = false;
                break;
            case SYS_USB_DISCONNECTED:
                logenabled = true;
                /*fall through*/
            case EV_STARTUP:
                logf("Thread Started");
                cpu_boost(true);
                if (commit())
                    tc_stat.ready = true;
                cpu_boost(false);
                logdump(true);
                gThread.user_index++;
                logdisplay();
                break;
            case EV_EXIT:
                gThread.exiting = true;
                return;
            default:
                break;
        }
        yield();
    }
}

static void thread_create(void)
{
    /* put the thread's queue in the bcast list */
    rb->queue_init(&gThread.queue, true);
    /*Note: tagcache_stack is defined in apps/tagcache.c */
    gThread.last_useraction_tick = current_tick;
    gThread.id = rb->create_thread(thread, tagcache_stack, sizeof(tagcache_stack),
                                      0, "db_commit"
                                      IF_PRIO(, PRIORITY_USER_INTERFACE)
                                      IF_COP(, CPU));
    rb->queue_post(&gThread.queue, EV_STARTUP, 0);
    yield();
}