From 880509beece11b1b673073cb283c9b605def4344 Mon Sep 17 00:00:00 2001 From: Perry Werneck Date: Tue, 26 May 2020 15:41:28 -0300 Subject: [PATCH] Setting element properties from xml definition. --- src/objects/keypad/attribute.c | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/objects/keypad/element.c | 32 ++++++++++++++++++++++++++++++-- src/objects/keypad/keypad.cbp | 3 +++ src/objects/keypad/private.h | 11 +++++++---- 4 files changed, 179 insertions(+), 6 deletions(-) create mode 100644 src/objects/keypad/attribute.c diff --git a/src/objects/keypad/attribute.c b/src/objects/keypad/attribute.c new file mode 100644 index 0000000..a565a3e --- /dev/null +++ b/src/objects/keypad/attribute.c @@ -0,0 +1,139 @@ +/* + * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270 + * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a + * aplicativos mainframe. Registro no INPI sob o nome G3270. + * + * Copyright (C) <2008> + * + * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob + * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela + * Free Software Foundation. + * + * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER + * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO + * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para + * obter mais detalhes. + * + * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este + * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin + * St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Este programa está nomeado como - e possui - linhas de código. + * + * Contatos: + * + * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck) + * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça) + * + */ + + #include "private.h" + #include + +/*---[ Globals & Object definition ]----------------------------------------------------------------*/ + + struct Attribute { + GObject * object; + gboolean translatable; + GParamSpec *spec; + }; + +/*---[ Implement ]----------------------------------------------------------------------------------*/ + + + static void parse_error (GMarkupParseContext *context, GError *error, gpointer data) { + g_free(data); + } + + static void parse_text(GMarkupParseContext *context, const gchar *text, gsize text_len, gpointer user_data, GError **error) { + + if( ((struct Attribute *) user_data)->translatable ) { + text = gettext(text); + } + + GParamSpec *spec = ((struct Attribute *) user_data)->spec; + + debug("%s=\"%s\"",spec->name,text); + + GValue value = G_VALUE_INIT; + g_value_init(&value,spec->value_type); + + switch(spec->value_type) { + case G_TYPE_STRING: + g_value_set_string(&value,text); + break; + + case G_TYPE_UINT: + g_value_set_uint(&value,(unsigned int) atoi(text)); + break; + + case G_TYPE_INT: + g_value_set_int(&value, atoi(text)); + break; + + default: + g_set_error_literal( + error, + g_quark_from_static_string("keypad"), + ENOENT, + _( "Invalid or unknown property type" ) + ); + + g_value_unset(&value); + return; + + } + + g_object_set_property(((struct Attribute *) user_data)->object,spec->name,&value); + g_value_unset(&value); + } + + void attribute_element_start(GMarkupParseContext *context,const gchar **names,const gchar **values, GObject *parent, GError **error) { + + struct Attribute * data = g_new0(struct Attribute,1); + const gchar *name; + + data->object = parent; + + if(!g_markup_collect_attributes( + "attribute",names,values,error, + G_MARKUP_COLLECT_BOOLEAN|G_MARKUP_COLLECT_OPTIONAL, "translatable", &data->translatable, + G_MARKUP_COLLECT_STRING, "name", &name, + G_MARKUP_COLLECT_INVALID + )) { + + g_free(data); + return; + + } + + data->spec = g_object_class_find_property(G_OBJECT_GET_CLASS(parent),name); + if(!data->spec) { + g_set_error_literal( + error, + g_quark_from_static_string("keypad"), + ENOENT, + _( "Invalid or unknown property name" ) + ); + g_free(data); + return; + } + + static const GMarkupParser parser = { + NULL, + NULL, + parse_text, + NULL, + parse_error + }; + + g_markup_parse_context_push(context, &parser, data); + + } + + void attribute_element_end(GMarkupParseContext *context, GObject *parent, GError **error) { + + struct Attribute * data = g_markup_parse_context_pop(context); + g_free(data); + + } diff --git a/src/objects/keypad/element.c b/src/objects/keypad/element.c index 1d58c54..454ba9c 100644 --- a/src/objects/keypad/element.c +++ b/src/objects/keypad/element.c @@ -89,6 +89,16 @@ ) ); + g_object_class_install_property(object_class, PROP_ACTION, + g_param_spec_string ( + I_("action"), + I_("action"), + N_("The name of associated action"), + NULL, + G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE + ) + ); + g_object_class_install_property(object_class, PROP_LABEL, g_param_spec_string ( I_("label"), @@ -206,6 +216,14 @@ } break; + case PROP_ICON_NAME: + + if(element->icon_name) { + g_free(element->icon_name); + element->icon_name = g_value_dup_string(value); + } + break; + case PROP_ACTION: if(element->action) { @@ -235,13 +253,23 @@ } } - static void element_start(GMarkupParseContext *context, const gchar *element_name, const gchar **names,const gchar **values, GList *keypads, GError **error) { + static void element_start(GMarkupParseContext *context, const gchar *element_name, const gchar **names,const gchar **values, GObject *element, GError **error) { + + if(!g_ascii_strcasecmp(element_name,"attribute")) { + attribute_element_start(context,names,values,element,error); + return; + } debug("%s(%s)",__FUNCTION__,element_name); } - static void element_end(GMarkupParseContext *context, const gchar *element_name, GList *keypads, GError **error) { + static void element_end(GMarkupParseContext *context, const gchar *element_name, GObject *element, GError **error) { + + if(!g_ascii_strcasecmp(element_name,"attribute")) { + attribute_element_end(context,element,error); + return; + } debug("%s(%s)",__FUNCTION__,element_name); diff --git a/src/objects/keypad/keypad.cbp b/src/objects/keypad/keypad.cbp index 6153cdd..bfd635f 100644 --- a/src/objects/keypad/keypad.cbp +++ b/src/objects/keypad/keypad.cbp @@ -39,6 +39,9 @@ + + diff --git a/src/objects/keypad/private.h b/src/objects/keypad/private.h index 0ffc3fe..65e8aec 100644 --- a/src/objects/keypad/private.h +++ b/src/objects/keypad/private.h @@ -103,12 +103,15 @@ }; - void keypad_model_set_position(GObject *model, const gchar *position); - void keypad_model_parse_context(GObject *model, GMarkupParseContext *context); + G_GNUC_INTERNAL void keypad_model_set_position(GObject *model, const gchar *position); + G_GNUC_INTERNAL void keypad_model_parse_context(GObject *model, GMarkupParseContext *context); - const gchar * keypad_model_get_position(GObject *mode); + G_GNUC_INTERNAL const gchar * keypad_model_get_position(GObject *mode); - void keypad_model_element_parse_context(GObject *element, GMarkupParseContext *context); + G_GNUC_INTERNAL void keypad_model_element_parse_context(GObject *element, GMarkupParseContext *context); + + G_GNUC_INTERNAL void attribute_element_start(GMarkupParseContext *context,const gchar **names,const gchar **values, GObject *parent, GError **error); + G_GNUC_INTERNAL void attribute_element_end(GMarkupParseContext *context, GObject *parent, GError **error); G_END_DECLS -- libgit2 0.21.2