Commodities
[GnuCash Engine: Core, Non-GUI Accounting Functions]


Detailed Description

A commodity is something of value that is easily tradeable or sellable; for example, currencies, stocks, bonds, grain, copper, and oil are all commodities. This file provides an API for defining a commodities, and for working with collections of commodities. All GnuCash financial transactions must identify the commodity that is being traded.

Warning:
The system used here does not follow the object handling and identification system (GUID's, Entities, etc.) that the other parts of GnuCash use. The API really should be ported over. This would allow us to get rid of the commodity table routines defined below.


Files

file  gnc-commodity.h
 Commodity handling public routines.

Commodity Quote Source functions

enum  QuoteSourceType {
  SOURCE_SINGLE = 0, SOURCE_MULTI, SOURCE_UNKNOWN, SOURCE_MAX,
  SOURCE_CURRENCY = SOURCE_MAX
}
gboolean gnc_quote_source_fq_installed (void)
void gnc_quote_source_set_fq_installed (const GList *sources_list)
gint gnc_quote_source_num_entries (QuoteSourceType type)
gnc_quote_sourcegnc_quote_source_add_new (const char *name, gboolean supported)
gnc_quote_sourcegnc_quote_source_lookup_by_internal (const char *internal_name)
gnc_quote_sourcegnc_quote_source_lookup_by_ti (QuoteSourceType type, gint index)
gboolean gnc_quote_source_get_supported (const gnc_quote_source *source)
QuoteSourceType gnc_quote_source_get_type (const gnc_quote_source *source)
gint gnc_quote_source_get_index (const gnc_quote_source *source)
const char * gnc_quote_source_get_user_name (const gnc_quote_source *source)
const char * gnc_quote_source_get_internal_name (const gnc_quote_source *source)
const char * gnc_quote_source_get_old_internal_name (const gnc_quote_source *source)

Commodity Creation

gnc_commoditygnc_commodity_new (QofBook *book, const char *fullname, const char *namespace, const char *mnemonic, const char *cusip, int fraction)
void gnc_commodity_destroy (gnc_commodity *cm)
void gnc_commodity_copy (gnc_commodity *dest, const gnc_commodity *src)
gnc_commoditygnc_commodity_clone (const gnc_commodity *src, QofBook *dest_book)

Commodity Accessor Routines - Get

const char * gnc_commodity_get_mnemonic (const gnc_commodity *cm)
const char * gnc_commodity_get_namespace (const gnc_commodity *cm)
const char * gnc_commodity_get_namespace_compat (const gnc_commodity *cm)
gnc_commodity_namespacegnc_commodity_get_namespace_ds (const gnc_commodity *cm)
const char * gnc_commodity_get_fullname (const gnc_commodity *cm)
const char * gnc_commodity_get_printname (const gnc_commodity *cm)
const char * gnc_commodity_get_cusip (const gnc_commodity *cm)
const char * gnc_commodity_get_unique_name (const gnc_commodity *cm)
int gnc_commodity_get_fraction (const gnc_commodity *cm)
gint16 gnc_commodity_get_mark (const gnc_commodity *cm)
gboolean gnc_commodity_get_quote_flag (const gnc_commodity *cm)
gnc_quote_sourcegnc_commodity_get_quote_source (const gnc_commodity *cm)
gnc_quote_sourcegnc_commodity_get_default_quote_source (const gnc_commodity *cm)
const char * gnc_commodity_get_quote_tz (const gnc_commodity *cm)

Commodity Accessor Routines - Set

void gnc_commodity_set_mnemonic (gnc_commodity *cm, const char *mnemonic)
void gnc_commodity_set_namespace (gnc_commodity *cm, const char *namespace)
void gnc_commodity_set_fullname (gnc_commodity *cm, const char *fullname)
void gnc_commodity_set_cusip (gnc_commodity *cm, const char *cusip)
void gnc_commodity_set_fraction (gnc_commodity *cm, int smallest_fraction)
void gnc_commodity_user_set_quote_flag (gnc_commodity *cm, const gboolean flag)
void gnc_commodity_set_quote_flag (gnc_commodity *cm, const gboolean flag)
void gnc_commodity_set_quote_source (gnc_commodity *cm, gnc_quote_source *src)
void gnc_commodity_set_quote_tz (gnc_commodity *cm, const char *tz)

Commodity Usage Count Adjustment Routines

void gnc_commodity_increment_usage_count (gnc_commodity *cm)
void gnc_commodity_decrement_usage_count (gnc_commodity *cm)

Commodity Comparison

gboolean gnc_commodity_equiv (const gnc_commodity *a, const gnc_commodity *b)
gboolean gnc_commodity_equal (const gnc_commodity *a, const gnc_commodity *b)

Currency Checks

gboolean gnc_commodity_namespace_is_iso (const char *namespace)
gboolean gnc_commodity_is_iso (const gnc_commodity *cm)
gboolean gnc_commodity_is_currency (const gnc_commodity *cm)

Commodity Table

gnc_commodity_tablegnc_commodity_table_get_table (QofBook *book)
gboolean gnc_commodity_table_equal (gnc_commodity_table *t_1, gnc_commodity_table *t_2)
void gnc_commodity_table_copy (gnc_commodity_table *dest, gnc_commodity_table *src, QofBook *dest_book)
#define gnc_book_get_commodity_table   gnc_commodity_table_get_table

Commodity Table Lookup functions

gnc_commoditygnc_commodity_table_lookup (const gnc_commodity_table *table, const char *namespace, const char *mnemonic)
gnc_commoditygnc_commodity_table_lookup_unique (const gnc_commodity_table *table, const char *unique_name)
gnc_commoditygnc_commodity_table_find_full (const gnc_commodity_table *t, const char *namespace, const char *fullname)
gnc_commoditygnc_commodity_find_commodity_by_guid (const GUID *guid, QofBook *book)
gnc_commodity_namespacegnc_commodity_find_namespace_by_guid (const GUID *guid, QofBook *book)

Commodity Table Maintenance functions

gnc_commoditygnc_commodity_table_insert (gnc_commodity_table *table, gnc_commodity *comm)
void gnc_commodity_table_remove (gnc_commodity_table *table, gnc_commodity *comm)
gboolean gnc_commodity_table_add_default_data (gnc_commodity_table *table, QofBook *book)

Commodity Table Namespace functions

const char * gnc_commodity_namespace_get_name (const gnc_commodity_namespace *ns)
GList * gnc_commodity_namespace_get_commodity_list (const gnc_commodity_namespace *ns)
guint gnc_commodity_table_get_number_of_namespaces (const gnc_commodity_table *tbl)
int gnc_commodity_table_has_namespace (const gnc_commodity_table *table, const char *namespace)
GList * gnc_commodity_table_get_namespaces (const gnc_commodity_table *t)
GList * gnc_commodity_table_get_namespaces_list (const gnc_commodity_table *t)
gnc_commodity_namespacegnc_commodity_table_add_namespace (gnc_commodity_table *table, const char *namespace, QofBook *book)
gnc_commodity_namespacegnc_commodity_table_find_namespace (const gnc_commodity_table *table, const char *namespace)
void gnc_commodity_table_delete_namespace (gnc_commodity_table *table, const char *namespace)

Commodity Table Accessor functions

guint gnc_commodity_table_get_size (const gnc_commodity_table *tbl)
CommodityListgnc_commodity_table_get_commodities (const gnc_commodity_table *table, const char *namespace)
CommodityListgnc_commodity_table_get_quotable_commodities (const gnc_commodity_table *table)
gboolean gnc_commodity_table_foreach_commodity (const gnc_commodity_table *table, gboolean(*f)(gnc_commodity *cm, gpointer user_data), gpointer user_data)

Commodity Table Private/Internal-Use Only Routines

void gnc_commodity_set_mark (gnc_commodity *cm, gint16 mark)
gnc_commodity_tablegnc_commodity_table_new (void)
void gnc_commodity_table_destroy (gnc_commodity_table *table)
gnc_commoditygnc_commodity_obtain_twin (const gnc_commodity *from, QofBook *book)
gboolean gnc_commodity_table_register (void)
void gnc_commodity_begin_edit (gnc_commodity *cm)
void gnc_commodity_commit_edit (gnc_commodity *cm)

Defines

#define GNC_TYPE_COMMODITY   (gnc_commodity_get_type ())
#define GNC_COMMODITY(o)   (G_TYPE_CHECK_INSTANCE_CAST ((o), GNC_TYPE_COMMODITY, gnc_commodity))
#define GNC_COMMODITY_CLASS(k)   (G_TYPE_CHECK_CLASS_CAST((k), GNC_TYPE_COMMODITY, gnc_commodityClass))
#define GNC_IS_COMMODITY(o)   (G_TYPE_CHECK_INSTANCE_TYPE ((o), GNC_TYPE_COMMODITY))
#define GNC_IS_COMMODITY_CLASS(k)   (G_TYPE_CHECK_CLASS_TYPE ((k), GNC_TYPE_COMMODITY))
#define GNC_COMMODITY_GET_CLASS(o)   (G_TYPE_INSTANCE_GET_CLASS ((o), GNC_TYPE_COMMODITY, gnc_commodityClass))
#define GNC_TYPE_COMMODITY_NAMESPACE   (gnc_commodity_namespace_get_type ())
#define GNC_COMMODITY_NAMESPACE(o)   (G_TYPE_CHECK_INSTANCE_CAST ((o), GNC_TYPE_COMMODITY_NAMESPACE, gnc_commodity_namespace))
#define GNC_COMMODITY_NAMESPACE_CLASS(k)   (G_TYPE_CHECK_CLASS_CAST((k), GNC_TYPE_COMMODITY_NAMESPACE, gnc_commodity_namespaceClass))
#define GNC_IS_COMMODITY_NAMESPACE(o)   (G_TYPE_CHECK_INSTANCE_TYPE ((o), GNC_TYPE_COMMODITY_NAMESPACE))
#define GNC_IS_COMMODITY_NAMESPACE_CLASS(k)   (G_TYPE_CHECK_CLASS_TYPE ((k), GNC_TYPE_COMMODITY_NAMESPACE))
#define GNC_COMMODITY_NAMESPACE_GET_CLASS(o)   (G_TYPE_INSTANCE_GET_CLASS ((o), GNC_TYPE_COMMODITY_NAMESPACE, gnc_commodity_namespaceClass))
#define GNC_COMMODITY_TABLE   "gnc_commodity_table"
#define GNC_COMMODITY_NS_LEGACY   "GNC_LEGACY_CURRENCIES"
#define GNC_COMMODITY_NS_ISO   "ISO4217"
#define GNC_COMMODITY_NS_CURRENCY   "CURRENCY"
#define GNC_COMMODITY_NS_NASDAQ   "NASDAQ"
#define GNC_COMMODITY_NS_NYSE   "NYSE"
#define GNC_COMMODITY_NS_EUREX   "EUREX"
#define GNC_COMMODITY_NS_MUTUAL   "FUND"
#define GNC_COMMODITY_NS_AMEX   "AMEX"
#define GNC_COMMODITY_NS_ASX   "ASX"

Typedefs

typedef _GncCommodityClass gnc_commodityClass
typedef _GncCommodityNamespaceClass gnc_commodity_namespaceClass
typedef GList CommodityList

Functions

GType gnc_commodity_get_type (void)
GType gnc_commodity_namespace_get_type (void)


Define Documentation

#define GNC_COMMODITY_NS_LEGACY   "GNC_LEGACY_CURRENCIES"

The commodity namespace definitions are used to tag a commodity by its type, or a stocks by the exchange where it is traded.

The LEGACY name is only used by the file i/o routines, and is converted to another commodity namespace before it is seen by the rest of the system. The ISO namespace represents currencies. With the exception of the NASDAQ namespace (which is used once in the binary importer) the rest of the namespace declarations are only used to populate an option menu in the commodity selection window.

Definition at line 97 of file gnc-commodity.h.


Enumeration Type Documentation

enum QuoteSourceType

The quote source type enum account types are used to determine how the transaction data in the account is displayed. These values can be safely changed from one release to the next.

Enumerator:
SOURCE_SINGLE  This quote source pulls from a single specific web site. For example, the yahoo_australia source only pulls from the yahoo web site.
SOURCE_MULTI  This quote source may pull from multiple web sites. For example, the australia source may pull from ASX, yahoo, etc.
SOURCE_UNKNOWN  This is a locally installed quote source that gnucash knows nothing about. May pull from single or multiple locations.
SOURCE_MAX 
SOURCE_CURRENCY  The special currency quote source.

Definition at line 118 of file gnc-commodity.h.

00119 {
00120   SOURCE_SINGLE = 0,    
00124   SOURCE_MULTI,         
00127   SOURCE_UNKNOWN,       
00131   SOURCE_MAX,
00132   SOURCE_CURRENCY = SOURCE_MAX, 
00133 } QuoteSourceType;


Function Documentation

gnc_commodity* gnc_commodity_clone ( const gnc_commodity src,
QofBook dest_book 
)

allocate and copy

Definition at line 661 of file gnc-commodity.c.

00662 {
00663   gnc_commodity * dest = g_object_new(GNC_TYPE_COMMODITY, NULL);
00664   qof_instance_init_data (&dest->inst, GNC_ID_COMMODITY, dest_book);
00665 
00666   dest->fullname = CACHE_INSERT(src->fullname);
00667   dest->mnemonic = CACHE_INSERT(src->mnemonic);
00668   dest->cusip = CACHE_INSERT(src->cusip);
00669   dest->quote_tz = CACHE_INSERT(src->quote_tz);
00670 
00671   dest->namespace = src->namespace;
00672 
00673   dest->mark = 0;
00674   dest->fraction = src->fraction;
00675   dest->quote_flag = src->quote_flag;
00676 
00677   gnc_commodity_set_quote_source (dest, gnc_commodity_get_quote_source (src));
00678 
00679   kvp_frame_delete (dest->inst.kvp_data);
00680   dest->inst.kvp_data = kvp_frame_copy (src->inst.kvp_data);
00681 
00682   reset_printname(dest);
00683   reset_unique_name(dest);
00684 
00685   return dest;
00686 }

void gnc_commodity_copy ( gnc_commodity dest,
const gnc_commodity src 
)

Copy src into dest

Definition at line 647 of file gnc-commodity.c.

00648 {
00649   gnc_commodity_set_fullname (dest, src->fullname);
00650   dest->namespace = src->namespace;
00651   gnc_commodity_set_fraction (dest, src->fraction);
00652   gnc_commodity_set_cusip (dest, src->cusip);
00653   gnc_commodity_set_quote_flag (dest, src->quote_flag);
00654   gnc_commodity_set_quote_source (dest, gnc_commodity_get_quote_source (src));
00655   gnc_commodity_set_quote_tz (dest, src->quote_tz);
00656   kvp_frame_delete (dest->inst.kvp_data);
00657   dest->inst.kvp_data = kvp_frame_copy (src->inst.kvp_data);
00658 }

void gnc_commodity_decrement_usage_count ( gnc_commodity cm  ) 

Decrement a commodity's internal counter that tracks how many accounts are using that commodity. For currencies, this may have the side effect of disabling the commodity's quote flag.

Parameters:
cm A pointer to a commodity data structure.

Definition at line 1109 of file gnc-commodity.c.

01110 {
01111   const char *str;
01112 
01113   ENTER("(cm=%p)", cm);
01114 
01115   if(!cm) {
01116     LEAVE("");
01117     return;
01118   }
01119 
01120   if(cm->usage_count == 0) {
01121     PWARN("usage_count already zero");
01122     LEAVE("");
01123     return;
01124   }
01125 
01126   cm->usage_count--;
01127   if((cm->usage_count == 0) && cm->quote_flag
01128       && gnc_commodity_get_auto_quote_control_flag(cm)
01129       && gnc_commodity_is_iso(cm)) {
01130     /* if this is a currency with auto quote control enabled and no more
01131      * accounts reference this currency, disable quote retrieval */
01132     gnc_commodity_set_quote_flag(cm, FALSE);
01133   }
01134   LEAVE("(usage_count=%d)", cm->usage_count);
01135 }

void gnc_commodity_destroy ( gnc_commodity cm  ) 

Destroy a commodity. Release all memory attached to this data structure.

Note:
This function does not (can not) check to see if the commodity is referenced anywhere.
Parameters:
cm The commodity to destroy.

Definition at line 600 of file gnc-commodity.c.

00601 {
00602   QofBook *book;
00603   gnc_commodity_table *table;
00604   if(!cm) return;
00605 
00606   book = qof_instance_get_book(&cm->inst);
00607   table = gnc_commodity_table_get_table(book);
00608   gnc_commodity_table_remove(table, cm);
00609 
00610   qof_event_gen (&cm->inst, QOF_EVENT_DESTROY, NULL);
00611 
00612   /* Set at creation */
00613   CACHE_REMOVE (cm->fullname);
00614   CACHE_REMOVE (cm->cusip);
00615   CACHE_REMOVE (cm->mnemonic);
00616   CACHE_REMOVE (cm->quote_tz);
00617   cm->namespace = NULL;
00618 
00619   /* Set through accessor functions */
00620   cm->quote_source = NULL;
00621 
00622   /* Automatically generated */
00623   g_free(cm->printname);
00624   cm->printname = NULL;
00625 
00626   g_free(cm->unique_name);
00627   cm->unique_name = NULL;
00628 
00629   cm->mark = 0;
00630 
00631 #ifdef ACCOUNTS_CLEANED_UP
00632   /* Account objects are not actually cleaned up when a book is closed (in fact
00633    * a memory leak), but commodities are, so in currently this warning gets hit
00634    * quite frequently.  Disable the check until cleaning up of accounts objects
00635    * on close is implemented.  */
00636   if(cm->usage_count != 0) {
00637       PWARN("Destroying commodity (%p) with non-zero usage_count (%d).", cm,
00638               cm->usage_count);
00639   }
00640 #endif
00641 
00642   /* qof_instance_release (&cm->inst); */
00643   g_object_unref(cm);
00644 }

gboolean gnc_commodity_equal ( const gnc_commodity a,
const gnc_commodity b 
)

This routine returns TRUE if the two commodities are equal. Commodities are equal if they have the same namespace, mnemonic, fullname, exchange private code and fraction.

Definition at line 1157 of file gnc-commodity.c.

01158 {
01159   if (a == b) return TRUE;
01160 
01161   if (!a || !b)
01162   {
01163     DEBUG ("one is NULL");
01164     return FALSE;
01165   }
01166 
01167   if (a->namespace != b->namespace)
01168   {
01169     DEBUG ("namespaces differ: %p(%s) vs %p(%s)",
01170            a->namespace, gnc_commodity_namespace_get_name(a->namespace),
01171            b->namespace, gnc_commodity_namespace_get_name(b->namespace));
01172     return FALSE;
01173   }
01174 
01175   if (safe_strcmp(a->mnemonic, b->mnemonic) != 0)
01176   {
01177     DEBUG ("mnemonics differ: %s vs %s", a->mnemonic, b->mnemonic);
01178     return FALSE;
01179   }
01180 
01181   if (safe_strcmp(a->fullname, b->fullname) != 0)
01182   {
01183     DEBUG ("fullnames differ: %s vs %s", a->fullname, b->fullname);
01184     return FALSE;
01185   }
01186 
01187   if (safe_strcmp(a->cusip, b->cusip) != 0)
01188   {
01189     DEBUG ("cusips differ: %s vs %s", a->cusip, b->cusip);
01190     return FALSE;
01191   }
01192 
01193   if (a->fraction != b->fraction)
01194   {
01195     DEBUG ("fractions differ: %d vs %d", a->fraction, b->fraction);
01196     return FALSE;
01197   }
01198 
01199   return TRUE;
01200 }

gboolean gnc_commodity_equiv ( const gnc_commodity a,
const gnc_commodity b 
)

This routine returns TRUE if the two commodities are equivalent. Commodities are equivalent if they have the same namespace and mnemonic. Equivalent commodities may belong to different exchanges, may have different fullnames, and may have different fractions.

Definition at line 1147 of file gnc-commodity.c.

01148 {
01149   if(a == b) return TRUE;
01150   if(!a || !b) return FALSE;
01151   if(a->namespace != b->namespace) return FALSE;
01152   if(safe_strcmp(a->mnemonic, b->mnemonic) != 0) return FALSE;
01153   return TRUE;
01154 }

const char* gnc_commodity_get_cusip ( const gnc_commodity cm  ) 

Retrieve the 'exchange code' for the specified commodity. This will be a pointer to a null terminated string of the form "AXQ14728", etc. This field is often used when presenting information to the user.

Note:
This is a unique code that specifies a particular item or set of shares of a commodity, not a code that specifies a stock exchange. That is the namespace field.
Parameters:
cm A pointer to a commodity data structure.
Returns:
A pointer to the exchange code for this commodity. This string is owned by the engine and should not be freed by the caller.

Definition at line 769 of file gnc-commodity.c.

00770 {
00771   if(!cm) return NULL;
00772   return cm->cusip;
00773 }

int gnc_commodity_get_fraction ( const gnc_commodity cm  ) 

Retrieve the fraction for the specified commodity. This will be an integer value specifying the number of fractional units that one of these commodities can be divided into. Should always be a power of 10.

Parameters:
cm A pointer to a commodity data structure.
Returns:
The number of fractional units that one of these commodities can be divided into.

Definition at line 780 of file gnc-commodity.c.

00781 {
00782   if(!cm) return 0;
00783   return cm->fraction;
00784 }

const char* gnc_commodity_get_fullname ( const gnc_commodity cm  ) 

Retrieve the full name for the specified commodity. This will be a pointer to a null terminated string of the form "Acme Systems, Inc.", etc.

Parameters:
cm A pointer to a commodity data structure.
Returns:
A pointer to the full name for this commodity. This string is owned by the engine and should not be freed by the caller.

Definition at line 745 of file gnc-commodity.c.

00746 {
00747   if(!cm) return NULL;
00748   return cm->fullname;
00749 }

gint16 gnc_commodity_get_mark ( const gnc_commodity cm  ) 

Retrieve the 'mark' field for the specified commodity.

Note:
This is a private field used by the Postgres back end.
Parameters:
cm A pointer to a commodity data structure.
Returns:
The value of the mark field.

Definition at line 791 of file gnc-commodity.c.

00792 {
00793   if(!cm) return 0;
00794   return cm->mark;
00795 }

const char* gnc_commodity_get_mnemonic ( const gnc_commodity cm  ) 

Retrieve the mnemonic for the specified commodity. This will be a pointer to a null terminated string of the form "ACME", "QWER", etc.

Parameters:
cm A pointer to a commodity data structure.
Returns:
A pointer to the mnemonic for this commodity. This string is owned by the engine and should not be freed by the caller.

Definition at line 693 of file gnc-commodity.c.

00694 {
00695   if(!cm) return NULL;
00696   return cm->mnemonic;
00697 }

const char* gnc_commodity_get_namespace ( const gnc_commodity cm  ) 

Retrieve the namespace for the specified commodity. This will be a pointer to a null terminated string of the form "AMEX", "NASDAQ", etc.

Parameters:
cm A pointer to a commodity data structure.
Returns:
A pointer to the namespace for this commodity. This string is owned by the engine and should not be freed by the caller.

Definition at line 716 of file gnc-commodity.c.

00717 {
00718   if(!cm) return NULL;
00719   return gnc_commodity_namespace_get_name(cm->namespace);
00720 }

const char* gnc_commodity_get_namespace_compat ( const gnc_commodity cm  ) 

Retrieve the namespace for the specified commodity. This will be a pointer to a null terminated string of the form "AMEX", "NASDAQ", etc. The only difference between function and gnc_commodity_get_namespace() is that this function returns ISO4217 instead of CURRENCY for backward compatability with the 1.8 data files.

Parameters:
cm A pointer to a commodity data structure.
Returns:
A pointer to the namespace for this commodity. This string is owned by the engine and should not be freed by the caller.

Definition at line 723 of file gnc-commodity.c.

00724 {
00725   if (!cm || !cm->namespace) return NULL;
00726   if (cm->namespace->iso4217) {
00727     /* Data files are still written with ISO4217. */
00728     return GNC_COMMODITY_NS_ISO;
00729   }
00730   return gnc_commodity_namespace_get_name(cm->namespace);
00731 }

gnc_commodity_namespace* gnc_commodity_get_namespace_ds ( const gnc_commodity cm  ) 

Retrieve the namespace data strucure for the specified commodity. This will be a pointer to another data structure.

Parameters:
cm A pointer to a commodity data structure.
Returns:
A pointer to the namespace data structure for this commodity.

Definition at line 734 of file gnc-commodity.c.

00735 {
00736   if(!cm) return NULL;
00737   return cm->namespace;
00738 }

const char* gnc_commodity_get_printname ( const gnc_commodity cm  ) 

Retrieve the 'print' name for the specified commodity. This will be a pointer to a null terminated string of the form "Acme Systems, Inc. (ACME)", etc.

Parameters:
cm A pointer to a commodity data structure.
Returns:
A pointer to the print name for this commodity. This string is owned by the engine and should not be freed by the caller.

Definition at line 704 of file gnc-commodity.c.

00705 {
00706   if(!cm) return NULL;
00707   return cm->printname;
00708 }

gboolean gnc_commodity_get_quote_flag ( const gnc_commodity cm  ) 

Retrieve the automatic price quote flag for the specified commodity. This flag indicates whether stock quotes should be retrieved for the specified stock.

Parameters:
cm A pointer to a commodity data structure.
Returns:
TRUE if quotes should be pulled for this commodity, FALSE otherwise.

Definition at line 817 of file gnc-commodity.c.

00818 {
00819   if(!cm) return FALSE;
00820   return (cm->quote_flag);
00821 }

gnc_quote_source* gnc_commodity_get_quote_source ( const gnc_commodity cm  ) 

Retrieve the automatic price quote source for the specified commodity. This will be a pointer to a null terminated string of the form "Yahoo (Asia)", etc.

Parameters:
cm A pointer to a commodity data structure.
Returns:
A pointer to the price quote source for this commodity.

Definition at line 828 of file gnc-commodity.c.

00829 {
00830   if(!cm) return NULL;
00831   if (!cm->quote_source && gnc_commodity_is_iso(cm))
00832     return &currency_quote_source;
00833   return cm->quote_source;
00834 }

const char* gnc_commodity_get_quote_tz ( const gnc_commodity cm  ) 

Retrieve the automatic price quote timezone for the specified commodity. This will be a pointer to a null terminated string of the form "America/New_York", etc.

Parameters:
cm A pointer to a commodity data structure.
Returns:
A pointer to the price quote timezone for this commodity. This string is owned by the engine and should not be freed by the caller.

Definition at line 850 of file gnc-commodity.c.

00851 {
00852   if(!cm) return NULL;
00853   return cm->quote_tz;
00854 }

const char* gnc_commodity_get_unique_name ( const gnc_commodity cm  ) 

Retrieve the 'unique' name for the specified commodity. This will be a pointer to a null terminated string of the form "AMEX::ACME", etc. This field is often used when performing comparisons or other functions invisible to the user.

Parameters:
cm A pointer to a commodity data structure.
Returns:
A pointer to the 'unique' name for this commodity. This string is owned by the engine and should not be freed by the caller.

Definition at line 757 of file gnc-commodity.c.

00758 {
00759   if(!cm) return NULL;
00760   return cm->unique_name;
00761 }

void gnc_commodity_increment_usage_count ( gnc_commodity cm  ) 

Increment a commodity's internal counter that tracks how many accounts are using that commodity. For currencies, this may have the side effect of enabling the commodity's quote flag.

Parameters:
cm A pointer to a commodity data structure.

Definition at line 1078 of file gnc-commodity.c.

01079 {
01080   const char *str;
01081 
01082   ENTER("(cm=%p)", cm);
01083 
01084   if(!cm) {
01085     LEAVE("");
01086     return;
01087   }
01088 
01089   if((cm->usage_count == 0) && !cm->quote_flag
01090       && gnc_commodity_get_auto_quote_control_flag(cm)
01091       && gnc_commodity_is_iso(cm)) {
01092     /* compatability hack - Gnucash 1.8 gets currency quotes when a
01093        non-default currency is assigned to an account.  */
01094     gnc_commodity_begin_edit(cm);
01095     gnc_commodity_set_quote_flag(cm, TRUE);
01096     gnc_commodity_set_quote_source(cm,
01097         gnc_commodity_get_default_quote_source(cm));
01098     gnc_commodity_commit_edit(cm);
01099   }
01100   cm->usage_count++;
01101   LEAVE("(usage_count=%d)", cm->usage_count);
01102 }

gboolean gnc_commodity_is_currency ( const gnc_commodity cm  ) 

Checks to see if the specified commodity is an ISO 4217 recognized currency or a legacy currency.

Parameters:
cm The commodity to check.
Returns:
TRUE if the commodity represents a currency, FALSE otherwise.

Definition at line 1590 of file gnc-commodity.c.

01591 {
01592     const char *ns_name;
01593     if (!cm) return FALSE;
01594 
01595     ns_name = gnc_commodity_namespace_get_name(cm->namespace);
01596     return (!safe_strcmp(ns_name, GNC_COMMODITY_NS_LEGACY) ||
01597             !safe_strcmp(ns_name, GNC_COMMODITY_NS_CURRENCY));
01598 }

gboolean gnc_commodity_is_iso ( const gnc_commodity cm  ) 

Checks to see if the specified commodity is an ISO 4217 recognized currency.

Parameters:
cm The commodity to check.
Returns:
TRUE if the commodity represents a currency, FALSE otherwise.

Definition at line 1583 of file gnc-commodity.c.

01584 {
01585   if (!cm || !cm->namespace) return FALSE;
01586   return cm->namespace->iso4217;
01587 }

GList* gnc_commodity_namespace_get_commodity_list ( const gnc_commodity_namespace ns  ) 

Return a list of all commodity data structures in the specified namespace.

Returns:
A pointer to the list of structures. NULL if an invalid argument was supplied.
Note:
This list is owned by the engine. The caller must not free the list.

Definition at line 1215 of file gnc-commodity.c.

01216 {
01217   if (!namespace)
01218     return NULL;
01219 
01220   return namespace->cm_list;
01221 }

const char* gnc_commodity_namespace_get_name ( const gnc_commodity_namespace ns  ) 

Return the textual name of a namespace data strucure.

Parameters:
ns A pointer to the namespace data strucure.
Returns:
A pointer to the name of the namespace. This string is owned by the engine and should not be freed by the caller.

Definition at line 1207 of file gnc-commodity.c.

01208 {
01209   if (ns == NULL)
01210     return NULL;
01211   return ns->name;
01212 }

gboolean gnc_commodity_namespace_is_iso ( const char *  namespace  ) 

Checks to see if the specified commodity namespace is the namespace for ISO 4217 currencies.

Parameters:
namespace The string to check.
Returns:
TRUE if the string indicates an ISO currency, FALSE otherwise.

Definition at line 1224 of file gnc-commodity.c.

01225 {
01226   return ((safe_strcmp(namespace, GNC_COMMODITY_NS_ISO) == 0) ||
01227           (safe_strcmp(namespace, GNC_COMMODITY_NS_CURRENCY) == 0));
01228 }

gnc_commodity* gnc_commodity_new ( QofBook book,
const char *  fullname,
const char *  namespace,
const char *  mnemonic,
const char *  cusip,
int  fraction 
)

Create a new commodity. This function allocates a new commodity data structure, populates it with the data provided, and then generates the dynamic names that exist as part of a commodity.

Note:
This function does not check to see if the commodity exists before adding a new commodity.
Parameters:
book The book that the new commodity will belong to.
fullname The complete name of this commodity. E.G. "Acme Systems, Inc."
namespace An aggregation of commodities. E.G. ISO4217, Nasdaq, Downbelow, etc.
mnemonic An abbreviation for this stock. For publicly traced stocks, this field should contain the stock ticker symbol. This field is used to get online price quotes, so it must match the stock ticker symbol used by the exchange where you want to get automatic stock quote updates. E.G. ACME, ACME.US, etc.
cusip A string containing the CUSIP code or similar UNIQUE code for this commodity. The stock ticker is NOT appropriate as that goes in the mnemonic field.
fraction The smallest division of this commodity allowed. I.E. If this is 1, then the commodity must be traded in whole units; if 100 then the commodity may be traded in 0.01 units, etc.
Returns:
A pointer to the new commodity.

Definition at line 559 of file gnc-commodity.c.

00562 {
00563   gnc_commodity * retval = g_object_new(GNC_TYPE_COMMODITY, NULL);
00564   gnc_commodity_table *table;
00565 
00566   qof_instance_init_data (&retval->inst, GNC_ID_COMMODITY, book);
00567   table = gnc_commodity_table_get_table(book);
00568   if (namespace) {
00569     retval->namespace = gnc_commodity_table_find_namespace(table, namespace);
00570     if (!retval->namespace)
00571       retval->namespace = gnc_commodity_table_add_namespace(table, namespace, book);
00572   } else {
00573     retval->namespace = NULL;
00574   }
00575   
00576   retval->fullname = CACHE_INSERT(fullname);
00577   retval->mnemonic = CACHE_INSERT(mnemonic);
00578   retval->cusip = CACHE_INSERT(cusip);
00579   retval->fraction = fraction;
00580   retval->mark = 0;
00581   retval->quote_flag = 0;
00582   retval->quote_source = NULL;
00583   retval->quote_tz = CACHE_INSERT("");
00584 
00585   reset_printname(retval);
00586   reset_unique_name(retval);
00587   if (gnc_commodity_namespace_is_iso(namespace))
00588     retval->quote_source = gnc_quote_source_lookup_by_internal("currency");
00589   qof_event_gen (&retval->inst, QOF_EVENT_CREATE, NULL);
00590 
00591   return retval;
00592 }

gnc_commodity* gnc_commodity_obtain_twin ( const gnc_commodity from,
QofBook book 
)

Given the commodity 'from', this routine will find and return the equivalent commodity (commodity with the same 'unique name') in the indicated book. This routine is primarily useful for setting up clones of things across multiple books.

Definition at line 1264 of file gnc-commodity.c.

01265 {
01266   gnc_commodity *twin;
01267   const char * ucom;
01268   gnc_commodity_table * comtbl;
01269 
01270   if (!from) return NULL;
01271   comtbl = gnc_commodity_table_get_table (book);
01272   if (!comtbl) return NULL;
01273 
01274   ucom = gnc_commodity_get_unique_name (from);
01275   twin = gnc_commodity_table_lookup_unique (comtbl, ucom);
01276   if (!twin)
01277   {
01278     twin = gnc_commodity_clone (from, book);
01279     twin = gnc_commodity_table_insert (comtbl, twin);
01280   }
01281   return twin;
01282 }

void gnc_commodity_set_cusip ( gnc_commodity cm,
const char *  cusip 
)

Set the 'exchange code' for the specified commodity. This should be a pointer to a null terminated string of the form "AXQ14728", etc.

Note:
This is a unique code that specifies a particular item or set of shares of a commodity, not a code that specifies a stock exchange. That is the namespace field.
Parameters:
cm A pointer to a commodity data structure.
cusip A pointer to the cusip or other exchange specific data for this commodity. This string belongs to the caller and will be duplicated by the engine.

Definition at line 928 of file gnc-commodity.c.

00930 {
00931   if(!cm) return;
00932   if(cm->cusip == cusip) return;
00933 
00934   gnc_commodity_begin_edit(cm);
00935   CACHE_REMOVE (cm->cusip);
00936   cm->cusip = CACHE_INSERT (cusip);
00937   mark_commodity_dirty(cm);
00938