r21722 - gnucash/trunk/src - Add "Export to PDF" menu item for reports.

Christian Stimming cstim at code.gnucash.org
Mon Dec 12 16:53:14 EST 2011


Author: cstim
Date: 2011-12-12 16:53:14 -0500 (Mon, 12 Dec 2011)
New Revision: 21722
Trac: http://svn.gnucash.org/trac/changeset/21722

Modified:
   gnucash/trunk/src/business/business-gnome/dialog-invoice.c
   gnucash/trunk/src/html/gnc-html-webkit.c
   gnucash/trunk/src/html/gnc-html.c
   gnucash/trunk/src/html/gnc-html.h
   gnucash/trunk/src/report/report-gnome/gnc-plugin-page-report-ui.xml
   gnucash/trunk/src/report/report-gnome/gnc-plugin-page-report.c
Log:
Add "Export to PDF" menu item for reports.

gnc_html_print gets an extra boolean argument to choose the "Export as PDF" option.
The GtkPrintOperation expects a full file name in set_export_filename, though.
We obtain one by asking the user for it.

This action still needs a more suitable icon.

Modified: gnucash/trunk/src/business/business-gnome/dialog-invoice.c
===================================================================
--- gnucash/trunk/src/business/business-gnome/dialog-invoice.c	2011-12-12 09:03:02 UTC (rev 21721)
+++ gnucash/trunk/src/business/business-gnome/dialog-invoice.c	2011-12-12 21:53:14 UTC (rev 21722)
@@ -2779,7 +2779,7 @@
         { N_("Process Payment"), pay_invoice_cb, NULL, FALSE},
         { N_("Duplicate"), NULL, multi_duplicate_invoice_cb, FALSE},
         { N_("Post"), NULL, multi_post_invoice_cb, FALSE},
-        { N_("Print"), NULL, multi_print_invoice_cb, TRUE},
+        { N_("Printable Report"), NULL, multi_print_invoice_cb, TRUE},
         { NULL },
     };
     static GNCSearchCallbackButton bill_buttons[] =
@@ -2788,7 +2788,7 @@
         { N_("Process Payment"), pay_invoice_cb, NULL, FALSE},
         { N_("Duplicate"), NULL, multi_duplicate_invoice_cb, FALSE},
         { N_("Post"), NULL, multi_post_invoice_cb, FALSE},
-        { N_("Print"), NULL, multi_print_invoice_cb, TRUE},
+        { N_("Printable Report"), NULL, multi_print_invoice_cb, TRUE},
         { NULL },
     };
     static GNCSearchCallbackButton emp_buttons[] =
@@ -2799,7 +2799,7 @@
         { N_("Process Payment"), pay_invoice_cb, NULL, FALSE},
         { N_("Duplicate"), NULL, multi_duplicate_invoice_cb, FALSE},
         { N_("Post"), NULL, multi_post_invoice_cb, FALSE},
-        { N_("Print"), NULL, multi_print_invoice_cb, TRUE},
+        { N_("Printable Report"), NULL, multi_print_invoice_cb, TRUE},
         { NULL },
     };
 

Modified: gnucash/trunk/src/html/gnc-html-webkit.c
===================================================================
--- gnucash/trunk/src/html/gnc-html-webkit.c	2011-12-12 09:03:02 UTC (rev 21721)
+++ gnucash/trunk/src/html/gnc-html-webkit.c	2011-12-12 21:53:14 UTC (rev 21722)
@@ -106,7 +106,7 @@
 static void impl_webkit_reload( GncHtml* self );
 static void impl_webkit_copy_to_clipboard( GncHtml* self );
 static gboolean impl_webkit_export_to_file( GncHtml* self, const gchar* filepath );
-static void impl_webkit_print( GncHtml* self, const gchar* jobname );
+static void impl_webkit_print( GncHtml* self, const gchar* jobname, gboolean export_pdf );
 static void impl_webkit_cancel( GncHtml* self );
 static void impl_webkit_set_parent( GncHtml* self, GtkWindow* parent );
 
@@ -1054,6 +1054,8 @@
     }
 }
 
+#define GNC_GTK_PRINT_SETTINGS_EXPORT_DIR "gnc-pdf-export-directory"
+
 /**
  * Prints the current page.
  *
@@ -1067,17 +1069,19 @@
  * @param self HTML renderer object
  */
 static void
-impl_webkit_print( GncHtml* self, const gchar* jobname )
+impl_webkit_print( GncHtml* self, const gchar* jobname, gboolean export_pdf )
 {
 #if !HAVE(WEBKIT_WEB_FRAME_PRINT_FULL)
     extern void webkit_web_frame_print( WebKitWebFrame * frame );
 #endif
 
+    gchar *export_filename = NULL;
     GncHtmlWebkitPrivate* priv;
     WebKitWebFrame* frame;
 #if HAVE(WEBKIT_WEB_FRAME_PRINT_FULL)
     GtkPrintOperation* op = gtk_print_operation_new();
     GError* error = NULL;
+    GtkPrintSettings *print_settings;
 #endif
 
     priv = GNC_HTML_WEBKIT_GET_PRIVATE(self);
@@ -1085,11 +1089,108 @@
 
 #if HAVE(WEBKIT_WEB_FRAME_PRINT_FULL)
     gnc_print_operation_init( op, jobname );
+    print_settings = gtk_print_operation_get_print_settings (op);
+    if (!print_settings)
+    {
+        print_settings = gtk_print_settings_new();
+        gtk_print_operation_set_print_settings(op, print_settings);
+    }
 #ifdef G_OS_WIN32
     gtk_print_operation_set_unit( op, GTK_UNIT_POINTS );
 #endif
-    webkit_web_frame_print_full( frame, op, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, &error );
 
+    // Make sure to generate a full export filename
+    if (g_str_has_suffix(jobname, ".pdf"))
+    {
+        export_filename = g_strdup(jobname);
+    }
+    else
+    {
+        export_filename = g_strconcat(jobname, ".pdf", NULL);
+    }
+
+    // Two different modes of operation. Either export to PDF, or run the
+    // normal print dialog
+    if (export_pdf)
+    {
+        gboolean have_outputdir = gtk_print_settings_has_key(print_settings, GNC_GTK_PRINT_SETTINGS_EXPORT_DIR);
+        if (have_outputdir)
+        {
+            gchar *tmp = g_build_filename(gtk_print_settings_get(print_settings, GNC_GTK_PRINT_SETTINGS_EXPORT_DIR),
+                                          export_filename, NULL);
+            g_free(export_filename);
+            export_filename = tmp;
+        }
+        else
+        {
+            GtkWidget *dialog;
+            gint result;
+            dialog = gtk_file_chooser_dialog_new (_("Save PDF File"),
+                                                  NULL,
+                                                  GTK_FILE_CHOOSER_ACTION_SAVE,
+                                                  GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+                                                  GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
+                                                  NULL);
+            gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), TRUE);
+            gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER(dialog), export_filename);
+            result = gtk_dialog_run (GTK_DIALOG (dialog));
+            if (result == GTK_RESPONSE_ACCEPT)
+            {
+                gchar *dirname;
+                char *tmp = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
+                g_free(export_filename);
+                export_filename = tmp;
+
+                // Store the directory for later
+                dirname = g_path_get_dirname(export_filename);
+                gtk_print_settings_set(print_settings, GNC_GTK_PRINT_SETTINGS_EXPORT_DIR, dirname);
+                g_free(dirname);
+            }
+            gtk_widget_destroy (dialog);
+
+            if (result != GTK_RESPONSE_ACCEPT)
+            {
+                // User pressed cancel - no saving here.
+                g_free(export_filename);
+                g_object_unref( op );
+                return;
+            }
+        }
+
+        // This function expects the full filename including (absolute?) path
+        gtk_print_operation_set_export_filename(op, export_filename);
+
+        // Run the "Export to PDF" print operation
+        webkit_web_frame_print_full( frame, op, GTK_PRINT_OPERATION_ACTION_EXPORT, &error );
+    }
+    else
+    {
+
+        // Also store this export file name as output URI in the settings
+        if (gtk_print_settings_has_key(print_settings, GTK_PRINT_SETTINGS_OUTPUT_URI))
+        {
+            const gchar *olduri = gtk_print_settings_get(print_settings, GTK_PRINT_SETTINGS_OUTPUT_URI);
+            gchar *dirname = g_path_get_dirname(olduri);
+            gchar *newuri = (g_strcmp0(dirname, ".") == 0)
+                    ? g_strdup(export_filename)
+                    : g_build_filename(dirname, export_filename, NULL);
+            //g_warning("olduri=%s newuri=%s", olduri, newuri);
+
+            // This function expects the full filename including protocol, path, and name
+            gtk_print_settings_set(print_settings, GTK_PRINT_SETTINGS_OUTPUT_URI, newuri);
+
+            g_free(newuri);
+            g_free(dirname);
+        }
+        else
+        {
+            gtk_print_settings_set(print_settings, GTK_PRINT_SETTINGS_OUTPUT_URI, export_filename);
+        }
+
+        // Run the normal printing dialog
+        webkit_web_frame_print_full( frame, op, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, &error );
+    }
+
     if ( error != NULL )
     {
         GtkWidget* window = gtk_widget_get_toplevel( GTK_WIDGET(priv->web_view) );
@@ -1107,6 +1208,7 @@
     // Remember to save the printing settings after this print job
     gnc_print_operation_save_print_settings(op);
     g_object_unref( op );
+    g_free(export_filename);
 
 #else
     webkit_web_frame_print( frame );

Modified: gnucash/trunk/src/html/gnc-html.c
===================================================================
--- gnucash/trunk/src/html/gnc-html.c	2011-12-12 09:03:02 UTC (rev 21721)
+++ gnucash/trunk/src/html/gnc-html.c	2011-12-12 21:53:14 UTC (rev 21722)
@@ -530,14 +530,14 @@
 }
 
 void
-gnc_html_print( GncHtml* self, const gchar* jobname )
+gnc_html_print( GncHtml* self, const gchar* jobname, gboolean export_pdf )
 {
     g_return_if_fail( self != NULL );
     g_return_if_fail( GNC_IS_HTML(self) );
 
     if ( GNC_HTML_GET_CLASS(self)->print != NULL )
     {
-        GNC_HTML_GET_CLASS(self)->print( self, jobname );
+        GNC_HTML_GET_CLASS(self)->print( self, jobname, export_pdf );
     }
     else
     {

Modified: gnucash/trunk/src/html/gnc-html.h
===================================================================
--- gnucash/trunk/src/html/gnc-html.h	2011-12-12 09:03:02 UTC (rev 21721)
+++ gnucash/trunk/src/html/gnc-html.h	2011-12-12 21:53:14 UTC (rev 21722)
@@ -135,7 +135,7 @@
     void (*reload)( GncHtml* html );
     void (*copy_to_clipboard)( GncHtml* html );
     gboolean (*export_to_file)( GncHtml* html, const gchar* file );
-    void (*print)( GncHtml* html, const gchar* jobname );
+    void (*print)( GncHtml* html, const gchar* jobname, gboolean export_pdf );
     void (*cancel)( GncHtml* html );
     URLType (*parse_url)( GncHtml* html, const gchar* url,
                           gchar** url_location, gchar** url_label );
@@ -199,8 +199,11 @@
  * Prints the report.
  *
  * @param html GncHtml object
+ * \param jobname A jobname for identifying this job, or to be used as an output file
+ * \param export_pdf If TRUE, only run a "print to PDF" operation in order to
+ *        export this to pdf. If FALSE, run a normal printing dialog.
  */
-void gnc_html_print( GncHtml* html, const gchar* jobname );
+void gnc_html_print( GncHtml* html, const gchar* jobname, gboolean export_pdf );
 
 /**
  * Cancels the current operation

Modified: gnucash/trunk/src/report/report-gnome/gnc-plugin-page-report-ui.xml
===================================================================
--- gnucash/trunk/src/report/report-gnome/gnc-plugin-page-report-ui.xml	2011-12-12 09:03:02 UTC (rev 21721)
+++ gnucash/trunk/src/report/report-gnome/gnc-plugin-page-report-ui.xml	2011-12-12 21:53:14 UTC (rev 21722)
@@ -7,6 +7,7 @@
       <menu name="FileExport" action="FileExportAction">
         <placeholder name="FileExportPlaceholder">
           <menuitem name="FileReportExport" action="ReportExportAction" />
+          <menuitem name="FileExportPDF" action="FilePrintPDFAction"/>
         </placeholder>
       </menu>
     </menu>
@@ -36,6 +37,7 @@
       <toolitem name="ReportToolbarExport" action="ReportExportAction" />
       <toolitem name="ReportToolbarOptions" action="ReportOptionsAction" />
       <toolitem name="ReportToolbarPrint" action="FilePrintAction" />
+      <toolitem name="ReportToolbarExportPDF" action="FilePrintPDFAction"/>
     </placeholder>
   </toolbar>
 </ui>

Modified: gnucash/trunk/src/report/report-gnome/gnc-plugin-page-report.c
===================================================================
--- gnucash/trunk/src/report/report-gnome/gnc-plugin-page-report.c	2011-12-12 09:03:02 UTC (rev 21721)
+++ gnucash/trunk/src/report/report-gnome/gnc-plugin-page-report.c	2011-12-12 21:53:14 UTC (rev 21722)
@@ -174,6 +174,7 @@
 static void gnc_plugin_page_report_export_cb(GtkAction *action, GncPluginPageReport *rep);
 static void gnc_plugin_page_report_options_cb(GtkAction *action, GncPluginPageReport *rep);
 static void gnc_plugin_page_report_print_cb(GtkAction *action, GncPluginPageReport *rep);
+static void gnc_plugin_page_report_exportpdf_cb(GtkAction *action, GncPluginPageReport *rep);
 static void gnc_plugin_page_report_copy_cb(GtkAction *action, GncPluginPageReport *rep);
 
 GType
@@ -1031,6 +1032,11 @@
         G_CALLBACK(gnc_plugin_page_report_print_cb)
     },
     {
+        "FilePrintPDFAction", GTK_STOCK_PRINT_REPORT, N_("Export as P_DF..."), NULL,
+        N_("Export the current report as a PDF document"),
+        G_CALLBACK(gnc_plugin_page_report_exportpdf_cb)
+    },
+    {
         "EditCutAction", GTK_STOCK_CUT, N_("Cu_t"), NULL,
         N_("Cut the current selection and copy it to clipboard"),
         NULL
@@ -1597,16 +1603,14 @@
     }
 }
 
-static void
-gnc_plugin_page_report_print_cb( GtkAction *action, GncPluginPageReport *report )
+static gchar *report_create_jobname(GncPluginPageReportPrivate *priv)
 {
-    GncPluginPageReportPrivate *priv;
+    gchar *job_name = NULL;
     gchar *report_name = NULL;
-    gchar *job_name = NULL;
     gchar *job_date = qof_print_date( time( NULL ) );
     const gchar *default_jobname = N_("GnuCash-Report");
 
-    priv = GNC_PLUGIN_PAGE_REPORT_GET_PRIVATE(report);
+    g_assert(priv);
 
     if (priv->cur_report == SCM_BOOL_F)
         report_name = g_strdup (_(default_jobname));
@@ -1704,14 +1708,36 @@
         }
     }
 
+    return job_name;
+}
+
+static void
+gnc_plugin_page_report_print_cb( GtkAction *action, GncPluginPageReport *report )
+{
+    GncPluginPageReportPrivate *priv = GNC_PLUGIN_PAGE_REPORT_GET_PRIVATE(report);
+    gchar *job_name = report_create_jobname(priv);
+
     //g_warning("Setting job name=%s", job_name);
 
-    gnc_html_print(priv->html, job_name);
+    gnc_html_print(priv->html, job_name, FALSE);
 
     g_free (job_name);
 }
 
 static void
+gnc_plugin_page_report_exportpdf_cb( GtkAction *action, GncPluginPageReport *report )
+{
+    GncPluginPageReportPrivate *priv = GNC_PLUGIN_PAGE_REPORT_GET_PRIVATE(report);
+    gchar *job_name = report_create_jobname(priv);
+
+    g_warning("Setting job name=%s", job_name);
+
+    gnc_html_print(priv->html, job_name, TRUE);
+
+    g_free (job_name);
+}
+
+static void
 gnc_plugin_page_report_copy_cb(GtkAction *action, GncPluginPageReport *report)
 {
     GncPluginPageReportPrivate *priv;



More information about the gnucash-changes mailing list