Implement KVP_TYPE_BOOLEAN

In terms of KVP_TYPE_STRING such that TRUE->"true" and FALSE->NULL. This
provides compatibility with the existing boolean KVP in Account.c.
This commit is contained in:
John Ralls 2015-05-07 16:12:54 -07:00
parent 31cfa0a9f0
commit 32d4fff366
2 changed files with 99 additions and 73 deletions

View File

@ -932,6 +932,13 @@ kvp_value_new_double(double value)
return new KvpValueImpl{value}; return new KvpValueImpl{value};
} }
KvpValue *
kvp_value_new_boolean(gboolean value)
{
if (!value) return {};
return new KvpValueImpl{g_strdup("true")};
}
KvpValue * KvpValue *
kvp_value_new_numeric(gnc_numeric value) kvp_value_new_numeric(gnc_numeric value)
{ {
@ -1024,6 +1031,15 @@ kvp_value_get_double(const KvpValue * ovalue)
return value->get<double>(); return value->get<double>();
} }
bool
kvp_value_get_boolean (const KvpValue *ovalue)
{
if (!ovalue) return {};
const KvpValueImpl *value {static_cast<const KvpValueImpl*>(ovalue)};
const char* str = value->get<char*>();
return str && strcmp(str, "true") == 0;
}
gnc_numeric gnc_numeric
kvp_value_get_numeric(const KvpValue * ovalue) kvp_value_get_numeric(const KvpValue * ovalue)
{ {
@ -1186,7 +1202,7 @@ gvalue_list_from_kvp_value (KvpValue *kval, gpointer pList)
GValue *gval = gvalue_from_kvp_value (kval); GValue *gval = gvalue_from_kvp_value (kval);
gvlist = (GList**)pList; gvlist = (GList**)pList;
if (G_VALUE_TYPE (gval)) if (G_VALUE_TYPE (gval))
*gvlist = g_list_prepend (*gvlist, gval); *gvlist = g_list_prepend (*gvlist, gval);
} }
static void static void
@ -1195,7 +1211,7 @@ kvp_value_list_from_gvalue (GValue *gval, gpointer pList)
GList **kvplist = (GList**)pList; GList **kvplist = (GList**)pList;
KvpValue *kvp; KvpValue *kvp;
if (!(gval && G_VALUE_TYPE (gval))) if (!(gval && G_VALUE_TYPE (gval)))
return; return;
kvp = kvp_value_from_gvalue (gval); kvp = kvp_value_from_gvalue (gval);
*kvplist = g_list_prepend (*kvplist, kvp); *kvplist = g_list_prepend (*kvplist, kvp);
} }
@ -1213,55 +1229,59 @@ gvalue_from_kvp_value (KvpValue *kval)
switch (kvp_value_get_type(kval)) switch (kvp_value_get_type(kval))
{ {
case KVP_TYPE_GINT64: case KVP_TYPE_GINT64:
g_value_init (val, G_TYPE_INT64); g_value_init (val, G_TYPE_INT64);
g_value_set_int64 (val, kvp_value_get_gint64 (kval)); g_value_set_int64 (val, kvp_value_get_gint64 (kval));
break; break;
case KVP_TYPE_DOUBLE: case KVP_TYPE_DOUBLE:
g_value_init (val, G_TYPE_DOUBLE); g_value_init (val, G_TYPE_DOUBLE);
g_value_set_double (val, kvp_value_get_double (kval)); g_value_set_double (val, kvp_value_get_double (kval));
break; break;
case KVP_TYPE_NUMERIC: case KVP_TYPE_BOOLEAN:
g_value_init (val, GNC_TYPE_NUMERIC); g_value_init (val, G_TYPE_BOOLEAN);
num = kvp_value_get_numeric (kval); g_value_set_boolean (val, kvp_value_get_boolean (kval));
g_value_set_boxed (val, &num); break;
break; case KVP_TYPE_NUMERIC:
case KVP_TYPE_STRING: g_value_init (val, GNC_TYPE_NUMERIC);
g_value_init (val, G_TYPE_STRING); num = kvp_value_get_numeric (kval);
g_value_set_string (val, kvp_value_get_string (kval)); g_value_set_boxed (val, &num);
break; break;
case KVP_TYPE_GUID: case KVP_TYPE_STRING:
g_value_init (val, GNC_TYPE_GUID); g_value_init (val, G_TYPE_STRING);
g_value_set_boxed (val, kvp_value_get_guid (kval)); g_value_set_string (val, kvp_value_get_string (kval));
break; break;
case KVP_TYPE_TIMESPEC: case KVP_TYPE_GUID:
g_value_init (val, GNC_TYPE_TIMESPEC); g_value_init (val, GNC_TYPE_GUID);
tm = kvp_value_get_timespec (kval); g_value_set_boxed (val, kvp_value_get_guid (kval));
g_value_set_boxed (val, &tm); break;
break; case KVP_TYPE_TIMESPEC:
case KVP_TYPE_GDATE: g_value_init (val, GNC_TYPE_TIMESPEC);
g_value_init (val, G_TYPE_DATE); tm = kvp_value_get_timespec (kval);
gdate = kvp_value_get_gdate (kval); g_value_set_boxed (val, &tm);
g_value_set_boxed (val, &gdate); break;
break; case KVP_TYPE_GDATE:
case KVP_TYPE_GLIST: g_value_init (val, G_TYPE_DATE);
{ gdate = kvp_value_get_gdate (kval);
GList *gvalue_list = NULL; g_value_set_boxed (val, &gdate);
GList *kvp_list = kvp_value_get_glist (kval); break;
g_list_foreach (kvp_list, (GFunc)gvalue_list_from_kvp_value, &gvalue_list); case KVP_TYPE_GLIST:
g_value_init (val, GNC_TYPE_VALUE_LIST); {
gvalue_list = g_list_reverse (gvalue_list); GList *gvalue_list = NULL;
g_value_set_boxed (val, gvalue_list); GList *kvp_list = kvp_value_get_glist (kval);
break; g_list_foreach (kvp_list, (GFunc)gvalue_list_from_kvp_value, &gvalue_list);
} g_value_init (val, GNC_TYPE_VALUE_LIST);
gvalue_list = g_list_reverse (gvalue_list);
g_value_set_boxed (val, gvalue_list);
break;
}
/* No transfer of KVP frames outside of QofInstance-derived classes! */ /* No transfer of KVP frames outside of QofInstance-derived classes! */
case KVP_TYPE_FRAME: case KVP_TYPE_FRAME:
PWARN ("Error! Attempt to transfer KvpFrame!"); PWARN ("Error! Attempt to transfer KvpFrame!");
default: default:
PWARN ("Error! Invalid KVP Transfer Request!"); PWARN ("Error! Invalid KVP Transfer Request!");
g_slice_free (GValue, val); g_slice_free (GValue, val);
val = NULL; val = NULL;
break; break;
} }
return val; return val;
} }
@ -1270,36 +1290,41 @@ KvpValue*
kvp_value_from_gvalue (const GValue *gval) kvp_value_from_gvalue (const GValue *gval)
{ {
KvpValue *val = NULL; KvpValue *val = NULL;
GType type = G_VALUE_TYPE (gval); GType type;
if (gval == NULL)
return NULL;
type = G_VALUE_TYPE (gval);
g_return_val_if_fail (G_VALUE_TYPE (gval), NULL); g_return_val_if_fail (G_VALUE_TYPE (gval), NULL);
if (type == G_TYPE_INT64) if (type == G_TYPE_INT64)
val = kvp_value_new_gint64 (g_value_get_int64 (gval)); val = kvp_value_new_gint64 (g_value_get_int64 (gval));
else if (type == G_TYPE_DOUBLE) else if (type == G_TYPE_DOUBLE)
val = kvp_value_new_double (g_value_get_double (gval)); val = kvp_value_new_double (g_value_get_double (gval));
else if (type == G_TYPE_BOOLEAN)
val = kvp_value_new_boolean (g_value_get_boolean (gval));
else if (type == GNC_TYPE_NUMERIC) else if (type == GNC_TYPE_NUMERIC)
val = kvp_value_new_numeric (*(gnc_numeric*)g_value_get_boxed (gval)); val = kvp_value_new_numeric (*(gnc_numeric*)g_value_get_boxed (gval));
else if (type == G_TYPE_STRING) else if (type == G_TYPE_STRING)
val = kvp_value_new_string (g_value_get_string (gval)); val = kvp_value_new_string (g_value_get_string (gval));
else if (type == GNC_TYPE_GUID) else if (type == GNC_TYPE_GUID)
val = kvp_value_new_guid ((GncGUID*)g_value_get_boxed (gval)); val = kvp_value_new_guid ((GncGUID*)g_value_get_boxed (gval));
else if (type == GNC_TYPE_TIMESPEC) else if (type == GNC_TYPE_TIMESPEC)
val = kvp_value_new_timespec (*(Timespec*)g_value_get_boxed (gval)); val = kvp_value_new_timespec (*(Timespec*)g_value_get_boxed (gval));
else if (type == G_TYPE_DATE) else if (type == G_TYPE_DATE)
val = kvp_value_new_gdate (*(GDate*)g_value_get_boxed (gval)); val = kvp_value_new_gdate (*(GDate*)g_value_get_boxed (gval));
else if (type == GNC_TYPE_VALUE_LIST) else if (type == GNC_TYPE_VALUE_LIST)
{ {
GList *gvalue_list = (GList*)g_value_get_boxed (gval); GList *gvalue_list = (GList*)g_value_get_boxed (gval);
GList *kvp_list = NULL; GList *kvp_list = NULL;
g_list_foreach (gvalue_list, (GFunc)kvp_value_list_from_gvalue, &kvp_list); g_list_foreach (gvalue_list, (GFunc)kvp_value_list_from_gvalue, &kvp_list);
kvp_list = g_list_reverse (kvp_list); kvp_list = g_list_reverse (kvp_list);
val = kvp_value_new_glist_nc (kvp_list); val = kvp_value_new_glist_nc (kvp_list);
// g_list_free_full (gvalue_list, (GDestroyNotify)g_value_unset); // g_list_free_full (gvalue_list, (GDestroyNotify)g_value_unset);
// gvalue_list = NULL; // gvalue_list = NULL;
} }
else else
PWARN ("Error! Don't know how to make a KvpValue from a %s", PWARN ("Error! Don't know how to make a KvpValue from a %s",
G_VALUE_TYPE_NAME (gval)); G_VALUE_TYPE_NAME (gval));
return val; return val;
} }
@ -1315,7 +1340,7 @@ kvp_frame_get_gvalue (KvpFrame *frame, const gchar *key)
void void
kvp_frame_set_gvalue (KvpFrame *frame, const gchar *key, const GValue *value) kvp_frame_set_gvalue (KvpFrame *frame, const gchar *key, const GValue *value)
{ {
kvp_frame_set_value_nc (frame, key, kvp_value_from_gvalue (value)); kvp_frame_set_value_nc (frame, key, kvp_value_from_gvalue (value));
} }
static void static void
@ -1356,9 +1381,9 @@ gnc_value_list_get_type (void)
static GType type = 0; static GType type = 0;
if (type == 0) if (type == 0)
{ {
type = g_boxed_type_register_static ("gnc_value_list", type = g_boxed_type_register_static ("gnc_value_list",
(GBoxedCopyFunc)gnc_value_list_copy, (GBoxedCopyFunc)gnc_value_list_copy,
(GBoxedFreeFunc)gnc_value_list_free); (GBoxedFreeFunc)gnc_value_list_free);
} }
return type; return type;
} }

View File

@ -95,14 +95,15 @@ typedef enum
KVP_TYPE_INVALID = -1, KVP_TYPE_INVALID = -1,
KVP_TYPE_GINT64 = 1, /**< QOF_TYPE_INT64 gint64 */ KVP_TYPE_GINT64 = 1, /**< QOF_TYPE_INT64 gint64 */
KVP_TYPE_DOUBLE, /**< QOF_TYPE_DOUBLE gdouble */ KVP_TYPE_DOUBLE, /**< QOF_TYPE_DOUBLE gdouble */
KVP_TYPE_BOOLEAN, /**< QOF_TYPE_BOOLEAN gboolean */
KVP_TYPE_NUMERIC, /**< QOF_TYPE_NUMERIC */ KVP_TYPE_NUMERIC, /**< QOF_TYPE_NUMERIC */
KVP_TYPE_STRING, /**< QOF_TYPE_STRING gchar* */ KVP_TYPE_STRING, /**< QOF_TYPE_STRING gchar* */
KVP_TYPE_GUID, /**< QOF_TYPE_GUID */ KVP_TYPE_GUID, /**< QOF_TYPE_GUID */
KVP_TYPE_TIMESPEC, /**< QOF_TYPE_DATE */ KVP_TYPE_TIMESPEC, /**< QOF_TYPE_DATE */
KVP_TYPE_PLACEHOLDER_DONT_USE, /* Replaces KVP_TYPE_BINARY */ KVP_TYPE_PLACEHOLDER_DONT_USE, /* Replaces KVP_TYPE_BINARY */
KVP_TYPE_GLIST, /**< no QOF equivalent. */ KVP_TYPE_GLIST, /**< no QOF equivalent. */
KVP_TYPE_FRAME /**< no QOF equivalent. */ KVP_TYPE_FRAME, /**< no QOF equivalent. */
, KVP_TYPE_GDATE /**< no QOF equivalent. */ KVP_TYPE_GDATE /**< no QOF equivalent. */
} KvpValueType; } KvpValueType;
/** \deprecated Deprecated backwards compat token /** \deprecated Deprecated backwards compat token