diff --git a/src/engine/Account.c b/src/engine/Account.c index 9c387e2371..9048d8559b 100644 --- a/src/engine/Account.c +++ b/src/engine/Account.c @@ -141,8 +141,6 @@ xaccMallocAccount (QofBook *book) Account * xaccCloneAccountSimple(const Account *from, QofBook *book) { - const char * ucom; - const gnc_commodity_table * comtbl; Account *ret; if (!from || !book) return NULL; @@ -166,9 +164,7 @@ xaccCloneAccountSimple(const Account *from, QofBook *book) /* The new book should contain a commodity that matches * the one in the old book. Find it, use it. */ - ucom = gnc_commodity_get_unique_name (from->commodity); - comtbl = gnc_commodity_table_get_table (book); - ret->commodity = gnc_commodity_table_lookup_unique (comtbl, ucom); + ret->commodity = gnc_commodity_obtain_twin (from->commodity, book); ret->commodity_scu = from->commodity_scu; ret->non_standard_scu = from->non_standard_scu; @@ -181,8 +177,6 @@ xaccCloneAccountSimple(const Account *from, QofBook *book) Account * xaccCloneAccount (const Account *from, QofBook *book) { - const char * ucom; - const gnc_commodity_table * comtbl; Account *ret; if (!from || !book) return NULL; @@ -206,9 +200,7 @@ xaccCloneAccount (const Account *from, QofBook *book) /* The new book should contain a commodity that matches * the one in the old book. Find it, use it. */ - ucom = gnc_commodity_get_unique_name (from->commodity); - comtbl = gnc_commodity_table_get_table (book); - ret->commodity = gnc_commodity_table_lookup_unique (comtbl, ucom); + ret->commodity = gnc_commodity_obtain_twin (from->commodity, book); ret->commodity_scu = from->commodity_scu; ret->non_standard_scu = from->non_standard_scu; diff --git a/src/engine/gnc-commodity.c b/src/engine/gnc-commodity.c index dda4c3e6ee..de7c5ce9e5 100644 --- a/src/engine/gnc-commodity.c +++ b/src/engine/gnc-commodity.c @@ -932,6 +932,27 @@ gnc_commodity_table_set_table(QofBook *book, gnc_commodity_table *ct) gnc_commodity_table_destroy (old_ct); } +gnc_commodity * +gnc_commodity_obtain_twin (gnc_commodity *from, QofBook *book) +{ + gnc_commodity *twin; + const char * ucom; + gnc_commodity_table * comtbl; + + if (!from) return NULL; + comtbl = gnc_commodity_table_get_table (book); + if (!comtbl) return NULL; + + ucom = gnc_commodity_get_unique_name (from); + twin = gnc_commodity_table_lookup_unique (comtbl, ucom); + if (!twin) + { + twin = gnc_commodity_clone (from); + twin = gnc_commodity_table_insert (comtbl, twin); + } + return twin; +} + /******************************************************************** * gnc_commodity_get_size * get the size of the commodity table diff --git a/src/engine/gnc-commodity.h b/src/engine/gnc-commodity.h index 87fd31cd46..447579a660 100644 --- a/src/engine/gnc-commodity.h +++ b/src/engine/gnc-commodity.h @@ -596,7 +596,7 @@ gnc_commodity * gnc_commodity_table_find_full(const gnc_commodity_table * t, * nothing), or another entries has the same namespace and mnemonic * (updates the existing entry). * - * @param table A pointer to the commodity table for the book. + * @param table A pointer to the commodity table * * @param comm A pointer to the commodity to add. * @@ -611,7 +611,7 @@ gnc_commodity * gnc_commodity_table_insert(gnc_commodity_table * table, /** Remove a commodity from the commodity table. If the commodity to * remove doesn't exist, nothing happens. * - * @param table A pointer to the commodity table for the book. + * @param table A pointer to the commodity table * * @param comm A pointer to the commodity to remove. */ void gnc_commodity_table_remove(gnc_commodity_table * table, @@ -622,7 +622,7 @@ void gnc_commodity_table_remove(gnc_commodity_table * table, * etc. It also adds all of the ISO 4217 currencies to the commodity * table. * - * @param table A pointer to the commodity table for the book. */ + * @param table A pointer to the commodity table */ gboolean gnc_commodity_table_add_default_data(gnc_commodity_table *table); /** @} */ @@ -639,8 +639,7 @@ guint gnc_commodity_table_get_number_of_namespaces(gnc_commodity_table* tbl); /** Test to see if the indicated namespace exits in the commodity table. * - * @param table A pointer to the commodity table for the current - * book. + * @param table A pointer to the commodity table * * @param namespace The new namespace to check. * @@ -661,8 +660,7 @@ GList * gnc_commodity_table_get_namespaces(const gnc_commodity_table * t); /** This function adds a new string to the list of commodity namespaces. * If the new namespace already exists, nothing happens. * - * @param table A pointer to the commodity table for the current - * book. + * @param table A pointer to the commodity table * * @param namespace The new namespace to be added.*/ void gnc_commodity_table_add_namespace(gnc_commodity_table * table, @@ -671,8 +669,7 @@ void gnc_commodity_table_add_namespace(gnc_commodity_table * table, /** This function deletes a string from the list of commodity namespaces. * If the namespace does not exist, nothing happens. * - * @param table A pointer to the commodity table for the current - * book. + * @param table A pointer to the commodity table * * @param namespace The namespace to be deleted. * @@ -687,8 +684,7 @@ void gnc_commodity_table_delete_namespace(gnc_commodity_table * t, /** Returns the number of commodities in the commodity table. * - * @param table A pointer to the commodity table for the current - * book. + * @param table A pointer to the commodity table * * @return The number of commodities in the table. 0 if there are no * commodities, or the routine was passed a bad argument. */ @@ -713,8 +709,7 @@ GList * gnc_commodity_table_get_commodities(const gnc_commodity_table * t, * field has been set. All matching commodities are queued onto a * list, and the head of that list is returned. * - * @param table A pointer to the commodity table for the current - * book. + * @param table A pointer to the commodity table * * @param expression Use the given expression as a filter on the * commodities to be returned. If non-null, only commodities in @@ -733,8 +728,7 @@ GList * gnc_commodity_table_get_quotable_commodities(const gnc_commodity_table * * This table walk returns whenever the end of the table is reached, * or the function returns FALSE. * - * @param table A pointer to the commodity table for the current - * book. + * @param table A pointer to the commodity table * * @param f The function to call for each commodity. * @@ -772,6 +766,13 @@ void gnc_commodity_table_destroy(gnc_commodity_table * table); */ void gnc_commodity_table_set_table(QofBook *book, gnc_commodity_table *ct); +/** Given the commodity 'from', this routine will find and return the + * equivalent commodity (commodity with the same 'unique name') in + * the indicated book. This routine is primarily useful for setting + * up clones of things across multiple books. + */ +gnc_commodity * gnc_commodity_obtain_twin (gnc_commodity *from, QofBook *book); + /** You should probably not be using gnc_commodity_table_register() * It is an internal routine for registering the gncObject for the * commodity table.