Setting list values



I'm having trouble with GConf lists. I'm writing an applet to display
one or more lm-sensors gauges, using GConf for its options, and as the
number of gauges is configurable, I thought it would be a good idea to
use lists for the options for each gauge. So I have a list of themes, a
list of sensors to read, a list of minimum reading on the gauge, ditto
maximum etc.

The problem is, when I try to set the values, the strings either get
written as NULL, or gobbledegook (invalid UTF-8) which fails, and the
ints as badly out of range values.

Are lists not really the best thing to use anyway? They don't seem all
that well supported; for example Gnome 2's panel applet library has its
own interface to GConf which doesn't support lists at all. I wondered if
it would be better to have a separate key for each gauge's option. I
presume there's no problem creating keys on the fly, but is there a way
to make schemas apply to a related set of keys? For example, if I had
/apps/tgauge/sensor/1, /apps/tgauge/sensor/2 etc, could I make a schema
for /apps/tgauge/sensor, or alternatively use wildcards?

Anyway, here are bits of the code I've got at the moment, which isn't
working properly. I've checked that the arrays being passed in (sensors
and temps) have the correct number of elements containing sane values.

typedef struct tgConfData tgConfData;

struct tgConfData
{
    tgSetData *tgset;
    GConfClient *client;
    gchar *top_dir;
    gint ngauges;
};

static void tgconf_set_list(tgConfData *tgc, const gchar *key,
        GConfValueType val_type, const void *array)
{
    gint gauge;
    GError *err = NULL;
    GSList *list = NULL;
    GSList *item;
    gint const *int_array = array;
    gchar const * const *str_array = array;

    for (gauge = 0; gauge < tgc->ngauges; ++gauge)
    {
	GConfValue *gcv = gconf_value_new(val_type);

	if (val_type == GCONF_VALUE_STRING)
	{
	    gconf_value_set_string(gcv, str_array[gauge]);
	}
	else if (val_type == GCONF_VALUE_INT)
	{
	    gconf_value_set_int(gcv, int_array[gauge]);
	}
	else
	{
	    g_assert_not_reached();
	}
	list = g_slist_append(list, gcv);
    }
    gconf_client_set_list(tgc->client, key, val_type, list, &err);
    if (err)
    {
	g_warning(_("Error setting '%s' options: %s"), key, err->message);
	g_error_free(err);
    }
    for (item = list; item; item = g_slist_next(item))
    {
	gconf_value_free(item->data);
	item->data = NULL;
    }
    g_slist_free(list);
}

#define TGCONF_SET_STRING_LIST(tgc, key, a) \
    tgconf_set_list((tgc), (key), GCONF_VALUE_STRING, (a))

#define TGCONF_SET_INT_LIST(tgc, key, a) \
    tgconf_set_list((tgc), (key), GCONF_VALUE_INT, (a))

void tgconf_set_sensors(tgConfData *tgc, gchar const * const *sensors)
{
    TGCONF_SET_STRING_LIST(tgc, "/apps/tgauge/sensors", sensors);
}

void tgconf_set_maxs(tgConfData *tgc, const gint *temps)
{
    TGCONF_SET_INT_LIST(tgc, "/apps/tgauge/maxs", temps);
}

-- 
TH * http://www.realh.co.uk



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]