Esempio n. 1
0
/********************************************************************\
 * gnc_query_list_set_query_sort                                    *
 *   sets the sorting order of entries in the list                  *
 *                                                                  *
 * Args: list       - list to change the sort order for             *
 *	 new_column - is this a new column (so should we set the    *
 *                    query sort order or just set the 'increasing' *
 * Returns: nothing                                                 *
\********************************************************************/
static void
gnc_query_list_set_query_sort (GNCQueryList *list, gboolean new_column)
{
    gboolean sort_order = list->increasing;
    GList *node;
    GNCSearchParam *param;

    /* Find the column parameter definition */
    node = g_list_nth(list->column_params, list->sort_column);
    param = node->data;

    /* If we're asked to invert numerics, and if this is a numeric or
     * debred column, then invert the sort order.
     */
    if (list->numeric_inv_sort)
    {
        const char *type = gnc_search_param_get_param_type (param);
        if (!safe_strcmp(type, QOF_TYPE_NUMERIC) ||
                !safe_strcmp(type, QOF_TYPE_DEBCRED))
            sort_order = !sort_order;
    }

    /* Set the sort order for the engine, if the key changed */
    if (new_column)
    {
        GSList *p1, *p2;

        p1 = gnc_search_param_get_param_path(param);
        p2 = g_slist_prepend(NULL, QUERY_DEFAULT_SORT);
        qof_query_set_sort_order (list->query, p1, p2, NULL);
    }

    qof_query_set_sort_increasing (list->query,
                                   sort_order,
                                   sort_order,
                                   sort_order);

    /*
     * Recompute the list. Is this really necessary? Why not just sort
     * the rows already in the clist?  Answer: it would be an n-squared
     * algorithm to get the clist to match the resulting list.
     */
    gnc_query_list_refresh(list);
}
Esempio n. 2
0
static void
search_update_query (GNCSearchWindow *sw)
{
    static GSList *active_params = NULL;
    QofQuery *q, *q2, *new_q;
    GList *node;
    QofQueryOp op;
    QofQueryPredData* pdata;

    if (sw->grouping == GNC_SEARCH_MATCH_ANY)
        op = QOF_QUERY_OR;
    else
        op = QOF_QUERY_AND;

    if (active_params == NULL)
        active_params = g_slist_prepend (NULL, QOF_PARAM_ACTIVE);

    /* Make sure we supply a book! */
    if (sw->start_q == NULL)
    {
        sw->start_q = qof_query_create_for (sw->search_for);
        qof_query_set_book (sw->start_q, gnc_get_current_book ());
    }
    else
    {
        /* We've got a query -- purge it of any "active" parameters */
        qof_query_purge_terms (sw->start_q, active_params);
    }

    /* Now create a new query to work from */
    q = qof_query_create_for (sw->search_for);

    /* Walk the list of criteria */
    for (node = sw->crit_list; node; node = node->next)
    {
        struct _crit_data *data = node->data;

        pdata = gnc_search_core_type_get_predicate (data->element);
        if (pdata)
            qof_query_add_term (q, gnc_search_param_get_param_path (data->param),
                                pdata, op);
    }

    /* Now combine this query with the existing query, depending on
     * what we want to do...  We can assume that cases 1, 2, and 3
     * already have sw->q being valid!
     */

    switch (sw->search_type)
    {
    case 0:			/* New */
        new_q = qof_query_merge (sw->start_q, q, QOF_QUERY_AND);
        qof_query_destroy (q);
        break;
    case 1:			/* Refine */
        new_q = qof_query_merge (sw->q, q, QOF_QUERY_AND);
        qof_query_destroy (q);
        break;
    case 2:			/* Add */
        new_q = qof_query_merge (sw->q, q, QOF_QUERY_OR);
        qof_query_destroy (q);
        break;
    case 3:			/* Delete */
        q2 = qof_query_invert (q);
        new_q = qof_query_merge (sw->q, q2, QOF_QUERY_AND);
        qof_query_destroy (q2);
        qof_query_destroy (q);
        break;
    default:
        g_warning ("bad search type: %d", sw->search_type);
        new_q = q;
        break;
    }

    if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (sw->active_only_check)))
    {
        qof_query_add_boolean_match (new_q, active_params, TRUE, QOF_QUERY_AND);
        active_params = NULL;
    }

    /* Destroy the old query */
    if (sw->q)
        qof_query_destroy (sw->q);

    /* And save the new one */
    sw->q = new_q;
}