plug-in-message.c 23.4 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (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
 * along with this program; if not, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
17
 */
18

Elliot Lee's avatar
Elliot Lee committed
19
#include "config.h"
Tor Lillqvist's avatar
Tor Lillqvist committed
20

Elliot Lee's avatar
Elliot Lee committed
21
#include <string.h>
Tor Lillqvist's avatar
Tor Lillqvist committed
22

23
#include <glib-object.h>
24

25
26
27
#include "libgimpbase/gimpbase.h"
#include "libgimpbase/gimpprotocol.h"
#include "libgimpbase/gimpwire.h"
28

Michael Natterer's avatar
Michael Natterer committed
29
#include "plug-in-types.h"
30

Michael Natterer's avatar
Michael Natterer committed
31
32
33
#include "base/tile.h"
#include "base/tile-manager.h"

34
#include "core/gimp.h"
35
#include "core/gimpdrawable.h"
36

Michael Natterer's avatar
Michael Natterer committed
37
#include "plug-in.h"
38
39
40
#include "plug-ins.h"
#include "plug-in-def.h"
#include "plug-in-params.h"
41
#include "plug-in-proc.h"
42
#include "plug-in-shm.h"
43

44

45
typedef struct _PlugInBlocked PlugInBlocked;
Elliot Lee's avatar
Elliot Lee committed
46
47
48
49

struct _PlugInBlocked
{
  PlugIn *plug_in;
50
  gchar  *proc_name;
Elliot Lee's avatar
Elliot Lee committed
51
52
53
};


54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*  local function prototypes  */

static void plug_in_handle_quit             (PlugIn          *plug_in);
static void plug_in_handle_tile_req         (PlugIn          *plug_in,
                                             GPTileReq       *tile_req);
static void plug_in_handle_proc_run         (PlugIn          *plug_in,
                                             GPProcRun       *proc_run);
static void plug_in_handle_proc_return_priv (PlugIn          *plug_in,
                                             GPProcReturn    *proc_return);
static void plug_in_handle_proc_return      (PlugIn          *plug_in,
                                             GPProcReturn    *proc_return);
#ifdef ENABLE_TEMP_RETURN
static void plug_in_handle_temp_proc_return (PlugIn          *plug_in,
                                             GPProcReturn    *proc_return);
Tor Lillqvist's avatar
Tor Lillqvist committed
68
#endif
69
70
71
72
73
74
static void plug_in_handle_proc_install     (PlugIn          *plug_in,
                                             GPProcInstall   *proc_install);
static void plug_in_handle_proc_uninstall   (PlugIn          *plug_in,
                                             GPProcUninstall *proc_uninstall);
static void plug_in_handle_extension_ack    (PlugIn          *plug_in);
static void plug_in_handle_has_init         (PlugIn          *plug_in);
Tor Lillqvist's avatar
Tor Lillqvist committed
75

76

77
/*  private variables  */
78

79
static GSList *blocked_plug_ins = NULL;
80

81

82
/*  public functions  */
Elliot Lee's avatar
Elliot Lee committed
83
84

void
85
86
plug_in_handle_message (PlugIn      *plug_in,
                        WireMessage *msg)
Elliot Lee's avatar
Elliot Lee committed
87
88
89
90
{
  switch (msg->type)
    {
    case GP_QUIT:
91
      plug_in_handle_quit (plug_in);
Elliot Lee's avatar
Elliot Lee committed
92
      break;
Michael Natterer's avatar
Michael Natterer committed
93

Elliot Lee's avatar
Elliot Lee committed
94
    case GP_CONFIG:
95
      g_warning ("plug_in_handle_message: "
96
		 "received a config message (should not happen)");
97
      plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
98
      break;
Michael Natterer's avatar
Michael Natterer committed
99

Elliot Lee's avatar
Elliot Lee committed
100
    case GP_TILE_REQ:
101
      plug_in_handle_tile_req (plug_in, msg->data);
Elliot Lee's avatar
Elliot Lee committed
102
      break;
Michael Natterer's avatar
Michael Natterer committed
103

Elliot Lee's avatar
Elliot Lee committed
104
    case GP_TILE_ACK:
105
      g_warning ("plug_in_handle_message: "
106
		 "received a tile ack message (should not happen)");
107
      plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
108
      break;
Michael Natterer's avatar
Michael Natterer committed
109

Elliot Lee's avatar
Elliot Lee committed
110
    case GP_TILE_DATA:
111
      g_warning ("plug_in_handle_message: "
112
		 "received a tile data message (should not happen)");
113
      plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
114
      break;
Michael Natterer's avatar
Michael Natterer committed
115

Elliot Lee's avatar
Elliot Lee committed
116
    case GP_PROC_RUN:
117
      plug_in_handle_proc_run (plug_in, msg->data);
Elliot Lee's avatar
Elliot Lee committed
118
      break;
Michael Natterer's avatar
Michael Natterer committed
119

Elliot Lee's avatar
Elliot Lee committed
120
    case GP_PROC_RETURN:
121
      plug_in_handle_proc_return (plug_in, msg->data);
Elliot Lee's avatar
Elliot Lee committed
122
      break;
Michael Natterer's avatar
Michael Natterer committed
123

Elliot Lee's avatar
Elliot Lee committed
124
    case GP_TEMP_PROC_RUN:
125
      g_warning ("plug_in_handle_message: "
126
		 "received a temp proc run message (should not happen)");
127
      plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
128
      break;
Michael Natterer's avatar
Michael Natterer committed
129

Elliot Lee's avatar
Elliot Lee committed
130
    case GP_TEMP_PROC_RETURN:
131
132
133
134
135
136
137
#ifdef ENABLE_TEMP_RETURN
      plug_in_handle_temp_proc_return (plug_in, msg->data);
#else
      g_warning ("plug_in_handle_message: "
		 "received a temp proc return message (should not happen)");
      plug_in_close (plug_in, TRUE);
#endif
Elliot Lee's avatar
Elliot Lee committed
138
      break;
Michael Natterer's avatar
Michael Natterer committed
139

Elliot Lee's avatar
Elliot Lee committed
140
    case GP_PROC_INSTALL:
141
      plug_in_handle_proc_install (plug_in, msg->data);
Elliot Lee's avatar
Elliot Lee committed
142
      break;
Michael Natterer's avatar
Michael Natterer committed
143

Elliot Lee's avatar
Elliot Lee committed
144
    case GP_PROC_UNINSTALL:
145
      plug_in_handle_proc_uninstall (plug_in, msg->data);
Elliot Lee's avatar
Elliot Lee committed
146
      break;
Michael Natterer's avatar
Michael Natterer committed
147

Elliot Lee's avatar
Elliot Lee committed
148
    case GP_EXTENSION_ACK:
Michael Natterer's avatar
Michael Natterer committed
149
      plug_in_handle_extension_ack (plug_in);
Elliot Lee's avatar
Elliot Lee committed
150
      break;
Michael Natterer's avatar
Michael Natterer committed
151

152
    case GP_HAS_INIT:
153
      plug_in_handle_has_init (plug_in);
Michael Natterer's avatar
Michael Natterer committed
154
      break;
Elliot Lee's avatar
Elliot Lee committed
155
156
157
    }
}

158
159
160

/*  private functions  */

Elliot Lee's avatar
Elliot Lee committed
161
static void
162
plug_in_handle_quit (PlugIn *plug_in)
Elliot Lee's avatar
Elliot Lee committed
163
{
164
  plug_in_close (plug_in, FALSE);
Elliot Lee's avatar
Elliot Lee committed
165
166
167
}

static void
168
169
plug_in_handle_tile_req (PlugIn    *plug_in,
                         GPTileReq *tile_req)
Elliot Lee's avatar
Elliot Lee committed
170
{
171
172
173
  GPTileData   tile_data;
  GPTileData  *tile_info;
  WireMessage  msg;
Elliot Lee's avatar
Elliot Lee committed
174
  TileManager *tm;
175
  Tile        *tile;
176
177
178
  gint         shm_ID;

  shm_ID = plug_in_shm_get_ID (plug_in->gimp);
Elliot Lee's avatar
Elliot Lee committed
179
180
181

  if (tile_req->drawable_ID == -1)
    {
Michael Natterer's avatar
Michael Natterer committed
182
183
      /*  this branch communicates with libgimp/gimptile.c:gimp_tile_put()  */

Elliot Lee's avatar
Elliot Lee committed
184
      tile_data.drawable_ID = -1;
185
186
187
188
189
190
191
      tile_data.tile_num    = 0;
      tile_data.shadow      = 0;
      tile_data.bpp         = 0;
      tile_data.width       = 0;
      tile_data.height      = 0;
      tile_data.use_shm     = (shm_ID == -1) ? FALSE : TRUE;
      tile_data.data        = NULL;
Elliot Lee's avatar
Elliot Lee committed
192

193
      if (! gp_tile_data_write (plug_in->my_write, &tile_data, plug_in))
Elliot Lee's avatar
Elliot Lee committed
194
	{
195
	  g_warning ("plug_in_handle_tile_req: ERROR");
196
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
197
198
199
	  return;
	}

200
      if (! wire_read_msg (plug_in->my_read, &msg, plug_in))
Elliot Lee's avatar
Elliot Lee committed
201
	{
202
	  g_warning ("plug_in_handle_tile_req: ERROR");
203
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
204
205
206
207
208
	  return;
	}

      if (msg.type != GP_TILE_DATA)
	{
209
	  g_warning ("expected tile data and received: %d", msg.type);
210
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
211
212
213
214
215
216
	  return;
	}

      tile_info = msg.data;

      if (tile_info->shadow)
217
218
219
	tm = gimp_drawable_shadow ((GimpDrawable *)
                                   gimp_item_get_by_ID (plug_in->gimp,
                                                        tile_info->drawable_ID));
Elliot Lee's avatar
Elliot Lee committed
220
      else
221
222
223
	tm = gimp_drawable_data ((GimpDrawable *)
                                 gimp_item_get_by_ID (plug_in->gimp,
                                                      tile_info->drawable_ID));
Elliot Lee's avatar
Elliot Lee committed
224
225
226

      if (!tm)
	{
227
	  g_warning ("plug-in requested invalid drawable (killing)");
228
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
229
230
231
	  return;
	}

232
      tile = tile_manager_get (tm, tile_info->tile_num, TRUE, TRUE);
Elliot Lee's avatar
Elliot Lee committed
233
234
      if (!tile)
	{
235
	  g_warning ("plug-in requested invalid tile (killing)");
236
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
237
238
239
240
	  return;
	}

      if (tile_data.use_shm)
241
242
243
	memcpy (tile_data_pointer (tile, 0, 0),
                plug_in_shm_get_addr (plug_in->gimp),
                tile_size (tile));
Elliot Lee's avatar
Elliot Lee committed
244
      else
245
246
247
	memcpy (tile_data_pointer (tile, 0, 0),
                tile_info->data,
                tile_size (tile));
Elliot Lee's avatar
Elliot Lee committed
248

249
      tile_release (tile, TRUE);
Elliot Lee's avatar
Elliot Lee committed
250
251

      wire_destroy (&msg);
252
      if (! gp_tile_ack_write (plug_in->my_write, plug_in))
Elliot Lee's avatar
Elliot Lee committed
253
	{
254
	  g_warning ("plug_in_handle_tile_req: ERROR");
255
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
256
257
258
259
260
	  return;
	}
    }
  else
    {
Michael Natterer's avatar
Michael Natterer committed
261
262
      /*  this branch communicates with libgimp/gimptile.c:gimp_tile_get()  */

Elliot Lee's avatar
Elliot Lee committed
263
      if (tile_req->shadow)
264
265
266
	tm = gimp_drawable_shadow ((GimpDrawable *)
                                   gimp_item_get_by_ID (plug_in->gimp,
                                                        tile_req->drawable_ID));
Elliot Lee's avatar
Elliot Lee committed
267
      else
268
269
270
	tm = gimp_drawable_data ((GimpDrawable *)
                                 gimp_item_get_by_ID (plug_in->gimp,
                                                      tile_req->drawable_ID));
Elliot Lee's avatar
Elliot Lee committed
271

272
      if (! tm)
Elliot Lee's avatar
Elliot Lee committed
273
	{
274
	  g_warning ("plug-in requested invalid drawable (killing)");
275
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
276
277
278
	  return;
	}

279
      tile = tile_manager_get (tm, tile_req->tile_num, TRUE, FALSE);
280
      if (! tile)
Elliot Lee's avatar
Elliot Lee committed
281
	{
282
	  g_warning ("plug-in requested invalid tile (killing)");
283
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
284
285
286
287
	  return;
	}

      tile_data.drawable_ID = tile_req->drawable_ID;
288
289
290
291
292
293
      tile_data.tile_num    = tile_req->tile_num;
      tile_data.shadow      = tile_req->shadow;
      tile_data.bpp         = tile_bpp (tile);
      tile_data.width       = tile_ewidth (tile);
      tile_data.height      = tile_eheight (tile);
      tile_data.use_shm     = (shm_ID == -1) ? FALSE : TRUE;
Elliot Lee's avatar
Elliot Lee committed
294
295

      if (tile_data.use_shm)
296
297
298
	memcpy (plug_in_shm_get_addr (plug_in->gimp),
                tile_data_pointer (tile, 0, 0),
                tile_size (tile));
Elliot Lee's avatar
Elliot Lee committed
299
      else
scott's avatar
scott committed
300
	tile_data.data = tile_data_pointer (tile, 0, 0);
Elliot Lee's avatar
Elliot Lee committed
301

302
      if (! gp_tile_data_write (plug_in->my_write, &tile_data, plug_in))
Elliot Lee's avatar
Elliot Lee committed
303
	{
304
	  g_message ("plug_in_handle_tile_req: ERROR");
305
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
306
307
308
	  return;
	}

309
      tile_release (tile, FALSE);
Elliot Lee's avatar
Elliot Lee committed
310

311
      if (! wire_read_msg (plug_in->my_read, &msg, plug_in))
Elliot Lee's avatar
Elliot Lee committed
312
	{
313
	  g_message ("plug_in_handle_tile_req: ERROR");
314
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
315
316
317
318
319
	  return;
	}

      if (msg.type != GP_TILE_ACK)
	{
320
	  g_warning ("expected tile ack and received: %d", msg.type);
321
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
322
323
324
325
326
327
328
329
	  return;
	}

      wire_destroy (&msg);
    }
}

static void
330
plug_in_handle_proc_run (PlugIn    *plug_in,
331
                         GPProcRun *proc_run)
Elliot Lee's avatar
Elliot Lee committed
332
{
333
334
335
336
  const gchar *proc_name = NULL;
  ProcRecord  *proc_rec;
  Argument    *args;
  Argument    *return_vals;
Elliot Lee's avatar
Elliot Lee committed
337

338
  proc_rec = procedural_db_lookup (plug_in->gimp, proc_run->name);
Elliot Lee's avatar
Elliot Lee committed
339

340
341
342
343
344
345
346
347
348
349
350
351
352
353
  if (! proc_rec)
    {
      proc_name = g_hash_table_lookup (plug_in->gimp->procedural_compat_ht,
                                       proc_run->name);

      if (proc_name)
        proc_rec = procedural_db_lookup (plug_in->gimp, proc_name);
    }

  if (! proc_name)
    proc_name = proc_run->name;

  args = plug_in_params_to_args (proc_run->params, proc_run->nparams, FALSE);

354
  plug_in_push (plug_in->gimp, plug_in);
355
356
357
358

  /*  Execute the procedure even if procedural_db_lookup() returned NULL,
   *  procedural_db_execute() will return appropriate error return_vals.
   */
359
  return_vals = procedural_db_execute (plug_in->gimp, proc_name, args);
360

361
  plug_in_pop (plug_in->gimp);
Elliot Lee's avatar
Elliot Lee committed
362
363
364

  if (return_vals)
    {
365
366
      GPProcReturn proc_return;

Elliot Lee's avatar
Elliot Lee committed
367
368
369
370
371
      proc_return.name = proc_run->name;

      if (proc_rec)
	{
	  proc_return.nparams = proc_rec->num_values + 1;
372
373
374
	  proc_return.params  = plug_in_args_to_params (return_vals,
                                                        proc_return.nparams,
                                                        FALSE);
Elliot Lee's avatar
Elliot Lee committed
375
376
377
378
	}
      else
	{
	  proc_return.nparams = 1;
379
	  proc_return.params  = plug_in_args_to_params (return_vals, 1, FALSE);
Elliot Lee's avatar
Elliot Lee committed
380
381
	}

382
      if (! gp_proc_return_write (plug_in->my_write, &proc_return, plug_in))
Elliot Lee's avatar
Elliot Lee committed
383
	{
384
	  g_warning ("plug_in_handle_proc_run: ERROR");
385
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
386
387
388
389
	  return;
	}

      plug_in_args_destroy (args, proc_run->nparams, FALSE);
390
391
392
393
394
395

      if (proc_rec)
        plug_in_args_destroy (return_vals, proc_rec->num_values + 1, TRUE);
      else
        plug_in_args_destroy (return_vals, 1, TRUE);

Elliot Lee's avatar
Elliot Lee committed
396
397
398
399
      plug_in_params_destroy (proc_return.params, proc_return.nparams, FALSE);
    }
  else
    {
400
401
      PlugInBlocked *blocked;

402
403
      blocked = g_new0 (PlugInBlocked, 1);

404
      blocked->plug_in   = plug_in;
Elliot Lee's avatar
Elliot Lee committed
405
      blocked->proc_name = g_strdup (proc_run->name);
406

Elliot Lee's avatar
Elliot Lee committed
407
408
409
410
411
      blocked_plug_ins = g_slist_prepend (blocked_plug_ins, blocked);
    }
}

static void
412
413
plug_in_handle_proc_return_priv (PlugIn       *plug_in,
                                 GPProcReturn *proc_return)
Elliot Lee's avatar
Elliot Lee committed
414
{
415
  if (plug_in->recurse_main_loop || plug_in->temp_main_loops)
Elliot Lee's avatar
Elliot Lee committed
416
    {
417
418
419
420
      plug_in->return_vals = plug_in_params_to_args (proc_return->params,
                                                     proc_return->nparams,
                                                     TRUE);
      plug_in->n_return_vals = proc_return->nparams;
Elliot Lee's avatar
Elliot Lee committed
421
422
423
    }
  else
    {
424
425
426
      GSList *list;

      for (list = blocked_plug_ins; list; list = g_slist_next (list))
Elliot Lee's avatar
Elliot Lee committed
427
	{
428
429
430
          PlugInBlocked *blocked;

	  blocked = (PlugInBlocked *) list->data;
Elliot Lee's avatar
Elliot Lee committed
431

432
	  if (blocked->proc_name && proc_return->name &&
Sven Neumann's avatar
Sven Neumann committed
433
	      strcmp (blocked->proc_name, proc_return->name) == 0)
Elliot Lee's avatar
Elliot Lee committed
434
	    {
435
	      if (! gp_proc_return_write (blocked->plug_in->my_write,
436
437
                                          proc_return,
                                          blocked->plug_in))
Elliot Lee's avatar
Elliot Lee committed
438
		{
439
		  g_message ("plug_in_handle_proc_run: ERROR");
440
		  plug_in_close (blocked->plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
441
442
		  return;
		}
443

Elliot Lee's avatar
Elliot Lee committed
444
445
446
447
448
449
450
451
452
	      blocked_plug_ins = g_slist_remove (blocked_plug_ins, blocked);
	      g_free (blocked->proc_name);
	      g_free (blocked);
	      break;
	    }
	}
    }
}

453
454
455
456
457
458
static void
plug_in_handle_proc_return (PlugIn       *plug_in,
                            GPProcReturn *proc_return)
{
  plug_in_handle_proc_return_priv (plug_in, proc_return);

459
460
  if (plug_in->recurse_main_loop)
    g_main_loop_quit (plug_in->recurse_main_loop);
461
462
463
464
465
466
467
468
469

  plug_in_close (plug_in, FALSE);
}

#ifdef ENABLE_TEMP_RETURN
static void
plug_in_handle_temp_proc_return (PlugIn       *plug_in,
                                 GPProcReturn *proc_return)
{
470
  if (plug_in->temp_main_loops)
471
472
473
474
475
476
    {
      plug_in_handle_proc_return_priv (plug_in, proc_return);

      plug_in_main_loop_quit (plug_in);
    }
  else
477
478
    {
      g_warning ("plug_in_handle_temp_proc_return: "
479
480
                 "received a temp_proc_return mesage while not running "
                 "a temp proc (should not happen)");
481
482
483
484
485
      plug_in_close (plug_in, TRUE);
    }
}
#endif

Elliot Lee's avatar
Elliot Lee committed
486
static void
487
plug_in_handle_proc_install (PlugIn        *plug_in,
488
                             GPProcInstall *proc_install)
Elliot Lee's avatar
Elliot Lee committed
489
{
490
  PlugInDef     *plug_in_def = NULL;
491
492
493
494
495
  PlugInProcDef *proc_def    = NULL;
  ProcRecord    *proc        = NULL;
  GSList        *tmp         = NULL;
  gchar         *prog        = NULL;
  gboolean       valid_utf8  = FALSE;
496
  gint           i;
Elliot Lee's avatar
Elliot Lee committed
497

498
  /*  Argument checking
Elliot Lee's avatar
Elliot Lee committed
499
500
501
502
503
504
505
506
   *   --only sanity check arguments when the procedure requests a menu path
   */

  if (proc_install->menu_path)
    {
      if (strncmp (proc_install->menu_path, "<Toolbox>", 9) == 0)
	{
	  if ((proc_install->nparams < 1) ||
507
	      (proc_install->params[0].type != GIMP_PDB_INT32))
Elliot Lee's avatar
Elliot Lee committed
508
	    {
509
	      g_message ("Plug-In \"%s\"\n(%s)\n"
510
			 "attempted to install <Toolbox> procedure \"%s\" "
511
512
513
			 "which does not take the standard <Toolbox> Plug-In "
                         "args.\n"
                         "(INT32)",
514
515
			 plug_in->name,
			 plug_in->prog,
516
			 proc_install->name);
Elliot Lee's avatar
Elliot Lee committed
517
518
519
520
521
522
	      return;
	    }
	}
      else if (strncmp (proc_install->menu_path, "<Image>", 7) == 0)
	{
	  if ((proc_install->nparams < 3) ||
523
524
525
	      (proc_install->params[0].type != GIMP_PDB_INT32) ||
	      (proc_install->params[1].type != GIMP_PDB_IMAGE) ||
	      (proc_install->params[2].type != GIMP_PDB_DRAWABLE))
Elliot Lee's avatar
Elliot Lee committed
526
	    {
527
	      g_message ("Plug-In \"%s\"\n(%s)\n"
528
			 "attempted to install <Image> procedure \"%s\" "
529
530
531
			 "which does not take the standard <Image> Plug-In "
                         "args.\n"
                         "(INT32, IMAGE, DRAWABLE)",
532
533
			 plug_in->name,
			 plug_in->prog,
534
			 proc_install->name);
Elliot Lee's avatar
Elliot Lee committed
535
536
537
538
539
540
	      return;
	    }
	}
      else if (strncmp (proc_install->menu_path, "<Load>", 6) == 0)
	{
	  if ((proc_install->nparams < 3) ||
541
542
543
	      (proc_install->params[0].type != GIMP_PDB_INT32) ||
	      (proc_install->params[1].type != GIMP_PDB_STRING) ||
	      (proc_install->params[2].type != GIMP_PDB_STRING))
Elliot Lee's avatar
Elliot Lee committed
544
	    {
545
	      g_message ("Plug-In \"%s\"\n(%s)\n"
546
			 "attempted to install <Load> procedure \"%s\" "
547
548
549
			 "which does not take the standard <Load> Plug-In "
                         "args.\n"
                         "(INT32, STRING, STRING)",
550
551
			 plug_in->name,
			 plug_in->prog,
552
			 proc_install->name);
Elliot Lee's avatar
Elliot Lee committed
553
554
555
556
557
558
	      return;
	    }
	}
      else if (strncmp (proc_install->menu_path, "<Save>", 6) == 0)
	{
	  if ((proc_install->nparams < 5) ||
559
560
	      (proc_install->params[0].type != GIMP_PDB_INT32)    ||
	      (proc_install->params[1].type != GIMP_PDB_IMAGE)    ||
561
	      (proc_install->params[2].type != GIMP_PDB_DRAWABLE) ||
562
	      (proc_install->params[3].type != GIMP_PDB_STRING)   ||
563
	      (proc_install->params[4].type != GIMP_PDB_STRING))
Elliot Lee's avatar
Elliot Lee committed
564
	    {
565
	      g_message ("Plug-In \"%s\"\n(%s)\n"
566
			 "attempted to install <Save> procedure \"%s\" "
567
568
569
			 "which does not take the standard <Save> Plug-In "
                         "args.\n"
                         "(INT32, IMAGE, DRAWABLE, STRING, STRING)",
570
571
			 plug_in->name,
			 plug_in->prog,
572
			 proc_install->name);
Elliot Lee's avatar
Elliot Lee committed
573
574
575
576
577
	      return;
	    }
	}
      else
	{
578
	  g_message ("Plug-In \"%s\"\n(%s)\n"
579
		     "attempted to install procedure \"%s\" "
580
581
		     "in an invalid menu location.\n"
		     "Use either \"<Toolbox>\", \"<Image>\", "
Sven Neumann's avatar
Sven Neumann committed
582
		     "\"<Load>\", or \"<Save>\".",
583
584
		     plug_in->name,
		     plug_in->prog,
585
		     proc_install->name);
Elliot Lee's avatar
Elliot Lee committed
586
587
588
589
	  return;
	}
    }

590
  /*  Sanity check for array arguments  */
Elliot Lee's avatar
Elliot Lee committed
591

592
  for (i = 1; i < proc_install->nparams; i++)
Elliot Lee's avatar
Elliot Lee committed
593
    {
594
      if ((proc_install->params[i].type == GIMP_PDB_INT32ARRAY ||
595
	   proc_install->params[i].type == GIMP_PDB_INT8ARRAY  ||
596
	   proc_install->params[i].type == GIMP_PDB_FLOATARRAY ||
597
	   proc_install->params[i].type == GIMP_PDB_STRINGARRAY)
598
          &&
599
	  proc_install->params[i-1].type != GIMP_PDB_INT32)
Elliot Lee's avatar
Elliot Lee committed
600
	{
601
	  g_message ("Plug-In \"%s\"\n(%s)\n"
602
603
604
		     "attempted to install procedure \"%s\" "
		     "which fails to comply with the array parameter "
		     "passing standard.  Argument %d is noncompliant.",
605
606
		     plug_in->name,
		     plug_in->prog,
607
		     proc_install->name, i);
Elliot Lee's avatar
Elliot Lee committed
608
609
610
	  return;
	}
    }
611
612
613

  /*  Sanity check strings for UTF-8 validity  */

614
  if ((proc_install->menu_path == NULL ||
615
616
       g_utf8_validate (proc_install->menu_path, -1, NULL)) &&
      (g_utf8_validate (proc_install->name, -1, NULL))      &&
617
      (proc_install->blurb == NULL ||
618
       g_utf8_validate (proc_install->blurb, -1, NULL))     &&
619
      (proc_install->help == NULL ||
620
621
622
       g_utf8_validate (proc_install->help, -1, NULL))      &&
      (proc_install->author == NULL ||
       g_utf8_validate (proc_install->author, -1, NULL))    &&
623
      (proc_install->copyright == NULL ||
624
625
626
627
       g_utf8_validate (proc_install->copyright, -1, NULL)) &&
      (proc_install->date == NULL ||
       g_utf8_validate (proc_install->date, -1, NULL)))
    {
628
      valid_utf8 = TRUE;
629

630
      for (i = 0; i < proc_install->nparams && valid_utf8; i++)
631
632
        {
          if (! (g_utf8_validate (proc_install->params[i].name, -1, NULL) &&
633
                 (proc_install->params[i].description == NULL ||
634
                  g_utf8_validate (proc_install->params[i].description, -1, NULL))))
635
            valid_utf8 = FALSE;
636
        }
637

638
      for (i = 0; i < proc_install->nreturn_vals && valid_utf8; i++)
639
640
641
642
        {
          if (! (g_utf8_validate (proc_install->return_vals[i].name, -1, NULL) &&
                 (proc_install->return_vals[i].description == NULL ||
                  g_utf8_validate (proc_install->return_vals[i].description, -1, NULL))))
643
            valid_utf8 = FALSE;
644
645
        }
    }
646

647
  if (! valid_utf8)
648
649
    {
      g_message ("Plug-In \"%s\"\n(%s)\n"
650
                 "attempted to install a procedure with invalid UTF-8 strings.",
651
652
                 plug_in->name,
                 plug_in->prog);
653
654
      return;
    }
Elliot Lee's avatar
Elliot Lee committed
655

656
657
  /*  Initialization  */

Elliot Lee's avatar
Elliot Lee committed
658
659
  switch (proc_install->type)
    {
660
661
    case GIMP_PLUGIN:
    case GIMP_EXTENSION:
662
663
      plug_in_def = plug_in->plug_in_def;
      prog        = plug_in_def->prog;
Elliot Lee's avatar
Elliot Lee committed
664
665
666
667

      tmp = plug_in_def->proc_defs;
      break;

668
    case GIMP_TEMPORARY:
669
670
      plug_in_def = NULL;
      prog        = "none";
Elliot Lee's avatar
Elliot Lee committed
671

672
      tmp = plug_in->temp_proc_defs;
Elliot Lee's avatar
Elliot Lee committed
673
674
675
676
677
678
679
680
681
682
      break;
    }

  while (tmp)
    {
      proc_def = tmp->data;
      tmp = tmp->next;

      if (strcmp (proc_def->db_info.name, proc_install->name) == 0)
	{
683
          switch (proc_install->type)
684
            {
685
686
            case GIMP_PLUGIN:
            case GIMP_EXTENSION:
687
688
689
              plug_in_def->proc_defs = g_slist_remove (plug_in_def->proc_defs,
                                                       proc_def);
              plug_in_proc_def_free (proc_def);
690
691
692
693
694
695
696
              break;

            case GIMP_TEMPORARY:
              plug_in->temp_proc_defs = g_slist_remove (plug_in->temp_proc_defs,
                                                        proc_def);
              plug_ins_temp_proc_def_remove (plug_in->gimp, proc_def);
              break;
697
            }
698

Elliot Lee's avatar
Elliot Lee committed
699
700
701
702
	  break;
	}
    }

703
  proc_def = plug_in_proc_def_new ();
Elliot Lee's avatar
Elliot Lee committed
704

705
  proc_def->prog            = g_strdup (prog);
706
707
708
709
710
711
  proc_def->menu_path       = g_strdup (proc_install->menu_path);
  proc_def->accelerator     = NULL;
  proc_def->extensions      = NULL;
  proc_def->prefixes        = NULL;
  proc_def->magics          = NULL;
  proc_def->image_types     = g_strdup (proc_install->image_types);
712
  proc_def->image_types_val = plug_ins_image_types_parse (proc_def->image_types);
GMT 1999 Andy Thomas's avatar
GMT 1999 Andy Thomas committed
713
  /* Install temp one use todays time */
714
  proc_def->mtime           = time (NULL);
Elliot Lee's avatar
Elliot Lee committed
715

716
717
  /*  The procedural database procedure  */

718
719
  proc = &proc_def->db_info;

720
721
722
723
  proc->name      = g_strdup (proc_install->name);
  proc->blurb     = g_strdup (proc_install->blurb);
  proc->help      = g_strdup (proc_install->help);
  proc->author    = g_strdup (proc_install->author);
Elliot Lee's avatar
Elliot Lee committed
724
  proc->copyright = g_strdup (proc_install->copyright);
725
  proc->date      = g_strdup (proc_install->date);
Elliot Lee's avatar
Elliot Lee committed
726
727
  proc->proc_type = proc_install->type;

728
  proc->num_args   = proc_install->nparams;
Elliot Lee's avatar
Elliot Lee committed
729
730
  proc->num_values = proc_install->nreturn_vals;

731
732
  proc->args   = g_new0 (ProcArg, proc->num_args);
  proc->values = g_new0 (ProcArg, proc->num_values);
Elliot Lee's avatar
Elliot Lee committed
733
734
735

  for (i = 0; i < proc->num_args; i++)
    {
736
737
      proc->args[i].arg_type    = proc_install->params[i].type;
      proc->args[i].name        = g_strdup (proc_install->params[i].name);
Elliot Lee's avatar
Elliot Lee committed
738
739
740
741
742
      proc->args[i].description = g_strdup (proc_install->params[i].description);
    }

  for (i = 0; i < proc->num_values; i++)
    {
743
744
      proc->values[i].arg_type    = proc_install->return_vals[i].type;
      proc->values[i].name        = g_strdup (proc_install->return_vals[i].name);
Elliot Lee's avatar
Elliot Lee committed
745
746
747
748
749
      proc->values[i].description = g_strdup (proc_install->return_vals[i].description);
    }

  switch (proc_install->type)
    {
750
751
    case GIMP_PLUGIN:
    case GIMP_EXTENSION:
752
753
      plug_in_def->proc_defs = g_slist_prepend (plug_in_def->proc_defs,
                                                proc_def);
Elliot Lee's avatar
Elliot Lee committed
754
755
      break;

756
    case GIMP_TEMPORARY:
757
758
      plug_in->temp_proc_defs = g_slist_prepend (plug_in->temp_proc_defs,
                                                 proc_def);
Elliot Lee's avatar
Elliot Lee committed
759

760
      proc->exec_method.temporary.plug_in = plug_in;
Elliot Lee's avatar
Elliot Lee committed
761

762
      plug_ins_temp_proc_def_add (plug_in->gimp, proc_def);
Elliot Lee's avatar
Elliot Lee committed
763
764
765
766
767
      break;
    }
}

static void
768
plug_in_handle_proc_uninstall (PlugIn          *plug_in,
769
                               GPProcUninstall *proc_uninstall)
Elliot Lee's avatar
Elliot Lee committed
770
{
771
  GSList *tmp;
Elliot Lee's avatar
Elliot Lee committed
772

Michael Natterer's avatar
Michael Natterer committed
773
  for (tmp = plug_in->temp_proc_defs; tmp; tmp = g_slist_next (tmp))
Elliot Lee's avatar
Elliot Lee committed
774
    {
775
776
      PlugInProcDef *proc_def;

Elliot Lee's avatar
Elliot Lee committed
777
778
      proc_def = tmp->data;

Michael Natterer's avatar
Michael Natterer committed
779
      if (! strcmp (proc_def->db_info.name, proc_uninstall->name))
Elliot Lee's avatar
Elliot Lee committed
780
	{
781
782
	  plug_in->temp_proc_defs = g_slist_remove (plug_in->temp_proc_defs,
                                                    proc_def);
783
	  plug_ins_temp_proc_def_remove (plug_in->gimp, proc_def);
Elliot Lee's avatar
Elliot Lee committed
784
785
786
787
	  break;
	}
    }
}
788

Michael Natterer's avatar
Michael Natterer committed
789
790
791
static void
plug_in_handle_extension_ack (PlugIn *plug_in)
{
792
  if (plug_in->ext_main_loop)
793
    {
794
      g_main_loop_quit (plug_in->ext_main_loop);
795
796
797
798
799
800
801
802
    }
  else
    {
      g_warning ("plug_in_handle_extension_ack: "
		 "received an extension_ack message while not starting "
                 "an extension (should not happen)");
      plug_in_close (plug_in, TRUE);
    }
Michael Natterer's avatar
Michael Natterer committed
803
804
}

805
static void
806
plug_in_handle_has_init (PlugIn *plug_in)
807
{
808
  if (plug_in->query)
809
810
811
812
813
814
815
816
817
818
    {
      plug_in_def_set_has_init (plug_in->plug_in_def, TRUE);
    }
  else
    {
      g_warning ("plug_in_handle_has_init: "
		 "received a has_init message while not in query() "
                 "(should not happen)");
      plug_in_close (plug_in, TRUE);
    }
819
}