gimp-modules.c 5.54 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3 4 5 6
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimpmodules.c
 * (C) 1999 Austin Donnelly <austin@gimp.org>
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10 11 12 13 14 15 16 17
 * (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
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 20 21 22
 */

#include "config.h"

23
#include <gio/gio.h>
24 25

#include "libgimpbase/gimpbase.h"
26
#include "libgimpmodule/gimpmodule.h"
27
#include "libgimpconfig/gimpconfig.h"
28 29 30

#include "core-types.h"

31
#include "config/gimpcoreconfig.h"
32

33
#include "gimp.h"
34
#include "gimp-modules.h"
35

36
#include "gimp-intl.h"
37 38 39 40 41 42 43


void
gimp_modules_init (Gimp *gimp)
{
  g_return_if_fail (GIMP_IS_GIMP (gimp));

44 45 46 47 48
  if (! gimp->no_interface)
    {
      gimp->module_db = gimp_module_db_new (gimp->be_verbose);
      gimp->write_modulerc = FALSE;
    }
49 50 51 52 53 54 55
}

void
gimp_modules_exit (Gimp *gimp)
{
  g_return_if_fail (GIMP_IS_GIMP (gimp));

56
  g_clear_object (&gimp->module_db);
57 58 59 60 61
}

void
gimp_modules_load (Gimp *gimp)
{
62
  GFile    *file;
63 64
  GScanner *scanner;
  gchar    *module_load_inhibit = NULL;
65

66 67
  g_return_if_fail (GIMP_IS_GIMP (gimp));

68 69
  if (gimp->no_interface)
    return;
70

71
  gimp_module_db_set_verbose (gimp->module_db, gimp->be_verbose);
72

73
  file = gimp_directory_file ("modulerc", NULL);
74 75

  if (gimp->be_verbose)
76
    g_print ("Parsing '%s'\n", gimp_file_get_utf8_name (file));
77

78 79
  scanner = gimp_scanner_new_gfile (file, NULL);
  g_object_unref (file);
80

81 82 83 84 85
  if (scanner)
    {
      GTokenType  token;
      GError     *error = NULL;

86 87
#define MODULE_LOAD_INHIBIT 1

88 89
      g_scanner_scope_add_symbol (scanner, 0, "module-load-inhibit",
                                  GINT_TO_POINTER (MODULE_LOAD_INHIBIT));
90

91
      token = G_TOKEN_LEFT_PAREN;
92

93
      while (g_scanner_peek_next_token (scanner) == token)
94
        {
95
          token = g_scanner_get_next_token (scanner);
96

97
          switch (token)
98
            {
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
            case G_TOKEN_LEFT_PAREN:
              token = G_TOKEN_SYMBOL;
              break;

            case G_TOKEN_SYMBOL:
              if (scanner->value.v_symbol == GINT_TO_POINTER (MODULE_LOAD_INHIBIT))
                {
                  token = G_TOKEN_STRING;

                  if (! gimp_scanner_parse_string_no_validate (scanner,
                                                               &module_load_inhibit))
                    goto error;
                }
              token = G_TOKEN_RIGHT_PAREN;
              break;

            case G_TOKEN_RIGHT_PAREN:
              token = G_TOKEN_LEFT_PAREN;
              break;

            default: /* do nothing */
              break;
121 122 123 124 125
            }
        }

#undef MODULE_LOAD_INHIBIT

126 127 128 129 130 131
      if (token != G_TOKEN_LEFT_PAREN)
        {
          g_scanner_get_next_token (scanner);
          g_scanner_unexp_token (scanner, token, NULL, NULL, NULL,
                                 _("fatal parse error"), TRUE);
        }
132

133
    error:
134

135 136
      if (error)
        {
137
          gimp_message_literal (gimp, NULL, GIMP_MESSAGE_ERROR, error->message);
138 139
          g_clear_error (&error);
        }
140

141 142
      gimp_scanner_destroy (scanner);
    }
143 144 145 146 147 148 149

  if (module_load_inhibit)
    {
      gimp_module_db_set_load_inhibit (gimp->module_db, module_load_inhibit);
      g_free (module_load_inhibit);
    }

150
  gimp_module_db_load (gimp->module_db, gimp->config->module_path);
151 152 153
}

static void
Michael Natterer's avatar
Michael Natterer committed
154
add_to_inhibit_string (gpointer data,
155
                       gpointer user_data)
156
{
157 158
  GimpModule *module = data;
  GString    *str    = user_data;
159

160
  if (module->load_inhibit)
161
    {
162 163
      g_string_append_c (str, G_SEARCHPATH_SEPARATOR);
      g_string_append (str, module->filename);
164 165 166
    }
}

167 168
void
gimp_modules_unload (Gimp *gimp)
169
{
170
  g_return_if_fail (GIMP_IS_GIMP (gimp));
171

172
  if (! gimp->no_interface && gimp->write_modulerc)
173
    {
174 175
      GimpConfigWriter *writer;
      GString          *str;
176
      const gchar      *p;
177
      GFile            *file;
178
      GError           *error = NULL;
179 180

      str = g_string_new (NULL);
181 182
      g_list_foreach (gimp_module_db_get_modules (gimp->module_db),
                      add_to_inhibit_string, str);
183 184 185 186 187
      if (str->len > 0)
        p = str->str + 1;
      else
        p = "";

188
      file = gimp_directory_file ("modulerc", NULL);
189 190

      if (gimp->be_verbose)
191
        g_print ("Writing '%s'\n", gimp_file_get_utf8_name (file));
192

193 194 195
      writer = gimp_config_writer_new_gfile (file, TRUE,
                                             "GIMP modulerc", &error);
      g_object_unref (file);
196

197
      if (writer)
198
        {
199
          gimp_config_writer_open (writer, "module-load-inhibit");
200
          gimp_config_writer_string (writer, p);
201 202 203
          gimp_config_writer_close (writer);

          gimp_config_writer_finish (writer, "end of modulerc", &error);
204 205 206 207 208

          gimp->write_modulerc = FALSE;
        }

      g_string_free (str, TRUE);
209 210 211

      if (error)
        {
212
          gimp_message_literal (gimp, NULL, GIMP_MESSAGE_ERROR, error->message);
213 214
          g_clear_error (&error);
        }
215 216 217
    }
}

218 219
void
gimp_modules_refresh (Gimp *gimp)
220
{
221
  g_return_if_fail (GIMP_IS_GIMP (gimp));
222

223 224
  if (! gimp->no_interface)
    {
225
      gimp_module_db_refresh (gimp->module_db, gimp->config->module_path);
226
    }
227
}