From 223fc8274122932883a0dbdee0ce0a992d24eb9a Mon Sep 17 00:00:00 2001 From: Dave Peticolas Date: Sat, 23 Dec 2000 01:28:48 +0000 Subject: [PATCH] Fix memory handling of queries by the register. git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@3347 57a11ea4-9604-0410-9ed3-97b8803252fd --- src/MultiLedger.c | 89 ++++++++++++++++++++++------ src/MultiLedger.h | 8 +++ src/gnome/dialog-find-transactions.c | 55 +++++++++++------ src/gnome/window-register.c | 11 ++-- 4 files changed, 119 insertions(+), 44 deletions(-) diff --git a/src/MultiLedger.c b/src/MultiLedger.c index 7c4ac5d628..fd72ef3b96 100644 --- a/src/MultiLedger.c +++ b/src/MultiLedger.c @@ -43,6 +43,12 @@ static short module = MOD_LEDGER; +/** Declarations ****************************************************/ +static xaccLedgerDisplay * +xaccLedgerDisplayInternal (Account *lead_account, GList *accounts, Query *q, + SplitRegisterType type, SplitRegisterStyle style); + + /** Implementations *************************************************/ static gboolean @@ -339,6 +345,30 @@ close_handler (gpointer user_data) g_free (ld); } +static void +make_ledger_query (xaccLedgerDisplay *ld, gboolean show_all, + SplitRegisterType type) +{ + ld->query = xaccMallocQuery (); + + /* This is a bit of a hack. The number of splits should be + * configurable, or maybe we should go back a time range instead + * of picking a number, or maybe we should be able to exclude + * based on reconciled status. Anyway, this works for now. */ + if (!show_all && (type != SEARCH_LEDGER)) + xaccQuerySetMaxSplits (ld->query, 30); + + xaccQuerySetGroup (ld->query, gncGetCurrentGroup()); + + if (ld->displayed_accounts) + xaccQueryAddAccountMatch (ld->query, ld->displayed_accounts, + ACCT_MATCH_ANY, QUERY_OR); + + if (ld->leader && + (g_list_find (ld->displayed_accounts, ld->leader) == NULL)) + xaccQueryAddSingleAccountMatch (ld->query, ld->leader, QUERY_OR); +} + /********************************************************************\ * xaccLedgerDisplayGeneral * * opens up a ledger window for a list of accounts * @@ -347,11 +377,46 @@ close_handler (gpointer user_data) * (may be NULL) * * accounts - the list of accounts to display * * (may be NULL) * + * type - the type of split register to open * + * style - the style of register to use * * Return: the register window instance * \********************************************************************/ xaccLedgerDisplay * xaccLedgerDisplayGeneral (Account *lead_account, GList *accounts, SplitRegisterType type, SplitRegisterStyle style) +{ + return xaccLedgerDisplayInternal (lead_account, accounts, NULL, type, style); +} + +/********************************************************************\ + * xaccLedgerDisplayQuery * + * opens up a ledger window for an arbitrary query * + * * + * Args: query - the query to use for the register * + * type - the type of split register to open * + * style - the style of register to use * + * Return: the register window instance * +\********************************************************************/ +xaccLedgerDisplay * +xaccLedgerDisplayQuery (Query *query, SplitRegisterType type, + SplitRegisterStyle style) +{ + return xaccLedgerDisplayInternal (NULL, NULL, query, type, style); +} + +void +xaccLedgerDisplaySetQuery (xaccLedgerDisplay *ledger_display, Query *q) +{ + if (!ledger_display || !q) + return; + + xaccFreeQuery (ledger_display->query); + ledger_display->query = xaccQueryCopy (q); +} + +static xaccLedgerDisplay * +xaccLedgerDisplayInternal (Account *lead_account, GList *accounts, Query *q, + SplitRegisterType type, SplitRegisterStyle style) { xaccLedgerDisplay *ld; gboolean show_all; @@ -402,24 +467,10 @@ xaccLedgerDisplayGeneral (Account *lead_account, GList *accounts, TRUE); /* set up the query filter */ - ld->query = xaccMallocQuery (); - - /* This is a bit of a hack. The number of splits should be - * configurable, or maybe we should go back a time range instead - * of picking a number, or maybe we should be able to exclude - * based on reconciled status. Anyway, this works for now. */ - if (!show_all && (type != SEARCH_LEDGER)) - xaccQuerySetMaxSplits(ld->query, 30); - - xaccQuerySetGroup (ld->query, gncGetCurrentGroup()); - - if (ld->displayed_accounts) - xaccQueryAddAccountMatch (ld->query, ld->displayed_accounts, - ACCT_MATCH_ANY, QUERY_OR); - - if ((ld->leader != NULL) && - (g_list_find (ld->displayed_accounts, ld->leader) == NULL)) - xaccQueryAddSingleAccountMatch (ld->query, ld->leader, QUERY_OR); + if (q) + ld->query = xaccQueryCopy (q); + else + make_ledger_query (ld, show_all, type); ld->component_id = gnc_register_gui_component (class, NULL, close_handler, ld); @@ -459,7 +510,7 @@ void xaccLedgerDisplayRefresh (xaccLedgerDisplay *ld) { /* If we don't really need the redraw, don't do it. */ - if (!(ld->dirty)) + if (!ld->dirty) return; ld->dirty = FALSE; /* mark clean */ diff --git a/src/MultiLedger.h b/src/MultiLedger.h index c06243c1ee..7e1d16bfb0 100644 --- a/src/MultiLedger.h +++ b/src/MultiLedger.h @@ -79,6 +79,14 @@ xaccLedgerDisplay * xaccLedgerDisplayGeneral (Account *lead_account, SplitRegisterType type, SplitRegisterStyle style); +/* display a register for an arbitrary query */ +xaccLedgerDisplay * xaccLedgerDisplayQuery (Query *query, + SplitRegisterType type, + SplitRegisterStyle style); + +/* Set the query used for a register. */ +void xaccLedgerDisplaySetQuery (xaccLedgerDisplay *ledger_display, Query *q); + /* redisplay/redraw all windows that contain any transactions that are * associated with the indicated account. */ void xaccAccountDisplayRefresh (Account *account); diff --git a/src/gnome/dialog-find-transactions.c b/src/gnome/dialog-find-transactions.c index f79db1832e..a47a682eff 100644 --- a/src/gnome/dialog-find-transactions.c +++ b/src/gnome/dialog-find-transactions.c @@ -39,12 +39,16 @@ #include "dialog-utils.h" #include "glade-cb-gnc-dialogs.h" #include "gnc-dateedit.h" +#include "gnc-engine-util.h" #include "gnc-ui.h" #include "messages.h" #include "splitreg.h" #include "window-help.h" #include "window-register.h" +/* This static indicates the debugging module that this .o belongs to. */ +static short module = MOD_GUI; + /********************************************************************\ * gnc_ui_find_transactions_dialog_create * make a new print check dialog and wait for it. @@ -59,12 +63,12 @@ gnc_ui_find_transactions_dialog_create(xaccLedgerDisplay * orig_ledg) { ftd->ledger = orig_ledg; if(orig_ledg) { - ftd->q = orig_ledg->query; + ftd->q = xaccQueryCopy (orig_ledg->query); } else { ftd->q = NULL; } - + /* initialize the radiobutton state vars */ ftd->search_type = 0; @@ -227,7 +231,13 @@ gnc_ui_find_transactions_dialog_destroy(FindTransactionsDialog * ftd) { gnome_dialog_close(GNOME_DIALOG(ftd->dialog)); ftd->dialog = NULL; + + xaccFreeQuery (ftd->q); + ftd->q = NULL; + g_free(ftd->ymd_format); + ftd->ymd_format = NULL; + g_free(ftd); } @@ -339,6 +349,7 @@ gnc_ui_find_transactions_dialog_ok_cb(GtkButton * button, float amt_temp; int amt_type; Query * q, * q2; + Query * new_q; gboolean new_ledger = FALSE; int use_start_date, use_end_date; @@ -516,39 +527,45 @@ gnc_ui_find_transactions_dialog_ok_cb(GtkButton * button, xaccQueryAddBalanceMatch(q, how, QUERY_AND); } - if(!ftd->ledger) { - new_ledger = TRUE; - ftd->ledger = xaccLedgerDisplayGeneral(NULL, NULL, - SEARCH_LEDGER, - REG_STYLE_JOURNAL); - xaccFreeQuery(ftd->ledger->query); - } - switch(search_type) { case 0: - ftd->ledger->query = q; + new_q = q; break; case 1: - ftd->ledger->query = xaccQueryMerge(ftd->q, q, QUERY_AND); + new_q = xaccQueryMerge(ftd->q, q, QUERY_AND); xaccFreeQuery(q); break; case 2: - ftd->ledger->query = xaccQueryMerge(ftd->q, q, QUERY_OR); + new_q = xaccQueryMerge(ftd->q, q, QUERY_OR); xaccFreeQuery(q); break; case 3: q2 = xaccQueryInvert(q); - ftd->ledger->query = xaccQueryMerge(ftd->q, q2, QUERY_AND); + new_q = xaccQueryMerge(ftd->q, q2, QUERY_AND); xaccFreeQuery(q2); xaccFreeQuery(q); break; + default: + PERR ("bad search type: %d", search_type); + new_q = q; + break; } - ftd->ledger->dirty = 1; + if(!ftd->ledger) { + new_ledger = TRUE; + ftd->ledger = xaccLedgerDisplayQuery (new_q, SEARCH_LEDGER, + REG_STYLE_JOURNAL); + } + else + xaccLedgerDisplaySetQuery (ftd->ledger, new_q); + + xaccFreeQuery (new_q); + + ftd->ledger->dirty = TRUE; xaccLedgerDisplayRefresh(ftd->ledger); - if (new_ledger) regWindowLedger(ftd->ledger); - + + if (new_ledger) + regWindowLedger(ftd->ledger); + gnc_ui_find_transactions_dialog_destroy(ftd); } - - diff --git a/src/gnome/window-register.c b/src/gnome/window-register.c index 5bccd0c692..50f8cbff7e 100644 --- a/src/gnome/window-register.c +++ b/src/gnome/window-register.c @@ -905,12 +905,11 @@ static void gnc_ui_find_transactions_cb (GtkWidget *widget, gpointer data) { RegWindow * regdata = data; - if(regdata->ledger->type == SEARCH_LEDGER) { - gnc_ui_find_transactions_dialog_create(regdata->ledger); - } - else { - gnc_ui_find_transactions_dialog_create(NULL); - } + + if (regdata->ledger->type == SEARCH_LEDGER) + gnc_ui_find_transactions_dialog_create (regdata->ledger); + else + gnc_ui_find_transactions_dialog_create (NULL); }