Example #1
0
File: ipc.c Project: dreamerc/xmms2
static gboolean
type_and_msg_to_arg (xmmsv_type_t expected_type, xmmsv_t *argument_list,
                     xmms_object_cmd_arg_t *arg, gint i)
{
	xmmsv_t *arg_value;
	xmmsv_type_t actual_type;

	if (argument_list && xmmsv_list_get (argument_list, i, &arg_value)) {
		xmmsv_ref (arg_value);
	} else {
		arg_value = xmmsv_new_none ();
	}

	actual_type = xmmsv_get_type (arg_value);

	if (actual_type != expected_type) {
		XMMS_DBG ("Expected type %i, but got type %i",
		          expected_type, actual_type);

		xmmsv_unref (arg_value);

		return FALSE;
	} else {
		arg->values[i] = arg_value;

		return TRUE;
	}
}
Example #2
0
/**
 * Returns a collection with a LIMIT operator added
 *
 * @param coll The collection to add the limit operator to
 * @param lim_start The index of the first element to include, or 0 to disable
 * @param lim_len The length of the interval, or 0 to disable
 * @return A new collection with LIMIT operator added
 */
xmmsv_t *
xmmsv_coll_add_limit_operator (xmmsv_t *coll, int lim_start, int lim_len)
{
	xmmsv_t *ret;
	char str[12];

	x_return_val_if_fail (lim_start >= 0 && lim_len >= 0, NULL);

	if (lim_start == 0 && lim_len == 0) {
		return xmmsv_ref (coll);
	}

	ret = xmmsv_new_coll (XMMS_COLLECTION_TYPE_LIMIT);
	xmmsv_coll_add_operand (ret, coll);

	if (lim_start != 0) {
		int count = snprintf (str, sizeof (str), "%i", lim_start);
		if (count > 0 && count < sizeof (str)) {
			xmmsv_coll_attribute_set_string (ret, "start", str);
		} else {
			x_api_warning ("could not set collection limit operator start");
		}
	}

	if (lim_len != 0) {
		int count = snprintf (str, sizeof (str), "%i", lim_len);
		if (count > 0 && count < sizeof (str)) {
			xmmsv_coll_attribute_set_string (ret, "length", str);
		} else {
			x_api_warning ("could not set collection limit operator length");
		}
	}

	return ret;
}
Example #3
0
static VALUE
extract_value (VALUE parent, xmmsv_t *val)
{
	switch (xmmsv_get_type (val)) {
		case XMMSV_TYPE_INT32:
			return int_get (val);
		case XMMSV_TYPE_STRING:
			return string_get (val);
		case XMMSV_TYPE_BIN:
			return bin_get (val);
		case XMMSV_TYPE_COLL:
			return coll_get (val);
		case XMMSV_TYPE_LIST:
			return list_get (parent, val);
		case XMMSV_TYPE_DICT:
			// will be handled below
			break;
		default:
			return Qnil;
	}

	VALUE value;
	RbDict *dict = NULL;

	value = Data_Make_Struct (cRawDict, RbDict,
			c_dict_mark, c_dict_free,
			dict);

	dict->real = xmmsv_ref (val);
	dict->parent = parent;

	rb_obj_call_init (value, 0, NULL);

	return value;
}
Example #4
0
static int
restore_callback (void *userdata, int columns, char **col_strs, char **col_names)
{
    struct db_info *info = userdata;
    static gint previd = -1;
    gint id, type, nsid, i;
    const gchar *label;
    static xmmsv_t *coll = NULL;

    for (i = 0; i < columns; i++) {
        if (!strcmp (col_names[i], "id")) {
            id = atoi (col_strs[i]);
        } else if (!strcmp (col_names[i], "type")) {
            type = atoi (col_strs[i]);
        } else if (!strcmp (col_names[i], "nsid")) {
            nsid = atoi (col_strs[i]);
        } else if (!strcmp (col_names[i], "label")) {
            label = col_strs[i];
        }
    }

    /* Do not duplicate operator if same id */
    if (previd < 0 || id != previd) {
        coll = xmms_collection_dbread_operator (info->db, id, type);
        previd = id;
    }
    else {
        xmmsv_ref (coll);  /* New label references the coll */
    }

    g_hash_table_replace (info->ht[nsid], g_strdup (label), coll);

    return 0;
}
Example #5
0
/**
 * Return a collection with several order-operators added.
 *
 * @param coll	the original collection
 * @param order list of ordering strings or dicts.
 *
 * @return	coll with order-operators added
 */
xmmsv_t *
xmmsv_coll_add_order_operators (xmmsv_t *coll, xmmsv_t *order)
{
	xmmsv_list_iter_t *it;
	xmmsv_t *current, *value;

	x_api_error_if (coll == NULL, "with a NULL coll", NULL);

	xmmsv_ref (coll);

	if (!order) {
		return coll;
	}

	x_api_error_if (!xmmsv_is_type (order, XMMSV_TYPE_LIST),
	                "with a non list order", coll);

	current = coll;

	xmmsv_get_list_iter (order, &it);
	xmmsv_list_iter_last (it);

	while (xmmsv_list_iter_entry (it, &value)) {
		xmmsv_t *ordered;

		ordered = xmmsv_coll_add_order_operator (current, value);
		xmmsv_unref (current);

		current = ordered;
		xmmsv_list_iter_prev (it);
	}

	return current;
}
/* Applies an aggregation function to the leafs in an xmmsv dict tree */
static xmmsv_t *
aggregate_result (xmmsv_t *val, gint depth, aggregate_function_t aggr_func)
{
	xmmsv_dict_iter_t *it;

	if (depth == 0) {
		return aggregate_data (val, aggr_func);
	}

	if (val == NULL && depth > 0) {
		return xmmsv_new_dict();
	}

	/* If it's a dict we call this function recursively on all its values */
	xmmsv_get_dict_iter (val, &it);

	while (xmmsv_dict_iter_valid (it)) {
		xmmsv_t *entry;

		xmmsv_dict_iter_pair (it, NULL, &entry);
		xmmsv_ref (entry);

		entry = aggregate_result (entry, depth - 1, aggr_func);
		xmmsv_dict_iter_set (it, entry);
		xmmsv_unref (entry);

		xmmsv_dict_iter_next (it);
	}

	return val;
}
Example #7
0
static xmmsv_t *
xmmsv_coll_normalize_order_arguments (xmmsv_t *value)
{
	xmmsv_t *order;
	const char *key;

	if (value == NULL) {
		return NULL;
	}

	if (xmmsv_is_type (value, XMMSV_TYPE_DICT)) {
		return xmmsv_ref (value);
	}

	x_api_error_if (!xmmsv_get_string (value, &key),
	                "order entry must be string or dict", NULL);

	order = xmmsv_new_dict ();

	if (key[0] == '-') {
		xmmsv_dict_set_string (order, "direction", "DESC");
		key++;
	}

	if (strcmp (key, "random") == 0) {
		xmmsv_dict_set_string (order, "type", "random");
	} else if (strcmp (key, "id") == 0) {
		xmmsv_dict_set_string (order, "type", "id");
	} else {
		xmmsv_dict_set_string (order, "type", "value");
		xmmsv_dict_set_string (order, "field", key);
	}

	return order;
}
Example #8
0
/**
 * List the ids of all media matched by the given collection.
 * A list of ordering properties can be specified, as well as offsets
 * to only retrieve part of the result set.
 *
 * @param conn  The connection to the server.
 * @param coll  The collection used to query.
 * @param order  The list of properties to order by, passed as an #xmmsv_t list of strings.
 * @param limit_start  The offset at which to start retrieving results (0 to disable).
 * @param limit_len  The maximum number of entries to retrieve (0 to disable).
 */
xmmsc_result_t*
xmmsc_coll_query_ids (xmmsc_connection_t *conn, xmmsv_coll_t *coll,
                      xmmsv_t *order, int limit_start,
                      int limit_len)
{
	xmms_ipc_msg_t *msg;

	x_check_conn (conn, NULL);
	x_api_error_if (!coll, "with a NULL collection", NULL);

	/* default to empty ordering */
	if (!order) {
		order = xmmsv_new_list ();
	} else {
		xmmsv_ref (order);
	}

	msg = xmms_ipc_msg_new (XMMS_IPC_OBJECT_COLLECTION, XMMS_IPC_CMD_QUERY_IDS);
	xmms_ipc_msg_put_collection (msg, coll);
	xmms_ipc_msg_put_int32 (msg, limit_start);
	xmms_ipc_msg_put_int32 (msg, limit_len);
	xmms_ipc_msg_put_value_list (msg, order); /* purposedly skip typing */

	xmmsv_unref (order);

	return xmmsc_send_msg (conn, msg);
}
Example #9
0
void Dict::setValue( xmmsv_t *newval )
{
    if( value_ ) {
        xmmsv_unref( value_ );
    }
    value_ = newval;
    xmmsv_ref( value_ );
}
Example #10
0
PropDict& PropDict::operator=( const PropDict& dict )
{
    Dict::operator=( dict );
    if( propdict_ ) {
        xmmsv_unref( propdict_ );
    }
    propdict_ = dict.propdict_;
    xmmsv_ref( propdict_ );
    return *this;
}
Example #11
0
PropDict::PropDict( xmmsv_t* val ) : Dict( val ), propdict_( val )
{
    xmmsv_ref( propdict_ );

    // Immediately replace with a "flat" dict (default sources)
    xmmsv_t *flat = xmmsv_propdict_to_dict( propdict_, NULL );
    setValue( flat );
    // setValue refs flat, unref here to get back to refcount of 1
    xmmsv_unref ( flat );
}
Example #12
0
/**
 * Format a client-to-client message.
 *
 * Messages are dictionaries of the form:\n
 * "sender" : id of the sender client\n
 * "destination" : id of the destination client\n
 * "id" : 0 if the message doesn't expect reply, else a message id\n
 * "payload" : the contents of the message
 *
 * @param sender the id of the sender client
 * @param dest the id of the destination client
 * @param id the id of the message
 * @param payload the contents of the message
 *
 * @return the formatted message
 *
 * @note Increases the refcount of payload.
 */
xmmsv_t *
xmmsv_c2c_message_format (int sender, int dest, int id,
                          xmmsv_t *payload)
{
	xmmsv_ref (payload);
	return xmmsv_build_dict (XMMSV_DICT_ENTRY_INT ("sender", sender),
	                         XMMSV_DICT_ENTRY_INT ("destination", dest),
	                         XMMSV_DICT_ENTRY_INT ("id", id),
	                         XMMSV_DICT_ENTRY ("payload", payload),
	                         XMMSV_DICT_END);
}
Example #13
0
/**
 * Replace all attributes in the given collection.
 *
 * @param coll The collection in which to set the attribute.
 * @param attributes The new attributes.
 */
void
xmmsv_coll_attributes_set (xmmsv_t *coll, xmmsv_t *attributes)
{
	xmmsv_t *old;

	x_return_if_fail (coll);
	x_return_if_fail (attributes);
	x_return_if_fail (xmmsv_is_type (attributes, XMMSV_TYPE_DICT));

	old = coll->value.coll->attributes;
	coll->value.coll->attributes = xmmsv_ref (attributes);
	xmmsv_unref (old);
}
Example #14
0
/**
 * Replace all operands in the given collection.
 *
 * @param coll The collection in which to set the operands.
 * @param operands The new operands.
 */
void
xmmsv_coll_operands_set (xmmsv_t *coll, xmmsv_t *operands)
{
	xmmsv_t *old;

	x_return_if_fail (coll);
	x_return_if_fail (operands);
	x_return_if_fail (xmmsv_list_restrict_type (operands, XMMSV_TYPE_COLL));

	old = coll->value.coll->operands;
	coll->value.coll->operands = xmmsv_ref (operands);
	xmmsv_unref (old);
}
Example #15
0
/**
 * Replace the idlist in the given collection.
 *
 * @param coll The collection in which to set the idlist.
 * @param operands The new idlist.
 */
void
xmmsv_coll_idlist_set (xmmsv_t *coll, xmmsv_t *idlist)
{
	xmmsv_t *old;

	x_return_if_fail (coll);
	x_return_if_fail (idlist);
	x_return_if_fail (xmmsv_list_restrict_type (idlist, XMMSV_TYPE_INT64));

	old = coll->value.coll->idlist;
	coll->value.coll->idlist = xmmsv_ref (idlist);
	xmmsv_unref (old);
}
Example #16
0
static gint
refresh_active_playlist (xmmsv_t *val, void *udata)
{
    cli_cache_t *cache = (cli_cache_t *) udata;

    if (!xmmsv_is_error (val)) {
        xmmsv_unref (cache->active_playlist);
        cache->active_playlist = xmmsv_ref (val);
    }

    freshness_received (&cache->freshness_active_playlist);

    return TRUE;
}
Example #17
0
/**
 * Return a collection with an order-operator added.
 *
 * @param coll  the original collection
 * @param value an ordering string, optionally starting with "-" (for
 *              descending ordering), followed by a string "id", "random"
 *              or a key identifying a  property, such as "artist" or "album".
 *              Or it can be a dict containing the attributes to set.
 * @return coll with order-operators added
 */
xmmsv_t *
xmmsv_coll_add_order_operator (xmmsv_t *coll, xmmsv_t *value)
{
	value = xmmsv_coll_normalize_order_arguments (value);
	if (value != NULL) {
		xmmsv_t *ordered;

		ordered = xmmsv_new_coll (XMMS_COLLECTION_TYPE_ORDER);
		xmmsv_coll_add_operand (ordered, coll);
		xmmsv_coll_attributes_set (ordered, value);

		xmmsv_unref (value);

		return ordered;
	}

	return xmmsv_ref (coll);
}
Example #18
0
static xmmsv_t *
xmmsv_propdict_to_dict (xmmsv_t *propdict, const char **src_prefs)
{
  xmmsv_ref (propdict);
  return propdict;
}
Example #19
0
/**
 * Unit test predicate
 */
static gboolean
run_unit_test (xmms_medialib_t *mlib, const gchar *name, xmmsv_t *content,
               xmmsv_t *coll, xmmsv_t *specification, xmmsv_t *expected,
               gint format, const gchar *datasetname)
{
	gboolean matches, ordered = FALSE;
	xmmsv_t *ret, *value;
	xmms_medialib_t *medialib;
	xmms_coll_dag_t *dag;
	gint status;

	g_debug ("Running test: %s", name);

	xmms_ipc_init ();
	xmms_config_init ("memory://");
	xmms_config_property_register ("medialib.path", "memory://", NULL, NULL);

	medialib = xmms_medialib_init ();
	dag = xmms_collection_init (medialib);

	populate_medialib (medialib, content);

	memory_status_calibrate (name);

	ret = XMMS_IPC_CALL (dag, XMMS_IPC_CMD_QUERY,
	                     xmmsv_ref (coll),
	                     xmmsv_ref (specification));

	status = memory_status_verify (name);

	xmmsv_dict_get (expected, "result", &value);
	xmmsv_dict_entry_get_int (expected, "ordered", &ordered);

	if (!xmmsv_is_type (ret, XMMSV_TYPE_ERROR)) {
		if (ordered) {
			matches = xmmsv_compare (ret, value);
		} else {
			matches = xmmsv_compare_unordered (ret, value);
		}
	} else {
		matches = FALSE;
	}

	if (matches && status == MEMORY_OK) {
		if (format == FORMAT_CSV) {
			g_print ("\"%s\", 1\n", name);
		} else {
			g_print ("............................................................ Success!");
			g_print ("\r%s \n", name);
		}

	} else {
		if (format == FORMAT_CSV) {
			g_print ("\"%s\", 0\n", name);
		} else {
			g_print ("............................................................ Failure!");
			if (status & MEMORY_LEAK) {
				g_print (" Memory Leaks!");
			}
			if (status & MEMORY_ERROR) {
				g_print (" Memory errors!");
			}

			g_print ("\r%s \n", name);
		}

		if (!matches) {
			g_printerr ("The result:\n");
			xmmsv_dump (ret);
			g_printerr ("Does not equal:\n");
			xmmsv_dump (value);
		}
	}

	xmmsv_unref (ret);

	xmms_object_unref (medialib);
	xmms_object_unref (dag);
	xmms_config_shutdown ();
	xmms_ipc_shutdown ();

	return matches && status == MEMORY_OK;
}
Example #20
0
Dict::Dict( const Dict& dict ) : value_( dict.value_ )
{
    xmmsv_ref( value_ );
}
Example #21
0
PropDict::PropDict( const PropDict& dict )
    : Dict( dict ), propdict_( dict.propdict_ )
{
    xmmsv_ref( propdict_ );
}
Example #22
0
	void Coll::ref()
	{
		xmmsv_ref( coll_ );
	}
/* Converts the temporary value returned by result_to_xmmsv into the real value */
static xmmsv_t *
aggregate_data (xmmsv_t *value, aggregate_function_t aggr_func)
{
	const random_data_t *random_data;
	const avg_data_t *avg_data;
	const set_data_t *set_data;
	gconstpointer data;
	xmmsv_t *ret;
	guint len;

	ret = NULL;
	data = NULL;

	if (value != NULL && xmmsv_is_type (value, XMMSV_TYPE_BIN))
		xmmsv_get_bin (value, (const guchar **) &data, &len);

	switch (aggr_func) {
		case AGGREGATE_FIRST:
		case AGGREGATE_MIN:
		case AGGREGATE_MAX:
		case AGGREGATE_SUM:
			if (value != NULL) {
				ret = xmmsv_ref (value);
			} else {
				ret = xmmsv_new_none ();
			}
			break;
		case AGGREGATE_LIST:
			if (value != NULL) {
				ret = xmmsv_ref (value);
			} else {
				ret = xmmsv_new_list ();
			}
			break;
		case AGGREGATE_RANDOM:
			random_data = data;
			if (random_data != NULL) {
				ret = random_data->data;
			} else {
				ret = xmmsv_new_none ();
			}
			break;
		case AGGREGATE_SET:
			set_data = data;
			if (set_data != NULL) {
				g_hash_table_destroy (set_data->ht);
				ret = set_data->list;
			} else {
				ret = xmmsv_new_list ();
			}
			break;
		case AGGREGATE_AVG:
			avg_data = data;
			if (avg_data != NULL) {
				ret = xmmsv_new_float (avg_data->n ? avg_data->sum * 1.0 / avg_data->n : 0);
			} else {
				ret = xmmsv_new_none ();
			}
			break;
		default:
			g_assert_not_reached ();
	}

	if (value != NULL) {
		xmmsv_unref (value);
	}

	return ret;
}
Example #24
0
static void
operands_each (xmmsv_t *operand, void *user_data)
{
	rb_yield (TO_XMMS_CLIENT_COLLECTION (xmmsv_ref (operand)));
}
Example #25
0
/**
 * Increases the references for the #xmmsv_t
 *
 * @deprecated Use xmmsv_ref instead.
 *
 * @param coll the collection to reference.
 * @return coll
 */
xmmsv_t *
xmmsv_coll_ref (xmmsv_t *coll)
{
	return xmmsv_ref (coll);
}