gimptooloptions.c 10.6 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 26
#include <errno.h>

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

#include <glib/gstdio.h>
27
#include <glib-object.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_dispose      (GObject         *object);
54 55 56 57 58 59 60 61 62 63 64
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);

static void   gimp_tool_options_real_reset   (GimpToolOptions *tool_options);

65 66 67
static void   gimp_tool_options_tool_notify  (GimpToolOptions *options,
                                              GParamSpec      *pspec);

68

69
G_DEFINE_TYPE (GimpToolOptions, gimp_tool_options, GIMP_TYPE_CONTEXT)
70

71 72
#define parent_class gimp_tool_options_parent_class

73

74
static void
75 76
gimp_tool_options_class_init (GimpToolOptionsClass *klass)
{
77
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
78

79
  object_class->dispose      = gimp_tool_options_dispose;
80 81 82 83 84
  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;

85 86
  g_object_class_override_property (object_class, PROP_TOOL, "tool");

87 88 89 90
  g_object_class_install_property (object_class, PROP_TOOL_INFO,
                                   g_param_spec_object ("tool-info",
                                                        NULL, NULL,
                                                        GIMP_TYPE_TOOL_INFO,
91
                                                        GIMP_PARAM_READWRITE));
92

93 94
}

95 96 97
static void
gimp_tool_options_init (GimpToolOptions *options)
{
98
  options->tool_info = NULL;
99 100 101 102

  g_signal_connect (options, "notify::tool",
                    G_CALLBACK (gimp_tool_options_tool_notify),
                    NULL);
103 104
}

105 106 107 108 109 110 111 112 113 114 115 116 117 118
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);
}

119 120 121 122 123 124
/*  This is such a horrible hack, but neccessary because we
 *  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,
125 126
                                   GimpToolInfo    *tool_info,
                                   gboolean         warn)
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
{
  if (G_OBJECT_TYPE (options) == tool_info->tool_options_type)
    {
      return tool_info;
    }
  else
    {
      GList *list;

      for (list = gimp_get_tool_info_iter (tool_info->gimp);
           list;
           list = g_list_next (list))
        {
          GimpToolInfo *new_info = list->data;

          if (G_OBJECT_TYPE (options) == new_info->tool_options_type)
            {
144 145 146 147 148 149
              if (warn)
                g_printerr ("%s: correcting bogus deserialized tool "
                            "type '%s' with right type '%s'\n",
                            g_type_name (G_OBJECT_TYPE (options)),
                            gimp_object_get_name (tool_info),
                            gimp_object_get_name (new_info));
150 151 152 153 154 155 156 157 158

              return new_info;
            }
        }

      g_return_val_if_reached (NULL);
    }
}

159 160 161 162 163 164
static void
gimp_tool_options_set_property (GObject      *object,
                                guint         property_id,
                                const GValue *value,
                                GParamSpec   *pspec)
{
165
  GimpToolOptions *options = GIMP_TOOL_OPTIONS (object);
166 167 168

  switch (property_id)
    {
169 170 171 172 173 174 175 176 177 178
    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);

179
        tool_info = gimp_tool_options_check_tool_info (options, tool_info, TRUE);
180 181 182 183 184 185

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

186
    case PROP_TOOL_INFO:
187
      {
188
        GimpToolInfo *tool_info = g_value_get_object (value);
189 190 191 192

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

193
        tool_info = gimp_tool_options_check_tool_info (options, tool_info, TRUE);
194

195
        if (! options->tool_info)
196
          options->tool_info = g_value_dup_object (value);
197
      }
198 199 200 201 202 203 204 205 206 207 208 209 210
      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)
211
{
212
  GimpToolOptions *options = GIMP_TOOL_OPTIONS (object);
213 214 215

  switch (property_id)
    {
216 217 218 219
    case PROP_TOOL:
      g_value_set_object (value, gimp_context_get_tool (GIMP_CONTEXT (options)));
      break;

220 221 222 223 224 225 226 227 228 229 230 231 232
    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)
{
233
  gimp_config_reset (GIMP_CONFIG (tool_options));
234 235
}

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
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  */

257 258 259 260 261 262 263 264
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);
}

265 266 267 268
gboolean
gimp_tool_options_serialize (GimpToolOptions  *tool_options,
                             GError          **error)
{
269 270 271 272
  gchar    *filename;
  gchar    *header;
  gchar    *footer;
  gboolean  retval;
273 274 275 276

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

277 278
  filename = gimp_tool_info_build_options_filename (tool_options->tool_info,
                                                    NULL);
279

280 281 282
  if (tool_options->tool_info->gimp->be_verbose)
    g_print ("Writing '%s'\n", gimp_filename_to_utf8 (filename));

283
  header = g_strdup_printf ("GIMP %s options",
284
                            gimp_object_get_name (tool_options->tool_info));
285
  footer = g_strdup_printf ("end of %s options",
286
                            gimp_object_get_name (tool_options->tool_info));
287

288
  retval = gimp_config_serialize_to_file (GIMP_CONFIG (tool_options),
289 290 291 292
                                          filename,
                                          header, footer,
                                          NULL,
                                          error);
293 294

  g_free (filename);
295 296
  g_free (header);
  g_free (footer);
297 298 299 300 301 302 303 304

  return retval;
}

gboolean
gimp_tool_options_deserialize (GimpToolOptions  *tool_options,
                               GError          **error)
{
305 306
  gchar    *filename;
  gboolean  retval;
307 308 309 310

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

311 312
  filename = gimp_tool_info_build_options_filename (tool_options->tool_info,
                                                    NULL);
313

314 315 316
  if (tool_options->tool_info->gimp->be_verbose)
    g_print ("Parsing '%s'\n", gimp_filename_to_utf8 (filename));

317
  retval = gimp_config_deserialize_file (GIMP_CONFIG (tool_options),
318 319 320
                                         filename,
                                         NULL,
                                         error);
321 322 323 324 325

  g_free (filename);

  return retval;
}
326

327 328 329
gboolean
gimp_tool_options_delete (GimpToolOptions  *tool_options,
                          GError          **error)
330
{
331 332
  gchar    *filename;
  gboolean  retval = TRUE;
333

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

337 338
  filename = gimp_tool_info_build_options_filename (tool_options->tool_info,
                                                    NULL);
339

340
  if (g_unlink (filename) != 0 && errno != ENOENT)
341
    {
342
      retval = FALSE;
343 344
      g_set_error (error, GIMP_ERROR, GIMP_FAILED,
		   _("Deleting \"%s\" failed: %s"),
345
                   gimp_filename_to_utf8 (filename), g_strerror (errno));
346 347
    }

348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
  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);
364
}