1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3  *
4  * gimppluginprocframe.c
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #include "config.h"
21 
22 #include <string.h>
23 
24 #include <gdk-pixbuf/gdk-pixbuf.h>
25 #include <gegl.h>
26 
27 #include "libgimpbase/gimpbase.h"
28 
29 #include "plug-in-types.h"
30 
31 #include "core/gimpprogress.h"
32 
33 #include "pdb/gimppdbcontext.h"
34 #include "pdb/gimppdberror.h"
35 
36 #include "gimpplugin.h"
37 #include "gimpplugin-cleanup.h"
38 #include "gimpplugin-progress.h"
39 #include "gimppluginprocedure.h"
40 
41 #include "gimp-intl.h"
42 
43 
44 /*  public functions  */
45 
46 GimpPlugInProcFrame *
gimp_plug_in_proc_frame_new(GimpContext * context,GimpProgress * progress,GimpPlugInProcedure * procedure)47 gimp_plug_in_proc_frame_new (GimpContext         *context,
48                              GimpProgress        *progress,
49                              GimpPlugInProcedure *procedure)
50 {
51   GimpPlugInProcFrame *proc_frame;
52 
53   g_return_val_if_fail (GIMP_IS_PDB_CONTEXT (context), NULL);
54   g_return_val_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress), NULL);
55   g_return_val_if_fail (GIMP_IS_PLUG_IN_PROCEDURE (procedure), NULL);
56 
57   proc_frame = g_slice_new0 (GimpPlugInProcFrame);
58 
59   proc_frame->ref_count = 1;
60 
61   gimp_plug_in_proc_frame_init (proc_frame, context, progress, procedure);
62 
63   return proc_frame;
64 }
65 
66 void
gimp_plug_in_proc_frame_init(GimpPlugInProcFrame * proc_frame,GimpContext * context,GimpProgress * progress,GimpPlugInProcedure * procedure)67 gimp_plug_in_proc_frame_init (GimpPlugInProcFrame *proc_frame,
68                               GimpContext         *context,
69                               GimpProgress        *progress,
70                               GimpPlugInProcedure *procedure)
71 {
72   g_return_if_fail (proc_frame != NULL);
73   g_return_if_fail (GIMP_IS_PDB_CONTEXT (context));
74   g_return_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress));
75   g_return_if_fail (procedure == NULL ||
76                     GIMP_IS_PLUG_IN_PROCEDURE (procedure));
77 
78   proc_frame->main_context       = g_object_ref (context);
79   proc_frame->context_stack      = NULL;
80   proc_frame->procedure          = procedure ? g_object_ref (GIMP_PROCEDURE (procedure)) : NULL;
81   proc_frame->main_loop          = NULL;
82   proc_frame->return_vals        = NULL;
83   proc_frame->progress           = progress ? g_object_ref (progress) : NULL;
84   proc_frame->progress_created   = FALSE;
85   proc_frame->progress_cancel_id = 0;
86   proc_frame->error_handler      = GIMP_PDB_ERROR_HANDLER_INTERNAL;
87 
88   if (progress)
89     gimp_plug_in_progress_attach (progress);
90 }
91 
92 void
gimp_plug_in_proc_frame_dispose(GimpPlugInProcFrame * proc_frame,GimpPlugIn * plug_in)93 gimp_plug_in_proc_frame_dispose (GimpPlugInProcFrame *proc_frame,
94                                  GimpPlugIn          *plug_in)
95 {
96   g_return_if_fail (proc_frame != NULL);
97   g_return_if_fail (GIMP_IS_PLUG_IN (plug_in));
98 
99   if (proc_frame->progress)
100     {
101       gimp_plug_in_progress_end (plug_in, proc_frame);
102 
103       g_clear_object (&proc_frame->progress);
104     }
105 
106   if (proc_frame->context_stack)
107     {
108       g_list_free_full (proc_frame->context_stack,
109                         (GDestroyNotify) g_object_unref);
110       proc_frame->context_stack = NULL;
111     }
112 
113   g_clear_object (&proc_frame->main_context);
114   g_clear_pointer (&proc_frame->return_vals, gimp_value_array_unref);
115   g_clear_pointer (&proc_frame->main_loop, g_main_loop_unref);
116 
117   if (proc_frame->image_cleanups || proc_frame->item_cleanups)
118     gimp_plug_in_cleanup (plug_in, proc_frame);
119 
120   g_clear_object (&proc_frame->procedure);
121 }
122 
123 GimpPlugInProcFrame *
gimp_plug_in_proc_frame_ref(GimpPlugInProcFrame * proc_frame)124 gimp_plug_in_proc_frame_ref (GimpPlugInProcFrame *proc_frame)
125 {
126   g_return_val_if_fail (proc_frame != NULL, NULL);
127 
128   proc_frame->ref_count++;
129 
130   return proc_frame;
131 }
132 
133 void
gimp_plug_in_proc_frame_unref(GimpPlugInProcFrame * proc_frame,GimpPlugIn * plug_in)134 gimp_plug_in_proc_frame_unref (GimpPlugInProcFrame *proc_frame,
135                                GimpPlugIn          *plug_in)
136 {
137   g_return_if_fail (proc_frame != NULL);
138   g_return_if_fail (GIMP_IS_PLUG_IN (plug_in));
139 
140   proc_frame->ref_count--;
141 
142   if (proc_frame->ref_count < 1)
143     {
144       gimp_plug_in_proc_frame_dispose (proc_frame, plug_in);
145       g_slice_free (GimpPlugInProcFrame, proc_frame);
146     }
147 }
148 
149 GimpValueArray *
gimp_plug_in_proc_frame_get_return_values(GimpPlugInProcFrame * proc_frame)150 gimp_plug_in_proc_frame_get_return_values (GimpPlugInProcFrame *proc_frame)
151 {
152   GimpValueArray *return_vals;
153 
154   g_return_val_if_fail (proc_frame != NULL, NULL);
155 
156   if (proc_frame->return_vals)
157     {
158       if (gimp_value_array_length (proc_frame->return_vals) >=
159           proc_frame->procedure->num_values + 1)
160         {
161           return_vals = proc_frame->return_vals;
162         }
163       else
164         {
165           /* Allocate new return values of the correct size. */
166           return_vals = gimp_procedure_get_return_values (proc_frame->procedure,
167                                                           TRUE, NULL);
168 
169           /* Copy all of the arguments we can. */
170           memcpy (gimp_value_array_index (return_vals, 0),
171                   gimp_value_array_index (proc_frame->return_vals, 0),
172                   sizeof (GValue) *
173                   gimp_value_array_length (proc_frame->return_vals));
174 
175           /* Free the old arguments. */
176           memset (gimp_value_array_index (proc_frame->return_vals, 0), 0,
177                   sizeof (GValue) *
178                   gimp_value_array_length (proc_frame->return_vals));
179           gimp_value_array_unref (proc_frame->return_vals);
180         }
181 
182       /* We have consumed any saved values, so clear them. */
183       proc_frame->return_vals = NULL;
184     }
185   else
186     {
187       GimpProcedure *procedure = proc_frame->procedure;
188       GError        *error;
189 
190       error = g_error_new (GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_RETURN_VALUE,
191                            _("Procedure '%s' returned no return values"),
192                            gimp_object_get_name (procedure));
193 
194       return_vals = gimp_procedure_get_return_values (procedure, FALSE,
195                                                       error);
196       g_error_free (error);
197     }
198 
199   return return_vals;
200 }
201