1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3  *
4  * gimpplugin-progress.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 <gdk-pixbuf/gdk-pixbuf.h>
23 #include <gegl.h>
24 
25 #include "plug-in-types.h"
26 
27 #include "core/gimp.h"
28 #include "core/gimpparamspecs.h"
29 #include "core/gimppdbprogress.h"
30 #include "core/gimpprogress.h"
31 
32 #include "pdb/gimppdb.h"
33 #include "pdb/gimppdberror.h"
34 
35 #include "gimpplugin.h"
36 #include "gimpplugin-progress.h"
37 #include "gimppluginmanager.h"
38 #include "gimptemporaryprocedure.h"
39 
40 #include "gimp-intl.h"
41 
42 
43 /*  local function prototypes  */
44 
45 static void   gimp_plug_in_progress_cancel_callback (GimpProgress *progress,
46                                                      GimpPlugIn   *plug_in);
47 
48 
49 /*  public functions  */
50 
51 gint
gimp_plug_in_progress_attach(GimpProgress * progress)52 gimp_plug_in_progress_attach (GimpProgress *progress)
53 {
54   gint attach_count;
55 
56   g_return_val_if_fail (GIMP_IS_PROGRESS (progress), 0);
57 
58   attach_count =
59     GPOINTER_TO_INT (g_object_get_data (G_OBJECT (progress),
60                                         "plug-in-progress-attach-count"));
61 
62   attach_count++;
63 
64   g_object_set_data (G_OBJECT (progress), "plug-in-progress-attach-count",
65                      GINT_TO_POINTER (attach_count));
66 
67   return attach_count;
68 }
69 
70 gint
gimp_plug_in_progress_detach(GimpProgress * progress)71 gimp_plug_in_progress_detach (GimpProgress *progress)
72 {
73   gint attach_count;
74 
75   g_return_val_if_fail (GIMP_IS_PROGRESS (progress), 0);
76 
77   attach_count =
78     GPOINTER_TO_INT (g_object_get_data (G_OBJECT (progress),
79                                         "plug-in-progress-attach-count"));
80 
81   attach_count--;
82 
83   g_object_set_data (G_OBJECT (progress), "plug-in-progress-attach-count",
84                      GINT_TO_POINTER (attach_count));
85 
86   return attach_count;
87 }
88 
89 void
gimp_plug_in_progress_start(GimpPlugIn * plug_in,const gchar * message,GimpObject * display)90 gimp_plug_in_progress_start (GimpPlugIn  *plug_in,
91                              const gchar *message,
92                              GimpObject  *display)
93 {
94   GimpPlugInProcFrame *proc_frame;
95 
96   g_return_if_fail (GIMP_IS_PLUG_IN (plug_in));
97   g_return_if_fail (display == NULL || GIMP_IS_OBJECT (display));
98 
99   proc_frame = gimp_plug_in_get_proc_frame (plug_in);
100 
101   if (! proc_frame->progress)
102     {
103       proc_frame->progress = gimp_new_progress (plug_in->manager->gimp,
104                                                 display);
105 
106       if (proc_frame->progress)
107         {
108           proc_frame->progress_created = TRUE;
109 
110           g_object_ref (proc_frame->progress);
111 
112           gimp_plug_in_progress_attach (proc_frame->progress);
113         }
114     }
115 
116   if (proc_frame->progress)
117     {
118       if (! proc_frame->progress_cancel_id)
119         {
120           g_object_add_weak_pointer (G_OBJECT (proc_frame->progress),
121                                      (gpointer) &proc_frame->progress);
122 
123           proc_frame->progress_cancel_id =
124             g_signal_connect (proc_frame->progress, "cancel",
125                               G_CALLBACK (gimp_plug_in_progress_cancel_callback),
126                               plug_in);
127         }
128 
129       if (gimp_progress_is_active (proc_frame->progress))
130         {
131           if (message)
132             gimp_progress_set_text_literal (proc_frame->progress, message);
133 
134           if (gimp_progress_get_value (proc_frame->progress) > 0.0)
135             gimp_progress_set_value (proc_frame->progress, 0.0);
136         }
137       else
138         {
139           gimp_progress_start (proc_frame->progress, TRUE,
140                                "%s", message ? message : "");
141         }
142     }
143 }
144 
145 void
gimp_plug_in_progress_end(GimpPlugIn * plug_in,GimpPlugInProcFrame * proc_frame)146 gimp_plug_in_progress_end (GimpPlugIn          *plug_in,
147                            GimpPlugInProcFrame *proc_frame)
148 {
149   g_return_if_fail (GIMP_IS_PLUG_IN (plug_in));
150   g_return_if_fail (proc_frame != NULL);
151 
152   if (proc_frame->progress)
153     {
154       if (proc_frame->progress_cancel_id)
155         {
156           g_signal_handler_disconnect (proc_frame->progress,
157                                        proc_frame->progress_cancel_id);
158           proc_frame->progress_cancel_id = 0;
159 
160           g_object_remove_weak_pointer (G_OBJECT (proc_frame->progress),
161                                         (gpointer) &proc_frame->progress);
162         }
163 
164       if (gimp_plug_in_progress_detach (proc_frame->progress) < 1 &&
165           gimp_progress_is_active (proc_frame->progress))
166         {
167           gimp_progress_end (proc_frame->progress);
168         }
169 
170       if (proc_frame->progress_created)
171         {
172           gimp_free_progress (plug_in->manager->gimp, proc_frame->progress);
173           g_clear_object (&proc_frame->progress);
174         }
175     }
176 }
177 
178 void
gimp_plug_in_progress_set_text(GimpPlugIn * plug_in,const gchar * message)179 gimp_plug_in_progress_set_text (GimpPlugIn  *plug_in,
180                                 const gchar *message)
181 {
182   GimpPlugInProcFrame *proc_frame;
183 
184   g_return_if_fail (GIMP_IS_PLUG_IN (plug_in));
185 
186   proc_frame = gimp_plug_in_get_proc_frame (plug_in);
187 
188   if (proc_frame->progress)
189     gimp_progress_set_text_literal (proc_frame->progress, message);
190 }
191 
192 void
gimp_plug_in_progress_set_value(GimpPlugIn * plug_in,gdouble percentage)193 gimp_plug_in_progress_set_value (GimpPlugIn *plug_in,
194                                  gdouble     percentage)
195 {
196   GimpPlugInProcFrame *proc_frame;
197 
198   g_return_if_fail (GIMP_IS_PLUG_IN (plug_in));
199 
200   proc_frame = gimp_plug_in_get_proc_frame (plug_in);
201 
202   if (! proc_frame->progress                           ||
203       ! gimp_progress_is_active (proc_frame->progress) ||
204       ! proc_frame->progress_cancel_id)
205     {
206       gimp_plug_in_progress_start (plug_in, NULL, NULL);
207     }
208 
209   if (proc_frame->progress && gimp_progress_is_active (proc_frame->progress))
210     gimp_progress_set_value (proc_frame->progress, percentage);
211 }
212 
213 void
gimp_plug_in_progress_pulse(GimpPlugIn * plug_in)214 gimp_plug_in_progress_pulse (GimpPlugIn *plug_in)
215 {
216   GimpPlugInProcFrame *proc_frame;
217 
218   g_return_if_fail (GIMP_IS_PLUG_IN (plug_in));
219 
220   proc_frame = gimp_plug_in_get_proc_frame (plug_in);
221 
222   if (! proc_frame->progress                           ||
223       ! gimp_progress_is_active (proc_frame->progress) ||
224       ! proc_frame->progress_cancel_id)
225     {
226       gimp_plug_in_progress_start (plug_in, NULL, NULL);
227     }
228 
229   if (proc_frame->progress && gimp_progress_is_active (proc_frame->progress))
230     gimp_progress_pulse (proc_frame->progress);
231 }
232 
233 guint32
gimp_plug_in_progress_get_window_id(GimpPlugIn * plug_in)234 gimp_plug_in_progress_get_window_id (GimpPlugIn *plug_in)
235 {
236   GimpPlugInProcFrame *proc_frame;
237 
238   g_return_val_if_fail (GIMP_IS_PLUG_IN (plug_in), 0);
239 
240   proc_frame = gimp_plug_in_get_proc_frame (plug_in);
241 
242   if (proc_frame->progress)
243     return gimp_progress_get_window_id (proc_frame->progress);
244 
245   return 0;
246 }
247 
248 gboolean
gimp_plug_in_progress_install(GimpPlugIn * plug_in,const gchar * progress_callback)249 gimp_plug_in_progress_install (GimpPlugIn  *plug_in,
250                                const gchar *progress_callback)
251 {
252   GimpPlugInProcFrame *proc_frame;
253   GimpProcedure       *procedure;
254 
255   g_return_val_if_fail (GIMP_IS_PLUG_IN (plug_in), FALSE);
256   g_return_val_if_fail (progress_callback != NULL, FALSE);
257 
258   procedure = gimp_pdb_lookup_procedure (plug_in->manager->gimp->pdb,
259                                          progress_callback);
260 
261   if (! GIMP_IS_TEMPORARY_PROCEDURE (procedure)                ||
262       GIMP_TEMPORARY_PROCEDURE (procedure)->plug_in != plug_in ||
263       procedure->num_args                           != 3       ||
264       ! GIMP_IS_PARAM_SPEC_INT32 (procedure->args[0])          ||
265       ! G_IS_PARAM_SPEC_STRING   (procedure->args[1])          ||
266       ! G_IS_PARAM_SPEC_DOUBLE   (procedure->args[2]))
267     {
268       return FALSE;
269     }
270 
271   proc_frame = gimp_plug_in_get_proc_frame (plug_in);
272 
273   if (proc_frame->progress)
274     {
275       gimp_plug_in_progress_end (plug_in, proc_frame);
276 
277       g_clear_object (&proc_frame->progress);
278     }
279 
280   proc_frame->progress = g_object_new (GIMP_TYPE_PDB_PROGRESS,
281                                        "pdb",           plug_in->manager->gimp->pdb,
282                                        "context",       proc_frame->main_context,
283                                        "callback-name", progress_callback,
284                                        NULL);
285 
286   gimp_plug_in_progress_attach (proc_frame->progress);
287 
288   return TRUE;
289 }
290 
291 gboolean
gimp_plug_in_progress_uninstall(GimpPlugIn * plug_in,const gchar * progress_callback)292 gimp_plug_in_progress_uninstall (GimpPlugIn  *plug_in,
293                                  const gchar *progress_callback)
294 {
295   GimpPlugInProcFrame *proc_frame;
296 
297   g_return_val_if_fail (GIMP_IS_PLUG_IN (plug_in), FALSE);
298   g_return_val_if_fail (progress_callback != NULL, FALSE);
299 
300   proc_frame = gimp_plug_in_get_proc_frame (plug_in);
301 
302   if (GIMP_IS_PDB_PROGRESS (proc_frame->progress))
303     {
304       gimp_plug_in_progress_end (plug_in, proc_frame);
305 
306       g_clear_object (&proc_frame->progress);
307 
308       return TRUE;
309     }
310 
311   return FALSE;
312 }
313 
314 gboolean
gimp_plug_in_progress_cancel(GimpPlugIn * plug_in,const gchar * progress_callback)315 gimp_plug_in_progress_cancel (GimpPlugIn  *plug_in,
316                               const gchar *progress_callback)
317 {
318   g_return_val_if_fail (GIMP_IS_PLUG_IN (plug_in), FALSE);
319   g_return_val_if_fail (progress_callback != NULL, FALSE);
320 
321   return FALSE;
322 }
323 
324 
325 /*  private functions  */
326 
327 static GimpValueArray *
get_cancel_return_values(GimpProcedure * procedure)328 get_cancel_return_values (GimpProcedure *procedure)
329 {
330   GimpValueArray *return_vals;
331   GError         *error;
332 
333   error = g_error_new_literal (GIMP_PDB_ERROR, GIMP_PDB_ERROR_CANCELLED,
334                                _("Cancelled"));
335   return_vals = gimp_procedure_get_return_values (procedure, FALSE, error);
336   g_error_free (error);
337 
338   return return_vals;
339 }
340 
341 static void
gimp_plug_in_progress_cancel_callback(GimpProgress * progress,GimpPlugIn * plug_in)342 gimp_plug_in_progress_cancel_callback (GimpProgress *progress,
343                                        GimpPlugIn   *plug_in)
344 {
345   GimpPlugInProcFrame *proc_frame = &plug_in->main_proc_frame;
346   GList               *list;
347 
348   if (proc_frame->main_loop)
349     {
350       proc_frame->return_vals =
351         get_cancel_return_values (proc_frame->procedure);
352     }
353 
354   for (list = plug_in->temp_proc_frames; list; list = g_list_next (list))
355     {
356       proc_frame = list->data;
357 
358       if (proc_frame->main_loop)
359         {
360           proc_frame->return_vals =
361             get_cancel_return_values (proc_frame->procedure);
362         }
363     }
364 
365   gimp_plug_in_close (plug_in, TRUE);
366 }
367