From 9c7db46be9933e36e79b4caac86a5bdc3183122d Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Thu, 26 Aug 2010 22:36:07 +0200 Subject: Merged commands.c and control_commands.c. --- control_commands.c | 89 ------------------------------------------------------ 1 file changed, 89 deletions(-) delete mode 100644 control_commands.c (limited to 'control_commands.c') diff --git a/control_commands.c b/control_commands.c deleted file mode 100644 index 41e8965..0000000 --- a/control_commands.c +++ /dev/null @@ -1,89 +0,0 @@ -#include "control_commands.h" -#include "servers.h" -#include "music.h" -#include "server_communication.h" - -#include -#include - -static void list_servers(GSocketConnection *connection, const gchar *cmd) { - GSocket *socket = g_socket_connection_get_socket(connection); - - for(GSList *node = servers; node; node = g_slist_next(node)) { - struct server *s = node->data; - gchar *buffer = g_strdup_printf("%s:%d\n", s->host, s->port); - g_socket_send(socket, buffer, strlen(buffer), NULL, NULL); - } -} - -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) { - list = music_find_artist(data[2]); - } else if(g_ascii_strcasecmp(data[1] , "title") == 0) { - list = music_find_title(data[2]); - } else if(g_ascii_strcasecmp(data[1], "album") == 0) { - list = music_find_album(data[2]); - } else { - g_debug("unknown search"); - } - - GString *string = g_string_new(NULL); - 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); - - for(GSList *node = servers; node; node = g_slist_next(node)) { - struct server *server = node->data; - g_debug("fetching data from server %s", server->host); - gchar **temp = server_find(server, data[1], data[2]); - if(temp == NULL) { - continue; - } - for(gint i = 0; i < g_strv_length(temp); i++) { - if(strlen(temp[i]) == 0) { - break; - } - g_string_append_printf(string, "%s:%s\n", server->host, temp[i]); - } - g_strfreev(temp); - } - - 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); -} - -void control_commands_handle(GSocketConnection *connection, const gchar *cmd) { - if(g_strcmp0(cmd, "servers") == 0) { - list_servers(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); - GSocket *socket = g_socket_connection_get_socket(connection); - g_socket_send(socket, buf, strlen(buf), NULL, NULL); - g_free(buf); - } -} -- cgit v1.2.3