Merge branch 'maint-augment-run-report' into maint #857

This commit is contained in:
Christopher Lam 2021-01-03 06:31:39 +08:00
commit 1e7433daf3
6 changed files with 77 additions and 8 deletions

View File

@ -137,14 +137,12 @@ static gboolean
gnc_report_system_report_stream_cb (const char *location, char ** data, int *len)
{
gboolean ok;
gchar *captured_str;
ok = gnc_run_report_id_string (location, data);
ok = gnc_run_report_id_string_with_error_handling (location, data, &captured_str);
if (!ok)
{
SCM captured = scm_c_eval_string ("gnc:last-captured-error");
gchar *captured_str = gnc_scm_to_utf8_string(captured);
*data = g_strdup_printf ("<html><body><h3>%s</h3>"
"<p>%s</p><pre>%s</pre></body></html>",
_("Report error"),

View File

@ -260,9 +260,9 @@ return a document object with export-string or export-error.") << std::endl;
if (scm_is_false (id))
scm_cleanup_and_exit_with_failure (nullptr);
char* html;
gnc_run_report (scm_to_int(id), &html);
if (html && *html)
char *html, *errmsg;
if (gnc_run_report_with_error_handling (scm_to_int(id), &html, &errmsg))
{
if (!args->output_file.empty())
{
@ -273,6 +273,10 @@ return a document object with export-string or export-error.") << std::endl;
std::cout << html << std::endl;
}
}
else
{
std::cerr << errmsg << std::endl;
}
}
qof_session_destroy (session);

View File

@ -206,6 +206,34 @@ gnc_reports_get_global(void)
return reports;
}
gboolean
gnc_run_report_with_error_handling (gint report_id, gchar ** data, gchar **errmsg)
{
SCM report, res, html, captured_error;
report = gnc_report_find (report_id);
g_return_val_if_fail (data, FALSE);
g_return_val_if_fail (errmsg, FALSE);
g_return_val_if_fail (!scm_is_false (report), FALSE);
res = scm_call_1 (scm_c_eval_string ("gnc:render-report"), report);
html = scm_car (res);
captured_error = scm_cadr (res);
if (!scm_is_false (html))
{
*data = gnc_scm_to_utf8_string (html);
*errmsg = NULL;
return TRUE;
}
else
{
*errmsg = gnc_scm_to_utf8_string (captured_error);
*data = NULL;
return FALSE;
}
}
static void
error_handler(const char *str)
{
@ -218,6 +246,8 @@ gnc_run_report (gint report_id, char ** data)
SCM scm_text;
gchar *str;
PWARN ("gnc_run_report is deprecated. use gnc_run_report_with_error_handling instead.");
g_return_val_if_fail (data != NULL, FALSE);
*data = NULL;
@ -238,6 +268,8 @@ gnc_run_report_id_string (const char * id_string, char **data)
{
gint report_id;
PWARN ("gnc_run_report_id_string is deprecated. use gnc_run_report_id_string_with_error_handling instead.");
g_return_val_if_fail (id_string != NULL, FALSE);
g_return_val_if_fail (data != NULL, FALSE);
*data = NULL;
@ -262,6 +294,25 @@ gnc_report_name( SCM report )
return gnc_scm_call_1_to_string(get_name, report);
}
gboolean
gnc_run_report_id_string_with_error_handling (const char * id_string, char **data,
gchar **errmsg)
{
gint report_id;
g_return_val_if_fail (id_string, FALSE);
g_return_val_if_fail (data, FALSE);
*data = NULL;
if (strncmp ("id=", id_string, 3) != 0)
return FALSE;
if (sscanf (id_string + 3, "%d", &report_id) != 1)
return FALSE;
return gnc_run_report_with_error_handling (report_id, data, errmsg);
}
gchar*
gnc_get_default_report_font_family(void)
{

View File

@ -39,7 +39,13 @@ void gnc_report_init (void);
gboolean gnc_run_report (gint report_id, char ** data);
gboolean gnc_run_report_with_error_handling (gint report_id,
gchar **data,
gchar **errmsg);
gboolean gnc_run_report_id_string (const char * id_string, char **data);
gboolean gnc_run_report_id_string_with_error_handling (const char * id_string,
char **data,
gchar **errmsg);
/**
* @param report The SCM version of the report.

View File

@ -76,6 +76,7 @@
(export gnc:report-needs-save?)
(export gnc:report-options)
(export gnc:report-render-html)
(export gnc:render-report)
(export gnc:report-run)
(export gnc:report-serialize)
(export gnc:report-set-ctext!)
@ -763,9 +764,17 @@ not found.")))
(gnc:report-set-dirty?! report #f) ;; mark it clean
html)))))
;; render report. will return a 2-element list: either (list html #f)
;; where html is the report html string, or (list #f captured-error)
;; where captured-error is the error string.
(define (gnc:render-report report)
(define (get-report) (gnc:report-render-html report #t))
(gnc:apply-with-error-handling get-report '()))
;; looks up the report by id and renders it with gnc:report-render-html
;; marks the cursor busy during rendering; returns the html
(define (gnc:report-run id)
(issue-deprecation-warning "gnc:report-run is deprecated. use gnc:render-report instead.")
(let ((report (gnc-report-find id))
(html #f))
(gnc-set-busy-cursor '() #t)

View File

@ -78,11 +78,12 @@
((result #f) result)
((_ captured-error)
(display captured-error (current-error-port))
;; the next line will be removed in 5.x - deprecated
(set! gnc:last-captured-error (gnc:html-string-sanitize captured-error))
(when (defined? 'gnc:warn) (gnc:warn captured-error))
#f)))
(define gnc:last-captured-error "")
(define gnc:last-captured-error "") ;deprecate - remove in 5.x
;; This database can be used to store and retrieve translatable
;; strings. Strings that are returned by the lookup function are