diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2010-08-23 18:34:43 +0200 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2010-08-23 18:34:43 +0200 |
commit | 0baaa8c068f57e44efffa7c1f6806faefdd655e4 (patch) | |
tree | 29c01f9d20db918a60a95504c10c0915842ad3d8 | |
parent | 4574b09685bb3e805aeddad956d524941f47cb37 (diff) |
Implemented the find command.
-rw-r--r-- | commands.c | 42 |
1 files changed, 42 insertions, 0 deletions
@@ -54,6 +54,46 @@ static void commands_list(GSocketConnection *connection, const gchar *cmd) { g_string_free(string, TRUE); } +static void commands_find(GSocketConnection *connection, const gchar *cmd) { + GError *error = NULL; + gchar **data = g_strsplit(cmd, " ", 3); + + if(g_strv_length(data) != 3) { + const gchar *buf = "syntax: find (artist) search\n"; + GSocket *socket = g_socket_connection_get_socket(connection); + g_socket_send(socket, buf, strlen(buf), NULL, NULL); + return; + } + + GSList *list = NULL; + g_debug("strlen(%s) == %d", data[1], strlen(data[1])); + if(g_ascii_strcasecmp(data[1], "artist") == 0) { + g_debug("artist search"); + list = music_find_artist(data[2]); + } else { + g_debug("unknown search"); + } + + GString *string = g_string_new(NULL); + g_string_append_printf(string, "%d\n", g_slist_length(list)); + for(GSList *node = list; node; node = g_slist_next(node)) { + struct file *f = node->data; + gchar *relpath = g_build_filename(f->parent->path + + strlen(music_root->path), f->name, NULL); + g_string_append_printf(string, "%s\n", relpath); + g_free(relpath); + } + g_slist_free(list); + + GOutputStream *os = g_io_stream_get_output_stream((GIOStream*)connection); + if(g_output_stream_write_all(os, string->str, string->len, NULL, NULL, + &error) == FALSE) { + g_warning(error->message); + g_error_free(error); + } + g_string_free(string, TRUE); +} + static void commands_ping(GSocketConnection *connection, const gchar *cmd) { GError *error = NULL; GSocket *socket = g_socket_connection_get_socket(connection); @@ -69,6 +109,8 @@ void commands_handle(GSocketConnection *connection, const gchar *cmd) { commands_ping(connection, cmd); } else if(g_strncasecmp(cmd, "list", 4) == 0) { commands_list(connection, cmd); + } else if(g_strncasecmp(cmd, "find", 4) == 0) { + commands_find(connection, cmd); } else { g_debug("unknown command"); gchar *buf = g_strdup_printf("error: unknown command %s\n", cmd); |