[gnumeric] Export: move the multi-sheet selection logic out of the savers



commit 1b369fdd0d8d1b774ec63b68172a4c5654519861
Author: Morten Welinder <terra gnome org>
Date:   Fri May 11 10:08:25 2018 -0400

    Export: move the multi-sheet selection logic out of the savers

 plugins/html/html.c |   15 ++++++---------
 src/gutils.c        |   48 ++++++++++++++++++++++++++++++++++++++++++++++--
 src/gutils.h        |    3 +++
 src/print-info.c    |   10 +++++++---
 src/stf-export.c    |   15 +++++----------
 5 files changed, 67 insertions(+), 24 deletions(-)
---
diff --git a/plugins/html/html.c b/plugins/html/html.c
index f400c7a..c7076bf 100644
--- a/plugins/html/html.c
+++ b/plugins/html/html.c
@@ -44,12 +44,11 @@
 #include <rendered-value.h>
 #include "style.h"
 #include "hlink.h"
+#include "gutils.h"
 
 #include <gsf/gsf-output.h>
 #include <string.h>
 
-#define SHEET_SELECTION_KEY "sheet-selection"
-
 /*
  * html_version_t:
  *
@@ -699,7 +698,7 @@ html_file_save (GOFileSaver const *fs, GOIOContext *io_context,
        Workbook *wb = wb_view_get_workbook (wb_view);
        GOFileSaveScope save_scope;
        GPtrArray *sel;
-       unsigned ui, count;
+       unsigned ui;
 
        g_return_if_fail (fs != NULL);
        g_return_if_fail (wb != NULL);
@@ -780,14 +779,12 @@ html_file_save (GOFileSaver const *fs, GOIOContext *io_context,
 
        save_scope = go_file_saver_get_save_scope (fs);
 
-       sel = g_object_get_data (G_OBJECT (wb), SHEET_SELECTION_KEY);
-       count = sel ? sel->len : workbook_sheet_count (wb);
-       for (ui = 0; ui < count; ui++) {
-               Sheet *sheet = sel
-                       ? g_ptr_array_index (sel, ui)
-                       : workbook_sheet_by_index (wb, ui);
+       sel = gnm_file_saver_get_sheets (fs, wb_view, TRUE);
+       for (ui = 0; ui < sel->len; ui++) {
+               Sheet *sheet = g_ptr_array_index (sel, ui);
                write_sheet (output, sheet, version, save_scope);
        }
+       g_ptr_array_unref (sel);
 
        if (version == HTML32 || version == HTML40 || version == XHTML)
                gsf_output_puts (output, "</body>\n</html>\n");
diff --git a/src/gutils.c b/src/gutils.c
index d9c02e4..c17278f 100644
--- a/src/gutils.c
+++ b/src/gutils.c
@@ -17,6 +17,7 @@
 #include "ranges.h"
 #include "mathfunc.h"
 #include "workbook-view.h"
+#include "workbook.h"
 
 #include <goffice/goffice.h>
 
@@ -868,17 +869,60 @@ gnm_file_saver_get_sheet (GOFileSaver const *fs, WorkbookView const *wbv)
        GPtrArray *sel;
 
        g_return_val_if_fail (GO_IS_FILE_SAVER (fs), NULL);
-       g_return_val_if_fail (go_file_saver_get_save_scope (fs) == GO_FILE_SAVE_SHEET, NULL);
+       g_return_val_if_fail (go_file_saver_get_save_scope (fs) ==
+                             GO_FILE_SAVE_SHEET, NULL);
        g_return_val_if_fail (GNM_IS_WORKBOOK_VIEW (wbv), NULL);
 
        wb = wb_view_get_workbook (wbv);
 
        sel = g_object_get_data (G_OBJECT (wb), SHEET_SELECTION_KEY);
        if (sel) {
-               if (sel->len)
+               if (sel->len == 1)
                        return g_ptr_array_index (sel, 0);
                g_critical ("Someone messed up sheet selection");
        }
 
        return wb_view_cur_sheet (wbv);
 }
+
+/**
+ * gnm_file_saver_get_sheets:
+ * @fs: #GOFileSaver
+ * @wbv: #WorkbookView
+ * @default_all: If %TRUE, all sheets will be selected by default; if %FALSE,
+ * this function will return %NULL if no sheets were explicitly selected.
+ *
+ * For a workbook-scope saver, this function determines what sheets to save.
+ *
+ * Returns: (transfer container) (element-type Sheet): the sheets to export
+ *
+ * Note: the return value should be unreffed, not freed.
+ */
+GPtrArray *
+gnm_file_saver_get_sheets (GOFileSaver const *fs,
+                          WorkbookView const *wbv,
+                          gboolean default_all)
+{
+       Workbook *wb;
+       GPtrArray *sel;
+
+       g_return_val_if_fail (GO_IS_FILE_SAVER (fs), NULL);
+       g_return_val_if_fail (go_file_saver_get_save_scope (fs) ==
+                             GO_FILE_SAVE_WORKBOOK, NULL);
+       g_return_val_if_fail (GNM_IS_WORKBOOK_VIEW (wbv), NULL);
+
+       wb = wb_view_get_workbook (wbv);
+       sel = g_object_get_data (G_OBJECT (wb), SHEET_SELECTION_KEY);
+       if (sel)
+               g_ptr_array_ref (sel);
+       else if (default_all) {
+               int i;
+               sel = g_ptr_array_new ();
+               for (i = 0; i < workbook_sheet_count (wb); i++) {
+                       Sheet *sheet = workbook_sheet_by_index (wb, i);
+                       g_ptr_array_add (sel, sheet);
+               }
+       }
+
+       return sel;
+}
diff --git a/src/gutils.h b/src/gutils.h
index 0d48555..8940413 100644
--- a/src/gutils.h
+++ b/src/gutils.h
@@ -64,6 +64,9 @@ void gnm_xml_in_doc_dispose_on_exit (GsfXMLInDoc **pdoc);
 
 Sheet *gnm_file_saver_get_sheet (GOFileSaver const *fs,
                                 WorkbookView const *wbv);
+GPtrArray *gnm_file_saver_get_sheets (GOFileSaver const *fs,
+                                     WorkbookView const *wbv,
+                                     gboolean default_all);
 
 G_END_DECLS
 
diff --git a/src/print-info.c b/src/print-info.c
index 3bd9d70..d143b25 100644
--- a/src/print-info.c
+++ b/src/print-info.c
@@ -37,8 +37,6 @@
 #include <locale.h>
 #include <time.h>
 
-#define SHEET_SELECTION_KEY "sheet-selection"
-
 #define PDF_SAVER_ID "Gnumeric_pdf:pdf_assistant"
 
 #define MAX_SAVED_CUSTOM_HF_FORMATS 9
@@ -838,7 +836,10 @@ pdf_write_workbook (G_GNUC_UNUSED GOFileSaver const *fs,
        GPtrArray *sheets;
 
        sheets = g_object_get_data (G_OBJECT (wb), "pdf-sheets");
-       if (!sheets) sheets = g_object_get_data (G_OBJECT (wb), SHEET_SELECTION_KEY);
+       if (sheets)
+               g_ptr_array_ref (sheets);
+       else
+               sheets = gnm_file_saver_get_sheets (fs, wbv, FALSE);
 
        if (sheets) {
                int i;
@@ -853,6 +854,9 @@ pdf_write_workbook (G_GNUC_UNUSED GOFileSaver const *fs,
                }
        }
 
+       if (sheets)
+               g_ptr_array_unref (sheets);
+
        gnm_print_sheet (NULL, wb_view_cur_sheet (wbv), FALSE,
                         GNM_PRINT_ALL_SHEETS, output);
 }
diff --git a/src/stf-export.c b/src/stf-export.c
index e93d25d..a1166f4 100644
--- a/src/stf-export.c
+++ b/src/stf-export.c
@@ -34,6 +34,7 @@
 #include "workbook.h"
 #include "cell.h"
 #include "value.h"
+#include "gutils.h"
 #include "gnm-format.h"
 #include "gnm-datetime.h"
 #include <gsf/gsf-output-iconv.h>
@@ -44,8 +45,6 @@
 #include <string.h>
 #include <locale.h>
 
-#define SHEET_SELECTION_KEY "sheet-selection"
-
 struct _GnmStfExport {
        GsfOutputCsv csv;
 
@@ -681,15 +680,11 @@ gnm_stf_file_saver_save (G_GNUC_UNUSED GOFileSaver const *fs,
 
        nosheets = (stfe->sheet_list == NULL);
        if (nosheets) {
-               GPtrArray *sel = g_object_get_data (G_OBJECT (wb), SHEET_SELECTION_KEY);
-               if (sel) {
-                       unsigned ui;
-                       for (ui = 0; ui < sel->len; ui++)
-                               gnm_stf_export_options_sheet_list_add
-                                       (stfe, g_ptr_array_index (sel, ui));
-               } else
+               GPtrArray *sel = gnm_file_saver_get_sheets (fs, wbv, TRUE);
+               unsigned ui;
+               for (ui = 0; ui < sel->len; ui++)
                        gnm_stf_export_options_sheet_list_add
-                               (stfe, wb_view_cur_sheet (wbv));
+                               (stfe, g_ptr_array_index (sel, ui));
        }
 
        g_object_set (G_OBJECT (stfe), "sink", output, NULL);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]