Commit c3d479cd authored by Murray Cumming's avatar Murray Cumming
Browse files

Move report building code around.

* glom/report_builder.[h|cc]: report_build(): Return the filepath,
instead of opening it in the browser.
* glom/xsl_utils[h|cc]: transform_and_open(): Rename to transform(),
returning the filepath. Also correct some ustring filepath to std::string.
* glom/utils_ui[h|cc]: Added show_report_in_browser().
* glom/frame_glom.cc: on_menu_report_selected():
* glom/mode_data/box_data_manyrecords.cc: print_layout: Adapted.
parent 854ba701
2011-10-17 Murray Cumming <murrayc@murrayc.com>
Move report building code around.
* glom/report_builder.[h|cc]: report_build(): Return the filepath,
instead of opening it in the browser.
* glom/xsl_utils[h|cc]: transform_and_open(): Rename to transform(),
returning the filepath. Also correct some ustring filepath to std::string.
* glom/utils_ui[h|cc]: Added show_report_in_browser().
* glom/frame_glom.cc: on_menu_report_selected():
* glom/mode_data/box_data_manyrecords.cc: print_layout: Adapted.
2011-10-17 Murray Cumming <murrayc@murrayc.com>
Move some XSLT-processing code around.
......
......@@ -2316,7 +2316,9 @@ void Frame_Glom::on_menu_report_selected(const Glib::ustring& report_name)
ReportBuilder report_builder;
report_builder.set_document(document);
report_builder.report_build(found_set, report, get_app_window()); //TODO: Use found set's where_clause.
const std::string filepath =
report_builder.report_build(found_set, report); //TODO: Use found set's where_clause.
Utils::show_report_in_browser(filepath, get_app_window());
}
#ifndef GLOM_ENABLE_CLIENT_ONLY
......
......@@ -23,6 +23,7 @@
#include <glom/glade_utils.h>
#include <glom/report_builder.h>
#include <glom/mode_design/layout/dialog_layout_list.h>
#include <glom/utils_ui.h>
#include <libglom/privs.h>
#include <libglom/utils.h> //For bold_message()).
#include <sstream> //For stringstream
......@@ -83,7 +84,9 @@ void Box_Data_ManyRecords::print_layout()
ReportBuilder report_builder;
report_builder.set_document(document);
report_builder.report_build(m_found_set, report_temp, get_app_window());
const std::string filepath =
report_builder.report_build(m_found_set, report_temp);
Utils::show_report_in_browser(filepath, get_app_window());
}
}
......
......@@ -484,7 +484,7 @@ void ReportBuilder::report_build_records_vertical_group(const FoundSet& found_se
}
void ReportBuilder::report_build(const FoundSet& found_set, const sharedptr<const Report>& report, Gtk::Window* parent_window)
std::string ReportBuilder::report_build(const FoundSet& found_set, const sharedptr<const Report>& report)
{
//Create a DOM Document with the XML:
xmlpp::DomParser dom_parser;;
......@@ -566,17 +566,17 @@ void ReportBuilder::report_build(const FoundSet& found_set, const sharedptr<cons
{
//Handle database errors here rather than crashing the whole application:
handle_error(ex);
return;
return std::string();
}
catch(const std::exception& ex)
{
//Handle database errors here rather than crashing the whole application:
handle_error(ex);
return;
return std::string();
}
}
GlomXslUtils::transform_and_open(*pDocument, "print_report_to_html.xsl", parent_window);
return GlomXslUtils::transform(*pDocument, "print_report_to_html.xsl");
}
static void fill_standard_list_report_fill(const sharedptr<Report>& report, const sharedptr<const LayoutGroup>& layout_group)
......
......@@ -39,10 +39,11 @@ public:
//void set_report(const Glib::ustring& table_name, const sharedptr<const Report>& report);
//sharedptr<Report> get_report();
void report_build(const FoundSet& found_set, const sharedptr<const Report>& report, Gtk::Window* parent_window = 0);
/**
* @result The filepath of the generated HTML file.
*/
std::string report_build(const FoundSet& found_set, const sharedptr<const Report>& report);
private:
void report_build_groupby(const FoundSet& found_set_parent, xmlpp::Element& parent_node, const sharedptr<LayoutItem_GroupBy>& group_by);
......
......@@ -49,6 +49,11 @@
#include <stack>
// For ShellExecute:
#ifdef G_OS_WIN32
# include <windows.h>
#endif
namespace
{
......@@ -478,4 +483,38 @@ bool Utils::show_warning_no_records_found(Gtk::Window& transient_for)
}
void Utils::show_report_in_browser(const std::string& filepath, Gtk::Window* parent_window)
{
//Give the user a clue, in case the web browser opens in the background, for instance in a new tab:
if(parent_window)
show_ok_dialog(_("Report Finished"), _("The report will now be opened in your web browser."), *parent_window, Gtk::MESSAGE_INFO);
#ifdef G_OS_WIN32
// gtk_show_uri doesn't seem to work on Win32, at least not for local files
// We use Windows API instead.
// TODO: Check it again and file a bug if necessary.
ShellExecute(0, "open", filepath.c_str(), 0, 0, SW_SHOW);
#else
Glib::ustring uri;
try
{
uri = Glib::filename_to_uri(filepath);
}
catch(const Glib::ConvertError& ex)
{
std::cerr << G_STRFUNC << ": Could not convert filepath to URI: " << filepath << std::endl;
return;
}
//Use the GNOME browser:
GError* gerror = 0;
if(!gtk_show_uri(0 /* screen */, uri.c_str(), GDK_CURRENT_TIME, &gerror))
{
std::cerr << G_STRFUNC << ": " << gerror->message << std::endl;
g_error_free(gerror);
}
#endif //G_OS_WIN32
}
} //namespace Glom
......@@ -94,6 +94,8 @@ Glib::RefPtr<Gdk::Pixbuf> image_scale_keeping_ratio(const Glib::RefPtr<Gdk::Pixb
///@result Whether the user would like to find again.
bool show_warning_no_records_found(Gtk::Window& transient_for);
void show_report_in_browser(const std::string& filepath, Gtk::Window* parent_window);
} //namespace Utils
} //namespace Glom
......
......@@ -24,7 +24,6 @@
#include <libglom/connectionpool.h>
#include <libglom/data_structure/layout/report_parts/layoutitem_fieldsummary.h>
#include <libglom/data_structure/glomconversions.h>
#include <glom/frame_glom.h>
#include <libxml++/libxml++.h>
#include <libxslt/transform.h>
//#include <libexslt/exslt.h> //For exsltRegisterAll().
......@@ -41,18 +40,14 @@
#include <iostream> // for cout, endl
#include <iomanip>
// For ShellExecute:
#ifdef G_OS_WIN32
# include <windows.h>
#endif
namespace
{
Glib::ustring get_xslt_file(const Glib::ustring& xsl_file)
std::string get_xslt_file(const std::string& xsl_file)
{
#ifdef G_OS_WIN32
gchar* directory = g_win32_get_package_installation_directory_of_module(0);
Glib::ustring xsltdir = Glib::build_filename(Glib::build_filename(directory,
std::string xsltdir = Glib::build_filename(Glib::build_filename(directory,
"share" G_DIR_SEPARATOR_S "glom" G_DIR_SEPARATOR_S "xslt"), xsl_file);
g_free(directory);
return xsltdir;
......@@ -75,7 +70,7 @@ static Glib::ustring xslt_process(const xmlpp::Document& xml_document, const std
//nonconst.write_to_stream_formatted(std::cout);
//std::cout << std::endl;
Glib::ustring result;
Glib::ustring result;
//Use libxslt to transform the XML:
xmlDocPtr style = xmlReadFile(filepath_xslt.c_str(), 0, 0);
......@@ -119,14 +114,14 @@ static Glib::ustring xslt_process(const xmlpp::Document& xml_document, const std
return result;
}
void GlomXslUtils::transform_and_open(const xmlpp::Document& xml_document, const Glib::ustring& xsl_file_path, Gtk::Window* parent_window)
std::string GlomXslUtils::transform(const xmlpp::Document& xml_document, const std::string& xsl_file_path)
{
//Use libxslt to convert the XML to HTML:
const Glib::ustring result = xslt_process(xml_document, get_xslt_file(xsl_file_path));
//std::cout << "After xslt: " << result << std::endl;
//Save it to a temporary file and show it in a browser:
const Glib::ustring temp_path = Glib::build_filename(
const std::string temp_path = Glib::build_filename(
Glib::get_tmp_dir(), "glom_printout.html");
std::cout << "temp_path=" << temp_path << std::endl;
......@@ -150,7 +145,7 @@ void GlomXslUtils::transform_and_open(const xmlpp::Document& xml_document, const
catch(const Gio::Error& ex)
{
// If the operation was not successful, print the error and abort
return; // false; // print_error(ex, output_uri_string);
return std::string(); // print_error(ex, output_uri_string);
}
//Write the data to the output uri
......@@ -163,31 +158,13 @@ void GlomXslUtils::transform_and_open(const xmlpp::Document& xml_document, const
catch(const Gio::Error& ex)
{
// If the operation was not successful, print the error and abort
return; // false; //print_error(ex, output_uri_string);
return std::string(); // false; //print_error(ex, output_uri_string);
}
if(bytes_written != (gssize)result_bytes)
return; //false
//Give the user a clue, in case the web browser opens in the background, for instance in a new tab:
if(parent_window)
Frame_Glom::show_ok_dialog(_("Report Finished"), _("The report will now be opened in your web browser."), *parent_window, Gtk::MESSAGE_INFO);
return std::string(); //false
#ifdef G_OS_WIN32
// gtk_show_uri doesn't seem to work on Win32, at least not for local files
// We use Windows API instead.
// TODO: Check it again and file a bug if necessary.
ShellExecute(0, "open", file->get_path().c_str(), 0, 0, SW_SHOW);
#else
//Use the GNOME browser:
GError* gerror = 0;
if(!gtk_show_uri(0 /* screen */, file->get_uri().c_str(), GDK_CURRENT_TIME, &gerror))
{
std::cerr << G_STRFUNC << ": " << gerror->message << std::endl;
g_error_free(gerror);
}
#endif //G_OS_WIN32
return file->get_path();
}
} //namespace Glom
......@@ -27,10 +27,6 @@
#include <libglom/data_structure/layout/layoutitem_field.h>
#include <libxml++/libxml++.h>
namespace Gtk
{
class Window;
}
namespace Glom
{
......@@ -42,7 +38,10 @@ typedef std::list<type_pair_sort_field> type_sort_clause;
namespace GlomXslUtils
{
void transform_and_open(const xmlpp::Document& xml_document, const Glib::ustring& xsl_file_path, Gtk::Window* parent_window = 0);
/**
* @result the filepath of the generated HTML file.
*/
std::string transform(const xmlpp::Document& xml_document, const std::string& xsl_file_path);
} //namespace GlomXslUtils
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment