263 lines
6.8 KiB
C
263 lines
6.8 KiB
C
/*
|
|
* Copyright (C) 2012 - Juan Ferrer Toribio
|
|
*
|
|
* 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 3 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, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "db-param.h"
|
|
|
|
G_DEFINE_TYPE (DbParam, db_param, GVN_TYPE_PARAM);
|
|
|
|
/**
|
|
* db_param_new:
|
|
* @column_name: col name of #DbModel associated to param.
|
|
*
|
|
* Creates a new #DbParam.
|
|
*
|
|
* Return value: the new #DbParam
|
|
**/
|
|
GvnParam * db_param_new (const gchar * column_name)
|
|
{
|
|
return g_object_new (DB_TYPE_PARAM, "column-name", column_name, NULL);
|
|
}
|
|
|
|
/**
|
|
* db_param_new_with_index:
|
|
* @column_index: col index of #DbModel associated to param.
|
|
*
|
|
* Creates a new #DbParam.
|
|
*
|
|
* Return value: the new #DbParam
|
|
**/
|
|
GvnParam * db_param_new_with_index (guint column_index)
|
|
{
|
|
return g_object_new (DB_TYPE_PARAM, "column-index", column_index, NULL);
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Private
|
|
|
|
static void db_param_on_value_changed (DbParam * obj, const GValue * value, gpointer data)
|
|
{
|
|
db_iterator_set_value (obj->iterator, obj->column_index, value, NULL);
|
|
}
|
|
|
|
static void db_param_on_iterator_status_changed (DbIterator * iterator, gboolean ready, DbParam * obj)
|
|
{
|
|
GvnParamClass * klass = GVN_PARAM_GET_CLASS (obj);
|
|
|
|
if (ready)
|
|
{
|
|
if (obj->column_name)
|
|
obj->column_index = db_iterator_get_column_index (obj->iterator, obj->column_name);
|
|
|
|
klass->set_spec (GVN_PARAM (obj),
|
|
db_iterator_get_spec (obj->iterator, obj->column_index));
|
|
}
|
|
}
|
|
|
|
static void db_param_on_iterator_iter_changed (DbIterator * iterator, DbParam * obj)
|
|
{
|
|
const GValue * value;
|
|
GvnParamClass * klass = GVN_PARAM_GET_CLASS (obj);
|
|
|
|
if (db_iterator_get_row (obj->iterator) != -1 && obj->column_index != -1)
|
|
{
|
|
klass->set_status (GVN_PARAM (obj), GVN_PARAM_STATUS_OK);
|
|
value = db_iterator_get_value (obj->iterator, obj->column_index);
|
|
}
|
|
else
|
|
{
|
|
klass->set_status (GVN_PARAM (obj), GVN_PARAM_STATUS_BUSY);
|
|
value = NULL;
|
|
}
|
|
|
|
g_signal_handlers_block_by_func (obj, db_param_on_value_changed, NULL);
|
|
|
|
if (!value)
|
|
{
|
|
GValue tmp_value = {0};
|
|
g_value_init (&tmp_value, GVN_TYPE_NULL);
|
|
klass->put_value (GVN_PARAM (obj), &tmp_value);
|
|
g_value_unset (&tmp_value);
|
|
}
|
|
else
|
|
klass->put_value (GVN_PARAM (obj), value);
|
|
|
|
g_signal_handlers_unblock_by_func (obj, db_param_on_value_changed, NULL);
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Public
|
|
|
|
/**
|
|
* db_param_get_column_index:
|
|
* @obj: a #DbParam
|
|
*
|
|
* Gets the iterator column referenced by the param.
|
|
*
|
|
* Return value: the column index.
|
|
**/
|
|
guint db_param_get_column_index (DbParam * obj)
|
|
{
|
|
g_return_val_if_fail (DB_IS_PARAM (obj), 0);
|
|
|
|
return obj->column_index;
|
|
}
|
|
|
|
/**
|
|
* db_param_get_column_name:
|
|
* @obj: a #DbParam
|
|
*
|
|
* Gets the iterator column referenced by the param.
|
|
*
|
|
* Return value: the column name.
|
|
**/
|
|
const gchar * db_param_get_column_name (DbParam * obj)
|
|
{
|
|
g_return_val_if_fail (DB_IS_PARAM (obj), 0);
|
|
|
|
return obj->column_name;
|
|
}
|
|
|
|
/**
|
|
* db_param_set_iterator:
|
|
* @obj: a #DbParam
|
|
* @iterator: the #DbIterator
|
|
*
|
|
* Sets the iterator that updates the param value, this property can be set if
|
|
* the parameter does not have a iterator associated yet.
|
|
**/
|
|
void db_param_set_iterator (DbParam * obj, DbIterator * iterator)
|
|
{
|
|
g_return_if_fail (DB_IS_PARAM (obj));
|
|
g_return_if_fail (DB_IS_ITERATOR (iterator));
|
|
g_return_if_fail (!obj->iterator);
|
|
|
|
obj->iterator = g_object_ref (iterator);
|
|
g_object_connect (iterator
|
|
,"signal::iter-changed", db_param_on_iterator_iter_changed, obj
|
|
,"signal::status-changed", db_param_on_iterator_status_changed, obj
|
|
,NULL
|
|
);
|
|
db_param_on_iterator_status_changed (obj->iterator,
|
|
db_iterator_is_ready (iterator), obj);
|
|
db_param_on_iterator_iter_changed (obj->iterator, obj);
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties
|
|
|
|
enum
|
|
{
|
|
PROP_ITERATOR = 1
|
|
,PROP_COLUMN_INDEX
|
|
,PROP_COLUMN_NAME
|
|
};
|
|
|
|
static void db_param_set_property (DbParam * obj, guint property_id,
|
|
const GValue * value, GParamSpec * pspec)
|
|
{
|
|
switch (property_id)
|
|
{
|
|
case PROP_ITERATOR:
|
|
db_param_set_iterator (obj, g_value_get_object (value));
|
|
break;
|
|
case PROP_COLUMN_INDEX:
|
|
obj->column_index = g_value_get_int (value);
|
|
break;
|
|
case PROP_COLUMN_NAME:
|
|
g_free (obj->column_name);
|
|
obj->column_name = g_value_dup_string (value);
|
|
break;
|
|
default:
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
|
|
}
|
|
}
|
|
|
|
static void db_param_get_property (DbParam * obj, guint property_id,
|
|
GValue * value, GParamSpec * pspec)
|
|
{
|
|
switch (property_id)
|
|
{
|
|
case PROP_ITERATOR:
|
|
g_value_set_object (value, obj->iterator);
|
|
break;
|
|
case PROP_COLUMN_INDEX:
|
|
g_value_set_int (value, obj->column_index);
|
|
break;
|
|
case PROP_COLUMN_NAME:
|
|
g_value_set_string (value, obj->column_name);
|
|
break;
|
|
default:
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
|
|
}
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
|
|
|
|
static void db_param_init (DbParam * obj)
|
|
{
|
|
obj->column_index = -1;
|
|
obj->column_name = NULL;
|
|
obj->iterator = NULL;
|
|
g_signal_connect (obj, "value-changed",
|
|
G_CALLBACK (db_param_on_value_changed), NULL);
|
|
}
|
|
|
|
static void db_param_finalize (DbParam * obj)
|
|
{
|
|
if (obj->iterator)
|
|
{
|
|
g_object_disconnect (obj->iterator
|
|
,"any_signal", db_param_on_iterator_iter_changed, obj
|
|
,"any_signal", db_param_on_iterator_status_changed, obj
|
|
,NULL
|
|
);
|
|
g_clear_object (&obj->iterator);
|
|
}
|
|
|
|
g_signal_handlers_disconnect_by_func (obj,
|
|
db_param_on_value_changed, NULL);
|
|
g_free (obj->column_name);
|
|
G_OBJECT_CLASS (db_param_parent_class)->finalize (G_OBJECT (obj));
|
|
}
|
|
|
|
static void db_param_class_init (DbParamClass * k)
|
|
{
|
|
GObjectClass * klass = G_OBJECT_CLASS (k);
|
|
klass->finalize = (GObjectFinalizeFunc) db_param_finalize;
|
|
klass->set_property = (GObjectSetPropertyFunc) db_param_set_property;
|
|
klass->get_property = (GObjectGetPropertyFunc) db_param_get_property;
|
|
|
|
g_object_class_install_property (klass, PROP_ITERATOR,
|
|
g_param_spec_object ("iterator"
|
|
,_("Iterator")
|
|
,_("The iterator owner of param")
|
|
,DB_TYPE_ITERATOR
|
|
,G_PARAM_READWRITE
|
|
));
|
|
g_object_class_install_property (klass, PROP_COLUMN_INDEX,
|
|
g_param_spec_int ("column-index"
|
|
,_("Column index")
|
|
,_("The referenced column index")
|
|
,-1 ,1024 ,-1
|
|
,G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE
|
|
));
|
|
g_object_class_install_property (klass, PROP_COLUMN_NAME,
|
|
g_param_spec_string ("column-name"
|
|
,_("Column name")
|
|
,_("The referenced column name")
|
|
,NULL
|
|
,G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE
|
|
));
|
|
}
|