00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "config.h"
00026
00027 #include <gtk/gtk.h>
00028 #include <glib/gi18n.h>
00029
00030 #include "dialog-commodity.h"
00031 #include "dialog-utils.h"
00032 #include "gnc-commodity.h"
00033 #include "gnc-component-manager.h"
00034 #include "qof.h"
00035 #include "gnc-tree-view-commodity.h"
00036 #include "gnc-ui.h"
00037 #include "gnc-ui-util.h"
00038 #include "gnc-gconf-utils.h"
00039 #include "gnc-gnome-utils.h"
00040 #include "gnc-session.h"
00041
00042
00043 #define DIALOG_COMMODITIES_CM_CLASS "dialog-commodities"
00044 #define GCONF_SECTION "dialogs/edit_commodities"
00045
00046
00047
00048
00049 typedef struct
00050 {
00051 GtkWidget * dialog;
00052 QofSession *session;
00053 QofBook *book;
00054
00055 GncTreeViewCommodity * commodity_tree;
00056 GtkWidget * edit_button;
00057 GtkWidget * remove_button;
00058 gboolean show_currencies;
00059
00060 gboolean new;
00061 } CommoditiesDialog;
00062
00063
00064 void gnc_commodities_window_destroy_cb (GtkObject *object, CommoditiesDialog *cd);
00065 void gnc_commodities_dialog_response (GtkDialog *dialog, gint response, CommoditiesDialog *cd);
00066 void gnc_commodities_show_currencies_toggled (GtkToggleButton *toggle, CommoditiesDialog *cd);
00067
00068
00069
00070 void
00071 gnc_commodities_window_destroy_cb (GtkObject *object, CommoditiesDialog *cd)
00072 {
00073 gnc_unregister_gui_component_by_data (DIALOG_COMMODITIES_CM_CLASS, cd);
00074
00075 g_free (cd);
00076 }
00077
00078 static void
00079 edit_clicked (CommoditiesDialog *cd)
00080 {
00081 gnc_commodity *commodity;
00082
00083 commodity = gnc_tree_view_commodity_get_selected_commodity (cd->commodity_tree);
00084 if (commodity == NULL)
00085 return;
00086
00087 if (gnc_ui_edit_commodity_modal (commodity, cd->dialog))
00088 gnc_gui_refresh_all ();
00089 }
00090
00091 static void
00092 row_activated_cb (GtkTreeView *view, GtkTreePath *path,
00093 GtkTreeViewColumn *column, CommoditiesDialog *cd)
00094 {
00095 GtkTreeModel *model;
00096 GtkTreeIter iter;
00097
00098 g_return_if_fail(view);
00099
00100 model = gtk_tree_view_get_model(view);
00101 if (gtk_tree_model_get_iter(model, &iter, path))
00102 {
00103 if (gtk_tree_model_iter_has_child(model, &iter))
00104 {
00105
00106
00107 if (gtk_tree_view_row_expanded(view, path))
00108 gtk_tree_view_collapse_row(view, path);
00109 else
00110 gtk_tree_view_expand_row(view, path, FALSE);
00111 }
00112 else
00113
00114 edit_clicked(cd);
00115 }
00116 }
00117
00118 static void
00119 remove_clicked (CommoditiesDialog *cd)
00120 {
00121 GNCPriceDB *pdb;
00122 GList *node;
00123 GList *prices;
00124 GList *accounts;
00125 gboolean do_delete;
00126 gboolean can_delete;
00127 gnc_commodity *commodity;
00128 GtkWidget *dialog;
00129 const gchar *message, *warning;
00130 gint response;
00131
00132 commodity = gnc_tree_view_commodity_get_selected_commodity (cd->commodity_tree);
00133 if (commodity == NULL)
00134 return;
00135
00136 accounts = gnc_account_get_descendants (gnc_book_get_root_account(cd->book));
00137 can_delete = TRUE;
00138 do_delete = FALSE;
00139
00140 for (node = accounts; node; node = node->next)
00141 {
00142 Account *account = node->data;
00143
00144 if (commodity == xaccAccountGetCommodity (account))
00145 {
00146 can_delete = FALSE;
00147 break;
00148 }
00149 }
00150
00151
00152
00153 if (!can_delete)
00154 {
00155 const char *message = _("That commodity is currently used by "
00156 "at least one of your accounts. You may "
00157 "not delete it.");
00158
00159 gnc_warning_dialog (cd->dialog, "%s", message);
00160 g_list_free (accounts);
00161 return;
00162 }
00163 g_list_free (accounts);
00164
00165 pdb = gnc_pricedb_get_db (cd->book);
00166 prices = gnc_pricedb_get_prices(pdb, commodity, NULL);
00167 if (prices)
00168 {
00169 message = _("This commodity has price quotes. Are "
00170 "you sure you want to delete the selected "
00171 "commodity and its price quotes?");
00172 warning = "delete_commodity2";
00173 }
00174 else
00175 {
00176 message = _("Are you sure you want to delete the "
00177 "selected commodity?");
00178 warning = "delete_commodity";
00179 }
00180
00181 dialog = gtk_message_dialog_new(GTK_WINDOW(cd->dialog),
00182 GTK_DIALOG_DESTROY_WITH_PARENT,
00183 GTK_MESSAGE_QUESTION,
00184 GTK_BUTTONS_NONE,
00185 "%s", _("Delete commodity?"));
00186 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
00187 "%s", message);
00188 gtk_dialog_add_buttons(GTK_DIALOG(dialog),
00189 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
00190 GTK_STOCK_DELETE, GTK_RESPONSE_OK,
00191 (gchar *)NULL);
00192 response = gnc_dialog_run(GTK_DIALOG(dialog), warning);
00193 gtk_widget_destroy(dialog);
00194
00195 if (response == GTK_RESPONSE_OK)
00196 {
00197 gnc_commodity_table *ct;
00198
00199 ct = gnc_commodity_table_get_table (cd->book);
00200 for (node = prices; node; node = node->next)
00201 gnc_pricedb_remove_price(pdb, node->data);
00202
00203 gnc_commodity_table_remove (ct, commodity);
00204 gnc_commodity_destroy (commodity);
00205 commodity = NULL;
00206 }
00207
00208 gnc_price_list_destroy(prices);
00209 gnc_gui_refresh_all ();
00210 }
00211
00212 static void
00213 add_clicked (CommoditiesDialog *cd)
00214 {
00215 gnc_commodity *commodity;
00216 const char *namespace;
00217
00218 commodity = gnc_tree_view_commodity_get_selected_commodity (cd->commodity_tree);
00219 if (commodity)
00220 namespace = gnc_commodity_get_namespace (commodity);
00221 else
00222 namespace = NULL;
00223
00224 commodity = gnc_ui_new_commodity_modal (namespace, cd->dialog);
00225 }
00226
00227 void
00228 gnc_commodities_dialog_response (GtkDialog *dialog,
00229 gint response,
00230 CommoditiesDialog *cd)
00231 {
00232 switch (response)
00233 {
00234 case GNC_RESPONSE_NEW:
00235 add_clicked (cd);
00236 return;
00237
00238 case GNC_RESPONSE_DELETE:
00239 remove_clicked (cd);
00240 return;
00241
00242 case GNC_RESPONSE_EDIT:
00243 edit_clicked (cd);
00244 return;
00245
00246 case GTK_RESPONSE_CLOSE:
00247 default:
00248 gnc_close_gui_component_by_data (DIALOG_COMMODITIES_CM_CLASS, cd);
00249 return;
00250 }
00251 }
00252
00253 static void
00254 gnc_commodities_dialog_selection_changed (GtkTreeSelection *selection,
00255 CommoditiesDialog *cd)
00256 {
00257 gboolean remove_ok;
00258 gnc_commodity *commodity;
00259
00260 commodity = gnc_tree_view_commodity_get_selected_commodity (cd->commodity_tree);
00261 remove_ok = commodity && !gnc_commodity_is_iso(commodity);
00262 gtk_widget_set_sensitive (cd->edit_button, commodity != NULL);
00263 gtk_widget_set_sensitive (cd->remove_button, remove_ok);
00264 }
00265
00266 void
00267 gnc_commodities_show_currencies_toggled (GtkToggleButton *toggle,
00268 CommoditiesDialog *cd)
00269 {
00270
00271 cd->show_currencies = gtk_toggle_button_get_active (toggle);
00272 gnc_tree_view_commodity_refilter (cd->commodity_tree);
00273 }
00274
00275 static gboolean
00276 gnc_commodities_dialog_filter_ns_func (gnc_commodity_namespace *namespace,
00277 gpointer data)
00278 {
00279 CommoditiesDialog *cd = data;
00280 const gchar *name;
00281 GList *list;
00282
00283
00284 name = gnc_commodity_namespace_get_name (namespace);
00285 if (safe_strcmp (name, "template") == 0)
00286 return FALSE;
00287
00288
00289 if (!cd->show_currencies && gnc_commodity_namespace_is_iso(name))
00290 return FALSE;
00291
00292
00293 list = gnc_commodity_namespace_get_commodity_list(namespace);
00294 return (list != NULL);
00295 }
00296
00297 static gboolean
00298 gnc_commodities_dialog_filter_cm_func (gnc_commodity *commodity,
00299 gpointer data)
00300 {
00301 CommoditiesDialog *cd = data;
00302
00303 if (cd->show_currencies)
00304 return TRUE;
00305 return !gnc_commodity_is_iso(commodity);
00306 }
00307
00308 static void
00309 gnc_commodities_dialog_create (GtkWidget * parent, CommoditiesDialog *cd)
00310 {
00311 GtkWidget *dialog;
00312 GtkWidget *button;
00313 GtkWidget *scrolled_window;
00314 GladeXML *xml;
00315 GtkTreeView *view;
00316 GtkTreeSelection *selection;
00317
00318 xml = gnc_glade_xml_new ("commodities.glade", "Securities Dialog");
00319 dialog = glade_xml_get_widget (xml, "Securities Dialog");
00320
00321 cd->dialog = dialog;
00322 cd->session = gnc_get_current_session();
00323 cd->book = qof_session_get_book(cd->session);
00324 cd->show_currencies = gnc_gconf_get_bool(GCONF_SECTION, "include_iso", NULL);
00325
00326 glade_xml_signal_autoconnect_full(xml, gnc_glade_autoconnect_full_func, cd);
00327
00328
00329 if (parent != NULL)
00330 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent));
00331
00332
00333 cd->remove_button = glade_xml_get_widget (xml, "remove_button");
00334 cd->edit_button = glade_xml_get_widget (xml, "edit_button");
00335
00336
00337
00338 scrolled_window = glade_xml_get_widget (xml, "commodity_list_window");
00339 view = gnc_tree_view_commodity_new(cd->book,
00340 "gconf-section", GCONF_SECTION,
00341 "show-column-menu", TRUE,
00342 NULL);
00343 cd->commodity_tree = GNC_TREE_VIEW_COMMODITY(view);
00344 gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET(view));
00345 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(cd->commodity_tree), TRUE);
00346 gnc_tree_view_commodity_set_filter (cd->commodity_tree,
00347 gnc_commodities_dialog_filter_ns_func,
00348 gnc_commodities_dialog_filter_cm_func,
00349 cd, NULL);
00350 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
00351 g_signal_connect (G_OBJECT (selection), "changed",
00352 G_CALLBACK (gnc_commodities_dialog_selection_changed), cd);
00353
00354 g_signal_connect (G_OBJECT (cd->commodity_tree), "row-activated",
00355 G_CALLBACK (row_activated_cb), cd);
00356
00357
00358 button = glade_xml_get_widget (xml, "show_currencies_button");
00359 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(button), cd->show_currencies);
00360
00361 gnc_restore_window_size (GCONF_SECTION, GTK_WINDOW(cd->dialog));
00362 }
00363
00364 static void
00365 close_handler (gpointer user_data)
00366 {
00367 CommoditiesDialog *cd = user_data;
00368
00369 gnc_save_window_size(GCONF_SECTION, GTK_WINDOW(cd->dialog));
00370
00371 gnc_gconf_set_bool(GCONF_SECTION, "include_iso", cd->show_currencies, NULL);
00372
00373 gtk_widget_destroy(cd->dialog);
00374 }
00375
00376 static void
00377 refresh_handler (GHashTable *changes, gpointer user_data)
00378 {
00379 CommoditiesDialog *cd = user_data;
00380
00381 g_return_if_fail(cd != NULL);
00382
00383 gnc_tree_view_commodity_refilter (cd->commodity_tree);
00384 }
00385
00386 static gboolean
00387 show_handler (const char *class, gint component_id,
00388 gpointer user_data, gpointer iter_data)
00389 {
00390 CommoditiesDialog *cd = user_data;
00391
00392 if (!cd)
00393 return(FALSE);
00394 gtk_window_present (GTK_WINDOW(cd->dialog));
00395 return(TRUE);
00396 }
00397
00398
00399
00400
00401
00402
00403
00404
00405 void
00406 gnc_commodities_dialog (GtkWidget * parent)
00407 {
00408 CommoditiesDialog *cd;
00409 gint component_id;
00410
00411 if (gnc_forall_gui_components (DIALOG_COMMODITIES_CM_CLASS,
00412 show_handler, NULL))
00413 return;
00414
00415 cd = g_new0 (CommoditiesDialog, 1);
00416
00417 gnc_commodities_dialog_create (parent, cd);
00418
00419 component_id = gnc_register_gui_component (DIALOG_COMMODITIES_CM_CLASS,
00420 refresh_handler, close_handler,
00421 cd);
00422 gnc_gui_component_set_session (component_id, cd->session);
00423
00424 gtk_widget_grab_focus (GTK_WIDGET(cd->commodity_tree));
00425
00426 gtk_widget_show (cd->dialog);
00427 }