<commit_before>/*  XMMS2 - X Music Multiplexer System
 *  Copyright (C) 2003	<NAME>, <NAME>, <NAME>
 *
 *  PLUGINS ARE NOT CONSIDERED TO BE DERIVED WORK !!!
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 */

#include "xmms2_client.h"

static void mlib_add (xmmsc_connection_t *conn, int argc, char **argv);
static void mlib_loadall (xmmsc_connection_t *conn, int argc, char **argv);
static void mlib_searchadd (xmmsc_connection_t *conn, int argc, char **argv);
static void mlib_search (xmmsc_connection_t *conn, int argc, char **argv);
static void mlib_query (xmmsc_connection_t *conn, int argc, char **argv);
static void mlib_queryadd (xmmsc_connection_t *conn, int argc, char **argv);
static void mlib_playlist_list (xmmsc_connection_t *conn, int argc, char **argv);
static void mlib_playlist_save (xmmsc_connection_t *conn, int argc, char **argv);
static void mlib_playlist_load (xmmsc_connection_t *conn, int argc, char **argv);
static void mlib_playlists_list (xmmsc_connection_t *conn, int argc, char **argv);
static void mlib_playlist_import (xmmsc_connection_t *conn, int argc, char **argv);
static void mlib_playlist_export (xmmsc_connection_t *conn, int argc, char **argv);
static void mlib_playlist_remove (xmmsc_connection_t *conn, int argc, char **argv);
static void mlib_addpath (xmmsc_connection_t *conn, int argc, char **argv);
static void mlib_rehash (xmmsc_connection_t *conn, int argc, char **argv);

cmds mlib_commands[] = {
	{ "add", "[url] - Add 'url' to medialib", mlib_add },
	{ "loadall", "Load everything from the mlib to the playlist", mlib_loadall },
	{ "searchadd", "[artist=Dismantled] ... - Search for, and add songs to playlist", mlib_searchadd },
	{ "search", "[artist=Dismantled] ... - Search for songs matching criteria", mlib_search },
	{ "query", "[\"raw sql statement\"] - Send a raw SQL statement to the mlib", mlib_query },
	{ "queryadd", "[\"raw sql statement\"] - Send a raw SQL statement to the mlib and add all entries to playlist", mlib_queryadd },
	{ "list_playlist", "[playlistname] - List 'playlistname' stored in medialib", mlib_playlist_list },
	{ "save_playlist", "[playlistname] - Save current playlist to 'playlistname'", mlib_playlist_save },
	{ "load_playlist", "[playlistname] - Load 'playlistname' stored in medialib", mlib_playlist_load },
	{ "playlists_list", "List all available playlists", mlib_playlists_list },
	{ "import_playlist", "[name] [filename] - Import playlist from file", mlib_playlist_import },
	{ "export_playlist", "[playlistname] [mimetype] - Export playlist", mlib_playlist_export },
	{ "remove_playlist", "[playlistname] - Remove a playlist", mlib_playlist_remove },
	{ "addpath", "[path] - Import metadata from all media files under 'path'", mlib_addpath },
	{ "rehash", "Force the medialib to check whether its data is up to date", mlib_rehash },

	{ NULL, NULL, NULL },
};

static void
mlib_add (xmmsc_connection_t *conn, int argc, char **argv)
{
	xmmsc_result_t *res;
	gint i;

	for (i = 3; argv[i]; i++) {
		char *url = format_url (argv[i]);
		if (!url) return;
		res = xmmsc_medialib_add_entry (conn, url);
		free (url);
		xmmsc_result_wait (res);
		printf ("Added %s to medialib\n", argv[i]);
		xmmsc_result_unref (res);
	}
}

static void
mlib_loadall (xmmsc_connection_t *conn, int argc, char **argv)
{
	xmmsc_result_t *res;
	res = xmmsc_medialib_add_to_playlist (conn, "select id from Media where key='url'");
	xmmsc_result_wait (res);
	xmmsc_result_unref (res);
}

char *
mlib_query_from_args (int argc, char **argv) {
	gchar *query;
	char **s;
	guint i;
	xmmsc_query_attribute_t *query_spec;

	if (argc < 4) {
		print_error ("Supply a query");
	}

	query_spec = (xmmsc_query_attribute_t*) malloc((argc-3)*sizeof(xmmsc_query_attribute_t));

	for (i=3; i < argc; i++) {
		s = g_strsplit (argv[i], "=", 0);
		if (!s[0] || !s[1]) {
			return NULL;
		}
		query_spec[i-3].key = xmmsc_sqlite_prepare_string(s[0]);
		query_spec[i-3].value = xmmsc_sqlite_prepare_string(s[1]);

		if (query_spec[i-3].key == NULL || query_spec[i-3].value == NULL) {
			free(query_spec[i-3].key);
			free(query_spec[i-3].value);
			while (i-3 > 0) {
				i--;
				free(query_spec[i-3].key);
				free(query_spec[i-3].value);
			}
			free(query_spec);
			return NULL;
		}

		g_strfreev(s);
	}

	query = xmmsc_querygen_and(query_spec, argc-3);

	free(query_spec);

	return query;

}

static void
mlib_queryadd (xmmsc_connection_t *conn, int argc, char **argv)<commit_msg>PRIVATE:Cleaned up mlib_query_from_args and fixed a few memory leaks.<commit_after>/*  XMMS2 - X Music Multiplexer System
 *  Copyright (C) 2003	<NAME>, <NAME>, <NAME>
 *
 *  PLUGINS ARE NOT CONSIDERED TO BE DERIVED WORK !!!
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 */

#include "xmms2_client.h"

static void mlib_add (xmmsc_connection_t *conn, int argc, char **argv);
static void mlib_loadall (xmmsc_connection_t *conn, int argc, char **argv);
static void mlib_searchadd (xmmsc_connection_t *conn, int argc, char **argv);
static void mlib_search (xmmsc_connection_t *conn, int argc, char **argv);
static void mlib_query (xmmsc_connection_t *conn, int argc, char **argv);
static void mlib_queryadd (xmmsc_connection_t *conn, int argc, char **argv);
static void mlib_playlist_list (xmmsc_connection_t *conn, int argc, char **argv);
static void mlib_playlist_save (xmmsc_connection_t *conn, int argc, char **argv);
static void mlib_playlist_load (xmmsc_connection_t *conn, int argc, char **argv);
static void mlib_playlists_list (xmmsc_connection_t *conn, int argc, char **argv);
static void mlib_playlist_import (xmmsc_connection_t *conn, int argc, char **argv);
static void mlib_playlist_export (xmmsc_connection_t *conn, int argc, char **argv);
static void mlib_playlist_remove (xmmsc_connection_t *conn, int argc, char **argv);
static void mlib_addpath (xmmsc_connection_t *conn, int argc, char **argv);
static void mlib_rehash (xmmsc_connection_t *conn, int argc, char **argv);

cmds mlib_commands[] = {
	{ "add", "[url] - Add 'url' to medialib", mlib_add },
	{ "loadall", "Load everything from the mlib to the playlist", mlib_loadall },
	{ "searchadd", "[artist=Dismantled] ... - Search for, and add songs to playlist", mlib_searchadd },
	{ "search", "[artist=Dismantled] ... - Search for songs matching criteria", mlib_search },
	{ "query", "[\"raw sql statement\"] - Send a raw SQL statement to the mlib", mlib_query },
	{ "queryadd", "[\"raw sql statement\"] - Send a raw SQL statement to the mlib and add all entries to playlist", mlib_queryadd },
	{ "list_playlist", "[playlistname] - List 'playlistname' stored in medialib", mlib_playlist_list },
	{ "save_playlist", "[playlistname] - Save current playlist to 'playlistname'", mlib_playlist_save },
	{ "load_playlist", "[playlistname] - Load 'playlistname' stored in medialib", mlib_playlist_load },
	{ "playlists_list", "List all available playlists", mlib_playlists_list },
	{ "import_playlist", "[name] [filename] - Import playlist from file", mlib_playlist_import },
	{ "export_playlist", "[playlistname] [mimetype] - Export playlist", mlib_playlist_export },
	{ "remove_playlist", "[playlistname] - Remove a playlist", mlib_playlist_remove },
	{ "addpath", "[path] - Import metadata from all media files under 'path'", mlib_addpath },
	{ "rehash", "Force the medialib to check whether its data is up to date", mlib_rehash },

	{ NULL, NULL, NULL },
};

static void
mlib_add (xmmsc_connection_t *conn, int argc, char **argv)
{
	xmmsc_result_t *res;
	gint i;

	for (i = 3; argv[i]; i++) {
		char *url = format_url (argv[i]);
		if (!url) return;
		res = xmmsc_medialib_add_entry (conn, url);
		free (url);
		xmmsc_result_wait (res);
		printf ("Added %s to medialib\n", argv[i]);
		xmmsc_result_unref (res);
	}
}

static void
mlib_loadall (xmmsc_connection_t *conn, int argc, char **argv)
{
	xmmsc_result_t *res;
	res = xmmsc_medialib_add_to_playlist (conn, "select id from Media where key='url'");
	xmmsc_result_wait (res);
	xmmsc_result_unref (res);
}

static void
free_query_spec (xmmsc_query_attribute_t *specs, gint num_attribs)
{
	gint i;

	if (!specs) {
		g_assert (!num_attribs);
		return;
	}

	for (i = 0; i < num_attribs; i++) {
		free (specs[i].key);
		free (specs[i].value);
	}

	g_free (specs);
}

static char *
mlib_query_from_args (int argc, char **argv)
{
	gchar *query;
	gchar **s;
	guint i, num_attribs;
	xmmsc_query_attribute_t *query_spec;

	if (argc < 4) {
		print_error ("Supply a query");
	}

	num_attribs = argc - 3;
	query_spec = g_new0 (xmmsc_query_attribute_t, num_attribs);

	for (i = 0; i < num_attribs; i++) {
		s = g_strsplit (argv[i + 3], "=", 0);
		g_assert (s);

		if (!s[0] || !s[1]) {
			g_strfreev (s);
			free_query_spec (query_spec, num_attribs);

			return NULL;
		}

		query_spec[i].key = xmmsc_sqlite_prepare_string (s[0]);
		query_spec[i].value = xmmsc_sqlite_prepare_string (s[1]);
		g_strfreev (s);

		if (!query_spec[i].key || !query_spec[i].value) {
			free_query_spec (query_spec, num_attribs);

			return NULL;
		}
	}

	query = xmmsc_querygen_and (query_spec, num_attribs);

	free_query_spec (query_spec, num_attribs);

	return query;
}

static void
mlib_queryadd (xmmsc_connection_t *conn, int argc, char **argv)
{'fraction_non_alphanumeric': 0.12734637423434103, 'fraction_numerical': 0.004742145820983995, 'mean_word_length': 4.986398580721467, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6058663', 'n_tokens_mistral': 3776, 'n_tokens_neox': 3716, 'n_words': 1094}