1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995-2003 Spencer Kimball and Peter Mattis
3  *
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  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16  */
17 
18 #include "config.h"
19 
20 #include <string.h>
21 
22 #include <gdk-pixbuf/gdk-pixbuf.h>
23 #include <gegl.h>
24 
25 #include "pdb-types.h"
26 
27 #include "core/gimp.h"
28 #include "core/gimpbrushgenerated.h"
29 #include "core/gimpchannel.h"
30 #include "core/gimpcontainer.h"
31 #include "core/gimpdatafactory.h"
32 #include "core/gimpdrawable.h"
33 #include "core/gimpimage.h"
34 #include "core/gimpimage-guides.h"
35 #include "core/gimpimage-sample-points.h"
36 #include "core/gimpitem.h"
37 
38 #include "text/gimptextlayer.h"
39 
40 #include "vectors/gimpvectors.h"
41 
42 #include "gimppdb-utils.h"
43 #include "gimppdberror.h"
44 
45 #include "gimp-intl.h"
46 
47 
48 static GimpObject *
gimp_pdb_get_data_factory_item(GimpDataFactory * factory,const gchar * name)49 gimp_pdb_get_data_factory_item (GimpDataFactory *factory,
50                                 const gchar     *name)
51 {
52   GimpObject *object;
53 
54   object = gimp_container_get_child_by_name (gimp_data_factory_get_container (factory), name);
55 
56   if (! object)
57     object = gimp_container_get_child_by_name (gimp_data_factory_get_container_obsolete (factory), name);
58 
59   if (! object && ! strcmp (name, "Standard"))
60     {
61       Gimp *gimp = gimp_data_factory_get_gimp (factory);
62 
63       object = (GimpObject *)
64         gimp_data_factory_data_get_standard (factory,
65                                              gimp_get_user_context (gimp));
66     }
67 
68   return object;
69 }
70 
71 
72 GimpBrush *
gimp_pdb_get_brush(Gimp * gimp,const gchar * name,GimpPDBDataAccess access,GError ** error)73 gimp_pdb_get_brush (Gimp               *gimp,
74                     const gchar        *name,
75                     GimpPDBDataAccess   access,
76                     GError            **error)
77 {
78   GimpBrush *brush;
79 
80   g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
81   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
82 
83   if (! name || ! strlen (name))
84     {
85       g_set_error_literal (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
86                            _("Invalid empty brush name"));
87       return NULL;
88     }
89 
90   brush = (GimpBrush *) gimp_pdb_get_data_factory_item (gimp->brush_factory, name);
91 
92   if (! brush)
93     {
94       g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
95                    _("Brush '%s' not found"), name);
96     }
97   else if ((access & GIMP_PDB_DATA_ACCESS_WRITE) &&
98            ! gimp_data_is_writable (GIMP_DATA (brush)))
99     {
100       g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
101                    _("Brush '%s' is not editable"), name);
102       return NULL;
103     }
104   else if ((access & GIMP_PDB_DATA_ACCESS_RENAME) &&
105            ! gimp_viewable_is_name_editable (GIMP_VIEWABLE (brush)))
106     {
107       g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
108                    _("Brush '%s' is not renamable"), name);
109       return NULL;
110     }
111 
112   return brush;
113 }
114 
115 GimpBrush *
gimp_pdb_get_generated_brush(Gimp * gimp,const gchar * name,GimpPDBDataAccess access,GError ** error)116 gimp_pdb_get_generated_brush (Gimp               *gimp,
117                               const gchar        *name,
118                               GimpPDBDataAccess   access,
119                               GError            **error)
120 {
121   GimpBrush *brush;
122 
123   g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
124   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
125 
126   brush = gimp_pdb_get_brush (gimp, name, access, error);
127 
128   if (! brush)
129     return NULL;
130 
131   if (! GIMP_IS_BRUSH_GENERATED (brush))
132     {
133       g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
134                    _("Brush '%s' is not a generated brush"), name);
135       return NULL;
136     }
137 
138   return brush;
139 }
140 
141 GimpDynamics *
gimp_pdb_get_dynamics(Gimp * gimp,const gchar * name,GimpPDBDataAccess access,GError ** error)142 gimp_pdb_get_dynamics (Gimp               *gimp,
143                        const gchar        *name,
144                        GimpPDBDataAccess   access,
145                        GError            **error)
146 {
147   GimpDynamics *dynamics;
148 
149   g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
150   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
151 
152   if (! name || ! strlen (name))
153     {
154       g_set_error_literal (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
155                            _("Invalid empty paint dynamics name"));
156       return NULL;
157     }
158 
159   dynamics = (GimpDynamics *) gimp_pdb_get_data_factory_item (gimp->dynamics_factory, name);
160 
161   if (! dynamics)
162     {
163       g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
164                    _("Paint dynamics '%s' not found"), name);
165     }
166   else if ((access & GIMP_PDB_DATA_ACCESS_WRITE) &&
167            ! gimp_data_is_writable (GIMP_DATA (dynamics)))
168     {
169       g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
170                    _("Paint dynamics '%s' is not editable"), name);
171       return NULL;
172     }
173   else if ((access & GIMP_PDB_DATA_ACCESS_RENAME) &&
174            ! gimp_viewable_is_name_editable (GIMP_VIEWABLE (dynamics)))
175     {
176       g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
177                    _("Paint dynamics '%s' is not renamable"), name);
178       return NULL;
179     }
180 
181   return dynamics;
182 }
183 
184 GimpMybrush *
gimp_pdb_get_mybrush(Gimp * gimp,const gchar * name,GimpPDBDataAccess access,GError ** error)185 gimp_pdb_get_mybrush (Gimp               *gimp,
186                       const gchar        *name,
187                       GimpPDBDataAccess   access,
188                       GError            **error)
189 {
190   GimpMybrush *brush;
191 
192   g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
193   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
194 
195   if (! name || ! strlen (name))
196     {
197       g_set_error_literal (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
198                            _("Invalid empty MyPaint brush name"));
199       return NULL;
200     }
201 
202   brush = (GimpMybrush *) gimp_pdb_get_data_factory_item (gimp->mybrush_factory, name);
203 
204   if (! brush)
205     {
206       g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
207                    _("MyPaint brush '%s' not found"), name);
208     }
209   else if ((access & GIMP_PDB_DATA_ACCESS_WRITE) &&
210            ! gimp_data_is_writable (GIMP_DATA (brush)))
211     {
212       g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
213                    _("MyPaint brush '%s' is not editable"), name);
214       return NULL;
215     }
216   else if ((access & GIMP_PDB_DATA_ACCESS_RENAME) &&
217            ! gimp_viewable_is_name_editable (GIMP_VIEWABLE (brush)))
218     {
219       g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
220                    _("MyPaint brush '%s' is not renamable"), name);
221       return NULL;
222     }
223 
224   return brush;
225 }
226 
227 GimpPattern *
gimp_pdb_get_pattern(Gimp * gimp,const gchar * name,GError ** error)228 gimp_pdb_get_pattern (Gimp         *gimp,
229                       const gchar  *name,
230                       GError      **error)
231 {
232   GimpPattern *pattern;
233 
234   g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
235   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
236 
237   if (! name || ! strlen (name))
238     {
239       g_set_error_literal (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
240                            _("Invalid empty pattern name"));
241       return NULL;
242     }
243 
244   pattern = (GimpPattern *) gimp_pdb_get_data_factory_item (gimp->pattern_factory, name);
245 
246   if (! pattern)
247     {
248       g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
249                    _("Pattern '%s' not found"), name);
250     }
251 
252   return pattern;
253 }
254 
255 GimpGradient *
gimp_pdb_get_gradient(Gimp * gimp,const gchar * name,GimpPDBDataAccess access,GError ** error)256 gimp_pdb_get_gradient (Gimp               *gimp,
257                        const gchar        *name,
258                        GimpPDBDataAccess   access,
259                        GError            **error)
260 {
261   GimpGradient *gradient;
262 
263   g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
264   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
265 
266   if (! name || ! strlen (name))
267     {
268       g_set_error_literal (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
269                            _("Invalid empty gradient name"));
270       return NULL;
271     }
272 
273   gradient = (GimpGradient *) gimp_pdb_get_data_factory_item (gimp->gradient_factory, name);
274 
275   if (! gradient)
276     {
277       g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
278                    _("Gradient '%s' not found"), name);
279     }
280   else if ((access & GIMP_PDB_DATA_ACCESS_WRITE) &&
281            ! gimp_data_is_writable (GIMP_DATA (gradient)))
282     {
283       g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
284                    _("Gradient '%s' is not editable"), name);
285       return NULL;
286     }
287   else if ((access & GIMP_PDB_DATA_ACCESS_RENAME) &&
288            ! gimp_viewable_is_name_editable (GIMP_VIEWABLE (gradient)))
289     {
290       g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
291                    _("Gradient '%s' is not renamable"), name);
292       return NULL;
293     }
294 
295   return gradient;
296 }
297 
298 GimpPalette *
gimp_pdb_get_palette(Gimp * gimp,const gchar * name,GimpPDBDataAccess access,GError ** error)299 gimp_pdb_get_palette (Gimp               *gimp,
300                       const gchar        *name,
301                       GimpPDBDataAccess   access,
302                       GError            **error)
303 {
304   GimpPalette *palette;
305 
306   g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
307   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
308 
309   if (! name || ! strlen (name))
310     {
311       g_set_error_literal (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
312                            _("Invalid empty palette name"));
313       return NULL;
314     }
315 
316   palette = (GimpPalette *) gimp_pdb_get_data_factory_item (gimp->palette_factory, name);
317 
318   if (! palette)
319     {
320       g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
321                    _("Palette '%s' not found"), name);
322     }
323   else if ((access & GIMP_PDB_DATA_ACCESS_WRITE) &&
324            ! gimp_data_is_writable (GIMP_DATA (palette)))
325     {
326       g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
327                    _("Palette '%s' is not editable"), name);
328       return NULL;
329     }
330   else if ((access & GIMP_PDB_DATA_ACCESS_RENAME) &&
331            ! gimp_viewable_is_name_editable (GIMP_VIEWABLE (palette)))
332     {
333       g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
334                    _("Palette '%s' is not renamable"), name);
335       return NULL;
336     }
337 
338   return palette;
339 }
340 
341 GimpFont *
gimp_pdb_get_font(Gimp * gimp,const gchar * name,GError ** error)342 gimp_pdb_get_font (Gimp         *gimp,
343                    const gchar  *name,
344                    GError      **error)
345 {
346   GimpFont *font;
347 
348   g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
349   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
350 
351   if (! name || ! strlen (name))
352     {
353       g_set_error_literal (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
354                            _("Invalid empty font name"));
355       return NULL;
356     }
357 
358   font = (GimpFont *) gimp_pdb_get_data_factory_item (gimp->font_factory, name);
359 
360   if (! font)
361     {
362       g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
363                    _("Font '%s' not found"), name);
364     }
365 
366   return font;
367 }
368 
369 GimpBuffer *
gimp_pdb_get_buffer(Gimp * gimp,const gchar * name,GError ** error)370 gimp_pdb_get_buffer (Gimp         *gimp,
371                      const gchar  *name,
372                      GError      **error)
373 {
374   GimpBuffer *buffer;
375 
376   g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
377   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
378 
379   if (! name || ! strlen (name))
380     {
381       g_set_error_literal (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
382                            _("Invalid empty buffer name"));
383       return NULL;
384     }
385 
386   buffer = (GimpBuffer *)
387     gimp_container_get_child_by_name (gimp->named_buffers, name);
388 
389   if (! buffer)
390     {
391       g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
392                    _("Named buffer '%s' not found"), name);
393     }
394 
395   return buffer;
396 }
397 
398 GimpPaintInfo *
gimp_pdb_get_paint_info(Gimp * gimp,const gchar * name,GError ** error)399 gimp_pdb_get_paint_info (Gimp         *gimp,
400                          const gchar  *name,
401                          GError      **error)
402 {
403   GimpPaintInfo *paint_info;
404 
405   g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
406   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
407 
408   if (! name || ! strlen (name))
409     {
410       g_set_error_literal (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
411                            _("Invalid empty paint method name"));
412       return NULL;
413     }
414 
415   paint_info = (GimpPaintInfo *)
416     gimp_container_get_child_by_name (gimp->paint_info_list, name);
417 
418   if (! paint_info)
419     {
420       g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
421                    _("Paint method '%s' does not exist"), name);
422     }
423 
424   return paint_info;
425 }
426 
427 gboolean
gimp_pdb_item_is_attached(GimpItem * item,GimpImage * image,GimpPDBItemModify modify,GError ** error)428 gimp_pdb_item_is_attached (GimpItem           *item,
429                            GimpImage          *image,
430                            GimpPDBItemModify   modify,
431                            GError            **error)
432 {
433   g_return_val_if_fail (GIMP_IS_ITEM (item), FALSE);
434   g_return_val_if_fail (image == NULL || GIMP_IS_IMAGE (image), FALSE);
435   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
436 
437   if (! gimp_item_is_attached (item))
438     {
439       g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
440                    _("Item '%s' (%d) cannot be used because it has not "
441                      "been added to an image"),
442                    gimp_object_get_name (item),
443                    gimp_item_get_ID (item));
444       return FALSE;
445     }
446 
447   if (image && image != gimp_item_get_image (item))
448     {
449       g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
450                    _("Item '%s' (%d) cannot be used because it is "
451                      "attached to another image"),
452                    gimp_object_get_name (item),
453                    gimp_item_get_ID (item));
454       return FALSE;
455     }
456 
457   return gimp_pdb_item_is_modifiable (item, modify, error);
458 }
459 
460 gboolean
gimp_pdb_item_is_in_tree(GimpItem * item,GimpImage * image,GimpPDBItemModify modify,GError ** error)461 gimp_pdb_item_is_in_tree (GimpItem           *item,
462                           GimpImage          *image,
463                           GimpPDBItemModify   modify,
464                           GError            **error)
465 {
466   g_return_val_if_fail (GIMP_IS_ITEM (item), FALSE);
467   g_return_val_if_fail (image == NULL || GIMP_IS_IMAGE (image), FALSE);
468   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
469 
470   if (! gimp_pdb_item_is_attached (item, image, modify, error))
471     return FALSE;
472 
473   if (! gimp_item_get_tree (item))
474     {
475       g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
476                    _("Item '%s' (%d) cannot be used because it is not "
477                      "a direct child of an item tree"),
478                    gimp_object_get_name (item),
479                    gimp_item_get_ID (item));
480       return FALSE;
481     }
482 
483   return TRUE;
484 }
485 
486 gboolean
gimp_pdb_item_is_in_same_tree(GimpItem * item,GimpItem * item2,GimpImage * image,GError ** error)487 gimp_pdb_item_is_in_same_tree (GimpItem   *item,
488                                GimpItem   *item2,
489                                GimpImage  *image,
490                                GError    **error)
491 {
492   g_return_val_if_fail (GIMP_IS_ITEM (item), FALSE);
493   g_return_val_if_fail (GIMP_IS_ITEM (item2), FALSE);
494   g_return_val_if_fail (image == NULL || GIMP_IS_IMAGE (image), FALSE);
495   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
496 
497   if (! gimp_pdb_item_is_in_tree (item, image, FALSE, error) ||
498       ! gimp_pdb_item_is_in_tree (item2, image, FALSE, error))
499     return FALSE;
500 
501   if (gimp_item_get_tree (item) != gimp_item_get_tree (item2))
502     {
503       g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
504                    _("Items '%s' (%d) and '%s' (%d) cannot be used "
505                      "because they are not part of the same item tree"),
506                    gimp_object_get_name (item),
507                    gimp_item_get_ID (item),
508                    gimp_object_get_name (item2),
509                    gimp_item_get_ID (item2));
510       return FALSE;
511     }
512 
513   return TRUE;
514 }
515 
516 gboolean
gimp_pdb_item_is_not_ancestor(GimpItem * item,GimpItem * not_descendant,GError ** error)517 gimp_pdb_item_is_not_ancestor (GimpItem  *item,
518                                GimpItem  *not_descendant,
519                                GError   **error)
520 {
521   g_return_val_if_fail (GIMP_IS_ITEM (item), FALSE);
522   g_return_val_if_fail (GIMP_IS_ITEM (not_descendant), FALSE);
523   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
524 
525   if (gimp_viewable_is_ancestor (GIMP_VIEWABLE (item),
526                                  GIMP_VIEWABLE (not_descendant)))
527     {
528       g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
529                    _("Item '%s' (%d) must not be an ancestor of "
530                      "'%s' (%d)"),
531                    gimp_object_get_name (item),
532                    gimp_item_get_ID (item),
533                    gimp_object_get_name (not_descendant),
534                    gimp_item_get_ID (not_descendant));
535       return FALSE;
536     }
537 
538   return TRUE;
539 }
540 
541 gboolean
gimp_pdb_item_is_floating(GimpItem * item,GimpImage * dest_image,GError ** error)542 gimp_pdb_item_is_floating (GimpItem  *item,
543                            GimpImage *dest_image,
544                            GError   **error)
545 {
546   g_return_val_if_fail (GIMP_IS_ITEM (item), FALSE);
547   g_return_val_if_fail (GIMP_IS_IMAGE (dest_image), FALSE);
548   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
549 
550   if (! g_object_is_floating (item))
551     {
552       g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
553                    _("Item '%s' (%d) has already been added to an image"),
554                    gimp_object_get_name (item),
555                    gimp_item_get_ID (item));
556       return FALSE;
557     }
558   else if (gimp_item_get_image (item) != dest_image)
559     {
560       g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
561                    _("Trying to add item '%s' (%d) to wrong image"),
562                    gimp_object_get_name (item),
563                    gimp_item_get_ID (item));
564       return FALSE;
565     }
566 
567   return TRUE;
568 }
569 
570 gboolean
gimp_pdb_item_is_modifiable(GimpItem * item,GimpPDBItemModify modify,GError ** error)571 gimp_pdb_item_is_modifiable (GimpItem           *item,
572                              GimpPDBItemModify   modify,
573                              GError            **error)
574 {
575   g_return_val_if_fail (GIMP_IS_ITEM (item), FALSE);
576   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
577 
578   /*  When a channel is position-locked, it is also implicitly
579    *  content-locked because we translate channels by modifying their
580    *  pixels.
581    */
582   if ((modify & GIMP_PDB_ITEM_POSITION) && GIMP_IS_CHANNEL (item))
583     modify |= GIMP_PDB_ITEM_CONTENT;
584 
585   if ((modify & GIMP_PDB_ITEM_CONTENT) && gimp_item_is_content_locked (item))
586     {
587       g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
588                    _("Item '%s' (%d) cannot be modified because its "
589                      "contents are locked"),
590                    gimp_object_get_name (item),
591                    gimp_item_get_ID (item));
592       return FALSE;
593     }
594 
595   if ((modify & GIMP_PDB_ITEM_POSITION) && gimp_item_is_position_locked (item))
596     {
597       g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
598                    _("Item '%s' (%d) cannot be modified because its "
599                      "position and size are locked"),
600                    gimp_object_get_name (item),
601                    gimp_item_get_ID (item));
602       return FALSE;
603     }
604 
605   return TRUE;
606 }
607 
608 gboolean
gimp_pdb_item_is_group(GimpItem * item,GError ** error)609 gimp_pdb_item_is_group (GimpItem  *item,
610                         GError   **error)
611 {
612   g_return_val_if_fail (GIMP_IS_ITEM (item), FALSE);
613   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
614 
615   if (! gimp_viewable_get_children (GIMP_VIEWABLE (item)))
616     {
617       g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
618                    _("Item '%s' (%d) cannot be used because it is "
619                      "not a group item"),
620                    gimp_object_get_name (item),
621                    gimp_item_get_ID (item));
622       return FALSE;
623     }
624 
625   return TRUE;
626 }
627 
628 gboolean
gimp_pdb_item_is_not_group(GimpItem * item,GError ** error)629 gimp_pdb_item_is_not_group (GimpItem  *item,
630                             GError   **error)
631 {
632   g_return_val_if_fail (GIMP_IS_ITEM (item), FALSE);
633   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
634 
635   if (gimp_viewable_get_children (GIMP_VIEWABLE (item)))
636     {
637       g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
638                    _("Item '%s' (%d) cannot be modified because it "
639                      "is a group item"),
640                    gimp_object_get_name (item),
641                    gimp_item_get_ID (item));
642       return FALSE;
643     }
644 
645   return TRUE;
646 }
647 
648 gboolean
gimp_pdb_layer_is_text_layer(GimpLayer * layer,GimpPDBItemModify modify,GError ** error)649 gimp_pdb_layer_is_text_layer (GimpLayer          *layer,
650                               GimpPDBItemModify   modify,
651                               GError            **error)
652 {
653   g_return_val_if_fail (GIMP_IS_LAYER (layer), FALSE);
654   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
655 
656   if (! gimp_item_is_text_layer (GIMP_ITEM (layer)))
657     {
658       g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
659                    _("Layer '%s' (%d) cannot be used because it is not "
660                      "a text layer"),
661                    gimp_object_get_name (layer),
662                    gimp_item_get_ID (GIMP_ITEM (layer)));
663 
664       return FALSE;
665     }
666 
667   return gimp_pdb_item_is_attached (GIMP_ITEM (layer), NULL, modify, error);
668 }
669 
670 static const gchar *
gimp_pdb_enum_value_get_nick(GType enum_type,gint value)671 gimp_pdb_enum_value_get_nick (GType enum_type,
672                               gint  value)
673 {
674   GEnumClass  *enum_class;
675   GEnumValue  *enum_value;
676   const gchar *nick;
677 
678   enum_class = g_type_class_ref (enum_type);
679   enum_value = g_enum_get_value (enum_class, value);
680 
681   nick = enum_value->value_nick;
682 
683   g_type_class_unref (enum_class);
684 
685   return nick;
686 }
687 
688 gboolean
gimp_pdb_image_is_base_type(GimpImage * image,GimpImageBaseType type,GError ** error)689 gimp_pdb_image_is_base_type (GimpImage          *image,
690                              GimpImageBaseType   type,
691                              GError            **error)
692 {
693   g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);
694   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
695 
696   if (gimp_image_get_base_type (image) == type)
697     return TRUE;
698 
699   g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
700                _("Image '%s' (%d) is of type '%s', "
701                  "but an image of type '%s' is expected"),
702                gimp_image_get_display_name (image),
703                gimp_image_get_ID (image),
704                gimp_pdb_enum_value_get_nick (GIMP_TYPE_IMAGE_BASE_TYPE,
705                                              gimp_image_get_base_type (image)),
706                gimp_pdb_enum_value_get_nick (GIMP_TYPE_IMAGE_BASE_TYPE, type));
707 
708   return FALSE;
709 }
710 
711 gboolean
gimp_pdb_image_is_not_base_type(GimpImage * image,GimpImageBaseType type,GError ** error)712 gimp_pdb_image_is_not_base_type (GimpImage          *image,
713                                  GimpImageBaseType   type,
714                                  GError            **error)
715 {
716   g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);
717   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
718 
719   if (gimp_image_get_base_type (image) != type)
720     return TRUE;
721 
722   g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
723                _("Image '%s' (%d) must not be of type '%s'"),
724                gimp_image_get_display_name (image),
725                gimp_image_get_ID (image),
726                gimp_pdb_enum_value_get_nick (GIMP_TYPE_IMAGE_BASE_TYPE, type));
727 
728   return FALSE;
729 }
730 
731 gboolean
gimp_pdb_image_is_precision(GimpImage * image,GimpPrecision precision,GError ** error)732 gimp_pdb_image_is_precision (GimpImage      *image,
733                              GimpPrecision   precision,
734                              GError        **error)
735 {
736   g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);
737   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
738 
739   if (gimp_image_get_precision (image) == precision)
740     return TRUE;
741 
742   g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
743                _("Image '%s' (%d) has precision '%s', "
744                  "but an image of precision '%s' is expected"),
745                gimp_image_get_display_name (image),
746                gimp_image_get_ID (image),
747                gimp_pdb_enum_value_get_nick (GIMP_TYPE_PRECISION,
748                                              gimp_image_get_precision (image)),
749                gimp_pdb_enum_value_get_nick (GIMP_TYPE_PRECISION, precision));
750 
751   return FALSE;
752 }
753 
754 gboolean
gimp_pdb_image_is_not_precision(GimpImage * image,GimpPrecision precision,GError ** error)755 gimp_pdb_image_is_not_precision (GimpImage      *image,
756                                  GimpPrecision   precision,
757                                  GError        **error)
758 {
759   g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);
760   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
761 
762   if (gimp_image_get_precision (image) != precision)
763     return TRUE;
764 
765   g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
766                _("Image '%s' (%d) must not be of precision '%s'"),
767                gimp_image_get_display_name (image),
768                gimp_image_get_ID (image),
769                gimp_pdb_enum_value_get_nick (GIMP_TYPE_PRECISION, precision));
770 
771   return FALSE;
772 }
773 
774 GimpGuide *
gimp_pdb_image_get_guide(GimpImage * image,gint guide_ID,GError ** error)775 gimp_pdb_image_get_guide (GimpImage  *image,
776                           gint        guide_ID,
777                           GError    **error)
778 {
779   GimpGuide *guide;
780 
781   g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
782   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
783 
784   guide = gimp_image_get_guide (image, guide_ID);
785 
786   if (guide)
787     return guide;
788 
789   g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
790                _("Image '%s' (%d) does not contain guide with ID %d"),
791                gimp_image_get_display_name (image),
792                gimp_image_get_ID (image),
793                guide_ID);
794   return NULL;
795 }
796 
797 GimpSamplePoint *
gimp_pdb_image_get_sample_point(GimpImage * image,gint sample_point_ID,GError ** error)798 gimp_pdb_image_get_sample_point (GimpImage  *image,
799                                  gint        sample_point_ID,
800                                  GError    **error)
801 {
802   GimpSamplePoint *sample_point;
803 
804   g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
805   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
806 
807   sample_point = gimp_image_get_sample_point (image, sample_point_ID);
808 
809   if (sample_point)
810     return sample_point;
811 
812   g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
813                _("Image '%s' (%d) does not contain sample point with ID %d"),
814                gimp_image_get_display_name (image),
815                gimp_image_get_ID (image),
816                sample_point_ID);
817   return NULL;
818 }
819 
820 GimpStroke *
gimp_pdb_get_vectors_stroke(GimpVectors * vectors,gint stroke_ID,GimpPDBItemModify modify,GError ** error)821 gimp_pdb_get_vectors_stroke (GimpVectors        *vectors,
822                              gint                stroke_ID,
823                              GimpPDBItemModify   modify,
824                              GError            **error)
825 {
826   GimpStroke *stroke = NULL;
827 
828   g_return_val_if_fail (GIMP_IS_VECTORS (vectors), NULL);
829   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
830 
831   if (! gimp_pdb_item_is_not_group (GIMP_ITEM (vectors), error))
832     return NULL;
833 
834   if (! modify || gimp_pdb_item_is_modifiable (GIMP_ITEM (vectors),
835                                                modify, error))
836     {
837       stroke = gimp_vectors_stroke_get_by_ID (vectors, stroke_ID);
838 
839       if (! stroke)
840         g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
841                      _("Vectors object %d does not contain stroke with ID %d"),
842                      gimp_item_get_ID (GIMP_ITEM (vectors)), stroke_ID);
843     }
844 
845   return stroke;
846 }
847