GnuCash fec5643+
Loading...
Searching...
No Matches
Macros | Typedefs | Enumerations

Each price in the database represents an "instantaneous" quote for a given commodity with respect to another commodity. More...

Macros

#define PRICE_TYPE_LAST   "last"
 
#define PRICE_TYPE_UNK   "unknown"
 
#define PRICE_TYPE_TRN   "transaction"
 

Typedefs

typedef GList PriceList
 

Enumerations

enum  PriceSource {
  PRICE_SOURCE_EDIT_DLG , PRICE_SOURCE_FQ , PRICE_SOURCE_USER_PRICE , PRICE_SOURCE_XFER_DLG_VAL ,
  PRICE_SOURCE_SPLIT_REG , PRICE_SOURCE_SPLIT_IMPORT , PRICE_SOURCE_STOCK_SPLIT , PRICE_SOURCE_STOCK_TRANSACTION ,
  PRICE_SOURCE_INVOICE , PRICE_SOURCE_TEMP , PRICE_SOURCE_INVALID
}
 Price source enum. More...
 

Constructors

GNCPrice * gnc_price_create (QofBook *book)
 gnc_price_create - returns a newly allocated and initialized price with a reference count of 1.
 
GNCPrice * gnc_price_clone (GNCPrice *p, QofBook *book)
 gnc_price_clone - returns a newly allocated price that's a content-wise duplicate of the given price, p.
 
GNCPrice * gnc_price_invert (GNCPrice *p)
 Return a newly-allocated price that's the inverse of the given price, p.
 

Memory Management

void gnc_price_ref (GNCPrice *p)
 gnc_price_ref - indicate your need for a given price to stick around (i.e.
 
void gnc_price_unref (GNCPrice *p)
 gnc_price_unref - indicate you're finished with a price (i.e.
 

Setters

All of the setters store copies of the data given, with the exception of the commodity field which just stores the pointer given.

It is assumed that commodities are a global resource and are pointer unique.

Invocations of the setters should be wrapped with calls to gnc_price_begin_edit() and commit_edit(). The begin/commit calls help ensure that the local price db is synchronized with the backend.

void gnc_price_begin_edit (GNCPrice *p)
 
void gnc_price_commit_edit (GNCPrice *p)
 
void gnc_price_set_commodity (GNCPrice *p, gnc_commodity *c)
 
void gnc_price_set_currency (GNCPrice *p, gnc_commodity *c)
 
void gnc_price_set_time64 (GNCPrice *p, time64 t)
 
void gnc_price_set_source (GNCPrice *p, PriceSource source)
 
void gnc_price_set_source_string (GNCPrice *p, const char *s)
 
void gnc_price_set_typestr (GNCPrice *p, const char *type)
 
void gnc_price_set_value (GNCPrice *p, gnc_numeric value)
 

Getters

All of the getters return data that's internal to the GNCPrice, not copies, so don't free these values.

GNCPrice * gnc_price_lookup (const GncGUID *guid, QofBook *book)
 
gnc_commodity * gnc_price_get_commodity (const GNCPrice *p)
 
gnc_commodity * gnc_price_get_currency (const GNCPrice *p)
 
time64 gnc_price_get_time64 (const GNCPrice *p)
 
PriceSource gnc_price_get_source (const GNCPrice *p)
 
const char * gnc_price_get_source_string (const GNCPrice *p)
 
const char * gnc_price_get_typestr (const GNCPrice *p)
 
gnc_numeric gnc_price_get_value (const GNCPrice *p)
 
gboolean gnc_price_equal (const GNCPrice *p1, const GNCPrice *p2)
 
#define gnc_price_get_guid(X)   qof_entity_get_guid(QOF_INSTANCE(X))
 
#define gnc_price_return_guid(X)   (*(qof_entity_get_guid(QOF_INSTANCE(X))))
 
#define gnc_price_get_book(X)   qof_instance_get_book(QOF_INSTANCE(X))
 

Internal/Debugging

void gnc_price_print (GNCPrice *db, FILE *f, int indent)
 This simple function can be useful for debugging the price code.
 
void gnc_pricedb_print_contents (GNCPriceDB *db, FILE *f)
 This simple function can be useful for debugging the pricedb code.
 

GNCPrice lists

The database communicates multiple prices in and out via gnc price lists.

These are just time sorted GLists of GNCPrice pointers. Functions for manipulating these lists are provided. These functions are helpful in that they handle maintaining proper reference counts on behalf of the price list for every price being held in a given list. I.e. insert "refs" the prices being inserted, remove and destroy "unref" the prices that will no longer be referred to by the list.

gboolean gnc_price_list_insert (PriceList **prices, GNCPrice *p, gboolean check_dupl)
 gnc_price_list_insert - insert a price into the given list, calling gnc_price_ref on it during the process.
 
gboolean gnc_price_list_remove (PriceList **prices, GNCPrice *p)
 gnc_price_list_remove - remove the price, p, from the given list, calling gnc_price_unref on it during the process.
 
void gnc_price_list_destroy (PriceList *prices)
 gnc_price_list_destroy - destroy the given price list, calling gnc_price_unref on all the prices included in the list.
 
gboolean gnc_price_list_equal (PriceList *prices1, PriceList *prices2)
 

Denominator Constants Price policy: In order to avoid rounding

problems, currency prices (often called exchange rates) are saved in terms of the smaller currency, so that price > 1, with a fixed denominator of 1/1000.

Commodity prices in currency are always expressed as value per unit of the commodity with a fixed denominator of the pricing currency's SCU * 10000.

#define CURRENCY_DENOM   10000
 
#define COMMODITY_DENOM_MULT   10000
 

Detailed Description

Each price in the database represents an "instantaneous" quote for a given commodity with respect to another commodity.

For example, a given price might represent the value of LNUX in USD on 2001-02-03.

Fields:
Implementation Details:
Note
For source and type, NULL and the empty string are considered the same, so if one of these is "", then after a file save/restore, it might be NULL. Behave accordingly.

GNCPrices are reference counted. When you gnc_price_create one or clone it, the new price's count is set to 1. When you are finished with a price, call gnc_price_unref. If you hand the price pointer to some other code that needs to keep it, make sure it calls gnc_price_ref to indicate its interest in that price, and calls gnc_price_unref when it's finished with the price. For those unfamiliar with reference counting, basically each price stores an integer count which starts at 1 and is incremented every time someone calls gnc_price_ref. Conversely, the count is decremented every time someone calls gnc_price_unref. If the count ever reaches 0, the price is destroyed.

All of the getters return data that's internal to the GNCPrice, not copies, so don't free these values.

All of the setters store copies of the data given, with the exception of the commodity field which just stores the pointer given. It is assumed that commodities are a global resource and are pointer unique.

Macro Definition Documentation

◆ COMMODITY_DENOM_MULT

#define COMMODITY_DENOM_MULT   10000

Definition at line 286 of file gnc-pricedb.h.

◆ CURRENCY_DENOM

#define CURRENCY_DENOM   10000

Definition at line 285 of file gnc-pricedb.h.

◆ gnc_price_get_book

#define gnc_price_get_book (   X)    qof_instance_get_book(QOF_INSTANCE(X))

Definition at line 270 of file gnc-pricedb.h.

◆ gnc_price_get_guid

#define gnc_price_get_guid (   X)    qof_entity_get_guid(QOF_INSTANCE(X))

Definition at line 268 of file gnc-pricedb.h.

◆ gnc_price_return_guid

#define gnc_price_return_guid (   X)    (*(qof_entity_get_guid(QOF_INSTANCE(X))))

Definition at line 269 of file gnc-pricedb.h.

◆ PRICE_TYPE_LAST

#define PRICE_TYPE_LAST   "last"

Definition at line 184 of file gnc-pricedb.h.

◆ PRICE_TYPE_TRN

#define PRICE_TYPE_TRN   "transaction"

Definition at line 186 of file gnc-pricedb.h.

◆ PRICE_TYPE_UNK

#define PRICE_TYPE_UNK   "unknown"

Definition at line 185 of file gnc-pricedb.h.

Typedef Documentation

◆ PriceList

typedef GList PriceList

Definition at line 162 of file gnc-pricedb.h.

Enumeration Type Documentation

◆ PriceSource

Price source enum.

Be sure to keep in sync with the source_name array in gnc-pricedb.c. These are in preference order, so for example a quote with PRICE_SOURCE_EDIT_DLG will overwrite one with PRICE_SOURCE_FQ but not the other way around.

Definition at line 169 of file gnc-pricedb.h.

170{
171 PRICE_SOURCE_EDIT_DLG, // "user:price-editor"
172 PRICE_SOURCE_FQ, // "Finance::Quote"
173 PRICE_SOURCE_USER_PRICE, // "user:price"
174 PRICE_SOURCE_XFER_DLG_VAL, // "user:xfer-dialog"
175 PRICE_SOURCE_SPLIT_REG, // "user:split-register"
176 PRICE_SOURCE_SPLIT_IMPORT, // "user:split-import"
177 PRICE_SOURCE_STOCK_SPLIT, // "user:stock-split"
178 PRICE_SOURCE_STOCK_TRANSACTION,// "user:stock-transaction"
179 PRICE_SOURCE_INVOICE, // "user:invoice-post"
180 PRICE_SOURCE_TEMP, // "temporary"
181 PRICE_SOURCE_INVALID, // "invalid"
PriceSource
Price source enum.

Function Documentation

◆ gnc_price_begin_edit()

void gnc_price_begin_edit ( GNCPrice *  p)

Definition at line 411 of file gnc-pricedb.cpp.

412{
413 qof_begin_edit(&p->inst);
414}
gboolean qof_begin_edit(QofInstance *inst)
begin_edit

◆ gnc_price_clone()

GNCPrice * gnc_price_clone ( GNCPrice *  p,
QofBook *  book 
)

gnc_price_clone - returns a newly allocated price that's a content-wise duplicate of the given price, p.

The returned clone will have a reference count of 1.

Definition at line 354 of file gnc-pricedb.cpp.

355{
356 /* the clone doesn't belong to a PriceDB */
357 GNCPrice *new_p;
358
359 g_return_val_if_fail (book, nullptr);
360
361 ENTER ("pr=%p", p);
362
363 if (!p)
364 {
365 LEAVE ("return nullptr");
366 return nullptr;
367 }
368
369 new_p = gnc_price_create(book);
370 if (!new_p)
371 {
372 LEAVE ("return nullptr");
373 return nullptr;
374 }
375
376 qof_instance_copy_version(new_p, p);
377
378 gnc_price_begin_edit(new_p);
379 /* never ever clone guid's */
380 gnc_price_set_commodity(new_p, gnc_price_get_commodity(p));
381 gnc_price_set_time64(new_p, gnc_price_get_time64(p));
382 gnc_price_set_source(new_p, gnc_price_get_source(p));
383 gnc_price_set_typestr(new_p, gnc_price_get_typestr(p));
384 gnc_price_set_value(new_p, gnc_price_get_value(p));
385 gnc_price_set_currency(new_p, gnc_price_get_currency(p));
386 gnc_price_commit_edit(new_p);
387 LEAVE ("return cloned price %p", new_p);
388 return(new_p);
389}
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition qoflog.h:282
#define ENTER(format, args...)
Print a function entry debugging message.
Definition qoflog.h:272
GNCPrice * gnc_price_create(QofBook *book)
gnc_price_create - returns a newly allocated and initialized price with a reference count of 1.

◆ gnc_price_commit_edit()

void gnc_price_commit_edit ( GNCPrice *  p)

Definition at line 425 of file gnc-pricedb.cpp.

426{
427 if (!qof_commit_edit (QOF_INSTANCE(p))) return;
428 qof_commit_edit_part2 (&p->inst, commit_err, noop, noop);
429}
gboolean qof_commit_edit_part2(QofInstance *inst, void(*on_error)(QofInstance *, QofBackendError), void(*on_done)(QofInstance *), void(*on_free)(QofInstance *))
part2 – deal with the backend
gboolean qof_commit_edit(QofInstance *inst)
commit_edit helpers

◆ gnc_price_create()

GNCPrice * gnc_price_create ( QofBook *  book)

gnc_price_create - returns a newly allocated and initialized price with a reference count of 1.

Definition at line 295 of file gnc-pricedb.cpp.

296{
297 GNCPrice *p;
298
299 g_return_val_if_fail (book, nullptr);
300
301 ENTER(" ");
302 p = static_cast<GNCPrice*>(g_object_new(GNC_TYPE_PRICE, nullptr));
303
304 qof_instance_init_data (&p->inst, GNC_ID_PRICE, book);
305 qof_event_gen (&p->inst, QOF_EVENT_CREATE, nullptr);
306 LEAVE ("price created %p", p);
307 return p;
308}
void qof_event_gen(QofInstance *entity, QofEventId event_id, gpointer event_data)
Invoke all registered event handlers using the given arguments.
Definition qofevent.cpp:231
void qof_instance_init_data(QofInstance *inst, QofIdType type, QofBook *book)
Initialise the settings associated with an instance.

◆ gnc_price_equal()

gboolean gnc_price_equal ( const GNCPrice *  p1,
const GNCPrice *  p2 
)

Definition at line 649 of file gnc-pricedb.cpp.

650{
651 time64 time1, time2;
652
653 if (p1 == p2) return TRUE;
654 if (!p1 || !p2) return FALSE;
655
656 if (!gnc_commodity_equiv (gnc_price_get_commodity (p1),
657 gnc_price_get_commodity (p2)))
658 return FALSE;
659
660 if (!gnc_commodity_equiv (gnc_price_get_currency (p1),
661 gnc_price_get_currency (p2)))
662 return FALSE;
663
664 time1 = gnc_price_get_time64 (p1);
665 time2 = gnc_price_get_time64 (p2);
666
667 if (time1 != time2)
668 return FALSE;
669
670 if (gnc_price_get_source (p1) != gnc_price_get_source (p2))
671 return FALSE;
672
673 if (g_strcmp0 (gnc_price_get_typestr (p1),
674 gnc_price_get_typestr (p2)) != 0)
675 return FALSE;
676
677 if (!gnc_numeric_eq (gnc_price_get_value (p1),
678 gnc_price_get_value (p2)))
679 return FALSE;
680
681 return TRUE;
682}
gboolean gnc_commodity_equiv(const gnc_commodity *a, const gnc_commodity *b)
This routine returns TRUE if the two commodities are equivalent.
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
Definition gnc-date.h:87
gboolean gnc_numeric_eq(gnc_numeric a, gnc_numeric b)
Equivalence predicate: Returns TRUE (1) if a and b are exactly the same (have the same numerator and ...

◆ gnc_price_get_commodity()

gnc_commodity * gnc_price_get_commodity ( const GNCPrice *  p)

Definition at line 597 of file gnc-pricedb.cpp.

598{
599 if (!p) return nullptr;
600 return p->commodity;
601}

◆ gnc_price_get_currency()

gnc_commodity * gnc_price_get_currency ( const GNCPrice *  p)

Definition at line 642 of file gnc-pricedb.cpp.

643{
644 if (!p) return nullptr;
645 return p->currency;
646}

◆ gnc_price_get_source()

PriceSource gnc_price_get_source ( const GNCPrice *  p)

Definition at line 610 of file gnc-pricedb.cpp.

611{
612 if (!p) return PRICE_SOURCE_INVALID;
613 return p->source;
614}

◆ gnc_price_get_source_string()

const char * gnc_price_get_source_string ( const GNCPrice *  p)

Definition at line 617 of file gnc-pricedb.cpp.

618{
619 if (!p) return nullptr;
620 return source_names[p->source];
621}

◆ gnc_price_get_time64()

time64 gnc_price_get_time64 ( const GNCPrice *  p)

Definition at line 604 of file gnc-pricedb.cpp.

605{
606 return p ? p->tmspec : 0;
607}

◆ gnc_price_get_typestr()

const char * gnc_price_get_typestr ( const GNCPrice *  p)

Definition at line 624 of file gnc-pricedb.cpp.

625{
626 if (!p) return nullptr;
627 return p->type;
628}

◆ gnc_price_get_value()

gnc_numeric gnc_price_get_value ( const GNCPrice *  p)

Definition at line 631 of file gnc-pricedb.cpp.

632{
633 if (!p)
634 {
635 PERR("price nullptr.\n");
636 return gnc_numeric_zero();
637 }
638 return p->value;
639}
#define PERR(format, args...)
Log a serious error.
Definition qoflog.h:244

◆ gnc_price_invert()

GNCPrice * gnc_price_invert ( GNCPrice *  p)

Return a newly-allocated price that's the inverse of the given price, p.

Inverse means that the commodity and currency are swapped and the value is the numeric inverse of the original's. The source is set to PRICE_SOURCE_TEMP to prevent it being saved in the pricedb.

Parameters
pThe price to invert
Returns
a new price, with a ref-count of 1. Don't forget to unref it!

Definition at line 392 of file gnc-pricedb.cpp.

393{
394 QofBook *book = qof_instance_get_book (QOF_INSTANCE(p));
395 GNCPrice *new_p = gnc_price_create (book);
396 qof_instance_copy_version(new_p, p);
397 gnc_price_begin_edit(new_p);
398 gnc_price_set_time64(new_p, gnc_price_get_time64(p));
399 gnc_price_set_source(new_p, PRICE_SOURCE_TEMP);
400 gnc_price_set_typestr(new_p, gnc_price_get_typestr(p));
401 gnc_price_set_commodity(new_p, gnc_price_get_currency(p));
402 gnc_price_set_currency(new_p, gnc_price_get_commodity(p));
403 gnc_price_set_value(new_p, gnc_numeric_invert(gnc_price_get_value(p)));
404 gnc_price_commit_edit(new_p);
405 return new_p;
406}
QofBook * qof_instance_get_book(gconstpointer inst)
Return the book pointer.
gnc_numeric gnc_numeric_invert(gnc_numeric num)
Invert a gnc_numeric.
QofBook reference.
Definition qofbook-p.hpp:47

◆ gnc_price_list_destroy()

void gnc_price_list_destroy ( PriceList *  prices)

gnc_price_list_destroy - destroy the given price list, calling gnc_price_unref on all the prices included in the list.

Definition at line 756 of file gnc-pricedb.cpp.

757{
758 g_list_free_full (prices, (GDestroyNotify)gnc_price_unref);
759}
void gnc_price_unref(GNCPrice *p)
gnc_price_unref - indicate you're finished with a price (i.e.

◆ gnc_price_list_equal()

gboolean gnc_price_list_equal ( PriceList *  prices1,
PriceList *  prices2 
)

Definition at line 762 of file gnc-pricedb.cpp.

763{
764 if (prices1 == prices2) return TRUE;
765
766 for (auto n1 = prices1, n2 = prices2; n1 || n2; n1 = g_list_next (n1), n2 = g_list_next (n2))
767 {
768 if (!n1)
769 {
770 PINFO ("prices2 has extra prices");
771 return FALSE;
772 }
773 if (!n2)
774 {
775 PINFO ("prices1 has extra prices");
776 return FALSE;
777 }
778 if (!gnc_price_equal (static_cast<GNCPrice*>(n1->data), static_cast<GNCPrice*>(n2->data)))
779 return FALSE;
780 };
781
782 return TRUE;
783}
#define PINFO(format, args...)
Print an informational note.
Definition qoflog.h:256

◆ gnc_price_list_insert()

gboolean gnc_price_list_insert ( PriceList **  prices,
GNCPrice *  p,
gboolean  check_dupl 
)

gnc_price_list_insert - insert a price into the given list, calling gnc_price_ref on it during the process.

Definition at line 720 of file gnc-pricedb.cpp.

721{
722 if (!prices || !p) return FALSE;
723 gnc_price_ref(p);
724
725 if (check_dupl && g_list_find_custom (*prices, p, (GCompareFunc)price_is_duplicate))
726 return true;
727
728 auto result_list = g_list_insert_sorted(*prices, p, compare_prices_by_date);
729 if (!result_list)
730 return false;
731
732 *prices = result_list;
733 return true;
734}
void gnc_price_ref(GNCPrice *p)
gnc_price_ref - indicate your need for a given price to stick around (i.e.

◆ gnc_price_list_remove()

gboolean gnc_price_list_remove ( PriceList **  prices,
GNCPrice *  p 
)

gnc_price_list_remove - remove the price, p, from the given list, calling gnc_price_unref on it during the process.

Definition at line 737 of file gnc-pricedb.cpp.

738{
739 GList *result_list;
740 GList *found_element;
741
742 if (!prices || !p) return FALSE;
743
744 found_element = g_list_find(*prices, p);
745 if (!found_element) return TRUE;
746
747 result_list = g_list_remove_link(*prices, found_element);
748 gnc_price_unref((GNCPrice *) found_element->data);
749 g_list_free(found_element);
750
751 *prices = result_list;
752 return TRUE;
753}

◆ gnc_price_lookup()

GNCPrice * gnc_price_lookup ( const GncGUID *  guid,
QofBook *  book 
)

Definition at line 587 of file gnc-pricedb.cpp.

588{
589 QofCollection *col;
590
591 if (!guid || !book) return nullptr;
592 col = qof_book_get_collection (book, GNC_ID_PRICE);
593 return (GNCPrice *) qof_collection_lookup_entity (col, guid);
594}
QofCollection * qof_book_get_collection(const QofBook *book, QofIdType entity_type)
Return The table of entities of the given type.
Definition qofbook.cpp:521
QofInstance * qof_collection_lookup_entity(const QofCollection *col, const GncGUID *guid)
Find the entity going only from its guid.
Definition qofid.cpp:209

◆ gnc_price_print()

void gnc_price_print ( GNCPrice *  db,
FILE *  f,
int  indent 
)

This simple function can be useful for debugging the price code.

Definition at line 2765 of file gnc-pricedb.cpp.

2766{
2767 gnc_commodity *commodity;
2768 gnc_commodity *currency;
2769 gchar *istr = nullptr; /* indent string */
2770 const char *str;
2771
2772 if (!p) return;
2773 if (!f) return;
2774
2775 commodity = gnc_price_get_commodity(p);
2776 currency = gnc_price_get_currency(p);
2777
2778 if (!commodity) return;
2779 if (!currency) return;
2780
2781 istr = g_strnfill(indent, ' ');
2782
2783 fprintf(f, "%s<pdb:price>\n", istr);
2784 fprintf(f, "%s <pdb:commodity pointer=%p>\n", istr, commodity);
2785 str = gnc_commodity_get_namespace(commodity);
2786 str = str ? str : "(null)";
2787 fprintf(f, "%s <cmdty:ref-space>%s</gnc:cmdty:ref-space>\n", istr, str);
2788 str = gnc_commodity_get_mnemonic(commodity);
2789 str = str ? str : "(null)";
2790 fprintf(f, "%s <cmdty:ref-id>%s</cmdty:ref-id>\n", istr, str);
2791 fprintf(f, "%s </pdb:commodity>\n", istr);
2792 fprintf(f, "%s <pdb:currency pointer=%p>\n", istr, currency);
2793 str = gnc_commodity_get_namespace(currency);
2794 str = str ? str : "(null)";
2795 fprintf(f, "%s <cmdty:ref-space>%s</gnc:cmdty:ref-space>\n", istr, str);
2796 str = gnc_commodity_get_mnemonic(currency);
2797 str = str ? str : "(null)";
2798 fprintf(f, "%s <cmdty:ref-id>%s</cmdty:ref-id>\n", istr, str);
2799 fprintf(f, "%s </pdb:currency>\n", istr);
2800 str = source_names[gnc_price_get_source(p)];
2801 str = str ? str : "invalid";
2802 fprintf(f, "%s %s\n", istr, str);
2803 str = gnc_price_get_typestr(p);
2804 str = str ? str : "(null)";
2805 fprintf(f, "%s %s\n", istr, str);
2806 fprintf(f, "%s %g\n", istr, gnc_numeric_to_double(gnc_price_get_value(p)));
2807 fprintf(f, "%s</pdb:price>\n", istr);
2808
2809 g_free(istr);
2810}
const char * gnc_commodity_get_namespace(const gnc_commodity *cm)
Retrieve the namespace for the specified commodity.
const char * gnc_commodity_get_mnemonic(const gnc_commodity *cm)
Retrieve the mnemonic for the specified commodity.
double gnc_numeric_to_double(gnc_numeric in)
Convert numeric to floating-point value.

◆ gnc_price_ref()

void gnc_price_ref ( GNCPrice *  p)

gnc_price_ref - indicate your need for a given price to stick around (i.e.

increase its reference count by 1).

Definition at line 324 of file gnc-pricedb.cpp.

325{
326 if (!p) return;
327 p->refcount++;
328}

◆ gnc_price_set_commodity()

void gnc_price_set_commodity ( GNCPrice *  p,
gnc_commodity *  c 
)

Definition at line 457 of file gnc-pricedb.cpp.

458{
459 if (!p) return;
460
461 if (!gnc_commodity_equiv(p->commodity, c))
462 {
463 /* Changing the commodity requires the hash table
464 * position to be modified. The easiest way of doing
465 * this is to remove and reinsert. */
466 gnc_price_ref (p);
467 remove_price (p->db, p, TRUE);
468 gnc_price_begin_edit (p);
469 p->commodity = c;
470 gnc_price_set_dirty(p);
471 gnc_price_commit_edit (p);
472 add_price (p->db, p);
473 gnc_price_unref (p);
474 }
475}

◆ gnc_price_set_currency()

void gnc_price_set_currency ( GNCPrice *  p,
gnc_commodity *  c 
)

Definition at line 479 of file gnc-pricedb.cpp.

480{
481 if (!p) return;
482
483 if (!gnc_commodity_equiv(p->currency, c))
484 {
485 /* Changing the currency requires the hash table
486 * position to be modified. The easiest way of doing
487 * this is to remove and reinsert. */
488 gnc_price_ref (p);
489 remove_price (p->db, p, TRUE);
490 gnc_price_begin_edit (p);
491 p->currency = c;
492 gnc_price_set_dirty(p);
493 gnc_price_commit_edit (p);
494 add_price (p->db, p);
495 gnc_price_unref (p);
496 }
497}

◆ gnc_price_set_source()

void gnc_price_set_source ( GNCPrice *  p,
PriceSource  source 
)

Definition at line 520 of file gnc-pricedb.cpp.

521{
522 if (!p) return;
523 gnc_price_begin_edit (p);
524 p->source = s;
525 gnc_price_set_dirty(p);
526 gnc_price_commit_edit(p);
527}

◆ gnc_price_set_source_string()

void gnc_price_set_source_string ( GNCPrice *  p,
const char *  s 
)

Definition at line 530 of file gnc-pricedb.cpp.

531{
532 if (!p) return;
533 for (PriceSource s = PRICE_SOURCE_EDIT_DLG;
534 s < PRICE_SOURCE_INVALID; s = PriceSource(s + 1))
535 if (strcmp(source_names[s], str) == 0)
536 {
537 gnc_price_set_source(p, s);
538 return;
539 }
540 /* No match. PRICE_SOURCE_INVALID is only a loop sentinel, never a settable
541 source, so its string form "invalid" reaches here as well -- and like any
542 other unrecognized string it leaves the source unchanged. Name the price
543 (commodity -> currency and date) so a user who spots this in the trace log
544 can find it in the price editor to correct or remove it. */
545 gnc_commodity *commodity = gnc_price_get_commodity(p);
546 gnc_commodity *currency = gnc_price_get_currency(p);
547 char datebuff[MAX_DATE_LENGTH + 1];
548 memset(datebuff, 0, sizeof(datebuff));
549 qof_print_date_buff(datebuff, sizeof(datebuff), gnc_price_get_time64(p));
550 PWARN("unknown price source string \"%s\" for %s:%s -> %s on %s;"
551 " leaving the source unchanged", str,
552 commodity ? gnc_commodity_get_namespace(commodity) : "?",
553 commodity ? gnc_commodity_get_mnemonic(commodity) : "?",
554 currency ? gnc_commodity_get_mnemonic(currency) : "?",
555 datebuff);
556}
#define MAX_DATE_LENGTH
The maximum length of a string created by the date printers.
Definition gnc-date.h:108
size_t qof_print_date_buff(char *buff, const size_t len, time64 t)
Convenience: calls through to qof_print_date_dmy_buff().
Definition gnc-date.cpp:574
#define PWARN(format, args...)
Log a warning.
Definition qoflog.h:250

◆ gnc_price_set_time64()

void gnc_price_set_time64 ( GNCPrice *  p,
time64  t 
)

Definition at line 500 of file gnc-pricedb.cpp.

501{
502 if (!p) return;
503 if (p->tmspec != t)
504 {
505 /* Changing the datestamp requires the hash table
506 * position to be modified. The easiest way of doing
507 * this is to remove and reinsert. */
508 gnc_price_ref (p);
509 remove_price (p->db, p, FALSE);
510 gnc_price_begin_edit (p);
511 p->tmspec = t;
512 gnc_price_set_dirty(p);
513 gnc_price_commit_edit (p);
514 add_price (p->db, p);
515 gnc_price_unref (p);
516 }
517}

◆ gnc_price_set_typestr()

void gnc_price_set_typestr ( GNCPrice *  p,
const char *  type 
)

Definition at line 558 of file gnc-pricedb.cpp.

559{
560 if (!p) return;
561 if (g_strcmp0(p->type, type) != 0)
562 {
563 gnc_price_begin_edit (p);
564 CACHE_REPLACE(p->type, type);
565 gnc_price_set_dirty(p);
566 gnc_price_commit_edit (p);
567 }
568}

◆ gnc_price_set_value()

void gnc_price_set_value ( GNCPrice *  p,
gnc_numeric  value 
)

Definition at line 571 of file gnc-pricedb.cpp.

572{
573 if (!p) return;
574 if (!gnc_numeric_eq(p->value, value))
575 {
576 gnc_price_begin_edit (p);
577 p->value = value;
578 gnc_price_set_dirty(p);
579 gnc_price_commit_edit (p);
580 }
581}

◆ gnc_price_unref()

void gnc_price_unref ( GNCPrice *  p)

gnc_price_unref - indicate you're finished with a price (i.e.

decrease its reference count by 1).

Definition at line 331 of file gnc-pricedb.cpp.

332{
333 if (!p) return;
334 if (p->refcount == 0)
335 {
336 return;
337 }
338
339 p->refcount--;
340
341 if (p->refcount <= 0)
342 {
343 if (nullptr != p->db)
344 {
345 PERR("last unref while price in database");
346 }
347 gnc_price_destroy (p);
348 }
349}

◆ gnc_pricedb_print_contents()

void gnc_pricedb_print_contents ( GNCPriceDB *  db,
FILE *  f 
)

This simple function can be useful for debugging the pricedb code.

Definition at line 2821 of file gnc-pricedb.cpp.

2822{
2823 if (!db)
2824 {
2825 PERR("nullptr PriceDB\n");
2826 return;
2827 }
2828 if (!f)
2829 {
2830 PERR("nullptr FILE*\n");
2831 return;
2832 }
2833
2834 fprintf(f, "<gnc:pricedb>\n");
2835 gnc_pricedb_foreach_price(db, print_pricedb_adapter, f, FALSE);
2836 fprintf(f, "</gnc:pricedb>\n");
2837}
gboolean gnc_pricedb_foreach_price(GNCPriceDB *db, GncPriceForeachFunc f, gpointer user_data, gboolean stable_order)
Call a GncPriceForeachFunction once for each price in db, until the function returns FALSE.