Bug #511182: Commodity mapping preferences are now preserved correctly. In

addition, use of the misleading term "stock" has been replaced by "security"
throughout the C code. Also includes a small fix to prevent passing a null
pointer to xaccAccountGetType(), which caused some critical warnings to be
logged. Some comment and whitespace cleanup as well.
BP


git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@17074 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Charles Day 2008-04-10 20:37:41 +00:00
parent f034c41dfb
commit 54d94702d5
4 changed files with 326 additions and 290 deletions

View File

@ -118,8 +118,9 @@ struct _qifimportwindow {
SCM memo_display_info;
SCM gnc_acct_info;
SCM stock_hash;
SCM new_stocks;
SCM security_hash;
SCM security_prefs;
SCM new_securities;
SCM ticker_map;
SCM imported_account_tree;
@ -192,8 +193,9 @@ gnc_ui_qif_import_druid_destroy (QIFImportWindow * window)
scm_gc_unprotect_object(window->memo_map_info);
scm_gc_unprotect_object(window->acct_display_info);
scm_gc_unprotect_object(window->acct_map_info);
scm_gc_unprotect_object(window->stock_hash);
scm_gc_unprotect_object(window->new_stocks);
scm_gc_unprotect_object(window->security_hash);
scm_gc_unprotect_object(window->security_prefs);
scm_gc_unprotect_object(window->new_securities);
scm_gc_unprotect_object(window->ticker_map);
scm_gc_unprotect_object(window->imported_account_tree);
scm_gc_unprotect_object(window->match_transactions);
@ -240,7 +242,7 @@ get_next_druid_page(QIFImportWindow * wind, GnomeDruidPage * page)
next = current->next;
while (!next ||
(!wind->show_doc_pages && g_list_find(wind->doc_pages, next->data)) ||
(wind->new_stocks == SCM_BOOL_F &&
(wind->new_securities == SCM_BOOL_F &&
GNOME_DRUID_PAGE(next->data) == get_named_page(wind, "commodity_doc_page"))) {
if(next && next->next) {
next = next->next;
@ -316,7 +318,7 @@ get_prev_druid_page(QIFImportWindow * wind, GnomeDruidPage * page)
* (c) the page is commodity related and the are no new commodities. */
while (!prev ||
(!wind->show_doc_pages && g_list_find(wind->doc_pages, prev->data)) ||
(wind->new_stocks == SCM_BOOL_F &&
(wind->new_securities == SCM_BOOL_F &&
GNOME_DRUID_PAGE(prev->data) == get_named_page(wind, "commodity_doc_page"))) {
/* We're either out of pages for this stage, or we've reached
* an optional doc page that shouldn't be shown. */
@ -333,7 +335,7 @@ get_prev_druid_page(QIFImportWindow * wind, GnomeDruidPage * page)
prev = g_list_last(wind->pre_comm_pages);
break;
case 2:
if(wind->new_stocks != SCM_BOOL_F) {
if(wind->new_securities != SCM_BOOL_F) {
prev = g_list_last(wind->commodity_pages);
}
else {
@ -1349,7 +1351,7 @@ gnc_ui_qif_import_commodity_update(QIFImportWindow * wind)
* each reference to the "old" commodity with a reference to the commodity
* returned by gnc_commodity_table_insert? */
if (old_commodity != page->commodity)
scm_hash_remove_x(wind->stock_hash, scm_makfrom0str(fullname));
scm_hash_remove_x(wind->security_hash, scm_makfrom0str(fullname));
}
}
@ -1456,7 +1458,7 @@ gnc_ui_qif_import_convert(QIFImportWindow * wind)
wind->acct_map_info,
wind->cat_map_info,
wind->memo_map_info,
wind->stock_hash,
wind->security_hash,
scm_makfrom0str(currname),
window),
SCM_EOL);
@ -1546,26 +1548,27 @@ static gboolean
gnc_ui_qif_import_new_securities(QIFImportWindow * wind)
{
SCM updates;
SCM update_stock = scm_c_eval_string("qif-import:update-stock-hash");
SCM update_securities = scm_c_eval_string("qif-import:update-security-hash");
/* Get a list of any new QIF securities since the previous call. */
updates = scm_call_3(update_stock, wind->stock_hash,
updates = scm_call_3(update_securities, wind->security_hash,
wind->ticker_map, wind->acct_map_info);
if (updates != SCM_BOOL_F)
{
/* A list of new QIF securities was returned. Save it. */
scm_gc_unprotect_object(wind->new_stocks);
if (wind->new_stocks != SCM_BOOL_F)
scm_gc_unprotect_object(wind->new_securities);
if (wind->new_securities != SCM_BOOL_F)
/* There is an existing list, so append the new list. */
wind->new_stocks = scm_append(scm_list_2(wind->new_stocks, updates));
wind->new_securities = scm_append(scm_list_2(wind->new_securities,
updates));
else
wind->new_stocks = updates;
scm_gc_protect_object(wind->new_stocks);
wind->new_securities = updates;
scm_gc_protect_object(wind->new_securities);
return TRUE;
}
if (wind->new_stocks != SCM_BOOL_F)
if (wind->new_securities != SCM_BOOL_F)
return TRUE;
return FALSE;
@ -1592,7 +1595,7 @@ gnc_ui_qif_import_memo_next_cb(GnomeDruidPage * page,
return gnc_ui_qif_import_generic_next_cb(page, arg1, wind);
else
{
/* if we need to look at stocks, do that, otherwise import
/* If we need to look at securities do that; otherwise import
xtns and go to the duplicates page */
if (gnc_ui_qif_import_new_securities(wind))
{
@ -1728,7 +1731,7 @@ gnc_ui_qif_import_comm_check_cb(GnomeDruidPage * page,
/********************************************************************
* gnc_ui_qif_import_commodity_prepare_cb
* build a mapping of QIF stock name to a gnc_commodity
* build a mapping of QIF security name to gnc_commodity
********************************************************************/
static void
@ -1739,7 +1742,7 @@ gnc_ui_qif_import_commodity_prepare_cb(GnomeDruidPage * page,
QIFImportWindow * wind = user_data;
SCM hash_ref = scm_c_eval_string("hash-ref");
SCM stocks;
SCM securities;
SCM comm_ptr_token;
GList * current;
@ -1748,7 +1751,7 @@ gnc_ui_qif_import_commodity_prepare_cb(GnomeDruidPage * page,
QIFDruidPage * new_page;
/* This shouldn't happen, but do the right thing if it does. */
if (wind->new_stocks == SCM_BOOL_F || SCM_NULLP(wind->new_stocks))
if (wind->new_securities == SCM_BOOL_F || SCM_NULLP(wind->new_securities))
{
g_warning("QIF import: BUG DETECTED! Reached commodity doc page with nothing to do!");
gnc_ui_qif_import_convert(wind);
@ -1759,9 +1762,9 @@ gnc_ui_qif_import_commodity_prepare_cb(GnomeDruidPage * page,
* Make druid pages for each new QIF security.
*/
gnc_set_busy_cursor(NULL, TRUE);
stocks = wind->new_stocks;
securities = wind->new_securities;
current = wind->commodity_pages;
while (!SCM_NULLP(stocks) && (stocks != SCM_BOOL_F))
while (!SCM_NULLP(securities) && (securities != SCM_BOOL_F))
{
if (current)
{
@ -1773,8 +1776,8 @@ gnc_ui_qif_import_commodity_prepare_cb(GnomeDruidPage * page,
{
/* Get the GnuCash commodity corresponding to the new QIF security. */
comm_ptr_token = scm_call_2(hash_ref,
wind->stock_hash,
SCM_CAR(stocks));
wind->security_hash,
SCM_CAR(securities));
#define FUNC_NAME "make_qif_druid_page"
commodity = SWIG_MustGetPtr(comm_ptr_token,
SWIG_TypeQuery("_p_gnc_commodity"), 1, 0);
@ -1801,7 +1804,7 @@ gnc_ui_qif_import_commodity_prepare_cb(GnomeDruidPage * page,
gtk_widget_show_all(new_page->page);
}
stocks = SCM_CDR(stocks);
securities = SCM_CDR(securities);
}
gnc_unset_busy_cursor(NULL);
@ -2086,8 +2089,9 @@ gnc_ui_qif_import_finish_cb(GnomeDruidPage * gpage,
/* Save the user's mapping preferences. */
scm_apply(save_map_prefs,
SCM_LIST4(wind->acct_map_info, wind->cat_map_info,
wind->memo_map_info, wind->stock_hash),
SCM_LIST5(wind->acct_map_info, wind->cat_map_info,
wind->memo_map_info, wind->security_hash,
wind->security_prefs),
SCM_EOL);
/* Open an account tab in the main window if one doesn't exist already. */
@ -2283,8 +2287,8 @@ gnc_ui_qif_import_druid_make(void)
retval->acct_map_info = SCM_BOOL_F;
retval->memo_display_info = SCM_BOOL_F;
retval->memo_map_info = SCM_BOOL_F;
retval->stock_hash = SCM_BOOL_F;
retval->new_stocks = SCM_BOOL_F;
retval->security_hash = SCM_BOOL_F;
retval->new_securities = SCM_BOOL_F;
retval->ticker_map = SCM_BOOL_F;
retval->imported_account_tree = SCM_BOOL_F;
retval->match_transactions = SCM_BOOL_F;
@ -2445,7 +2449,8 @@ gnc_ui_qif_import_druid_make(void)
retval->acct_map_info = scm_list_ref(mapping_info, scm_int2num(1));
retval->cat_map_info = scm_list_ref(mapping_info, scm_int2num(2));
retval->memo_map_info = scm_list_ref(mapping_info, scm_int2num(3));
retval->stock_hash = scm_list_ref(mapping_info, scm_int2num(4));
retval->security_hash = scm_list_ref(mapping_info, scm_int2num(4));
retval->security_prefs = scm_list_ref(mapping_info, scm_int2num(5));
create_ticker_map = scm_c_eval_string("make-ticker-map");
retval->ticker_map = scm_call_0(create_ticker_map);
@ -2459,8 +2464,9 @@ gnc_ui_qif_import_druid_make(void)
scm_gc_protect_object(retval->memo_map_info);
scm_gc_protect_object(retval->acct_display_info);
scm_gc_protect_object(retval->acct_map_info);
scm_gc_protect_object(retval->stock_hash);
scm_gc_protect_object(retval->new_stocks);
scm_gc_protect_object(retval->security_hash);
scm_gc_protect_object(retval->security_prefs);
scm_gc_protect_object(retval->new_securities);
scm_gc_protect_object(retval->ticker_map);
scm_gc_protect_object(retval->imported_account_tree);
scm_gc_protect_object(retval->match_transactions);

View File

@ -17,10 +17,10 @@
(define (default-interest-acct brokerage security)
(string-append (_ "Interest") (gnc-get-account-separator-string)
brokerage
(if (string=? security "")
""
(string-append (gnc-get-account-separator-string)
security))))
(if (string=? security "")
""
(string-append (gnc-get-account-separator-string)
security))))
(define (default-capital-return-acct brokerage security)
(string-append (_ "Cap Return") (gnc-get-account-separator-string)
@ -610,65 +610,63 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; qif-import:update-stock-hash
;; qif-import:update-security-hash
;;
;; make new commodities for each new stock in acct-hash that isn't
;; already in stock-hash. Return a list of the QIF names of the
;; new stocks or #f if none.
;; Make new commodities for each new security in acct-hash
;; that isn't already in security-hash. Return a list of
;; the QIF names for which new commodities are created, or
;; #f if none.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (qif-import:update-stock-hash stock-hash ticker-map acct-hash)
(define (qif-import:update-security-hash security-hash ticker-map acct-hash)
(let ((names '()))
(hash-fold
(lambda (qif-name map-entry p)
(let ((stock-name (qif-import:get-account-name qif-name)))
(let ((security-name (qif-import:get-account-name qif-name)))
;; is it: a stock or mutual fund and displayed and not already in
;; the stock-hash?
;; the security-hash?
(if (and
stock-name
security-name
(qif-map-entry:display? map-entry)
(or (memv GNC-STOCK-TYPE
(qif-map-entry:allowed-types map-entry))
(memv GNC-MUTUAL-TYPE
(qif-map-entry:allowed-types map-entry)))
(not (hash-ref stock-hash stock-name)))
(let* ((separator (string-ref (gnc-get-account-separator-string) 0))
(existing-gnc-acct
(not (hash-ref security-hash security-name)))
(let ((existing-gnc-acct
(gnc-account-lookup-by-full-name
(gnc-get-current-root-account)
(qif-map-entry:gnc-name map-entry)))
(book (gnc-account-get-book (gnc-get-current-root-account)))
(existing-type
(xaccAccountGetType existing-gnc-acct)))
(book (gnc-account-get-book (gnc-get-current-root-account))))
(if (and (not (null? existing-gnc-acct))
(memv existing-type (list GNC-STOCK-TYPE
GNC-MUTUAL-TYPE)))
(memv (xaccAccountGetType existing-gnc-acct)
(list GNC-STOCK-TYPE GNC-MUTUAL-TYPE)))
;; gnc account already exists... we *know* what the
;; security is supposed to be
(let ((commodity
(xaccAccountGetCommodity existing-gnc-acct)))
(hash-set! stock-hash stock-name commodity))
(hash-set! security-hash security-name commodity))
;; we know nothing about this security.. we need to
;; ask about it
(let ((ticker-symbol
(qif-ticker-map:lookup-ticker ticker-map
stock-name))
security-name))
(namespace GNC_COMMODITY_NS_MUTUAL))
(if (not ticker-symbol)
(set! ticker-symbol stock-name)
(set! ticker-symbol security-name)
(set! namespace
(qif-dialog:default-namespace ticker-symbol)))
(set! names (cons stock-name names))
(hash-set!
stock-hash stock-name
(gnc-commodity-new book
stock-name
namespace
ticker-symbol
""
100000))))))
(set! names (cons security-name names))
(hash-set! security-hash
security-name
(gnc-commodity-new book
security-name
namespace
ticker-symbol
""
100000))))))
#f))
#f acct-hash)

View File

@ -33,7 +33,8 @@
;; - a hash of QIF category to gnucash account info
;; - a hash of QIF memo/payee to gnucash account info
;; (older saved prefs may not have this one)
;; - a hash of QIF stock name to gnc-commodity*
;; - a hash of QIF security name to gnc-commodity*
;; - a list of all previously saved security mappings
;; (older saved prefs may not have this one)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@ -80,18 +81,18 @@
(let ((qif-account-list #f)
(qif-cat-list #f)
(qif-memo-list #f)
(qif-stock-list #f)
(qif-security-list #f)
(qif-account-hash #f)
(qif-cat-hash #f)
(qif-memo-hash #f)
(qif-stock-hash #f)
(qif-security-hash #f)
(saved-sep #f))
;; Read the mapping file.
(set! qif-account-list (safe-read))
(set! qif-cat-list (safe-read))
(set! qif-memo-list (safe-read))
(set! qif-stock-list (safe-read))
(set! qif-security-list (safe-read))
(set! saved-sep (safe-read))
;; Process the QIF account mapping.
@ -114,22 +115,24 @@
saved-sep)))
;; Process the QIF security mapping.
(if (not (list? qif-stock-list))
(set! qif-stock-hash (make-hash-table 20))
(set! qif-stock-hash (qif-import:read-commodities
qif-stock-list)))
(if (not (list? qif-security-list))
(set! qif-security-hash (make-hash-table 20))
(set! qif-security-hash (qif-import:read-securities
qif-security-list)))
;; Put all the mappings together in a list.
(set! results (list qif-account-hash
qif-cat-hash
qif-memo-hash
qif-stock-hash)))))
qif-security-hash
qif-security-list)))))
;; Otherwise, we can't get any saved mappings. Use empty tables.
(set! results (list (make-hash-table 20)
(make-hash-table 20)
(make-hash-table 20)
(make-hash-table 20))))
(make-hash-table 20)
'())))
;; Build the list of all known account names.
(let* ((all-accounts (gnc-get-current-root-account))
@ -180,20 +183,22 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; qif-import:read-commodities
;; qif-import:read-securities
;;
;; This procedure examines a list of previously seen commodities
;; and returns a hash table of them, if they still exist.
;; This procedure examines a list of previously seen security
;; mappings and returns a hash table pairing QIF security names
;; with existing GnuCash commodities.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (qif-import:read-commodities commlist)
(define (qif-import:read-securities security-list)
(let ((table (make-hash-table 20)))
(for-each
(lambda (entry)
(if (and (list? entry)
(= 3 (length entry)))
;; The saved information about each commodity is a
;; list of three items: name, namespace, and mnemonic.
;; The saved information about each security mapping is a
;; list of three items: the QIF name, and the GnuCash
;; namespace and mnemonic (symbol) to which it maps.
;; Example: ("McDonald's" "NYSE" "MCD")
(let ((commodity (gnc-commodity-table-lookup
(gnc-commodity-table-get-table
@ -201,26 +206,47 @@
(cadr entry)
(caddr entry))))
(if (and commodity (not (null? commodity)))
;; The commodity is defined in GnuCash.
;; There is an existing GnuCash commodity for this
;; combination of namespace and symbol.
(hash-set! table (car entry) commodity)))))
commlist)
security-list)
table))
(define (qif-import:write-commodities hashtab)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; qif-import:write-securities
;;
;; This procedure writes a mapping QIF security names to
;; GnuCash commodity namespaces and mnemonics (symbols).
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (qif-import:write-securities security-hash security-prefs)
(let ((table '()))
;; For each security that has been paired with an existing
;; GnuCash commodity, create a list containing the QIF name
;; and the commodity's namespace and mnemonic (symbol).
(hash-fold
(lambda (key value p)
;;FIXME: we used to type-check the values, like:
;; (gw:wcp-is-of-type? <gnc:commodity*> value)
(if (and value #t)
(set! table
(cons (list key
(gnc-commodity-get-namespace value)
(gnc-commodity-get-mnemonic value))
table))
(gnc:warn "qif-import:write-commodities:"
" something funny in hash table."))
#f) #f hashtab)
(lambda (key value p)
;;FIXME: we used to type-check the values, like:
;; (gw:wcp-is-of-type? <gnc:commodity*> value)
(if (and value #t)
(set! table (cons (list key
(gnc-commodity-get-namespace value)
(gnc-commodity-get-mnemonic value))
table))
(gnc:warn "qif-import:write-securities:"
" something funny in hash table."))
#f)
#f security-hash)
;; Add on the rest of the saved security mapping preferences.
(for-each
(lambda (m)
(if (not (hash-ref security-hash (car m)))
(set! table (cons m table))))
security-prefs)
;; Write out the mappings.
(write table)))
@ -233,7 +259,8 @@
;; user cancels the import instead.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (qif-import:save-map-prefs acct-map cat-map memo-map stock-map)
(define (qif-import:save-map-prefs acct-map cat-map memo-map
security-map security-prefs)
(let* ((pref-filename (gnc-build-dotgnucash-path "qif-accounts-map")))
;; does the file exist? if not, create it; in either case,
;; make sure it's a directory and we have write and execute
@ -241,26 +268,31 @@
(with-output-to-file pref-filename
(lambda ()
(display ";;; qif-accounts-map\n")
(display ";;; automatically generated by GNUcash. DO NOT EDIT\n")
(display ";;; (unless you really, really want to).\n")
(display ";;; Automatically generated by GnuCash. DO NOT EDIT.\n")
(display ";;; (Unless you really, really want to.)\n")
(display ";;; map from QIF accounts to GNC accounts") (newline)
(display ";;; Map QIF accounts to GnuCash accounts")
(newline)
(qif-import:write-map acct-map)
(newline)
(display ";;; map from QIF categories to GNC accounts") (newline)
(display ";;; Map QIF categories to GnuCash accounts")
(newline)
(qif-import:write-map cat-map)
(newline)
(display ";;; map from QIF payee/memo to GNC accounts") (newline)
(display ";;; Map QIF payee/memo to GnuCash accounts")
(newline)
(qif-import:write-map memo-map)
(newline)
(display ";;; map from QIF stock name to GNC commodity") (newline)
(qif-import:write-commodities stock-map)
(display ";;; Map QIF security names to GnuCash commodities")
(newline)
(qif-import:write-securities security-map security-prefs)
(newline)
(display ";;; GnuCash separator used in these mappings") (newline)
(display ";;; GnuCash separator used in these mappings")
(newline)
(write (string-ref (gnc-get-account-separator-string) 0))
(newline)))))

View File

@ -39,7 +39,7 @@
(export qif-import:get-all-accts)
(export qif-import:fix-from-acct)
(export qif-import:any-new-accts?)
(export qif-import:update-stock-hash)
(export qif-import:update-security-hash)
(export qif-import:refresh-match-selection)
(export qif-import:save-map-prefs)
(export qif-import:load-map-prefs)