static xmlNodePtr gnc_price_to_dom_tree(const xmlChar *tag, GNCPrice *price) { xmlNodePtr price_xml; const gchar *typestr, *sourcestr; xmlNodePtr tmpnode; gnc_commodity *commodity; gnc_commodity *currency; Timespec timesp; gnc_numeric value; if (!(tag && price)) return NULL; price_xml = xmlNewNode(NULL, tag); if (!price_xml) return NULL; commodity = gnc_price_get_commodity(price); currency = gnc_price_get_currency(price); if (!(commodity && currency)) return NULL; tmpnode = guid_to_dom_tree("price:id", gnc_price_get_guid(price)); if (!add_child_or_kill_parent(price_xml, tmpnode)) return NULL; tmpnode = commodity_ref_to_dom_tree("price:commodity", commodity); if (!add_child_or_kill_parent(price_xml, tmpnode)) return NULL; tmpnode = commodity_ref_to_dom_tree("price:currency", currency); if (!add_child_or_kill_parent(price_xml, tmpnode)) return NULL; timesp = gnc_price_get_time(price); tmpnode = timespec_to_dom_tree("price:time", ×p); if (!add_child_or_kill_parent(price_xml, tmpnode)) return NULL; sourcestr = gnc_price_get_source(price); if (sourcestr && (strlen(sourcestr) != 0)) { tmpnode = text_to_dom_tree("price:source", sourcestr); if (!add_child_or_kill_parent(price_xml, tmpnode)) return NULL; } typestr = gnc_price_get_typestr(price); if (typestr && (strlen(typestr) != 0)) { tmpnode = text_to_dom_tree("price:type", typestr); if (!add_child_or_kill_parent(price_xml, tmpnode)) return NULL; } value = gnc_price_get_value(price); tmpnode = gnc_numeric_to_dom_tree("price:value", &value); if (!add_child_or_kill_parent(price_xml, tmpnode)) return NULL; return price_xml; }
static void refresh_details_page (StockSplitInfo *info) { GNCPrintAmountInfo print_info; gnc_commodity *commodity, *currency; Account *account; QofBook *book; GNCPriceDB *db; GList *prices; account = info->acct; g_return_if_fail (account != NULL); print_info = gnc_account_print_info (account, FALSE); gnc_amount_edit_set_print_info (GNC_AMOUNT_EDIT (info->distribution_edit), print_info); gnc_amount_edit_set_fraction (GNC_AMOUNT_EDIT (info->distribution_edit), xaccAccountGetCommoditySCU (account)); commodity = xaccAccountGetCommodity (account); book = gnc_account_get_book (account); db = gnc_pricedb_get_db(book); prices = gnc_pricedb_lookup_latest_any_currency(db, commodity); if (prices) { /* Use the first existing price */ if (gnc_commodity_equiv (commodity, gnc_price_get_currency(prices->data))) currency = gnc_price_get_commodity(prices->data); else currency = gnc_price_get_currency(prices->data); } else { /* Take a wild guess. */ currency = gnc_default_currency (); } gnc_price_list_destroy(prices); gnc_currency_edit_set_currency (GNC_CURRENCY_EDIT (info->price_currency_edit), currency); }
static gboolean save_price (GncSqlBackend* be, QofInstance* inst) { GNCPrice* pPrice = GNC_PRICE (inst); E_DB_OPERATION op; gboolean is_infant; gboolean is_ok = TRUE; g_return_val_if_fail (be != NULL, FALSE); g_return_val_if_fail (inst != NULL, FALSE); g_return_val_if_fail (GNC_IS_PRICE (inst), FALSE); is_infant = qof_instance_get_infant (inst); if (qof_instance_get_destroying (inst)) { op = OP_DB_DELETE; } else if (be->is_pristine_db || is_infant) { op = OP_DB_INSERT; } else { op = OP_DB_UPDATE; } if (op != OP_DB_DELETE) { /* Ensure commodity and currency are in the db */ (void)gnc_sql_save_commodity (be, gnc_price_get_commodity (pPrice)); is_ok = gnc_sql_save_commodity (be, gnc_price_get_currency (pPrice)); } if (is_ok) { is_ok = gnc_sql_do_db_operation (be, op, TABLE_NAME, GNC_ID_PRICE, pPrice, col_table); } return is_ok; }