gimpselectiondata.c 21.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <stdlib.h>
Sven Neumann's avatar
Sven Neumann committed
22
#include <stdio.h>
23
#include <string.h>
Michael Natterer's avatar
Michael Natterer committed
24 25 26 27 28 29
#include <sys/types.h>
#include <unistd.h>

#ifdef G_OS_WIN32
#include <process.h> /* getpid() */
#endif
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

#include <gtk/gtk.h>

#include "libgimpcolor/gimpcolor.h"

#include "widgets-types.h"

#include "core/gimp.h"
#include "core/gimpbrush.h"
#include "core/gimpcontainer.h"
#include "core/gimpdatafactory.h"
#include "core/gimpgradient.h"
#include "core/gimpimage.h"
#include "core/gimpimagefile.h"
#include "core/gimpitem.h"
#include "core/gimppalette.h"
#include "core/gimppattern.h"
#include "core/gimptoolinfo.h"

#include "text/gimpfont.h"

#include "gimpselectiondata.h"

#include "gimp-intl.h"


/* #define DEBUG_DND */

#ifdef DEBUG_DND
#define D(stmnt) stmnt
#else
#define D(stmnt)
#endif


Michael Natterer's avatar
Michael Natterer committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78
/*  local function prototypes  */

static gchar      * gimp_selection_data_get_name   (GtkSelectionData *selection);
static GimpObject * gimp_selection_data_get_object (GtkSelectionData *selection,
                                                    GimpContainer    *container,
                                                    GimpObject       *additional);
static gchar      * gimp_unescape_uri_string       (const char       *escaped,
                                                    int               len,
                                                    const char       *illegal_escaped_characters,
                                                    gboolean          ascii_must_not_be_escaped);


/*  public functions  */

79
void
80 81
gimp_selection_data_set_uri_list (GtkSelectionData *selection,
                                  GList            *uri_list)
82 83 84 85 86
{
  GList *list;
  gchar *vals = NULL;

  g_return_if_fail (selection != NULL);
87
  g_return_if_fail (uri_list != NULL);
88

89
  for (list = uri_list; list; list = g_list_next (list))
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    {
      if (vals)
        {
          gchar *tmp = g_strconcat (vals,
                                    list->data,
                                    list->next ? "\n" : NULL,
                                    NULL);
          g_free (vals);
          vals = tmp;
        }
      else
        {
          vals = g_strconcat (list->data,
                              list->next ? "\n" : NULL,
                              NULL);
        }
    }

108
  gtk_selection_data_set (selection, selection->target,
109 110 111 112 113 114
                          8, (guchar *) vals, strlen (vals) + 1);

  g_free (vals);
}

GList *
115
gimp_selection_data_get_uri_list (GtkSelectionData *selection)
116
{
117 118 119 120
  GList       *crap_list = NULL;
  GList       *uri_list  = NULL;
  GList       *list;
  const gchar *buffer;
121 122 123

  g_return_val_if_fail (selection != NULL, NULL);

124
  if ((selection->format != 8) || (selection->length < 1))
125 126 127 128 129
    {
      g_warning ("Received invalid file data!");
      return NULL;
    }

130
  buffer = (const gchar *) selection->data;
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

  D (g_print ("%s: raw buffer >>%s<<\n", G_STRFUNC, buffer));

  {
    gchar name_buffer[1024];

    while (*buffer && (buffer - (gchar *) selection->data < selection->length))
      {
	gchar *name = name_buffer;
	gint   len  = 0;

	while (len < sizeof (name_buffer) && *buffer && *buffer != '\n')
	  {
	    *name++ = *buffer++;
	    len++;
	  }
	if (len == 0)
	  break;

	if (*(name - 1) == 0xd)   /* gmc uses RETURN+NEWLINE as delimiter */
	  len--;

	if (len > 2)
	  crap_list = g_list_prepend (crap_list, g_strndup (name_buffer, len));

	if (*buffer)
	  buffer++;
      }
  }

  if (! crap_list)
    return NULL;

  /*  do various checks because file drag sources send all kinds of
   *  arbitrary crap...
   */
  for (list = crap_list; list; list = g_list_next (list))
    {
      const gchar *dnd_crap = list->data;
      gchar       *filename;
171
      gchar       *hostname;
172 173 174 175 176 177
      gchar       *uri   = NULL;
      GError      *error = NULL;

      D (g_print ("%s: trying to convert \"%s\" to an uri.\n",
                  G_STRFUNC, dnd_crap));

178
      filename = g_filename_from_uri (dnd_crap, &hostname, NULL);
179 180 181

      if (filename)
        {
182 183 184 185 186
          /*  if we got a correctly encoded "file:" uri...
           *
           *  (for GLib < 2.4.4, this is escaped UTF-8,
           *   for GLib > 2.4.4, this is escaped local filename encoding)
           */
187

188
          uri = g_filename_to_uri (filename, hostname, NULL);
189

190
          g_free (hostname);
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
          g_free (filename);
        }
      else if (g_file_test (dnd_crap, G_FILE_TEST_EXISTS))
        {
          /*  ...else if we got a valid local filename...  */

          uri = g_filename_to_uri (dnd_crap, NULL, NULL);
        }
      else
        {
          /*  ...otherwise do evil things...  */

          const gchar *start = dnd_crap;

          if (! strncmp (dnd_crap, "file://", strlen ("file://")))
            {
              start += strlen ("file://");
            }
          else if (! strncmp (dnd_crap, "file:", strlen ("file:")))
            {
              start += strlen ("file:");
            }

          if (start != dnd_crap)
            {
216 217 218 219 220 221
              /*  try if we got a "file:" uri in the wrong encoding...
               *
               *  (for GLib < 2.4.4, this is escaped local filename encoding,
               *   for GLib > 2.4.4, this is escaped UTF-8)
               */
              gchar *unescaped_filename;
222 223 224

              if (strstr (dnd_crap, "%"))
                {
225 226
                  gchar *local_filename;

227 228
                  unescaped_filename = gimp_unescape_uri_string (start, -1,
                                                                 "/", FALSE);
229

230 231 232 233 234 235 236 237 238
                  /*  check if we got a drop from an application that
                   *  encodes file: URIs as UTF-8 (apps linked against
                   *  GLib < 2.4.4)
                   */
                  local_filename = g_filename_from_utf8 (unescaped_filename,
                                                         -1, NULL, NULL,
                                                         NULL);

                  if (local_filename)
239
                    {
240 241
                      g_free (unescaped_filename);
                      unescaped_filename = local_filename;
242
                    }
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
                }
              else
                {
                  unescaped_filename = g_strdup (start);
                }

              uri = g_filename_to_uri (unescaped_filename, NULL, &error);

              if (! uri)
                {
                  gchar *escaped_filename = g_strescape (unescaped_filename,
                                                         NULL);

                  g_message (_("The filename '%s' couldn't be converted to a "
                               "valid URI:\n\n%s"),
                             escaped_filename,
                             error->message ?
                             error->message : _("Invalid UTF-8"));
                  g_free (escaped_filename);
                  g_clear_error (&error);

                  g_free (unescaped_filename);
                  continue;
                }

              g_free (unescaped_filename);
            }
          else
            {
              /*  otherwise try the crap passed anyway, in case it's
               *  a "http:" or whatever uri a plug-in might handle
               */
              uri = g_strdup (dnd_crap);
            }
        }

      uri_list = g_list_prepend (uri_list, uri);
    }

  g_list_foreach (crap_list, (GFunc) g_free, NULL);
  g_list_free (crap_list);

  return uri_list;
}

void
gimp_selection_data_set_color (GtkSelectionData *selection,
                               const GimpRGB    *color)
{
  guint16 *vals;
  guchar   r, g, b, a;

  g_return_if_fail (selection != NULL);
  g_return_if_fail (color != NULL);

  vals = g_new (guint16, 4);

  gimp_rgba_get_uchar (color, &r, &g, &b, &a);

  vals[0] = r + (r << 8);
  vals[1] = g + (g << 8);
  vals[2] = b + (b << 8);
  vals[3] = a + (a << 8);

307
  gtk_selection_data_set (selection, selection->target,
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
                          16, (guchar *) vals, 8);

  g_free (vals);
}

gboolean
gimp_selection_data_get_color (GtkSelectionData *selection,
                               GimpRGB          *color)
{
  guint16 *color_vals;

  g_return_val_if_fail (selection != NULL, FALSE);
  g_return_val_if_fail (color != NULL, FALSE);

  if ((selection->format != 16) || (selection->length != 8))
    {
      g_warning ("Received invalid color data!");
      return FALSE;
    }

  color_vals = (guint16 *) selection->data;

  gimp_rgba_set_uchar (color,
		       (guchar) (color_vals[0] >> 8),
		       (guchar) (color_vals[1] >> 8),
		       (guchar) (color_vals[2] >> 8),
		       (guchar) (color_vals[3] >> 8));

  return TRUE;
}

void
340 341 342
gimp_selection_data_set_stream (GtkSelectionData *selection,
                                const guchar     *stream,
                                gsize             stream_length)
343 344
{
  g_return_if_fail (selection != NULL);
345 346
  g_return_if_fail (stream != NULL);
  g_return_if_fail (stream_length > 0);
347

348
  gtk_selection_data_set (selection, selection->target,
349
                          8, (guchar *) stream, stream_length);
350 351
}

352 353 354
const guchar *
gimp_selection_data_get_stream (GtkSelectionData *selection,
                                gsize            *stream_length)
355 356
{
  g_return_val_if_fail (selection != NULL, NULL);
357
  g_return_val_if_fail (stream_length != NULL, NULL);
358

359
  if ((selection->format != 8) || (selection->length < 1))
360
    {
361
      g_warning ("Received invalid data stream!");
362 363 364
      return NULL;
    }

365
  *stream_length = selection->length;
366

367
  return (const guchar *) selection->data;
368 369 370 371 372 373
}

void
gimp_selection_data_set_image (GtkSelectionData *selection,
                               GimpImage        *gimage)
{
Michael Natterer's avatar
Michael Natterer committed
374
  gchar *str;
375 376 377 378

  g_return_if_fail (selection != NULL);
  g_return_if_fail (GIMP_IS_IMAGE (gimage));

Michael Natterer's avatar
Michael Natterer committed
379
  str = g_strdup_printf ("%d:%d", getpid (), gimp_image_get_ID (gimage));
380

381
  gtk_selection_data_set (selection, selection->target,
Michael Natterer's avatar
Michael Natterer committed
382
                          8, (guchar *) str, strlen (str) + 1);
383

Michael Natterer's avatar
Michael Natterer committed
384
  g_free (str);
385 386 387 388 389 390
}

GimpImage *
gimp_selection_data_get_image (GtkSelectionData *selection,
                               Gimp             *gimp)
{
Michael Natterer's avatar
Michael Natterer committed
391 392 393 394
  GimpImage *image = NULL;
  gchar     *str;
  gint       pid;
  gint       ID;
395 396 397 398

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
  g_return_val_if_fail (selection != NULL, NULL);

Michael Natterer's avatar
Michael Natterer committed
399 400 401 402 403 404
  str = gimp_selection_data_get_name (selection);
  if (! str)
    return NULL;

  if (sscanf (str, "%i:%i", &pid, &ID) == 2 &&
      pid == getpid ())
405
    {
Michael Natterer's avatar
Michael Natterer committed
406
      image = gimp_image_get_by_ID (gimp, ID);
407 408
    }

Michael Natterer's avatar
Michael Natterer committed
409
  g_free (str);
410

Michael Natterer's avatar
Michael Natterer committed
411
  return image;
412 413
}

414 415 416 417 418
void
gimp_selection_data_set_component (GtkSelectionData *selection,
                                   GimpImage        *gimage,
                                   GimpChannelType   channel)
{
Michael Natterer's avatar
Michael Natterer committed
419
  gchar *str;
420 421 422 423

  g_return_if_fail (selection != NULL);
  g_return_if_fail (GIMP_IS_IMAGE (gimage));

Michael Natterer's avatar
Michael Natterer committed
424 425
  str = g_strdup_printf ("%d:%d:%d", getpid (), gimp_image_get_ID (gimage),
                         (gint) channel);
426

427
  gtk_selection_data_set (selection, selection->target,
Michael Natterer's avatar
Michael Natterer committed
428
                          8, (guchar *) str, strlen (str) + 1);
429

Michael Natterer's avatar
Michael Natterer committed
430
  g_free (str);
431 432 433 434 435 436 437
}

GimpImage *
gimp_selection_data_get_component (GtkSelectionData *selection,
                                   Gimp             *gimp,
                                   GimpChannelType  *channel)
{
Michael Natterer's avatar
Michael Natterer committed
438 439 440 441 442
  GimpImage *image = NULL;
  gchar     *str;
  gint       pid;
  gint       ID;
  gint       ch;
443 444 445 446

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
  g_return_val_if_fail (selection != NULL, NULL);

Michael Natterer's avatar
Michael Natterer committed
447 448
  if (channel)
    *channel = 0;
449

Michael Natterer's avatar
Michael Natterer committed
450 451 452
  str = gimp_selection_data_get_name (selection);
  if (! str)
    return NULL;
453

Michael Natterer's avatar
Michael Natterer committed
454 455
  if (sscanf (str, "%i:%i:%i", &pid, &ID, &ch) == 3 &&
      pid == getpid ())
456
    {
Michael Natterer's avatar
Michael Natterer committed
457
      image = gimp_image_get_by_ID (gimp, ID);
458

Michael Natterer's avatar
Michael Natterer committed
459 460 461
      if (image && channel)
        *channel = ch;
    }
462

Michael Natterer's avatar
Michael Natterer committed
463
  g_free (str);
464

Michael Natterer's avatar
Michael Natterer committed
465
  return image;
466 467
}

468 469 470 471
void
gimp_selection_data_set_item (GtkSelectionData *selection,
                              GimpItem         *item)
{
Michael Natterer's avatar
Michael Natterer committed
472
  gchar *str;
473 474 475 476

  g_return_if_fail (selection != NULL);
  g_return_if_fail (GIMP_IS_ITEM (item));

Michael Natterer's avatar
Michael Natterer committed
477
  str = g_strdup_printf ("%d:%d", getpid (), gimp_item_get_ID (item));
478

479
  gtk_selection_data_set (selection, selection->target,
Michael Natterer's avatar
Michael Natterer committed
480
                          8, (guchar *) str, strlen (str) + 1);
481

Michael Natterer's avatar
Michael Natterer committed
482
  g_free (str);
483 484 485 486 487 488
}

GimpItem *
gimp_selection_data_get_item (GtkSelectionData *selection,
                              Gimp             *gimp)
{
Michael Natterer's avatar
Michael Natterer committed
489 490 491 492
  GimpItem *item = NULL;
  gchar    *str;
  gint      pid;
  gint      ID;
493 494 495 496

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
  g_return_val_if_fail (selection != NULL, NULL);

Michael Natterer's avatar
Michael Natterer committed
497 498 499 500 501 502
  str = gimp_selection_data_get_name (selection);
  if (! str)
    return NULL;

  if (sscanf (str, "%i:%i", &pid, &ID) == 2 &&
      pid == getpid ())
503
    {
Michael Natterer's avatar
Michael Natterer committed
504
      item = gimp_item_get_by_ID (gimp, ID);
505 506
    }

Michael Natterer's avatar
Michael Natterer committed
507
  g_free (str);
508

Michael Natterer's avatar
Michael Natterer committed
509
  return item;
510 511 512
}

void
Michael Natterer's avatar
Michael Natterer committed
513 514
gimp_selection_data_set_object (GtkSelectionData *selection,
                                GimpObject       *object)
515 516 517 518
{
  const gchar *name;

  g_return_if_fail (selection != NULL);
Michael Natterer's avatar
Michael Natterer committed
519
  g_return_if_fail (GIMP_IS_OBJECT (object));
520

Michael Natterer's avatar
Michael Natterer committed
521
  name = gimp_object_get_name (object);
522 523

  if (name)
524
    {
Michael Natterer's avatar
Michael Natterer committed
525
      gchar *str;
526

Michael Natterer's avatar
Michael Natterer committed
527 528 529 530 531
      str = g_strdup_printf ("%d:%p:%s", getpid (), object, name);
      gtk_selection_data_set (selection, selection->target,
                              8, (guchar *) str, strlen (str) + 1);
      g_free (str);
    }
532 533
}

534 535 536 537 538 539 540
GimpBrush *
gimp_selection_data_get_brush (GtkSelectionData *selection,
                               Gimp             *gimp)
{
  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
  g_return_val_if_fail (selection != NULL, NULL);

Michael Natterer's avatar
Michael Natterer committed
541 542 543 544
  return (GimpBrush *)
    gimp_selection_data_get_object (selection,
                                    gimp->brush_factory->container,
                                    GIMP_OBJECT (gimp_brush_get_standard ()));
545 546 547 548 549 550 551 552 553
}

GimpPattern *
gimp_selection_data_get_pattern (GtkSelectionData *selection,
                                 Gimp             *gimp)
{
  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
  g_return_val_if_fail (selection != NULL, NULL);

Michael Natterer's avatar
Michael Natterer committed
554 555 556 557
  return (GimpPattern *)
    gimp_selection_data_get_object (selection,
                                    gimp->pattern_factory->container,
                                    GIMP_OBJECT (gimp_pattern_get_standard ()));
558 559 560 561 562 563 564 565 566
}

GimpGradient *
gimp_selection_data_get_gradient (GtkSelectionData *selection,
                                  Gimp             *gimp)
{
  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
  g_return_val_if_fail (selection != NULL, NULL);

Michael Natterer's avatar
Michael Natterer committed
567 568 569 570
  return (GimpGradient *)
    gimp_selection_data_get_object (selection,
                                    gimp->gradient_factory->container,
                                    GIMP_OBJECT (gimp_gradient_get_standard ()));
571 572 573 574 575 576 577 578 579
}

GimpPalette *
gimp_selection_data_get_palette (GtkSelectionData *selection,
                                 Gimp             *gimp)
{
  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
  g_return_val_if_fail (selection != NULL, NULL);

Michael Natterer's avatar
Michael Natterer committed
580 581 582 583
  return (GimpPalette *)
    gimp_selection_data_get_object (selection,
                                    gimp->palette_factory->container,
                                    GIMP_OBJECT (gimp_palette_get_standard ()));
584 585 586 587 588 589
}

GimpFont *
gimp_selection_data_get_font (GtkSelectionData *selection,
                              Gimp             *gimp)
{
Michael Natterer's avatar
Michael Natterer committed
590 591
  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
  g_return_val_if_fail (selection != NULL, NULL);
592

Michael Natterer's avatar
Michael Natterer committed
593 594 595 596 597 598 599 600 601 602
  return (GimpFont *)
    gimp_selection_data_get_object (selection,
                                    gimp->fonts,
                                    GIMP_OBJECT (gimp_font_get_standard ()));
}

GimpBuffer *
gimp_selection_data_get_buffer (GtkSelectionData *selection,
                                Gimp             *gimp)
{
603 604 605
  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
  g_return_val_if_fail (selection != NULL, NULL);

Michael Natterer's avatar
Michael Natterer committed
606 607 608 609 610
  return (GimpBuffer *)
    gimp_selection_data_get_object (selection,
                                    gimp->named_buffers,
                                    GIMP_OBJECT (gimp->global_buffer));
}
611

Michael Natterer's avatar
Michael Natterer committed
612 613 614 615 616 617
GimpImagefile *
gimp_selection_data_get_imagefile (GtkSelectionData *selection,
                                   Gimp             *gimp)
{
  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
  g_return_val_if_fail (selection != NULL, NULL);
618

Michael Natterer's avatar
Michael Natterer committed
619 620 621 622
  return (GimpImagefile *) gimp_selection_data_get_object (selection,
                                                           gimp->documents,
                                                           NULL);
}
623

Michael Natterer's avatar
Michael Natterer committed
624 625 626 627 628 629
GimpTemplate *
gimp_selection_data_get_template (GtkSelectionData *selection,
                                  Gimp             *gimp)
{
  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
  g_return_val_if_fail (selection != NULL, NULL);
630

Michael Natterer's avatar
Michael Natterer committed
631 632 633
  return (GimpTemplate *) gimp_selection_data_get_object (selection,
                                                          gimp->templates,
                                                          NULL);
634 635
}

Michael Natterer's avatar
Michael Natterer committed
636 637 638
GimpToolInfo *
gimp_selection_data_get_tool_info (GtkSelectionData *selection,
                                   Gimp             *gimp)
639
{
Michael Natterer's avatar
Michael Natterer committed
640 641
  GimpToolInfo *tool_info;
  gchar        *name;
642 643 644 645

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
  g_return_val_if_fail (selection != NULL, NULL);

646 647 648
  name = gimp_selection_data_get_name (selection);
  if (! name)
    return NULL;
649

Michael Natterer's avatar
Michael Natterer committed
650 651
  if (strcmp (name, "gimp-standard-tool") == 0)
    tool_info = gimp_tool_info_get_standard (gimp);
652
  else
Michael Natterer's avatar
Michael Natterer committed
653 654
    tool_info = (GimpToolInfo *)
      gimp_container_get_child_by_name (gimp->tool_info_list, name);
655

656 657
  g_free (name);

Michael Natterer's avatar
Michael Natterer committed
658
  return tool_info;
659 660 661
}


Michael Natterer's avatar
Michael Natterer committed
662 663 664 665 666 667
/*  private functions  */

static gchar *
gimp_selection_data_get_name (GtkSelectionData *selection)
{
  gchar *name;
668 669 670

  if ((selection->format != 8) || (selection->length < 1))
    {
Michael Natterer's avatar
Michael Natterer committed
671
      g_warning ("Received invalid selection data");
672 673 674
      return NULL;
    }

Sven Neumann's avatar
Sven Neumann committed
675
  name = g_strndup ((const gchar *) selection->data, selection->length);
676

Michael Natterer's avatar
Michael Natterer committed
677 678 679 680 681 682 683
  if (! g_utf8_validate (name, -1, NULL))
    {
      g_warning ("Received invalid selection data "
                 "(doesn't validate as UTF-8)!");
      g_free (name);
      return NULL;
    }
684

Michael Natterer's avatar
Michael Natterer committed
685
  g_printerr ("%s: name = '%s'\n", G_STRFUNC, name);
686

Michael Natterer's avatar
Michael Natterer committed
687
  return name;
688 689
}

Michael Natterer's avatar
Michael Natterer committed
690 691 692 693
static GimpObject *
gimp_selection_data_get_object (GtkSelectionData *selection,
                                GimpContainer    *container,
                                GimpObject       *additional)
694
{
Michael Natterer's avatar
Michael Natterer committed
695 696 697 698 699 700 701 702 703
  GimpObject *object = NULL;
  gchar      *str;
  gint        pid;
  gpointer    object_addr;
  gint        name_offset = 0;

  str = gimp_selection_data_get_name (selection);
  if (! str)
    return NULL;
704

Michael Natterer's avatar
Michael Natterer committed
705 706
  if (sscanf (str, "%i:%p:%n", &pid, &object_addr, &name_offset) >= 2 &&
      pid == getpid () && name_offset > 0)
707
    {
Michael Natterer's avatar
Michael Natterer committed
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
      gchar *name = str + name_offset;

      g_printerr ("%s: pid = %d, addr = %p, name = '%s'\n",
                  G_STRFUNC, pid, object_addr, name);

      if (additional &&
          strcmp (name, gimp_object_get_name (additional)) == 0 &&
          object_addr == (gpointer) additional)
        {
          object = additional;
        }
      else
        {
          object = gimp_container_get_child_by_name (container, name);

          if (object && object_addr != (gpointer) object)
            object = NULL;
        }
726 727
    }

Michael Natterer's avatar
Michael Natterer committed
728
  g_free (str);
729

Michael Natterer's avatar
Michael Natterer committed
730 731
  return object;
}
732

Michael Natterer's avatar
Michael Natterer committed
733 734 735 736 737 738 739 740 741
/*  the next two functions are straight cut'n'paste from glib/glib/gconvert.c,
 *  except that gimp_unescape_uri_string() does not try to UTF-8 validate
 *  the unescaped result.
 */
static int
unescape_character (const char *scanner)
{
  int first_digit;
  int second_digit;
742

Michael Natterer's avatar
Michael Natterer committed
743 744 745 746 747 748 749 750 751
  first_digit = g_ascii_xdigit_value (scanner[0]);
  if (first_digit < 0)
    return -1;

  second_digit = g_ascii_xdigit_value (scanner[1]);
  if (second_digit < 0)
    return -1;

  return (first_digit << 4) | second_digit;
752 753
}

Michael Natterer's avatar
Michael Natterer committed
754 755 756 757 758
static gchar *
gimp_unescape_uri_string (const char *escaped,
                          int         len,
                          const char *illegal_escaped_characters,
                          gboolean    ascii_must_not_be_escaped)
759
{
Michael Natterer's avatar
Michael Natterer committed
760 761 762
  const gchar *in, *in_end;
  gchar *out, *result;
  int c;
763

Michael Natterer's avatar
Michael Natterer committed
764 765
  if (escaped == NULL)
    return NULL;
766

Michael Natterer's avatar
Michael Natterer committed
767 768 769 770 771 772 773
  if (len < 0)
    len = strlen (escaped);

  result = g_malloc (len + 1);

  out = result;
  for (in = escaped, in_end = escaped + len; in < in_end; in++)
774
    {
Michael Natterer's avatar
Michael Natterer committed
775
      c = *in;
776

Michael Natterer's avatar
Michael Natterer committed
777 778 779 780 781
      if (c == '%')
        {
          /* catch partial escape sequences past the end of the substring */
          if (in + 3 > in_end)
            break;
782

Michael Natterer's avatar
Michael Natterer committed
783
          c = unescape_character (in + 1);
784

Michael Natterer's avatar
Michael Natterer committed
785 786 787
          /* catch bad escape sequences and NUL characters */
          if (c <= 0)
            break;
788

Michael Natterer's avatar
Michael Natterer committed
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
          /* catch escaped ASCII */
          if (ascii_must_not_be_escaped && c <= 0x7F)
            break;

          /* catch other illegal escaped characters */
          if (strchr (illegal_escaped_characters, c) != NULL)
            break;

          in += 2;
        }

      *out++ = c;
    }

  g_assert (out - result <= len);
  *out = '\0';

  if (in != in_end)
    {
      g_free (result);
      return NULL;
    }

  return result;
813
}