gimppdb-utils.c 18.2 KB
Newer Older
1 2 3
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995-2003 Spencer Kimball and Peter Mattis
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7 8 9 10 11 12 13 14
 * (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
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21
 */

#include "config.h"

#include <string.h>

22
#include <gegl.h>
23 24 25 26 27 28 29

#include "pdb-types.h"

#include "core/gimp.h"
#include "core/gimpbrushgenerated.h"
#include "core/gimpcontainer.h"
#include "core/gimpdatafactory.h"
30
#include "core/gimpdrawable.h"
31
#include "core/gimpimage.h"
32
#include "core/gimpitem.h"
33

34 35
#include "text/gimptextlayer.h"

36 37
#include "vectors/gimpvectors.h"

38 39 40 41 42 43
#include "gimppdb-utils.h"
#include "gimppdberror.h"

#include "gimp-intl.h"


44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
static GimpObject *
gimp_pdb_get_data_factory_item (GimpDataFactory *data_factory,
                                const gchar     *name)
{
  GimpObject *gimp_object;

  gimp_object = gimp_container_get_child_by_name (gimp_data_factory_get_container (data_factory), name);

  if (! gimp_object)
    gimp_object = gimp_container_get_child_by_name (gimp_data_factory_get_container_obsolete (data_factory), name);

  return gimp_object;
}


59 60 61 62 63 64 65 66 67 68 69 70 71
GimpBrush *
gimp_pdb_get_brush (Gimp         *gimp,
                    const gchar  *name,
                    gboolean      writable,
                    GError      **error)
{
  GimpBrush *brush;

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  if (! name || ! strlen (name))
    {
72 73
      g_set_error_literal (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
			   _("Invalid empty brush name"));
74 75 76
      return NULL;
    }

77
  brush = (GimpBrush *) gimp_pdb_get_data_factory_item (gimp->brush_factory, name);
78 79 80

  if (! brush)
    {
81
      g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
82 83
                   _("Brush '%s' not found"), name);
    }
84
  else if (writable && ! gimp_data_is_writable (GIMP_DATA (brush)))
85
    {
86
      g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
                   _("Brush '%s' is not editable"), name);
      return NULL;
    }

  return brush;
}

GimpBrush *
gimp_pdb_get_generated_brush (Gimp         *gimp,
                              const gchar  *name,
                              gboolean      writable,
                              GError      **error)
{
  GimpBrush *brush;

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  brush = gimp_pdb_get_brush (gimp, name, writable, error);

  if (! brush)
    return NULL;

  if (! GIMP_IS_BRUSH_GENERATED (brush))
    {
112
      g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
                   _("Brush '%s' is not a generated brush"), name);
      return NULL;
    }

  return brush;
}

GimpPattern *
gimp_pdb_get_pattern (Gimp         *gimp,
                      const gchar  *name,
                      GError      **error)
{
  GimpPattern *pattern;

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  if (! name || ! strlen (name))
    {
132 133
      g_set_error_literal (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
			   _("Invalid empty pattern name"));
134 135 136
      return NULL;
    }

137
  pattern = (GimpPattern *) gimp_pdb_get_data_factory_item (gimp->pattern_factory, name);
138 139 140

  if (! pattern)
    {
141
      g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
                   _("Pattern '%s' not found"), name);
    }

  return pattern;
}

GimpGradient *
gimp_pdb_get_gradient (Gimp         *gimp,
                       const gchar  *name,
                       gboolean      writable,
                       GError      **error)
{
  GimpGradient *gradient;

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  if (! name || ! strlen (name))
    {
161 162
      g_set_error_literal (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
			   _("Invalid empty gradient name"));
163 164 165
      return NULL;
    }

166
  gradient = (GimpGradient *) gimp_pdb_get_data_factory_item (gimp->gradient_factory, name);
167 168 169

  if (! gradient)
    {
170
      g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
171 172
                   _("Gradient '%s' not found"), name);
    }
173
  else if (writable && ! gimp_data_is_writable (GIMP_DATA (gradient)))
174
    {
175
      g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
                   _("Gradient '%s' is not editable"), name);
      return NULL;
    }

  return gradient;
}

GimpPalette *
gimp_pdb_get_palette (Gimp         *gimp,
                      const gchar  *name,
                      gboolean      writable,
                      GError      **error)
{
  GimpPalette *palette;

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  if (! name || ! strlen (name))
    {
196 197
      g_set_error_literal (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
			   _("Invalid empty palette name"));
198 199 200
      return NULL;
    }

201
  palette = (GimpPalette *) gimp_pdb_get_data_factory_item (gimp->palette_factory, name);
202 203 204

  if (! palette)
    {
205
      g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
206 207
                   _("Palette '%s' not found"), name);
    }
208
  else if (writable && ! gimp_data_is_writable (GIMP_DATA (palette)))
209
    {
210
      g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
                   _("Palette '%s' is not editable"), name);
      return NULL;
    }

  return palette;
}

GimpFont *
gimp_pdb_get_font (Gimp         *gimp,
                   const gchar  *name,
                   GError      **error)
{
  GimpFont *font;

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  if (! name || ! strlen (name))
    {
230 231
      g_set_error_literal (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
			   _("Invalid empty font name"));
232 233 234 235 236 237 238 239
      return NULL;
    }

  font = (GimpFont *)
    gimp_container_get_child_by_name (gimp->fonts, name);

  if (! font)
    {
240
      g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
                   _("Font '%s' not found"), name);
    }

  return font;
}

GimpBuffer *
gimp_pdb_get_buffer (Gimp         *gimp,
                     const gchar  *name,
                     GError      **error)
{
  GimpBuffer *buffer;

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  if (! name || ! strlen (name))
    {
259 260
      g_set_error_literal (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
			   _("Invalid empty buffer name"));
261 262 263 264 265 266 267 268
      return NULL;
    }

  buffer = (GimpBuffer *)
    gimp_container_get_child_by_name (gimp->named_buffers, name);

  if (! buffer)
    {
269
      g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
                   _("Named buffer '%s' not found"), name);
    }

  return buffer;
}

GimpPaintInfo *
gimp_pdb_get_paint_info (Gimp         *gimp,
                         const gchar  *name,
                         GError      **error)
{
  GimpPaintInfo *paint_info;

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  if (! name || ! strlen (name))
    {
288 289
      g_set_error_literal (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
			   _("Invalid empty paint method name"));
290 291 292 293 294 295 296 297
      return NULL;
    }

  paint_info = (GimpPaintInfo *)
    gimp_container_get_child_by_name (gimp->paint_info_list, name);

  if (! paint_info)
    {
298
      g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
299 300 301 302 303
                   _("Paint method '%s' does not exist"), name);
    }

  return paint_info;
}
304 305 306

gboolean
gimp_pdb_item_is_attached (GimpItem  *item,
307
                           GimpImage *image,
308
                           gboolean   writable,
309 310 311
                           GError   **error)
{
  g_return_val_if_fail (GIMP_IS_ITEM (item), FALSE);
312
  g_return_val_if_fail (image == NULL || GIMP_IS_IMAGE (image), FALSE);
313 314
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

315 316 317
  if (! gimp_item_is_attached (item))
    {
      g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
318
                   _("Item '%s' (%d) cannot be used because it has not "
319
                     "been added to an image"),
320
                   gimp_object_get_name (item),
321 322 323
                   gimp_item_get_ID (item));
      return FALSE;
    }
324

325 326 327
  if (image && image != gimp_item_get_image (item))
    {
      g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
328
                   _("Item '%s' (%d) cannot be used because it is "
329 330 331 332 333 334
                     "attached to another image"),
                   gimp_object_get_name (item),
                   gimp_item_get_ID (item));
      return FALSE;
    }

335 336 337
  if (writable)
    return gimp_pdb_item_is_writable (item, error);

338
  return TRUE;
339 340
}

341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
gboolean
gimp_pdb_item_is_in_tree (GimpItem   *item,
                          GimpImage  *image,
                          gboolean    writable,
                          GError    **error)
{
  g_return_val_if_fail (GIMP_IS_ITEM (item), FALSE);
  g_return_val_if_fail (image == NULL || GIMP_IS_IMAGE (image), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  if (! gimp_pdb_item_is_attached (item, image, writable, error))
    return FALSE;

  if (! gimp_item_get_tree (item))
    {
      g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
357
                   _("Item '%s' (%d) cannot be used because it is not "
358 359 360 361 362 363 364 365 366
                     "a direct child of an item tree"),
                   gimp_object_get_name (item),
                   gimp_item_get_ID (item));
      return FALSE;
    }

  return TRUE;
}

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
gboolean
gimp_pdb_item_is_in_same_tree (GimpItem   *item,
                               GimpItem   *item2,
                               GimpImage  *image,
                               GError    **error)
{
  g_return_val_if_fail (GIMP_IS_ITEM (item), FALSE);
  g_return_val_if_fail (GIMP_IS_ITEM (item2), FALSE);
  g_return_val_if_fail (image == NULL || GIMP_IS_IMAGE (image), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  if (! gimp_pdb_item_is_in_tree (item, image, FALSE, error) ||
      ! gimp_pdb_item_is_in_tree (item2, image, FALSE, error))
    return FALSE;

  if (gimp_item_get_tree (item) != gimp_item_get_tree (item2))
    {
      g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
                   _("Items '%s' (%d) and '%s' (%d) cannot be used "
                     "because they are not part of the same item tree"),
                   gimp_object_get_name (item),
                   gimp_item_get_ID (item),
                   gimp_object_get_name (item2),
                   gimp_item_get_ID (item2));
      return FALSE;
    }

  return TRUE;
}

gboolean
gimp_pdb_item_is_not_ancestor (GimpItem  *item,
                               GimpItem  *not_descendant,
                               GError   **error)
{
  g_return_val_if_fail (GIMP_IS_ITEM (item), FALSE);
  g_return_val_if_fail (GIMP_IS_ITEM (not_descendant), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  if (gimp_viewable_is_ancestor (GIMP_VIEWABLE (item),
                                 GIMP_VIEWABLE (not_descendant)))
    {
      g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
                   _("Item '%s' (%d) must not be an ancestor of "
                     "'%s' (%d)"),
                   gimp_object_get_name (item),
                   gimp_item_get_ID (item),
                   gimp_object_get_name (not_descendant),
                   gimp_item_get_ID (not_descendant));
      return FALSE;
    }

  return TRUE;
}

422 423
gboolean
gimp_pdb_item_is_floating (GimpItem  *item,
424
                           GimpImage *dest_image,
425 426 427
                           GError   **error)
{
  g_return_val_if_fail (GIMP_IS_ITEM (item), FALSE);
428
  g_return_val_if_fail (GIMP_IS_IMAGE (dest_image), FALSE);
429 430
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

431 432 433 434
  if (! g_object_is_floating (item))
    {
      g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
                   _("Item '%s' (%d) has already been added to an image"),
435
                   gimp_object_get_name (item),
436 437 438 439 440 441 442
                   gimp_item_get_ID (item));
      return FALSE;
    }
  else if (gimp_item_get_image (item) != dest_image)
    {
      g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
                   _("Trying to add item '%s' (%d) to wrong image"),
443
                   gimp_object_get_name (item),
444 445 446
                   gimp_item_get_ID (item));
      return FALSE;
    }
447

448
  return TRUE;
449
}
450

451 452 453 454 455 456 457
gboolean
gimp_pdb_item_is_writable (GimpItem  *item,
                           GError   **error)
{
  g_return_val_if_fail (GIMP_IS_ITEM (item), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

458
  if (gimp_item_is_content_locked (item))
459 460 461 462
    {
      g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
                   _("Item '%s' (%d) cannot be modified because its "
                     "contents are locked"),
463
                   gimp_object_get_name (item),
464 465 466 467 468 469 470
                   gimp_item_get_ID (item));
      return FALSE;
    }

  return TRUE;
}

471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
gboolean
gimp_pdb_item_is_group (GimpItem  *item,
                        GError   **error)
{
  g_return_val_if_fail (GIMP_IS_ITEM (item), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  if (! gimp_viewable_get_children (GIMP_VIEWABLE (item)))
    {
      g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
                   _("Item '%s' (%d) cannot be used because it is "
                     "not a group item"),
                   gimp_object_get_name (item),
                   gimp_item_get_ID (item));
      return FALSE;
    }

  return TRUE;
}

491 492 493 494 495 496 497 498 499 500 501 502
gboolean
gimp_pdb_item_is_not_group (GimpItem  *item,
                            GError   **error)
{
  g_return_val_if_fail (GIMP_IS_ITEM (item), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  if (gimp_viewable_get_children (GIMP_VIEWABLE (item)))
    {
      g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
                   _("Item '%s' (%d) cannot be modified because it "
                     "is a group item"),
503
                   gimp_object_get_name (item),
504 505 506 507 508 509 510
                   gimp_item_get_ID (item));
      return FALSE;
    }

  return TRUE;
}

511 512
gboolean
gimp_pdb_layer_is_text_layer (GimpLayer  *layer,
513
                              gboolean    writable,
514 515 516 517 518
                              GError    **error)
{
  g_return_val_if_fail (GIMP_IS_LAYER (layer), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

519
  if (! gimp_item_is_text_layer (GIMP_DRAWABLE (layer)))
520
    {
521
      g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
522
                   _("Layer '%s' (%d) cannot be used because it is not "
523
                     "a text layer"),
524
                   gimp_object_get_name (layer),
525 526 527 528 529
                   gimp_item_get_ID (GIMP_ITEM (layer)));

      return FALSE;
    }

530
  return gimp_pdb_item_is_attached (GIMP_ITEM (layer), NULL, writable, error);
531 532
}

533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
static const gchar *
gimp_pdb_enum_value_get_nick (GType enum_type,
                              gint  value)
{
  GEnumClass  *enum_class;
  GEnumValue  *enum_value;
  const gchar *nick;

  enum_class = g_type_class_ref (enum_type);
  enum_value = g_enum_get_value (enum_class, value);

  nick = enum_value->value_nick;

  g_type_class_unref (enum_class);

  return nick;
}

gboolean
gimp_pdb_image_is_base_type (GimpImage          *image,
                             GimpImageBaseType   type,
                             GError            **error)
{
  g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  if (gimp_image_base_type (image) == type)
    return TRUE;

  g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
Sven Neumann's avatar
Sven Neumann committed
563 564
               _("Image '%s' (%d) is of type '%s', "
                 "but an image of type '%s' is expected"),
565
               gimp_image_get_display_name (image),
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
               gimp_image_get_ID (image),
               gimp_pdb_enum_value_get_nick (GIMP_TYPE_IMAGE_BASE_TYPE,
                                             gimp_image_base_type (image)),
               gimp_pdb_enum_value_get_nick (GIMP_TYPE_IMAGE_BASE_TYPE, type));

  return FALSE;
}

gboolean
gimp_pdb_image_is_not_base_type (GimpImage          *image,
                                 GimpImageBaseType   type,
                                 GError            **error)
{
  g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  if (gimp_image_base_type (image) != type)
    return TRUE;

  g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
Sven Neumann's avatar
Sven Neumann committed
586
               _("Image '%s' (%d) is already of type '%s'"),
587
               gimp_image_get_display_name (image),
588 589 590 591 592
               gimp_image_get_ID (image),
               gimp_pdb_enum_value_get_nick (GIMP_TYPE_IMAGE_BASE_TYPE, type));

  return FALSE;
}
593

594 595 596
GimpStroke *
gimp_pdb_get_vectors_stroke (GimpVectors  *vectors,
                             gint          stroke_ID,
597
                             gboolean      writable,
598 599
                             GError      **error)
{
600
  GimpStroke *stroke = NULL;
601 602 603 604

  g_return_val_if_fail (GIMP_IS_VECTORS (vectors), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

605 606 607
  if (! gimp_pdb_item_is_not_group (GIMP_ITEM (vectors), error))
    return NULL;

608
  if (! writable || gimp_pdb_item_is_writable (GIMP_ITEM (vectors), error))
609
    {
610 611 612 613 614 615
      stroke = gimp_vectors_stroke_get_by_ID (vectors, stroke_ID);

      if (! stroke)
        g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
                     _("Vectors object %d does not contain stroke with ID %d"),
                     gimp_item_get_ID (GIMP_ITEM (vectors)), stroke_ID);
616 617 618 619
    }

  return stroke;
}