~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

« back to all changes in this revision

Viewing changes to tools/test-clipboard.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2007-05-02 16:33:03 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070502163303-bvzhjzbpw8qglc4y
Tags: 2.3.16-1ubuntu1
* Resynchronized with Debian, remaining Ubuntu changes:
  - debian/rules: i18n magic.
* debian/control.in:
  - Maintainer: Ubuntu Core Developers <ubuntu-devel@lists.ubuntu.com>
* debian/patches/02_help-message.patch,
  debian/patches/03_gimp.desktop.in.in.patch,
  debian/patches/10_dont_show_wizard.patch: updated.
* debian/patches/04_composite-signedness.patch,
  debian/patches/05_add-letter-spacing.patch: dropped, used upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * test-clipboard.c -- do clipboard things
 
3
 *
 
4
 * Copyright (C) 2005  Michael Natterer <mitch@gimp.org>
 
5
 *
 
6
 * Use this code for whatever you like.
 
7
 */
 
8
 
 
9
#include "config.h"
 
10
 
 
11
#include <errno.h>
 
12
#include <fcntl.h>
 
13
#include <stdlib.h>
 
14
#include <string.h>
 
15
#include <sys/types.h>
 
16
#include <sys/stat.h>
 
17
#ifdef HAVE_UNISTD_H
 
18
#include <unistd.h>
 
19
#endif
 
20
 
 
21
#include <gtk/gtk.h>
 
22
 
 
23
#include "libgimpbase/gimpversion.h"
 
24
 
 
25
 
 
26
typedef struct _CopyData CopyData;
 
27
 
 
28
struct _CopyData
 
29
{
 
30
  const gchar *filename;
 
31
  gboolean     file_copied;
 
32
  GError      *error;
 
33
};
 
34
 
 
35
 
 
36
static void     test_clipboard_show_version    (void) G_GNUC_NORETURN;
 
37
static gboolean test_clipboard_parse_selection (const gchar       *option_name,
 
38
                                                const gchar       *value,
 
39
                                                gpointer           data,
 
40
                                                GError           **error);
 
41
static gboolean test_clipboard_list_targets    (GtkClipboard      *clipboard);
 
42
static gboolean test_clipboard_copy            (GtkClipboard      *clipboard,
 
43
                                                const gchar       *target,
 
44
                                                const gchar       *filename);
 
45
static gboolean test_clipboard_store           (GtkClipboard      *clipboard,
 
46
                                                const gchar       *target,
 
47
                                                const gchar       *filename);
 
48
static gboolean test_clipboard_paste           (GtkClipboard      *clipboard,
 
49
                                                const gchar       *target,
 
50
                                                const gchar       *filename);
 
51
static void     test_clipboard_copy_callback   (GtkClipboard      *clipboard,
 
52
                                                GtkSelectionData  *selection,
 
53
                                                guint              info,
 
54
                                                gpointer           data);
 
55
 
 
56
 
 
57
static GdkAtom   option_selection_type = GDK_SELECTION_CLIPBOARD;
 
58
static gboolean  option_list_targets   = FALSE;
 
59
static gchar    *option_target         = NULL;
 
60
static gchar    *option_copy_filename  = NULL;
 
61
static gchar    *option_store_filename = NULL;
 
62
static gchar    *option_paste_filename = NULL;
 
63
 
 
64
static const GOptionEntry main_entries[] =
 
65
{
 
66
  {
 
67
    "selection-type", 's', 0,
 
68
    G_OPTION_ARG_CALLBACK, test_clipboard_parse_selection,
 
69
    "Selection type (primary|secondary|clipboard)", "<type>"
 
70
  },
 
71
  {
 
72
    "list-targets", 'l', 0,
 
73
    G_OPTION_ARG_NONE, &option_list_targets,
 
74
    "List the targets offered by the clipboard", NULL
 
75
  },
 
76
  {
 
77
    "target", 't', 0,
 
78
    G_OPTION_ARG_STRING, &option_target,
 
79
    "The target format to copy or paste", "<target>"
 
80
  },
 
81
  {
 
82
    "copy", 'c', 0,
 
83
    G_OPTION_ARG_STRING, &option_copy_filename,
 
84
    "Copy <file> to clipboard", "<file>"
 
85
  },
 
86
  {
 
87
    "store", 'S', 0,
 
88
    G_OPTION_ARG_STRING, &option_store_filename,
 
89
    "Store <file> in the clipboard manager", "<file>"
 
90
  },
 
91
  {
 
92
    "paste", 'p', 0,
 
93
    G_OPTION_ARG_STRING, &option_paste_filename,
 
94
    "Paste clipoard into <file>", "<file>"
 
95
  },
 
96
  {
 
97
    "version", 'v', G_OPTION_FLAG_NO_ARG,
 
98
    G_OPTION_ARG_CALLBACK, test_clipboard_show_version,
 
99
    "Show version information and exit", NULL
 
100
  },
 
101
  { NULL }
 
102
};
 
103
 
 
104
 
 
105
gint
 
106
main (gint   argc,
 
107
      gchar *argv[])
 
108
{
 
109
  GOptionContext *context;
 
110
  GtkClipboard   *clipboard;
 
111
  GError         *error = NULL;
 
112
 
 
113
  context = g_option_context_new (NULL);
 
114
  g_option_context_add_main_entries (context, main_entries, NULL);
 
115
  g_option_context_add_group (context, gtk_get_option_group (TRUE));
 
116
 
 
117
  if (! g_option_context_parse (context, &argc, &argv, &error))
 
118
    {
 
119
      if (error)
 
120
        {
 
121
          g_printerr ("%s\n", error->message);
 
122
          g_error_free (error);
 
123
        }
 
124
      else
 
125
        {
 
126
          g_print ("%s\n",
 
127
                   "Could not initialize the graphical user interface.\n"
 
128
                   "Make sure a proper setup for your display environment "
 
129
                   "exists.");
 
130
        }
 
131
 
 
132
      return EXIT_FAILURE;
 
133
    }
 
134
 
 
135
  gtk_init (&argc, &argv);
 
136
 
 
137
  clipboard = gtk_clipboard_get_for_display (gdk_display_get_default (),
 
138
                                             option_selection_type);
 
139
 
 
140
  if (! clipboard)
 
141
    g_error ("gtk_clipboard_get_for_display");
 
142
 
 
143
  if (option_list_targets)
 
144
    {
 
145
      if (! test_clipboard_list_targets (clipboard))
 
146
        return EXIT_FAILURE;
 
147
 
 
148
      return EXIT_SUCCESS;
 
149
    }
 
150
 
 
151
  if ((option_copy_filename  && option_paste_filename) ||
 
152
      (option_copy_filename  && option_store_filename) ||
 
153
      (option_paste_filename && option_store_filename))
 
154
    {
 
155
      g_printerr ("Can't perform two operations at the same time\n");
 
156
      return EXIT_FAILURE;
 
157
    }
 
158
 
 
159
  if (option_copy_filename)
 
160
    {
 
161
      if (! option_target)
 
162
        {
 
163
          g_printerr ("Usage: %s -t <target> -c <file>\n", argv[0]);
 
164
          return EXIT_FAILURE;
 
165
        }
 
166
 
 
167
      if (! test_clipboard_copy (clipboard, option_target,
 
168
                                 option_copy_filename))
 
169
        return EXIT_FAILURE;
 
170
    }
 
171
 
 
172
  if (option_store_filename)
 
173
    {
 
174
      if (! option_target)
 
175
        {
 
176
          g_printerr ("Usage: %s -t <target> -S <file>\n", argv[0]);
 
177
          return EXIT_FAILURE;
 
178
        }
 
179
 
 
180
      if (! test_clipboard_store (clipboard, option_target,
 
181
                                  option_store_filename))
 
182
        return EXIT_FAILURE;
 
183
    }
 
184
 
 
185
  if (option_paste_filename)
 
186
    {
 
187
      if (! option_target)
 
188
        {
 
189
          g_printerr ("Usage: %s -t <target> -p <file>\n", argv[0]);
 
190
          return EXIT_FAILURE;
 
191
        }
 
192
 
 
193
      if (! test_clipboard_paste (clipboard, option_target,
 
194
                                  option_paste_filename))
 
195
        return EXIT_FAILURE;
 
196
    }
 
197
 
 
198
  return EXIT_SUCCESS;
 
199
}
 
200
 
 
201
static void
 
202
test_clipboard_show_version (void)
 
203
{
 
204
  g_print ("test-clipboard (GIMP clipboard testbed) version %s\n",
 
205
           GIMP_VERSION);
 
206
 
 
207
  exit (EXIT_SUCCESS);
 
208
}
 
209
 
 
210
static gboolean
 
211
test_clipboard_parse_selection (const gchar  *option_name,
 
212
                                const gchar  *value,
 
213
                                gpointer      data,
 
214
                                GError      **error)
 
215
{
 
216
  if (! strcmp (value, "primary"))
 
217
    option_selection_type = GDK_SELECTION_PRIMARY;
 
218
  else if (! strcmp (value, "secondary"))
 
219
    option_selection_type = GDK_SELECTION_SECONDARY;
 
220
  else if (! strcmp (value, "clipboard"))
 
221
    option_selection_type = GDK_SELECTION_CLIPBOARD;
 
222
  else
 
223
    return FALSE;
 
224
 
 
225
  return TRUE;
 
226
}
 
227
 
 
228
static gboolean
 
229
test_clipboard_list_targets (GtkClipboard *clipboard)
 
230
{
 
231
  GtkSelectionData *data;
 
232
 
 
233
  data = gtk_clipboard_wait_for_contents (clipboard,
 
234
                                          gdk_atom_intern ("TARGETS",
 
235
                                                           FALSE));
 
236
  if (data)
 
237
    {
 
238
      GdkAtom  *targets;
 
239
      gint      n_targets;
 
240
      gboolean  success;
 
241
 
 
242
      success = gtk_selection_data_get_targets (data, &targets, &n_targets);
 
243
 
 
244
      gtk_selection_data_free (data);
 
245
 
 
246
      if (success)
 
247
        {
 
248
          gint i;
 
249
 
 
250
          for (i = 0; i < n_targets; i++)
 
251
            g_print ("%s\n", gdk_atom_name (targets[i]));
 
252
 
 
253
          g_free (targets);
 
254
        }
 
255
    }
 
256
 
 
257
  return TRUE;
 
258
}
 
259
 
 
260
static gboolean
 
261
test_clipboard_copy (GtkClipboard *clipboard,
 
262
                     const gchar  *target,
 
263
                     const gchar  *filename)
 
264
{
 
265
  GtkTargetEntry entry;
 
266
  CopyData       data;
 
267
 
 
268
  entry.target = g_strdup (target);
 
269
  entry.flags  = 0;
 
270
  entry.info   = 1;
 
271
 
 
272
  data.filename    = filename;
 
273
  data.file_copied = FALSE;
 
274
  data.error       = NULL;
 
275
 
 
276
  if (! gtk_clipboard_set_with_data (clipboard, &entry, 1,
 
277
                                     test_clipboard_copy_callback,
 
278
                                     NULL,
 
279
                                     &data))
 
280
    {
 
281
      g_printerr ("%s: gtk_clipboard_set_with_data() failed\n",
 
282
                  g_get_prgname());
 
283
      return FALSE;
 
284
    }
 
285
 
 
286
  gtk_main ();
 
287
 
 
288
  if (! data.file_copied)
 
289
    {
 
290
      if (data.error)
 
291
        {
 
292
          g_printerr ("%s: copying failed: %s\n",
 
293
                      g_get_prgname (), data.error->message);
 
294
          g_error_free (data.error);
 
295
        }
 
296
      else
 
297
        {
 
298
          g_printerr ("%s: copying failed\n",
 
299
                      g_get_prgname ());
 
300
        }
 
301
 
 
302
      return FALSE;
 
303
    }
 
304
 
 
305
  return TRUE;
 
306
}
 
307
 
 
308
static gboolean
 
309
test_clipboard_store (GtkClipboard *clipboard,
 
310
                      const gchar  *target,
 
311
                      const gchar  *filename)
 
312
{
 
313
  GtkTargetEntry entry;
 
314
  CopyData       data;
 
315
 
 
316
  entry.target = g_strdup (target);
 
317
  entry.flags  = 0;
 
318
  entry.info   = 1;
 
319
 
 
320
  data.filename    = filename;
 
321
  data.file_copied = FALSE;
 
322
  data.error       = NULL;
 
323
 
 
324
  if (! gtk_clipboard_set_with_data (clipboard, &entry, 1,
 
325
                                     test_clipboard_copy_callback,
 
326
                                     NULL,
 
327
                                     &data))
 
328
    {
 
329
      g_printerr ("%s: gtk_clipboard_set_with_data() failed\n",
 
330
                  g_get_prgname ());
 
331
      return FALSE;
 
332
    }
 
333
 
 
334
  gtk_clipboard_set_can_store (clipboard, &entry, 1);
 
335
  gtk_clipboard_store (clipboard);
 
336
 
 
337
  if (! data.file_copied)
 
338
    {
 
339
      if (data.error)
 
340
        {
 
341
          g_printerr ("%s: storing failed: %s\n",
 
342
                      g_get_prgname (), data.error->message);
 
343
          g_error_free (data.error);
 
344
        }
 
345
      else
 
346
        {
 
347
          g_printerr ("%s: could not contact clipboard manager\n",
 
348
                      g_get_prgname ());
 
349
        }
 
350
 
 
351
      return FALSE;
 
352
    }
 
353
 
 
354
  return TRUE;
 
355
}
 
356
 
 
357
static gboolean
 
358
test_clipboard_paste (GtkClipboard *clipboard,
 
359
                      const gchar  *target,
 
360
                      const gchar  *filename)
 
361
{
 
362
  GtkSelectionData *data;
 
363
 
 
364
  data = gtk_clipboard_wait_for_contents (clipboard,
 
365
                                          gdk_atom_intern (target,
 
366
                                                           FALSE));
 
367
  if (data)
 
368
    {
 
369
      gsize bytes;
 
370
      gint  fd;
 
371
 
 
372
      fd = open (filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
 
373
 
 
374
      if (fd < 0)
 
375
        {
 
376
          g_printerr ("%s: open() filed: %s",
 
377
                      g_get_prgname (), g_strerror (errno));
 
378
          return FALSE;
 
379
        }
 
380
 
 
381
      bytes = data->length * data->format / 8;
 
382
 
 
383
      if (write (fd, data->data, bytes) < bytes)
 
384
        {
 
385
          close (fd);
 
386
          g_printerr ("%s: write() failed: %s",
 
387
                      g_get_prgname (), g_strerror (errno));
 
388
          return FALSE;
 
389
        }
 
390
 
 
391
      if (close (fd) < 0)
 
392
        {
 
393
          g_printerr ("%s: close() failed: %s",
 
394
                      g_get_prgname (), g_strerror (errno));
 
395
          return FALSE;
 
396
        }
 
397
 
 
398
      gtk_selection_data_free (data);
 
399
    }
 
400
 
 
401
  return TRUE;
 
402
}
 
403
 
 
404
static void
 
405
test_clipboard_copy_callback (GtkClipboard     *clipboard,
 
406
                              GtkSelectionData *selection,
 
407
                              guint             info,
 
408
                              gpointer          data)
 
409
{
 
410
  CopyData *copy_data = data;
 
411
  gchar    *buf;
 
412
  gsize     buf_size;
 
413
 
 
414
  if (! g_file_get_contents (copy_data->filename, &buf, &buf_size,
 
415
                             &copy_data->error))
 
416
    {
 
417
      if (! option_store_filename)
 
418
        gtk_main_quit ();
 
419
 
 
420
      return;
 
421
    }
 
422
 
 
423
  gtk_selection_data_set (selection, selection->target,
 
424
                          8, (guchar *) buf, buf_size);
 
425
 
 
426
  g_free (buf);
 
427
 
 
428
  copy_data->file_copied = TRUE;
 
429
 
 
430
  g_print ("%s: data transfer in progress, hit <ctrl>+c when pasted...",
 
431
           G_STRFUNC);
 
432
}