GnuCash  5.6-150-g038405b370+
dialog-price-editor.c
1 /********************************************************************\
2  * dialog-price-editor.c -- price editor dialog *
3  * Copyright (C) 2001 Gnumatic, Inc. *
4  * Author: Dave Peticolas <dave@krondo.com> *
5  * Copyright (c) 2006 David Hampton <hampton@employees.org> *
6  * Copyright (c) 2009 Herbert Thoma <herbie@hthoma.de> *
7  * Copyright (c) 2011 Robert Fewell *
8  * *
9  * This program is free software; you can redistribute it and/or *
10  * modify it under the terms of the GNU General Public License as *
11  * published by the Free Software Foundation; either version 2 of *
12  * the License, or (at your option) any later version. *
13  * *
14  * This program is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17  * GNU General Public License for more details. *
18  * *
19  * You should have received a copy of the GNU General Public License*
20  * along with this program; if not, contact: *
21  * *
22  * Free Software Foundation Voice: +1-617-542-5942 *
23  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
24  * Boston, MA 02110-1301, USA gnu@gnu.org *
25 \********************************************************************/
26 
27 #include <config.h>
28 
29 #include <gtk/gtk.h>
30 #include <glib/gi18n.h>
31 #include <time.h>
32 
33 #include "dialog-utils.h"
34 #include "gnc-gtk-utils.h"
35 #include "gnc-amount-edit.h"
36 #include "gnc-commodity-edit.h"
37 #include "dialog-commodity.h"
38 #include "gnc-general-select.h"
39 #include "gnc-component-manager.h"
40 #include "gnc-currency-edit.h"
41 #include "gnc-date-edit.h"
42 #include "qof.h"
43 #include "gnc-pricedb.h"
44 #include "gnc-session.h"
45 #include "gnc-ui.h"
46 #include "gnc-ui-util.h"
47 #include "gnc-warnings.h"
48 #include "engine-helpers.h"
49 
50 
51 #define DIALOG_PRICE_EDIT_CM_CLASS "dialog-price-edit"
52 #define GNC_PREFS_GROUP "dialogs.price-editor"
53 
54 /* This static indicates the debugging module that this .o belongs to. */
55 G_GNUC_UNUSED static QofLogModule log_module = GNC_MOD_GUI;
56 
57 
58 typedef struct
59 {
60  GtkWidget * dialog;
61  QofSession *session;
62  QofBook *book;
63  GNCPriceDB *price_db;
64  GNCPriceEditType type;
65 
66  GtkWidget * namespace_cbwe;
67  GtkWidget * commodity_cbwe;
68  GtkWidget * currency_edit;
69  GtkWidget * date_edit;
70  GtkWidget * source_entry;
71  GtkWidget * type_combobox;
72  GtkWidget * price_edit;
73 
74  GtkWidget * help_button;
75  GtkWidget * cancel_button;
76  GtkWidget * apply_button;
77  GtkWidget * ok_button;
78 
79  GNCPrice *price;
80  gboolean changed;
81  gboolean is_new;
82 
84 
85 void pedit_dialog_response_cb (GtkDialog *dialog, gint response, gpointer data);
86 void pedit_data_changed_cb (GtkWidget *w, gpointer data);
87 void pedit_commodity_ns_changed_cb (GtkComboBox *cbwe, gpointer data);
88 void pedit_commodity_changed_cb (GtkComboBox *cbwe, gpointer data);
89 
90 
91 static void
92 gnc_prices_set_changed (PriceEditDialog *pedit_dialog, gboolean changed)
93 {
94  pedit_dialog->changed = changed;
95 
96  gtk_widget_set_sensitive (pedit_dialog->apply_button, changed);
97  gtk_widget_set_sensitive (pedit_dialog->ok_button, changed);
98 }
99 
100 
101 static int
102 type_string_to_index (const char *type)
103 {
104  if (g_strcmp0 (type, "bid") == 0)
105  return 0;
106 
107  if (g_strcmp0 (type, "ask") == 0)
108  return 1;
109 
110  if (g_strcmp0 (type, "last") == 0)
111  return 2;
112 
113  if (g_strcmp0 (type, "nav") == 0)
114  return 3;
115 
116  return 4;
117 }
118 
119 
120 static const char *
121 type_index_to_string (int index)
122 {
123  switch (index)
124  {
125  case 0:
126  return "bid";
127  case 1:
128  return "ask";
129  case 2:
130  return "last";
131  case 3:
132  return "nav";
133  default:
134  return "unknown";
135  }
136 }
137 
138 
139 static void
140 price_to_gui (PriceEditDialog *pedit_dialog)
141 {
142  GNCPrintAmountInfo print_info;
143  gnc_commodity *commodity = NULL;
144  gnc_commodity *currency = NULL;
145  const gchar *name_space, *fullname;
146  const char *source;
147  const char *type;
148  gnc_numeric value;
149  time64 date;
150 
151  if (pedit_dialog->price)
152  {
153  commodity = gnc_price_get_commodity (pedit_dialog->price);
154  }
155 
156  if (commodity)
157  {
158  name_space = gnc_commodity_get_namespace(commodity);
159  fullname = gnc_commodity_get_printname(commodity);
160  gnc_ui_update_namespace_picker(pedit_dialog->namespace_cbwe,
161  name_space, DIAG_COMM_ALL);
162  gnc_ui_update_commodity_picker(pedit_dialog->commodity_cbwe,
163  name_space, fullname);
164 
165  currency = gnc_price_get_currency (pedit_dialog->price);
166  date = gnc_price_get_time64 (pedit_dialog->price);
167  source = gnc_price_get_source_string (pedit_dialog->price);
168  type = gnc_price_get_typestr (pedit_dialog->price);
169  value = gnc_price_get_value (pedit_dialog->price);
170  }
171  else
172  {
173  currency = gnc_default_currency ();
174  date = gnc_time (NULL);
175  source = "user:price-editor"; //Sync with source_names in gnc-pricedb.c
176  type = "";
177  value = gnc_numeric_zero ();
178  }
179 
180 
181  if (currency)
182  {
184  (GNC_CURRENCY_EDIT (pedit_dialog->currency_edit), currency);
185  }
186 
187  gnc_date_edit_set_time (GNC_DATE_EDIT (pedit_dialog->date_edit), date);
188 
189  gtk_entry_set_text (GTK_ENTRY (pedit_dialog->source_entry), source);
190 
191  gtk_combo_box_set_active (GTK_COMBO_BOX(pedit_dialog->type_combobox),
192  type_string_to_index (type));
193 
194  print_info = gnc_commodity_print_info (currency, FALSE);
195  gnc_amount_edit_set_print_info (GNC_AMOUNT_EDIT (pedit_dialog->price_edit), print_info);
196  gnc_amount_edit_set_fraction (GNC_AMOUNT_EDIT (pedit_dialog->price_edit), 0);
197 
198  gnc_amount_edit_set_amount (GNC_AMOUNT_EDIT (pedit_dialog->price_edit), value);
199 }
200 
201 
202 static gboolean
203 pedit_dialog_replace_found_price (PriceEditDialog *pedit_dialog,
204  const gnc_commodity *commodity,
205  const gnc_commodity *currency, time64 t)
206 {
207  gboolean price_found = FALSE;
208  GNCPrice *test_price = gnc_pricedb_lookup_day_t64 (pedit_dialog->price_db,
209  commodity, currency, t);
210 
211  if (test_price)
212  {
213  if (pedit_dialog->is_new) // new price
214  price_found = TRUE;
215  else // edit price
216  {
217  if (!gnc_price_equal (test_price, pedit_dialog->price))
218  price_found = TRUE;
219  }
220  gnc_price_unref (test_price);
221  }
222 
223  if (price_found)
224  {
225  gint response;
226  GtkWidget *dialog;
227  gchar *message = _("Are you sure you want to replace the existing price?");
228 
229  dialog = gtk_message_dialog_new (GTK_WINDOW (pedit_dialog->dialog),
230  GTK_DIALOG_DESTROY_WITH_PARENT,
231  GTK_MESSAGE_QUESTION,
232  GTK_BUTTONS_NONE,
233  "%s", _("Replace price?"));
234  gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG(dialog),
235  "%s", message);
236 
237  gtk_dialog_add_buttons (GTK_DIALOG(dialog),
238  _("_Cancel"), GTK_RESPONSE_CANCEL,
239  _("_Replace"), GTK_RESPONSE_YES,
240  (gchar *)NULL);
241  gtk_dialog_set_default_response (GTK_DIALOG(dialog), GTK_RESPONSE_YES);
242  response = gnc_dialog_run (GTK_DIALOG(dialog), GNC_PREF_WARN_PRICE_QUOTES_REPLACE);
243  gtk_widget_destroy (dialog);
244 
245  if (response == GTK_RESPONSE_CANCEL)
246  return FALSE;
247  }
248  return TRUE;
249 }
250 
251 
252 static const char *
253 gui_to_price (PriceEditDialog *pedit_dialog)
254 {
255  GNCPrintAmountInfo print_info;
256  gnc_commodity *commodity;
257  gnc_commodity *currency;
258  gchar *name_space;
259  const gchar *fullname;
260  const char *source;
261  const char *type;
262  gnc_numeric value;
263  time64 date;
264 
265  name_space = gnc_ui_namespace_picker_ns (pedit_dialog->namespace_cbwe);
266  fullname = gtk_entry_get_text( GTK_ENTRY( gtk_bin_get_child( GTK_BIN( GTK_COMBO_BOX(pedit_dialog->commodity_cbwe)))));
267 
268  commodity = gnc_commodity_table_find_full(gnc_get_current_commodities(), name_space, fullname);
269  if (!commodity)
270  return _("You must select a Security.");
271 
273  (GNC_CURRENCY_EDIT (pedit_dialog->currency_edit));
274  if (!currency)
275  return _("You must select a Currency.");
276 
277  date = gnc_date_edit_get_date (GNC_DATE_EDIT (pedit_dialog->date_edit));
278 
279  source = gtk_entry_get_text (GTK_ENTRY (pedit_dialog->source_entry));
280 
281  type = type_index_to_string
282  (gtk_combo_box_get_active (GTK_COMBO_BOX (pedit_dialog->type_combobox)));
283 
284  print_info = gnc_commodity_print_info (currency, FALSE);
285  gnc_amount_edit_set_print_info (GNC_AMOUNT_EDIT (pedit_dialog->price_edit), print_info);
286  gnc_amount_edit_set_fraction (GNC_AMOUNT_EDIT (pedit_dialog->price_edit), 0);
287 
288  if (!gnc_amount_edit_evaluate (GNC_AMOUNT_EDIT (pedit_dialog->price_edit), NULL))
289  return _("You must enter a valid amount.");
290 
291  value = gnc_amount_edit_get_amount
292  (GNC_AMOUNT_EDIT (pedit_dialog->price_edit));
293 
294  // test for existing price on same day
295  if (pedit_dialog_replace_found_price (pedit_dialog, commodity, currency, date))
296  {
297  if (!pedit_dialog->price)
298  pedit_dialog->price = gnc_price_create (pedit_dialog->book);
299  gnc_price_begin_edit (pedit_dialog->price);
300  gnc_price_set_commodity (pedit_dialog->price, commodity);
301  gnc_price_set_currency (pedit_dialog->price, currency);
302  gnc_price_set_time64 (pedit_dialog->price, date);
303  gnc_price_set_source_string (pedit_dialog->price, source);
304  gnc_price_set_typestr (pedit_dialog->price, type);
305  gnc_price_set_value (pedit_dialog->price, value);
306  gnc_price_commit_edit (pedit_dialog->price);
307  g_free (name_space);
308  return NULL;
309  }
310  else
311  {
312  g_free (name_space);
313  return "CANCEL";
314  }
315 }
316 
317 
318 static void
319 pedit_dialog_destroy_cb (GtkWidget *widget, gpointer data)
320 {
321  PriceEditDialog *pedit_dialog = data;
322 
323  gnc_unregister_gui_component_by_data (DIALOG_PRICE_EDIT_CM_CLASS,
324  pedit_dialog);
325 
326  if (pedit_dialog->price)
327  {
328  gnc_price_unref (pedit_dialog->price);
329  pedit_dialog->price = NULL;
330  pedit_dialog->is_new = FALSE;
331  }
332 
333  g_free (pedit_dialog);
334 }
335 
336 
337 void
338 pedit_dialog_response_cb (GtkDialog *dialog, gint response, gpointer data)
339 {
340  PriceEditDialog *pedit_dialog = data;
341  GNCPrice *new_price = NULL;
342  const char *error_str;
343 
344  if ((response == GTK_RESPONSE_OK) || (response == GTK_RESPONSE_APPLY))
345  {
346  error_str = gui_to_price (pedit_dialog);
347  if (g_strcmp0 (error_str, "CANCEL") == 0) // cancel from replace price dialog
348  {
349  // set the ok and cancel buttons sensitivity
350  gnc_prices_set_changed (pedit_dialog, FALSE);
351  return;
352  }
353  else if (error_str) // error string from gui
354  {
355  gnc_warning_dialog (GTK_WINDOW (pedit_dialog->dialog), "%s", error_str);
356  return;
357  }
358  // set the ok and cancel buttons sensitivity
359  gnc_prices_set_changed (pedit_dialog, FALSE);
360 
361  if (pedit_dialog->is_new)
362  gnc_pricedb_add_price (pedit_dialog->price_db, pedit_dialog->price);
363 
364  gnc_gui_refresh_all ();
365  }
366 
367  if (response == GTK_RESPONSE_APPLY)
368  {
369  new_price = gnc_price_clone (pedit_dialog->price, pedit_dialog->book);
370  pedit_dialog->is_new = TRUE;
371 
372  gnc_price_unref (pedit_dialog->price);
373  pedit_dialog->price = new_price;
374  }
375  else if (response == GTK_RESPONSE_HELP)
376  {
377  gnc_gnome_help (GTK_WINDOW (pedit_dialog->dialog), DF_MANUAL, DL_PRICE_EDIT);
378  }
379  else
380  {
381  gnc_save_window_size(GNC_PREFS_GROUP, GTK_WINDOW(pedit_dialog->dialog));
382  gtk_widget_destroy (GTK_WIDGET (pedit_dialog->dialog));
383  pedit_dialog_destroy_cb (NULL, pedit_dialog);
384  }
385 }
386 
387 
388 void
389 pedit_commodity_ns_changed_cb (GtkComboBox *cbwe, gpointer data)
390 {
391  PriceEditDialog *pedit_dialog = data;
392  gchar *name_space;
393 
394  gnc_prices_set_changed (pedit_dialog, TRUE);
395 
396  name_space = gnc_ui_namespace_picker_ns (pedit_dialog->namespace_cbwe);
397  gnc_ui_update_commodity_picker (pedit_dialog->commodity_cbwe, name_space, NULL);
398 
399  g_free(name_space);
400 }
401 
402 
403 void
404 pedit_commodity_changed_cb (GtkComboBox *cbwe, gpointer data)
405 {
406  gnc_commodity *commodity = NULL;
407  gnc_commodity *currency = NULL;
408  gchar *name_space;
409  const gchar *fullname;
410  GList *price_list;
411  PriceEditDialog *pedit_dialog = data;
412 
413  gnc_prices_set_changed (pedit_dialog, TRUE);
414 
415  name_space = gnc_ui_namespace_picker_ns (pedit_dialog->namespace_cbwe);
416  fullname = gtk_entry_get_text( GTK_ENTRY( gtk_bin_get_child( GTK_BIN( GTK_COMBO_BOX(pedit_dialog->commodity_cbwe)))));
417 
418  commodity = gnc_commodity_table_find_full(gnc_get_current_commodities(), name_space, fullname);
419 
420  if (commodity)
421  {
423  (pedit_dialog->price_db, commodity);
424  if (price_list)
425  {
426  GNCPrice * price = (GNCPrice*)price_list->data;
427  if (gnc_commodity_equiv(commodity, gnc_price_get_currency(price)))
428  currency = gnc_price_get_commodity((GNCPrice *)price);
429  else
430  currency = gnc_price_get_currency((GNCPrice *)price);
431 
432  if (currency)
434  (GNC_CURRENCY_EDIT (pedit_dialog->currency_edit), currency);
435 
436  gnc_price_list_destroy(price_list);
437  }
438  else
439  {
441  (GNC_CURRENCY_EDIT (pedit_dialog->currency_edit), gnc_default_currency());
442  }
443  }
444 
445  g_free(name_space);
446 }
447 
448 
449 void
450 pedit_data_changed_cb (GtkWidget *w, gpointer data)
451 {
452  PriceEditDialog *pedit_dialog = data;
453 
454  gnc_prices_set_changed (pedit_dialog, TRUE);
455 }
456 
457 
458 static void
459 gnc_price_pedit_dialog_create (GtkWidget *parent,
460  PriceEditDialog *pedit_dialog,
461  QofSession *session)
462 {
463  GtkBuilder *builder;
464  GNCPrintAmountInfo print_info;
465  GtkWidget *dialog;
466  GtkWidget *entry;
467  GtkWidget *box;
468  GtkWidget *w;
469  GtkWidget *label;
470  gchar *name_space;
471 
472  builder = gtk_builder_new();
473  gnc_builder_add_from_file (builder, "dialog-price.glade", "liststore1");
474  gnc_builder_add_from_file (builder, "dialog-price.glade", "liststore2");
475  gnc_builder_add_from_file (builder, "dialog-price.glade", "liststore3");
476  gnc_builder_add_from_file (builder, "dialog-price.glade", "price_dialog");
477 
478  pedit_dialog->session = session;
479  pedit_dialog->book = qof_session_get_book(pedit_dialog->session);
480  pedit_dialog->price_db = gnc_pricedb_get_db(pedit_dialog->book);
481 
482  dialog = GTK_WIDGET(gtk_builder_get_object (builder, "price_dialog"));
483  pedit_dialog->dialog = dialog;
484 
485  /* parent */
486  if (parent != NULL)
487  gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent));
488 
489  w = GTK_WIDGET(gtk_builder_get_object (builder, "namespace_cbwe"));
490  pedit_dialog->namespace_cbwe = w;
491 
493  gnc_cbwe_require_list_item(GTK_COMBO_BOX(pedit_dialog->namespace_cbwe));
494  gtk_combo_box_set_active(GTK_COMBO_BOX(pedit_dialog->namespace_cbwe), 1);
495 
496  w = GTK_WIDGET(gtk_builder_get_object (builder, "commodity_cbwe"));
497  pedit_dialog->commodity_cbwe = w;
498 
499  gnc_cbwe_require_list_item(GTK_COMBO_BOX(pedit_dialog->commodity_cbwe));
500  name_space = gnc_ui_namespace_picker_ns(pedit_dialog->namespace_cbwe);
501  gnc_ui_update_commodity_picker(pedit_dialog->commodity_cbwe, name_space, NULL);
502  g_free(name_space);
503 
504  box = GTK_WIDGET(gtk_builder_get_object (builder, "currency_box"));
505  w = gnc_currency_edit_new ();
506  gnc_currency_edit_set_currency (GNC_CURRENCY_EDIT (w),
508  pedit_dialog->currency_edit = w;
509  gtk_box_pack_start (GTK_BOX (box), w, TRUE, TRUE, 0);
510  gtk_widget_show (w);
511  g_signal_connect (G_OBJECT (GTK_COMBO_BOX(w)), "changed",
512  G_CALLBACK (pedit_data_changed_cb), pedit_dialog);
513  label = GTK_WIDGET(gtk_builder_get_object (builder, "currency_label"));
514  gtk_label_set_mnemonic_widget (GTK_LABEL(label), w);
515 
516  box = GTK_WIDGET(gtk_builder_get_object (builder, "date_box"));
517  w = gnc_date_edit_new (time (NULL), FALSE, FALSE);
518  pedit_dialog->date_edit = w;
519  gtk_box_pack_start (GTK_BOX (box), w, TRUE, TRUE, 0);
520  gtk_widget_show (w);
521  g_signal_connect (G_OBJECT (w), "date_changed",
522  G_CALLBACK (pedit_data_changed_cb), pedit_dialog);
523  g_signal_connect (G_OBJECT (GNC_DATE_EDIT (w)->date_entry), "changed",
524  G_CALLBACK (pedit_data_changed_cb), pedit_dialog);
525  gtk_entry_set_activates_default(GTK_ENTRY(GNC_DATE_EDIT(w)->date_entry), TRUE);
526  label = GTK_WIDGET(gtk_builder_get_object (builder, "date__label"));
527  gnc_date_make_mnemonic_target (GNC_DATE_EDIT(w), label);
528 
529  w = GTK_WIDGET(gtk_builder_get_object (builder, "source_entry"));
530  pedit_dialog->source_entry = w;
531 
532  w = GTK_WIDGET(gtk_builder_get_object (builder, "type_combobox"));
533  pedit_dialog->type_combobox = w;
534 
535  box = GTK_WIDGET(gtk_builder_get_object (builder, "price_box"));
536  w = gnc_amount_edit_new ();
537  pedit_dialog->price_edit = w;
538  gtk_box_pack_start (GTK_BOX (box), w, TRUE, TRUE, 0);
539  entry = gnc_amount_edit_gtk_entry (GNC_AMOUNT_EDIT (w));
540  gnc_amount_edit_set_evaluate_on_enter (GNC_AMOUNT_EDIT (w), TRUE);
541  print_info = gnc_default_price_print_info (gnc_currency_edit_get_currency
542  (GNC_CURRENCY_EDIT (pedit_dialog->currency_edit)));
543  gnc_amount_edit_set_print_info (GNC_AMOUNT_EDIT (w), print_info);
544  gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
545  gtk_widget_show (w);
546  label = GTK_WIDGET(gtk_builder_get_object (builder, "price_label"));
547  gnc_amount_edit_make_mnemonic_target (GNC_AMOUNT_EDIT(w), label);
548 
549  g_signal_connect (G_OBJECT (w), "changed",
550  G_CALLBACK (pedit_data_changed_cb), pedit_dialog);
551 
552  w = GTK_WIDGET(gtk_builder_get_object (builder, "pd_help_button"));
553  pedit_dialog->help_button = w;
554 
555  w = GTK_WIDGET(gtk_builder_get_object (builder, "pd_cancel_button"));
556  pedit_dialog->cancel_button = w;
557 
558  w = GTK_WIDGET(gtk_builder_get_object (builder, "pd_apply_button"));
559  pedit_dialog->apply_button = w;
560 
561  w = GTK_WIDGET(gtk_builder_get_object (builder, "pd_ok_button"));
562  pedit_dialog->ok_button = w;
563 
564  gnc_prices_set_changed (pedit_dialog, FALSE);
565 
566  gtk_builder_connect_signals_full (builder, gnc_builder_connect_full_func, pedit_dialog);
567 
568  g_object_unref(G_OBJECT(builder));
569 }
570 
571 
572 static void
573 close_handler (gpointer user_data)
574 {
575  PriceEditDialog *pedit_dialog = user_data;
576 
577  gtk_dialog_response(GTK_DIALOG(pedit_dialog->dialog), GTK_RESPONSE_CANCEL);
578 }
579 
580 
581 static void
582 refresh_handler (GHashTable *changes, gpointer user_data)
583 {
584  // PriceEditDialog *pedit_dialog = user_data;
585 
586  // gnc_prices_load_prices (pedit_dialog);
587 }
588 
589 
590 static gboolean
591 show_handler (const char *klass, gint component_id,
592  gpointer user_data, gpointer iter_data)
593 {
594  PriceEditDialog *pedit_dialog = user_data;
595  GNCPrice * price = iter_data;
596 
597  if (!pedit_dialog || (pedit_dialog->price != price))
598  return(FALSE);
599 
600  gtk_window_present (GTK_WINDOW(pedit_dialog->dialog));
601  return(TRUE);
602 }
603 
604 
605 /********************************************************************\
606  * gnc_price_edit_dialog *
607  * opens up a window to edit price information *
608  * *
609  * Args: parent - the parent of the window to be created *
610  * Return: nothing *
611 \********************************************************************/
612 void
613 gnc_price_edit_dialog (GtkWidget * parent,
614  QofSession *session,
615  GNCPrice * price,
616  GNCPriceEditType type)
617 {
618  PriceEditDialog *pedit_dialog;
619  gint component_id;
620 
621  if ((type == GNC_PRICE_EDIT) &&
622  (gnc_forall_gui_components (DIALOG_PRICE_EDIT_CM_CLASS,
623  show_handler, price)))
624  return;
625 
626  pedit_dialog = g_new0 (PriceEditDialog, 1);
627  gnc_price_pedit_dialog_create (parent, pedit_dialog, session);
628  gnc_restore_window_size(GNC_PREFS_GROUP, GTK_WINDOW(pedit_dialog->dialog), GTK_WINDOW(parent));
629  pedit_dialog->type = type;
630 
631  switch (type)
632  {
633  case GNC_PRICE_NEW:
634  if (price)
635  {
636  price = gnc_price_clone(price, pedit_dialog->book);
637 
638  gnc_price_set_source (price, PRICE_SOURCE_EDIT_DLG);
639  gnc_price_set_time64 (price, gnc_time (NULL));
640  gnc_price_set_value (price, gnc_numeric_zero ());
641  }
642 
643  pedit_dialog->is_new = TRUE;
644  /* New price will only have one ref, this dialog. */
645  break;
646  case GNC_PRICE_EDIT:
647  gnc_price_ref(price); /* Add ref from this dialog */
648  pedit_dialog->is_new = FALSE;
649  break;
650  }
651 
652  pedit_dialog->price = price;
653  price_to_gui(pedit_dialog);
654  gnc_prices_set_changed (pedit_dialog, FALSE);
655  component_id = gnc_register_gui_component (DIALOG_PRICE_EDIT_CM_CLASS,
656  refresh_handler, close_handler,
657  pedit_dialog);
658  gnc_gui_component_set_session (component_id, pedit_dialog->session);
659  gtk_widget_grab_focus (pedit_dialog->commodity_cbwe);
660  gtk_widget_show (pedit_dialog->dialog);
661 }
662 
663 
664 /********************************************************************\
665  * gnc_price_edit_by_guid *
666  * opens up a window to edit price information *
667  * *
668  * Args: parent - the parent of the window to be created *
669  * Return: nothing *
670 \********************************************************************/
671 GNCPrice *
672 gnc_price_edit_by_guid (GtkWidget * parent, const GncGUID * guid)
673 {
674  GNCPrice *price;
675  QofSession *session = gnc_get_current_session();
676  QofBook* book = qof_session_get_book (session);
677 
678  if (!book)
679  return (NULL);
680  price = gnc_price_lookup (guid, book);
681  if (price == NULL)
682  return(NULL);
683 
684  gnc_price_edit_dialog(parent, session, price, GNC_PRICE_EDIT);
685  return price;
686 }
void gnc_price_list_destroy(PriceList *prices)
gnc_price_list_destroy - destroy the given price list, calling gnc_price_unref on all the prices incl...
GNCPrice * gnc_pricedb_lookup_day_t64(GNCPriceDB *db, const gnc_commodity *c, const gnc_commodity *currency, time64 t)
Return the price between the two commodities on the indicated day.
GNCPrice * gnc_price_create(QofBook *book)
gnc_price_create - returns a newly allocated and initialized price with a reference count of 1...
void gnc_ui_update_commodity_picker(GtkWidget *cbwe, const gchar *name_space, const gchar *init_string)
Given a combo box, fill in all the known commodities for the specified namespace, and then select one...
void gnc_currency_edit_set_currency(GNCCurrencyEdit *gce, const gnc_commodity *currency)
Set the widget to display a certain currency name.
a simple price database for gnucash
utility functions for the GnuCash UI
gtk helper routines.
gchar * gnc_ui_namespace_picker_ns(GtkWidget *cbwe)
Given a combo box, return the currently selected namespaces.
void gnc_price_unref(GNCPrice *p)
gnc_price_unref - indicate you&#39;re finished with a price (i.e.
gboolean gnc_pricedb_add_price(GNCPriceDB *db, GNCPrice *p)
Add a price to the pricedb.
const char * gnc_commodity_get_namespace(const gnc_commodity *cm)
Retrieve the namespace for the specified commodity.
GNCPriceDB * gnc_pricedb_get_db(QofBook *book)
Return the pricedb associated with the book.
gnc_commodity * gnc_default_currency(void)
Return the default currency set by the user.
Dialog box should allow selection of anything.
Currency selection widget.
QofBook * qof_session_get_book(const QofSession *session)
Returns the QofBook of this session.
Definition: qofsession.cpp:574
void gnc_gnome_help(GtkWindow *parent, const char *file_name, const char *anchor)
Launch the systems default help browser, gnome&#39;s yelp for linux, and open to a given link within a gi...
gnc_commodity * gnc_currency_edit_get_currency(GNCCurrencyEdit *gce)
Retrieve the displayed currency of the widget.
PriceList * gnc_pricedb_lookup_latest_any_currency(GNCPriceDB *db, const gnc_commodity *commodity)
Find the most recent price between a commodity and all other commodities.
const char * gnc_commodity_get_printname(const gnc_commodity *cm)
Retrieve the &#39;print&#39; name for the specified commodity.
GtkWidget * gnc_currency_edit_new(void)
Create a new GNCCurrencyEdit widget which can be used to provide an easy way to enter ISO currency co...
GNCPrice * gnc_price_clone(GNCPrice *p, QofBook *book)
gnc_price_clone - returns a newly allocated price that&#39;s a content-wise duplicate of the given price...
time64 gnc_time(time64 *tbuf)
get the current time
Definition: gnc-date.cpp:261
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
Definition: gnc-date.h:87
The type used to store guids in C.
Definition: guid.h:75
void gnc_ui_update_namespace_picker(GtkWidget *cbwe, const gchar *sel, dialog_commodity_mode mode)
Given a combo box, fill in the known commodity namespaces and then select one.
"select" and "new" commodity windows
void gnc_price_ref(GNCPrice *p)
gnc_price_ref - indicate your need for a given price to stick around (i.e.
gboolean gnc_commodity_equiv(const gnc_commodity *a, const gnc_commodity *b)
This routine returns TRUE if the two commodities are equivalent.