Commit baee43b5 authored by Michael Natterer's avatar Michael Natterer 😴 Committed by Michael Natterer
Browse files

added --store option which tries to store a file in the clipboard manager,

2005-05-25  Michael Natterer  <mitch@gimp.org>

	* tools/test-clipboard.c: added --store option which tries to
	store a file in the clipboard manager, improved error reporting.
parent 014f22c4
2005-05-25 Michael Natterer <mitch@gimp.org>
* tools/test-clipboard.c: added --store option which tries to
store a file in the clipboard manager, improved error reporting.
2005-05-24 Sven Neumann <sven@gimp.org>
* libgimpwidgets/gimpcolorselect.c: only set a horizontal size
......
......@@ -21,15 +21,28 @@
#include <gtk/gtk.h>
typedef struct _CopyData CopyData;
struct _CopyData
{
const gchar *filename;
gboolean file_copied;
GError *error;
};
static gboolean test_clipboard_parse_selection (const gchar *option_name,
const gchar *value,
gpointer data,
GError **error);
static void test_clipboard_list_targets (GtkClipboard *clipboard);
static void test_clipboard_copy (GtkClipboard *clipboard,
static gboolean test_clipboard_list_targets (GtkClipboard *clipboard);
static gboolean test_clipboard_copy (GtkClipboard *clipboard,
const gchar *target,
const gchar *filename);
static void test_clipboard_paste (GtkClipboard *clipboard,
static gboolean test_clipboard_store (GtkClipboard *clipboard,
const gchar *target,
const gchar *filename);
static gboolean test_clipboard_paste (GtkClipboard *clipboard,
const gchar *target,
const gchar *filename);
static void test_clipboard_copy_callback (GtkClipboard *clipboard,
......@@ -42,6 +55,7 @@ static GdkAtom option_selection_type = GDK_SELECTION_CLIPBOARD;
static gboolean option_list_targets = FALSE;
static gchar *option_target = NULL;
static gchar *option_copy_filename = NULL;
static gchar *option_store_filename = NULL;
static gchar *option_paste_filename = NULL;
static const GOptionEntry main_entries[] =
......@@ -67,6 +81,11 @@ static const GOptionEntry main_entries[] =
G_OPTION_ARG_STRING, &option_copy_filename,
"Copy <file> to clipboard", "<file>"
},
{
"store", 'S', 0,
G_OPTION_ARG_STRING, &option_store_filename,
"Store <file> in the clipboard manager", "<file>"
},
{
"paste", 'p', 0,
G_OPTION_ARG_STRING, &option_paste_filename,
......@@ -116,13 +135,17 @@ main (gint argc,
if (option_list_targets)
{
test_clipboard_list_targets (clipboard);
if (! test_clipboard_list_targets (clipboard))
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
if (option_copy_filename && option_paste_filename)
if ((option_copy_filename && option_paste_filename) ||
(option_copy_filename && option_store_filename) ||
(option_paste_filename && option_store_filename))
{
g_printerr ("Can't copy and paste at the same time\n");
g_printerr ("Can't perform two operations at the same time\n");
return EXIT_FAILURE;
}
......@@ -134,8 +157,22 @@ main (gint argc,
return EXIT_FAILURE;
}
test_clipboard_copy (clipboard, option_target, option_copy_filename);
return EXIT_SUCCESS;
if (! test_clipboard_copy (clipboard, option_target,
option_copy_filename))
return EXIT_FAILURE;
}
if (option_store_filename)
{
if (! option_target)
{
g_printerr ("Usage: %s -t <target> -S <file>\n", argv[0]);
return EXIT_FAILURE;
}
if (! test_clipboard_store (clipboard, option_target,
option_store_filename))
return EXIT_FAILURE;
}
if (option_paste_filename)
......@@ -146,8 +183,9 @@ main (gint argc,
return EXIT_FAILURE;
}
test_clipboard_paste (clipboard, option_target, option_paste_filename);
return EXIT_SUCCESS;
if (! test_clipboard_paste (clipboard, option_target,
option_paste_filename))
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
......@@ -171,7 +209,7 @@ test_clipboard_parse_selection (const gchar *option_name,
return TRUE;
}
static void
static gboolean
test_clipboard_list_targets (GtkClipboard *clipboard)
{
GtkSelectionData *data;
......@@ -199,29 +237,108 @@ test_clipboard_list_targets (GtkClipboard *clipboard)
g_free (targets);
}
}
return TRUE;
}
static void
static gboolean
test_clipboard_copy (GtkClipboard *clipboard,
const gchar *target,
const gchar *filename)
{
GtkTargetEntry entry;
CopyData data;
entry.target = g_strdup (target);
entry.flags = 0;
entry.info = 1;
data.filename = filename;
data.file_copied = FALSE;
data.error = NULL;
if (! gtk_clipboard_set_with_data (clipboard, &entry, 1,
test_clipboard_copy_callback,
NULL,
g_strdup (filename)))
g_error ("gtk_clipboard_set_with_data");
&data))
{
g_printerr ("%s: gtk_clipboard_set_with_data() failed\n",
g_get_prgname());
return FALSE;
}
gtk_main ();
if (! data.file_copied)
{
if (data.error)
{
g_printerr ("%s: copying failed: %s\n",
g_get_prgname (), data.error->message);
g_error_free (data.error);
}
else
{
g_printerr ("%s: copying failed\n",
g_get_prgname ());
}
return FALSE;
}
return TRUE;
}
static void
static gboolean
test_clipboard_store (GtkClipboard *clipboard,
const gchar *target,
const gchar *filename)
{
GtkTargetEntry entry;
CopyData data;
entry.target = g_strdup (target);
entry.flags = 0;
entry.info = 1;
data.filename = filename;
data.file_copied = FALSE;
data.error = NULL;
if (! gtk_clipboard_set_with_data (clipboard, &entry, 1,
test_clipboard_copy_callback,
NULL,
&data))
{
g_printerr ("%s: gtk_clipboard_set_with_data() failed\n",
g_get_prgname ());
return FALSE;
}
gtk_clipboard_set_can_store (clipboard, &entry, 1);
gtk_clipboard_store (clipboard);
if (! data.file_copied)
{
if (data.error)
{
g_printerr ("%s: storing failed: %s\n",
g_get_prgname (), data.error->message);
g_error_free (data.error);
}
else
{
g_printerr ("%s: could not contact clipboard manager\n",
g_get_prgname ());
}
return FALSE;
}
return TRUE;
}
static gboolean
test_clipboard_paste (GtkClipboard *clipboard,
const gchar *target,
const gchar *filename)
......@@ -239,18 +356,33 @@ test_clipboard_paste (GtkClipboard *clipboard,
fd = open (filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0)
g_error ("open: %s", g_strerror (errno));
{
g_printerr ("%s: open() filed: %s",
g_get_prgname (), g_strerror (errno));
return FALSE;
}
bytes = data->length * data->format / 8;
if (write (fd, data->data, bytes) < bytes)
g_error ("write: %s", g_strerror (errno));
{
close (fd);
g_printerr ("%s: write() failed: %s",
g_get_prgname (), g_strerror (errno));
return FALSE;
}
if (close (fd) < 0)
g_error ("close: %s", g_strerror (errno));
{
g_printerr ("%s: close() failed: %s",
g_get_prgname (), g_strerror (errno));
return FALSE;
}
gtk_selection_data_free (data);
}
return TRUE;
}
static void
......@@ -259,20 +391,21 @@ test_clipboard_copy_callback (GtkClipboard *clipboard,
guint info,
gpointer data)
{
gchar *filename = data;
gchar *buf;
gsize buf_size;
GError *error = NULL;
CopyData *copy_data = data;
gchar *buf;
gsize buf_size;
if (! g_file_get_contents (filename, &buf, &buf_size, &error))
g_error ("g_file_get_contents: %s", error->message);
g_free (filename);
if (! g_file_get_contents (copy_data->filename, &buf, &buf_size,
&copy_data->error))
return;
gtk_selection_data_set (selection, selection->target,
8, (guchar *) buf, buf_size);
g_free (buf);
gtk_main_quit ();
if (! option_store_filename)
gtk_main_quit ();
copy_data->file_copied = TRUE;
}
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