28 #include <glib/gstdio.h>
39 #include "gnc-guile-utils.h"
40 #include "gnc-report.h"
46 static GHashTable *reports = NULL;
47 static gint report_next_serial_id = 0;
50 gnc_report_init_table(
void)
54 reports = g_hash_table_new_full(
55 g_int_hash, g_int_equal,
56 g_free, (GDestroyNotify) scm_gc_unprotect_object);
61 gnc_report_remove_by_id(gint
id)
64 g_hash_table_remove(reports, &
id);
67 SCM gnc_report_find(gint
id)
69 gpointer report = NULL;
73 report = g_hash_table_lookup(reports, &
id);
82 gint gnc_report_add(SCM report)
84 SCM get_id = scm_c_eval_string(
"gnc:report-id");
88 gnc_report_init_table();
90 value = scm_call_1(get_id, report);
91 if (scm_is_number(value))
93 id = scm_to_int(value);
94 if (!g_hash_table_lookup(reports, &
id))
98 g_hash_table_insert(reports, key, (gpointer)report);
99 scm_gc_protect_object(report);
102 g_warning(
"Report specified id of %d is already is use. "
103 "Using generated id.",
id);
106 id = report_next_serial_id++;
107 while (
id < G_MAXINT)
109 if (!g_hash_table_lookup(reports, &
id))
111 key = g_new(gint, 1);
113 g_hash_table_insert(reports, key, (gpointer)report);
114 scm_gc_protect_object(report);
117 id = report_next_serial_id++;
120 g_warning(
"Unable to add report to table. %d reports in use.", G_MAXINT);
121 report_next_serial_id = G_MAXINT;
126 yes_remove(gpointer key, gpointer val, gpointer data)
132 gnc_reports_flush_global(
void)
135 g_hash_table_foreach_remove(reports, yes_remove, NULL);
139 gnc_reports_get_global(
void)
141 gnc_report_init_table();
146 error_handler(
const char *str)
148 g_warning(
"Failure running report: %s", str);
152 gnc_run_report (gint report_id,
char ** data)
158 g_return_val_if_fail (data != NULL, FALSE);
161 str = g_strdup_printf(
"(gnc:report-run %d)", report_id);
162 scm_text = gfec_eval_string(str, error_handler);
165 if (scm_text == SCM_UNDEFINED || !scm_is_string (scm_text))
168 *data = gnc_scm_to_utf8_string (scm_text);
174 gnc_run_report_id_string (
const char * id_string,
char **data)
178 g_return_val_if_fail (id_string != NULL, FALSE);
179 g_return_val_if_fail (data != NULL, FALSE);
182 if (strncmp (
"id=", id_string, 3) != 0)
185 if (sscanf (id_string + 3,
"%d", &report_id) != 1)
188 return gnc_run_report (report_id, data);
192 gnc_report_name( SCM report )
194 SCM get_name = scm_c_eval_string(
"gnc:report-name");
196 if (report == SCM_BOOL_F)
199 return gnc_scm_call_1_to_string(get_name, report);
203 gnc_get_default_report_font_family(
void)
206 GtkWidget* top_widget;
207 GtkStyle* top_widget_style;
208 const gchar* default_font_family;
210 top_list = gtk_window_list_toplevels();
211 g_return_val_if_fail (top_list != NULL, NULL);
212 top_widget = GTK_WIDGET(top_list->data);
213 g_list_free(top_list);
214 top_widget_style = gtk_rc_get_style(top_widget);
215 default_font_family =
216 pango_font_description_get_family(top_widget_style->font_desc);
218 if (default_font_family == NULL)
219 return g_strdup(
"Arial");
221 return g_strdup(default_font_family);
225 gnc_saved_reports_write_internal (
const gchar *file,
const gchar *contents, gboolean overwrite)
227 gboolean success = TRUE;
232 gint flags = O_WRONLY | O_CREAT | (overwrite ? O_TRUNC : O_APPEND);
234 fd = g_open (file, flags, 0666);
237 PWARN(
"Cannot open file %s: %s\n", file, strerror(errno));
241 length = strlen (contents);
242 written = write(fd, contents, length);
246 PWARN(
"Cannot write to file %s: %s\n", file, strerror(errno));
249 else if (written != length)
252 PWARN(
"File %s truncated (provided %d, written %d)",
253 file, length, (
int)written);
257 else if (close(fd) == -1)
258 PWARN(
"Close failed for file %s: %s", file, strerror(errno));
264 gboolean gnc_saved_reports_backup (
void)
266 gboolean success = FALSE;
268 gchar *saved_rpts_bkp_path = g_strconcat (saved_rpts_path,
"-backup", NULL);
269 gchar *contents = NULL;
270 GError *save_error = NULL;
272 if (g_file_test (saved_rpts_path, G_FILE_TEST_EXISTS))
274 if (!g_file_get_contents (saved_rpts_path, &contents, NULL, &save_error))
276 PWARN (
"Couldn't read contents of %s.\nReason: %s", saved_rpts_path, save_error->message);
277 g_error_free (save_error);
283 DEBUG (
"creating backup of file %s", saved_rpts_bkp_path);
284 success = gnc_saved_reports_write_internal (saved_rpts_bkp_path, contents, TRUE);
287 g_free (saved_rpts_path);
288 g_free (saved_rpts_bkp_path);
295 gnc_saved_reports_write_to_file (
const gchar* report_def, gboolean overwrite)
297 gboolean success = FALSE;
302 DEBUG (
"writing to %s", saved_rpts_path);
303 success = gnc_saved_reports_write_internal (saved_rpts_path, report_def, overwrite);
306 g_free (saved_rpts_path);
#define DEBUG(format, args...)
gchar * gnc_build_dotgnucash_path(const gchar *filename)
Make a path to filename in the user's configuration directory.
#define PWARN(format, args...)
All type declarations for the whole Gnucash engine.
File path resolution utility functions.
const gchar * QofLogModule