2005-11-07 09:45:58 -06:00
|
|
|
/********************************************************************\
|
2006-01-08 11:51:29 -06:00
|
|
|
* qofclass.c -- provide QOF parameterized data objects *
|
2005-11-07 09:45:58 -06:00
|
|
|
* Copyright (C) 2002 Derek Atkins <warlord@MIT.EDU> *
|
|
|
|
* *
|
|
|
|
* 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, contact: *
|
|
|
|
* *
|
|
|
|
* Free Software Foundation Voice: +1-617-542-5942 *
|
2005-11-16 23:35:02 -06:00
|
|
|
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
|
|
|
|
* Boston, MA 02110-1301, USA gnu@gnu.org *
|
2005-11-07 09:45:58 -06:00
|
|
|
* *
|
|
|
|
\********************************************************************/
|
|
|
|
|
2017-10-26 04:14:21 -05:00
|
|
|
#include <config.h>
|
2005-11-07 09:45:58 -06:00
|
|
|
#include <glib.h>
|
2014-04-25 15:41:11 -05:00
|
|
|
|
2006-01-08 11:51:29 -06:00
|
|
|
#include "qof.h"
|
2005-11-07 09:45:58 -06:00
|
|
|
#include "qofclass-p.h"
|
|
|
|
|
|
|
|
static QofLogModule log_module = QOF_MOD_CLASS;
|
|
|
|
|
|
|
|
static GHashTable *classTable = NULL;
|
|
|
|
static GHashTable *sortTable = NULL;
|
|
|
|
static gboolean initialized = FALSE;
|
|
|
|
|
|
|
|
static gboolean clear_table (gpointer key, gpointer value, gpointer user_data)
|
|
|
|
{
|
2014-04-25 15:41:11 -05:00
|
|
|
g_hash_table_destroy (static_cast<GHashTable*>(value));
|
2009-09-18 14:40:57 -05:00
|
|
|
return TRUE;
|
2005-11-07 09:45:58 -06:00
|
|
|
}
|
|
|
|
|
2006-01-08 11:51:29 -06:00
|
|
|
/* *******************************************************************/
|
|
|
|
/* PRIVATE FUNCTIONS */
|
|
|
|
|
|
|
|
static gboolean check_init (void)
|
|
|
|
{
|
|
|
|
if (initialized) return TRUE;
|
|
|
|
|
|
|
|
PERR("You must call qof_class_init() before using qof_class.");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
qof_class_init(void)
|
|
|
|
{
|
2009-09-18 14:40:57 -05:00
|
|
|
if (initialized) return;
|
|
|
|
initialized = TRUE;
|
2006-01-08 11:51:29 -06:00
|
|
|
|
2009-09-18 14:40:57 -05:00
|
|
|
classTable = g_hash_table_new (g_str_hash, g_str_equal);
|
|
|
|
sortTable = g_hash_table_new (g_str_hash, g_str_equal);
|
2006-01-08 11:51:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
qof_class_shutdown (void)
|
|
|
|
{
|
2009-09-18 14:40:57 -05:00
|
|
|
if (!initialized) return;
|
|
|
|
initialized = FALSE;
|
2006-01-08 11:51:29 -06:00
|
|
|
|
2009-09-18 14:40:57 -05:00
|
|
|
g_hash_table_foreach_remove (classTable, clear_table, NULL);
|
|
|
|
g_hash_table_destroy (classTable);
|
|
|
|
g_hash_table_destroy (sortTable);
|
2006-01-08 11:51:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
QofSortFunc
|
|
|
|
qof_class_get_default_sort (QofIdTypeConst obj_name)
|
|
|
|
{
|
2009-09-18 14:40:57 -05:00
|
|
|
if (!obj_name) return NULL;
|
2014-04-25 15:41:11 -05:00
|
|
|
return reinterpret_cast<QofSortFunc>(g_hash_table_lookup (sortTable,
|
|
|
|
obj_name));
|
2006-01-08 11:51:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* *******************************************************************/
|
2005-11-07 09:45:58 -06:00
|
|
|
/* PUBLISHED API FUNCTIONS */
|
|
|
|
|
2006-01-08 11:51:29 -06:00
|
|
|
void
|
2005-11-07 09:45:58 -06:00
|
|
|
qof_class_register (QofIdTypeConst obj_name,
|
|
|
|
QofSortFunc default_sort_function,
|
|
|
|
const QofParam *params)
|
|
|
|
{
|
2009-09-18 14:40:57 -05:00
|
|
|
GHashTable *ht;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!obj_name) return;
|
|
|
|
if (!check_init()) return;
|
|
|
|
|
|
|
|
if (default_sort_function)
|
|
|
|
{
|
2014-04-25 15:41:11 -05:00
|
|
|
g_hash_table_insert (sortTable, (char *)obj_name,
|
|
|
|
reinterpret_cast<void*>(default_sort_function));
|
2009-09-18 14:40:57 -05:00
|
|
|
}
|
|
|
|
|
2014-04-25 15:41:11 -05:00
|
|
|
ht = static_cast<GHashTable*>(g_hash_table_lookup (classTable, obj_name));
|
2009-09-18 14:40:57 -05:00
|
|
|
|
|
|
|
/* If it doesn't already exist, create a new table for this object */
|
|
|
|
if (!ht)
|
|
|
|
{
|
|
|
|
ht = g_hash_table_new (g_str_hash, g_str_equal);
|
|
|
|
g_hash_table_insert (classTable, (char *)obj_name, ht);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* At least right now, we allow dummy, parameterless objects,
|
|
|
|
* for testing purposes. Although I suppose that should be
|
|
|
|
* an error.. */
|
|
|
|
/* Now insert all the parameters */
|
|
|
|
if (params)
|
|
|
|
{
|
|
|
|
for (i = 0; params[i].param_name; i++)
|
|
|
|
g_hash_table_insert (ht,
|
|
|
|
(char *)params[i].param_name,
|
|
|
|
(gpointer)&(params[i]));
|
|
|
|
}
|
2005-11-07 09:45:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
qof_class_is_registered (QofIdTypeConst obj_name)
|
|
|
|
{
|
2009-09-18 14:40:57 -05:00
|
|
|
if (!obj_name) return FALSE;
|
|
|
|
if (!check_init()) return FALSE;
|
2005-11-07 09:45:58 -06:00
|
|
|
|
2009-09-18 14:40:57 -05:00
|
|
|
if (g_hash_table_lookup (classTable, obj_name)) return TRUE;
|
2005-11-07 09:45:58 -06:00
|
|
|
|
2009-09-18 14:40:57 -05:00
|
|
|
return FALSE;
|
2005-11-07 09:45:58 -06:00
|
|
|
}
|
|
|
|
|
2006-01-08 11:51:29 -06:00
|
|
|
const QofParam *
|
2005-11-07 09:45:58 -06:00
|
|
|
qof_class_get_parameter (QofIdTypeConst obj_name,
|
2009-09-18 14:40:57 -05:00
|
|
|
const char *parameter)
|
2005-11-07 09:45:58 -06:00
|
|
|
{
|
2009-09-18 14:40:57 -05:00
|
|
|
GHashTable *ht;
|
2005-11-07 09:45:58 -06:00
|
|
|
|
2009-09-18 14:40:57 -05:00
|
|
|
g_return_val_if_fail (obj_name, NULL);
|
|
|
|
g_return_val_if_fail (parameter, NULL);
|
|
|
|
if (!check_init()) return NULL;
|
2005-11-07 09:45:58 -06:00
|
|
|
|
2014-04-25 15:41:11 -05:00
|
|
|
ht = static_cast<GHashTable*>(g_hash_table_lookup (classTable, obj_name));
|
2009-09-18 14:40:57 -05:00
|
|
|
if (!ht)
|
|
|
|
{
|
|
|
|
PWARN ("no object of type %s", obj_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-11-07 09:45:58 -06:00
|
|
|
|
2014-04-25 15:41:11 -05:00
|
|
|
return static_cast<QofParam*>(g_hash_table_lookup (ht, parameter));
|
2005-11-07 09:45:58 -06:00
|
|
|
}
|
|
|
|
|
2006-01-08 11:51:29 -06:00
|
|
|
QofAccessFunc
|
2005-11-07 09:45:58 -06:00
|
|
|
qof_class_get_parameter_getter (QofIdTypeConst obj_name,
|
2009-09-18 14:40:57 -05:00
|
|
|
const char *parameter)
|
2005-11-07 09:45:58 -06:00
|
|
|
{
|
2009-09-18 14:40:57 -05:00
|
|
|
const QofParam *prm;
|
2005-11-07 09:45:58 -06:00
|
|
|
|
2009-09-18 14:40:57 -05:00
|
|
|
g_return_val_if_fail (obj_name, NULL);
|
|
|
|
g_return_val_if_fail (parameter, NULL);
|
2005-11-07 09:45:58 -06:00
|
|
|
|
2009-09-18 14:40:57 -05:00
|
|
|
prm = qof_class_get_parameter (obj_name, parameter);
|
|
|
|
if (prm)
|
|
|
|
return prm->param_getfcn;
|
2005-11-07 09:45:58 -06:00
|
|
|
|
2009-09-18 14:40:57 -05:00
|
|
|
return NULL;
|
2005-11-07 09:45:58 -06:00
|
|
|
}
|
|
|
|
|
2006-01-08 11:51:29 -06:00
|
|
|
QofSetterFunc
|
2005-11-07 09:45:58 -06:00
|
|
|
qof_class_get_parameter_setter (QofIdTypeConst obj_name,
|
2009-09-18 14:40:57 -05:00
|
|
|
const char *parameter)
|
2005-11-07 09:45:58 -06:00
|
|
|
{
|
2009-09-18 14:40:57 -05:00
|
|
|
const QofParam *prm;
|
2005-11-07 09:45:58 -06:00
|
|
|
|
2009-09-18 14:40:57 -05:00
|
|
|
g_return_val_if_fail (obj_name, NULL);
|
|
|
|
g_return_val_if_fail (parameter, NULL);
|
2005-11-07 09:45:58 -06:00
|
|
|
|
2009-09-18 14:40:57 -05:00
|
|
|
prm = qof_class_get_parameter (obj_name, parameter);
|
|
|
|
if (prm)
|
|
|
|
return prm->param_setfcn;
|
2005-11-07 09:45:58 -06:00
|
|
|
|
2009-09-18 14:40:57 -05:00
|
|
|
return NULL;
|
2005-11-07 09:45:58 -06:00
|
|
|
}
|
|
|
|
|
2006-01-08 11:51:29 -06:00
|
|
|
QofType
|
2005-11-07 09:45:58 -06:00
|
|
|
qof_class_get_parameter_type (QofIdTypeConst obj_name,
|
2009-09-18 14:40:57 -05:00
|
|
|
const char *param_name)
|
2005-11-07 09:45:58 -06:00
|
|
|
{
|
2009-09-18 14:40:57 -05:00
|
|
|
const QofParam *prm;
|
2005-11-07 09:45:58 -06:00
|
|
|
|
2009-09-18 14:40:57 -05:00
|
|
|
if (!obj_name || !param_name) return NULL;
|
2005-11-07 09:45:58 -06:00
|
|
|
|
2009-09-18 14:40:57 -05:00
|
|
|
prm = qof_class_get_parameter (obj_name, param_name);
|
|
|
|
if (!prm) return NULL;
|
2005-11-07 09:45:58 -06:00
|
|
|
|
2009-09-18 14:40:57 -05:00
|
|
|
return (prm->param_type);
|
2005-11-07 09:45:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ================================================================ */
|
|
|
|
|
2009-09-18 14:40:57 -05:00
|
|
|
struct class_iterate
|
|
|
|
{
|
|
|
|
QofClassForeachCB fcn;
|
|
|
|
gpointer data;
|
2005-11-07 09:45:58 -06:00
|
|
|
};
|
|
|
|
|
2006-01-08 11:51:29 -06:00
|
|
|
static void
|
2005-11-07 09:45:58 -06:00
|
|
|
class_foreach_cb (gpointer key, gpointer item, gpointer arg)
|
|
|
|
{
|
2014-04-25 15:41:11 -05:00
|
|
|
struct class_iterate *iter = static_cast<class_iterate*>(arg);
|
|
|
|
QofIdTypeConst id = static_cast<QofIdTypeConst>(key);
|
2006-01-08 11:51:29 -06:00
|
|
|
|
2009-09-18 14:40:57 -05:00
|
|
|
iter->fcn (id, iter->data);
|
2005-11-07 09:45:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
qof_class_foreach (QofClassForeachCB cb, gpointer user_data)
|
|
|
|
{
|
2009-09-18 14:40:57 -05:00
|
|
|
struct class_iterate iter;
|
2005-11-07 09:45:58 -06:00
|
|
|
|
2009-09-18 14:40:57 -05:00
|
|
|
if (!cb) return;
|
|
|
|
if (!classTable) return;
|
2005-11-07 09:45:58 -06:00
|
|
|
|
2009-09-18 14:40:57 -05:00
|
|
|
iter.fcn = cb;
|
|
|
|
iter.data = user_data;
|
2005-11-07 09:45:58 -06:00
|
|
|
|
2009-09-18 14:40:57 -05:00
|
|
|
g_hash_table_foreach (classTable, class_foreach_cb, &iter);
|
2005-11-07 09:45:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ================================================================ */
|
|
|
|
|
2009-09-18 14:40:57 -05:00
|
|
|
struct parm_iterate
|
|
|
|
{
|
|
|
|
QofParamForeachCB fcn;
|
|
|
|
gpointer data;
|
2005-11-07 09:45:58 -06:00
|
|
|
};
|
|
|
|
|
2006-01-08 11:51:29 -06:00
|
|
|
static void
|
2005-11-07 09:45:58 -06:00
|
|
|
param_foreach_cb (gpointer key, gpointer item, gpointer arg)
|
|
|
|
{
|
2014-04-25 15:41:11 -05:00
|
|
|
struct parm_iterate *iter = static_cast<parm_iterate*>(arg);
|
|
|
|
QofParam *parm = static_cast<QofParam*>(item);
|
2005-11-07 09:45:58 -06:00
|
|
|
|
2009-09-18 14:40:57 -05:00
|
|
|
iter->fcn (parm, iter->data);
|
2005-11-07 09:45:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
qof_class_param_foreach (QofIdTypeConst obj_name,
|
|
|
|
QofParamForeachCB cb, gpointer user_data)
|
|
|
|
{
|
2009-09-18 14:40:57 -05:00
|
|
|
struct parm_iterate iter;
|
|
|
|
GHashTable *param_ht;
|
2005-11-07 09:45:58 -06:00
|
|
|
|
2009-09-18 14:40:57 -05:00
|
|
|
if (!obj_name || !cb) return;
|
|
|
|
if (!classTable) return;
|
2014-04-25 15:41:11 -05:00
|
|
|
param_ht = static_cast<GHashTable*>(g_hash_table_lookup (classTable, obj_name));
|
2009-09-18 14:40:57 -05:00
|
|
|
if (!param_ht) return;
|
2005-11-07 09:45:58 -06:00
|
|
|
|
2009-09-18 14:40:57 -05:00
|
|
|
iter.fcn = cb;
|
|
|
|
iter.data = user_data;
|
2005-11-07 09:45:58 -06:00
|
|
|
|
2009-09-18 14:40:57 -05:00
|
|
|
g_hash_table_foreach (param_ht, param_foreach_cb, &iter);
|
2005-11-07 09:45:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
struct param_ref_list
|
|
|
|
{
|
2009-09-18 14:40:57 -05:00
|
|
|
GList *list;
|
2005-11-07 09:45:58 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
find_reference_param_cb(QofParam *param, gpointer user_data)
|
|
|
|
{
|
2009-09-18 14:40:57 -05:00
|
|
|
struct param_ref_list *b;
|
|
|
|
|
|
|
|
b = (struct param_ref_list*)user_data;
|
|
|
|
if ((param->param_getfcn == NULL) || (param->param_setfcn == NULL))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2012-08-07 12:24:55 -05:00
|
|
|
if (0 == g_strcmp0(param->param_type, QOF_TYPE_STRING))
|
2009-09-18 14:40:57 -05:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2012-08-07 12:24:55 -05:00
|
|
|
if (0 == g_strcmp0(param->param_type, QOF_TYPE_NUMERIC))
|
2009-09-18 14:40:57 -05:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2012-08-07 12:24:55 -05:00
|
|
|
if (0 == g_strcmp0(param->param_type, QOF_TYPE_DATE))
|
2009-09-18 14:40:57 -05:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2012-08-07 12:24:55 -05:00
|
|
|
if (0 == g_strcmp0(param->param_type, QOF_TYPE_CHAR))
|
2009-09-18 14:40:57 -05:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2012-08-07 12:24:55 -05:00
|
|
|
if (0 == g_strcmp0(param->param_type, QOF_TYPE_DEBCRED))
|
2009-09-18 14:40:57 -05:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2012-08-07 12:24:55 -05:00
|
|
|
if (0 == g_strcmp0(param->param_type, QOF_TYPE_GUID))
|
2009-09-18 14:40:57 -05:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2012-08-07 12:24:55 -05:00
|
|
|
if (0 == g_strcmp0(param->param_type, QOF_TYPE_INT32))
|
2009-09-18 14:40:57 -05:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2012-08-07 12:24:55 -05:00
|
|
|
if (0 == g_strcmp0(param->param_type, QOF_TYPE_INT64))
|
2009-09-18 14:40:57 -05:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2012-08-07 12:24:55 -05:00
|
|
|
if (0 == g_strcmp0(param->param_type, QOF_TYPE_DOUBLE))
|
2009-09-18 14:40:57 -05:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2012-08-07 12:24:55 -05:00
|
|
|
if (0 == g_strcmp0(param->param_type, QOF_TYPE_KVP))
|
2009-09-18 14:40:57 -05:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2012-08-07 12:24:55 -05:00
|
|
|
if (0 == g_strcmp0(param->param_type, QOF_TYPE_BOOLEAN))
|
2009-09-18 14:40:57 -05:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2012-08-07 12:24:55 -05:00
|
|
|
if (0 == g_strcmp0(param->param_type, QOF_ID_BOOK))
|
2009-09-18 14:40:57 -05:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
b->list = g_list_append(b->list, param);
|
2005-11-07 09:45:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
GList*
|
|
|
|
qof_class_get_referenceList(QofIdTypeConst type)
|
|
|
|
{
|
2009-09-18 14:40:57 -05:00
|
|
|
GList *ref_list;
|
|
|
|
struct param_ref_list b;
|
|
|
|
|
|
|
|
ref_list = NULL;
|
|
|
|
b.list = NULL;
|
|
|
|
qof_class_param_foreach(type, find_reference_param_cb, &b);
|
|
|
|
ref_list = g_list_copy(b.list);
|
|
|
|
return ref_list;
|
2005-11-07 09:45:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ============================= END OF FILE ======================== */
|