summaryrefslogtreecommitdiff
path: root/control_commands.c
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2010-08-26 21:37:18 +0200
committerJon Bergli Heier <snakebite@jvnv.net>2010-08-26 21:37:18 +0200
commit12077e596832cd5908a1c97caa017bb5198fc024 (patch)
tree0ae37b9fea856961e6593dd13576eb2e1a4f7d60 /control_commands.c
parent381d191019f876af5bb2cdfac783194755762800 (diff)
Search in added servers when searching from the control service using 'find'.
Diffstat (limited to 'control_commands.c')
-rw-r--r--control_commands.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/control_commands.c b/control_commands.c
index 61477ac..f8f3a54 100644
--- a/control_commands.c
+++ b/control_commands.c
@@ -1,5 +1,7 @@
#include "control_commands.h"
#include "servers.h"
+#include "music.h"
+#include "server_communication.h"
#include <glib.h>
#include <string.h>
@@ -14,9 +16,66 @@ static void list_servers(GSocketConnection *connection, const gchar *cmd) {
}
}
+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);
+ 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 = 1; 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);