gimppdb-utils.c 14.3 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 84 85
                   _("Brush '%s' not found"), name);
    }
  else if (writable && ! GIMP_DATA (brush)->writable)
    {
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 173 174
                   _("Gradient '%s' not found"), name);
    }
  else if (writable && ! GIMP_DATA (gradient)->writable)
    {
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 208 209
                   _("Palette '%s' not found"), name);
    }
  else if (writable && ! GIMP_DATA (palette)->writable)
    {
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
                           gboolean   writable,
308 309 310 311 312
                           GError   **error)
{
  g_return_val_if_fail (GIMP_IS_ITEM (item), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

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

323 324 325
  if (writable)
    return gimp_pdb_item_is_writable (item, error);

326
  return TRUE;
327 328 329 330
}

gboolean
gimp_pdb_item_is_floating (GimpItem  *item,
331
                           GimpImage *dest_image,
332 333 334
                           GError   **error)
{
  g_return_val_if_fail (GIMP_IS_ITEM (item), FALSE);
335
  g_return_val_if_fail (GIMP_IS_IMAGE (dest_image), FALSE);
336 337
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

338 339 340 341
  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"),
342
                   gimp_object_get_name (item),
343 344 345 346 347 348 349
                   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"),
350
                   gimp_object_get_name (item),
351 352 353
                   gimp_item_get_ID (item));
      return FALSE;
    }
354

355
  return TRUE;
356
}
357

358 359 360 361 362 363 364
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);

365
  if (gimp_item_is_content_locked (item))
366 367 368 369
    {
      g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
                   _("Item '%s' (%d) cannot be modified because its "
                     "contents are locked"),
370
                   gimp_object_get_name (item),
371 372 373 374 375 376 377
                   gimp_item_get_ID (item));
      return FALSE;
    }

  return TRUE;
}

378 379 380 381 382 383 384 385 386 387 388 389
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"),
390
                   gimp_object_get_name (item),
391 392 393 394 395 396 397
                   gimp_item_get_ID (item));
      return FALSE;
    }

  return TRUE;
}

398 399
gboolean
gimp_pdb_layer_is_text_layer (GimpLayer  *layer,
400
                              gboolean    writable,
401 402 403 404 405 406 407
                              GError    **error)
{
  g_return_val_if_fail (GIMP_IS_LAYER (layer), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  if (! gimp_drawable_is_text_layer (GIMP_DRAWABLE (layer)))
    {
408
      g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_INVALID_ARGUMENT,
409 410
                   _("Layer '%s' (%d) can not be used because it is not "
                     "a text layer"),
411
                   gimp_object_get_name (layer),
412 413 414 415 416
                   gimp_item_get_ID (GIMP_ITEM (layer)));

      return FALSE;
    }

417
  return gimp_pdb_item_is_attached (GIMP_ITEM (layer), writable, error);
418 419
}

420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
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
450 451
               _("Image '%s' (%d) is of type '%s', "
                 "but an image of type '%s' is expected"),
452
               gimp_image_get_display_name (image),
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
               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
473
               _("Image '%s' (%d) is already of type '%s'"),
474
               gimp_image_get_display_name (image),
475 476 477 478 479
               gimp_image_get_ID (image),
               gimp_pdb_enum_value_get_nick (GIMP_TYPE_IMAGE_BASE_TYPE, type));

  return FALSE;
}
480

481 482 483
GimpStroke *
gimp_pdb_get_vectors_stroke (GimpVectors  *vectors,
                             gint          stroke_ID,
484
                             gboolean      writable,
485 486
                             GError      **error)
{
487
  GimpStroke *stroke = NULL;
488 489 490 491

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

492 493 494
  if (! gimp_pdb_item_is_not_group (GIMP_ITEM (vectors), error))
    return NULL;

495
  if (! writable || gimp_pdb_item_is_writable (GIMP_ITEM (vectors), error))
496
    {
497 498 499 500 501 502
      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);
503 504 505 506
    }

  return stroke;
}