Fold separate call of gnc_pricedb_lookup_latest() into lookup_price.

Requires a 3-state enum instead of a boolean for the second arg to lookup_price.
This commit is contained in:
John Ralls 2015-09-09 11:22:52 -07:00
parent 7db7e0cf7a
commit d9a0f311a4

View File

@ -216,6 +216,13 @@ round_price(gnc_commodity *from, gnc_commodity *to, gnc_numeric value)
return value;
}
typedef enum
{
SAME_DAY,
NEAREST,
LATEST
} PriceDate;
typedef struct
{
GNCPrice *price;
@ -240,7 +247,7 @@ price_request_from_xferData(PriceReq *pr, XferDialog *xd)
}
static gboolean
lookup_price(PriceReq *pr, gboolean day_only)
lookup_price(PriceReq *pr, PriceDate pd)
{
GNCPrice *prc = NULL;
g_return_val_if_fail (pr != NULL, FALSE);
@ -249,26 +256,37 @@ lookup_price(PriceReq *pr, gboolean day_only)
g_return_val_if_fail (pr->to != NULL, FALSE);
pr->reverse = FALSE;
if (day_only)
switch (pd)
{
prc = gnc_pricedb_lookup_day (pr->pricedb, pr->from, pr->to, pr->ts);
if (!prc)
{
prc = gnc_pricedb_lookup_day (pr->pricedb, pr->to,
pr->from, pr->ts);
pr->reverse = TRUE;
}
}
else
{
prc = gnc_pricedb_lookup_nearest_in_time (pr->pricedb, pr->from,
pr->to, pr->ts);
if (!prc)
{
prc = gnc_pricedb_lookup_nearest_in_time (pr->pricedb, pr->to,
pr->from, pr->ts);
pr->reverse = TRUE;
}
default:
case SAME_DAY:
prc = gnc_pricedb_lookup_day (pr->pricedb, pr->from,
pr->to, pr->ts);
if (!prc)
{
prc = gnc_pricedb_lookup_day (pr->pricedb, pr->to,
pr->from, pr->ts);
pr->reverse = TRUE;
}
break;
case NEAREST:
prc = gnc_pricedb_lookup_nearest_in_time (pr->pricedb, pr->from,
pr->to, pr->ts);
if (!prc)
{
prc = gnc_pricedb_lookup_nearest_in_time (pr->pricedb, pr->to,
pr->from, pr->ts);
pr->reverse = TRUE;
}
break;
case LATEST:
prc = gnc_pricedb_lookup_latest (pr->pricedb, pr->from, pr->to);
if (!prc)
{
prc = gnc_pricedb_lookup_latest (pr->pricedb, pr->to, pr->from);
pr->reverse = TRUE;
}
break;
}
if (pr->reverse)
{
@ -305,8 +323,8 @@ gnc_xfer_dialog_update_price (XferDialog *xferData)
if (!xferData->pricedb) return;
price_request_from_xferData(&pr, xferData);
if (!lookup_price(&pr, TRUE))
if (!lookup_price(&pr, FALSE))
if (!lookup_price(&pr, SAME_DAY))
if (!lookup_price(&pr, NEAREST))
return;
/* grab the price from the pricedb */
@ -1605,7 +1623,7 @@ create_price(XferDialog *xferData, Timespec ts)
* shifts to be < 1. */
price_request_from_xferData(&pr, xferData);
if (lookup_price(&pr, TRUE))
if (lookup_price(&pr, SAME_DAY))
{
price_value = gnc_price_get_value(pr.price);
if (gnc_numeric_equal(pr.reverse ? gnc_numeric_invert(value) : value,
@ -1810,14 +1828,11 @@ gnc_xfer_dialog_close_cb(GtkDialog *dialog, gpointer data)
void
gnc_xfer_dialog_fetch (GtkButton *button, XferDialog *xferData)
{
gnc_numeric rate;
GNCPrice *prc;
gnc_commodity *from = xferData->from_commodity;
gnc_commodity *to = xferData->to_commodity;
PriceReq pr;
SCM quotes_func;
SCM book_scm;
SCM scm_window;
gboolean have_price = FALSE;
g_return_if_fail (xferData);
@ -1852,29 +1867,15 @@ gnc_xfer_dialog_fetch (GtkButton *button, XferDialog *xferData)
gnc_unset_busy_cursor (NULL);
/*the results should be in the price db now, but don't crash if not. */
prc = gnc_pricedb_lookup_latest(xferData->pricedb, from, to);
if (prc)
price_request_from_xferData(&pr, xferData);
if (lookup_price(&pr, LATEST))
{
rate = gnc_price_get_value (prc);
_gnc_xfer_dialog_set_price_edit(xferData, rate);
gnc_price_unref (prc);
have_price = TRUE;
}
/* Lets try reversing the commodities */
if(!have_price)
{
prc = gnc_pricedb_lookup_latest (xferData->pricedb, to, from);
if (prc)
{
/* FIXME: We probably want to swap the result price's to and from, not invert the price. */
rate = gnc_numeric_invert(gnc_price_get_value (prc));
rate = round_price(from, to, rate);
_gnc_xfer_dialog_set_price_edit(xferData, rate);
gnc_price_unref (prc);
have_price = TRUE;
}
gnc_numeric price_value = gnc_price_get_value(pr.price);
if (pr.reverse)
price_value = round_price(pr.from, pr.to,
gnc_numeric_invert(price_value));
_gnc_xfer_dialog_set_price_edit(xferData, price_value);
gnc_price_unref (pr.price);
}
LEAVE("quote retrieved");