2001-08-07 18:36:04 -05:00
|
|
|
/********************************************************************
|
|
|
|
* gnc-commodity.c -- api for tradable commodities (incl. currency) *
|
|
|
|
* Copyright (C) 2000 Bill Gribble *
|
2003-06-10 17:22:47 -05:00
|
|
|
* Copyright (C) 2001,2003 Linas Vepstas <linas@linas.org> *
|
2006-01-23 00:38:20 -06:00
|
|
|
* Copyright (c) 2006 David Hampton <hampton@employees.org> *
|
2001-08-07 18:36:04 -05:00
|
|
|
* *
|
|
|
|
* 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 *
|
2005-11-16 23:35:02 -06:00
|
|
|
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
|
|
|
|
* Boston, MA 02110-1301, USA gnu@gnu.org *
|
2001-08-07 18:36:04 -05:00
|
|
|
* *
|
|
|
|
*******************************************************************/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2005-11-19 17:53:34 -06:00
|
|
|
#include <glib.h>
|
|
|
|
#include <glib/gi18n.h>
|
2003-07-01 22:35:20 -05:00
|
|
|
#include <ctype.h>
|
2001-08-07 18:36:04 -05:00
|
|
|
#include <limits.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2003-07-26 03:08:20 -05:00
|
|
|
#include <stdlib.h>
|
2003-07-03 17:40:18 -05:00
|
|
|
#include <regex.h>
|
2001-08-07 18:36:04 -05:00
|
|
|
|
|
|
|
#include "gnc-commodity.h"
|
2006-01-20 21:50:25 -06:00
|
|
|
#include "gnc-main.h"
|
2001-08-07 18:36:04 -05:00
|
|
|
|
2005-11-01 21:32:36 -06:00
|
|
|
static QofLogModule log_module = GNC_MOD_COMMODITY;
|
2001-08-07 18:36:04 -05:00
|
|
|
|
2003-06-10 17:22:47 -05:00
|
|
|
/* Parts per unit is nominal, i.e. number of 'partname' units in
|
2001-08-07 18:36:04 -05:00
|
|
|
* a 'unitname' unit. fraction is transactional, i.e. how many
|
|
|
|
* of the smallest-transactional-units of the currency are there
|
|
|
|
* in a 'unitname' unit. */
|
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
enum {
|
|
|
|
PROP_0,
|
|
|
|
PROP_NAMESPACE,
|
|
|
|
PROP_FULL_NAME,
|
|
|
|
PROP_MNEMONIC,
|
|
|
|
PROP_PRINTNAME,
|
|
|
|
PROP_CUSIP,
|
|
|
|
PROP_FRACTION,
|
|
|
|
PROP_UNIQUE_NAME,
|
|
|
|
PROP_QUOTE_FLAG,
|
|
|
|
PROP_QUOTE_SOURCE,
|
|
|
|
PROP_QUOTE_TZ,
|
|
|
|
};
|
|
|
|
|
2003-06-10 17:22:47 -05:00
|
|
|
struct gnc_commodity_s
|
|
|
|
{
|
2005-11-01 21:32:36 -06:00
|
|
|
QofInstance inst;
|
2008-08-01 11:02:07 -05:00
|
|
|
};
|
2005-11-01 21:32:36 -06:00
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
typedef struct CommodityPrivate
|
|
|
|
{
|
2005-11-01 21:32:36 -06:00
|
|
|
gnc_commodity_namespace *namespace;
|
|
|
|
|
2002-01-01 22:28:32 -06:00
|
|
|
char * fullname;
|
|
|
|
char * mnemonic;
|
|
|
|
char * printname;
|
2006-02-11 14:00:52 -06:00
|
|
|
char * cusip; /* CUSIP or other identifying code */
|
2002-01-01 22:28:32 -06:00
|
|
|
int fraction;
|
|
|
|
char * unique_name;
|
|
|
|
gint16 mark; /* user-defined mark, handy for traversals */
|
2003-05-10 19:45:03 -05:00
|
|
|
|
|
|
|
gboolean quote_flag; /* user wants price quotes */
|
2003-07-01 22:35:20 -05:00
|
|
|
gnc_quote_source * quote_source; /* current/old source of quotes */
|
2003-05-10 19:45:03 -05:00
|
|
|
char * quote_tz;
|
2007-12-25 23:17:37 -06:00
|
|
|
|
|
|
|
/* the number of accounts using this commodity - this field is not
|
|
|
|
* persisted */
|
|
|
|
int usage_count;
|
2008-08-01 11:02:07 -05:00
|
|
|
} CommodityPrivate;
|
|
|
|
|
|
|
|
#define GET_PRIVATE(o) \
|
|
|
|
(G_TYPE_INSTANCE_GET_PRIVATE((o), GNC_TYPE_COMMODITY, CommodityPrivate))
|
2001-08-07 18:36:04 -05:00
|
|
|
|
2007-04-04 19:24:18 -05:00
|
|
|
struct _GncCommodityClass
|
|
|
|
{
|
|
|
|
QofInstanceClass parent_class;
|
|
|
|
};
|
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
static void commodity_free(gnc_commodity * cm);
|
|
|
|
|
2003-08-10 22:06:17 -05:00
|
|
|
struct gnc_commodity_namespace_s
|
|
|
|
{
|
2005-11-01 21:32:36 -06:00
|
|
|
QofInstance inst;
|
|
|
|
|
2006-04-03 16:39:25 -05:00
|
|
|
gchar * name;
|
|
|
|
gboolean iso4217;
|
2005-11-01 21:32:36 -06:00
|
|
|
GHashTable * cm_table;
|
|
|
|
GList * cm_list;
|
2001-08-07 18:36:04 -05:00
|
|
|
};
|
|
|
|
|
2007-04-04 19:24:18 -05:00
|
|
|
struct _GncCommodityNamespaceClass
|
|
|
|
{
|
|
|
|
QofInstanceClass parent_class;
|
|
|
|
};
|
|
|
|
|
2003-08-10 22:06:17 -05:00
|
|
|
struct gnc_commodity_table_s
|
|
|
|
{
|
2005-11-01 21:32:36 -06:00
|
|
|
GHashTable * ns_table;
|
|
|
|
GList * ns_list;
|
2001-08-07 18:36:04 -05:00
|
|
|
};
|
|
|
|
|
2003-08-10 22:06:17 -05:00
|
|
|
struct gnc_new_iso_code
|
|
|
|
{
|
2003-05-29 18:47:52 -05:00
|
|
|
const char *old_code;
|
|
|
|
const char *new_code;
|
|
|
|
} gnc_new_iso_codes[] = {
|
2007-01-23 08:24:06 -06:00
|
|
|
{"RUR", "RUB"}, /* Russian Ruble: RUR through 1997-12, RUB from 1998-01 onwards; see bug #393185 */
|
2004-11-10 15:17:50 -06:00
|
|
|
{"PLZ", "PLN"}, /* Polish Zloty */
|
|
|
|
{"UAG", "UAH"}, /* Ukraine Hryvnia */
|
2008-10-26 16:38:10 -05:00
|
|
|
{"NIS", "ILS"}, /* New Israeli Shekel: The informal abbreviation may be "NIS", but
|
|
|
|
its iso-4217 is clearly ILS and only this! Incorrectly changed
|
|
|
|
due to bug#152755 (Nov 2004) and changed back again by bug#492417
|
|
|
|
(Oct 2008). */
|
2006-06-16 20:00:18 -05:00
|
|
|
{"MXP", "MXN"}, /* Mexican (Nuevo) Peso */
|
2007-02-15 14:22:49 -06:00
|
|
|
{"TRL", "TRY"}, /* New Turkish Lira: changed 2005 */
|
2005-11-01 21:32:36 -06:00
|
|
|
|
|
|
|
/* Only add currencies to this table when the old currency no longer
|
|
|
|
* exists in the file iso-4217-currencies.scm */
|
2003-05-29 18:47:52 -05:00
|
|
|
};
|
|
|
|
#define GNC_NEW_ISO_CODES \
|
|
|
|
(sizeof(gnc_new_iso_codes) / sizeof(struct gnc_new_iso_code))
|
2003-07-01 22:35:20 -05:00
|
|
|
|
|
|
|
static gboolean fq_is_installed = FALSE;
|
|
|
|
|
|
|
|
struct gnc_quote_source_s {
|
|
|
|
gboolean supported;
|
|
|
|
QuoteSourceType type;
|
|
|
|
gint index;
|
|
|
|
char *user_name; /* User friendly name */
|
|
|
|
char *old_internal_name; /* Name used internally (deprecated) */
|
|
|
|
char *internal_name; /* Name used internally and by finance::quote. */
|
|
|
|
};
|
|
|
|
|
|
|
|
static gnc_quote_source currency_quote_source =
|
2006-03-31 23:45:07 -06:00
|
|
|
{ TRUE, 0, 0, "Currency", "CURRENCY", "currency" };
|
2003-07-01 22:35:20 -05:00
|
|
|
|
|
|
|
static gnc_quote_source single_quote_sources[] = {
|
|
|
|
{ FALSE, 0, 0, "AEX", "AEX", "aex" },
|
2006-02-09 20:13:21 -06:00
|
|
|
{ FALSE, 0, 0, "AEX Futures", "AEX_FUTURES", "aex_futures" },
|
|
|
|
{ FALSE, 0, 0, "AEX Options", "AEX_OPTIONS", "aex_options" },
|
2004-05-08 00:27:26 -05:00
|
|
|
{ FALSE, 0, 0, "AMFI India", "AMFIINDIA", "amfiindia" },
|
2006-02-09 20:13:21 -06:00
|
|
|
{ FALSE, 0, 0, "ASE", "ASEGR", "asegr" },
|
2003-07-01 22:35:20 -05:00
|
|
|
{ FALSE, 0, 0, "ASX", "ASX", "asx" },
|
2006-02-09 20:13:21 -06:00
|
|
|
{ FALSE, 0, 0, "BMO NesbittBurns", "BMONESBITTBURNS", "bmonesbittburns" },
|
|
|
|
{ FALSE, 0, 0, "Deka Investments", "DEKA", "deka" },
|
2003-07-01 22:35:20 -05:00
|
|
|
{ FALSE, 0, 0, "DWS", "DWS", "dwsfunds" },
|
|
|
|
{ FALSE, 0, 0, "Fidelity Direct", "FIDELITY_DIRECT", "fidelity_direct" },
|
2006-02-09 20:13:21 -06:00
|
|
|
{ FALSE, 0, 0, "Finance Canada", "FINANCECANADA", "financecanada" },
|
|
|
|
{ FALSE, 0, 0, "First Trust Portfolios", "FTPORTFOLIOS_DIRECT", "ftportfolios_direct" },
|
2003-07-01 22:35:20 -05:00
|
|
|
{ FALSE, 0, 0, "Fund Library", "FUNDLIBRARY", "fundlibrary" },
|
2006-02-09 20:13:21 -06:00
|
|
|
{ FALSE, 0, 0, "Man Investments", "maninv", "maninv" },
|
|
|
|
{ FALSE, 0, 0, "Motley Fool", "FOOL", "fool" },
|
|
|
|
{ FALSE, 0, 0, "NZX", "NZX", "nzx" },
|
|
|
|
{ FALSE, 0, 0, "Platinum Asset Management", "PLATINUM", "platinum" },
|
|
|
|
{ FALSE, 0, 0, "SEB", "SEB_FUNDS", "seb_funds" },
|
|
|
|
{ FALSE, 0, 0, "Sharenet", "sharenet", "sharenet" },
|
2003-07-01 22:35:20 -05:00
|
|
|
{ FALSE, 0, 0, "TD Waterhouse Canada", "TDWATERHOUSE", "tdwaterhouse" },
|
2004-07-02 21:20:33 -05:00
|
|
|
{ FALSE, 0, 0, "TD Efunds", "TDEFUNDS", "tdefunds" },
|
2003-07-01 22:35:20 -05:00
|
|
|
{ FALSE, 0, 0, "TIAA-CREF", "TIAACREF", "tiaacref" },
|
|
|
|
{ FALSE, 0, 0, "T. Rowe Price", "TRPRICE_DIRECT", "troweprice_direct" },
|
|
|
|
{ FALSE, 0, 0, "Trustnet", "TRUSTNET", "trustnet" },
|
|
|
|
{ FALSE, 0, 0, "Union Investments", "UNIONFUNDS", "unionfunds" },
|
2006-02-09 20:13:21 -06:00
|
|
|
{ FALSE, 0, 0, "US Treasury Bonds", "usfedbonds", "usfedbonds" },
|
|
|
|
{ FALSE, 0, 0, "US Govt. Thrift Savings Plan", "TSP", "tsp" },
|
2003-07-01 22:35:20 -05:00
|
|
|
{ FALSE, 0, 0, "Vanguard", "VANGUARD", "vanguard" },
|
|
|
|
{ FALSE, 0, 0, "VWD", "VWD", "vwd" },
|
|
|
|
{ FALSE, 0, 0, "Yahoo", "YAHOO", "yahoo" },
|
|
|
|
{ FALSE, 0, 0, "Yahoo Asia", "YAHOO_ASIA", "yahoo_asia" },
|
|
|
|
{ FALSE, 0, 0, "Yahoo Australia", "YAHOO_AUSTRALIA", "yahoo_australia" },
|
2006-02-09 20:13:21 -06:00
|
|
|
{ FALSE, 0, 0, "Yahoo Brasil", "YAHOO_BRASIL", "yahoo_brasil" },
|
2003-07-01 22:35:20 -05:00
|
|
|
{ FALSE, 0, 0, "Yahoo Europe", "YAHOO_EUROPE", "yahoo_europe" },
|
2006-02-09 20:13:21 -06:00
|
|
|
{ FALSE, 0, 0, "Yahoo New Zealand", "YAHOO_NZ", "yahoo_nz" },
|
|
|
|
{ FALSE, 0, 0, "Zuerich Investments", "ZIFUNDS", "zifunds" }, /* Removed from F::Q 1.11. */
|
2003-07-01 22:35:20 -05:00
|
|
|
};
|
|
|
|
static gnc_quote_source multiple_quote_sources[] = {
|
|
|
|
{ FALSE, 0, 0, "Asia (Yahoo, ...)", "ASIA", "asia" },
|
|
|
|
{ FALSE, 0, 0, "Australia (ASX, Yahoo, ...)", "AUSTRALIA", "australia" },
|
2006-02-09 20:13:21 -06:00
|
|
|
{ FALSE, 0, 0, "Brasil (Yahoo, ...)", "BRASIL", "brasil" },
|
2003-07-01 22:35:20 -05:00
|
|
|
{ FALSE, 0, 0, "Canada (Yahoo, ...)", "CANADA", "canada" },
|
|
|
|
{ FALSE, 0, 0, "Canada Mutual (Fund Library, ...)", "CANADAMUTUAL", "canadamutual" },
|
|
|
|
{ FALSE, 0, 0, "Dutch (AEX, ...)", "DUTCH", "dutch" },
|
|
|
|
{ FALSE, 0, 0, "Europe (Yahoo, ...)", "EUROPE", "europe" },
|
2006-02-09 20:13:21 -06:00
|
|
|
{ FALSE, 0, 0, "Greece (ASE, ...)", "GREECE", "greece" },
|
2004-05-08 00:27:26 -05:00
|
|
|
{ FALSE, 0, 0, "India Mutual (AMFI, ...)", "INDIAMUTUAL", "indiamutual" },
|
2003-07-01 22:35:20 -05:00
|
|
|
{ FALSE, 0, 0, "Fidelity (Fidelity, ...)", "FIDELITY", "fidelity" },
|
2006-02-09 20:13:21 -06:00
|
|
|
{ FALSE, 0, 0, "First Trust (First Trust, ...)", "FTPORTFOLIOS", "ftportfolios" },
|
2003-07-01 22:35:20 -05:00
|
|
|
{ FALSE, 0, 0, "Nasdaq (Yahoo, ...)", "NASDAQ", "nasdaq" },
|
2006-02-09 20:13:21 -06:00
|
|
|
{ FALSE, 0, 0, "New Zealand (Yahoo, ...)", "NZ", "nz" },
|
2003-07-01 22:35:20 -05:00
|
|
|
{ FALSE, 0, 0, "NYSE (Yahoo, ...)", "NYSE", "nyse" },
|
2006-02-09 20:13:21 -06:00
|
|
|
{ FALSE, 0, 0, "South Africa (Sharenet, ...)", "ZA", "za" },
|
2003-07-01 22:35:20 -05:00
|
|
|
{ FALSE, 0, 0, "T. Rowe Price", "TRPRICE", "troweprice" },
|
|
|
|
{ FALSE, 0, 0, "U.K. Unit Trusts", "UKUNITTRUSTS", "uk_unit_trusts" },
|
|
|
|
{ FALSE, 0, 0, "USA (Yahoo, Fool ...)", "USA", "usa" },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const int num_single_quote_sources =
|
|
|
|
sizeof(single_quote_sources) / sizeof(gnc_quote_source);
|
|
|
|
static const int num_multiple_quote_sources =
|
|
|
|
sizeof(multiple_quote_sources) / sizeof(gnc_quote_source);
|
|
|
|
static GList *new_quote_sources = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_quote_source_fq_installed
|
|
|
|
*
|
|
|
|
* This function indicates whether or not the Finance::Quote module
|
|
|
|
* is installed on a users computer.
|
|
|
|
********************************************************************/
|
|
|
|
gboolean
|
|
|
|
gnc_quote_source_fq_installed (void)
|
|
|
|
{
|
|
|
|
return fq_is_installed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_quote_source_num_entries
|
|
|
|
*
|
|
|
|
* Return the number of entries for a given type of price source.
|
|
|
|
********************************************************************/
|
|
|
|
gint gnc_quote_source_num_entries(QuoteSourceType type)
|
|
|
|
{
|
|
|
|
if (type == SOURCE_CURRENCY)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (type == SOURCE_SINGLE)
|
|
|
|
return num_single_quote_sources;
|
2003-05-29 18:47:52 -05:00
|
|
|
|
2003-07-01 22:35:20 -05:00
|
|
|
if (type == SOURCE_MULTI)
|
|
|
|
return num_multiple_quote_sources;
|
|
|
|
|
|
|
|
return g_list_length(new_quote_sources);
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_quote_source_init_tables
|
|
|
|
*
|
|
|
|
* Update the type/index values for prices sources.
|
|
|
|
********************************************************************/
|
|
|
|
static void
|
|
|
|
gnc_quote_source_init_tables (void)
|
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
for (i = 0; i < num_single_quote_sources; i++) {
|
|
|
|
single_quote_sources[i].type = SOURCE_SINGLE;
|
|
|
|
single_quote_sources[i].index = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < num_multiple_quote_sources; i++) {
|
|
|
|
multiple_quote_sources[i].type = SOURCE_MULTI;
|
|
|
|
multiple_quote_sources[i].index = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
currency_quote_source.type = SOURCE_CURRENCY;
|
|
|
|
currency_quote_source.index = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_quote_source_add_new
|
|
|
|
*
|
|
|
|
* Add a new price source. Called when unknown source names are found
|
|
|
|
* either in the F::Q installation (a newly available source) or in
|
|
|
|
* the user's data file (a source that has vanished but needs to be
|
|
|
|
* tracked.)
|
|
|
|
********************************************************************/
|
|
|
|
gnc_quote_source *
|
|
|
|
gnc_quote_source_add_new (const char *source_name, gboolean supported)
|
|
|
|
{
|
|
|
|
gnc_quote_source *new_source;
|
|
|
|
|
2007-02-09 11:35:00 -06:00
|
|
|
DEBUG("Creating new source %s", (source_name == NULL ? "(null)" : source_name));
|
2003-07-01 22:35:20 -05:00
|
|
|
new_source = malloc(sizeof(gnc_quote_source));
|
|
|
|
new_source->supported = supported;
|
|
|
|
new_source->type = SOURCE_UNKNOWN;
|
|
|
|
new_source->index = g_list_length(new_quote_sources);
|
|
|
|
|
|
|
|
/* This name can be changed if/when support for this price source is
|
|
|
|
* integrated into gnucash. */
|
|
|
|
new_source->user_name = strdup(source_name);
|
|
|
|
|
|
|
|
/* This name is permanent and must be kept the same if/when support
|
|
|
|
* for this price source is integrated into gnucash (i.e. for a
|
|
|
|
* nice user name). */
|
|
|
|
new_source->old_internal_name = strdup(source_name);
|
|
|
|
new_source->internal_name = strdup(source_name);
|
|
|
|
new_quote_sources = g_list_append(new_quote_sources, new_source);
|
|
|
|
return new_source;
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_quote_source_lookup_by_xxx
|
|
|
|
*
|
|
|
|
* Lookup a price source data structure based upon various criteria.
|
|
|
|
********************************************************************/
|
|
|
|
gnc_quote_source *
|
|
|
|
gnc_quote_source_lookup_by_ti (QuoteSourceType type, gint index)
|
|
|
|
{
|
|
|
|
gnc_quote_source *source;
|
|
|
|
GList *node;
|
|
|
|
|
|
|
|
ENTER("type/index is %d/%d", type, index);
|
|
|
|
switch (type) {
|
|
|
|
case SOURCE_CURRENCY:
|
|
|
|
LEAVE("found %s", currency_quote_source.user_name);
|
|
|
|
return ¤cy_quote_source;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SOURCE_SINGLE:
|
|
|
|
if (index < num_single_quote_sources) {
|
|
|
|
LEAVE("found %s", single_quote_sources[index].user_name);
|
|
|
|
return &single_quote_sources[index];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SOURCE_MULTI:
|
|
|
|
if (index < num_multiple_quote_sources) {
|
|
|
|
LEAVE("found %s", multiple_quote_sources[index].user_name);
|
|
|
|
return &multiple_quote_sources[index];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SOURCE_UNKNOWN:
|
|
|
|
default:
|
|
|
|
node = g_list_nth(new_quote_sources, index);
|
|
|
|
if (node) {
|
|
|
|
source = node->data;
|
|
|
|
LEAVE("found %s", source->user_name);
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LEAVE("not found");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
gnc_quote_source *
|
|
|
|
gnc_quote_source_lookup_by_internal(const char * name)
|
|
|
|
{
|
|
|
|
gnc_quote_source *source;
|
|
|
|
GList *node;
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
if ((name == NULL) || (safe_strcmp(name, "") == 0)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (safe_strcmp(name, currency_quote_source.internal_name) == 0)
|
|
|
|
return ¤cy_quote_source;
|
|
|
|
if (safe_strcmp(name, currency_quote_source.old_internal_name) == 0)
|
|
|
|
return ¤cy_quote_source;
|
|
|
|
|
|
|
|
for (i = 0; i < num_single_quote_sources; i++) {
|
|
|
|
if (safe_strcmp(name, single_quote_sources[i].internal_name) == 0)
|
|
|
|
return &single_quote_sources[i];
|
|
|
|
if (safe_strcmp(name, single_quote_sources[i].old_internal_name) == 0)
|
|
|
|
return &single_quote_sources[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < num_multiple_quote_sources; i++) {
|
|
|
|
if (safe_strcmp(name, multiple_quote_sources[i].internal_name) == 0)
|
|
|
|
return &multiple_quote_sources[i];
|
|
|
|
if (safe_strcmp(name, multiple_quote_sources[i].old_internal_name) == 0)
|
|
|
|
return &multiple_quote_sources[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0, node = new_quote_sources; node; node = node->next, i++) {
|
|
|
|
source = node->data;
|
|
|
|
if (safe_strcmp(name, source->internal_name) == 0)
|
|
|
|
return source;
|
|
|
|
if (safe_strcmp(name, source->old_internal_name) == 0)
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
|
|
|
|
LEAVE("Unknown source %s", name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_quote_source_get_xxx
|
|
|
|
*
|
|
|
|
* Accessor functions - get functions only. There are no set functions.
|
|
|
|
********************************************************************/
|
|
|
|
QuoteSourceType
|
2007-12-27 20:27:57 -06:00
|
|
|
gnc_quote_source_get_type (const gnc_quote_source *source)
|
2003-07-01 22:35:20 -05:00
|
|
|
{
|
|
|
|
ENTER("%p", source);
|
|
|
|
if (!source) {
|
|
|
|
LEAVE("bad source");
|
|
|
|
return SOURCE_SINGLE;
|
|
|
|
}
|
|
|
|
|
2007-11-24 09:38:19 -06:00
|
|
|
LEAVE("type is %d",source->type);
|
2003-07-01 22:35:20 -05:00
|
|
|
return source->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
gint
|
2007-12-27 20:27:57 -06:00
|
|
|
gnc_quote_source_get_index (const gnc_quote_source *source)
|
2003-07-01 22:35:20 -05:00
|
|
|
{
|
|
|
|
ENTER("%p", source);
|
|
|
|
if (!source) {
|
|
|
|
LEAVE("bad source");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-11-24 09:38:19 -06:00
|
|
|
LEAVE("index is %d", source->index);
|
2003-07-01 22:35:20 -05:00
|
|
|
return source->index;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
2007-12-27 20:27:57 -06:00
|
|
|
gnc_quote_source_get_supported (const gnc_quote_source *source)
|
2003-07-01 22:35:20 -05:00
|
|
|
{
|
|
|
|
ENTER("%p", source);
|
|
|
|
if (!source) {
|
|
|
|
LEAVE("bad source");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2007-11-24 09:38:19 -06:00
|
|
|
LEAVE("%ssupported", source && source->supported ? "" : "not ");
|
2003-07-01 22:35:20 -05:00
|
|
|
return source->supported;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2007-12-27 20:27:57 -06:00
|
|
|
gnc_quote_source_get_user_name (const gnc_quote_source *source)
|
2003-07-01 22:35:20 -05:00
|
|
|
{
|
|
|
|
ENTER("%p", source);
|
|
|
|
if (!source) {
|
|
|
|
LEAVE("bad source");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
LEAVE("user name %s", source->user_name);
|
|
|
|
return source->user_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2007-12-27 20:27:57 -06:00
|
|
|
gnc_quote_source_get_old_internal_name (const gnc_quote_source *source)
|
2003-07-01 22:35:20 -05:00
|
|
|
{
|
|
|
|
ENTER("%p", source);
|
|
|
|
if (!source) {
|
|
|
|
LEAVE("bad source");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
LEAVE("old internal name %s", source->old_internal_name);
|
|
|
|
return source->old_internal_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2007-12-27 20:27:57 -06:00
|
|
|
gnc_quote_source_get_internal_name (const gnc_quote_source *source)
|
2003-07-01 22:35:20 -05:00
|
|
|
{
|
|
|
|
ENTER("%p", source);
|
|
|
|
if (!source) {
|
|
|
|
LEAVE("bad source");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
LEAVE("internal name %s", source->internal_name);
|
|
|
|
return source->internal_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_quote_source_set_fq_installed
|
|
|
|
*
|
|
|
|
* Update gnucash internal tables on what Finance::Quote sources are
|
|
|
|
* installed.
|
|
|
|
********************************************************************/
|
|
|
|
void
|
2007-12-27 20:27:57 -06:00
|
|
|
gnc_quote_source_set_fq_installed (const GList *sources_list)
|
2003-07-01 22:35:20 -05:00
|
|
|
{
|
|
|
|
gnc_quote_source *source;
|
|
|
|
char *source_name;
|
2007-12-27 20:27:57 -06:00
|
|
|
const GList *node;
|
2003-07-01 22:35:20 -05:00
|
|
|
|
|
|
|
ENTER(" ");
|
|
|
|
fq_is_installed = TRUE;
|
|
|
|
|
|
|
|
if (!sources_list)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (node = sources_list; node; node = node->next) {
|
|
|
|
source_name = node->data;
|
|
|
|
|
|
|
|
source = gnc_quote_source_lookup_by_internal(source_name);
|
|
|
|
if (source != NULL) {
|
|
|
|
DEBUG("Found source %s: %s", source_name, source->user_name);
|
|
|
|
source->supported = TRUE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
gnc_quote_source_add_new(source_name, TRUE);
|
|
|
|
}
|
|
|
|
LEAVE(" ");
|
|
|
|
}
|
|
|
|
|
2006-05-06 01:14:21 -05:00
|
|
|
/********************************************************************
|
|
|
|
* QoF Helpers
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
void
|
|
|
|
gnc_commodity_begin_edit (gnc_commodity *cm)
|
|
|
|
{
|
|
|
|
qof_begin_edit(&cm->inst);
|
|
|
|
}
|
|
|
|
|
2006-11-29 09:13:02 -06:00
|
|
|
static void commit_err (QofInstance *inst, QofBackendError errcode)
|
2006-05-06 01:14:21 -05:00
|
|
|
{
|
|
|
|
PERR ("Failed to commit: %d", errcode);
|
2008-10-27 18:42:01 -05:00
|
|
|
gnc_engine_signal_commit_error( errcode );
|
2006-05-06 01:14:21 -05:00
|
|
|
}
|
|
|
|
|
2006-11-29 09:13:02 -06:00
|
|
|
static void noop (QofInstance *inst) {}
|
2006-05-06 01:14:21 -05:00
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
static void
|
|
|
|
comm_free(QofInstance* inst)
|
|
|
|
{
|
|
|
|
commodity_free( GNC_COMMODITY(inst) );
|
|
|
|
}
|
|
|
|
|
2006-05-06 01:14:21 -05:00
|
|
|
void
|
|
|
|
gnc_commodity_commit_edit (gnc_commodity *cm)
|
|
|
|
{
|
|
|
|
if (!qof_commit_edit (QOF_INSTANCE(cm))) return;
|
2008-08-01 11:02:07 -05:00
|
|
|
qof_commit_edit_part2 (&cm->inst, commit_err, noop, comm_free);
|
2006-05-06 01:14:21 -05:00
|
|
|
}
|
|
|
|
|
2001-08-07 18:36:04 -05:00
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_new
|
|
|
|
********************************************************************/
|
|
|
|
|
2005-11-01 21:32:36 -06:00
|
|
|
static void
|
|
|
|
mark_commodity_dirty (gnc_commodity *cm)
|
|
|
|
{
|
2006-05-02 21:28:18 -05:00
|
|
|
qof_instance_set_dirty(&cm->inst);
|
2007-04-01 17:18:50 -05:00
|
|
|
qof_event_gen (&cm->inst, QOF_EVENT_MODIFY, NULL);
|
2005-11-01 21:32:36 -06:00
|
|
|
}
|
2003-08-17 02:13:02 -05:00
|
|
|
|
2001-08-07 18:36:04 -05:00
|
|
|
static void
|
2008-08-01 11:02:07 -05:00
|
|
|
reset_printname(CommodityPrivate *priv)
|
2001-08-07 18:36:04 -05:00
|
|
|
{
|
2008-08-01 11:02:07 -05:00
|
|
|
g_free(priv->printname);
|
|
|
|
priv->printname = g_strdup_printf("%s (%s)",
|
|
|
|
priv->mnemonic ? priv->mnemonic : "",
|
|
|
|
priv->fullname ? priv->fullname : "");
|
2001-08-07 18:36:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-08-01 11:02:07 -05:00
|
|
|
reset_unique_name(CommodityPrivate *priv)
|
2001-08-07 18:36:04 -05:00
|
|
|
{
|
2005-11-01 21:32:36 -06:00
|
|
|
gnc_commodity_namespace *ns;
|
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
g_free(priv->unique_name);
|
|
|
|
ns = priv->namespace;
|
|
|
|
priv->unique_name = g_strdup_printf("%s::%s",
|
2005-11-01 21:32:36 -06:00
|
|
|
ns ? ns->name : "",
|
2008-08-01 11:02:07 -05:00
|
|
|
priv->mnemonic ? priv->mnemonic : "");
|
2001-08-07 18:36:04 -05:00
|
|
|
}
|
|
|
|
|
2007-04-04 19:24:18 -05:00
|
|
|
/* GObject Initialization */
|
2008-08-01 11:02:07 -05:00
|
|
|
G_DEFINE_TYPE(gnc_commodity, gnc_commodity, QOF_TYPE_INSTANCE);
|
2007-04-04 19:24:18 -05:00
|
|
|
|
|
|
|
static void
|
|
|
|
gnc_commodity_init(gnc_commodity* com)
|
|
|
|
{
|
2008-08-01 11:02:07 -05:00
|
|
|
CommodityPrivate* priv;
|
|
|
|
|
|
|
|
priv = GET_PRIVATE(com);
|
|
|
|
|
|
|
|
priv->namespace = NULL;
|
|
|
|
priv->fullname = CACHE_INSERT("");
|
|
|
|
priv->mnemonic = CACHE_INSERT("");
|
|
|
|
priv->cusip = CACHE_INSERT("");
|
|
|
|
priv->fraction = 10000;
|
|
|
|
priv->mark = 0;
|
|
|
|
priv->quote_flag = 0;
|
|
|
|
priv->quote_source = NULL;
|
|
|
|
priv->quote_tz = CACHE_INSERT("");
|
|
|
|
|
|
|
|
reset_printname(priv);
|
|
|
|
reset_unique_name(priv);
|
2007-04-04 19:24:18 -05:00
|
|
|
}
|
|
|
|
|
2007-04-04 22:10:26 -05:00
|
|
|
static void
|
2008-08-01 11:02:07 -05:00
|
|
|
gnc_commodity_dispose(GObject *comp)
|
2007-04-04 22:10:26 -05:00
|
|
|
{
|
2008-08-01 11:02:07 -05:00
|
|
|
G_OBJECT_CLASS(gnc_commodity_parent_class)->dispose(comp);
|
2007-04-04 22:10:26 -05:00
|
|
|
}
|
|
|
|
|
2007-04-04 19:24:18 -05:00
|
|
|
static void
|
2008-08-01 11:02:07 -05:00
|
|
|
gnc_commodity_finalize(GObject* comp)
|
2007-04-04 19:24:18 -05:00
|
|
|
{
|
2008-08-01 11:02:07 -05:00
|
|
|
G_OBJECT_CLASS(gnc_commodity_parent_class)->finalize(comp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gnc_commodity_get_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
gnc_commodity *commodity;
|
|
|
|
CommodityPrivate* priv;
|
|
|
|
|
|
|
|
g_return_if_fail(GNC_IS_COMMODITY(object));
|
|
|
|
|
|
|
|
commodity = GNC_COMMODITY(object);
|
|
|
|
priv = GET_PRIVATE(commodity);
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_NAMESPACE:
|
|
|
|
g_value_set_object(value, priv->namespace);
|
|
|
|
break;
|
|
|
|
case PROP_FULL_NAME:
|
|
|
|
g_value_set_string(value, priv->fullname);
|
|
|
|
break;
|
|
|
|
case PROP_MNEMONIC:
|
|
|
|
g_value_set_string(value, priv->mnemonic);
|
|
|
|
break;
|
|
|
|
case PROP_PRINTNAME:
|
|
|
|
g_value_set_string(value, priv->printname);
|
|
|
|
break;
|
|
|
|
case PROP_CUSIP:
|
|
|
|
g_value_set_string(value, priv->cusip);
|
|
|
|
break;
|
|
|
|
case PROP_FRACTION:
|
|
|
|
g_value_set_int(value, priv->fraction);
|
|
|
|
break;
|
|
|
|
case PROP_UNIQUE_NAME:
|
|
|
|
g_value_set_string(value, priv->unique_name);
|
|
|
|
break;
|
|
|
|
case PROP_QUOTE_FLAG:
|
|
|
|
g_value_set_boolean(value, priv->quote_flag);
|
|
|
|
break;
|
|
|
|
case PROP_QUOTE_SOURCE:
|
|
|
|
g_value_set_pointer(value, priv->quote_source);
|
|
|
|
break;
|
|
|
|
case PROP_QUOTE_TZ:
|
|
|
|
g_value_set_string(value, priv->quote_tz);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gnc_commodity_set_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
gnc_commodity *commodity;
|
|
|
|
gnc_numeric *number;
|
|
|
|
|
|
|
|
g_return_if_fail(GNC_IS_COMMODITY(object));
|
|
|
|
|
|
|
|
commodity = GNC_COMMODITY(object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_NAMESPACE:
|
|
|
|
gnc_commodity_set_namespace(commodity, g_value_get_object(value));
|
|
|
|
break;
|
|
|
|
case PROP_FULL_NAME:
|
|
|
|
gnc_commodity_set_fullname(commodity, g_value_get_string(value));
|
|
|
|
break;
|
|
|
|
case PROP_MNEMONIC:
|
|
|
|
gnc_commodity_set_mnemonic(commodity, g_value_get_string(value));
|
|
|
|
break;
|
|
|
|
case PROP_CUSIP:
|
|
|
|
gnc_commodity_set_cusip(commodity, g_value_get_string(value));
|
|
|
|
break;
|
|
|
|
case PROP_FRACTION:
|
|
|
|
gnc_commodity_set_fraction(commodity, g_value_get_int(value));
|
|
|
|
break;
|
|
|
|
case PROP_QUOTE_FLAG:
|
|
|
|
gnc_commodity_set_quote_flag(commodity, g_value_get_boolean(value));
|
|
|
|
break;
|
|
|
|
case PROP_QUOTE_SOURCE:
|
|
|
|
gnc_commodity_set_quote_source(commodity, g_value_get_pointer(value));
|
|
|
|
break;
|
|
|
|
case PROP_QUOTE_TZ:
|
|
|
|
gnc_commodity_set_quote_tz(commodity, g_value_get_string(value));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static void
|
|
|
|
gnc_commodity_class_init(struct _GncCommodityClass* klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
|
|
|
|
|
|
|
|
gobject_class->dispose = gnc_commodity_dispose;
|
|
|
|
gobject_class->finalize = gnc_commodity_finalize;
|
|
|
|
gobject_class->set_property = gnc_commodity_set_property;
|
|
|
|
gobject_class->get_property = gnc_commodity_get_property;
|
|
|
|
|
|
|
|
g_type_class_add_private(klass, sizeof(CommodityPrivate));
|
|
|
|
|
|
|
|
g_object_class_install_property(gobject_class,
|
|
|
|
PROP_NAMESPACE,
|
|
|
|
g_param_spec_object ("namespace",
|
|
|
|
"Namespace",
|
|
|
|
"The namespace field denotes the "
|
|
|
|
"namespace for this commodity, either "
|
|
|
|
"a currency or symbol from a quote source.",
|
|
|
|
GNC_TYPE_COMMODITY_NAMESPACE,
|
|
|
|
G_PARAM_READWRITE));
|
|
|
|
g_object_class_install_property(gobject_class,
|
|
|
|
PROP_FULL_NAME,
|
|
|
|
g_param_spec_string ("fullname",
|
|
|
|
"Full Commodity Name",
|
|
|
|
"The fullname is the official full name of"
|
|
|
|
"the currency.",
|
|
|
|
NULL,
|
|
|
|
G_PARAM_READWRITE));
|
|
|
|
g_object_class_install_property(gobject_class,
|
|
|
|
PROP_MNEMONIC,
|
|
|
|
g_param_spec_string ("mnemonic",
|
|
|
|
"Commodity Mnemonic",
|
|
|
|
"The mnemonic is the official abbreviated"
|
|
|
|
"designation for the currency.",
|
|
|
|
NULL,
|
|
|
|
G_PARAM_READWRITE));
|
|
|
|
g_object_class_install_property(gobject_class,
|
|
|
|
PROP_PRINTNAME,
|
|
|
|
g_param_spec_string ("printname",
|
|
|
|
"Commodity Print Name",
|
|
|
|
"Printable form of the commodity name.",
|
|
|
|
NULL,
|
|
|
|
G_PARAM_READABLE));
|
|
|
|
g_object_class_install_property(gobject_class,
|
|
|
|
PROP_CUSIP,
|
|
|
|
g_param_spec_string ("cusip",
|
|
|
|
"Commodity CUSIP Code",
|
|
|
|
"?????",
|
|
|
|
NULL,
|
|
|
|
G_PARAM_READWRITE));
|
|
|
|
g_object_class_install_property(gobject_class,
|
|
|
|
PROP_FRACTION,
|
|
|
|
g_param_spec_int ("fraction",
|
|
|
|
"Fraction",
|
|
|
|
"The fraction is the number of sub-units that "
|
|
|
|
"the basic commodity can be divided into.",
|
|
|
|
1,
|
|
|
|
1000000,
|
|
|
|
1,
|
|
|
|
G_PARAM_READWRITE));
|
|
|
|
g_object_class_install_property(gobject_class,
|
|
|
|
PROP_UNIQUE_NAME,
|
|
|
|
g_param_spec_string ("unique-name",
|
|
|
|
"Commodity Unique Name",
|
|
|
|
"Unique form of the commodity name which combines "
|
|
|
|
"the namespace name and the commodity name.",
|
|
|
|
NULL,
|
|
|
|
G_PARAM_READABLE));
|
|
|
|
g_object_class_install_property(gobject_class,
|
|
|
|
PROP_QUOTE_FLAG,
|
|
|
|
g_param_spec_boolean ("quote_flag",
|
|
|
|
"Quote Flag",
|
|
|
|
"TRUE if prices are to be downloaded for this "
|
|
|
|
"commodity from a quote source.",
|
|
|
|
FALSE,
|
|
|
|
G_PARAM_READWRITE));
|
|
|
|
g_object_class_install_property(gobject_class,
|
|
|
|
PROP_QUOTE_SOURCE,
|
|
|
|
g_param_spec_pointer("quote-source",
|
|
|
|
"Quote Source",
|
|
|
|
"The quote source from which prices are downloaded.",
|
|
|
|
G_PARAM_READWRITE));
|
|
|
|
g_object_class_install_property(gobject_class,
|
|
|
|
PROP_QUOTE_TZ,
|
|
|
|
g_param_spec_string ("quote-tz",
|
|
|
|
"Commodity Quote Timezone",
|
|
|
|
"?????",
|
|
|
|
NULL,
|
|
|
|
G_PARAM_READWRITE));
|
2007-04-04 19:24:18 -05:00
|
|
|
}
|
|
|
|
|
2001-08-07 18:36:04 -05:00
|
|
|
gnc_commodity *
|
2005-11-01 21:32:36 -06:00
|
|
|
gnc_commodity_new(QofBook *book, const char * fullname,
|
2001-08-07 18:36:04 -05:00
|
|
|
const char * namespace, const char * mnemonic,
|
2006-02-11 14:00:52 -06:00
|
|
|
const char * cusip, int fraction)
|
2001-08-07 18:36:04 -05:00
|
|
|
{
|
2007-04-04 19:24:18 -05:00
|
|
|
gnc_commodity * retval = g_object_new(GNC_TYPE_COMMODITY, NULL);
|
2005-11-01 21:32:36 -06:00
|
|
|
gnc_commodity_table *table;
|
|
|
|
|
2007-04-04 19:24:18 -05:00
|
|
|
qof_instance_init_data (&retval->inst, GNC_ID_COMMODITY, book);
|
2008-08-01 11:02:07 -05:00
|
|
|
gnc_commodity_begin_edit(retval);
|
|
|
|
|
|
|
|
if( namespace != NULL ) {
|
|
|
|
gnc_commodity_set_namespace(retval, namespace);
|
|
|
|
if (gnc_commodity_namespace_is_iso(namespace)) {
|
|
|
|
gnc_commodity_set_quote_source(retval,
|
|
|
|
gnc_quote_source_lookup_by_internal("currency") );
|
|
|
|
}
|
2005-11-01 21:32:36 -06:00
|
|
|
}
|
2008-08-01 11:02:07 -05:00
|
|
|
gnc_commodity_set_fullname(retval, fullname);
|
|
|
|
gnc_commodity_set_mnemonic(retval, mnemonic);
|
|
|
|
gnc_commodity_set_cusip(retval, cusip);
|
|
|
|
gnc_commodity_set_fraction(retval, fraction);
|
|
|
|
gnc_commodity_commit_edit(retval);
|
|
|
|
|
2007-04-01 17:18:50 -05:00
|
|
|
qof_event_gen (&retval->inst, QOF_EVENT_CREATE, NULL);
|
2001-08-07 18:36:04 -05:00
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_destroy
|
|
|
|
********************************************************************/
|
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
static void
|
|
|
|
commodity_free(gnc_commodity * cm)
|
2001-10-03 05:07:45 -05:00
|
|
|
{
|
2006-04-05 12:59:14 -05:00
|
|
|
QofBook *book;
|
|
|
|
gnc_commodity_table *table;
|
2008-08-01 11:02:07 -05:00
|
|
|
CommodityPrivate* priv;
|
|
|
|
|
2001-08-07 18:36:04 -05:00
|
|
|
if(!cm) return;
|
2001-10-03 05:07:45 -05:00
|
|
|
|
2006-04-05 12:59:14 -05:00
|
|
|
book = qof_instance_get_book(&cm->inst);
|
|
|
|
table = gnc_commodity_table_get_table(book);
|
|
|
|
gnc_commodity_table_remove(table, cm);
|
2008-08-01 11:02:07 -05:00
|
|
|
priv = GET_PRIVATE(cm);
|
2006-04-05 12:59:14 -05:00
|
|
|
|
2007-04-01 17:18:50 -05:00
|
|
|
qof_event_gen (&cm->inst, QOF_EVENT_DESTROY, NULL);
|
2005-11-01 21:32:36 -06:00
|
|
|
|
2003-05-10 19:45:03 -05:00
|
|
|
/* Set at creation */
|
2008-08-01 11:02:07 -05:00
|
|
|
CACHE_REMOVE (priv->fullname);
|
|
|
|
CACHE_REMOVE (priv->cusip);
|
|
|
|
CACHE_REMOVE (priv->mnemonic);
|
|
|
|
CACHE_REMOVE (priv->quote_tz);
|
|
|
|
priv->namespace = NULL;
|
2001-10-03 05:07:45 -05:00
|
|
|
|
2003-05-10 19:45:03 -05:00
|
|
|
/* Set through accessor functions */
|
2008-08-01 11:02:07 -05:00
|
|
|
priv->quote_source = NULL;
|
2003-05-10 19:45:03 -05:00
|
|
|
|
|
|
|
/* Automatically generated */
|
2008-08-01 11:02:07 -05:00
|
|
|
g_free(priv->printname);
|
|
|
|
priv->printname = NULL;
|
2003-05-10 19:45:03 -05:00
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
g_free(priv->unique_name);
|
|
|
|
priv->unique_name = NULL;
|
2001-10-03 05:07:45 -05:00
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
priv->mark = 0;
|
2001-10-03 05:07:45 -05:00
|
|
|
|
2007-12-27 08:02:06 -06:00
|
|
|
#ifdef ACCOUNTS_CLEANED_UP
|
2007-12-25 23:17:37 -06:00
|
|
|
/* Account objects are not actually cleaned up when a book is closed (in fact
|
|
|
|
* a memory leak), but commodities are, so in currently this warning gets hit
|
|
|
|
* quite frequently. Disable the check until cleaning up of accounts objects
|
|
|
|
* on close is implemented. */
|
2008-08-01 11:02:07 -05:00
|
|
|
if(priv->usage_count != 0) {
|
2007-12-25 23:17:37 -06:00
|
|
|
PWARN("Destroying commodity (%p) with non-zero usage_count (%d).", cm,
|
2008-08-01 11:02:07 -05:00
|
|
|
priv->usage_count);
|
2007-12-25 23:17:37 -06:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-04-04 19:24:18 -05:00
|
|
|
/* qof_instance_release (&cm->inst); */
|
|
|
|
g_object_unref(cm);
|
2001-08-07 18:36:04 -05:00
|
|
|
}
|
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
void
|
|
|
|
gnc_commodity_destroy(gnc_commodity * cm)
|
|
|
|
{
|
|
|
|
gnc_commodity_begin_edit(cm);
|
|
|
|
qof_instance_set_destroying(cm, TRUE);
|
|
|
|
gnc_commodity_commit_edit(cm);
|
|
|
|
}
|
|
|
|
|
2003-08-16 23:20:25 -05:00
|
|
|
void
|
2007-12-27 20:27:57 -06:00
|
|
|
gnc_commodity_copy(gnc_commodity * dest, const gnc_commodity *src)
|
2003-08-16 23:20:25 -05:00
|
|
|
{
|
2008-08-01 11:02:07 -05:00
|
|
|
CommodityPrivate* src_priv = GET_PRIVATE(src);
|
|
|
|
CommodityPrivate* dest_priv = GET_PRIVATE(dest);
|
|
|
|
|
|
|
|
gnc_commodity_set_fullname (dest, src_priv->fullname);
|
2009-01-11 13:25:41 -06:00
|
|
|
gnc_commodity_set_mnemonic (dest, src_priv->mnemonic);
|
2008-08-01 11:02:07 -05:00
|
|
|
dest_priv->namespace = src_priv->namespace;
|
|
|
|
gnc_commodity_set_fraction (dest, src_priv->fraction);
|
|
|
|
gnc_commodity_set_cusip (dest, src_priv->cusip);
|
|
|
|
gnc_commodity_set_quote_flag (dest, src_priv->quote_flag);
|
2003-08-17 02:13:02 -05:00
|
|
|
gnc_commodity_set_quote_source (dest, gnc_commodity_get_quote_source (src));
|
2008-08-01 11:02:07 -05:00
|
|
|
gnc_commodity_set_quote_tz (dest, src_priv->quote_tz);
|
|
|
|
kvp_frame_delete (dest->inst.kvp_data);
|
|
|
|
dest->inst.kvp_data = kvp_frame_copy (src->inst.kvp_data);
|
2007-12-25 23:17:37 -06:00
|
|
|
kvp_frame_delete (dest->inst.kvp_data);
|
|
|
|
dest->inst.kvp_data = kvp_frame_copy (src->inst.kvp_data);
|
2003-08-16 23:20:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
gnc_commodity *
|
2007-12-27 20:27:57 -06:00
|
|
|
gnc_commodity_clone(const gnc_commodity *src, QofBook *dest_book)
|
2003-08-16 23:20:25 -05:00
|
|
|
{
|
2008-08-01 11:02:07 -05:00
|
|
|
CommodityPrivate* src_priv;
|
|
|
|
CommodityPrivate* dest_priv;
|
|
|
|
|
2007-04-04 19:24:18 -05:00
|
|
|
gnc_commodity * dest = g_object_new(GNC_TYPE_COMMODITY, NULL);
|
2007-12-25 23:17:37 -06:00
|
|
|
qof_instance_init_data (&dest->inst, GNC_ID_COMMODITY, dest_book);
|
2008-08-01 11:02:07 -05:00
|
|
|
src_priv = GET_PRIVATE(src);
|
|
|
|
dest_priv = GET_PRIVATE(dest);
|
2003-08-16 23:20:25 -05:00
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
dest_priv->fullname = CACHE_INSERT(src_priv->fullname);
|
|
|
|
dest_priv->mnemonic = CACHE_INSERT(src_priv->mnemonic);
|
|
|
|
dest_priv->cusip = CACHE_INSERT(src_priv->cusip);
|
|
|
|
dest_priv->quote_tz = CACHE_INSERT(src_priv->quote_tz);
|
2006-01-10 20:58:59 -06:00
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
dest_priv->namespace = src_priv->namespace;
|
2003-08-16 23:20:25 -05:00
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
dest_priv->mark = 0;
|
|
|
|
dest_priv->fraction = src_priv->fraction;
|
|
|
|
dest_priv->quote_flag = src_priv->quote_flag;
|
2003-08-17 01:37:24 -05:00
|
|
|
|
|
|
|
gnc_commodity_set_quote_source (dest, gnc_commodity_get_quote_source (src));
|
2003-08-16 23:20:25 -05:00
|
|
|
|
2007-12-25 23:17:37 -06:00
|
|
|
kvp_frame_delete (dest->inst.kvp_data);
|
|
|
|
dest->inst.kvp_data = kvp_frame_copy (src->inst.kvp_data);
|
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
reset_printname(dest_priv);
|
|
|
|
reset_unique_name(dest_priv);
|
2003-08-16 23:20:25 -05:00
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
2001-08-07 18:36:04 -05:00
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_get_mnemonic
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
const char *
|
2002-01-01 22:28:32 -06:00
|
|
|
gnc_commodity_get_mnemonic(const gnc_commodity * cm)
|
|
|
|
{
|
2001-08-07 18:36:04 -05:00
|
|
|
if(!cm) return NULL;
|
2008-08-01 11:02:07 -05:00
|
|
|
return GET_PRIVATE(cm)->mnemonic;
|
2001-08-07 18:36:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_get_printname
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
const char *
|
2002-01-01 22:28:32 -06:00
|
|
|
gnc_commodity_get_printname(const gnc_commodity * cm)
|
|
|
|
{
|
2001-08-07 18:36:04 -05:00
|
|
|
if(!cm) return NULL;
|
2008-08-01 11:02:07 -05:00
|
|
|
return GET_PRIVATE(cm)->printname;
|
2001-08-07 18:36:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_get_namespace
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
const char *
|
2002-01-01 22:28:32 -06:00
|
|
|
gnc_commodity_get_namespace(const gnc_commodity * cm)
|
|
|
|
{
|
2001-08-07 18:36:04 -05:00
|
|
|
if(!cm) return NULL;
|
2008-08-01 11:02:07 -05:00
|
|
|
return gnc_commodity_namespace_get_name(GET_PRIVATE(cm)->namespace);
|
2001-08-07 18:36:04 -05:00
|
|
|
}
|
|
|
|
|
2006-04-03 16:39:25 -05:00
|
|
|
const char *
|
|
|
|
gnc_commodity_get_namespace_compat(const gnc_commodity * cm)
|
|
|
|
{
|
2008-08-01 11:02:07 -05:00
|
|
|
CommodityPrivate* priv;
|
|
|
|
|
|
|
|
if (!cm) return NULL;
|
|
|
|
priv = GET_PRIVATE(cm);
|
|
|
|
if(!priv->namespace) return NULL;
|
|
|
|
if (priv->namespace->iso4217) {
|
2006-04-13 21:29:53 -05:00
|
|
|
/* Data files are still written with ISO4217. */
|
2006-04-03 16:39:25 -05:00
|
|
|
return GNC_COMMODITY_NS_ISO;
|
2006-04-13 21:29:53 -05:00
|
|
|
}
|
2008-08-01 11:02:07 -05:00
|
|
|
return gnc_commodity_namespace_get_name(priv->namespace);
|
2006-04-03 16:39:25 -05:00
|
|
|
}
|
|
|
|
|
2005-11-01 21:32:36 -06:00
|
|
|
gnc_commodity_namespace *
|
|
|
|
gnc_commodity_get_namespace_ds(const gnc_commodity * cm)
|
|
|
|
{
|
|
|
|
if(!cm) return NULL;
|
2008-08-01 11:02:07 -05:00
|
|
|
return GET_PRIVATE(cm)->namespace;
|
2005-11-01 21:32:36 -06:00
|
|
|
}
|
2001-08-07 18:36:04 -05:00
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_get_fullname
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
const char *
|
2002-01-01 22:28:32 -06:00
|
|
|
gnc_commodity_get_fullname(const gnc_commodity * cm)
|
|
|
|
{
|
2001-08-07 18:36:04 -05:00
|
|
|
if(!cm) return NULL;
|
2008-08-01 11:02:07 -05:00
|
|
|
return GET_PRIVATE(cm)->fullname;
|
2001-08-07 18:36:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_get_unique_name
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
const char *
|
2002-01-01 22:28:32 -06:00
|
|
|
gnc_commodity_get_unique_name(const gnc_commodity * cm)
|
|
|
|
{
|
2001-08-07 18:36:04 -05:00
|
|
|
if(!cm) return NULL;
|
2008-08-01 11:02:07 -05:00
|
|
|
return GET_PRIVATE(cm)->unique_name;
|
2001-08-07 18:36:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/********************************************************************
|
2006-02-11 14:00:52 -06:00
|
|
|
* gnc_commodity_get_cusip
|
2001-08-07 18:36:04 -05:00
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
const char *
|
2006-02-11 14:00:52 -06:00
|
|
|
gnc_commodity_get_cusip(const gnc_commodity * cm)
|
2002-01-01 22:28:32 -06:00
|
|
|
{
|
2001-08-07 18:36:04 -05:00
|
|
|
if(!cm) return NULL;
|
2008-08-01 11:02:07 -05:00
|
|
|
return GET_PRIVATE(cm)->cusip;
|
2001-08-07 18:36:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_get_fraction
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
int
|
2002-01-01 22:28:32 -06:00
|
|
|
gnc_commodity_get_fraction(const gnc_commodity * cm)
|
|
|
|
{
|
2001-08-07 18:36:04 -05:00
|
|
|
if(!cm) return 0;
|
2008-08-01 11:02:07 -05:00
|
|
|
return GET_PRIVATE(cm)->fraction;
|
2001-08-07 18:36:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_get_mark
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
gint16
|
2002-01-01 22:28:32 -06:00
|
|
|
gnc_commodity_get_mark(const gnc_commodity * cm)
|
|
|
|
{
|
2001-08-07 18:36:04 -05:00
|
|
|
if(!cm) return 0;
|
2008-08-01 11:02:07 -05:00
|
|
|
return GET_PRIVATE(cm)->mark;
|
2001-08-07 18:36:04 -05:00
|
|
|
}
|
|
|
|
|
2007-12-25 23:17:37 -06:00
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_get_auto_quote_control_flag
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gnc_commodity_get_auto_quote_control_flag(const gnc_commodity *cm)
|
|
|
|
{
|
|
|
|
const char *str;
|
|
|
|
|
|
|
|
if(!cm) return FALSE;
|
|
|
|
|
|
|
|
str = kvp_frame_get_string(cm->inst.kvp_data, "auto_quote_control");
|
|
|
|
return !str || (strcmp(str, "false") != 0);
|
|
|
|
}
|
|
|
|
|
2003-05-10 19:45:03 -05:00
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_get_quote_flag
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gnc_commodity_get_quote_flag(const gnc_commodity *cm)
|
|
|
|
{
|
|
|
|
if(!cm) return FALSE;
|
2008-08-01 11:02:07 -05:00
|
|
|
return (GET_PRIVATE(cm)->quote_flag);
|
2003-05-10 19:45:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_get_quote_source
|
|
|
|
********************************************************************/
|
|
|
|
|
2003-07-01 22:35:20 -05:00
|
|
|
gnc_quote_source*
|
2003-05-10 19:45:03 -05:00
|
|
|
gnc_commodity_get_quote_source(const gnc_commodity *cm)
|
|
|
|
{
|
2008-08-01 11:02:07 -05:00
|
|
|
CommodityPrivate* priv;
|
|
|
|
|
2003-05-10 19:45:03 -05:00
|
|
|
if(!cm) return NULL;
|
2008-08-01 11:02:07 -05:00
|
|
|
priv = GET_PRIVATE(cm);
|
|
|
|
if (!priv->quote_source && gnc_commodity_is_iso(cm))
|
2003-07-01 22:35:20 -05:00
|
|
|
return ¤cy_quote_source;
|
2008-08-01 11:02:07 -05:00
|
|
|
return priv->quote_source;
|
2003-05-10 19:45:03 -05:00
|
|
|
}
|
|
|
|
|
2003-07-01 22:35:20 -05:00
|
|
|
gnc_quote_source*
|
|
|
|
gnc_commodity_get_default_quote_source(const gnc_commodity *cm)
|
|
|
|
{
|
|
|
|
if (cm && gnc_commodity_is_iso(cm))
|
|
|
|
return ¤cy_quote_source;
|
|
|
|
/* Should make this a user option at some point. */
|
|
|
|
return gnc_quote_source_lookup_by_internal("yahoo");
|
|
|
|
}
|
|
|
|
|
2003-05-10 19:45:03 -05:00
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_get_quote_tz
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
const char*
|
|
|
|
gnc_commodity_get_quote_tz(const gnc_commodity *cm)
|
|
|
|
{
|
|
|
|
if(!cm) return NULL;
|
2008-08-01 11:02:07 -05:00
|
|
|
return GET_PRIVATE(cm)->quote_tz;
|
2003-05-10 19:45:03 -05:00
|
|
|
}
|
|
|
|
|
2001-08-07 18:36:04 -05:00
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_set_mnemonic
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
void
|
2002-01-02 04:08:17 -06:00
|
|
|
gnc_commodity_set_mnemonic(gnc_commodity * cm, const char * mnemonic)
|
|
|
|
{
|
2008-08-01 11:02:07 -05:00
|
|
|
CommodityPrivate* priv;
|
|
|
|
|
2001-08-07 18:36:04 -05:00
|
|
|
if(!cm) return;
|
2008-08-01 11:02:07 -05:00
|
|
|
priv = GET_PRIVATE(cm);
|
|
|
|
if(priv->mnemonic == mnemonic) return;
|
2001-08-07 18:36:04 -05:00
|
|
|
|
2006-05-06 01:14:21 -05:00
|
|
|
gnc_commodity_begin_edit(cm);
|
2008-08-01 11:02:07 -05:00
|
|
|
CACHE_REMOVE (priv->mnemonic);
|
|
|
|
priv->mnemonic = CACHE_INSERT(mnemonic);
|
2001-08-07 18:36:04 -05:00
|
|
|
|
2005-11-01 21:32:36 -06:00
|
|
|
mark_commodity_dirty (cm);
|
2008-08-01 11:02:07 -05:00
|
|
|
reset_printname(priv);
|
|
|
|
reset_unique_name(priv);
|
2006-05-06 01:14:21 -05:00
|
|
|
gnc_commodity_commit_edit(cm);
|
2001-08-07 18:36:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_set_namespace
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
void
|
2002-01-02 04:08:17 -06:00
|
|
|
gnc_commodity_set_namespace(gnc_commodity * cm, const char * namespace)
|
|
|
|
{
|
2005-11-01 21:32:36 -06:00
|
|
|
QofBook *book;
|
|
|
|
gnc_commodity_table *table;
|
|
|
|
gnc_commodity_namespace *nsp;
|
2008-08-01 11:02:07 -05:00
|
|
|
CommodityPrivate* priv;
|
2001-08-07 18:36:04 -05:00
|
|
|
|
2005-11-01 21:32:36 -06:00
|
|
|
if(!cm) return;
|
2008-08-01 11:02:07 -05:00
|
|
|
priv = GET_PRIVATE(cm);
|
2005-11-01 21:32:36 -06:00
|
|
|
book = qof_instance_get_book (&cm->inst);
|
|
|
|
table = gnc_commodity_table_get_table(book);
|
2006-01-02 15:11:26 -06:00
|
|
|
nsp = gnc_commodity_table_add_namespace(table, namespace, book);
|
2008-08-01 11:02:07 -05:00
|
|
|
if (priv->namespace == nsp)
|
2005-11-01 21:32:36 -06:00
|
|
|
return;
|
2001-08-07 18:36:04 -05:00
|
|
|
|
2006-05-06 01:14:21 -05:00
|
|
|
gnc_commodity_begin_edit(cm);
|
2008-08-01 11:02:07 -05:00
|
|
|
priv->namespace = nsp;
|
2006-04-03 16:39:25 -05:00
|
|
|
if (nsp->iso4217)
|
2008-08-01 11:02:07 -05:00
|
|
|
priv->quote_source = gnc_quote_source_lookup_by_internal("currency");
|
2005-11-01 21:32:36 -06:00
|
|
|
mark_commodity_dirty(cm);
|
2008-08-01 11:02:07 -05:00
|
|
|
reset_printname(priv);
|
|
|
|
reset_unique_name(priv);
|
2006-05-06 01:14:21 -05:00
|
|
|
gnc_commodity_commit_edit(cm);
|
2001-08-07 18:36:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_set_fullname
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
void
|
2002-01-02 04:08:17 -06:00
|
|
|
gnc_commodity_set_fullname(gnc_commodity * cm, const char * fullname)
|
|
|
|
{
|
2008-08-01 11:02:07 -05:00
|
|
|
CommodityPrivate* priv;
|
|
|
|
|
2001-08-07 18:36:04 -05:00
|
|
|
if(!cm) return;
|
2008-08-01 11:02:07 -05:00
|
|
|
priv = GET_PRIVATE(cm);
|
|
|
|
if(priv->fullname == fullname) return;
|
2001-08-07 18:36:04 -05:00
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
CACHE_REMOVE (priv->fullname);
|
|
|
|
priv->fullname = CACHE_INSERT (fullname);
|
2001-08-07 18:36:04 -05:00
|
|
|
|
2006-05-06 01:14:21 -05:00
|
|
|
gnc_commodity_begin_edit(cm);
|
2005-11-01 21:32:36 -06:00
|
|
|
mark_commodity_dirty(cm);
|
2008-08-01 11:02:07 -05:00
|
|
|
reset_printname(priv);
|
2006-05-06 01:14:21 -05:00
|
|
|
gnc_commodity_commit_edit(cm);
|
2001-08-07 18:36:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************
|
2006-02-11 14:00:52 -06:00
|
|
|
* gnc_commodity_set_cusip
|
2001-08-07 18:36:04 -05:00
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
void
|
2006-02-11 14:00:52 -06:00
|
|
|
gnc_commodity_set_cusip(gnc_commodity * cm,
|
|
|
|
const char * cusip)
|
2002-01-02 04:08:17 -06:00
|
|
|
{
|
2008-08-01 11:02:07 -05:00
|
|
|
CommodityPrivate* priv;
|
|
|
|
|
2001-08-07 18:36:04 -05:00
|
|
|
if(!cm) return;
|
2008-08-01 11:02:07 -05:00
|
|
|
|
|
|
|
priv = GET_PRIVATE(cm);
|
|
|
|
if(priv->cusip == cusip) return;
|
2001-08-07 18:36:04 -05:00
|
|
|
|
2006-05-06 01:14:21 -05:00
|
|
|
gnc_commodity_begin_edit(cm);
|
2008-08-01 11:02:07 -05:00
|
|
|
CACHE_REMOVE (priv->cusip);
|
|
|
|
priv->cusip = CACHE_INSERT (cusip);
|
2005-11-01 21:32:36 -06:00
|
|
|
mark_commodity_dirty(cm);
|
2006-05-06 01:14:21 -05:00
|
|
|
gnc_commodity_commit_edit(cm);
|
2001-08-07 18:36:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_set_fraction
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
void
|
2002-01-02 04:08:17 -06:00
|
|
|
gnc_commodity_set_fraction(gnc_commodity * cm, int fraction)
|
|
|
|
{
|
2001-08-07 18:36:04 -05:00
|
|
|
if(!cm) return;
|
2006-05-06 01:14:21 -05:00
|
|
|
gnc_commodity_begin_edit(cm);
|
2008-08-01 11:02:07 -05:00
|
|
|
GET_PRIVATE(cm)->fraction = fraction;
|
2005-11-01 21:32:36 -06:00
|
|
|
mark_commodity_dirty(cm);
|
2006-05-06 01:14:21 -05:00
|
|
|
gnc_commodity_commit_edit(cm);
|
2001-08-07 18:36:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************
|
2003-05-10 19:45:03 -05:00
|
|
|
* gnc_commodity_set_mark
|
2001-08-07 18:36:04 -05:00
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
void
|
2002-01-02 04:08:17 -06:00
|
|
|
gnc_commodity_set_mark(gnc_commodity * cm, gint16 mark)
|
|
|
|
{
|
2001-08-07 18:36:04 -05:00
|
|
|
if(!cm) return;
|
2008-08-01 11:02:07 -05:00
|
|
|
GET_PRIVATE(cm)->mark = mark;
|
2001-08-07 18:36:04 -05:00
|
|
|
}
|
|
|
|
|
2007-12-25 23:17:37 -06:00
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_set_auto_quote_control_flag
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
static void
|
|
|
|
gnc_commodity_set_auto_quote_control_flag(gnc_commodity *cm,
|
|
|
|
const gboolean flag)
|
|
|
|
{
|
|
|
|
ENTER ("(cm=%p, flag=%d)", cm, flag);
|
|
|
|
|
|
|
|
if(!cm) {
|
|
|
|
LEAVE("");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gnc_commodity_begin_edit(cm);
|
|
|
|
kvp_frame_set_string(cm->inst.kvp_data,
|
|
|
|
"auto_quote_control", flag ? NULL : "false");
|
|
|
|
mark_commodity_dirty(cm);
|
|
|
|
gnc_commodity_commit_edit(cm);
|
|
|
|
LEAVE("");
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_user_set_quote_flag
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
void
|
|
|
|
gnc_commodity_user_set_quote_flag(gnc_commodity *cm, const gboolean flag)
|
|
|
|
{
|
2008-08-01 11:02:07 -05:00
|
|
|
CommodityPrivate* priv;
|
|
|
|
|
2007-12-25 23:17:37 -06:00
|
|
|
ENTER ("(cm=%p, flag=%d)", cm, flag);
|
|
|
|
|
|
|
|
if(!cm) {
|
|
|
|
LEAVE("");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
priv = GET_PRIVATE(cm);
|
2007-12-25 23:17:37 -06:00
|
|
|
gnc_commodity_begin_edit(cm);
|
|
|
|
gnc_commodity_set_quote_flag(cm, flag);
|
|
|
|
if(gnc_commodity_is_iso(cm)) {
|
|
|
|
/* For currencies, disable auto quote control if the quote flag is being
|
|
|
|
* changed from its default value and enable it if the quote flag is being
|
|
|
|
* reset to its default value. The defaults for the quote flag are
|
|
|
|
* disabled if no accounts are using the currency, and true otherwise.
|
|
|
|
* Thus enable auto quote control if flag is FALSE and there are not any
|
|
|
|
* accounts using this currency OR flag is TRUE and there are accounts
|
|
|
|
* using this currency; otherwise disable auto quote control */
|
|
|
|
gnc_commodity_set_auto_quote_control_flag(cm,
|
2008-08-01 11:02:07 -05:00
|
|
|
(!flag && (priv->usage_count == 0)) || (flag && (priv->usage_count != 0)));
|
2007-12-25 23:17:37 -06:00
|
|
|
}
|
|
|
|
gnc_commodity_commit_edit(cm);
|
|
|
|
LEAVE("");
|
|
|
|
}
|
|
|
|
|
2003-05-10 19:45:03 -05:00
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_set_quote_flag
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
void
|
|
|
|
gnc_commodity_set_quote_flag(gnc_commodity *cm, const gboolean flag)
|
|
|
|
{
|
|
|
|
ENTER ("(cm=%p, flag=%d)", cm, flag);
|
|
|
|
|
|
|
|
if(!cm) return;
|
2006-05-06 01:14:21 -05:00
|
|
|
gnc_commodity_begin_edit(cm);
|
2008-08-01 11:02:07 -05:00
|
|
|
GET_PRIVATE(cm)->quote_flag = flag;
|
2005-11-01 21:32:36 -06:00
|
|
|
mark_commodity_dirty(cm);
|
2006-05-06 01:14:21 -05:00
|
|
|
gnc_commodity_commit_edit(cm);
|
2003-05-10 19:45:03 -05:00
|
|
|
LEAVE(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_set_quote_source
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
void
|
2003-07-01 22:35:20 -05:00
|
|
|
gnc_commodity_set_quote_source(gnc_commodity *cm, gnc_quote_source *src)
|
2003-05-10 19:45:03 -05:00
|
|
|
{
|
2003-07-01 22:35:20 -05:00
|
|
|
ENTER ("(cm=%p, src=%p(%s))", cm, src, src ? src->internal_name : "unknown");
|
2003-05-10 19:45:03 -05:00
|
|
|
|
|
|
|
if(!cm) return;
|
2006-05-06 01:14:21 -05:00
|
|
|
gnc_commodity_begin_edit(cm);
|
2008-08-01 11:02:07 -05:00
|
|
|
GET_PRIVATE(cm)->quote_source = src;
|
2005-11-01 21:32:36 -06:00
|
|
|
mark_commodity_dirty(cm);
|
2006-05-06 01:14:21 -05:00
|
|
|
gnc_commodity_commit_edit(cm);
|
2003-05-10 19:45:03 -05:00
|
|
|
LEAVE(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_set_quote_tz
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
void
|
|
|
|
gnc_commodity_set_quote_tz(gnc_commodity *cm, const char *tz)
|
|
|
|
{
|
2008-08-01 11:02:07 -05:00
|
|
|
CommodityPrivate* priv;
|
|
|
|
|
2007-11-24 09:38:19 -06:00
|
|
|
ENTER ("(cm=%p, tz=%s)", cm, tz ? tz : "(null)");
|
2003-05-10 19:45:03 -05:00
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
if(!cm) return;
|
|
|
|
|
|
|
|
priv = GET_PRIVATE(cm);
|
|
|
|
|
|
|
|
if(tz == priv->quote_tz) return;
|
2003-05-10 19:45:03 -05:00
|
|
|
|
2006-05-06 01:14:21 -05:00
|
|
|
gnc_commodity_begin_edit(cm);
|
2008-08-01 11:02:07 -05:00
|
|
|
CACHE_REMOVE (priv->quote_tz);
|
|
|
|
priv->quote_tz = CACHE_INSERT (tz);
|
2005-11-01 21:32:36 -06:00
|
|
|
mark_commodity_dirty(cm);
|
2006-05-06 01:14:21 -05:00
|
|
|
gnc_commodity_commit_edit(cm);
|
2003-05-10 19:45:03 -05:00
|
|
|
LEAVE(" ");
|
|
|
|
}
|
|
|
|
|
2007-12-25 23:17:37 -06:00
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_increment_usage_count
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
void
|
|
|
|
gnc_commodity_increment_usage_count(gnc_commodity *cm)
|
|
|
|
{
|
|
|
|
const char *str;
|
2008-08-01 11:02:07 -05:00
|
|
|
CommodityPrivate* priv;
|
2007-12-25 23:17:37 -06:00
|
|
|
|
|
|
|
ENTER("(cm=%p)", cm);
|
|
|
|
|
|
|
|
if(!cm) {
|
|
|
|
LEAVE("");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
priv = GET_PRIVATE(cm);
|
|
|
|
|
|
|
|
if((priv->usage_count == 0) && !priv->quote_flag
|
2007-12-25 23:17:37 -06:00
|
|
|
&& gnc_commodity_get_auto_quote_control_flag(cm)
|
|
|
|
&& gnc_commodity_is_iso(cm)) {
|
|
|
|
/* compatability hack - Gnucash 1.8 gets currency quotes when a
|
|
|
|
non-default currency is assigned to an account. */
|
|
|
|
gnc_commodity_begin_edit(cm);
|
|
|
|
gnc_commodity_set_quote_flag(cm, TRUE);
|
|
|
|
gnc_commodity_set_quote_source(cm,
|
|
|
|
gnc_commodity_get_default_quote_source(cm));
|
|
|
|
gnc_commodity_commit_edit(cm);
|
|
|
|
}
|
2008-08-01 11:02:07 -05:00
|
|
|
priv->usage_count++;
|
|
|
|
LEAVE("(usage_count=%d)", priv->usage_count);
|
2007-12-25 23:17:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_decrement_usage_count
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
void
|
|
|
|
gnc_commodity_decrement_usage_count(gnc_commodity *cm)
|
|
|
|
{
|
|
|
|
const char *str;
|
2008-08-01 11:02:07 -05:00
|
|
|
CommodityPrivate* priv;
|
2007-12-25 23:17:37 -06:00
|
|
|
|
|
|
|
ENTER("(cm=%p)", cm);
|
|
|
|
|
|
|
|
if(!cm) {
|
|
|
|
LEAVE("");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
priv = GET_PRIVATE(cm);
|
|
|
|
|
|
|
|
if(priv->usage_count == 0) {
|
2007-12-25 23:17:37 -06:00
|
|
|
PWARN("usage_count already zero");
|
|
|
|
LEAVE("");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
priv->usage_count--;
|
|
|
|
if((priv->usage_count == 0) && priv->quote_flag
|
2007-12-25 23:17:37 -06:00
|
|
|
&& gnc_commodity_get_auto_quote_control_flag(cm)
|
|
|
|
&& gnc_commodity_is_iso(cm)) {
|
|
|
|
/* if this is a currency with auto quote control enabled and no more
|
|
|
|
* accounts reference this currency, disable quote retrieval */
|
|
|
|
gnc_commodity_set_quote_flag(cm, FALSE);
|
|
|
|
}
|
2008-08-01 11:02:07 -05:00
|
|
|
LEAVE("(usage_count=%d)", priv->usage_count);
|
2007-12-25 23:17:37 -06:00
|
|
|
}
|
|
|
|
|
2003-05-10 19:45:03 -05:00
|
|
|
/********************************************************************\
|
|
|
|
\********************************************************************/
|
|
|
|
|
|
|
|
|
2001-08-07 18:36:04 -05:00
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_equiv
|
|
|
|
* are two commodities the same?
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
gboolean
|
2002-01-02 04:08:17 -06:00
|
|
|
gnc_commodity_equiv(const gnc_commodity * a, const gnc_commodity * b)
|
|
|
|
{
|
2008-08-01 11:02:07 -05:00
|
|
|
CommodityPrivate* priv_a;
|
|
|
|
CommodityPrivate* priv_b;
|
|
|
|
|
2001-08-07 18:36:04 -05:00
|
|
|
if(a == b) return TRUE;
|
|
|
|
if(!a || !b) return FALSE;
|
2008-08-01 11:02:07 -05:00
|
|
|
|
|
|
|
priv_a = GET_PRIVATE(a);
|
|
|
|
priv_b = GET_PRIVATE(b);
|
|
|
|
if(priv_a->namespace != priv_b->namespace) return FALSE;
|
|
|
|
if(safe_strcmp(priv_a->mnemonic, priv_b->mnemonic) != 0) return FALSE;
|
2001-08-07 18:36:04 -05:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2001-10-12 05:50:35 -05:00
|
|
|
gboolean
|
|
|
|
gnc_commodity_equal(const gnc_commodity * a, const gnc_commodity * b)
|
|
|
|
{
|
2008-08-01 11:02:07 -05:00
|
|
|
CommodityPrivate* priv_a;
|
|
|
|
CommodityPrivate* priv_b;
|
|
|
|
|
2001-10-12 05:50:35 -05:00
|
|
|
if (a == b) return TRUE;
|
|
|
|
|
|
|
|
if (!a || !b)
|
|
|
|
{
|
2002-12-07 17:50:14 -06:00
|
|
|
DEBUG ("one is NULL");
|
2001-10-12 05:50:35 -05:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
priv_a = GET_PRIVATE(a);
|
|
|
|
priv_b = GET_PRIVATE(b);
|
|
|
|
|
|
|
|
if (priv_a->namespace != priv_b->namespace)
|
2001-10-12 05:50:35 -05:00
|
|
|
{
|
2005-11-01 21:32:36 -06:00
|
|
|
DEBUG ("namespaces differ: %p(%s) vs %p(%s)",
|
2008-08-01 11:02:07 -05:00
|
|
|
priv_a->namespace, gnc_commodity_namespace_get_name(priv_a->namespace),
|
|
|
|
priv_b->namespace, gnc_commodity_namespace_get_name(priv_b->namespace));
|
2001-10-12 05:50:35 -05:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
if (safe_strcmp(priv_a->mnemonic, priv_b->mnemonic) != 0)
|
2001-10-12 05:50:35 -05:00
|
|
|
{
|
2008-08-01 11:02:07 -05:00
|
|
|
DEBUG ("mnemonics differ: %s vs %s", priv_a->mnemonic, priv_b->mnemonic);
|
2001-10-12 05:50:35 -05:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
if (safe_strcmp(priv_a->fullname, priv_b->fullname) != 0)
|
2001-10-12 05:50:35 -05:00
|
|
|
{
|
2008-08-01 11:02:07 -05:00
|
|
|
DEBUG ("fullnames differ: %s vs %s", priv_a->fullname, priv_b->fullname);
|
2001-10-12 05:50:35 -05:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
if (safe_strcmp(priv_a->cusip, priv_b->cusip) != 0)
|
2001-10-12 05:50:35 -05:00
|
|
|
{
|
2008-08-01 11:02:07 -05:00
|
|
|
DEBUG ("cusips differ: %s vs %s", priv_a->cusip, priv_b->cusip);
|
2001-10-12 05:50:35 -05:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
if (priv_a->fraction != priv_b->fraction)
|
2001-10-12 05:50:35 -05:00
|
|
|
{
|
2008-08-01 11:02:07 -05:00
|
|
|
DEBUG ("fractions differ: %d vs %d", priv_a->fraction, priv_b->fraction);
|
2001-10-12 05:50:35 -05:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2008-11-20 11:00:37 -06:00
|
|
|
int gnc_commodity_compare(const gnc_commodity * a, const gnc_commodity * b)
|
|
|
|
{
|
|
|
|
if (gnc_commodity_equal(a,b))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2001-08-07 18:36:04 -05:00
|
|
|
|
2008-11-20 11:11:47 -06:00
|
|
|
int gnc_commodity_compare_void(const void * a, const void * b)
|
|
|
|
{
|
|
|
|
return gnc_commodity_compare(a, b);
|
|
|
|
}
|
|
|
|
|
2005-11-01 21:32:36 -06:00
|
|
|
/************************************************************
|
|
|
|
* Namespace functions *
|
|
|
|
************************************************************/
|
|
|
|
const char *
|
2007-12-27 20:27:57 -06:00
|
|
|
gnc_commodity_namespace_get_name (const gnc_commodity_namespace *ns)
|
2005-11-01 21:32:36 -06:00
|
|
|
{
|
|
|
|
if (ns == NULL)
|
|
|
|
return NULL;
|
|
|
|
return ns->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
GList *
|
|
|
|
gnc_commodity_namespace_get_commodity_list(const gnc_commodity_namespace *namespace)
|
|
|
|
{
|
|
|
|
if (!namespace)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return namespace->cm_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gnc_commodity_namespace_is_iso(const char *namespace)
|
|
|
|
{
|
2006-04-03 16:39:25 -05:00
|
|
|
return ((safe_strcmp(namespace, GNC_COMMODITY_NS_ISO) == 0) ||
|
|
|
|
(safe_strcmp(namespace, GNC_COMMODITY_NS_CURRENCY) == 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
static const gchar *
|
|
|
|
gnc_commodity_table_map_namespace(const char * namespace)
|
|
|
|
{
|
|
|
|
if (safe_strcmp(namespace, GNC_COMMODITY_NS_ISO) == 0)
|
|
|
|
return GNC_COMMODITY_NS_CURRENCY;
|
|
|
|
return namespace;
|
2005-11-01 21:32:36 -06:00
|
|
|
}
|
|
|
|
|
2001-08-07 18:36:04 -05:00
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_table_new
|
|
|
|
* make a new commodity table
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
gnc_commodity_table *
|
2002-01-02 04:08:17 -06:00
|
|
|
gnc_commodity_table_new(void)
|
|
|
|
{
|
2001-08-07 18:36:04 -05:00
|
|
|
gnc_commodity_table * retval = g_new0(gnc_commodity_table, 1);
|
2005-11-01 21:32:36 -06:00
|
|
|
retval->ns_table = g_hash_table_new(&g_str_hash, &g_str_equal);
|
|
|
|
retval->ns_list = NULL;
|
2001-08-07 18:36:04 -05:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2003-06-09 22:33:06 -05:00
|
|
|
/********************************************************************
|
|
|
|
* book anchor functons
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
gnc_commodity_table *
|
2003-06-24 19:52:46 -05:00
|
|
|
gnc_commodity_table_get_table(QofBook *book)
|
2003-06-09 22:33:06 -05:00
|
|
|
{
|
|
|
|
if (!book) return NULL;
|
2003-06-24 19:52:46 -05:00
|
|
|
return qof_book_get_data (book, GNC_COMMODITY_TABLE);
|
2003-06-09 22:33:06 -05:00
|
|
|
}
|
|
|
|
|
2003-10-21 00:00:30 -05:00
|
|
|
gnc_commodity *
|
2007-12-27 20:27:57 -06:00
|
|
|
gnc_commodity_obtain_twin (const gnc_commodity *from, QofBook *book)
|
2003-10-21 00:00:30 -05:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2007-12-25 23:17:37 -06:00
|
|
|
twin = gnc_commodity_clone (from, book);
|
2003-10-21 00:00:30 -05:00
|
|
|
twin = gnc_commodity_table_insert (comtbl, twin);
|
|
|
|
}
|
|
|
|
return twin;
|
|
|
|
}
|
|
|
|
|
2001-08-07 18:36:04 -05:00
|
|
|
/********************************************************************
|
2005-11-01 21:32:36 -06:00
|
|
|
* gnc_commodity_table_get_size
|
2001-08-07 18:36:04 -05:00
|
|
|
* get the size of the commodity table
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
guint
|
2007-12-27 20:27:57 -06:00
|
|
|
gnc_commodity_table_get_number_of_namespaces(const gnc_commodity_table* tbl)
|
2001-08-07 18:36:04 -05:00
|
|
|
{
|
|
|
|
g_return_val_if_fail(tbl, 0);
|
2005-11-01 21:32:36 -06:00
|
|
|
g_return_val_if_fail(tbl->ns_table, 0);
|
|
|
|
return g_hash_table_size(tbl->ns_table);
|
2001-08-07 18:36:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
count_coms(gpointer key, gpointer value, gpointer user_data)
|
|
|
|
{
|
2005-11-01 21:32:36 -06:00
|
|
|
GHashTable *tbl = ((gnc_commodity_namespace*)value)->cm_table;
|
2001-08-07 18:36:04 -05:00
|
|
|
guint *count = (guint*)user_data;
|
|
|
|
|
2006-04-13 22:49:14 -05:00
|
|
|
if(safe_strcmp((char*)key, GNC_COMMODITY_NS_CURRENCY) == 0)
|
2001-08-07 18:36:04 -05:00
|
|
|
{
|
|
|
|
/* don't count default commodities */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!value) return;
|
|
|
|
|
|
|
|
*count += g_hash_table_size(tbl);
|
|
|
|
}
|
|
|
|
|
|
|
|
guint
|
2007-12-27 20:27:57 -06:00
|
|
|
gnc_commodity_table_get_size(const gnc_commodity_table* tbl)
|
2001-08-07 18:36:04 -05:00
|
|
|
{
|
|
|
|
guint count = 0;
|
|
|
|
g_return_val_if_fail(tbl, 0);
|
2005-11-01 21:32:36 -06:00
|
|
|
g_return_val_if_fail(tbl->ns_table, 0);
|
2001-08-07 18:36:04 -05:00
|
|
|
|
2005-11-01 21:32:36 -06:00
|
|
|
g_hash_table_foreach(tbl->ns_table, count_coms, (gpointer)&count);
|
2001-08-07 18:36:04 -05:00
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_table_lookup
|
|
|
|
* locate a commodity by namespace and mnemonic.
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
gnc_commodity *
|
|
|
|
gnc_commodity_table_lookup(const gnc_commodity_table * table,
|
2003-05-29 18:47:52 -05:00
|
|
|
const char * namespace, const char * mnemonic)
|
|
|
|
{
|
2001-08-07 18:36:04 -05:00
|
|
|
gnc_commodity_namespace * nsp = NULL;
|
2003-05-30 13:34:06 -05:00
|
|
|
unsigned int i;
|
2001-08-07 18:36:04 -05:00
|
|
|
|
|
|
|
if (!table || !namespace || !mnemonic) return NULL;
|
|
|
|
|
2005-11-01 21:32:36 -06:00
|
|
|
nsp = gnc_commodity_table_find_namespace(table, namespace);
|
2001-08-07 18:36:04 -05:00
|
|
|
|
|
|
|
if(nsp) {
|
2003-01-11 15:36:34 -06:00
|
|
|
/*
|
2003-05-29 18:47:52 -05:00
|
|
|
* Backward compatability support for currencies that have
|
|
|
|
* recently changed.
|
2003-01-11 15:36:34 -06:00
|
|
|
*/
|
2009-01-11 06:46:57 -06:00
|
|
|
if (nsp->iso4217) {
|
|
|
|
for (i = 0; i < GNC_NEW_ISO_CODES; i++) {
|
|
|
|
if (strcmp(mnemonic, gnc_new_iso_codes[i].old_code) == 0) {
|
|
|
|
mnemonic = gnc_new_iso_codes[i].new_code;
|
|
|
|
break;
|
|
|
|
}
|
2003-05-29 18:47:52 -05:00
|
|
|
}
|
|
|
|
}
|
2005-11-01 21:32:36 -06:00
|
|
|
return g_hash_table_lookup(nsp->cm_table, (gpointer)mnemonic);
|
2001-08-07 18:36:04 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_table_lookup
|
|
|
|
* locate a commodity by unique name.
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
gnc_commodity *
|
|
|
|
gnc_commodity_table_lookup_unique(const gnc_commodity_table *table,
|
|
|
|
const char * unique_name)
|
|
|
|
{
|
|
|
|
char *namespace;
|
|
|
|
char *mnemonic;
|
|
|
|
gnc_commodity *commodity;
|
|
|
|
|
|
|
|
if (!table || !unique_name) return NULL;
|
|
|
|
|
|
|
|
namespace = g_strdup (unique_name);
|
|
|
|
mnemonic = strstr (namespace, "::");
|
|
|
|
if (!mnemonic)
|
|
|
|
{
|
|
|
|
g_free (namespace);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*mnemonic = '\0';
|
|
|
|
mnemonic += 2;
|
|
|
|
|
|
|
|
commodity = gnc_commodity_table_lookup (table, namespace, mnemonic);
|
|
|
|
|
|
|
|
g_free (namespace);
|
|
|
|
|
|
|
|
return commodity;
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_table_find_full
|
|
|
|
* locate a commodity by namespace and printable name
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
gnc_commodity *
|
|
|
|
gnc_commodity_table_find_full(const gnc_commodity_table * table,
|
|
|
|
const char * namespace,
|
2003-08-16 23:20:25 -05:00
|
|
|
const char * fullname)
|
|
|
|
{
|
2001-08-07 18:36:04 -05:00
|
|
|
gnc_commodity * retval=NULL;
|
|
|
|
GList * all;
|
|
|
|
GList * iterator;
|
|
|
|
|
|
|
|
if (!fullname || (fullname[0] == '\0'))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
all = gnc_commodity_table_get_commodities(table, namespace);
|
|
|
|
|
|
|
|
for(iterator = all; iterator; iterator=iterator->next) {
|
|
|
|
if(!strcmp(fullname,
|
|
|
|
gnc_commodity_get_printname(iterator->data))) {
|
|
|
|
retval = iterator->data;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_list_free (all);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_table_insert
|
|
|
|
* add a commodity to the table.
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
gnc_commodity *
|
|
|
|
gnc_commodity_table_insert(gnc_commodity_table * table,
|
2002-01-02 04:08:17 -06:00
|
|
|
gnc_commodity * comm)
|
|
|
|
{
|
2001-08-07 18:36:04 -05:00
|
|
|
gnc_commodity_namespace * nsp = NULL;
|
|
|
|
gnc_commodity *c;
|
2005-11-01 21:32:36 -06:00
|
|
|
const char *ns_name;
|
2008-08-01 11:02:07 -05:00
|
|
|
CommodityPrivate* priv;
|
2006-01-02 15:11:26 -06:00
|
|
|
QofBook *book;
|
2001-08-07 18:36:04 -05:00
|
|
|
|
|
|
|
if (!table) return NULL;
|
|
|
|
if (!comm) return NULL;
|
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
priv = GET_PRIVATE(comm);
|
|
|
|
|
2007-02-09 11:35:00 -06:00
|
|
|
ENTER ("(table=%p, comm=%p) %s %s", table, comm,
|
2008-08-01 11:02:07 -05:00
|
|
|
(priv->mnemonic == NULL ? "(null)" : priv->mnemonic),
|
|
|
|
(priv->fullname == NULL ? "(null)" : priv->fullname));
|
|
|
|
ns_name = gnc_commodity_namespace_get_name(priv->namespace);
|
|
|
|
c = gnc_commodity_table_lookup (table, ns_name, priv->mnemonic);
|
2001-08-07 18:36:04 -05:00
|
|
|
|
2003-08-16 23:20:25 -05:00
|
|
|
if (c)
|
|
|
|
{
|
2001-08-07 18:36:04 -05:00
|
|
|
if (c == comm)
|
2003-08-10 22:06:17 -05:00
|
|
|
{
|
2007-04-11 11:47:42 -05:00
|
|
|
LEAVE("already in table");
|
2001-08-07 18:36:04 -05:00
|
|
|
return c;
|
2003-08-10 22:06:17 -05:00
|
|
|
}
|
2009-01-11 15:00:54 -06:00
|
|
|
|
|
|
|
/* Backward compatability support for currencies that have
|
|
|
|
* recently changed. */
|
|
|
|
if (priv->namespace->iso4217) {
|
|
|
|
guint i;
|
|
|
|
for (i = 0; i < GNC_NEW_ISO_CODES; i++) {
|
|
|
|
if (!priv->mnemonic
|
|
|
|
|| !strcmp(priv->mnemonic, gnc_new_iso_codes[i].old_code)) {
|
|
|
|
gnc_commodity_set_mnemonic(comm, gnc_new_iso_codes[i].new_code);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-16 23:20:25 -05:00
|
|
|
gnc_commodity_copy (c, comm);
|
2001-08-07 18:36:04 -05:00
|
|
|
gnc_commodity_destroy (comm);
|
2007-04-11 11:47:42 -05:00
|
|
|
LEAVE("found at %p", c);
|
2001-08-07 18:36:04 -05:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2006-01-02 15:11:26 -06:00
|
|
|
book = qof_instance_get_book (&comm->inst);
|
|
|
|
nsp = gnc_commodity_table_add_namespace(table, ns_name, book);
|
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
PINFO ("insert %p %s into nsp=%p %s", priv->mnemonic, priv->mnemonic,
|
2005-11-01 21:32:36 -06:00
|
|
|
nsp->cm_table, nsp->name);
|
|
|
|
g_hash_table_insert(nsp->cm_table,
|
2008-08-01 11:02:07 -05:00
|
|
|
CACHE_INSERT(priv->mnemonic),
|
2001-08-07 18:36:04 -05:00
|
|
|
(gpointer)comm);
|
2005-11-01 21:32:36 -06:00
|
|
|
nsp->cm_list = g_list_append(nsp->cm_list, comm);
|
2001-08-07 18:36:04 -05:00
|
|
|
|
2007-04-01 17:18:50 -05:00
|
|
|
qof_event_gen (&comm->inst, QOF_EVENT_ADD, NULL);
|
2003-08-10 22:06:17 -05:00
|
|
|
LEAVE ("(table=%p, comm=%p)", table, comm);
|
2001-08-07 18:36:04 -05:00
|
|
|
return comm;
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_table_remove
|
|
|
|
* remove a commodity from the table.
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
void
|
|
|
|
gnc_commodity_table_remove(gnc_commodity_table * table,
|
|
|
|
gnc_commodity * comm)
|
|
|
|
{
|
|
|
|
gnc_commodity_namespace * nsp;
|
|
|
|
gnc_commodity *c;
|
2008-08-01 11:02:07 -05:00
|
|
|
CommodityPrivate* priv;
|
2005-11-01 21:32:36 -06:00
|
|
|
const char *ns_name;
|
2001-08-07 18:36:04 -05:00
|
|
|
|
|
|
|
if (!table) return;
|
|
|
|
if (!comm) return;
|
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
priv = GET_PRIVATE(comm);
|
|
|
|
ns_name = gnc_commodity_namespace_get_name(priv->namespace);
|
|
|
|
c = gnc_commodity_table_lookup (table, ns_name, priv->mnemonic);
|
2001-08-07 18:36:04 -05:00
|
|
|
if (c != comm) return;
|
|
|
|
|
2007-04-01 17:18:50 -05:00
|
|
|
qof_event_gen (&comm->inst, QOF_EVENT_REMOVE, NULL);
|
2005-11-01 21:32:36 -06:00
|
|
|
|
|
|
|
nsp = gnc_commodity_table_find_namespace(table, ns_name);
|
2001-08-07 18:36:04 -05:00
|
|
|
if (!nsp) return;
|
|
|
|
|
2005-11-01 21:32:36 -06:00
|
|
|
nsp->cm_list = g_list_remove(nsp->cm_list, comm);
|
2008-08-01 11:02:07 -05:00
|
|
|
g_hash_table_remove (nsp->cm_table, priv->mnemonic);
|
2003-08-16 23:48:40 -05:00
|
|
|
/* XXX minor mem leak, should remove the key as well */
|
2001-08-07 18:36:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_table_has_namespace
|
2003-05-10 19:45:03 -05:00
|
|
|
* see if the commodities namespace exists. May have zero commodities.
|
2001-08-07 18:36:04 -05:00
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
int
|
|
|
|
gnc_commodity_table_has_namespace(const gnc_commodity_table * table,
|
2002-01-02 04:08:17 -06:00
|
|
|
const char * namespace)
|
|
|
|
{
|
2001-08-07 18:36:04 -05:00
|
|
|
gnc_commodity_namespace * nsp = NULL;
|
|
|
|
|
|
|
|
if(!table || !namespace) { return 0; }
|
|
|
|
|
2005-11-01 21:32:36 -06:00
|
|
|
nsp = gnc_commodity_table_find_namespace(table, namespace);
|
2001-08-07 18:36:04 -05:00
|
|
|
if(nsp) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-01-02 04:08:17 -06:00
|
|
|
hash_keys_helper(gpointer key, gpointer value, gpointer data)
|
|
|
|
{
|
2001-08-07 18:36:04 -05:00
|
|
|
GList ** l = data;
|
|
|
|
*l = g_list_prepend(*l, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GList *
|
2002-01-02 04:08:17 -06:00
|
|
|
g_hash_table_keys(GHashTable * table)
|
|
|
|
{
|
2001-08-07 18:36:04 -05:00
|
|
|
GList * l = NULL;
|
|
|
|
g_hash_table_foreach(table, &hash_keys_helper, (gpointer) &l);
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-01-02 04:08:17 -06:00
|
|
|
hash_values_helper(gpointer key, gpointer value, gpointer data)
|
|
|
|
{
|
2001-08-07 18:36:04 -05:00
|
|
|
GList ** l = data;
|
|
|
|
*l = g_list_prepend(*l, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GList *
|
2002-01-02 04:08:17 -06:00
|
|
|
g_hash_table_values(GHashTable * table)
|
|
|
|
{
|
2001-08-07 18:36:04 -05:00
|
|
|
GList * l = NULL;
|
|
|
|
g_hash_table_foreach(table, &hash_values_helper, (gpointer) &l);
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_table_get_namespaces
|
|
|
|
* see if any commodities in the namespace exist
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
GList *
|
2002-01-02 04:08:17 -06:00
|
|
|
gnc_commodity_table_get_namespaces(const gnc_commodity_table * table)
|
|
|
|
{
|
2001-08-07 18:36:04 -05:00
|
|
|
if (!table)
|
|
|
|
return NULL;
|
|
|
|
|
2005-11-01 21:32:36 -06:00
|
|
|
return g_hash_table_keys(table->ns_table);
|
2001-08-07 18:36:04 -05:00
|
|
|
}
|
|
|
|
|
2005-11-01 21:32:36 -06:00
|
|
|
GList *
|
|
|
|
gnc_commodity_table_get_namespaces_list(const gnc_commodity_table * table)
|
2003-04-29 01:15:34 -05:00
|
|
|
{
|
2005-11-01 21:32:36 -06:00
|
|
|
if (!table)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return table->ns_list;
|
2003-04-29 01:15:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gnc_commodity_is_iso(const gnc_commodity * cm)
|
|
|
|
{
|
2008-08-01 11:02:07 -05:00
|
|
|
CommodityPrivate* priv;
|
|
|
|
|
|
|
|
if (!cm) return FALSE;
|
|
|
|
|
|
|
|
priv = GET_PRIVATE(cm);
|
|
|
|
if( !priv->namespace) return FALSE;
|
|
|
|
return priv->namespace->iso4217;
|
2003-04-29 01:15:34 -05:00
|
|
|
}
|
2001-08-07 18:36:04 -05:00
|
|
|
|
2006-03-06 20:42:12 -06:00
|
|
|
gboolean
|
|
|
|
gnc_commodity_is_currency(const gnc_commodity *cm)
|
|
|
|
{
|
|
|
|
const char *ns_name;
|
|
|
|
if (!cm) return FALSE;
|
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
ns_name = gnc_commodity_namespace_get_name(GET_PRIVATE(cm)->namespace);
|
2006-03-06 20:42:12 -06:00
|
|
|
return (!safe_strcmp(ns_name, GNC_COMMODITY_NS_LEGACY) ||
|
2006-04-12 10:22:57 -05:00
|
|
|
!safe_strcmp(ns_name, GNC_COMMODITY_NS_CURRENCY));
|
2006-03-06 20:42:12 -06:00
|
|
|
}
|
|
|
|
|
2001-08-07 18:36:04 -05:00
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_table_get_commodities
|
|
|
|
* list commodities in a give namespace
|
|
|
|
********************************************************************/
|
|
|
|
|
2006-10-15 14:02:05 -05:00
|
|
|
CommodityList *
|
2001-08-07 18:36:04 -05:00
|
|
|
gnc_commodity_table_get_commodities(const gnc_commodity_table * table,
|
2002-01-02 04:08:17 -06:00
|
|
|
const char * namespace)
|
|
|
|
{
|
2001-08-07 18:36:04 -05:00
|
|
|
gnc_commodity_namespace * ns = NULL;
|
|
|
|
|
2003-05-10 19:45:03 -05:00
|
|
|
if (!table)
|
|
|
|
return NULL;
|
2001-08-07 18:36:04 -05:00
|
|
|
|
2005-11-01 21:32:36 -06:00
|
|
|
ns = gnc_commodity_table_find_namespace(table, namespace);
|
2003-05-10 19:45:03 -05:00
|
|
|
if (!ns)
|
2001-08-07 18:36:04 -05:00
|
|
|
return NULL;
|
2003-05-10 19:45:03 -05:00
|
|
|
|
2005-11-01 21:32:36 -06:00
|
|
|
return g_hash_table_values(ns->cm_table);
|
2003-05-10 19:45:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_table_get_quotable_commodities
|
|
|
|
* list commodities in a given namespace that get price quotes
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
static void
|
|
|
|
get_quotables_helper1(gpointer key, gpointer value, gpointer data)
|
|
|
|
{
|
|
|
|
gnc_commodity *comm = value;
|
2008-08-01 11:02:07 -05:00
|
|
|
CommodityPrivate* priv = GET_PRIVATE(comm);
|
2003-05-10 19:45:03 -05:00
|
|
|
GList ** l = data;
|
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
if (!priv->quote_flag ||
|
|
|
|
!priv->quote_source || !priv->quote_source->supported)
|
2003-05-10 19:45:03 -05:00
|
|
|
return;
|
|
|
|
*l = g_list_prepend(*l, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
get_quotables_helper2 (gnc_commodity *comm, gpointer data)
|
|
|
|
{
|
|
|
|
GList ** l = data;
|
2008-08-01 11:02:07 -05:00
|
|
|
CommodityPrivate* priv = GET_PRIVATE(comm);
|
2003-05-10 19:45:03 -05:00
|
|
|
|
2008-08-01 11:02:07 -05:00
|
|
|
if (!priv->quote_flag ||
|
|
|
|
!priv->quote_source || !priv->quote_source->supported)
|
2003-05-10 19:45:03 -05:00
|
|
|
return TRUE;
|
|
|
|
*l = g_list_prepend(*l, comm);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2006-10-15 14:02:05 -05:00
|
|
|
CommodityList *
|
2006-01-20 21:50:25 -06:00
|
|
|
gnc_commodity_table_get_quotable_commodities(const gnc_commodity_table * table)
|
2003-05-10 19:45:03 -05:00
|
|
|
{
|
2003-07-03 17:40:18 -05:00
|
|
|
gnc_commodity_namespace * ns = NULL;
|
|
|
|
const char *namespace;
|
|
|
|
GList * nslist, * tmp;
|
2003-05-10 19:45:03 -05:00
|
|
|
GList * l = NULL;
|
2003-07-03 17:40:18 -05:00
|
|
|
regex_t pattern;
|
2006-01-20 21:50:25 -06:00
|
|
|
const char *expression = gnc_main_get_namespace_regexp();
|
2003-05-10 19:45:03 -05:00
|
|
|
|
2003-07-03 17:40:18 -05:00
|
|
|
ENTER("table=%p, expression=%s", table, expression);
|
2003-05-10 19:45:03 -05:00
|
|
|
if (!table)
|
|
|
|
return NULL;
|
|
|
|
|
2003-07-03 17:40:18 -05:00
|
|
|
if (expression && *expression) {
|
|
|
|
if (regcomp(&pattern, expression, REG_EXTENDED|REG_ICASE) != 0) {
|
|
|
|
LEAVE("Cannot compile regex");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
nslist = gnc_commodity_table_get_namespaces(table);
|
|
|
|
for (tmp = nslist; tmp; tmp = tmp->next) {
|
|
|
|
namespace = tmp->data;
|
|
|
|
if (regexec(&pattern, namespace, 0, NULL, 0) == 0) {
|
|
|
|
DEBUG("Running list of %s commodities", namespace);
|
2005-11-01 21:32:36 -06:00
|
|
|
ns = gnc_commodity_table_find_namespace(table, namespace);
|
2003-07-03 17:40:18 -05:00
|
|
|
if (ns) {
|
2005-11-01 21:32:36 -06:00
|
|
|
g_hash_table_foreach(ns->cm_table, &get_quotables_helper1, (gpointer) &l);
|
2003-07-03 17:40:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_list_free(nslist);
|
|
|
|
regfree(&pattern);
|
2003-05-10 19:45:03 -05:00
|
|
|
} else {
|
|
|
|
gnc_commodity_table_foreach_commodity(table, get_quotables_helper2,
|
|
|
|
(gpointer) &l);
|
2001-08-07 18:36:04 -05:00
|
|
|
}
|
2003-05-10 19:45:03 -05:00
|
|
|
LEAVE("list head %p", l);
|
|
|
|
return l;
|
2001-08-07 18:36:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_table_add_namespace
|
|
|
|
* add an empty namespace if it does not exist
|
|
|
|
********************************************************************/
|
|
|
|
|
2007-04-04 21:44:47 -05:00
|
|
|
/* GObject Initialization */
|
|
|
|
QOF_GOBJECT_IMPL(gnc_commodity_namespace, gnc_commodity_namespace, QOF_TYPE_INSTANCE);
|
|
|
|
|
|
|
|
static void
|
|
|
|
gnc_commodity_namespace_init(gnc_commodity_namespace* ns)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-04-04 22:10:26 -05:00
|
|
|
static void
|
|
|
|
gnc_commodity_namespace_dispose_real (GObject *nsp)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-04-04 21:44:47 -05:00
|
|
|
static void
|
|
|
|
gnc_commodity_namespace_finalize_real(GObject* nsp)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2005-11-01 21:32:36 -06:00
|
|
|
gnc_commodity_namespace *
|
2001-08-07 18:36:04 -05:00
|
|
|
gnc_commodity_table_add_namespace(gnc_commodity_table * table,
|
2006-01-02 15:11:26 -06:00
|
|
|
const char * namespace,
|
|
|
|
QofBook *book)
|
2002-01-02 04:08:17 -06:00
|
|
|
{
|
2001-08-07 18:36:04 -05:00
|
|
|
gnc_commodity_namespace * ns = NULL;
|
|
|
|
|
2005-11-01 21:32:36 -06:00
|
|
|
if (!table) return NULL;
|
2001-08-07 18:36:04 -05:00
|
|
|
|
2006-04-03 16:39:25 -05:00
|
|
|
namespace = gnc_commodity_table_map_namespace(namespace);
|
2005-11-01 21:32:36 -06:00
|
|
|
ns = gnc_commodity_table_find_namespace(table, namespace);
|
2003-08-16 23:20:25 -05:00
|
|
|
if(!ns)
|
|
|
|
{
|
2007-04-04 19:24:18 -05:00
|
|
|
ns = g_object_new(GNC_TYPE_COMMODITY_NAMESPACE, NULL);
|
2005-11-01 21:32:36 -06:00
|
|
|
ns->cm_table = g_hash_table_new(g_str_hash, g_str_equal);
|
2006-04-10 17:20:12 -05:00
|
|
|
ns->name = CACHE_INSERT((gpointer)namespace);
|
2006-04-03 16:39:25 -05:00
|
|
|
ns->iso4217 = gnc_commodity_namespace_is_iso(namespace);
|
2007-04-04 19:24:18 -05:00
|
|
|
qof_instance_init_data (&ns->inst, GNC_ID_COMMODITY_NAMESPACE, book);
|
2007-04-01 17:18:50 -05:00
|
|
|
qof_event_gen (&ns->inst, QOF_EVENT_CREATE, NULL);
|
2005-11-01 21:32:36 -06:00
|
|
|
|
|
|
|
g_hash_table_insert(table->ns_table,
|
|
|
|
(gpointer) ns->name,
|
|
|
|
(gpointer) ns);
|
|
|
|
table->ns_list = g_list_append(table->ns_list, ns);
|
2007-04-01 17:18:50 -05:00
|
|
|
qof_event_gen (&ns->inst, QOF_EVENT_ADD, NULL);
|
2001-08-07 18:36:04 -05:00
|
|
|
}
|
2005-11-01 21:32:36 -06:00
|
|
|
return ns;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gnc_commodity_namespace *
|
|
|
|
gnc_commodity_table_find_namespace(const gnc_commodity_table * table,
|
|
|
|
const char * namespace)
|
|
|
|
{
|
|
|
|
if (!table || !namespace)
|
|
|
|
return NULL;
|
|
|
|
|
2006-04-03 16:39:25 -05:00
|
|
|
namespace = gnc_commodity_table_map_namespace(namespace);
|
2005-11-01 21:32:36 -06:00
|
|
|
return g_hash_table_lookup(table->ns_table, (gpointer)namespace);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gnc_commodity *
|
|
|
|
gnc_commodity_find_commodity_by_guid(const GUID *guid, QofBook *book)
|
|
|
|
{
|
|
|
|
QofCollection *col;
|
|
|
|
if (!guid || !book) return NULL;
|
|
|
|
col = qof_book_get_collection (book, GNC_ID_COMMODITY);
|
|
|
|
return (gnc_commodity *) qof_collection_lookup_entity (col, guid);
|
|
|
|
}
|
|
|
|
|
|
|
|
gnc_commodity_namespace *
|
|
|
|
gnc_commodity_find_namespace_by_guid(const GUID *guid, QofBook *book)
|
|
|
|
{
|
|
|
|
QofCollection *col;
|
|
|
|
if (!guid || !book) return NULL;
|
|
|
|
col = qof_book_get_collection (book, GNC_ID_COMMODITY_NAMESPACE);
|
|
|
|
return (gnc_commodity_namespace *) qof_collection_lookup_entity (col, guid);
|
2001-08-07 18:36:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-01 21:32:36 -06:00
|
|
|
|
2001-08-07 18:36:04 -05:00
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_table_delete_namespace
|
|
|
|
* delete a namespace
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
static int
|
2002-01-02 04:08:17 -06:00
|
|
|
ns_helper(gpointer key, gpointer value, gpointer user_data)
|
|
|
|
{
|
2001-08-07 18:36:04 -05:00
|
|
|
gnc_commodity * c = value;
|
|
|
|
gnc_commodity_destroy(c);
|
2006-04-10 17:20:12 -05:00
|
|
|
CACHE_REMOVE(key); /* key is commodity mnemonic */
|
2001-08-07 18:36:04 -05:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gnc_commodity_table_delete_namespace(gnc_commodity_table * table,
|
2002-01-02 04:08:17 -06:00
|
|
|
const char * namespace)
|
|
|
|
{
|
2003-08-16 23:20:25 -05:00
|
|
|
gnc_commodity_namespace * ns;
|
2005-11-01 21:32:36 -06:00
|
|
|
|
2003-08-16 23:20:25 -05:00
|
|
|
if (!table) return;
|
2001-08-07 18:36:04 -05:00
|
|
|
|
2005-11-01 21:32:36 -06:00
|
|
|
ns = gnc_commodity_table_find_namespace(table, namespace);
|
|
|
|
if (!ns)
|
|
|
|
return;
|
2001-08-07 18:36:04 -05:00
|
|
|
|
2007-04-01 17:18:50 -05:00
|
|
|
qof_event_gen (&ns->inst, QOF_EVENT_REMOVE, NULL);
|
2005-11-01 21:32:36 -06:00
|
|
|
g_hash_table_remove(table->ns_table, namespace);
|
|
|
|
table->ns_list = g_list_remove(table->ns_list, ns);
|
|
|
|
|
|
|
|
g_list_free(ns->cm_list);
|
|
|
|
ns->cm_list = NULL;
|
|
|
|
|
|
|
|
g_hash_table_foreach_remove(ns->cm_table, ns_helper, NULL);
|
|
|
|
g_hash_table_destroy(ns->cm_table);
|
2006-04-10 17:20:12 -05:00
|
|
|
CACHE_REMOVE(ns->name);
|
2005-11-01 21:32:36 -06:00
|
|
|
|
2007-04-01 17:18:50 -05:00
|
|
|
qof_event_gen (&ns->inst, QOF_EVENT_DESTROY, NULL);
|
2007-04-04 19:24:18 -05:00
|
|
|
/* qof_instance_release(&ns->inst); */
|
|
|
|
g_object_unref(ns);
|
2001-08-07 18:36:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_table_foreach_commodity
|
|
|
|
* call user-defined function once for every commodity in every
|
|
|
|
* namespace
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
gboolean ok;
|
|
|
|
gboolean (*func)(gnc_commodity *, gpointer);
|
|
|
|
gpointer user_data;
|
|
|
|
} IterData;
|
|
|
|
|
|
|
|
static void
|
|
|
|
iter_commodity (gpointer key, gpointer value, gpointer user_data)
|
|
|
|
{
|
|
|
|
IterData *iter_data = (IterData *) user_data;
|
|
|
|
gnc_commodity *cm = (gnc_commodity *) value;
|
|
|
|
|
|
|
|
if (iter_data->ok)
|
|
|
|
{
|
|
|
|
iter_data->ok = (iter_data->func)(cm, iter_data->user_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
iter_namespace (gpointer key, gpointer value, gpointer user_data)
|
|
|
|
{
|
2005-11-01 21:32:36 -06:00
|
|
|
GHashTable *namespace_hash = ((gnc_commodity_namespace *) value)->cm_table;
|
2001-08-07 18:36:04 -05:00
|
|
|
g_hash_table_foreach (namespace_hash, iter_commodity, user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
2003-05-10 19:45:03 -05:00
|
|
|
gnc_commodity_table_foreach_commodity (const gnc_commodity_table * tbl,
|
2001-08-07 18:36:04 -05:00
|
|
|
gboolean (*f)(gnc_commodity *, gpointer),
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
IterData iter_data;
|
|
|
|
|
2001-12-31 15:14:28 -06:00
|
|
|
if (!tbl || !f) return FALSE;
|
|
|
|
|
2001-08-07 18:36:04 -05:00
|
|
|
iter_data.ok = TRUE;
|
|
|
|
iter_data.func = f;
|
|
|
|
iter_data.user_data = user_data;
|
|
|
|
|
2005-11-01 21:32:36 -06:00
|
|
|
g_hash_table_foreach(tbl->ns_table, iter_namespace, (gpointer)&iter_data);
|
2001-08-07 18:36:04 -05:00
|
|
|
|
|
|
|
return iter_data.ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_table_destroy
|
|
|
|
* cleanup and free.
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
void
|
2001-12-31 15:14:28 -06:00
|
|
|
gnc_commodity_table_destroy(gnc_commodity_table * t)
|
|
|
|
{
|
2005-11-01 21:32:36 -06:00
|
|
|
gnc_commodity_namespace * ns;
|
|
|
|
GList *item, *next;
|
|
|
|
|
2001-08-07 18:36:04 -05:00
|
|
|
if (!t) return;
|
2003-08-10 22:06:17 -05:00
|
|
|
ENTER ("table=%p", t);
|
2001-08-07 18:36:04 -05:00
|
|
|
|
2005-11-01 21:32:36 -06:00
|
|
|
for (item = t->ns_list; item; item = next) {
|
|
|
|
next = g_list_next(item);
|
|
|
|
ns = item->data;
|
|
|
|
gnc_commodity_table_delete_namespace(t, ns->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_list_free(t->ns_list);
|
|
|
|
t->ns_list = NULL;
|
|
|
|
g_hash_table_destroy(t->ns_table);
|
|
|
|
t->ns_table = NULL;
|
2001-08-07 18:36:04 -05:00
|
|
|
g_free(t);
|
2003-08-10 22:06:17 -05:00
|
|
|
LEAVE ("table=%p", t);
|
2001-08-07 18:36:04 -05:00
|
|
|
}
|
|
|
|
|
2003-08-16 23:20:25 -05:00
|
|
|
/* =========================================================== */
|
|
|
|
|
2001-10-12 05:50:35 -05:00
|
|
|
static gboolean
|
|
|
|
table_equal_helper (gnc_commodity *cm_1, gpointer user_data)
|
|
|
|
{
|
|
|
|
gnc_commodity_table *t_2 = user_data;
|
|
|
|
gnc_commodity *cm_2;
|
|
|
|
|
|
|
|
cm_2 = gnc_commodity_table_lookup (t_2,
|
|
|
|
gnc_commodity_get_namespace (cm_1),
|
|
|
|
gnc_commodity_get_mnemonic (cm_1));
|
|
|
|
|
|
|
|
if (!cm_2)
|
|
|
|
{
|
|
|
|
PWARN ("one has commodity %s, the other does not",
|
|
|
|
gnc_commodity_get_unique_name (cm_1));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return gnc_commodity_equal (cm_1, cm_2);
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gnc_commodity_table_equal(gnc_commodity_table *t_1,
|
|
|
|
gnc_commodity_table *t_2)
|
|
|
|
{
|
|
|
|
gboolean ok;
|
|
|
|
|
|
|
|
if (t_1 == t_2) return TRUE;
|
|
|
|
if (!t_1 || !t_2) return FALSE;
|
|
|
|
|
|
|
|
ok = gnc_commodity_table_foreach_commodity (t_1, table_equal_helper, t_2);
|
|
|
|
if (!ok)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return gnc_commodity_table_foreach_commodity (t_2, table_equal_helper, t_1);
|
|
|
|
}
|
|
|
|
|
2003-08-16 23:20:25 -05:00
|
|
|
/* =========================================================== */
|
|
|
|
|
2007-12-25 23:17:37 -06:00
|
|
|
typedef struct {
|
|
|
|
gnc_commodity_table *dest;
|
|
|
|
QofBook *dest_book;
|
|
|
|
} table_copy_helper_data;
|
|
|
|
|
2003-08-16 23:20:25 -05:00
|
|
|
static gboolean
|
|
|
|
table_copy_helper (gnc_commodity *src_cm, gpointer user_data)
|
|
|
|
{
|
2007-12-25 23:17:37 -06:00
|
|
|
table_copy_helper_data *data = user_data;
|
|
|
|
gnc_commodity_table_insert (data->dest,
|
|
|
|
gnc_commodity_clone (src_cm, data->dest_book));
|
2003-08-16 23:20:25 -05:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gnc_commodity_table_copy(gnc_commodity_table *dest,
|
2007-12-25 23:17:37 -06:00
|
|
|
gnc_commodity_table *src,
|
|
|
|
QofBook *dest_book)
|
2003-08-16 23:20:25 -05:00
|
|
|
{
|
2007-12-25 23:17:37 -06:00
|
|
|
table_copy_helper_data data = {dest, dest_book};
|
|
|
|
gnc_commodity_table_foreach_commodity (src, table_copy_helper, &data);
|
2003-08-16 23:20:25 -05:00
|
|
|
}
|
|
|
|
|
2001-12-11 10:15:08 -06:00
|
|
|
/********************************************************************
|
|
|
|
* gnc_commodity_table_add_default_data
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
gboolean
|
2005-11-01 21:32:36 -06:00
|
|
|
gnc_commodity_table_add_default_data(gnc_commodity_table *table, QofBook *book)
|
2001-12-11 10:15:08 -06:00
|
|
|
{
|
2006-04-25 23:14:04 -05:00
|
|
|
QofCollection *col;
|
2008-08-01 11:02:07 -05:00
|
|
|
gnc_commodity* c;
|
2006-04-25 23:14:04 -05:00
|
|
|
|
2003-08-10 22:06:17 -05:00
|
|
|
ENTER ("table=%p", table);
|
2006-01-02 15:11:26 -06:00
|
|
|
gnc_commodity_table_add_namespace(table, GNC_COMMODITY_NS_AMEX, book);
|
|
|
|
gnc_commodity_table_add_namespace(table, GNC_COMMODITY_NS_NYSE, book);
|
|
|
|
gnc_commodity_table_add_namespace(table, GNC_COMMODITY_NS_NASDAQ, book);
|
|
|
|
gnc_commodity_table_add_namespace(table, GNC_COMMODITY_NS_EUREX, book);
|
|
|
|
gnc_commodity_table_add_namespace(table, GNC_COMMODITY_NS_MUTUAL, book);
|
2008-08-01 11:02:07 -05:00
|
|
|
gnc_commodity_table_add_namespace(table, "template", book);
|
|
|
|
c = gnc_commodity_new(book, "template", "template", "template", "template", 1);
|
|
|
|
gnc_commodity_table_insert(table, c);
|
2005-11-01 21:32:36 -06:00
|
|
|
|
|
|
|
#include "iso-4217-currencies.c"
|
|
|
|
|
2006-04-25 23:14:04 -05:00
|
|
|
/* We've just created the default namespaces and currencies. Mark
|
|
|
|
* these collections as clean because there is no USER entered data
|
|
|
|
* in these collections as of yet. */
|
|
|
|
col = qof_book_get_collection(book, GNC_ID_COMMODITY);
|
|
|
|
qof_collection_mark_clean(col);
|
|
|
|
col = qof_book_get_collection(book, GNC_ID_COMMODITY_NAMESPACE);
|
|
|
|
qof_collection_mark_clean(col);
|
|
|
|
|
2003-08-10 22:06:17 -05:00
|
|
|
LEAVE ("table=%p", table);
|
2001-12-11 10:15:08 -06:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2003-06-10 17:22:47 -05:00
|
|
|
/********************************************************************
|
|
|
|
********************************************************************/
|
2005-11-01 21:32:36 -06:00
|
|
|
/* QofObject function implementation and registration */
|
|
|
|
|
|
|
|
static QofObject commodity_object_def =
|
|
|
|
{
|
2008-11-26 08:35:36 -06:00
|
|
|
.interface_version = QOF_OBJECT_VERSION,
|
|
|
|
.e_type = GNC_ID_COMMODITY,
|
|
|
|
.type_label = "Commodity",
|
|
|
|
.book_begin = NULL,
|
|
|
|
.book_end = NULL,
|
|
|
|
.is_dirty = qof_collection_is_dirty,
|
|
|
|
.mark_clean = qof_collection_mark_clean,
|
|
|
|
.foreach = qof_collection_foreach,
|
|
|
|
.printable = (const char* (*)(gpointer)) gnc_commodity_get_fullname,
|
2005-11-01 21:32:36 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
static QofObject namespace_object_def =
|
|
|
|
{
|
2008-11-26 08:35:36 -06:00
|
|
|
.interface_version = QOF_OBJECT_VERSION,
|
|
|
|
.e_type = GNC_ID_COMMODITY_NAMESPACE,
|
|
|
|
.type_label = "Namespace",
|
|
|
|
.book_begin = NULL,
|
|
|
|
.book_end = NULL,
|
|
|
|
.is_dirty = NULL,
|
|
|
|
.mark_clean = NULL,
|
|
|
|
.foreach = NULL,
|
|
|
|
.printable = NULL,
|
2005-11-01 21:32:36 -06:00
|
|
|
};
|
2003-06-10 17:22:47 -05:00
|
|
|
|
|
|
|
static void
|
2003-06-24 19:52:46 -05:00
|
|
|
commodity_table_book_begin (QofBook *book)
|
2003-06-10 17:22:47 -05:00
|
|
|
{
|
|
|
|
gnc_commodity_table *ct;
|
2003-08-10 22:06:17 -05:00
|
|
|
ENTER ("book=%p", book);
|
2003-06-10 17:22:47 -05:00
|
|
|
|
2005-11-01 21:32:36 -06:00
|
|
|
if (gnc_commodity_table_get_table(book))
|
|
|
|
return;
|
|
|
|
|
2003-06-10 17:22:47 -05:00
|
|
|
ct = gnc_commodity_table_new ();
|
2005-11-01 21:32:36 -06:00
|
|
|
qof_book_set_data (book, GNC_COMMODITY_TABLE, ct);
|
|
|
|
|
|
|
|
if(!gnc_commodity_table_add_default_data(ct, book))
|
2003-06-10 17:22:47 -05:00
|
|
|
{
|
|
|
|
PWARN("unable to initialize book's commodity_table");
|
|
|
|
}
|
2005-11-01 21:32:36 -06:00
|
|
|
|
2003-08-10 22:06:17 -05:00
|
|
|
LEAVE ("book=%p", book);
|
2003-06-10 17:22:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2003-06-24 19:52:46 -05:00
|
|
|
commodity_table_book_end (QofBook *book)
|
2003-06-10 17:22:47 -05:00
|
|
|
{
|
2005-11-01 21:32:36 -06:00
|
|
|
gnc_commodity_table *ct;
|
|
|
|
|
|
|
|
ct = gnc_commodity_table_get_table (book);
|
|
|
|
qof_book_set_data (book, GNC_COMMODITY_TABLE, NULL);
|
|
|
|
gnc_commodity_table_destroy (ct);
|
2003-06-10 17:22:47 -05:00
|
|
|
}
|
|
|
|
|
2003-06-24 21:08:22 -05:00
|
|
|
static QofObject commodity_table_object_def =
|
2003-06-10 17:22:47 -05:00
|
|
|
{
|
2008-11-26 08:35:36 -06:00
|
|
|
.interface_version = QOF_OBJECT_VERSION,
|
|
|
|
.e_type = GNC_ID_COMMODITY_TABLE,
|
|
|
|
.type_label = "CommodityTable",
|
|
|
|
.create = NULL,
|
|
|
|
.book_begin = commodity_table_book_begin,
|
|
|
|
.book_end = commodity_table_book_end,
|
|
|
|
.is_dirty = qof_collection_is_dirty,
|
|
|
|
.mark_clean = qof_collection_mark_clean,
|
|
|
|
.foreach = NULL,
|
|
|
|
.printable = NULL,
|
|
|
|
.version_cmp = NULL,
|
2003-06-10 17:22:47 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gnc_commodity_table_register (void)
|
|
|
|
{
|
2003-07-01 22:35:20 -05:00
|
|
|
gnc_quote_source_init_tables();
|
2005-11-01 21:32:36 -06:00
|
|
|
|
|
|
|
if (!qof_object_register (&commodity_object_def))
|
|
|
|
return FALSE;
|
|
|
|
if (!qof_object_register (&namespace_object_def))
|
|
|
|
return FALSE;
|
2003-06-24 21:08:22 -05:00
|
|
|
return qof_object_register (&commodity_table_object_def);
|
2003-06-10 17:22:47 -05:00
|
|
|
}
|
|
|
|
|
2001-08-07 18:36:04 -05:00
|
|
|
/* ========================= END OF FILE ============================== */
|