File: gimpmodule.c

package info (click to toggle)
gimp 3.0.4-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 210,076 kB
  • sloc: ansic: 842,287; lisp: 10,761; python: 10,318; cpp: 7,238; perl: 4,355; sh: 1,043; xml: 963; yacc: 609; lex: 348; javascript: 150; makefile: 43
file content (701 lines) | stat: -rw-r--r-- 18,457 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * gimpmodule.c
 * (C) 1999 Austin Donnelly <austin@gimp.org>
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <string.h>

#include <gio/gio.h>

#include "libgimpbase/gimpbase.h"

#include "gimpmodule.h"

#include "libgimp/libgimp-intl.h"


/**
 * SECTION: gimpmodule
 * @title: GimpModule
 * @short_description: A #GTypeModule subclass which implements module
 *                     loading using #GModule.
 * @see_also: #GModule, #GTypeModule
 *
 * #GimpModule is a generic mechanism to dynamically load modules into
 * GIMP. It is a #GTypeModule subclass, implementing module loading
 * using #GModule.  #GimpModule does not know which functionality is
 * implemented by the modules, it just provides a framework to get
 * arbitrary #GType implementations loaded from disk.
 **/


enum
{
  PROP_0,
  PROP_AUTO_LOAD,
  PROP_ON_DISK,
  N_PROPS
};
static GParamSpec *obj_props[N_PROPS] = { NULL, };

typedef struct _GimpModulePrivate
{
  GFile           *file;       /* path to the module                       */
  gboolean         auto_load;  /* auto-load the module on creation         */
  gboolean         verbose;    /* verbose error reporting                  */

  GimpModuleInfo  *info;       /* returned values from module_query        */
  GimpModuleState  state;      /* what's happened to the module            */
  gchar           *last_error;

  gboolean         on_disk;    /* TRUE if file still exists                */

  GModule         *module;     /* handle on the module                     */

  GimpModuleQueryFunc     query_module;
  GimpModuleRegisterFunc  register_module;
} GimpModulePrivate;

#define GET_PRIVATE(obj) ((GimpModulePrivate *) gimp_module_get_instance_private ((GimpModule *) (obj)))


static void       gimp_module_get_property   (GObject      *object,
                                              guint         property_id,
                                              GValue       *value,
                                              GParamSpec   *pspec);
static void       gimp_module_set_property   (GObject      *object,
                                              guint         property_id,
                                              const GValue *value,
                                              GParamSpec   *pspec);
static void       gimp_module_finalize       (GObject      *object);

static gboolean   gimp_module_load           (GTypeModule  *module);
static void       gimp_module_unload         (GTypeModule  *module);

static gboolean   gimp_module_open           (GimpModule   *module);
static gboolean   gimp_module_close          (GimpModule   *module);
static void       gimp_module_set_last_error (GimpModule   *module,
                                              const gchar  *error_str);

static GimpModuleInfo * gimp_module_info_new  (guint32               abi_version,
                                               const gchar          *purpose,
                                               const gchar          *author,
                                               const gchar          *version,
                                               const gchar          *copyright,
                                               const gchar          *date);
static GimpModuleInfo * gimp_module_info_copy (const GimpModuleInfo *info);
static void             gimp_module_info_free (GimpModuleInfo       *info);


G_DEFINE_TYPE_WITH_PRIVATE (GimpModule, gimp_module, G_TYPE_TYPE_MODULE)

#define parent_class gimp_module_parent_class


static void
gimp_module_class_init (GimpModuleClass *klass)
{
  GObjectClass     *object_class = G_OBJECT_CLASS (klass);
  GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (klass);

  object_class->set_property = gimp_module_set_property;
  object_class->get_property = gimp_module_get_property;
  object_class->finalize = gimp_module_finalize;

  obj_props[PROP_AUTO_LOAD] = g_param_spec_boolean ("auto-load", "auto-load", "auto-load",
                                                    FALSE, GIMP_PARAM_READWRITE);
  obj_props[PROP_ON_DISK] = g_param_spec_boolean ("on-disk", "on-disk", "on-disk",
                                                   FALSE, GIMP_PARAM_READABLE);
  g_object_class_install_properties (object_class, N_PROPS, obj_props);

  module_class->load     = gimp_module_load;
  module_class->unload   = gimp_module_unload;
}

static void
gimp_module_init (GimpModule *module)
{
  GET_PRIVATE (module)->state = GIMP_MODULE_STATE_ERROR;
}

static void
gimp_module_set_property (GObject      *object,
                          guint         property_id,
                          const GValue *value,
                          GParamSpec   *pspec)
{
  GimpModule *module = GIMP_MODULE (object);

  switch (property_id)
    {
    case PROP_AUTO_LOAD:
      gimp_module_set_auto_load (module, g_value_get_boolean (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_module_get_property (GObject    *object,
                          guint       property_id,
                          GValue     *value,
                          GParamSpec *pspec)
{
  GimpModule *module = GIMP_MODULE (object);

  switch (property_id)
    {
    case PROP_AUTO_LOAD:
      g_value_set_boolean (value, gimp_module_get_auto_load (module));
      break;

    case PROP_ON_DISK:
      g_value_set_boolean (value, gimp_module_is_on_disk (module));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_module_finalize (GObject *object)
{
  GimpModule        *module = GIMP_MODULE (object);
  GimpModulePrivate *priv   = GET_PRIVATE (module);

  g_clear_object  (&priv->file);
  g_clear_pointer (&priv->info, gimp_module_info_free);
  g_clear_pointer (&priv->last_error, g_free);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static gboolean
gimp_module_load (GTypeModule *module)
{
  GimpModule        *gimp_module = GIMP_MODULE (module);
  GimpModulePrivate *priv        = GET_PRIVATE (gimp_module);
  gpointer    func;

  g_return_val_if_fail (priv->file != NULL, FALSE);
  g_return_val_if_fail (priv->module == NULL, FALSE);

  if (priv->verbose)
    g_print ("Loading module '%s'\n",
             gimp_file_get_utf8_name (priv->file));

  if (! gimp_module_open (gimp_module))
    return FALSE;

  if (! gimp_module_query_module (gimp_module))
    return FALSE;

  /* find the gimp_module_register symbol */
  if (! g_module_symbol (priv->module, "gimp_module_register",
                         &func))
    {
      gimp_module_set_last_error (gimp_module,
                                  "Missing gimp_module_register() symbol");

      g_message (_("Module '%s' load error: %s"),
                 gimp_file_get_utf8_name (priv->file),
                 priv->last_error);

      gimp_module_close (gimp_module);

      priv->state = GIMP_MODULE_STATE_ERROR;

      return FALSE;
    }

  priv->register_module = func;

  if (! priv->register_module (module))
    {
      gimp_module_set_last_error (gimp_module,
                                  "gimp_module_register() returned FALSE");

      g_message (_("Module '%s' load error: %s"),
                 gimp_file_get_utf8_name (priv->file),
                 priv->last_error);

      gimp_module_close (gimp_module);

      priv->state = GIMP_MODULE_STATE_LOAD_FAILED;

      return FALSE;
    }

  priv->state = GIMP_MODULE_STATE_LOADED;

  return TRUE;
}

static void
gimp_module_unload (GTypeModule *module)
{
  GimpModule        *gimp_module = GIMP_MODULE (module);
  GimpModulePrivate *priv        = GET_PRIVATE (gimp_module);

  g_return_if_fail (priv->module != NULL);

  if (priv->verbose)
    g_print ("Unloading module '%s'\n",
             gimp_file_get_utf8_name (priv->file));

  gimp_module_close (gimp_module);
}


/*  public functions  */

/**
 * gimp_module_new:
 * @file:      A #GFile pointing to a loadable module.
 * @auto_load: Pass %TRUE to exclude this module from auto-loading.
 * @verbose:   Pass %TRUE to enable debugging output.
 *
 * Creates a new #GimpModule instance.
 *
 * Returns: The new #GimpModule object.
 **/
GimpModule *
gimp_module_new (GFile    *file,
                 gboolean  auto_load,
                 gboolean  verbose)
{
  GimpModule        *module;
  GimpModulePrivate *priv;

  g_return_val_if_fail (G_IS_FILE (file), NULL);
  g_return_val_if_fail (g_file_is_native (file), NULL);

  module = g_object_new (GIMP_TYPE_MODULE, NULL);
  priv   = GET_PRIVATE (module);

  priv->file      = g_object_ref (file);
  priv->auto_load = auto_load ? TRUE : FALSE;
  priv->verbose   = verbose ? TRUE : FALSE;

  if (priv->auto_load)
    {
      if (gimp_module_load (G_TYPE_MODULE (module)))
        gimp_module_unload (G_TYPE_MODULE (module));
    }
  else
    {
      if (verbose)
        g_print ("Skipping module '%s'\n",
                 gimp_file_get_utf8_name (file));

      priv->state = GIMP_MODULE_STATE_NOT_LOADED;
    }

  return module;
}

/**
 * gimp_module_get_file:
 * @module: A #GimpModule
 *
 * Returns #GFile of the @module,
 *
 * Returns: (transfer none): The @module's #GFile.
 *
 * Since: 3.0
 **/
GFile *
gimp_module_get_file (GimpModule *module)
{
  g_return_val_if_fail (GIMP_IS_MODULE (module), NULL);

  return GET_PRIVATE (module)->file;
}

/**
 * gimp_module_set_auto_load:
 * @module:    A #GimpModule
 * @auto_load: Pass %FALSE to exclude this module from auto-loading
 *
 * Sets the @auto_load property of the module
 *
 * Since: 3.0
 **/
void
gimp_module_set_auto_load (GimpModule *module,
                           gboolean    auto_load)
{
  GimpModulePrivate *priv;

  g_return_if_fail (GIMP_IS_MODULE (module));

  priv = GET_PRIVATE (module);

  if (auto_load != priv->auto_load)
    {
      priv->auto_load = auto_load;

      g_object_notify_by_pspec (G_OBJECT (module), obj_props[PROP_AUTO_LOAD]);
    }
}

/**
 * gimp_module_get_auto_load:
 * @module: A #GimpModule
 *
 * Returns whether this @module in automatically loaded at startup.
 *
 * Returns: The @module's 'auto_load' property.
 *
 * Since: 3.0
 **/
gboolean
gimp_module_get_auto_load (GimpModule *module)
{
  g_return_val_if_fail (GIMP_IS_MODULE (module), FALSE);

  return GET_PRIVATE (module)->auto_load;
}

/**
 * gimp_module_is_on_disk:
 * @module: A #GimpModule
 *
 * Returns: Whether the @module is present on diak.
 *
 * Since: 3.0
 **/
gboolean
gimp_module_is_on_disk (GimpModule *module)
{
  GimpModulePrivate *priv;
  gboolean           old_on_disk;

  g_return_val_if_fail (GIMP_IS_MODULE (module), FALSE);

  priv = GET_PRIVATE (module);

  old_on_disk = priv->on_disk;

  priv->on_disk =
    (g_file_query_file_type (priv->file,
                             G_FILE_QUERY_INFO_NONE, NULL) ==
     G_FILE_TYPE_REGULAR);

  if (priv->on_disk != old_on_disk)
    g_object_notify_by_pspec (G_OBJECT (module), obj_props[PROP_ON_DISK]);

  return priv->on_disk;
}

/**
 * gimp_module_is_loaded:
 * @module: A #GimpModule
 *
 * Returns: Whether the @module is currently loaded.
 *
 * Since: 3.0
 **/
gboolean
gimp_module_is_loaded (GimpModule *module)
{
  g_return_val_if_fail (GIMP_IS_MODULE (module), FALSE);

  return GET_PRIVATE (module)->module != NULL;
}

/**
 * gimp_module_get_info:
 * @module: A #GimpModule
 *
 * Returns: (transfer none): The @module's #GimpModuleInfo as provided
 *          by the actual module, or %NULL.
 *
 * Since: 3.0
 **/
const GimpModuleInfo *
gimp_module_get_info (GimpModule *module)
{
  g_return_val_if_fail (GIMP_IS_MODULE (module), NULL);

  return GET_PRIVATE (module)->info;
}

/**
 * gimp_module_get_state:
 * @module: A #GimpModule
 *
 * Returns: The @module's state.
 *
 * Since: 3.0
 **/
GimpModuleState
gimp_module_get_state (GimpModule *module)
{
  g_return_val_if_fail (GIMP_IS_MODULE (module), GIMP_MODULE_STATE_ERROR);

  return GET_PRIVATE (module)->state;
}

/**
 * gimp_module_get_last_error:
 * @module: A #GimpModule
 *
 * Returns: The @module's last error message.
 *
 * Since: 3.0
 **/
const gchar *
gimp_module_get_last_error (GimpModule *module)
{
  g_return_val_if_fail (GIMP_IS_MODULE (module), NULL);

  return GET_PRIVATE (module)->last_error;
}

/**
 * gimp_module_query_module:
 * @module: A #GimpModule.
 *
 * Queries the module without actually registering any of the types it
 * may implement. After successful query, gimp_module_get_info() can be
 * used to get further about the module.
 *
 * Returns: %TRUE on success.
 **/
gboolean
gimp_module_query_module (GimpModule *module)
{
  const GimpModuleInfo *info;
  GimpModulePrivate    *priv;
  gboolean              close_module = FALSE;
  gpointer              func;

  g_return_val_if_fail (GIMP_IS_MODULE (module), FALSE);

  priv = GET_PRIVATE (module);

  if (! priv->module)
    {
      if (! gimp_module_open (module))
        return FALSE;

      close_module = TRUE;
    }

  /* find the gimp_module_query symbol */
  if (! g_module_symbol (priv->module, "gimp_module_query", &func))
    {
      gimp_module_set_last_error (module,
                                  "Missing gimp_module_query() symbol");

      g_message (_("Module '%s' load error: %s"),
                 gimp_file_get_utf8_name (priv->file),
                 priv->last_error);

      gimp_module_close (module);

      priv->state = GIMP_MODULE_STATE_ERROR;
      return FALSE;
    }

  priv->query_module = func;

  g_clear_pointer (&priv->info, gimp_module_info_free);

  info = priv->query_module (G_TYPE_MODULE (module));

  if (! info || info->abi_version != GIMP_MODULE_ABI_VERSION)
    {
      gimp_module_set_last_error (module,
                                  info ?
                                  "module ABI version does not match" :
                                  "gimp_module_query() returned NULL");

      g_message (_("Module '%s' load error: %s"),
                 gimp_file_get_utf8_name (priv->file),
                 priv->last_error);

      gimp_module_close (module);

      priv->state = GIMP_MODULE_STATE_ERROR;
      return FALSE;
    }

  priv->info = gimp_module_info_copy (info);

  if (close_module)
    return gimp_module_close (module);

  return TRUE;
}

/**
 * gimp_module_error_quark:
 *
 * This function is never called directly. Use GIMP_MODULE_ERROR() instead.
 *
 * Returns: the #GQuark that defines the GIMP module error domain.
 *
 * Since: 2.8
 **/
GQuark
gimp_module_error_quark (void)
{
  return g_quark_from_static_string ("gimp-module-error-quark");
}


/*  private functions  */

static gboolean
gimp_module_open (GimpModule *module)
{
  GimpModulePrivate *priv = GET_PRIVATE (module);
  gchar             *path = g_file_get_path (priv->file);

  priv->module = g_module_open (path, 0);

  g_free (path);

  if (! priv->module)
    {
      priv->state = GIMP_MODULE_STATE_ERROR;
      gimp_module_set_last_error (module, g_module_error ());

      g_message (_("Module '%s' load error: %s"),
                 gimp_file_get_utf8_name (priv->file),
                 priv->last_error);

      return FALSE;
    }

  return TRUE;
}

static gboolean
gimp_module_close (GimpModule *module)
{
  GimpModulePrivate *priv = GET_PRIVATE (module);

  g_module_close (priv->module); /* FIXME: error handling */
  priv->module          = NULL;
  priv->query_module    = NULL;
  priv->register_module = NULL;

  priv->state = GIMP_MODULE_STATE_NOT_LOADED;

  return TRUE;
}

static void
gimp_module_set_last_error (GimpModule  *module,
                            const gchar *error_str)
{
  GimpModulePrivate *priv = GET_PRIVATE (module);

  if (priv->last_error)
    g_free (priv->last_error);

  priv->last_error = g_strdup (error_str);
}


/*  GimpModuleInfo functions  */

/**
 * gimp_module_info_new:
 * @abi_version: The #GIMP_MODULE_ABI_VERSION the module was compiled against.
 * @purpose:     The module's general purpose.
 * @author:      The module's author.
 * @version:     The module's version.
 * @copyright:   The module's copyright.
 * @date:        The module's release date.
 *
 * Creates a newly allocated #GimpModuleInfo struct.
 *
 * Returns: The new #GimpModuleInfo struct.
 **/
static GimpModuleInfo *
gimp_module_info_new (guint32      abi_version,
                      const gchar *purpose,
                      const gchar *author,
                      const gchar *version,
                      const gchar *copyright,
                      const gchar *date)
{
  GimpModuleInfo *info = g_slice_new0 (GimpModuleInfo);

  info->abi_version = abi_version;
  info->purpose     = g_strdup (purpose);
  info->author      = g_strdup (author);
  info->version     = g_strdup (version);
  info->copyright   = g_strdup (copyright);
  info->date        = g_strdup (date);

  return info;
}

/**
 * gimp_module_info_copy:
 * @info: The #GimpModuleInfo struct to copy.
 *
 * Copies a #GimpModuleInfo struct.
 *
 * Returns: The new copy.
 **/
static GimpModuleInfo *
gimp_module_info_copy (const GimpModuleInfo *info)
{
  g_return_val_if_fail (info != NULL, NULL);

  return gimp_module_info_new (info->abi_version,
                               info->purpose,
                               info->author,
                               info->version,
                               info->copyright,
                               info->date);
}

/**
 * gimp_module_info_free:
 * @info: The #GimpModuleInfo struct to free
 *
 * Frees the passed #GimpModuleInfo.
 **/
static void
gimp_module_info_free (GimpModuleInfo *info)
{
  g_return_if_fail (info != NULL);

  g_free (info->purpose);
  g_free (info->author);
  g_free (info->version);
  g_free (info->copyright);
  g_free (info->date);

  g_slice_free (GimpModuleInfo, info);
}