From bf317b5a44efd63b24cce594db3dbfa9231631fb Mon Sep 17 00:00:00 2001 From: David Hampton Date: Sat, 26 Nov 2005 03:58:06 +0000 Subject: [PATCH] Enhance the "remove old prices" code to have options to retain the last price entered, and to retain any user entered prices. The "remove old" button should always be active. Group the "Add/Edit/Remove" buttons for a single price. Allow the prices dialog to be closed with the escape key. git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@12033 57a11ea4-9604-0410-9ed3-97b8803252fd --- ChangeLog | 7 + src/engine/gnc-pricedb.c | 78 +++++++- src/engine/gnc-pricedb.h | 3 +- src/gnome/dialog-price-edit-db.c | 70 ++++--- src/gnome/glade/price.glade | 303 +++++++++++++++++++++++-------- 5 files changed, 344 insertions(+), 117 deletions(-) diff --git a/ChangeLog b/ChangeLog index f874ca71f5..aa7a5d9d58 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,12 @@ 2005-11-25 David Hampton + * src/gnome/dialog-price-edit-db.c: Enhance the "remove old + prices" code to have options to retain the last price entered, and + to retain any user entered prices. The "remove old" button should + always be active. Group the "Add/Edit/Remove" buttons for a + single price. Allow the prices dialog to be closed with the + escape key. + * src/gnome-utils/gnc-date-edit.c: Show widget by default when its created for a glade dialog. diff --git a/src/engine/gnc-pricedb.c b/src/engine/gnc-pricedb.c index 596c7d654a..147d55ca19 100644 --- a/src/engine/gnc-pricedb.c +++ b/src/engine/gnc-pricedb.c @@ -964,6 +964,8 @@ gnc_pricedb_remove_price(GNCPriceDB *db, GNCPrice *p) typedef struct { GNCPriceDB *db; Timespec cutoff; + gboolean delete_user; + gboolean delete_last; GSList *list; } remove_info; @@ -971,27 +973,96 @@ static gboolean check_one_price_date (GNCPrice *price, gpointer user_data) { remove_info *data = user_data; + const gchar *source; Timespec pt; + ENTER("price %p (%s), data %p", price, + gnc_commodity_get_mnemonic(gnc_price_get_commodity(price)), + user_data); + if (!data->delete_user) { + source = gnc_price_get_source (price); + if (strcmp(source, "Finance::Quote") != 0) { + LEAVE("Not an automatic quote"); + return TRUE; + } + } + pt = gnc_price_get_time (price); - if (timespec_cmp (&pt, &data->cutoff) < 0) + { + gchar buf[40]; + gnc_timespec_to_iso8601_buff(pt , buf); + DEBUG("checking date %s", buf); + } + if (timespec_cmp (&pt, &data->cutoff) < 0) { data->list = g_slist_prepend(data->list, price); + DEBUG("will delete"); + } + LEAVE(" "); return TRUE; } +static void +pricedb_remove_foreach_pricelist (gpointer key, + gpointer val, + gpointer user_data) +{ + GList *price_list = (GList *) val; + GList *node = price_list; + remove_info *data = (remove_info *) user_data; + + ENTER("key %p, value %p, data %p", key, val, user_data); + + /* The most recent price is the first in the list */ + if (!data->delete_last) + node = g_list_next(node); + + /* now check each item in the list */ + g_list_foreach(node, (GFunc)check_one_price_date, data); + + LEAVE(" "); +} + +static void +pricedb_remove_foreach_currencies_hash (gpointer key, + gpointer val, + gpointer user_data) +{ + GHashTable *currencies_hash = (GHashTable *) val; + + ENTER("key %p, value %p, data %p", key, val, user_data); + g_hash_table_foreach(currencies_hash, + pricedb_remove_foreach_pricelist, user_data); + LEAVE(" "); +} + + gboolean -gnc_pricedb_remove_old_prices(GNCPriceDB *db, Timespec cutoff) +gnc_pricedb_remove_old_prices(GNCPriceDB *db, + Timespec cutoff, + gboolean delete_user, + gboolean delete_last) { remove_info data; GSList *item; data.db = db; data.cutoff = cutoff; + data.delete_user = delete_user; + data.delete_last = delete_last; data.list = NULL; + ENTER("db %p, delet_user %d, delete_last %d", db, delete_user, delete_last); + { + gchar buf[40]; + gnc_timespec_to_iso8601_buff(cutoff, buf); + DEBUG("checking date %s", buf); + } + /* Traverse the database once building up an external list of prices * to be deleted */ - gnc_pricedb_foreach_price(db, check_one_price_date, &data, FALSE); + g_hash_table_foreach(db->commodity_hash, + pricedb_remove_foreach_currencies_hash, + &data); if (data.list == NULL) return FALSE; @@ -1002,6 +1073,7 @@ gnc_pricedb_remove_old_prices(GNCPriceDB *db, Timespec cutoff) } g_slist_free(data.list); + LEAVE(" "); return TRUE; } diff --git a/src/engine/gnc-pricedb.h b/src/engine/gnc-pricedb.h index c0a77e0b6d..a98c2355e3 100644 --- a/src/engine/gnc-pricedb.h +++ b/src/engine/gnc-pricedb.h @@ -279,7 +279,8 @@ gboolean gnc_pricedb_add_price(GNCPriceDB *db, GNCPrice *p); pricedb. Returns TRUE if successful, FALSE otherwise. */ gboolean gnc_pricedb_remove_price(GNCPriceDB *db, GNCPrice *p); -gboolean gnc_pricedb_remove_old_prices(GNCPriceDB *db, Timespec cutoff); +gboolean gnc_pricedb_remove_old_prices(GNCPriceDB *db, Timespec cutoff, + gboolean delete_user, gboolean delete_last); /** gnc_pricedb_lookup_latest - find the most recent price for the given commodity in the given currency. Returns NULL on diff --git a/src/gnome/dialog-price-edit-db.c b/src/gnome/dialog-price-edit-db.c index 31e004774a..684fbfd806 100644 --- a/src/gnome/dialog-price-edit-db.c +++ b/src/gnome/dialog-price-edit-db.c @@ -53,6 +53,7 @@ static QofLogModule log_module = GNC_MOD_GUI; void gnc_prices_dialog_window_destroy_cb (GtkObject *object, gpointer data); +void gnc_prices_dialog_close_cb (GtkDialog *dialog, gpointer data); void gnc_prices_dialog_response (GtkDialog *dialog, gint response_id, gpointer data); void gnc_prices_dialog_edit_clicked (GtkWidget *widget, gpointer data); void gnc_prices_dialog_remove_clicked (GtkWidget *widget, gpointer data); @@ -69,7 +70,6 @@ typedef struct GtkWidget * edit_button; GtkWidget * remove_button; - GtkWidget * remove_old_button; GNCPriceDB *price_db; GNCPrice * price; /* Currently selected price */ @@ -90,10 +90,25 @@ gnc_prices_dialog_window_destroy_cb (GtkObject *object, gpointer data) pdb_dialog->price = NULL; } + if (pdb_dialog->dialog) { + gtk_widget_destroy(pdb_dialog->dialog); + pdb_dialog->dialog = NULL; + } + g_free (pdb_dialog); LEAVE(" "); } +void +gnc_prices_dialog_close_cb (GtkDialog *dialog, gpointer data) +{ + PricesDialog *pdb_dialog = data; + + ENTER(" "); + gnc_close_gui_component_by_data (DIALOG_PRICE_DB_CM_CLASS, pdb_dialog); + LEAVE(" "); +} + void gnc_prices_dialog_response (GtkDialog *dialog, gint response_id, gpointer data) { @@ -146,42 +161,20 @@ void gnc_prices_dialog_remove_old_clicked (GtkWidget *widget, gpointer data) { PricesDialog *pdb_dialog = data; - GtkWidget *dialog; - GtkWidget *label; - GtkWidget *date; - GtkWidget *vbox; + GladeXML *xml; + GtkWidget *dialog, *button, *date; gint result; + gboolean delete_user, delete_last; ENTER(" "); - dialog = gtk_dialog_new_with_buttons (_("Remove old prices"), - GTK_WINDOW (pdb_dialog->dialog), - GTK_DIALOG_DESTROY_WITH_PARENT, - GTK_STOCK_CANCEL, - GTK_RESPONSE_REJECT, - GTK_STOCK_OK, - GTK_RESPONSE_ACCEPT, - NULL); - - vbox = GTK_DIALOG (dialog)->vbox; - - gtk_box_set_spacing (GTK_BOX (vbox), 3); - gtk_container_set_border_width (GTK_CONTAINER (vbox), 3); - - label = gtk_label_new (_("All prices before the date below " - "will be deleted.")); - - gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0); - gtk_widget_show (label); - - date = gnc_date_edit_new (time (NULL), FALSE, FALSE); - g_object_ref (date); - gtk_object_sink (GTK_OBJECT (date)); - - gtk_box_pack_start (GTK_BOX (vbox), date, FALSE, FALSE, 0); - gtk_widget_show (date); + xml = gnc_glade_xml_new ("price.glade", "Deletion Date"); + dialog = glade_xml_get_widget (xml, "Deletion Date"); + date = glade_xml_get_widget (xml, "date"); + glade_xml_signal_autoconnect_full(xml, gnc_glade_autoconnect_full_func, pdb_dialog); + gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (pdb_dialog->dialog)); result = gtk_dialog_run (GTK_DIALOG (dialog)); - if (result == GTK_RESPONSE_ACCEPT) + if (result == GTK_RESPONSE_OK) { GNCBook *book = gnc_get_current_book (); GNCPriceDB *pdb = gnc_book_get_pricedb (book); @@ -191,10 +184,14 @@ gnc_prices_dialog_remove_old_clicked (GtkWidget *widget, gpointer data) ts.tv_sec = gnc_date_edit_get_date (GNC_DATE_EDIT (date)); ts.tv_nsec = 0; - gnc_pricedb_remove_old_prices(pdb, ts); + button = glade_xml_get_widget (xml, "delete_manual"); + delete_user = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)); + button = glade_xml_get_widget (xml, "delete_last"); + delete_last = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)); + + gnc_pricedb_remove_old_prices(pdb, ts, delete_user, delete_last); } - g_object_unref (date); gtk_widget_destroy(dialog); LEAVE(" "); } @@ -256,8 +253,6 @@ gnc_prices_dialog_selection_changed (GtkTreeSelection *treeselection, pdb_dialog->price != NULL); gtk_widget_set_sensitive (pdb_dialog->remove_button, pdb_dialog->price != NULL); - gtk_widget_set_sensitive (pdb_dialog->remove_old_button, - pdb_dialog->price != NULL); LEAVE(" "); } @@ -364,9 +359,6 @@ gnc_prices_dialog_create (GtkWidget * parent, PricesDialog *pdb_dialog) button = glade_xml_get_widget (xml, "remove_button"); pdb_dialog->remove_button = button; - - button = glade_xml_get_widget (xml, "remove_old_button"); - pdb_dialog->remove_old_button = button; } gnc_restore_window_size(GCONF_SECTION, GTK_WINDOW(pdb_dialog->dialog)); diff --git a/src/gnome/glade/price.glade b/src/gnome/glade/price.glade index fb1ef67b55..399182fd5f 100644 --- a/src/gnome/glade/price.glade +++ b/src/gnome/glade/price.glade @@ -20,6 +20,7 @@ True + @@ -123,79 +124,6 @@ - - - True - False - Remove prices older than a user-entered date - True - True - GTK_RELIEF_NORMAL - True - - - - - True - 0.5 - 0.5 - 0 - 0 - 0 - 0 - 0 - 0 - - - - True - False - 2 - - - - True - gtk-remove - 4 - 0.5 - 0.5 - 0 - 0 - - - 0 - False - False - - - - - - True - Remove _Old - True - False - GTK_JUSTIFY_LEFT - False - False - 0.5 - 0.5 - 0 - 0 - - - 0 - False - False - - - - - - - - - True @@ -269,6 +197,78 @@ + + + True + Remove prices older than a user-entered date + True + True + GTK_RELIEF_NORMAL + True + + + + + True + 0.5 + 0.5 + 0 + 0 + 0 + 0 + 0 + 0 + + + + True + False + 2 + + + + True + gtk-remove + 4 + 0.5 + 0.5 + 0 + 0 + + + 0 + False + False + + + + + + True + Remove _Old + True + False + GTK_JUSTIFY_LEFT + False + False + 0.5 + 0.5 + 0 + 0 + + + 0 + False + False + + + + + + + + + True @@ -658,7 +658,7 @@ 0 True - * + * False @@ -762,4 +762,159 @@ + + True + + GTK_WINDOW_TOPLEVEL + GTK_WIN_POS_NONE + False + True + False + True + False + False + GDK_WINDOW_TYPE_HINT_DIALOG + GDK_GRAVITY_NORTH_WEST + True + + + + True + False + 0 + + + + True + GTK_BUTTONBOX_END + + + + True + True + True + gtk-cancel + True + GTK_RELIEF_NORMAL + True + -6 + + + + + + True + True + True + gtk-ok + True + GTK_RELIEF_NORMAL + True + -5 + + + + + 0 + False + True + GTK_PACK_END + + + + + + True + False + 0 + + + + True + Delete all downloaded prices +dated earlier than this date. + False + False + GTK_JUSTIFY_LEFT + True + False + 0.5 + 0.5 + 0 + 0 + + + 0 + False + False + + + + + + True + gnc_date_edit_new_glade + 0 + 0 + Fri, 25 Nov 2005 22:13:25 GMT + + + 0 + True + True + + + + + + True + True + Delete _manually entered prices too + True + GTK_RELIEF_NORMAL + True + False + False + True + + + 0 + False + False + + + + + + True + True + Delete _last price for a stock + True + GTK_RELIEF_NORMAL + True + False + False + True + + + 0 + False + False + + + + + + + + + 0 + True + True + + + + + +