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
00026
00027
00028
00029 #include "config.h"
00030
00031 #include <glib/gi18n.h>
00032 #include <gtk/gtk.h>
00033 #include <glade/glade.h>
00034 #include <libguile.h>
00035 #include "swig-runtime.h"
00036
00037 #include "dialog-custom-report.h"
00038 #include "dialog-options.h"
00039 #include "dialog-utils.h"
00040 #include "gnc-main-window.h"
00041 #include "option-util.h"
00042 #include "window-report.h"
00043 #include "guile-mappings.h"
00044 #include "gnc-gui-query.h"
00045 #include "gnc-ui.h"
00046 #include "gnc-report.h"
00047 #include "gnc-plugin-page-report.h"
00048
00049
00050
00051
00052 enum
00053 {
00054 COL_NAME = 0,
00055 COL_NUM,
00056 NUM_COLS
00057 };
00058
00059
00060 typedef struct _CustomReportDialog
00061 {
00062
00063 GtkWidget *dialog;
00064 GtkWidget *reportview;
00065 GncMainWindow *window;
00066
00067
00068 SCM reportlist;
00069
00070 } CustomReportDialog;
00071
00072 static void
00073 custom_report_dialog_close_cb(GtkWidget* widget,
00074 CustomReportDialog *crd)
00075 {
00076 gtk_widget_destroy(crd->dialog);
00077 g_free(crd);
00078
00079 }
00080
00081
00082 static void
00083 cancel_custom_report_clicked_cb(GtkWidget* widget,
00084 CustomReportDialog *crd)
00085 {
00086 custom_report_dialog_close_cb(NULL, crd);
00087 }
00088
00089
00097 static void
00098 update_report_list(GtkListStore *store, CustomReportDialog *crd)
00099 {
00100
00101 SCM get_names = scm_c_eval_string("gnc:custom-report-template-names");
00102 SCM template_menu_name = scm_c_eval_string("gnc:report-template-menu-name/report-guid");
00103 SCM names;
00104 const gchar *name;
00105 int i;
00106 GtkTreeIter iter;
00107
00108 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), COL_NAME, GTK_SORT_ASCENDING);
00109
00110 crd->reportlist = scm_call_0(get_names);
00111 names = crd->reportlist;
00112
00113 gtk_list_store_clear(store);
00114
00115 if (scm_is_list(names))
00116 {
00117
00118
00119
00120 for (i = 0; !scm_is_null(names); i++)
00121 {
00122 char * str;
00123
00124 scm_dynwind_begin (0);
00125 str = scm_to_locale_string (scm_call_2(template_menu_name, SCM_CAR(names), SCM_BOOL_F));
00126 name = g_strdup (str);
00127 scm_dynwind_free (str);
00128 scm_dynwind_end ();
00129
00130 gtk_list_store_append(store, &iter);
00131 gtk_list_store_set(store, &iter,
00132 COL_NAME, name,
00133 COL_NUM, i,
00134 -1);
00135 names = SCM_CDR(names);
00136
00137 }
00138
00139 }
00140
00141 }
00142
00143
00144 static GtkTreeModel *
00145 create_and_fill_report_list(CustomReportDialog *crd)
00146 {
00147 GtkListStore *store;
00148 GtkTreeIter iter;
00149
00150 store = gtk_list_store_new(NUM_COLS, G_TYPE_STRING, G_TYPE_INT);
00151
00152 update_report_list(store, crd);
00153
00154 return GTK_TREE_MODEL (store);
00155
00156 }
00157
00158 static void
00159 set_reports_model(CustomReportDialog *crd)
00160 {
00161 GtkCellRenderer *renderer;
00162 GtkTreeModel *model;
00163
00164 renderer = gtk_cell_renderer_text_new();
00165
00166 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (crd->reportview), -1, "Report Name", renderer, "text", COL_NAME, NULL);
00167
00168 model = create_and_fill_report_list(crd);
00169
00170 gtk_tree_view_set_model (GTK_TREE_VIEW (crd->reportview), model);
00171
00172 g_object_unref(model);
00173
00174
00175 }
00176
00177
00185 static void
00186 custom_report_run_report(SCM guid,
00187 CustomReportDialog *crd)
00188 {
00189
00190 SCM make_report = scm_c_eval_string("gnc:make-report");
00191 int report_id;
00192 GncMainWindow *window = crd->window;
00193
00194 if (!scm_is_null(guid))
00195 {
00196
00197
00198 report_id = SCM_INUM(scm_call_1(make_report, guid));
00199
00200
00201
00202 custom_report_dialog_close_cb(NULL, crd);
00203
00204
00205 gnc_main_window_open_report(report_id, window);
00206
00207 }
00208
00209 }
00210
00222 static SCM
00223 get_custom_report_selection(CustomReportDialog *crd,
00224 const gchar* message)
00225 {
00226 GtkTreeSelection *sel;
00227 GtkTreeModel *model;
00228 GtkTreeIter iter;
00229 SCM guid;
00230
00231 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(crd->reportview));
00232
00233 if (gtk_tree_selection_get_selected(sel, &model, &iter))
00234 {
00235 int num;
00236
00237 gtk_tree_model_get(model, &iter, COL_NUM, &num, -1);
00238 guid = scm_list_ref(crd->reportlist, scm_int2num(num));
00239 }
00240
00241 else
00242 {
00243
00244 gnc_error_dialog(GTK_WIDGET(crd->window), "%s", message);
00245 return SCM_EOL;
00246
00247 }
00248
00249 return guid;
00250
00251 }
00252
00261 static void
00262 on_custom_report_list_view_row_activated(GtkTreeView *view,
00263 GtkTreePath *path,
00264 GtkTreeViewColumn *column,
00265 CustomReportDialog *crd)
00266 {
00267
00268 GtkTreeModel *model;
00269 GtkTreeIter iter;
00270
00271 model = gtk_tree_view_get_model(view);
00272
00273 if (gtk_tree_model_get_iter(model, &iter, path))
00274 {
00275 int num;
00276 SCM guid;
00277
00278 gtk_tree_model_get(model, &iter, COL_NUM, &num, -1);
00279
00280 guid = scm_list_ref(crd->reportlist, scm_int2num(num));
00281
00282 custom_report_run_report(guid, crd);
00283
00284 }
00285
00286 }
00287
00295 static void
00296 on_delete_custom_report_clicked(GtkWidget *button,
00297 CustomReportDialog *crd)
00298 {
00299 GtkTreeSelection *sel;
00300 GtkTreeModel *model;
00301 GtkTreeIter iter;
00302
00303 SCM template_menu_name = scm_c_eval_string("gnc:report-template-menu-name/report-guid");
00304 SCM guid;
00305 gchar* report_name;
00306
00307 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(crd->reportview));
00308
00309 guid = get_custom_report_selection(crd, _("You must select a report to delete."));
00310 if (!scm_is_null(guid))
00311 {
00312 char * str;
00313
00314 scm_dynwind_begin (0);
00315 str = scm_to_locale_string(scm_call_2(template_menu_name, guid, SCM_BOOL_F));
00316 report_name = g_strdup (str);
00317 scm_dynwind_free (str);
00318 scm_dynwind_end ();
00319
00320
00321 if (gnc_verify_dialog(crd->dialog, FALSE, "Are you sure you want to delete %s?", report_name))
00322 {
00323 SCM del_report = scm_c_eval_string("gnc:delete-report");
00324 scm_call_1(del_report, guid);
00325 update_report_list(GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(crd->reportview))),
00326 crd);
00327 }
00328 g_free (report_name);
00329 }
00330 }
00331
00332 static void
00333 run_custom_report_clicked_cb (GtkWidget* button,
00334 CustomReportDialog *crd)
00335 {
00336
00337 SCM guid = get_custom_report_selection(crd, _("You must select a report to run."));
00338 custom_report_run_report(guid, crd);
00339
00340 }
00341
00342
00343
00350 void gnc_ui_custom_report(GncMainWindow * window)
00351 {
00352
00353 GladeXML* xml;
00354 CustomReportDialog *crd;
00355
00356 crd = g_new0(CustomReportDialog, 1);
00357
00358 xml = gnc_glade_xml_new("custom-report-dialog.glade", "custom_report_dialog");
00359
00360 crd->dialog = glade_xml_get_widget(xml, "custom_report_dialog");
00361 crd->reportview = glade_xml_get_widget(xml, "custom_report_list_view");
00362 set_reports_model(crd);
00363 crd->window = window;
00364
00365
00366 glade_xml_signal_connect_data(xml, "cancel_custom_report_clicked_cb", G_CALLBACK(cancel_custom_report_clicked_cb), crd);
00367 glade_xml_signal_connect_data(xml, "custom_report_dialog_close_cb", G_CALLBACK(custom_report_dialog_close_cb), crd);
00368 glade_xml_signal_connect_data(xml, "on_custom_report_list_view_row_activated", G_CALLBACK(on_custom_report_list_view_row_activated), crd);
00369 glade_xml_signal_connect_data(xml, "on_delete_custom_report_clicked", G_CALLBACK(on_delete_custom_report_clicked), crd);
00370 glade_xml_signal_connect_data(xml, "run_custom_report_clicked_cb", G_CALLBACK(run_custom_report_clicked_cb), crd);
00371
00372 gtk_widget_show_all(crd->dialog);
00373
00374 }