summaryrefslogtreecommitdiff
path: root/music.c
blob: b90d5afded71a014f84bc1c43e29f9aad940bdcd (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
#include "music.h"
#include "conf.h"

#include <glib/gstdio.h>
#include <string.h>

struct directory *music_root = NULL;

gboolean music_init() {
	gchar *path = conf_get_string("audist", "music_root");
	if(path == NULL) {
		g_error("no music_root is set!");
	}

	music_root = g_new0(struct directory, 1);
	music_root->path = path;

	return 1;
}

gboolean music_scan(struct directory *directory) {
	GError *error = NULL;
	GDir *dir = g_dir_open(directory->path, 0, &error);

	if(dir == NULL) {
		g_error("%s", error->message);
		g_error_free(error);
		return 0;
	}

	const gchar *entry;
	while((entry = g_dir_read_name(dir)) != NULL) {
		struct stat st;
		gchar *fullpath = g_build_filename(directory->path, entry, NULL);
		if(fullpath == NULL) {
			g_error("g_build_filename returned NULL");
		}

		if(g_stat(fullpath, &st) < 0) {
			g_warning("g_stat failed on %s", fullpath);
		}

		if(S_ISREG(st.st_mode)) {
			struct file *f = g_new0(struct file, 1);
			f->name = g_strdup(entry);
			f->parent = directory;

			directory->files = g_slist_prepend(directory->files, f);

			struct tag *tag = tag_read(fullpath);
			if(tag) {
				f->tag = tag;
			}
		} else if(S_ISDIR(st.st_mode)) {
			struct directory *d = g_new0(struct directory, 1);
			d->path = g_build_filename(directory->path, entry, NULL);

			directory->sub = g_slist_prepend(directory->sub, d);

			music_scan(d);
		}
		g_free(fullpath);
	}

	g_dir_close(dir);

	return 1;
}

gboolean music_scan_root() {
	g_assert(music_root != NULL);

	return music_scan(music_root);
}

static struct directory *music_find_dir_rec(struct directory *root, const gchar *path) {
	if(g_strcmp0(root->path, path) == 0)
		return root;

	for(GSList *node = root->sub; node; node = g_slist_next(node)) {
		struct directory *d = node->data;
		if(g_strcmp0(d->path, path) == 0) {
			return d;
		}

		struct directory *temp = music_find_dir_rec(d, path);
		if(temp) {
			return temp;
		}
	}

	return NULL;
}

struct directory *music_find_dir(const gchar *path) {
	gchar *real_path = g_build_filename(music_root->path, path, NULL);
	int len = strlen(real_path);
	if(real_path[len - 1] == '/')
		real_path[len - 1] = '\0';

	struct directory *dir = music_find_dir_rec(music_root, real_path);

	g_free(real_path);

	return dir;
}

struct file *music_find_file(const gchar *path) {
	gchar *dirname = g_path_get_dirname(path);
	gchar *filename = g_path_get_basename(path);

	struct directory *directory = music_find_dir(dirname);
	g_free(dirname);

	if(directory == NULL) {
		g_free(filename);
		return NULL;
	}

	for(GSList *node = directory->files; node; node = g_slist_next(node)) {
		struct file *f = node->data;
		if(g_strcmp0(filename, f->name) == 0) {
			g_free(filename);
			return f;
		}
	}

	g_free(filename);
	return NULL;
}

static void music_do_free(struct directory *root) {
	g_assert(root != NULL);

	for(GSList *node = root->sub; node; node = g_slist_next(node)) {
		struct directory *d = node->data;
		music_do_free(d);
	}

	g_slist_free(root->sub);

	for(GSList *node = root->files; node; node = g_slist_next(node)) {
		struct file *f = node->data;
		g_free(f->name);
		g_free(f);
	}

	g_slist_free(root->files);

	g_free(root->path);
	g_free(root);
}

void music_free() {
	g_assert(music_root != NULL);

	music_do_free(music_root);
	music_root = NULL;
}

static GSList *music_find_rec(struct directory *directory, const gchar *name, enum tag_type type) {
	GSList *list = NULL;

	for(GSList *node = directory->sub; node; node = g_slist_next(node)) {
		struct directory *d = node->data;
		list = g_slist_concat(list, music_find_rec(d, name, type));
	}

	for(GSList *node = directory->files; node; node = g_slist_next(node)) {
		struct file *f = node->data;
		const gchar *tag_str = tag_get(f->tag, type);
		if(tag_str == NULL) {
			continue;
		}

		gchar *str = g_utf8_casefold(tag_str, strlen(tag_str));
		if(strstr(str, name) != NULL) {
			list = g_slist_prepend(list, f);
		}
		g_free(str);
	}

	return list;
}

static GSList *music_find(const gchar *_name, enum tag_type type) {
	gchar *name = g_utf8_casefold(_name, strlen(_name));

	GSList *list = music_find_rec(music_root, name, type);

	g_free(name);
	return list;
}

GSList *music_find_artist(const gchar *name) {
	return music_find(name, TAG_TYPE_ARTIST);
}

GSList *music_find_title(const gchar *name) {
	return music_find(name, TAG_TYPE_TITLE);
}

GSList *music_find_album(const gchar *name) {
	return music_find(name, TAG_TYPE_ALBUM);
}

gchar *music_get_full_path(const gchar *path) {
	return g_build_filename(music_root->path, path, NULL);
}