358 lines
9.5 KiB
C
358 lines
9.5 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"
|
|
|
|
static void db_param_param_init (GvnParamInterface * iface);
|
|
static void db_param_on_master_value_changed (GvnParam * master, const GValue * value, DbParam * self);
|
|
|
|
/**
|
|
* SECTION: db-param
|
|
* @Short_description: representation of the value of a field
|
|
* @Title: DbParam
|
|
*
|
|
* This class represents the value of a field in a #DbIterator.
|
|
**/
|
|
G_DEFINE_TYPE_WITH_CODE (DbParam, db_param, G_TYPE_OBJECT,
|
|
G_IMPLEMENT_INTERFACE (GVN_TYPE_PARAM,
|
|
db_param_param_init)
|
|
);
|
|
|
|
/**
|
|
* db_param_new:
|
|
* @column_name: col name of #DbModel associated to param.
|
|
*
|
|
* Creates a new #DbParam.
|
|
*
|
|
* Return value: (transfer full): 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: (transfer full): 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_put_value (DbParam * self, const GValue * value)
|
|
{
|
|
if (!gvn_value_ccopy (value, self->value))
|
|
return;
|
|
|
|
g_signal_emit_by_name (self, "value-changed", value);
|
|
|
|
if (self->master && self->change_master)
|
|
{
|
|
self->change_master = FALSE;
|
|
gvn_param_request_value (self->master, value, NULL);
|
|
self->change_master = TRUE;
|
|
}
|
|
}
|
|
|
|
static const GValue * db_param_get_value (DbParam * self)
|
|
{
|
|
return self->value;
|
|
}
|
|
|
|
static gboolean db_param_request_value (DbParam * self, const GValue * value, GError ** err)
|
|
{
|
|
return db_iterator_set_value_by_index (self->iterator, self->column_index, value, err);
|
|
}
|
|
|
|
static void db_param_on_master_value_changed (GvnParam * master, const GValue * value, DbParam * self)
|
|
{
|
|
if (self->change_master)
|
|
{
|
|
self->change_master = FALSE;
|
|
gvn_param_set_value (GVN_PARAM (self), value);
|
|
self->change_master = TRUE;
|
|
}
|
|
}
|
|
|
|
static GvnParam * db_param_get_master (DbParam * self)
|
|
{
|
|
return self->master;
|
|
}
|
|
|
|
static void db_param_set_master (DbParam * self, GvnParam * master)
|
|
{
|
|
if (self->master)
|
|
{
|
|
g_signal_handlers_disconnect_by_func (self->master,
|
|
db_param_on_master_value_changed, self);
|
|
g_clear_object (&self->master);
|
|
}
|
|
if (master)
|
|
{
|
|
self->master = g_object_ref_sink (master);
|
|
g_signal_connect (master, "value-changed",
|
|
G_CALLBACK (db_param_on_master_value_changed), self);
|
|
db_param_on_master_value_changed (master,
|
|
gvn_param_get_value (master), self);
|
|
}
|
|
}
|
|
|
|
static const GvnParamSpec * db_param_get_spec (DbParam * self)
|
|
{
|
|
if (self->column_index != -1)
|
|
return db_iterator_get_spec (self->iterator, self->column_index);
|
|
|
|
return NULL;
|
|
}
|
|
|
|
static GvnParamStatus db_param_get_status (DbParam * self)
|
|
{
|
|
if (db_iterator_get_row (self->iterator) != -1 && self->column_index != -1)
|
|
return GVN_PARAM_STATUS_OK;
|
|
|
|
return GVN_PARAM_STATUS_BUSY;
|
|
}
|
|
|
|
static void db_param_on_iterator_status_changed (DbIterator * iterator, gboolean ready, DbParam * self)
|
|
{
|
|
if (ready && self->column_name)
|
|
self->column_index = db_iterator_get_column_index (self->iterator, self->column_name);
|
|
|
|
g_signal_emit_by_name (self, "spec-changed");
|
|
}
|
|
|
|
static void db_param_on_iterator_iter_changed (DbIterator * iterator, DbParam * self)
|
|
{
|
|
g_signal_emit_by_name (self, "status-changed");
|
|
|
|
if (db_iterator_get_row (self->iterator) != -1 && self->column_index != -1)
|
|
{
|
|
const GValue * value = db_iterator_get_value_by_index (self->iterator, self->column_index);
|
|
db_param_put_value (self, value);
|
|
}
|
|
else
|
|
{
|
|
GValue tmp_value = G_VALUE_INIT;
|
|
g_value_init (&tmp_value, GVN_TYPE_NULL);
|
|
db_param_put_value (self, &tmp_value);
|
|
g_value_unset (&tmp_value);
|
|
}
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Public
|
|
|
|
/**
|
|
* db_param_get_column_index:
|
|
* @self: a #DbParam
|
|
*
|
|
* Gets the iterator column referenced by the param.
|
|
*
|
|
* Return value: the column index.
|
|
**/
|
|
guint db_param_get_column_index (DbParam * self)
|
|
{
|
|
g_return_val_if_fail (DB_IS_PARAM (self), 0);
|
|
|
|
return self->column_index;
|
|
}
|
|
|
|
/**
|
|
* db_param_get_column_name:
|
|
* @self: a #DbParam
|
|
*
|
|
* Gets the iterator column referenced by the param.
|
|
*
|
|
* Return value: the column name.
|
|
**/
|
|
const gchar * db_param_get_column_name (DbParam * self)
|
|
{
|
|
g_return_val_if_fail (DB_IS_PARAM (self), 0);
|
|
|
|
return self->column_name;
|
|
}
|
|
|
|
/**
|
|
* db_param_set_iterator:
|
|
* @self: 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 * self, DbIterator * iterator)
|
|
{
|
|
g_return_if_fail (DB_IS_PARAM (self));
|
|
g_return_if_fail (DB_IS_ITERATOR (iterator));
|
|
g_return_if_fail (!self->iterator);
|
|
|
|
self->iterator = g_object_ref (iterator);
|
|
g_object_connect (iterator
|
|
,"signal::iter-changed", db_param_on_iterator_iter_changed, self
|
|
,"signal::status-changed", db_param_on_iterator_status_changed, self
|
|
,NULL
|
|
);
|
|
db_param_on_iterator_status_changed (self->iterator,
|
|
db_iterator_is_ready (iterator), self);
|
|
db_param_on_iterator_iter_changed (self->iterator, self);
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties
|
|
|
|
enum
|
|
{
|
|
PROP_VALUE = 1
|
|
,PROP_MASTER
|
|
,PROP_ITERATOR
|
|
,PROP_COLUMN_INDEX
|
|
,PROP_COLUMN_NAME
|
|
};
|
|
|
|
static void db_param_set_property (DbParam * self, guint property_id,
|
|
const GValue * value, GParamSpec * pspec)
|
|
{
|
|
switch (property_id)
|
|
{
|
|
case PROP_VALUE:
|
|
db_param_request_value (self, g_value_get_boxed (value), NULL);
|
|
break;
|
|
case PROP_MASTER:
|
|
db_param_set_master (self, g_value_get_object (value));
|
|
break;
|
|
case PROP_ITERATOR:
|
|
db_param_set_iterator (self, g_value_get_object (value));
|
|
break;
|
|
case PROP_COLUMN_INDEX:
|
|
self->column_index = g_value_get_int (value);
|
|
break;
|
|
case PROP_COLUMN_NAME:
|
|
g_free (self->column_name);
|
|
self->column_name = g_value_dup_string (value);
|
|
break;
|
|
default:
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, property_id, pspec);
|
|
}
|
|
}
|
|
|
|
static void db_param_get_property (DbParam * self, guint property_id,
|
|
GValue * value, GParamSpec * pspec)
|
|
{
|
|
switch (property_id)
|
|
{
|
|
case PROP_VALUE:
|
|
g_value_set_boxed (value, self->value);
|
|
break;
|
|
case PROP_MASTER:
|
|
g_value_set_object (value, self->master);
|
|
break;
|
|
case PROP_ITERATOR:
|
|
g_value_set_object (value, self->iterator);
|
|
break;
|
|
case PROP_COLUMN_INDEX:
|
|
g_value_set_int (value, self->column_index);
|
|
break;
|
|
case PROP_COLUMN_NAME:
|
|
g_value_set_string (value, self->column_name);
|
|
break;
|
|
default:
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, property_id, pspec);
|
|
}
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
|
|
|
|
static void db_param_init (DbParam * self)
|
|
{
|
|
self->master = NULL;
|
|
self->change_master = TRUE;
|
|
self->column_index = -1;
|
|
self->column_name = NULL;
|
|
self->iterator = NULL;
|
|
|
|
self->value = g_new0 (GValue, 1);
|
|
g_value_init (self->value, GVN_TYPE_NULL);
|
|
}
|
|
|
|
static void db_param_finalize (DbParam * self)
|
|
{
|
|
db_param_set_master (self, NULL);
|
|
|
|
if (self->iterator)
|
|
{
|
|
g_object_disconnect (self->iterator
|
|
,"any_signal", db_param_on_iterator_iter_changed, self
|
|
,"any_signal", db_param_on_iterator_status_changed, self
|
|
,NULL
|
|
);
|
|
g_clear_object (&self->iterator);
|
|
}
|
|
|
|
g_value_unset (self->value);
|
|
g_free (self->value);
|
|
g_free (self->column_name);
|
|
G_OBJECT_CLASS (db_param_parent_class)->finalize (G_OBJECT (self));
|
|
}
|
|
|
|
static void db_param_class_init (DbParamClass * klass)
|
|
{
|
|
GObjectClass * k = G_OBJECT_CLASS (klass);
|
|
k->finalize = (GObjectFinalizeFunc) db_param_finalize;
|
|
k->set_property = (GObjectSetPropertyFunc) db_param_set_property;
|
|
k->get_property = (GObjectGetPropertyFunc) db_param_get_property;
|
|
|
|
g_object_class_override_property (k, PROP_VALUE, "value");
|
|
g_object_class_override_property (k, PROP_MASTER, "master");
|
|
|
|
g_object_class_install_property (k, PROP_ITERATOR,
|
|
g_param_spec_object ("iterator"
|
|
,_("Iterator")
|
|
,_("The iterator owner of param")
|
|
,DB_TYPE_ITERATOR
|
|
,G_PARAM_READWRITE
|
|
));
|
|
g_object_class_install_property (k, PROP_COLUMN_INDEX,
|
|
g_param_spec_int ("column-index"
|
|
,_("Column index")
|
|
,_("The referenced column index")
|
|
,-1 ,G_MAXINT ,-1
|
|
,G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE
|
|
));
|
|
g_object_class_install_property (k, PROP_COLUMN_NAME,
|
|
g_param_spec_string ("column-name"
|
|
,_("Column name")
|
|
,_("The referenced column name")
|
|
,NULL
|
|
,G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE
|
|
));
|
|
}
|
|
|
|
static void db_param_param_init (GvnParamInterface * iface)
|
|
{
|
|
iface->get_value = (GvnParamGetValueFunc) db_param_get_value;
|
|
iface->request_value = (GvnParamRequestValueFunc) db_param_request_value;
|
|
iface->get_master = (GvnParamGetMasterFunc) db_param_get_master;
|
|
iface->set_master = (GvnParamSetMasterFunc) db_param_set_master;
|
|
iface->get_spec = (GvnParamGetSpecFunc) db_param_get_spec;
|
|
iface->get_status = (GvnParamGetStatusFunc) db_param_get_status;
|
|
}
|