static gint
default_sort (gnc_commodity *comm_a, gnc_commodity *comm_b)
{
    gint fraction_a, fraction_b, result;

    result = safe_utf8_collate (gnc_commodity_get_namespace (comm_a),
                                gnc_commodity_get_namespace (comm_b));
    if (result != 0) return result;

    result = safe_utf8_collate (gnc_commodity_get_mnemonic (comm_a),
                                gnc_commodity_get_mnemonic (comm_b));
    if (result != 0) return result;

    result = safe_utf8_collate (gnc_commodity_get_fullname (comm_a),
                                gnc_commodity_get_fullname (comm_b));
    if (result != 0) return result;

    result = safe_utf8_collate (gnc_commodity_get_cusip (comm_a),
                                gnc_commodity_get_cusip (comm_b));
    if (result != 0) return result;

    fraction_a = gnc_commodity_get_fraction (comm_a);
    fraction_b = gnc_commodity_get_fraction (comm_b);

    if (fraction_a < fraction_b)
        return -1;

    if (fraction_b < fraction_a)
        return 1;

    return 0;
}
Exemplo n.º 2
0
xmlNodePtr
gnc_commodity_dom_tree_create(const gnc_commodity *com)
{
    gnc_quote_source *source;
    const char *string;
    xmlNodePtr ret;
    gboolean currency = gnc_commodity_is_iso(com);
    xmlNodePtr slotsnode =
        qof_instance_slots_to_dom_tree(cmdty_slots, QOF_INSTANCE(com));

    if (currency && !gnc_commodity_get_quote_flag(com) && !slotsnode)
        return NULL;

    ret = xmlNewNode(NULL, BAD_CAST gnc_commodity_string);

    xmlSetProp(ret, BAD_CAST "version", BAD_CAST commodity_version_string);

    xmlAddChild(ret, text_to_dom_tree(cmdty_namespace,
                                      gnc_commodity_get_namespace_compat(com)));
    xmlAddChild(ret, text_to_dom_tree(cmdty_id,
                                      gnc_commodity_get_mnemonic(com)));

    if (!currency)
    {
        if (gnc_commodity_get_fullname(com))
        {
            xmlAddChild(ret, text_to_dom_tree(cmdty_name,
                                              gnc_commodity_get_fullname(com)));
        }

        if (gnc_commodity_get_cusip(com) &&
                strlen(gnc_commodity_get_cusip(com)) > 0)
        {
            xmlAddChild(ret, text_to_dom_tree(
                            cmdty_xcode,
                            gnc_commodity_get_cusip(com)));
        }

        xmlAddChild(ret, int_to_dom_tree(cmdty_fraction,
                                         gnc_commodity_get_fraction(com)));
    }

    if (gnc_commodity_get_quote_flag(com))
    {
        xmlNewChild(ret, NULL, BAD_CAST cmdty_get_quotes, NULL);
        source = gnc_commodity_get_quote_source(com);
        if (source)
            xmlAddChild(ret, text_to_dom_tree(cmdty_quote_source,
                                              gnc_quote_source_get_internal_name(source)));
        string = gnc_commodity_get_quote_tz(com);
        if (string)
            xmlAddChild(ret, text_to_dom_tree(cmdty_quote_tz, string));
    }

    if (slotsnode)
        xmlAddChild(ret, slotsnode);

    return ret;
}
Exemplo n.º 3
0
static const char*
node_and_commodity_equal (xmlNodePtr node, const gnc_commodity* com)
{
    xmlNodePtr mark;

    while (g_strcmp0 ((char*)node->name, "text") == 0)
        node = node->next;

    if (!check_dom_tree_version (node, "2.0.0"))
    {
        return "version wrong.  Not 2.0.0 or not there";
    }

    if (!node->name || g_strcmp0 ((char*)node->name, "gnc:commodity"))
    {
        return "Name of toplevel node is bad";
    }

    for (mark = node->xmlChildrenNode; mark; mark = mark->next)
    {
        if (g_strcmp0 ((char*)mark->name, "text") == 0)
        {
        }
        else if (g_strcmp0 ((char*)mark->name, "cmdty:space") == 0)
        {
            if (!equals_node_val_vs_string (
                    mark, gnc_commodity_get_namespace_compat (com)))
            {
                return "namespaces differ";
            }
        }
        else if (g_strcmp0 ((char*)mark->name, "cmdty:id") == 0)
        {
            if (!equals_node_val_vs_string (
                    mark, gnc_commodity_get_mnemonic (com)))
            {
                return "mnemonic differ";
            }
        }
        else if (g_strcmp0 ((char*)mark->name, "cmdty:name") == 0)
        {
            if (!equals_node_val_vs_string (
                    mark, gnc_commodity_get_fullname (com)))
            {
                return "names differ";
            }
        }
        else if (g_strcmp0 ((char*)mark->name, "cmdty:xcode") == 0)
        {
            if (!equals_node_val_vs_string (
                    mark, gnc_commodity_get_cusip (com)))
            {
                return "exchange codes differ";
            }
        }
        else if (g_strcmp0 ((char*)mark->name, "cmdty:fraction") == 0)
        {
            gchar* txt;
            gint64 type;

            txt = dom_tree_to_text (mark);

            if (!txt)
            {
                return "couldn't get fraction string";
            }

            else if (!string_to_gint64 (txt, &type))
            {
                g_free (txt);
                return "couldn't convert fraction string to int";
            }
            else if (type != gnc_commodity_get_fraction (com))
            {
                g_free (txt);
                return "fractions differ";
            }
            else
            {
                g_free (txt);
            }
        }
        else if (g_strcmp0 ((char*)mark->name, "cmdty:slots") == 0)
        {
            if (!equals_node_val_vs_kvp_frame (mark,
                                               gnc_commodity_get_kvp_frame (com)))
                return "slots differ";
        }
        /* Legitimate tags which we don't yet have tests */
        else if (g_strcmp0 ((char*)mark->name, "cmdty:get_quotes") == 0 ||
                 g_strcmp0 ((char*)mark->name, "cmdty:quote_source") == 0 ||
                 g_strcmp0 ((char*)mark->name, "cmdty:quote_tz") == 0)
        {
            continue;
        }
        else
        {
            return "unknown node";
        }
    }

    return NULL;
}
Exemplo n.º 4
0
gnc_commodity * gnc_import_select_commodity(const char * cusip,
        gboolean ask_on_unknown,
        const char * default_fullname,
        const char * default_mnemonic)
{
    const gnc_commodity_table * commodity_table = gnc_get_current_commodities ();
    gnc_commodity * retval = NULL;
    gnc_commodity * tmp_commodity = NULL;
    char * tmp_namespace = NULL;
    GList * commodity_list = NULL;
    GList * namespace_list = NULL;
    DEBUG("Default fullname received: %s",
          default_fullname ? default_fullname : "(null)");
    DEBUG("Default mnemonic received: %s",
          default_mnemonic ? default_mnemonic : "(null)");

    g_return_val_if_fail(cusip, NULL);
    DEBUG("Looking for commodity with exchange_code: %s", cusip);

    g_assert(commodity_table);
    namespace_list = gnc_commodity_table_get_namespaces(commodity_table);


    namespace_list = g_list_first(namespace_list);
    while ( namespace_list != NULL && retval == NULL)
    {
        tmp_namespace = namespace_list->data;
        DEBUG("Looking at namespace %s", tmp_namespace);
        commodity_list = gnc_commodity_table_get_commodities(commodity_table,
                                                             tmp_namespace);
        commodity_list  = g_list_first(commodity_list);
        while ( commodity_list != NULL && retval == NULL)
        {
            const char* tmp_cusip = NULL;
            tmp_commodity = commodity_list->data;
            DEBUG("Looking at commodity %s",
                  gnc_commodity_get_fullname(tmp_commodity));
            tmp_cusip = gnc_commodity_get_cusip(tmp_commodity);
            if  (tmp_cusip != NULL && cusip != NULL)
            {
                int len = strlen(cusip) > strlen(tmp_cusip) ? strlen(cusip) :
                    strlen(tmp_cusip);
                if (strncmp(tmp_cusip, cusip, len) == 0)
                {
                    retval = tmp_commodity;
                    DEBUG("Commodity %s%s",
                          gnc_commodity_get_fullname(retval), " matches.");
                }
            }
            commodity_list = g_list_next(commodity_list);
        }
        namespace_list = g_list_next(namespace_list);
    }

    g_list_free(commodity_list);
    g_list_free(namespace_list);

    if (retval == NULL && ask_on_unknown != 0)
    {
        const gchar *message =
            _("Please select a commodity to match the following exchange "
              "specific code. Please note that the exchange code of the "
              "commodity you select will be overwritten.");
        retval = gnc_ui_select_commodity_modal_full(NULL,
                 NULL,
                 DIAG_COMM_ALL,
                 message,
                 cusip,
                 default_fullname,
                 default_mnemonic);

    }
    /* There seems to be a problem here - if the matched commodity does not
       have a cusip defined (gnc_commodity_get_cusip returns NULL) then
       it does not get overwritten - which is not consistent with the
       message - so Im adding it to do this.  Looks like this is all
       that was needed to fix the cash value used as stock units problem
       for pre-defined commodities which didnt have the cusip defined! */
    if (retval != NULL &&
            gnc_commodity_get_cusip(retval) != NULL &&
            cusip != NULL &&
            (strncmp(gnc_commodity_get_cusip(retval), cusip, strlen(cusip)) != 0))
    {
        gnc_commodity_set_cusip(retval, cusip);
    }
    else if (gnc_commodity_get_cusip(retval) == NULL && cusip != NULL)
    {
        gnc_commodity_set_cusip(retval, cusip);
    }
    return retval;
};