Abstract options handling to QofBook.

Eliminates direct KVP access in app-utils, all of which centered around
options.

Beneficial side effect: With all option writes now handled by
qof_book_set_option(), the problem of options not being committed is
eliminated.

The options system is unnecessarily complex, with much back-and-forth
between C and Guile. It needs to be completely rewritten, but that's a
project for its own branch.
This commit is contained in:
John Ralls
2015-06-12 15:19:45 -07:00
parent fa22188549
commit 9b3f6078fc
15 changed files with 239 additions and 225 deletions

View File

@@ -148,9 +148,8 @@
(define (gnc:option-get-value category key)
;;Access an option directly
(kvp-frame-get-slot-path-gslist
(qof-book-get-slots (gnc-get-current-book))
(append gnc:*kvp-option-path* (list category key))))
(qof-book-get-option (gnc-get-current-book)
(list category key)))
(export gnc:option-get-value)
;; config-var.scm
(export gnc:make-config-var)

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);
qof_book_load_options (book, gnc_option_db_load_from_kvp, odb);
qof_book_load_options (book, gnc_option_db_load, odb);
switch (ownertype)
{

View File

@@ -73,9 +73,9 @@
(gnc:error "Illegal invoice value set"))))
(lambda () (convert-to-invoice (default-getter)))
(gnc:restore-form-generator value->string)
(lambda (f p) (kvp-frame-set-slot-path-gslist f option p))
(lambda (f p)
(let ((v (kvp-frame-get-slot-path-gslist f p)))
(lambda (b p) (qof-book-set-option b option p))
(lambda (b p)
(let ((v (qof-book-get-option b p)))
(if (and v (string? v))
(begin
(set! option v)
@@ -134,9 +134,9 @@
(gnc:error "Illegal customer value set"))))
(lambda () (convert-to-customer (default-getter)))
(gnc:restore-form-generator value->string)
(lambda (f p) (kvp-frame-set-slot-path-gslist f option p))
(lambda (f p)
(let ((v (kvp-frame-get-slot-path-gslist f p)))
(lambda (b p) (qof-book-set-option b option p))
(lambda (b p)
(let ((v (qof-book-get-option b p)))
(if (and v (string? v))
(begin
(set! option v)
@@ -195,9 +195,9 @@
(gnc:error "Illegal vendor value set"))))
(lambda () (convert-to-vendor (default-getter)))
(gnc:restore-form-generator value->string)
(lambda (f p) (kvp-frame-set-slot-path-gslist f option p))
(lambda (f p)
(let ((v (kvp-frame-get-slot-path-gslist f p)))
(lambda (b p) (qof-book-set-option b option p))
(lambda (b p)
(let ((v (qof-book-get-option b p)))
(if (and v (string? v))
(begin
(set! option v)
@@ -256,9 +256,9 @@
(gnc:error "Illegal employee value set"))))
(lambda () (convert-to-employee (default-getter)))
(gnc:restore-form-generator value->string)
(lambda (f p) (kvp-frame-set-slot-path-gslist f option p))
(lambda (f p)
(let ((v (kvp-frame-get-slot-path-gslist f p)))
(lambda (b p) (qof-book-set-option b option p))
(lambda (b p)
(let ((v (qof-book-get-option b p)))
(if (and v (string? v))
(begin
(set! option v)
@@ -355,14 +355,14 @@
(gnc:error "Illegal owner value set"))))
(lambda () (convert-to-owner (default-getter)))
(gnc:restore-form-generator value->string)
(lambda (f p)
(kvp-frame-set-slot-path-gslist f (symbol->string (car option))
(lambda (b p)
(qof-book-set-option b (symbol->string (car option))
(append p '("type")))
(kvp-frame-set-slot-path-gslist f (cdr option)
(qof-book-set-option b (cdr option)
(append p '("value"))))
(lambda (f p)
(let ((t (kvp-frame-get-slot-path-gslist f (append p '("type"))))
(v (kvp-frame-get-slot-path-gslist f (append p '("value")))))
(lambda (b p)
(let ((t (qof-book-get-option b (append p '("type"))))
(v (qof-book-get-option b (append p '("value")))))
(if (and t v (string? t) (string? v))
(begin
(set! option (cons (string->symbol t) v))
@@ -422,9 +422,9 @@
(gnc:error "Illegal taxtable value set"))))
(lambda () (convert-to-taxtable (default-getter)))
(gnc:restore-form-generator value->string)
(lambda (f p) (kvp-frame-set-slot-path-gslist f option p))
(lambda (f p)
(let ((v (kvp-frame-get-slot-path-gslist f p)))
(lambda (b p) (qof-book-set-option b option p))
(lambda (b p)
(let ((v (qof-book-get-option b p)))
(if (and v (string? v))
(begin
(set! option v)
@@ -450,9 +450,9 @@
documentation-string
default-value)
(let ((option (gnc:make-number-range-option section name sort-tag documentation-string default-value 0 999999999 0 1)))
(gnc:set-option-scm->kvp option (lambda (f p) (kvp-frame-set-slot-path-gslist f (inexact->exact ((gnc:option-getter option))) (list "counters" key))))
(gnc:set-option-kvp->scm option (lambda (f p)
(let ((v (kvp-frame-get-slot-path-gslist f (list "counters" key))))
(gnc:set-option-scm->kvp option (lambda (b p) (qof-book-set-option b (inexact->exact ((gnc:option-getter option))) (list "counters" key))))
(gnc:set-option-kvp->scm option (lambda (b p)
(let ((v (qof-book-get-option b (list "counters" key))))
(if (and v (integer? v))
((gnc:option-setter option) v)))))
option))
@@ -467,9 +467,9 @@
documentation-string
default-value)
(let ((option (gnc:make-string-option section name sort-tag documentation-string default-value)))
(gnc:set-option-scm->kvp option (lambda (f p) (kvp-frame-set-slot-path-gslist f ((gnc:option-getter option)) (list "counter_formats" key))))
(gnc:set-option-kvp->scm option (lambda (f p)
(let ((v (kvp-frame-get-slot-path-gslist f (list "counter_formats" key))))
(gnc:set-option-scm->kvp option (lambda (b p) (qof-book-set-option b ((gnc:option-getter option)) (list "counter_formats" key))))
(gnc:set-option-kvp->scm option (lambda (b p)
(let ((v (qof-book-get-option b (list "counter_formats" key))))
(if (and v (string? v))
((gnc:option-setter option) v)))))
option))

View File

@@ -313,13 +313,12 @@ gnc_option_db_new_for_type(QofIdType id_type)
}
void
gnc_option_db_load_from_kvp(GNCOptionDB* odb, KvpFrame *slots)
gnc_option_db_load(GNCOptionDB* odb, QofBook *book)
{
static SCM kvp_to_scm = SCM_UNDEFINED;
static SCM kvp_option_path = SCM_UNDEFINED;
SCM scm_slots;
SCM scm_book;
if (!odb || !slots) return;
if (!odb || !book) return;
if (kvp_to_scm == SCM_UNDEFINED)
{
@@ -332,29 +331,19 @@ gnc_option_db_load_from_kvp(GNCOptionDB* odb, KvpFrame *slots)
}
}
if (kvp_option_path == SCM_UNDEFINED)
{
kvp_option_path = scm_c_eval_string("gnc:*kvp-option-path*");
if (kvp_option_path == SCM_UNDEFINED)
{
PERR ("can't find the option path");
return;
}
}
scm_slots = SWIG_NewPointerObj(slots, SWIG_TypeQuery("_p_KvpFrame"), 0);
scm_book = SWIG_NewPointerObj(book, SWIG_TypeQuery("_p_QofBook"), 0);
scm_call_3 (kvp_to_scm, odb->guile_options, scm_slots, kvp_option_path);
scm_call_2 (kvp_to_scm, odb->guile_options, scm_book);
}
void
gnc_option_db_save_to_kvp(GNCOptionDB* odb, KvpFrame *slots, gboolean clear_kvp)
gnc_option_db_save(GNCOptionDB* odb, QofBook *book, gboolean clear_all)
{
static SCM scm_to_kvp = SCM_UNDEFINED;
static SCM kvp_option_path = SCM_UNDEFINED;
SCM scm_slots;
SCM scm_clear_kvp;
SCM scm_book;
SCM scm_clear_all;
if (!odb || !slots) return;
if (!odb || !book) return;
if (scm_to_kvp == SCM_UNDEFINED)
{
@@ -367,19 +356,10 @@ gnc_option_db_save_to_kvp(GNCOptionDB* odb, KvpFrame *slots, gboolean clear_kvp)
}
}
if (kvp_option_path == SCM_UNDEFINED)
{
kvp_option_path = scm_c_eval_string("gnc:*kvp-option-path*");
if (kvp_option_path == SCM_UNDEFINED)
{
PERR ("can't find the option path");
return;
}
}
scm_slots = SWIG_NewPointerObj(slots, SWIG_TypeQuery("_p_KvpFrame"), 0);
scm_clear_kvp = scm_from_bool (clear_kvp);
scm_book = SWIG_NewPointerObj(book, SWIG_TypeQuery("_p_QofBook"), 0);
scm_clear_all = scm_from_bool (clear_all);
scm_call_4 (scm_to_kvp, odb->guile_options, scm_slots, kvp_option_path, scm_clear_kvp);
scm_call_3 (scm_to_kvp, odb->guile_options, scm_book, scm_clear_all);
}
/********************************************************************\
* gnc_option_db_destroy *

View File

@@ -71,8 +71,8 @@ void gnc_option_db_destroy(GNCOptionDB *odb);
* in the kvp.
*/
GNCOptionDB * gnc_option_db_new_for_type(QofIdType id_type);
void gnc_option_db_load_from_kvp(GNCOptionDB* odb, KvpFrame *slots);
void gnc_option_db_save_to_kvp(GNCOptionDB* odb, KvpFrame *slots, gboolean clear_kvp);
void gnc_option_db_load(GNCOptionDB* odb, QofBook *book);
void gnc_option_db_save(GNCOptionDB* odb, QofBook *book, gboolean clear_all);
void gnc_register_kvp_option_generator(QofIdType id_type, SCM generator);

View File

@@ -37,8 +37,8 @@
;; value.
generate-restore-form
;; the scm->kvp and kvp->scm functions should save and load
;; the option to a kvp. The arguments to these function will be
;; a kvp-frame and a base key-path list for this option.
;; the option to the book. The arguments to these function will be
;; a book and a base key-path list for this option.
scm->kvp
kvp->scm
;; Validation func should accept a value and return (#t value)
@@ -211,9 +211,9 @@
(lambda (x) (set! value x))
(lambda () default-value)
(gnc:restore-form-generator value->string)
(lambda (f p) (kvp-frame-set-slot-path-gslist f value p))
(lambda (f p)
(let ((v (kvp-frame-get-slot-path-gslist f p)))
(lambda (b p) (qof-book-set-option b value p))
(lambda (b p)
(let ((v (qof-book-get-option b p)))
(if (and v (string? v))
(set! value v))))
(lambda (x)
@@ -235,9 +235,9 @@
(lambda (x) (set! value x))
(lambda () default-value)
(gnc:restore-form-generator value->string)
(lambda (f p) (kvp-frame-set-slot-path-gslist f value p))
(lambda (f p)
(let ((v (kvp-frame-get-slot-path-gslist f p)))
(lambda (b p) (qof-book-set-option b value p))
(lambda (b p)
(let ((v (qof-book-get-option b p)))
(if (and v (string? v))
(set! value v))))
(lambda (x)
@@ -269,9 +269,9 @@
(lambda (x) (set! value x))
(lambda () default-value)
(gnc:restore-form-generator value->string)
(lambda (f p) (kvp-frame-set-slot-path-gslist f value p))
(lambda (f p)
(let ((v (kvp-frame-get-slot-path-gslist f p)))
(lambda (b p) (qof-book-set-option b value p))
(lambda (b p)
(let ((v (qof-book-get-option b p)))
(if (and v (string? v))
(set! value v))))
(lambda (x)
@@ -308,9 +308,9 @@
(lambda (x) (set! value (currency->scm x)))
(lambda () (scm->currency default-value))
(gnc:restore-form-generator value->string)
(lambda (f p) (kvp-frame-set-slot-path-gslist f value p))
(lambda (f p)
(let ((v (kvp-frame-get-slot-path-gslist f p)))
(lambda (b p) (qof-book-set-option b value p))
(lambda (b p)
(let ((v (qof-book-get-option b p)))
(if (and v (string? v))
(set! value v))))
(lambda (x) (list #t x))
@@ -369,14 +369,14 @@
" (gnc-get-current-book)))))"))
;; scm->kvp -- commit the change
;; f -- kvp-frame; p -- key-path
(lambda (f p)
(kvp-frame-set-slot-path-gslist
f (gncBudgetGetGUID selection-budget) p))
;; b -- book; p -- key-path
(lambda (b p)
(qof-book-set-option
b (gncBudgetGetGUID selection-budget) p))
;; kvp->scm -- get the stored value
(lambda (f p)
(let ((v (kvp-frame-get-slot-path-gslist f p)))
(lambda (b p)
(let ((v (qof-book-get-option b p)))
(if (and v (string? v))
(begin
(set! selection-budget (gnc-budget-lookup v (gnc-get-current-book)))))))
@@ -441,12 +441,12 @@
(set! value (commodity->scm x))))
(lambda () default-value)
(gnc:restore-form-generator value->string)
(lambda (f p)
(kvp-frame-set-slot-path-gslist f (cadr value) (append p '("ns")))
(kvp-frame-set-slot-path-gslist f (caddr value) (append p '("monic"))))
(lambda (f p)
(let ((ns (kvp-frame-get-slot-path-gslist f (append p '("ns"))))
(monic (kvp-frame-get-slot-path-gslist f (append p '("monic")))))
(lambda (b p)
(qof-book-set-option b (cadr value) (append p '("ns")))
(qof-book-set-option b (caddr value) (append p '("monic"))))
(lambda (b p)
(let ((ns (qof-book-get-option b (append p '("ns"))))
(monic (qof-book-get-option b (append p '("monic")))))
(if (and ns monic (string? ns) (string? monic))
(set! value (list 'commodity-scm ns monic)))))
(lambda (x) (list #t x))
@@ -500,14 +500,14 @@
(setter-function-called-cb x)))
(lambda () default-value)
(gnc:restore-form-generator value->string)
(lambda (f p) (kvp-frame-set-slot-path-gslist f
(lambda (b p) (qof-book-set-option b
;; As no boolean KvpValue exists, as a workaround
;; we store the string "t" for TRUE and "f" for
;; FALSE in a string KvpValue.
(if value "t" "f")
p))
(lambda (f p)
(let ((v (kvp-frame-get-slot-path-gslist f p)))
(lambda (b p)
(let ((v (qof-book-get-option b p)))
;; As no boolean KvpValue exists, as a workaround we store
;; the string "t" for TRUE and "f" for FALSE.
(cond ((equal? v "t") (set! v #t))
@@ -582,17 +582,17 @@
(gnc:error "Illegal date value set:" date)))
default-getter
(gnc:restore-form-generator value->string)
(lambda (f p)
(kvp-frame-set-slot-path-gslist f (symbol->string (car value))
(lambda (b p)
(qof-book-set-option b (symbol->string (car value))
(append p '("type")))
(kvp-frame-set-slot-path-gslist f
(qof-book-set-option b
(if (symbol? (cdr value))
(symbol->string (cdr value))
(cdr value))
(append p '("value"))))
(lambda (f p)
(let ((t (kvp-frame-get-slot-path-gslist f (append p '("type"))))
(v (kvp-frame-get-slot-path-gslist f (append p '("value")))))
(lambda (b p)
(let ((t (qof-book-get-option b (append p '("type"))))
(v (qof-book-get-option b (append p '("value")))))
(if (and t v (string? t))
(set! value (cons (string->symbol t)
(if (string? v) (string->symbol v) v))))))
@@ -720,25 +720,25 @@
(gnc:error "Illegal account list value set"))))
(lambda () (map convert-to-account (default-getter)))
(gnc:restore-form-generator value->string)
(lambda (f p)
(lambda (b p)
(define (save-acc list count)
(if (not (null? list))
(let ((key (string-append "acc" (gnc:value->string count))))
(kvp-frame-set-slot-path-gslist f (car list) (append p (list key)))
(qof-book-set-option b (car list) (append p (list key)))
(save-acc (cdr list) (+ 1 count)))))
(if option-set
(begin
(kvp-frame-set-slot-path-gslist f (length option)
(qof-book-set-option b (length option)
(append p '("len")))
(save-acc option 0))))
(lambda (f p)
(let ((len (kvp-frame-get-slot-path-gslist f (append p '("len")))))
(lambda (b p)
(let ((len (qof-book-get-option b (append p '("len")))))
(define (load-acc count)
(if (< count len)
(let* ((key (string-append "acc" (gnc:value->string count)))
(guid (kvp-frame-get-slot-path-gslist
f (append p (list key)))))
(guid (qof-book-get-option
b (append p (list key)))))
(cons guid (load-acc (+ count 1))))
'()))
@@ -839,9 +839,9 @@
(gnc:error "Illegal account value set"))))
(lambda () (convert-to-account (get-default)))
(gnc:restore-form-generator value->string)
(lambda (f p) (kvp-frame-set-slot-path-gslist f option p))
(lambda (f p)
(let ((v (kvp-frame-get-slot-path-gslist f p)))
(lambda (b p) (qof-book-set-option b option p))
(lambda (b p)
(let ((v (qof-book-get-option b p)))
(if (and v (string? v))
(set! option v))))
validator
@@ -918,9 +918,9 @@
(gnc:error "Illegal Multichoice option set")))
(lambda () default-value)
(gnc:restore-form-generator value->string)
(lambda (f p) (kvp-frame-set-slot-path-gslist f (symbol->string value) p))
(lambda (f p)
(let ((v (kvp-frame-get-slot-path-gslist f p)))
(lambda (b p) (qof-book-set-option b (symbol->string value) p))
(lambda (b p)
(let ((v (qof-book-get-option b p)))
(if (and v (string? v))
(set! value (string->symbol v)))))
(lambda (x)
@@ -1004,9 +1004,9 @@
(gnc:error "Illegal Radiobutton option set")))
(lambda () default-value)
(gnc:restore-form-generator value->string)
(lambda (f p) (kvp-frame-set-slot-path-gslist f (symbol->string value) p))
(lambda (f p)
(let ((v (kvp-frame-get-slot-path-gslist f p)))
(lambda (b p) (qof-book-set-option b (symbol->string value) p))
(lambda (b p)
(let ((v (qof-book-get-option b p)))
(if (and v (string? v))
(set! value (string->symbol v)))))
(lambda (x)
@@ -1066,21 +1066,21 @@
(gnc:error "Illegal list option set")))
(lambda () default-value)
(gnc:restore-form-generator value->string)
(lambda (f p)
(lambda (b p)
(define (save-item list count)
(if (not (null? list))
(let ((key (string-append "item" (gnc:value->string count))))
(kvp-frame-set-slot-path-gslist f (car list) (append p (list key)))
(qof-book-set-option b (car list) (append p (list key)))
(save-item (cdr list) (+ 1 count)))))
(kvp-frame-set-slot-path-gslist f (length value) (append p '("len")))
(qof-book-set-option b (length value) (append p '("len")))
(save-item value 0))
(lambda (f p)
(let ((len (kvp-frame-get-slot-path-gslist f (append p '("len")))))
(lambda (b p)
(let ((len (qof-book-get-option b (append p '("len")))))
(define (load-item count)
(if (< count len)
(let* ((key (string-append "item" (gnc:value->string count)))
(val (kvp-frame-get-slot-path-gslist
f (append p (list key)))))
(val (qof-book-get-option
b (append p (list key)))))
(cons val (load-item (+ count 1))))
'()))
@@ -1118,9 +1118,9 @@
(lambda (x) (set! value x))
(lambda () default-value)
(gnc:restore-form-generator value->string)
(lambda (f p) (kvp-frame-set-slot-path-gslist f value p))
(lambda (f p)
(let ((v (kvp-frame-get-slot-path-gslist f p)))
(lambda (b p) (qof-book-set-option b value p))
(lambda (b p)
(let ((v (qof-book-get-option b p)))
(if (and v (number? v))
(set! value v))))
(lambda (x)
@@ -1277,19 +1277,19 @@
(lambda (x) (set! value x))
(lambda () (def-value))
(gnc:restore-form-generator value->string)
(lambda (b p)
(qof-book-set-option
b (symbol->string (car value)) (append p '("fmt")))
(qof-book-set-option
b (symbol->string (cadr value)) (append p '("month")))
(qof-book-set-option
b (if (caddr value) 1 0) (append p '("years")))
(qof-book-set-option (cadddr value) (append p '("custom"))))
(lambda (f p)
(kvp-frame-set-slot-path-gslist
f (symbol->string (car value)) (append p '("fmt")))
(kvp-frame-set-slot-path-gslist
f (symbol->string (cadr value)) (append p '("month")))
(kvp-frame-set-slot-path-gslist
f (if (caddr value) 1 0) (append p '("years")))
(kvp-frame-set-slot-path-gslist f (cadddr value) (append p '("custom"))))
(lambda (f p)
(let ((fmt (kvp-frame-get-slot-path-gslist f (append p '("fmt"))))
(month (kvp-frame-get-slot-path-gslist f (append p '("month"))))
(years (kvp-frame-get-slot-path-gslist f (append p '("years"))))
(custom (kvp-frame-get-slot-path-gslist f (append p '("custom")))))
(let ((fmt (qof-book-get-option f (append p '("fmt"))))
(month (qof-book-get-option f (append p '("month"))))
(years (qof-book-get-option f (append p '("years"))))
(custom (qof-book-get-option f (append p '("custom")))))
(if (and
fmt (string? fmt)
month (string? month)
@@ -1364,14 +1364,11 @@
(value->string (lambda ()
(string-append "'" (gnc:value->string
(car value)))))
(trading-accounts-path (list (car gnc:*kvp-option-path*)
gnc:*option-section-accounts*
(trading-accounts-path (list gnc:*option-section-accounts*
gnc:*option-name-trading-accounts*))
(book-currency-path (list (car gnc:*kvp-option-path*)
gnc:*option-section-accounts*
(book-currency-path (list gnc:*option-section-accounts*
gnc:*option-name-book-currency*))
(gains-policy-path (list (car gnc:*kvp-option-path*)
gnc:*option-section-accounts*
(gains-policy-path (list gnc:*option-section-accounts*
gnc:*option-name-default-gains-policy*)))
(gnc:make-option
section name sort-tag 'currency-accounting
@@ -1387,26 +1384,26 @@
(cons default-cap-gains-policy-value '())))
(cons default-radiobutton-value '())))
(gnc:restore-form-generator value->string)
(lambda (f p)
(lambda (b p)
(if (eq? 'book-currency (car value))
(begin
;; Currency = selected currency
(kvp-frame-set-slot-path-gslist
f
(qof-book-set-option
b
(currency->scm (cadr value))
book-currency-path)
;; Default Gains Policy = selected policy
(kvp-frame-set-slot-path-gslist
f
(qof-book-set-option
b
(symbol->string (caddr value))
gains-policy-path))
(if (eq? 'trading (car value))
;; Use Trading Accounts = "t"
(kvp-frame-set-slot-path-gslist f "t" trading-accounts-path))))
(lambda (f p)
(qof-book-set-option b "t" trading-accounts-path))))
(lambda (b p)
(let* ((trading-option-path-kvp?
(kvp-frame-get-slot-path-gslist
f trading-accounts-path))
(qof-book-get-option
b trading-accounts-path))
(trading? (if (and trading-option-path-kvp?
(string=? "t" trading-option-path-kvp?))
#t
@@ -1416,11 +1413,11 @@
(v (if trading?
'trading
(let* ((book-currency-option-path-kvp?
(kvp-frame-get-slot-path-gslist
f book-currency-path))
(qof-book-get-option
b book-currency-path))
(gains-policy-option-path-kvp?
(kvp-frame-get-slot-path-gslist
f gains-policy-path))
(qof-book-get-option
b gains-policy-path))
(book-currency?
(if (and book-currency-option-path-kvp?
gains-policy-option-path-kvp?
@@ -1650,7 +1647,7 @@
(call-with-output-string generate-forms))
(define (scm->kvp kvp-frame key-path)
(define (scm->kvp book)
(options-for-each
(lambda (option)
(let ((value (gnc:option-value option))
@@ -1663,18 +1660,16 @@
(let ((save-fcn (gnc:option-scm->kvp option)))
(gnc:debug "save-fcn: " save-fcn)
(if save-fcn
(save-fcn kvp-frame (append key-path
(list section name))))))))))
(save-fcn book (list section name)))))))))
(define (kvp->scm kvp-frame key-path)
(define (kvp->scm book)
(options-for-each
(lambda (option)
(let ((section (gnc:option-section option))
(name (gnc:option-name option))
(load-fcn (gnc:option-kvp->scm option)))
(if load-fcn
(load-fcn kvp-frame (append key-path
(list section name))))))))
(load-fcn book (list section name)))))))
(define (register-callback section name callback)
(let ((id last-callback-id)
@@ -1774,13 +1769,13 @@
(define (gnc:generate-restore-forms options options-string)
((options 'generate-restore-forms) options-string))
(define (gnc:options-scm->kvp options kvp-frame key-path clear-kvp?)
(if clear-kvp?
(gnc-kvp-frame-delete-at-path kvp-frame key-path))
((options 'scm->kvp) kvp-frame key-path))
(define (gnc:options-scm->kvp options book clear-option?)
(if clear-option?
(qof-book-options-delete book))
((options 'scm->kvp) book))
(define (gnc:options-kvp->scm options kvp-frame key-path)
((options 'kvp->scm) kvp-frame key-path))
(define (gnc:options-kvp->scm options book)
((options 'kvp->scm) book))
(define (gnc:options-clear-changes options)
((options 'clear-changes)))

View File

@@ -1,5 +1,5 @@
/********************************************************************
* test-option-util.c: GLib g_test test suite for Split.c. *
* test-option-util.cpp: GLib test suite for option-util.c. *
* Copyright 2013 John Ralls <jralls@ceridwen.us> *
* *
* This program is free software; you can redistribute it and/or *
@@ -93,14 +93,14 @@ teardown (Fixture *fixture, gconstpointer pData)
}
static void
test_option_load_from_kvp (Fixture *fixture, gconstpointer pData)
test_option_load (Fixture *fixture, gconstpointer pData)
{
gchar *str = NULL;
SCM symbol_value;
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);
qof_book_load_options (book, gnc_option_db_load, odb);
symbol_value = gnc_currency_accounting_option_value_get_method (
gnc_option_db_lookup_option (odb,
OPTION_SECTION_ACCOUNTS,
@@ -127,7 +127,7 @@ test_option_load_from_kvp (Fixture *fixture, gconstpointer pData)
}
static void
test_option_load_from_kvp_book_currency (Fixture *fixture, gconstpointer pData)
test_option_load_book_currency (Fixture *fixture, gconstpointer pData)
{
gchar *str = NULL;
SCM symbol_value;
@@ -137,7 +137,7 @@ test_option_load_from_kvp_book_currency (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);
qof_book_load_options (book, gnc_option_db_load, odb);
symbol_value = gnc_currency_accounting_option_value_get_method (
gnc_option_db_lookup_option (odb,
OPTION_SECTION_ACCOUNTS,
@@ -186,7 +186,7 @@ test_option_load_from_kvp_book_currency (Fixture *fixture, gconstpointer pData)
}
static void
test_option_save_to_kvp (Fixture *fixture, gconstpointer pData)
test_option_save (Fixture *fixture, gconstpointer pData)
{
QofBook *book = fixture->book;
GNCOptionDB *odb = gnc_option_db_new_for_type (QOF_ID_BOOK);
@@ -203,7 +203,7 @@ test_option_save_to_kvp (Fixture *fixture, gconstpointer pData)
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);
qof_book_save_options (book, gnc_option_db_save, 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/Accounts/Use Split Action Field for Number"), == , "t");
g_assert_cmpstr (kvp_frame_get_string (slots, "options/Business/Company Name"), ==, "Bogus Company");
@@ -213,7 +213,7 @@ test_option_save_to_kvp (Fixture *fixture, gconstpointer pData)
}
static void
test_option_save_to_kvp_book_currency (Fixture *fixture, gconstpointer pData)
test_option_save_book_currency (Fixture *fixture, gconstpointer pData)
{
QofBook *book = fixture->book;
GNCOptionDB *odb = gnc_option_db_new_for_type (QOF_ID_BOOK);
@@ -224,9 +224,9 @@ test_option_save_to_kvp_book_currency (Fixture *fixture, gconstpointer pData)
scm_cons (scm_from_locale_symbol("book-currency"),
scm_cons (scm_from_utf8_string("GTQ"),
scm_cons (scm_from_locale_symbol("fifo"), SCM_EOL)))));
qof_book_save_options (book, gnc_option_db_save_to_kvp, odb, TRUE);
g_assert_cmpstr (kvp_frame_get_string (slots, "options/Accounts/Book Currency"), == , "GTQ");
g_assert_cmpstr (kvp_frame_get_string (slots, "options/Accounts/Default Gains Policy"), == , "fifo");
qof_book_save_options (book, gnc_option_db_save, odb, TRUE);
g_assert_cmpstr (kvp_frame_get_string(slots, "options/Accounts/Book Currency"), == , "GTQ");
g_assert_cmpstr (kvp_frame_get_string(slots, "options/Accounts/Default Gains Policy"), == , "fifo");
gnc_option_db_destroy (odb);
}
@@ -234,9 +234,8 @@ test_option_save_to_kvp_book_currency (Fixture *fixture, gconstpointer pData)
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 Load from KVP - Book Currency", Fixture, NULL, setup_kvp_book_currency, test_option_load_from_kvp_book_currency, teardown);
GNC_TEST_ADD (suitename, "Option DB Save to KVP", Fixture, NULL, setup, test_option_save_to_kvp, teardown);
GNC_TEST_ADD (suitename, "Option DB Save to KVP - Book Currency", Fixture, NULL, setup, test_option_save_to_kvp_book_currency, teardown);
GNC_TEST_ADD (suitename, "Option DB Load", Fixture, NULL, setup_kvp, test_option_load, teardown);
GNC_TEST_ADD (suitename, "Option DB Load - Book Currency", Fixture, NULL, setup_kvp_book_currency, test_option_load_book_currency, teardown);
GNC_TEST_ADD (suitename, "Option DB Save", Fixture, NULL, setup, test_option_save, teardown);
GNC_TEST_ADD (suitename, "Option DB Save - Book Currency", Fixture, NULL, setup, test_option_save_book_currency, teardown);
}

View File

@@ -142,18 +142,6 @@ functions. */
QofSession * qof_session_new (void);
QofBook * qof_session_get_book (QofSession *session);
/* This horror is to permit the scheme options in
* src/app-utils/options.scm to read and write the book's KVP (another
* horror) directly. It should be refactored into book functions that
* handle the KVP access.
*/
%inline {
KvpFrame *qof_book_get_slots (QofBook *book);
extern KvpFrame *qof_instance_get_slots (QofInstance*);
KvpFrame *qof_book_get_slots (QofBook *book) {
return qof_instance_get_slots (QOF_INSTANCE (book));
}
}
// TODO: Unroll/remove
const char *qof_session_get_url (QofSession *session);
@@ -238,10 +226,9 @@ Account * gnc_book_get_template_root(QofBook *book);
%typemap(out) KvpValue * " $result = gnc_kvp_value_ptr_to_scm($1); "
%typemap(in) GSList *key_path " $1 = gnc_scm_to_gslist_string($input);"
void gnc_kvp_frame_delete_at_path(KvpFrame *frame, GSList *key_path);
void kvp_frame_set_slot_path_gslist(
KvpFrame *frame, KvpValue *new_value, GSList *key_path);
KvpValue * kvp_frame_get_slot_path_gslist (KvpFrame *frame, GSList *key_path);
void qof_book_options_delete (QofBook *book);
void qof_book_set_option (QofBook *book, KvpValue *new_value, GSList *key_path);
KvpValue* qof_book_get_option (QofBook *book, GSList *key_path);
%clear GSList *key_path;

View File

@@ -112,9 +112,3 @@ gnc_kvp_value_ptr_to_scm(KvpValue* val)
}
return SCM_BOOL_F;
}
void
gnc_kvp_frame_delete_at_path (KvpFrame *frame, GSList *key_path)
{
kvp_frame_set_slot_path_gslist (frame, NULL, key_path);
}

View File

@@ -6,7 +6,6 @@
KvpValue* gnc_scm_to_kvp_value_ptr(SCM kvpval);
SCM gnc_kvp_value_ptr_to_scm(KvpValue* val);
void gnc_kvp_frame_delete_at_path(KvpFrame *frame, GSList *key_path);
#endif /* KVP_SCM_H */

View File

@@ -3950,7 +3950,7 @@ gnc_book_options_dialog_apply_cb(GNCOptionWin * optionwin,
gnc_option_db_commit (options);
qof_book_begin_edit (book);
qof_book_save_options (book, gnc_option_db_save_to_kvp, options, TRUE);
qof_book_save_options (book, gnc_option_db_save, 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)
@@ -3976,7 +3976,7 @@ gnc_book_options_dialog_cb (gboolean modal, gchar *title)
GNCOptionWin *optionwin;
options = gnc_option_db_new_for_type (QOF_ID_BOOK);
qof_book_load_options (book, gnc_option_db_load_from_kvp, options);
qof_book_load_options (book, gnc_option_db_load, options);
gnc_option_db_clean (options);
optionwin = gnc_options_dialog_new_modal (modal,

View File

@@ -23,7 +23,5 @@
(export gnc:make-menu)
(export gnc:make-separator)
(export gnc:kvp-option-dialog)
(load-from-path "gnc-menu-extensions")

View File

@@ -1021,7 +1021,7 @@ finish_book_options_helper(GNCOptionWin * optionwin,
if (!options) return;
gnc_option_db_commit (options);
qof_book_save_options (book, gnc_option_db_save_to_kvp, options, TRUE);
qof_book_save_options (book, gnc_option_db_save, options, TRUE);
use_split_action_for_num_after =
qof_book_use_split_action_for_num_field (book);
if (use_split_action_for_num_before != use_split_action_for_num_after)
@@ -1127,7 +1127,7 @@ assistant_instert_book_options_page (hierarchy_data *data)
data->options = gnc_option_db_new_for_type (QOF_ID_BOOK);
qof_book_load_options (gnc_get_current_book (),
gnc_option_db_load_from_kvp, data->options);
gnc_option_db_load, data->options);
gnc_option_db_clean (data->options);
data->optionwin = gnc_options_dialog_new_modal (TRUE, _("New Book Options"));

View File

@@ -1096,17 +1096,20 @@ qof_book_set_feature (QofBook *book, const gchar *key, const gchar *descr)
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);
load_cb (odb, book);
}
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));
/* Wrap this in begin/commit so that it commits only once instead of doing
* so for every option. Qof_book_set_option will take care of dirtying the
* book.
*/
qof_book_begin_edit (book);
save_cb (odb, book, clear);
qof_book_commit_edit (book);
}
static void noop (QofInstance *inst) {}
@@ -1118,6 +1121,32 @@ qof_book_commit_edit(QofBook *book)
qof_commit_edit_part2 (&book->inst, commit_err, noop, noop/*lot_free*/);
}
void
qof_book_set_option (QofBook *book, KvpValue *value, GSList *path)
{
KvpFrame *root = qof_instance_get_slots (QOF_INSTANCE (book));
KvpFrame *options = kvp_frame_get_frame_slash (root, KVP_OPTION_PATH);
qof_book_begin_edit (book);
kvp_frame_set_slot_path_gslist (options, value, path);
qof_instance_set_dirty (QOF_INSTANCE (book));
qof_book_commit_edit (book);
}
KvpValue*
qof_book_get_option (QofBook *book, GSList *path)
{
KvpFrame *root = qof_instance_get_slots(QOF_INSTANCE (book));
KvpFrame *options = kvp_frame_get_frame(root, KVP_OPTION_PATH);
return kvp_frame_get_slot_path_gslist(options, path);
}
void
qof_book_options_delete (QofBook *book)
{
KvpFrame *root = qof_instance_get_slots(QOF_INSTANCE (book));
kvp_frame_delete (kvp_frame_get_frame(root, KVP_OPTION_PATH));
}
/* QofObject function implementation and registration */
gboolean qof_book_register (void)
{

View File

@@ -71,8 +71,8 @@ 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*);
typedef void (*GNCOptionSave) (GNCOptionDB*, QofBook*, gboolean);
typedef void (*GNCOptionLoad) (GNCOptionDB*, QofBook*);
/* Book structure */
struct _QofBook
@@ -353,13 +353,47 @@ 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 */
/* Access functions for options. */
/** Load a GNCOptionsDB from KVP data.
* @param book: The book.
* @param load_cb: A callback function that does the loading.
* @param odb: The GNCOptionDB to load.
*/
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);
/** Save a GNCOptionsDB back to the book's KVP.
* @param book: The book.
* @param save_cb: A callback function that does the saving.
* @param odb: The GNCOptionsDB to save from.
* @param clear: Should the GNCOptionsDB be emptied after the save?
*/
void qof_book_save_options (QofBook *book, GNCOptionSave save_cb,
GNCOptionDB* odb, gboolean clear);
/** Save a single option value.
* Used from Scheme, the KvpValue<-->SCM translation is handled by the functions
* in kvp-scm.c and automated by SWIG. The starting element is set as
* KVP_OPTION_PATH in qofbookslots.h.
* @param book: The book.
* @param value: The KvpValue to store.
* @param path: A GSList of keys which form a path under KVP_OPTION_PATH.
*/
void qof_book_set_option (QofBook *book, KvpValue *value, GSList *path);
/** Read a single option value.
* Used from Scheme, the KvpValue<-->SCM translation is handled by the functions
* in kvp-scm.c and automated by SWIG. The starting element is set as
* KVP_OPTION_PATH in qofbookslots.h.
* @param book: The book.
* @param path: A GSList of keys which form a path under KVP_OPTION_PATH.
*/
KvpValue* qof_book_get_option (QofBook *book, GSList *path);
/** Delete the options.
* Primarily used from Scheme to clear out the options before saving a new set.
* @param book: The book.
* @param list: A GList of keys which from a path under KVP_OPTION_PATH.
*/
void qof_book_options_delete (QofBook *book);
/** deprecated */
#define qof_book_get_guid(X) qof_entity_get_guid (QOF_INSTANCE(X))