<commit_before>/*  XMMS2 - X Music Multiplexer System
 *  Copyright (C) 2003-2007 XMMS2 Team
 *
 *  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 <xmmsclient/xmmsclient.h>
#include <string.h>

#include <glib.h>
#include <glib/gprintf.h>

#include "main.h"
#include "status.h"
#include "utils.h"
#include "readline.h"
#include "cli_cache.h"
#include "cli_infos.h"

static void
status_update_playback (cli_infos_t *infos, status_entry_t *entry)
{
	xmmsc_result_t *res;
	guint status;
	gchar *playback;

	res = xmmsc_playback_status (infos->sync);
	xmmsc_result_wait (res);

	if (!xmmsc_result_iserror (res)) {
		xmmsc_result_get_uint (res, &status);

		switch (status) {
		case XMMS_PLAYBACK_STATUS_STOP:
			playback = g_strdup ("Stopped");
			break;
		case XMMS_PLAYBACK_STATUS_PLAY:
			playback = g_strdup ("Playing");
			break;
		case XMMS_PLAYBACK_STATUS_PAUSE:
			playback = g_strdup ("Paused");
			break;
		default:
			playback = g_strdup ("Unknown");
		}

		g_hash_table_insert (entry->data, "playback_status", playback);

	} else {
		g_printf (_("Server error: %s\n"), xmmsc_result_get_error (res));
	}

	xmmsc_result_unref (res);
}

static void
status_update_info (cli_infos_t *infos, status_entry_t *entry)
{
	xmmsc_result_t *res;
	guint currid;
	gint duration, min, sec;
	const gchar *title, *artist;

	currid = g_array_index (infos->cache->active_playlist, guint,
	                        infos->cache->currpos);

	res = xmmsc_medialib_get_info (infos->sync, currid);
	xmmsc_result_wait (res);

	/* FIXME: use xmmsc_entry_format ? */
	if (!xmmsc_result_iserror (res)) {
		xmmsc_result_get_dict_entry_string (res, "title", &title);
		xmmsc_result_get_dict_entry_string (res, "artist", &artist);

		/* + 500 for rounding */
		xmmsc_result_get_dict_entry_int (res, "duration", &duration);
		sec = (duration+500) / 1000;
		min = sec / 60;
		sec = sec % 60;

		g_hash_table_insert (entry->data, "title", g_strdup (title));
		g_hash_table_insert (entry->data, "artist", g_strdup (artist));
		g_hash_table_insert (entry->data, "duration",
		                     g_strdup_printf ("%02d:%02d", min, sec));
	}

	xmmsc_result_unref (res);
}

static void
status_update_playtime (cli_infos_t *infos, status_entry_t *entry)
{
	xmmsc_result_t *res;
	guint playtime, min, sec;
	gchar *formated;

	res = xmmsc_playback_playtime (infos->sync);
	xmmsc_result_wait (res);

	if (!xmmsc_result_iserror (res)) {
		xmmsc_result_get_uint (res, &playtime);
		
		/* + 500 for rounding */
		sec = (playtime+500) / 1000;
		min = sec / 60;
		sec = sec % 60;

		formated = g_strdup_printf ("%02d:%02d", min, sec);

		g_hash_table_insert (entry->data, "playtime", formated);

	} else {
		g_printf (_("Server error: %s\n"), xmmsc_result_get_error (res));
	}

	xmmsc_result_unref (res);
}<commit_msg>OTHER: Support for other (general) info fields in status format string.<commit_after>/*  XMMS2 - X Music Multiplexer System
 *  Copyright (C) 2003-2007 XMMS2 Team
 *
 *  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 <xmmsclient/xmmsclient.h>
#include <string.h>

#include <glib.h>
#include <glib/gprintf.h>

#include "main.h"
#include "status.h"
#include "utils.h"
#include "readline.h"
#include "cli_cache.h"
#include "cli_infos.h"

static gchar *format_time (gint duration);

static void
status_update_playback (cli_infos_t *infos, status_entry_t *entry)
{
	xmmsc_result_t *res;
	guint status;
	gchar *playback;

	res = xmmsc_playback_status (infos->sync);
	xmmsc_result_wait (res);

	if (!xmmsc_result_iserror (res)) {
		xmmsc_result_get_uint (res, &status);

		switch (status) {
		case XMMS_PLAYBACK_STATUS_STOP:
			playback = g_strdup (_("Stopped"));
			break;
		case XMMS_PLAYBACK_STATUS_PLAY:
			playback = g_strdup (_("Playing"));
			break;
		case XMMS_PLAYBACK_STATUS_PAUSE:
			playback = g_strdup (_("Paused"));
			break;
		default:
			playback = g_strdup (_("Unknown"));
		}

		g_hash_table_insert (entry->data, "playback_status", playback);

	} else {
		g_printf (_("Server error: %s\n"), xmmsc_result_get_error (res));
	}

	xmmsc_result_unref (res);
}

static void
status_update_info (cli_infos_t *infos, status_entry_t *entry)
{
	xmmsc_result_t *res;
	guint currid;
	gint i;

	const gchar *time_fields[] = { "duration", NULL };
	const gchar *noinfo_fields[] = { "playback_status", "playtime", NULL };

	currid = g_array_index (infos->cache->active_playlist, guint,
	                        infos->cache->currpos);

	res = xmmsc_medialib_get_info (infos->sync, currid);
	xmmsc_result_wait (res);

	if (!xmmsc_result_iserror (res)) {
		GList *it;

		for (it = g_list_first (entry->format);
		     it != NULL;
		     it = g_list_next (it)) {

			gint ival;
			gchar *value, *field;
			const gchar *sval;
			xmmsc_result_value_type_t type;

			field = (gchar *)it->data + 2;

			for (i = 0; noinfo_fields[i] != NULL; i++) {
				if (!strcmp (field, noinfo_fields[i])) {
					goto not_info_field;
				}
			}

			type = xmmsc_result_get_dict_entry_type (res, field);
			switch (type) {
			case XMMSC_RESULT_VALUE_TYPE_STRING:
				xmmsc_result_get_dict_entry_string (res, field, &sval);
				value = g_strdup (sval);
				break;

			case XMMSC_RESULT_VALUE_TYPE_INT32:
				xmmsc_result_get_dict_entry_int (res, field, &ival);

				for (i = 0; time_fields[i] != NULL; i++) {
					if (!strcmp (time_fields[i], field)) {
						break;
					}
				}

				if (time_fields[i] != NULL) {
					value = format_time (ival);
				} else {
					value = g_strdup_printf ("%d", ival);
				}
				break;

			default:
				value = g_strdup (_("invalid field"));
			}

			/* FIXME: work with undefined fileds! pll parser? */
			g_hash_table_insert (entry->data, field, value);

		    not_info_field:
			;
		}

	} else {
		g_printf (_("Server error: %s\n"), xmmsc_result_get_error (res));
	}

	xmmsc_result_unref (res);
}

static void
status_update_playtime (cli_infos_t *infos, status_entry_t *entry)
{
	xmmsc_result_t *res;
	guint playtime;

	res = xmmsc_playback_playtime (infos->sync);
	xmmsc_result_wait (res);

	if (!xmmsc_result_iserror (res)) {
		xmmsc_result_get_uint (res, &playtime);
		
		g_hash_table_insert (entry->data, "playtime", format_time (playtime));

	} else {
		g_printf (_("Server error: %s\n"), xmmsc_result_get_error (res));
	}

	xmmsc_result_unref (res);
}

/* Returned string must be freed by the caller */
static gchar *
format_time (gint duration)
{
	gint min, sec;

	/* +500 for rounding */
	sec = (duration+500) / 1000;
	min = sec / 60;
	sec = sec % 60;

	return g_strdup_printf ("%02d:%02d", min, sec);
}
{'fraction_non_alphanumeric': 0.12326961107448912, 'fraction_numerical': 0.01094264996704021, 'mean_word_length': 4.017195767195767, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 35, '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': '2236826', 'n_tokens_mistral': 3149, 'n_tokens_neox': 2889, 'n_words': 727}