aboutsummaryrefslogtreecommitdiff
path: root/item.c
blob: f25c40d2116c34dcf26fcc1f4d7c8018c78f8604 (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
/*
 * Ache - 2017-08-14 - GPLv3 or later
 */

#include "item.h"
#include<errno.h>

/*
 * Set of functions to manage a list of items (One file or directory is
 * an item).
 */



int sort_i(const void* A, const void* B) {
    itemC* a = (itemC*)A;
    itemC* b = (itemC*)B;

    if(a->opt > b->opt) 
        return -1;
    else if(a->opt < b->opt)
        return 1;
    else {
        return strcmp(a->cstr,b->cstr);
    }
}
void freelitem( itemC* m, int s) {
    for(int i = 0 ; i < s ; i++) {
        free(m[i].cstr);
        if( !m[i].opt) {
                taglib_file_free(m[i].info.file);
        }
    }

    taglib_tag_free_strings();
    free(m);
}
void listdir(int option, itemC** m, int* s) {
    DIR *dir;
    int nbitem = 0;
    itemC* menu = NULL;
    struct dirent *entry;


    if (!(dir = opendir("."))) {
        perror("opendir failled");
        return;
    }
    errno = 0;

    if (!(entry = readdir(dir)))
        return;



    do {
        if(errno) {
            break;
        }

        if( entry->d_name[0] == '.' && !(option & HIDDEN) && strcmp(entry->d_name, "..") )
            continue;

// QUICK FIX OF TAGLIB
        if( strchr(entry->d_name, '#') )
            continue;
#ifdef DEBUG
        mvprintc(1,1,entry->d_name,COLS/2-5);
#endif



        menu = realloc(menu, ++nbitem * sizeof *menu);

        if (entry->d_type == DT_DIR) { 

            char* tmp = malloc( strlen(entry->d_name)+3);
            sprintf(tmp, "[%s]", entry->d_name);
            menu[nbitem-1].cstr = tmp;
            menu[nbitem-1].opt  = 1;
            menu[nbitem-1].id   = nbitem-1;
            menu[nbitem-1].selected   = 0;
        }
        else {


            TagLib_File *file;
            TagLib_Tag *tag;
            const TagLib_AudioProperties *properties;

            file = taglib_file_new(entry->d_name); 

            if(file == NULL) {
                menu = realloc(menu, --nbitem * sizeof *menu);
                continue;
            }

            tag = taglib_file_tag(file);
            properties = taglib_file_audioproperties(file);

            if( ! properties )  {
                fprintf(stderr, "ID3 file but not audio");
                taglib_tag_free_strings();
                taglib_file_free(file);
                menu[nbitem-1].opt  = 2;
            }else{
                menu[nbitem-1].opt  = 0;
            }
            menu[nbitem-1].cstr = strdup(entry->d_name);
            menu[nbitem-1].id   = nbitem-1;
            menu[nbitem-1].info = (tagInfo){file, tag, properties};
            menu[nbitem-1].selected   = 0;

        }
    } while (entry = readdir(dir));
end:

    closedir(dir);
    *m = menu;
    *s = nbitem;
    refresh();
}