Ejemplo n.º 1
0
static gint
default_sort (GNCPrice *price_a, GNCPrice *price_b)
{
    gnc_commodity *curr_a, *curr_b;
    Timespec ts_a, ts_b;
    gint result;

    /* Primary sort (i.e. commodity name) handled by the tree structure.  */

    /* secondary sort: currency */
    curr_a = gnc_price_get_currency (price_a);
    curr_b = gnc_price_get_currency (price_b);

    result = safe_utf8_collate (gnc_commodity_get_namespace (curr_a),
                                gnc_commodity_get_namespace (curr_b));
    if (result != 0) return result;

    result = safe_utf8_collate (gnc_commodity_get_mnemonic (curr_a),
                                gnc_commodity_get_mnemonic (curr_b));
    if (result != 0) return result;

    /* tertiary sort: time */
    ts_a = gnc_price_get_time (price_a);
    ts_b = gnc_price_get_time (price_b);
    result = timespec_cmp (&ts_a, &ts_b);
    if (result)
        /* Reverse the result to present the most recent quote first. */
        return -result;

    /* last sort: value */
    return gnc_numeric_compare (gnc_price_get_value (price_a),
                                gnc_price_get_value (price_b));
}
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", &timesp);
    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;
}
Ejemplo n.º 3
0
static gint
sort_by_date (GtkTreeModel *f_model,
              GtkTreeIter *f_iter_a,
              GtkTreeIter *f_iter_b,
              gpointer user_data)
{
    GNCPrice *price_a, *price_b;
    Timespec ts_a, ts_b;
    gboolean result;

    if (!get_prices (f_model, f_iter_a, f_iter_b, &price_a, &price_b))
        return sort_ns_or_cm (f_model, f_iter_a, f_iter_b);

    /* sort by time first */
    ts_a = gnc_price_get_time (price_a);
    ts_b = gnc_price_get_time (price_b);
    result = timespec_cmp (&ts_a, &ts_b);
    if (result)
        /* Reverse the result to present the most recent quote first. */
        return -result;

    return default_sort (price_a, price_b);
}