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

Tomas Ogren's avatar
Tomas Ogren committed
18
#include "config.h"
Michael Natterer's avatar
Michael Natterer committed
19

20 21 22 23 24 25
#include <errno.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

26
#include <gegl.h>
27
#include <glib/gstdio.h>
Sven Neumann's avatar
Sven Neumann committed
28

29
#include "libgimpbase/gimpbase.h"
30 31 32
#ifdef G_OS_WIN32
#include "libgimpbase/gimpwin32-io.h" /* For S_IRGRP etc */
#endif
33
#include "libgimpconfig/gimpconfig.h"
34

35
#include "core-types.h"
Sven Neumann's avatar
Sven Neumann committed
36

37
#include "gimp.h"
38
#include "gimperror.h"
39 40
#include "gimptoolinfo.h"
#include "gimptooloptions.h"
41

42 43
#include "gimp-intl.h"

44

45
enum
46
{
47
  PROP_0,
48
  PROP_TOOL,
49 50 51 52
  PROP_TOOL_INFO
};


53
static void   gimp_tool_options_config_iface_init (GimpConfigInterface *iface);
54

55 56 57 58 59 60 61 62 63
static void   gimp_tool_options_dispose           (GObject         *object);
static void   gimp_tool_options_set_property      (GObject         *object,
                                                   guint            property_id,
                                                   const GValue    *value,
                                                   GParamSpec      *pspec);
static void   gimp_tool_options_get_property      (GObject         *object,
                                                   guint            property_id,
                                                   GValue          *value,
                                                   GParamSpec      *pspec);
64

65
static void   gimp_tool_options_real_reset        (GimpToolOptions *tool_options);
66

67
static void   gimp_tool_options_config_reset      (GimpConfig      *config);
68

69 70 71 72 73 74 75
static void   gimp_tool_options_tool_notify       (GimpToolOptions *options,
                                                   GParamSpec      *pspec);


G_DEFINE_TYPE_WITH_CODE (GimpToolOptions, gimp_tool_options, GIMP_TYPE_CONTEXT,
                         G_IMPLEMENT_INTERFACE (GIMP_TYPE_CONFIG,
                                                gimp_tool_options_config_iface_init))
76

77 78
#define parent_class gimp_tool_options_parent_class

79

80
static void
81 82
gimp_tool_options_class_init (GimpToolOptionsClass *klass)
{
83
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
84

85
  object_class->dispose      = gimp_tool_options_dispose;
86 87 88 89 90
  object_class->set_property = gimp_tool_options_set_property;
  object_class->get_property = gimp_tool_options_get_property;

  klass->reset               = gimp_tool_options_real_reset;

91 92
  g_object_class_override_property (object_class, PROP_TOOL, "tool");

93 94 95 96
  g_object_class_install_property (object_class, PROP_TOOL_INFO,
                                   g_param_spec_object ("tool-info",
                                                        NULL, NULL,
                                                        GIMP_TYPE_TOOL_INFO,
97
                                                        GIMP_PARAM_READWRITE));
98

99 100
}

101 102 103
static void
gimp_tool_options_init (GimpToolOptions *options)
{
104
  options->tool_info = NULL;
105 106 107 108

  g_signal_connect (options, "notify::tool",
                    G_CALLBACK (gimp_tool_options_tool_notify),
                    NULL);
109 110
}

111 112 113 114 115 116
static void
gimp_tool_options_config_iface_init (GimpConfigInterface *iface)
{
  iface->reset = gimp_tool_options_config_reset;
}

117 118 119 120 121 122 123 124 125 126 127 128 129 130
static void
gimp_tool_options_dispose (GObject *object)
{
  GimpToolOptions *options = GIMP_TOOL_OPTIONS (object);

  if (options->tool_info)
    {
      g_object_unref (options->tool_info);
      options->tool_info = NULL;
    }

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

131
/*  This is such a horrible hack, but necessary because we
132 133 134 135 136
 *  a) load an option's tool-info from disk in many cases
 *  b) screwed up in the past and saved the wrong tool-info in some cases
 */
static GimpToolInfo *
gimp_tool_options_check_tool_info (GimpToolOptions *options,
137 138
                                   GimpToolInfo    *tool_info,
                                   gboolean         warn)
139
{
140
  if (tool_info && G_OBJECT_TYPE (options) == tool_info->tool_options_type)
141 142 143 144 145 146 147
    {
      return tool_info;
    }
  else
    {
      GList *list;

148
      for (list = gimp_get_tool_info_iter (GIMP_CONTEXT (options)->gimp);
149 150 151 152 153 154 155
           list;
           list = g_list_next (list))
        {
          GimpToolInfo *new_info = list->data;

          if (G_OBJECT_TYPE (options) == new_info->tool_options_type)
            {
156 157 158 159
              if (warn)
                g_printerr ("%s: correcting bogus deserialized tool "
                            "type '%s' with right type '%s'\n",
                            g_type_name (G_OBJECT_TYPE (options)),
160
                            tool_info ? gimp_object_get_name (tool_info) : "NULL",
161
                            gimp_object_get_name (new_info));
162 163 164 165 166 167 168 169 170

              return new_info;
            }
        }

      g_return_val_if_reached (NULL);
    }
}

171 172 173 174 175 176
static void
gimp_tool_options_set_property (GObject      *object,
                                guint         property_id,
                                const GValue *value,
                                GParamSpec   *pspec)
{
177
  GimpToolOptions *options = GIMP_TOOL_OPTIONS (object);
178 179 180

  switch (property_id)
    {
181 182 183 184 185 186 187 188 189 190
    case PROP_TOOL:
      {
        GimpToolInfo *tool_info = g_value_get_object (value);
        GimpToolInfo *context_tool;

        context_tool = gimp_context_get_tool (GIMP_CONTEXT (options));

        g_return_if_fail (context_tool == NULL ||
                          context_tool == tool_info);

191
        tool_info = gimp_tool_options_check_tool_info (options, tool_info, TRUE);
192 193 194 195 196 197

        if (! context_tool)
          gimp_context_set_tool (GIMP_CONTEXT (options), tool_info);
      }
      break;

198
    case PROP_TOOL_INFO:
199
      {
200
        GimpToolInfo *tool_info = g_value_get_object (value);
201 202 203 204

        g_return_if_fail (options->tool_info == NULL ||
                          options->tool_info == tool_info);

205
        tool_info = gimp_tool_options_check_tool_info (options, tool_info, TRUE);
206

207
        if (! options->tool_info)
208 209 210 211 212 213 214 215 216 217
          {
            options->tool_info = g_object_ref (tool_info);

            if (tool_info->context_props)
              gimp_context_define_properties (GIMP_CONTEXT (options),
                                              tool_info->context_props, FALSE);

            gimp_context_set_serialize_properties (GIMP_CONTEXT (options),
                                                   tool_info->context_props);
          }
218
      }
219 220 221 222 223 224 225 226 227 228 229 230 231
      break;

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

static void
gimp_tool_options_get_property (GObject    *object,
                                guint       property_id,
                                GValue     *value,
                                GParamSpec *pspec)
232
{
233
  GimpToolOptions *options = GIMP_TOOL_OPTIONS (object);
234 235 236

  switch (property_id)
    {
237 238 239 240
    case PROP_TOOL:
      g_value_set_object (value, gimp_context_get_tool (GIMP_CONTEXT (options)));
      break;

241 242 243 244 245 246 247 248 249 250 251 252 253
    case PROP_TOOL_INFO:
      g_value_set_object (value, options->tool_info);
      break;

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

static void
gimp_tool_options_real_reset (GimpToolOptions *tool_options)
{
254
  gimp_config_reset (GIMP_CONFIG (tool_options));
255 256
}

257 258 259 260 261 262 263 264 265 266
static void
gimp_tool_options_config_reset (GimpConfig *config)
{
  gchar *name = g_strdup (gimp_object_get_name (config));

  gimp_config_reset_properties (G_OBJECT (config));

  gimp_object_take_name (GIMP_OBJECT (config), name);
}

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
static void
gimp_tool_options_tool_notify (GimpToolOptions *options,
                               GParamSpec      *pspec)
{
  GimpToolInfo *tool_info = gimp_context_get_tool (GIMP_CONTEXT (options));
  GimpToolInfo *new_info;

  new_info = gimp_tool_options_check_tool_info (options, tool_info, FALSE);

  if (tool_info && new_info != tool_info)
    g_warning ("%s: 'tool' property on %s was set to bogus value "
               "'%s', it MUST be '%s'.",
               G_STRFUNC,
               g_type_name (G_OBJECT_TYPE (options)),
               gimp_object_get_name (tool_info),
               gimp_object_get_name (new_info));
}


/*  public functions  */

288 289 290 291 292 293 294 295
void
gimp_tool_options_reset (GimpToolOptions *tool_options)
{
  g_return_if_fail (GIMP_IS_TOOL_OPTIONS (tool_options));

  GIMP_TOOL_OPTIONS_GET_CLASS (tool_options)->reset (tool_options);
}

296 297 298 299
gboolean
gimp_tool_options_serialize (GimpToolOptions  *tool_options,
                             GError          **error)
{
300 301 302 303
  gchar    *filename;
  gchar    *header;
  gchar    *footer;
  gboolean  retval;
304 305 306 307

  g_return_val_if_fail (GIMP_IS_TOOL_OPTIONS (tool_options), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

308 309
  filename = gimp_tool_info_build_options_filename (tool_options->tool_info,
                                                    NULL);
310

311 312 313
  if (tool_options->tool_info->gimp->be_verbose)
    g_print ("Writing '%s'\n", gimp_filename_to_utf8 (filename));

314
  header = g_strdup_printf ("GIMP %s options",
315
                            gimp_object_get_name (tool_options->tool_info));
316
  footer = g_strdup_printf ("end of %s options",
317
                            gimp_object_get_name (tool_options->tool_info));
318

319
  retval = gimp_config_serialize_to_file (GIMP_CONFIG (tool_options),
320 321 322 323
                                          filename,
                                          header, footer,
                                          NULL,
                                          error);
324 325

  g_free (filename);
326 327
  g_free (header);
  g_free (footer);
328 329 330 331 332 333 334 335

  return retval;
}

gboolean
gimp_tool_options_deserialize (GimpToolOptions  *tool_options,
                               GError          **error)
{
336 337
  gchar    *filename;
  gboolean  retval;
338 339 340 341

  g_return_val_if_fail (GIMP_IS_TOOL_OPTIONS (tool_options), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

342 343
  filename = gimp_tool_info_build_options_filename (tool_options->tool_info,
                                                    NULL);
344

345 346 347
  if (tool_options->tool_info->gimp->be_verbose)
    g_print ("Parsing '%s'\n", gimp_filename_to_utf8 (filename));

348
  retval = gimp_config_deserialize_file (GIMP_CONFIG (tool_options),
349 350 351
                                         filename,
                                         NULL,
                                         error);
352 353 354 355 356

  g_free (filename);

  return retval;
}
357

358 359 360
gboolean
gimp_tool_options_delete (GimpToolOptions  *tool_options,
                          GError          **error)
361
{
362 363
  gchar    *filename;
  gboolean  retval = TRUE;
364

365 366
  g_return_val_if_fail (GIMP_IS_TOOL_OPTIONS (tool_options), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
367

368 369
  filename = gimp_tool_info_build_options_filename (tool_options->tool_info,
                                                    NULL);
370

371
  if (g_unlink (filename) != 0 && errno != ENOENT)
372
    {
373
      retval = FALSE;
374 375
      g_set_error (error, GIMP_ERROR, GIMP_FAILED,
		   _("Deleting \"%s\" failed: %s"),
376
                   gimp_filename_to_utf8 (filename), g_strerror (errno));
377 378
    }

379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
  g_free (filename);

  return retval;
}

void
gimp_tool_options_create_folder (void)
{
  gchar *filename = g_build_filename (gimp_directory (), "tool-options", NULL);

  g_mkdir (filename,
           S_IRUSR | S_IWUSR | S_IXUSR |
           S_IRGRP | S_IXGRP |
           S_IROTH | S_IXOTH);

  g_free (filename);
395
}