From 47e6cdb3579238e7520cacd8d70e6a648b2d5383 Mon Sep 17 00:00:00 2001 From: Morten Welinder Date: Fri, 8 Jun 2018 14:22:55 -0400 Subject: [PATCH] Tests: hook up clipboard to ssconvert for testing. No testing actually done yet. --- src/application.c | 4 +- src/gui-clipboard.c | 88 ++++++++++++++++++++++++++++++++++++++++-- src/gui-clipboard.h | 2 + src/ssconvert.c | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 195 insertions(+), 8 deletions(-) diff --git a/src/application.c b/src/application.c index 1231a55a0..bafe29c1e 100644 --- a/src/application.c +++ b/src/application.c @@ -265,7 +265,9 @@ gnm_app_clipboard_cut_copy (WorkbookControl *wbc, gboolean is_cut, g_list_free (l); } - if (wb_control_claim_selection (wbc)) { + if (wbc == NULL) { + // Testing + } else if (wb_control_claim_selection (wbc)) { g_signal_emit (G_OBJECT (app), signals[CLIPBOARD_MODIFIED], 0); } else { gnm_app_clipboard_clear (FALSE); diff --git a/src/gui-clipboard.c b/src/gui-clipboard.c index 26aadbc20..554a8d29e 100644 --- a/src/gui-clipboard.c +++ b/src/gui-clipboard.c @@ -1015,15 +1015,18 @@ object_write (GnmCellRegion *cr, gchar const *mime_type, int *size) * Callback invoked when another application requests we render the selection. */ static void -x_clipboard_get_cb (GtkClipboard *gclipboard, GtkSelectionData *selection_data, - guint info_, gpointer app) +x_clipboard_get_cb (GtkClipboard *gclipboard, + GtkSelectionData *selection_data, + guint info_, G_GNUC_UNUSED gpointer app) { gboolean to_gnumeric = FALSE, content_needs_free = FALSE; GnmCellRegion *clipboard = gnm_app_clipboard_contents_get (); Sheet *sheet = gnm_app_clipboard_sheet_get (); GnmRange const *a = gnm_app_clipboard_area_get (); GOCmdContext *ctx = gnm_cmd_context_stderr_new (); - GdkAtom target = gtk_selection_data_get_target (selection_data); + GdkAtom target = gclipboard + ? gtk_selection_data_get_target (selection_data) + : gtk_selection_data_get_data_type (selection_data); // testing AtomInfoType info = info_; gchar *target_name = gdk_atom_name (target); @@ -1402,6 +1405,85 @@ gnm_x_store_clipboard_if_needed (Workbook *wb) } } +GBytes * +gui_clipboard_test (const char *fmt) +{ + GtkClipboard *gclipboard = NULL; + gpointer app = NULL; + GtkSelectionData *selection_data; + guint info; + unsigned ui; + GdkAtom atom = NULL; + const guchar *data; + gint len; + GBytes *res; + + for (ui = 0; ui < G_N_ELEMENTS (atom_names); ui++) { + if (g_str_equal (fmt, atom_names[ui])) { + atom = atoms[ui]; + break; + } + } + if (!atom) + return NULL; + + switch (ui) { + case ATOM_GNUMERIC: + info = INFO_GNUMERIC; + break; + case ATOM_UTF8_STRING: + case ATOM_STRING: + case ATOM_COMPOUND_TEXT: + info = INFO_GENERIC_TEXT; + break; + case ATOM_TEXT_HTML: + case ATOM_TEXT_HTML_WINDOWS: + info = INFO_HTML; + break; + case ATOM_BIFF8: + case ATOM_BIFF8_OO: + case ATOM_BIFF8_CITRIX: + case ATOM_BIFF5: + case ATOM_BIFF: + info = INFO_EXCEL; + break; + case ATOM_OOO: + case ATOM_OOO_WINDOWS: + case ATOM_OOO11: + info = INFO_OOO; + break; + case ATOM_IMAGE_SVGXML: + case ATOM_IMAGE_XWMF: + case ATOM_IMAGE_XEMF: + case ATOM_IMAGE_PNG: + case ATOM_IMAGE_JPEG: + case ATOM_IMAGE_BMP: + info = INFO_IMAGE; + break; + default: + g_printerr ("Unknown info type\n"); + info = INFO_UNKNOWN; + } + + { + // This is more than a little bit dirty. There is no good + // way to create a GtkSelectionData. + void *empty = g_new0 (char, 1000000); + selection_data = gtk_selection_data_copy (empty); + g_free (empty); + } + + gtk_selection_data_set (selection_data, atom, 8, NULL, 0); + // No way to set target??? + + x_clipboard_get_cb (gclipboard, selection_data, info, app); + data = gtk_selection_data_get_data_with_length (selection_data, &len); + res = g_bytes_new (data, len); + gtk_selection_data_free (selection_data); + return res; +} + + /** * gui_clipboard_init: (skip) */ diff --git a/src/gui-clipboard.h b/src/gui-clipboard.h index fd5769007..3eeaccbbe 100644 --- a/src/gui-clipboard.h +++ b/src/gui-clipboard.h @@ -11,6 +11,8 @@ void gnm_x_store_clipboard_if_needed (Workbook *wb); gboolean gnm_x_claim_clipboard (GdkDisplay *display); void gnm_x_disown_clipboard (void); +GBytes *gui_clipboard_test (const char *fmt); + void gui_clipboard_init (void); void gui_clipboard_shutdown (void); diff --git a/src/ssconvert.c b/src/ssconvert.c index 319e3020d..4f0af7e9f 100644 --- a/src/ssconvert.c +++ b/src/ssconvert.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -31,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -54,6 +56,7 @@ static gboolean ssconvert_one_file_per_sheet = FALSE; static gboolean ssconvert_recalc = FALSE; static gboolean ssconvert_solve = FALSE; static char *ssconvert_resize = NULL; +static char *ssconvert_clipboard = NULL; static char *ssconvert_range = NULL; static char *ssconvert_import_encoding = NULL; static char *ssconvert_import_id = NULL; @@ -155,7 +158,15 @@ static const GOptionEntry ssconvert_options [] = { /* ---------------------------------------- */ - /* For now these are for INTERNAL GNUMERIC USE ONLY. */ + // For now these are for INTERNAL GNUMERIC USE ONLY. They are used + // by the test suite. + { + "clipboard", 0, + G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING, &ssconvert_clipboard, + N_("Output via the clipboard"), + NULL + }, + { "export-range", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING, &ssconvert_range, @@ -1035,6 +1046,83 @@ convert (char const *inarg, char const *outarg, char const *mergeargs[], return res; } +static int +clipboard_export (const char *inarg, char const *outarg, GOCmdContext *cc) +{ + GOFileOpener *fo = NULL; + GOIOContext *io_context = NULL; + WorkbookView *wbv; + Workbook *wb = NULL; + char *infile = go_shell_arg_to_uri (inarg); + char *outfile = go_shell_arg_to_uri (outarg); + int res = 0; + GnmRangeRef const *range; + GnmRange r; + WorkbookControl *wbc = NULL; + GBytes *data = NULL; + GsfOutput *dst; + + io_context = go_io_context_new (cc); + wbv = workbook_view_new_from_uri (infile, fo, + io_context, + ssconvert_import_encoding); + + if (go_io_error_occurred (io_context)) { + go_io_error_display (io_context); + res = 1; + goto out; + } else if (wbv == NULL) { + g_printerr (_("Loading %s failed\n"), infile); + res = 1; + goto out; + } + + wb = wb_view_get_workbook (wbv); + + range = setup_range (G_OBJECT (wb), + "ssconvert-range", + wb, + ssconvert_range); + range_init_rangeref (&r, range); + if (range->a.sheet) + wb_view_sheet_focus (wbv, range->a.sheet); + + gnm_app_clipboard_cut_copy (wbc, FALSE, + wb_view_cur_sheet_view (wbv), + &r, FALSE); + + data = gui_clipboard_test (ssconvert_clipboard); + if (!data) { + g_printerr ("Failed to get clipboard data.\n"); + res = 1; + goto out; + } + + dst = go_file_create (outfile, NULL); + if (!dst) { + g_printerr ("Failed to write to %s\n", outfile); + res = 1; + goto out; + } + + gsf_output_write (dst, g_bytes_get_size (data), + g_bytes_get_data (data, NULL)); + gsf_output_close (dst); + g_object_unref (dst); + + out: + if (data) + g_bytes_unref (data); + if (wb) + g_object_unref (wb); + if (io_context) + g_object_unref (io_context); + g_free (infile); + g_free (outfile); + + return res; +} + int main (int argc, char const **argv) { @@ -1043,6 +1131,7 @@ main (int argc, char const **argv) GOCmdContext *cc; GOptionContext *ocontext; GError *error = NULL; + gboolean do_usage = FALSE; /* No code before here, we need to init threads */ argv = gnm_pre_parse_init (argc, argv); @@ -1098,11 +1187,23 @@ main (int argc, char const **argv) list_them (go_get_file_openers (), (get_desc_f) &go_file_opener_get_id, (get_desc_f) &go_file_opener_get_description); - else if (ssconvert_merge_target!=NULL && argc>=3) { - res = convert (argv[1], ssconvert_merge_target, argv+1, cc); + else if (ssconvert_clipboard) + if (argc == 3 && ssconvert_range) + res = clipboard_export (argv[1], argv[2], cc); + else + do_usage = TRUE; + else if (ssconvert_merge_target) { + if (argc >= 3) + res = convert (argv[1], ssconvert_merge_target, + argv + 1, cc); + else + do_usage = TRUE; } else if (argc == 2 || argc == 3) { res = convert (argv[1], argv[2], NULL, cc); - } else { + } else + do_usage = TRUE; + + if (do_usage) { g_printerr (_("Usage: %s [OPTION...] %s\n"), g_get_prgname (), _("INFILE [OUTFILE]")); -- 2.11.4.GIT