summaryrefslogtreecommitdiff
path: root/music.c
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2010-08-16 00:51:20 +0200
committerJon Bergli Heier <snakebite@jvnv.net>2010-08-16 00:51:20 +0200
commitb4cbca161a1638e96d9e0a6fe12a29ed43173e43 (patch)
tree296cc7512f6e4baf1e357cedd628356bc2cf5aa5 /music.c
Committed some work.
Diffstat (limited to 'music.c')
-rw-r--r--music.c90
1 files changed, 90 insertions, 0 deletions
diff --git a/music.c b/music.c
new file mode 100644
index 0000000..2e618bd
--- /dev/null
+++ b/music.c
@@ -0,0 +1,90 @@
+#include "music.h"
+
+#include <glib/gstdio.h>
+
+struct directory *music_root = NULL;
+
+gboolean music_init(const gchar *path) {
+ music_root = g_new0(struct directory, 1);
+ music_root->path = g_strdup(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 file *f = g_new0(struct file, 1);
+ f->name = g_strdup(entry);
+
+ if(directory->files == NULL) {
+ directory->files = f;
+ } else {
+ struct file *last = directory->files;
+ while(last->next) last = last->next;
+ last->next = f;
+ }
+ }
+
+ 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;
+
+ /* TODO: implement this */
+ g_error("not implemented");
+
+ return NULL;
+}
+
+struct directory *music_find_dir(const gchar *path) {
+ return music_find_dir_rec(music_root, path);
+}
+
+static void music_do_free(struct directory *root) {
+ struct directory *node;
+
+ g_assert(root != NULL);
+
+ for(node = root->sub; node; node = node->next) {
+ music_do_free(node);
+ }
+
+ for(node = root->next; node; node = node->next) {
+ music_do_free(node);
+ }
+
+ for(struct file *f = root->files; f; f = f->next) {
+ g_free(f->name);
+ g_free(f);
+ }
+
+ g_free(root->path);
+ g_free(root);
+}
+
+void music_free() {
+ g_assert(music_root != NULL);
+
+ music_do_free(music_root);
+ music_root = NULL;
+}