Contain getting slots for gnc_options_db inside QofBook

Turn gnc_option_db_save_to_kvp and …load_from_kvp into callbacks passed to qof_book_(save|load)_options to avoid adding a dependency between qofbook and option-utils.
This commit is contained in:
John Ralls 2013-10-27 13:38:55 -07:00
parent 38b3961bb8
commit 6515a8e858
9 changed files with 237 additions and 12 deletions

View File

@ -33,7 +33,7 @@ GncTaxTable* gnc_business_get_default_tax_table (QofBook *book, GncOwnerType own
GNCOptionDB *odb;
odb = gnc_option_db_new_for_type (GNC_ID_BOOK);
gnc_option_db_load_from_kvp (odb, qof_book_get_slots (book));
qof_book_load_options (book, gnc_option_db_load_from_kvp, odb);
switch (ownertype)
{

View File

@ -34,7 +34,7 @@
typedef struct gnc_option GNCOption;
typedef struct gnc_option_section GNCOptionSection;
typedef struct gnc_option_db GNCOptionDB;
/* typedef struct gnc_option_db GNCOptionDB is in qof-book.h */
typedef int GNCOptionDBHandle;

View File

@ -1,3 +1,5 @@
include $(top_srcdir)/test-templates/Makefile.decl
TESTS = \
test-link-module \
test-load-module \
@ -64,3 +66,17 @@ AM_CPPFLAGS = \
-I${top_srcdir}/src/libqof/qof \
${GUILE_INCS} \
${GLIB_CFLAGS}
TEST_PROGS += test-app-utils
noinst_PROGRAMS = ${TEST_PROGS} ${CHECK_PROGS}
test_app_utils_SOURCES = \
test-app-utils.c \
test-option-util.c
test_app_utils_CFLAGS = \
${DEFAULT_INCLUDES} \
-I${top_srcdir}/${MODULEPATH}/ \
-DTESTPROG=test_app_utils \
${GLIB_CFLAGS}

View File

@ -0,0 +1,56 @@
/********************************************************************
* test-app-utils.c: GLib g_test test execution file. *
* Copyright 2013 John Ralls <jralls@ceridwen.us> *
* *
* 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 *
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
* Boston, MA 02110-1301, USA gnu@gnu.org *
\********************************************************************/
#include <config.h>
#include <glib.h>
#include <qof.h>
#include <libguile.h>
#include <gnc-module.h>
extern void test_suite_option_util (void);
static void
guile_main (void *closure, int argc, char **argv)
{
GNCModule mod;
int retval;
gnc_module_system_init ();
mod = gnc_module_load ("gnucash/app-utils", 0);
test_suite_option_util ();
retval = g_test_run ();
exit (retval);
}
int
main (int argc, char *argv[])
{
qof_init (); /* Initialize the GObject system */
qof_log_init_filename_special ("stderr"); /* Init the log system */
g_test_init (&argc, &argv, NULL); /* initialize test program */
//qof_log_set_level("gnc", G_LOG_LEVEL_DEBUG);
g_test_bug_base("https://bugzilla.gnome.org/show_bug.cgi?id="); /* init the bugzilla URL */
g_setenv ("GNC_UNINSTALLED", "1", TRUE);
scm_boot_guile (argc, argv, guile_main, NULL);
}

View File

@ -0,0 +1,123 @@
/********************************************************************
* test-option-util.c: GLib g_test test suite for Split.c. *
* Copyright 2013 John Ralls <jralls@ceridwen.us> *
* *
* 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, you can retrieve it from *
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html *
* or contact: *
* *
* Free Software Foundation Voice: +1-617-542-5942 *
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
* Boston, MA 02110-1301, USA gnu@gnu.org *
********************************************************************/
#include <config.h>
#include <glib.h>
#include <unittest-support.h>
#include <qofbookslots.h>
#include "../option-util.h"
static const gchar *suitename = "/app-utils/option-util";
void test_suite_option_util (void);
typedef struct
{
QofBook *book;
GSList *hdlrs;
} Fixture;
/* Expose a mostly-private QofInstance function to load options into
* the Book.
*/
extern KvpFrame *qof_instance_get_slots (QofInstance*);
static void
setup (Fixture *fixture, gconstpointer pData)
{
fixture->book = qof_book_new ();
fixture->hdlrs = NULL;
}
static void
setup_kvp (Fixture *fixture, gconstpointer pData)
{
QofBook *book;
KvpFrame *slots;
setup (fixture, pData);
book = fixture->book;
slots = qof_instance_get_slots (QOF_INSTANCE (book));
qof_instance_set (QOF_INSTANCE (book),
"trading-accts", "t",
"split-action-num-field", "t",
"autoreadonly-days", (double)21,
NULL);
kvp_frame_set_string (slots, "options/Business/Company Name",
"Bogus Company");
}
static void
teardown (Fixture *fixture, gconstpointer pData)
{
qof_book_destroy (fixture->book);
g_slist_free_full (fixture->hdlrs, test_free_log_handler);
test_clear_error_list();
}
static void
test_option_load_from_kvp (Fixture *fixture, gconstpointer pData)
{
QofBook *book = fixture->book;
GNCOptionDB *odb = gnc_option_db_new_for_type (QOF_ID_BOOK);
qof_book_load_options (book, gnc_option_db_load_from_kvp, odb);
g_assert (gnc_option_db_lookup_boolean_option (odb, OPTION_SECTION_ACCOUNTS,
OPTION_NAME_TRADING_ACCOUNTS, FALSE));
g_assert_cmpstr (gnc_option_db_lookup_string_option (odb, "Business", "Company Name", FALSE), ==, "Bogus Company");
g_assert_cmpfloat (gnc_option_db_lookup_number_option (odb, OPTION_SECTION_ACCOUNTS, OPTION_NAME_AUTO_READONLY_DAYS, FALSE), ==, 21);
gnc_option_db_destroy (odb);
}
static void
test_option_save_to_kvp (Fixture *fixture, gconstpointer pData)
{
QofBook *book = fixture->book;
GNCOptionDB *odb = gnc_option_db_new_for_type (QOF_ID_BOOK);
KvpFrame *slots = qof_instance_get_slots (QOF_INSTANCE (book));
g_assert (gnc_option_db_set_boolean_option (odb, OPTION_SECTION_ACCOUNTS,
OPTION_NAME_TRADING_ACCOUNTS,
TRUE));
g_assert (gnc_option_db_set_string_option (odb, "Business", "Company Name",
"Bogus Company"));
g_assert (gnc_option_db_set_number_option (odb, OPTION_SECTION_ACCOUNTS,
OPTION_NAME_AUTO_READONLY_DAYS,
17));
qof_book_save_options (book, gnc_option_db_save_to_kvp, odb, TRUE);
g_assert_cmpstr (kvp_frame_get_string (slots, "options/Accounts/Use Trading Accounts"), == , "t");
g_assert_cmpstr (kvp_frame_get_string (slots, "options/Business/Company Name"), ==, "Bogus Company");
g_assert_cmpfloat (kvp_frame_get_double (slots, "options/Accounts/Day Threshold for Read-Only Transactions (red line)"), ==, 17);
gnc_option_db_destroy (odb);
}
void
test_suite_option_util (void)
{
GNC_TEST_ADD (suitename, "Option DB Load from KVP", Fixture, NULL, setup_kvp, test_option_load_from_kvp, teardown);
GNC_TEST_ADD (suitename, "Option DB Save to KVP", Fixture, NULL, setup, test_option_save_to_kvp, teardown);
}

View File

@ -109,10 +109,10 @@ gnc_book_dom_tree_create(QofBook *book)
xmlAddChild(ret, guid_to_dom_tree(book_id_string,
qof_book_get_guid(book)));
if (qof_book_get_slots(book))
if (qof_instance_get_slots (QOF_INSTANCE (book)))
{
xmlNodePtr kvpnode = kvp_frame_to_dom_tree(book_slots_string,
qof_book_get_slots(book));
qof_instance_get_slots (QOF_INSTANCE (book)));
if (kvpnode)
xmlAddChild(ret, kvpnode);
}
@ -162,10 +162,10 @@ write_book_parts(FILE *out, QofBook *book)
if (ferror(out) || fprintf(out, "\n") < 0)
return FALSE;
if (qof_book_get_slots(book))
if (qof_instance_get_slots (QOF_INSTANCE (book)))
{
xmlNodePtr kvpnode = kvp_frame_to_dom_tree(book_slots_string,
qof_book_get_slots(book));
qof_instance_get_slots (QOF_INSTANCE (book)));
if (kvpnode)
{
xmlElemDump(out, NULL, kvpnode);
@ -203,7 +203,7 @@ book_slots_handler (xmlNodePtr node, gpointer book_pdata)
/* the below works only because the get is gaurenteed to return
* a frame, even if its empty */
success = dom_tree_to_kvp_frame_given (node, qof_book_get_slots (book));
success = dom_tree_to_kvp_frame_given (node, qof_instance_get_slots (QOF_INSTANCE (book)));
g_return_val_if_fail(success, FALSE);

View File

@ -3936,20 +3936,21 @@ gnc_book_options_dialog_apply_cb(GNCOptionWin * optionwin,
gpointer user_data)
{
GNCOptionDB * options = user_data;
kvp_frame *slots = qof_book_get_slots (gnc_get_current_book ());
gboolean use_split_action_for_num_before =
qof_book_use_split_action_for_num_field (gnc_get_current_book ());
gboolean use_split_action_for_num_after;
QofBook *book = gnc_get_current_book ();
if (!options) return;
gnc_option_db_commit (options);
gnc_option_db_save_to_kvp (options, slots, TRUE);
qof_book_kvp_changed (gnc_get_current_book());
qof_book_begin_edit (book);
qof_book_save_options (book, gnc_option_db_save_to_kvp, options, TRUE);
use_split_action_for_num_after =
qof_book_use_split_action_for_num_field (gnc_get_current_book ());
if (use_split_action_for_num_before != use_split_action_for_num_after)
gnc_book_option_num_field_source_change_cb (use_split_action_for_num_after);
qof_book_commit_edit (book);
}
static void
@ -3965,12 +3966,12 @@ gnc_book_options_dialog_close_cb(GNCOptionWin * optionwin,
GtkWidget *
gnc_book_options_dialog_cb (gboolean modal, gchar *title)
{
kvp_frame *slots = qof_book_get_slots (gnc_get_current_book ());
QofBook *book = gnc_get_current_book ();
GNCOptionDB *options;
GNCOptionWin *optionwin;
options = gnc_option_db_new_for_type (QOF_ID_BOOK);
gnc_option_db_load_from_kvp (options, slots);
qof_book_load_options (book, gnc_option_db_load_from_kvp, options);
gnc_option_db_clean (options);
optionwin = gnc_options_dialog_new_modal (modal,

View File

@ -932,6 +932,22 @@ qof_book_set_feature (QofBook *book, const gchar *key, const gchar *descr)
qof_book_commit_edit (book);
}
void
qof_book_load_options (QofBook *book, GNCOptionLoad load_cb, GNCOptionDB *odb)
{
KvpFrame *slots = qof_instance_get_slots (QOF_INSTANCE (book));
load_cb (odb, slots);
}
void
qof_book_save_options (QofBook *book, GNCOptionSave save_cb,
GNCOptionDB* odb, gboolean clear)
{
KvpFrame *slots = qof_instance_get_slots (QOF_INSTANCE (book));
save_cb (odb, slots, clear);
qof_instance_set_dirty (QOF_INSTANCE (book));
}
static void noop (QofInstance *inst) {}
void

View File

@ -64,6 +64,11 @@ typedef struct _QofBookClass QofBookClass;
typedef void (*QofBookDirtyCB) (QofBook *, gboolean dirty, gpointer user_data);
typedef struct gnc_option_db GNCOptionDB;
typedef void (*GNCOptionSave) (GNCOptionDB*, KvpFrame*, gboolean);
typedef void (*GNCOptionLoad) (GNCOptionDB*, KvpFrame*);
/* Book structure */
struct _QofBook
{
@ -328,6 +333,14 @@ 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);
/* Access functions for loading and saving the file options */
void qof_book_load_options (QofBook *book, GNCOptionLoad load_cb,
GNCOptionDB *odb);
void
qof_book_save_options (QofBook *book, GNCOptionSave save_cb,
GNCOptionDB* odb, gboolean clear);
/** deprecated */
#define qof_book_get_guid(X) qof_entity_get_guid (QOF_INSTANCE(X))