Move features hash creation to QofBook

So that the KVP stays private.
This commit is contained in:
John Ralls 2013-10-27 15:28:37 -07:00
parent 48df2d3569
commit 11aae5b2b9
3 changed files with 63 additions and 38 deletions

View File

@ -66,10 +66,12 @@ static void gnc_features_init ()
g_strdup (known_features[i].desc));
}
static void gnc_features_test_one(const gchar *key, KvpValue *value, gpointer data)
static void gnc_features_test_one(gpointer pkey, gpointer value,
gpointer data)
{
const gchar *key = (const gchar*)pkey;
const gchar *feature_desc = (const gchar*)value;
GList **unknown_features;
gchar *feature_desc;
g_assert(data);
unknown_features = (GList**) data;
@ -79,54 +81,47 @@ static void gnc_features_test_one(const gchar *key, KvpValue *value, gpointer da
return;
/* It is unknown, so add the description to the unknown features list: */
feature_desc = kvp_value_get_string(value);
g_assert(feature_desc);
*unknown_features = g_list_prepend(*unknown_features, feature_desc);
*unknown_features = g_list_prepend(*unknown_features,
(gpointer)feature_desc);
}
/* Check if the session requires features unknown to this version of GnuCash.
*
* Returns a message to display if we found unknown features, NULL if we're okay.
* Returns a message to display if we found unknown features, NULL if
* we're okay.
*/
gchar *gnc_features_test_unknown (QofBook *book)
{
KvpFrame *frame = qof_book_get_slots (book);
KvpValue *value;
/* Setup the known_features hash table */
gnc_features_init();
g_assert(frame);
value = kvp_frame_get_value(frame, "features");
GList* features_list = NULL;
GHashTable *features_used = qof_book_get_features (book);
if (value)
/* Iterate over the members of this frame for unknown features */
g_hash_table_foreach (features_used, &gnc_features_test_one,
&features_list);
if (features_list)
{
GList* features_list = NULL;
frame = kvp_value_get_frame(value);
g_assert(frame);
/* Iterate over the members of this frame for unknown features */
kvp_frame_for_each_slot(frame, &gnc_features_test_one, &features_list);
if (features_list)
{
GList *i;
char* msg = g_strdup(
_("This Dataset contains features not supported by this "
"version of GnuCash. You must use a newer version of "
"GnuCash in order to support the following features:"
GList *i;
char* msg = g_strdup(_("This Dataset contains features not supported "
"by this version of GnuCash. You must use a "
"newer version of GnuCash in order to support "
"the following features:"
));
for (i = features_list; i; i = i->next)
{
char *tmp = g_strconcat(msg, "\n* ", i->data, NULL);
g_free (msg);
msg = tmp;
}
for (i = features_list; i; i = i->next)
{
char *tmp = g_strconcat(msg, "\n* ", i->data, NULL);
g_free (msg);
msg = tmp;
}
g_list_free(features_list);
return msg;
}
g_list_free(features_list);
return msg;
}
return NULL;
@ -134,9 +129,7 @@ gchar *gnc_features_test_unknown (QofBook *book)
void gnc_features_set_used (QofBook *book, const gchar *feature)
{
KvpFrame *frame;
const gchar *description;
gchar *kvp_path;
g_return_if_fail (book);
g_return_if_fail (feature);
@ -151,10 +144,7 @@ void gnc_features_set_used (QofBook *book, const gchar *feature)
return;
}
frame = qof_book_get_slots (book);
kvp_path = g_strconcat ("/features/", feature, NULL);
kvp_frame_set_string (frame, kvp_path, description);
qof_book_kvp_changed (book);
qof_book_set_feature (book, feature, description);
}

View File

@ -742,6 +742,36 @@ static void commit_err (QofInstance *inst, QofBackendError errcode)
// gnc_engine_signal_commit_error( errcode );
}
#define GNC_FEATURES "/features/"
static void
add_feature_to_hash (const gchar *key, KvpValue *value, gpointer user_data)
{
gchar *descr = kvp_value_get_string (value);
g_hash_table_insert (*(GHashTable**)user_data, (gchar*)key, descr);
}
GHashTable *
qof_book_get_features (QofBook *book)
{
KvpFrame *frame = qof_instance_get_slots (QOF_INSTANCE (book));
GHashTable *features = g_hash_table_new (g_str_hash, g_str_equal);
frame = kvp_frame_get_frame (frame, GNC_FEATURES);
kvp_frame_for_each_slot (frame, &add_feature_to_hash, &features);
return features;
}
void
qof_book_set_feature (QofBook *book, const gchar *key, const gchar *descr)
{
KvpFrame *frame = qof_instance_get_slots (QOF_INSTANCE (book));
gchar *path = g_strconcat (GNC_FEATURES, key, NULL);
qof_book_begin_edit (book);
kvp_frame_set_string (frame, path, descr);
qof_instance_set_dirty (QOF_INSTANCE (book));
qof_book_commit_edit (book);
}
static void noop (QofInstance *inst) {}
void

View File

@ -337,6 +337,11 @@ gchar *qof_book_get_counter_format (const QofBook *book, const char *counter_nam
const char* qof_book_get_string_option(const QofBook* book, const char* opt_name);
void qof_book_set_string_option(QofBook* book, const char* opt_name, const char* opt_val);
/** Access functions for reading and setting the used-features on this book.
*/
GHashTable *qof_book_get_features (QofBook *book);
void qof_book_set_feature (QofBook *book, const gchar *key, const gchar *descr);
void qof_book_begin_edit(QofBook *book);
void qof_book_commit_edit(QofBook *book);