glade-parameter.c 7.21 KB
Newer Older
Jose Maria Celorio's avatar
Jose Maria Celorio committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2001 Ximian, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Jose Maria Celorio's avatar
Jose Maria Celorio committed
18 19 20 21 22
 *
 * Authors:
 *   Chema Celorio <chema@celorio.com>
 */

Christian Persch's avatar
Christian Persch committed
23 24
#include "config.h"

25 26 27 28 29 30 31 32 33
/**
 * SECTION:glade-parameter
 * @Title: Glade Parameter
 * @Short_Description: Parameters set in the catalog retrievable from the plugin.
 *
 * Parameters can be set in your catalog file that may determine how
 * your plugin will treat certain widget classes.
 */

Jose Maria Celorio's avatar
Jose Maria Celorio committed
34
#include <stdlib.h> /* for atoi and atof */
35
#include <string.h>
Jose Maria Celorio's avatar
Jose Maria Celorio committed
36 37 38 39

#include "glade.h"
#include "glade-xml-utils.h"
#include "glade-parameter.h"
40
#include "glade-property-class.h"
Jose Maria Celorio's avatar
Jose Maria Celorio committed
41

42 43 44 45 46 47 48 49 50
/**
 * glade_parameter_get_integer:
 * @parameters: a #GList of #GladeParameters
 * @key: a string containing the parameter name
 * @value: a pointer to a #gint
 *
 * Searches through @parameters looking for a #GladeParameter named @key. If
 * found, it stores a #gint representation of its value into @value.
 */
Jose Maria Celorio's avatar
Jose Maria Celorio committed
51 52 53 54 55 56 57 58 59 60
void
glade_parameter_get_integer (GList *parameters, const gchar *key, gint *value)
{
	GladeParameter *parameter;
	GList *list;

	list = parameters;
	for (; list != NULL; list = list->next) {
		parameter = list->data;
		if (strcmp (key, parameter->key) == 0) {
61
			*value = g_ascii_strtoll (parameter->value, NULL, 10);
Jose Maria Celorio's avatar
Jose Maria Celorio committed
62 63 64 65 66
			return;
		}
	}
}

67 68 69 70 71 72 73 74 75
/**
 * glade_parameter_get_float:
 * @parameters: a #GList of #GladeParameters
 * @key: a string containing the parameter name
 * @value: a pointer to a #gfloat
 *
 * Searches through @parameters looking for a #GladeParameter named @key. If
 * found, it stores a #gfloat representation of its value into @value.
 */
Jose Maria Celorio's avatar
Jose Maria Celorio committed
76 77 78 79 80 81 82 83 84 85
void
glade_parameter_get_float (GList *parameters, const gchar *key, gfloat *value)
{
	GladeParameter *parameter;
	GList *list;

	list = parameters;
	for (; list != NULL; list = list->next) {
		parameter = list->data;
		if (strcmp (key, parameter->key) == 0) {
86
			*value = (float) g_ascii_strtod (parameter->value, NULL);
Jose Maria Celorio's avatar
Jose Maria Celorio committed
87 88 89 90 91
			return;
		}
	}
}

92 93 94 95 96 97 98 99 100
/**
 * glade_parameter_get_boolean:
 * @parameters: a #GList of #GladeParameters
 * @key: a string containing the parameter name
 * @value: a pointer to a #gboolean
 *
 * Searches through @parameters looking for a #GladeParameter named @key. If
 * found, it stores a #gboolean representation of its value into @value.
 */
Jose Maria Celorio's avatar
Jose Maria Celorio committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
void
glade_parameter_get_boolean (GList *parameters, const gchar *key, gboolean *value)
{
	GladeParameter *parameter;
	GList *list;

	list = parameters;
	for (; list != NULL; list = list->next) {
		parameter = list->data;
		if (strcmp (key, parameter->key) == 0) {
			if (strcmp (parameter->value, GLADE_TAG_TRUE) == 0)
				*value = TRUE;
			else if (strcmp (parameter->value, GLADE_TAG_FALSE) == 0)
				*value = FALSE;
			else
				g_warning ("Invalid boolean parameter *%s* (%s/%s)",
					   parameter->value, GLADE_TAG_TRUE, GLADE_TAG_FALSE);
			return;
		}
	}
}

123 124 125 126 127 128 129 130 131 132
/**
 * glade_parameter_get_string:
 * @parameters: a #GList of #GladeParameters
 * @key: a string containing the parameter name
 * @value: a pointer to an string
 *
 * Searches through @parameters looking for a #GladeParameter named @key. If
 * found, it stores a newly copied string representation of its value into 
 * @value.
 */
Jose Maria Celorio's avatar
Jose Maria Celorio committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
void
glade_parameter_get_string (GList *parameters, const gchar *key, gchar **value)
{
	GladeParameter *parameter;
	GList *list;

	list = parameters;
	for (; list != NULL; list = list->next) {
		parameter = list->data;
		if (strcmp (key, parameter->key) == 0) {
			if (*value != NULL)
				g_free (*value);
			*value = g_strdup (parameter->value);
			return;
		}
	}
}
150

151 152 153 154 155 156
/**
 * glade_parameter_free:
 * @parameter: a #GladeParameter
 *
 * Frees @parameter and its associated memory.
 */
157
void
158 159
glade_parameter_free (GladeParameter *parameter)
{
160 161
	if (!parameter)
		return;
162 163 164 165 166 167

	g_free (parameter->key);
	g_free (parameter->value);
	g_free (parameter);
}

168 169 170 171 172
/**
 * glade_parameter_new:
 *
 * Returns: a new #GladeParameter
 */
173
GladeParameter *
Jose Maria Celorio's avatar
Jose Maria Celorio committed
174 175 176 177 178 179 180 181 182
glade_parameter_new (void)
{
	GladeParameter *parameter;

	parameter = g_new0 (GladeParameter, 1);

	return parameter;
}

183 184 185 186 187 188
/**
 * glade_parameter_clone:
 * @parameter: a #GladeParameter
 *
 * Returns: a new #GladeParameter cloned from @parameter
 */
189 190 191
GladeParameter *
glade_parameter_clone (GladeParameter *parameter)
{
192
	GladeParameter *clone;
193 194 195 196

	if (parameter == NULL)
		return NULL;

197 198 199
	clone = glade_parameter_new ();
	clone->key = g_strdup (parameter->key);
	clone->value = g_strdup (parameter->value);
200

201
	return clone;
202 203
}

Jose Maria Celorio's avatar
Jose Maria Celorio committed
204
static GladeParameter *
Chema Celorio's avatar
Chema Celorio committed
205
glade_parameter_new_from_node (GladeXmlNode *node)
Jose Maria Celorio's avatar
Jose Maria Celorio committed
206 207 208 209 210 211 212
{
	GladeParameter *parameter;

	if (!glade_xml_node_verify (node, GLADE_TAG_PARAMETER))
		return NULL;
	
	parameter = glade_parameter_new ();
Chema Celorio's avatar
Chema Celorio committed
213 214
	parameter->key   = glade_xml_get_property_string_required (node, GLADE_TAG_KEY, NULL);
	parameter->value = glade_xml_get_property_string_required (node, GLADE_TAG_VALUE, NULL);
Jose Maria Celorio's avatar
Jose Maria Celorio committed
215 216 217 218 219 220 221

	if (!parameter->key || !parameter->value)
		return NULL;

	return parameter;
}

222 223 224 225 226 227 228 229 230 231 232
/**
 * glade_parameter_list_find_by_key:
 * @list: a #GList containing #GladeParameters
 * @key: a string used as a parameter key
 *
 * Searches through @list looking for a node which contains a
 * #GladeParameter matching @key
 * 
 * Returns: the #GList node containing the located #GladeParameter,
 *          or %NULL if none is found
 */
233
static GList *
234 235 236 237 238 239 240 241 242 243 244 245 246 247
glade_parameter_list_find_by_key (GList *list, const gchar *key)
{
	GladeParameter *parameter;
	
	for (; list != NULL; list = list->next) {
		parameter = list->data;
		g_return_val_if_fail (parameter->key != NULL, NULL);
		if (strcmp (parameter->key, key) == 0)
			return list;
	}

	return NULL;
}
		
248 249 250 251 252 253 254 255 256
/**
 * glade_parameter_list_new_from_node:
 * @list: a #GList node
 * @node: a #GladeXmlNode
 *
 * TODO: write me
 *
 * Returns:
 */
257
GList *
Chema Celorio's avatar
Chema Celorio committed
258
glade_parameter_list_new_from_node (GList *list, GladeXmlNode *node)
Jose Maria Celorio's avatar
Jose Maria Celorio committed
259 260
{
	GladeParameter *parameter;
Chema Celorio's avatar
Chema Celorio committed
261
	GladeXmlNode *child;
262
	GList *findme;
Jose Maria Celorio's avatar
Jose Maria Celorio committed
263 264 265 266 267 268 269

	if (!glade_xml_node_verify (node, GLADE_TAG_PARAMETERS))
		return NULL;
	child = glade_xml_search_child (node, GLADE_TAG_PARAMETER);
	if (child == NULL)
		return NULL;

Chema Celorio's avatar
Chema Celorio committed
270
	child = glade_xml_node_get_children (node);
Jose Maria Celorio's avatar
Jose Maria Celorio committed
271

272
	for (; child != NULL; child = glade_xml_node_next (child)) {
Jose Maria Celorio's avatar
Jose Maria Celorio committed
273 274
		if (!glade_xml_node_verify (child, GLADE_TAG_PARAMETER))
			return NULL;
275
		
Jose Maria Celorio's avatar
Jose Maria Celorio committed
276 277 278
		parameter = glade_parameter_new_from_node (child);
		if (parameter == NULL)
			return NULL;
279 280 281 282 283 284 285 286
		/* Is this parameter already there ? just replace
		 * the pointer and free the old one
		 */
		findme = glade_parameter_list_find_by_key (list,
							   parameter->key);
		if (findme) {
			glade_parameter_free (findme->data);
			findme->data = parameter;
287
			continue;
288 289
		}

Jose Maria Celorio's avatar
Jose Maria Celorio committed
290 291 292 293 294 295 296
		list = g_list_prepend (list, parameter);
	}

	list = g_list_reverse (list);
	
	return list;
}