2001-11-27 11:52:11 +08:00
|
|
|
/* The GIMP -- an image manipulation program
|
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
2001-12-08 00:10:53 +08:00
|
|
|
* Config file serialization and deserialization interface
|
2002-03-24 01:58:57 +08:00
|
|
|
* Copyright (C) 2001-2002 Sven Neumann <sven@gimp.org>
|
2001-12-08 00:10:53 +08:00
|
|
|
*
|
2001-11-27 11:52:11 +08:00
|
|
|
* 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
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2001-12-13 09:55:37 +08:00
|
|
|
#include <errno.h>
|
2001-11-27 11:52:11 +08:00
|
|
|
#include <fcntl.h>
|
2002-05-14 05:39:59 +08:00
|
|
|
#include <stdio.h>
|
2001-12-13 09:55:37 +08:00
|
|
|
#include <string.h>
|
2002-02-17 23:55:54 +08:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2001-11-27 11:52:11 +08:00
|
|
|
#include <unistd.h>
|
2002-02-17 23:55:54 +08:00
|
|
|
#endif
|
2001-12-17 09:01:34 +08:00
|
|
|
#include <sys/stat.h>
|
2001-12-13 09:55:37 +08:00
|
|
|
#include <sys/types.h>
|
2001-11-27 11:52:11 +08:00
|
|
|
|
|
|
|
#include <glib-object.h>
|
|
|
|
|
2002-05-31 21:58:18 +08:00
|
|
|
#include "libgimpcolor/gimpcolor.h"
|
|
|
|
|
2001-11-27 11:52:11 +08:00
|
|
|
#include "gimpconfig.h"
|
|
|
|
#include "gimpconfig-serialize.h"
|
2001-12-08 00:10:53 +08:00
|
|
|
#include "gimpconfig-deserialize.h"
|
2002-05-04 07:48:03 +08:00
|
|
|
#include "gimpconfig-utils.h"
|
2002-05-31 21:58:18 +08:00
|
|
|
#include "gimpscanner.h"
|
2001-11-27 11:52:11 +08:00
|
|
|
|
2001-12-13 09:55:37 +08:00
|
|
|
#include "libgimp/gimpintl.h"
|
|
|
|
|
2001-12-09 11:00:32 +08:00
|
|
|
|
2001-12-10 00:18:15 +08:00
|
|
|
/*
|
|
|
|
* The GimpConfig serialization and deserialization interface.
|
|
|
|
*/
|
2001-11-27 11:52:11 +08:00
|
|
|
|
2001-12-08 00:10:53 +08:00
|
|
|
static void gimp_config_iface_init (GimpConfigInterface *gimp_config_iface);
|
2001-11-27 11:52:11 +08:00
|
|
|
|
2002-05-15 19:05:32 +08:00
|
|
|
static gboolean gimp_config_iface_serialize (GObject *object,
|
2002-05-15 19:59:51 +08:00
|
|
|
gint fd,
|
2002-05-24 10:53:20 +08:00
|
|
|
gint indent_level,
|
2002-05-15 19:59:51 +08:00
|
|
|
gpointer data);
|
2001-12-08 00:10:53 +08:00
|
|
|
static gboolean gimp_config_iface_deserialize (GObject *object,
|
2002-05-15 19:59:51 +08:00
|
|
|
GScanner *scanner,
|
2002-05-27 22:04:21 +08:00
|
|
|
gint nest_level,
|
2002-05-15 19:59:51 +08:00
|
|
|
gpointer data);
|
2002-05-04 07:48:03 +08:00
|
|
|
static GObject *gimp_config_iface_duplicate (GObject *object);
|
|
|
|
static gboolean gimp_config_iface_equal (GObject *a,
|
|
|
|
GObject *b);
|
2001-11-27 11:52:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
GType
|
2001-12-08 00:10:53 +08:00
|
|
|
gimp_config_interface_get_type (void)
|
2001-11-27 11:52:11 +08:00
|
|
|
{
|
2001-12-08 00:10:53 +08:00
|
|
|
static GType config_iface_type = 0;
|
2001-11-27 11:52:11 +08:00
|
|
|
|
2001-12-08 00:10:53 +08:00
|
|
|
if (!config_iface_type)
|
2001-11-27 11:52:11 +08:00
|
|
|
{
|
2001-12-08 00:10:53 +08:00
|
|
|
static const GTypeInfo config_iface_info =
|
2001-11-27 11:52:11 +08:00
|
|
|
{
|
2001-12-08 00:10:53 +08:00
|
|
|
sizeof (GimpConfigInterface),
|
|
|
|
(GBaseInitFunc) gimp_config_iface_init,
|
2001-11-27 11:52:11 +08:00
|
|
|
(GBaseFinalizeFunc) NULL,
|
|
|
|
};
|
|
|
|
|
2001-12-08 00:10:53 +08:00
|
|
|
config_iface_type = g_type_register_static (G_TYPE_INTERFACE,
|
|
|
|
"GimpConfigInterface",
|
|
|
|
&config_iface_info,
|
|
|
|
0);
|
2001-11-27 11:52:11 +08:00
|
|
|
|
2001-12-08 00:10:53 +08:00
|
|
|
g_type_interface_add_prerequisite (config_iface_type,
|
|
|
|
G_TYPE_OBJECT);
|
|
|
|
}
|
|
|
|
|
|
|
|
return config_iface_type;
|
2001-11-27 11:52:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-12-08 00:10:53 +08:00
|
|
|
gimp_config_iface_init (GimpConfigInterface *gimp_config_iface)
|
2001-11-27 11:52:11 +08:00
|
|
|
{
|
2002-05-22 21:19:19 +08:00
|
|
|
gimp_config_iface->serialize = gimp_config_iface_serialize;
|
|
|
|
gimp_config_iface->deserialize = gimp_config_iface_deserialize;
|
|
|
|
gimp_config_iface->serialize_property = NULL;
|
|
|
|
gimp_config_iface->deserialize_property = NULL;
|
|
|
|
gimp_config_iface->duplicate = gimp_config_iface_duplicate;
|
|
|
|
gimp_config_iface->equal = gimp_config_iface_equal;
|
2001-11-27 11:52:11 +08:00
|
|
|
}
|
|
|
|
|
2002-05-15 19:05:32 +08:00
|
|
|
static gboolean
|
2002-05-15 19:59:51 +08:00
|
|
|
gimp_config_iface_serialize (GObject *object,
|
|
|
|
gint fd,
|
2002-05-24 10:53:20 +08:00
|
|
|
gint indent_level,
|
2002-05-15 19:59:51 +08:00
|
|
|
gpointer data)
|
2001-11-27 11:52:11 +08:00
|
|
|
{
|
2002-05-24 10:53:20 +08:00
|
|
|
return gimp_config_serialize_properties (object, fd, indent_level);
|
2001-12-08 00:10:53 +08:00
|
|
|
}
|
2001-11-27 11:52:11 +08:00
|
|
|
|
2001-12-08 00:10:53 +08:00
|
|
|
static gboolean
|
|
|
|
gimp_config_iface_deserialize (GObject *object,
|
2002-05-15 19:59:51 +08:00
|
|
|
GScanner *scanner,
|
2002-05-27 22:04:21 +08:00
|
|
|
gint nest_level,
|
2002-05-15 19:59:51 +08:00
|
|
|
gpointer data)
|
2001-12-08 00:10:53 +08:00
|
|
|
{
|
2002-05-27 22:04:21 +08:00
|
|
|
return gimp_config_deserialize_properties (object, scanner, nest_level, FALSE);
|
2001-11-27 11:52:11 +08:00
|
|
|
}
|
|
|
|
|
2002-05-04 07:48:03 +08:00
|
|
|
static GObject *
|
|
|
|
gimp_config_iface_duplicate (GObject *object)
|
|
|
|
{
|
2002-05-06 06:11:34 +08:00
|
|
|
GObject *dup = g_object_new (G_TYPE_FROM_INSTANCE (object), NULL);
|
2002-05-04 07:48:03 +08:00
|
|
|
|
2002-05-06 06:11:34 +08:00
|
|
|
gimp_config_copy_properties (object, dup);
|
2002-05-04 07:48:03 +08:00
|
|
|
|
|
|
|
return dup;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gimp_config_iface_equal (GObject *a,
|
|
|
|
GObject *b)
|
|
|
|
{
|
|
|
|
GObjectClass *klass;
|
|
|
|
GParamSpec **property_specs;
|
|
|
|
guint n_property_specs;
|
|
|
|
guint i;
|
|
|
|
gboolean equal = TRUE;
|
|
|
|
|
|
|
|
klass = G_OBJECT_GET_CLASS (a);
|
|
|
|
|
|
|
|
property_specs = g_object_class_list_properties (klass, &n_property_specs);
|
|
|
|
|
|
|
|
for (i = 0; equal && i < n_property_specs; i++)
|
|
|
|
{
|
|
|
|
GParamSpec *prop_spec;
|
|
|
|
GValue a_value = { 0, };
|
|
|
|
GValue b_value = { 0, };
|
|
|
|
|
|
|
|
prop_spec = property_specs[i];
|
|
|
|
|
|
|
|
if (! (prop_spec->flags & G_PARAM_READABLE))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
g_value_init (&a_value, prop_spec->value_type);
|
|
|
|
g_value_init (&b_value, prop_spec->value_type);
|
|
|
|
g_object_get_property (a, prop_spec->name, &a_value);
|
|
|
|
g_object_get_property (b, prop_spec->name, &b_value);
|
|
|
|
|
|
|
|
equal = gimp_config_values_equal (&a_value, &b_value);
|
|
|
|
|
|
|
|
g_value_unset (&a_value);
|
|
|
|
g_value_unset (&b_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (property_specs);
|
|
|
|
|
|
|
|
return equal;
|
|
|
|
}
|
|
|
|
|
2002-03-24 01:58:57 +08:00
|
|
|
/**
|
|
|
|
* gimp_config_serialize:
|
|
|
|
* @object: a #GObject that implements the #GimpConfigInterface.
|
|
|
|
* @filename: the name of the file to write the configuration to.
|
2002-05-15 19:05:32 +08:00
|
|
|
* @header: optional file header (should be a comment)
|
|
|
|
* @footer: optional file footer (should be a comment)
|
2002-05-15 19:59:51 +08:00
|
|
|
* @data: user data passed to the serialize implementation.
|
2002-03-24 01:58:57 +08:00
|
|
|
* @error:
|
|
|
|
*
|
|
|
|
* Serializes the object properties of @object to the file specified
|
|
|
|
* by @filename. If a file with that name already exists, it is
|
|
|
|
* overwritten. Basically this function opens @filename for you and
|
|
|
|
* calls the serialize function of the @object's #GimpConfigInterface.
|
|
|
|
*
|
|
|
|
* Return value: %TRUE if serialization succeeded, %FALSE otherwise.
|
|
|
|
**/
|
2001-12-08 00:10:53 +08:00
|
|
|
gboolean
|
2001-12-13 09:55:37 +08:00
|
|
|
gimp_config_serialize (GObject *object,
|
2002-03-24 01:58:57 +08:00
|
|
|
const gchar *filename,
|
2002-05-15 19:05:32 +08:00
|
|
|
const gchar *header,
|
|
|
|
const gchar *footer,
|
2002-05-15 19:59:51 +08:00
|
|
|
gpointer data,
|
2002-03-24 01:58:57 +08:00
|
|
|
GError **error)
|
2001-11-27 11:52:11 +08:00
|
|
|
{
|
2001-12-08 00:10:53 +08:00
|
|
|
GimpConfigInterface *gimp_config_iface;
|
2002-05-15 19:05:32 +08:00
|
|
|
gboolean success = TRUE;
|
2002-05-14 05:39:59 +08:00
|
|
|
gchar *tmpname;
|
2001-12-13 09:55:37 +08:00
|
|
|
gint fd;
|
2001-11-27 11:52:11 +08:00
|
|
|
|
2001-12-08 00:10:53 +08:00
|
|
|
g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
|
|
|
|
g_return_val_if_fail (filename != NULL, FALSE);
|
2002-03-24 01:58:57 +08:00
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
|
2001-11-27 11:52:11 +08:00
|
|
|
|
2001-12-08 00:10:53 +08:00
|
|
|
gimp_config_iface = GIMP_GET_CONFIG_INTERFACE (object);
|
2001-11-27 11:52:11 +08:00
|
|
|
|
2001-12-08 00:10:53 +08:00
|
|
|
g_return_val_if_fail (gimp_config_iface != NULL, FALSE);
|
2001-11-27 11:52:11 +08:00
|
|
|
|
2002-05-14 05:39:59 +08:00
|
|
|
tmpname = g_strconcat (filename, "XXXXXX", NULL);
|
|
|
|
|
|
|
|
fd = g_mkstemp (tmpname);
|
2001-11-27 11:52:11 +08:00
|
|
|
|
2001-12-13 09:55:37 +08:00
|
|
|
if (fd == -1)
|
|
|
|
{
|
2002-03-24 01:58:57 +08:00
|
|
|
g_set_error (error,
|
2002-05-28 05:50:50 +08:00
|
|
|
GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_WRITE,
|
2002-05-15 19:05:32 +08:00
|
|
|
_("Failed to create temporary file for '%s': %s"),
|
2002-03-24 01:58:57 +08:00
|
|
|
filename, g_strerror (errno));
|
2002-05-14 05:39:59 +08:00
|
|
|
g_free (tmpname);
|
2001-12-13 09:55:37 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
2001-11-27 11:52:11 +08:00
|
|
|
|
2002-05-15 19:05:32 +08:00
|
|
|
if (header)
|
2002-05-22 01:33:04 +08:00
|
|
|
success = (write (fd, header, strlen (header)) != -1 &&
|
|
|
|
write (fd, "\n", 1) != -1);
|
2002-05-15 19:05:32 +08:00
|
|
|
|
|
|
|
if (success)
|
2002-05-24 10:53:20 +08:00
|
|
|
success = gimp_config_iface->serialize (object, fd, 0, data);
|
2002-05-15 19:05:32 +08:00
|
|
|
|
|
|
|
if (success && footer)
|
2002-05-22 01:33:04 +08:00
|
|
|
success = (write (fd, "\n", 1) != -1 &&
|
2002-05-27 22:04:21 +08:00
|
|
|
write (fd, footer, strlen (footer)) != -1 &&
|
|
|
|
write (fd, "\n", 1) != -1);
|
2002-05-15 19:05:32 +08:00
|
|
|
|
|
|
|
if (! success)
|
|
|
|
{
|
2002-05-28 05:50:50 +08:00
|
|
|
gchar *msg;
|
|
|
|
|
2002-05-28 05:59:26 +08:00
|
|
|
if (g_file_test (filename, G_FILE_TEST_EXISTS))
|
2002-05-28 05:50:50 +08:00
|
|
|
{
|
|
|
|
msg = g_strdup_printf (_("Error when writing to temporary file for '%s': %s\n"
|
|
|
|
"The original file has not been touched."),
|
|
|
|
filename, g_strerror (errno));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
msg = g_strdup_printf (_("Error when writing to temporary file for '%s': %s\n"
|
|
|
|
"No file has been created."),
|
|
|
|
filename, g_strerror (errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
g_set_error (error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_WRITE, msg);
|
2002-05-15 19:05:32 +08:00
|
|
|
|
|
|
|
unlink (tmpname);
|
|
|
|
}
|
2001-12-08 00:10:53 +08:00
|
|
|
|
2001-12-13 09:55:37 +08:00
|
|
|
close (fd);
|
2001-12-08 00:10:53 +08:00
|
|
|
|
2002-05-15 19:05:32 +08:00
|
|
|
if (success && rename (tmpname, filename) == -1)
|
2002-05-14 05:39:59 +08:00
|
|
|
{
|
|
|
|
g_set_error (error,
|
2002-05-28 05:50:50 +08:00
|
|
|
GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_WRITE,
|
2002-05-15 19:05:32 +08:00
|
|
|
_("Failed to create file '%s': %s"),
|
|
|
|
filename, g_strerror (errno));
|
|
|
|
|
2002-05-14 05:39:59 +08:00
|
|
|
unlink (tmpname);
|
2002-05-15 19:05:32 +08:00
|
|
|
success = FALSE;
|
2002-05-14 05:39:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
g_free (tmpname);
|
|
|
|
|
2002-05-15 19:05:32 +08:00
|
|
|
return success;
|
2001-11-27 11:52:11 +08:00
|
|
|
}
|
|
|
|
|
2002-03-24 01:58:57 +08:00
|
|
|
/**
|
|
|
|
* gimp_config_deserialize:
|
|
|
|
* @object: a #GObject that implements the #GimpConfigInterface.
|
|
|
|
* @filename: the name of the file to read configuration from.
|
2002-05-16 02:23:17 +08:00
|
|
|
* @data: user data passed to the deserialize implementation.
|
2002-03-24 01:58:57 +08:00
|
|
|
* @error:
|
|
|
|
*
|
|
|
|
* Opens the file specified by @filename, reads configuration data
|
|
|
|
* from it and configures @object accordingly. Basically this function
|
|
|
|
* creates a properly configured #GScanner for you and calls the
|
|
|
|
* deserialize function of the @object's #GimpConfigInterface.
|
|
|
|
*
|
|
|
|
* Return value: %TRUE if deserialization succeeded, %FALSE otherwise.
|
|
|
|
**/
|
2001-11-27 11:52:11 +08:00
|
|
|
gboolean
|
2001-12-13 09:55:37 +08:00
|
|
|
gimp_config_deserialize (GObject *object,
|
2002-03-24 01:58:57 +08:00
|
|
|
const gchar *filename,
|
2002-05-15 19:59:51 +08:00
|
|
|
gpointer data,
|
2002-03-24 01:58:57 +08:00
|
|
|
GError **error)
|
2001-11-27 11:52:11 +08:00
|
|
|
{
|
2001-12-08 00:10:53 +08:00
|
|
|
GimpConfigInterface *gimp_config_iface;
|
|
|
|
GScanner *scanner;
|
|
|
|
gboolean success;
|
2001-11-27 11:52:11 +08:00
|
|
|
|
2001-12-08 00:10:53 +08:00
|
|
|
g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
|
|
|
|
g_return_val_if_fail (filename != NULL, FALSE);
|
2002-03-24 01:58:57 +08:00
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
|
2001-11-27 11:52:11 +08:00
|
|
|
|
2001-12-08 00:10:53 +08:00
|
|
|
gimp_config_iface = GIMP_GET_CONFIG_INTERFACE (object);
|
|
|
|
|
|
|
|
g_return_val_if_fail (gimp_config_iface != NULL, FALSE);
|
|
|
|
|
2002-05-31 21:58:18 +08:00
|
|
|
scanner = gimp_scanner_new (filename, error);
|
2002-05-14 20:33:11 +08:00
|
|
|
|
2002-05-31 21:58:18 +08:00
|
|
|
if (! scanner)
|
|
|
|
return FALSE;
|
2001-11-27 11:52:11 +08:00
|
|
|
|
2002-05-27 22:04:21 +08:00
|
|
|
success = gimp_config_iface->deserialize (object, scanner, 0, data);
|
2001-11-27 11:52:11 +08:00
|
|
|
|
2002-05-31 21:58:18 +08:00
|
|
|
gimp_scanner_destroy (scanner);
|
2001-11-27 11:52:11 +08:00
|
|
|
|
2002-05-22 21:19:19 +08:00
|
|
|
if (! success)
|
|
|
|
g_assert (error == NULL || *error != NULL);
|
|
|
|
|
2001-11-27 11:52:11 +08:00
|
|
|
return success;
|
|
|
|
}
|
2001-12-08 23:56:40 +08:00
|
|
|
|
2002-05-27 22:04:21 +08:00
|
|
|
gboolean
|
|
|
|
gimp_config_deserialize_return (GScanner *scanner,
|
|
|
|
GTokenType expected_token,
|
2002-06-18 03:36:33 +08:00
|
|
|
gint nest_level)
|
2002-05-27 22:04:21 +08:00
|
|
|
{
|
|
|
|
GTokenType next_token;
|
|
|
|
|
|
|
|
g_return_val_if_fail (scanner != NULL, FALSE);
|
|
|
|
|
|
|
|
next_token = g_scanner_peek_next_token (scanner);
|
|
|
|
|
|
|
|
if (expected_token != G_TOKEN_LEFT_PAREN)
|
|
|
|
{
|
|
|
|
g_scanner_get_next_token (scanner);
|
2002-06-18 03:36:33 +08:00
|
|
|
g_scanner_unexp_token (scanner, expected_token, NULL, NULL, NULL,
|
2002-05-27 22:04:21 +08:00
|
|
|
_("fatal parse error"), TRUE);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (nest_level > 0 && next_token == G_TOKEN_RIGHT_PAREN)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else if (next_token != G_TOKEN_EOF)
|
|
|
|
{
|
|
|
|
g_scanner_get_next_token (scanner);
|
2002-06-18 03:36:33 +08:00
|
|
|
g_scanner_unexp_token (scanner, expected_token, NULL, NULL, NULL,
|
2002-05-27 22:04:21 +08:00
|
|
|
_("fatal parse error"), TRUE);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2002-03-24 01:58:57 +08:00
|
|
|
GQuark
|
|
|
|
gimp_config_error_quark (void)
|
|
|
|
{
|
|
|
|
static GQuark q = 0;
|
|
|
|
if (q == 0)
|
|
|
|
q = g_quark_from_static_string ("gimp-config-error-quark");
|
|
|
|
|
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
2002-05-24 10:53:20 +08:00
|
|
|
void
|
|
|
|
gimp_config_string_indent (GString *string,
|
|
|
|
gint indent_level)
|
|
|
|
{
|
2002-05-24 21:52:07 +08:00
|
|
|
gint i;
|
2002-05-24 10:53:20 +08:00
|
|
|
|
|
|
|
g_return_if_fail (string != NULL);
|
2002-05-24 21:07:12 +08:00
|
|
|
g_return_if_fail (indent_level >= 0);
|
2002-05-24 10:53:20 +08:00
|
|
|
|
2002-05-24 21:52:07 +08:00
|
|
|
for (i = 0; i < indent_level; i++)
|
|
|
|
g_string_append_len (string, " ", 4);
|
2002-05-24 10:53:20 +08:00
|
|
|
}
|
|
|
|
|
2002-05-04 07:48:03 +08:00
|
|
|
/**
|
|
|
|
* gimp_config_duplicate:
|
|
|
|
* @object: a #GObject that implements the #GimpConfigInterface.
|
|
|
|
*
|
|
|
|
* Creates a copy of the passed object by copying all object
|
|
|
|
* properties. The default implementation of the #GimpConfigInterface
|
|
|
|
* only works for objects that are completely defined by their
|
|
|
|
* properties.
|
|
|
|
*
|
|
|
|
* Return value: the duplicated #GObject.
|
|
|
|
**/
|
|
|
|
GObject *
|
|
|
|
gimp_config_duplicate (GObject *object)
|
|
|
|
{
|
|
|
|
GimpConfigInterface *gimp_config_iface;
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_OBJECT (object), NULL);
|
|
|
|
|
|
|
|
gimp_config_iface = GIMP_GET_CONFIG_INTERFACE (object);
|
|
|
|
|
|
|
|
g_return_val_if_fail (gimp_config_iface != NULL, FALSE);
|
|
|
|
|
|
|
|
return gimp_config_iface->duplicate (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_config_is_equal_to:
|
|
|
|
* @a: a #GObject that implements the #GimpConfigInterface.
|
|
|
|
* @b: another #GObject of the same type as @a.
|
|
|
|
*
|
|
|
|
* Compares the two objects. The default implementation of the
|
|
|
|
* #GimpConfigInterface compares the object properties and thus only
|
|
|
|
* works for objects that are completely defined by their
|
|
|
|
* properties.
|
|
|
|
*
|
|
|
|
* Return value: %TRUE if the two objects are equal.
|
|
|
|
**/
|
|
|
|
gboolean
|
|
|
|
gimp_config_is_equal_to (GObject *a,
|
|
|
|
GObject *b)
|
|
|
|
{
|
|
|
|
GimpConfigInterface *gimp_config_iface;
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_OBJECT (a), FALSE);
|
|
|
|
g_return_val_if_fail (G_IS_OBJECT (b), FALSE);
|
2002-05-06 06:11:34 +08:00
|
|
|
g_return_val_if_fail (G_TYPE_FROM_INSTANCE (a) == G_TYPE_FROM_INSTANCE (b),
|
2002-05-04 07:48:03 +08:00
|
|
|
FALSE);
|
|
|
|
|
|
|
|
gimp_config_iface = GIMP_GET_CONFIG_INTERFACE (a);
|
|
|
|
|
|
|
|
g_return_val_if_fail (gimp_config_iface != NULL, FALSE);
|
|
|
|
|
|
|
|
return gimp_config_iface->equal (a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-10 00:18:15 +08:00
|
|
|
/*
|
|
|
|
* Code to store and lookup unknown tokens (string key/value pairs).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define GIMP_CONFIG_UNKNOWN_TOKENS "gimp-config-unknown-tokens"
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
gchar *key;
|
|
|
|
gchar *value;
|
|
|
|
} GimpConfigToken;
|
|
|
|
|
|
|
|
static void gimp_config_destroy_unknown_tokens (GSList *unknown_tokens);
|
|
|
|
|
|
|
|
|
2002-03-24 01:58:57 +08:00
|
|
|
/**
|
|
|
|
* gimp_config_add_unknown_token:
|
|
|
|
* @object: a #GObject.
|
|
|
|
* @key: a nul-terminated string to identify the value.
|
|
|
|
* @value: a nul-terminated string representing the value.
|
|
|
|
*
|
|
|
|
* This function allows to add arbitrary key/value pairs to a GObject.
|
|
|
|
* It's purpose is to attach additional data to a #GimpConfig object
|
2002-05-06 06:11:34 +08:00
|
|
|
* that can be stored along with the object properties when
|
|
|
|
* serializing the object to a configuration file. Please note however
|
|
|
|
* that the default gimp_config_serialize() implementation does not
|
|
|
|
* serialize unknown tokens.
|
2002-03-24 01:58:57 +08:00
|
|
|
*
|
|
|
|
* If you want to remove a key/value pair from the object, call this
|
|
|
|
* function with a %NULL @value.
|
|
|
|
**/
|
2001-12-09 11:00:32 +08:00
|
|
|
void
|
2001-12-10 00:18:15 +08:00
|
|
|
gimp_config_add_unknown_token (GObject *object,
|
|
|
|
const gchar *key,
|
|
|
|
const gchar *value)
|
2001-12-09 11:00:32 +08:00
|
|
|
{
|
2001-12-10 00:18:15 +08:00
|
|
|
GimpConfigToken *token;
|
|
|
|
GSList *unknown_tokens;
|
|
|
|
GSList *last;
|
|
|
|
GSList *list;
|
2001-12-09 11:00:32 +08:00
|
|
|
|
|
|
|
g_return_if_fail (G_IS_OBJECT (object));
|
|
|
|
g_return_if_fail (key != NULL);
|
|
|
|
|
2001-12-10 00:18:15 +08:00
|
|
|
unknown_tokens = (GSList *) g_object_get_data (object,
|
|
|
|
GIMP_CONFIG_UNKNOWN_TOKENS);
|
|
|
|
|
|
|
|
for (last = NULL, list = unknown_tokens;
|
|
|
|
list;
|
|
|
|
last = list, list = g_slist_next (list))
|
|
|
|
{
|
|
|
|
token = (GimpConfigToken *) list->data;
|
|
|
|
|
|
|
|
if (strcmp (token->key, key) == 0)
|
|
|
|
{
|
|
|
|
g_free (token->value);
|
2002-03-24 01:58:57 +08:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
token->value = g_strdup (value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_free (token->key);
|
|
|
|
|
|
|
|
unknown_tokens = g_slist_remove (unknown_tokens, token);
|
|
|
|
g_object_set_data_full (object, GIMP_CONFIG_UNKNOWN_TOKENS,
|
|
|
|
unknown_tokens,
|
|
|
|
(GDestroyNotify) gimp_config_destroy_unknown_tokens);
|
|
|
|
}
|
2001-12-10 00:18:15 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2001-12-09 11:00:32 +08:00
|
|
|
|
2001-12-10 00:18:15 +08:00
|
|
|
token = g_new (GimpConfigToken, 1);
|
|
|
|
token->key = g_strdup (key);
|
|
|
|
token->value = g_strdup (value);
|
|
|
|
|
|
|
|
if (last)
|
|
|
|
{
|
|
|
|
g_slist_append (last, token);
|
|
|
|
}
|
|
|
|
else
|
2001-12-09 11:00:32 +08:00
|
|
|
{
|
2001-12-10 00:18:15 +08:00
|
|
|
unknown_tokens = g_slist_append (NULL, token);
|
|
|
|
|
2001-12-09 11:00:32 +08:00
|
|
|
g_object_set_data_full (object, GIMP_CONFIG_UNKNOWN_TOKENS,
|
|
|
|
unknown_tokens,
|
2001-12-10 00:18:15 +08:00
|
|
|
(GDestroyNotify) gimp_config_destroy_unknown_tokens);
|
2001-12-09 11:00:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-24 01:58:57 +08:00
|
|
|
/**
|
|
|
|
* gimp_config_lookup_unknown_token:
|
|
|
|
* @object: a #GObject.
|
|
|
|
* @key: a nul-terminated string to identify the value.
|
|
|
|
*
|
|
|
|
* This function retrieves data that was previously attached using
|
|
|
|
* gimp_config_add_unknown_token(). You should not free or modify
|
|
|
|
* the returned string.
|
|
|
|
**/
|
2001-12-09 11:00:32 +08:00
|
|
|
const gchar *
|
|
|
|
gimp_config_lookup_unknown_token (GObject *object,
|
|
|
|
const gchar *key)
|
|
|
|
{
|
2001-12-10 00:18:15 +08:00
|
|
|
GimpConfigToken *token;
|
|
|
|
GSList *unknown_tokens;
|
|
|
|
GSList *list;
|
2001-12-09 11:00:32 +08:00
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_OBJECT (object), NULL);
|
|
|
|
g_return_val_if_fail (key != NULL, NULL);
|
|
|
|
|
2001-12-10 00:18:15 +08:00
|
|
|
unknown_tokens = (GSList *) g_object_get_data (object,
|
|
|
|
GIMP_CONFIG_UNKNOWN_TOKENS);
|
2001-12-09 11:00:32 +08:00
|
|
|
|
2001-12-10 00:18:15 +08:00
|
|
|
for (list = unknown_tokens; list; list = g_slist_next (list))
|
|
|
|
{
|
|
|
|
token = (GimpConfigToken *) list->data;
|
2001-12-09 11:00:32 +08:00
|
|
|
|
2001-12-10 00:18:15 +08:00
|
|
|
if (strcmp (token->key, key) == 0)
|
|
|
|
return token->value;
|
|
|
|
}
|
2001-12-08 23:56:40 +08:00
|
|
|
|
2001-12-10 00:18:15 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-12-08 23:56:40 +08:00
|
|
|
|
2002-03-24 01:58:57 +08:00
|
|
|
/**
|
|
|
|
* gimp_config_foreach_unknown_token:
|
|
|
|
* @object: a #GObject.
|
|
|
|
* @func: a function to call for each key/value pair.
|
|
|
|
* @user_data: data to pass to @func.
|
|
|
|
*
|
|
|
|
* Calls @func for each key/value stored with the @object using
|
|
|
|
* gimp_config_add_unknown_token().
|
|
|
|
**/
|
2001-12-08 23:56:40 +08:00
|
|
|
void
|
2001-12-10 00:18:15 +08:00
|
|
|
gimp_config_foreach_unknown_token (GObject *object,
|
|
|
|
GimpConfigForeachFunc func,
|
|
|
|
gpointer user_data)
|
2001-12-08 23:56:40 +08:00
|
|
|
{
|
2001-12-10 00:18:15 +08:00
|
|
|
GimpConfigToken *token;
|
|
|
|
GSList *unknown_tokens;
|
|
|
|
GSList *list;
|
|
|
|
|
2001-12-08 23:56:40 +08:00
|
|
|
g_return_if_fail (G_IS_OBJECT (object));
|
2001-12-10 00:18:15 +08:00
|
|
|
g_return_if_fail (func != NULL);
|
|
|
|
|
|
|
|
unknown_tokens = (GSList *) g_object_get_data (object,
|
|
|
|
GIMP_CONFIG_UNKNOWN_TOKENS);
|
2001-12-08 23:56:40 +08:00
|
|
|
|
2001-12-10 00:18:15 +08:00
|
|
|
for (list = unknown_tokens; list; list = g_slist_next (list))
|
2001-12-08 23:56:40 +08:00
|
|
|
{
|
2001-12-10 00:18:15 +08:00
|
|
|
token = (GimpConfigToken *) list->data;
|
|
|
|
|
|
|
|
func (token->key, token->value, user_data);
|
|
|
|
}
|
|
|
|
}
|
2001-12-08 23:56:40 +08:00
|
|
|
|
2001-12-10 00:18:15 +08:00
|
|
|
static void
|
|
|
|
gimp_config_destroy_unknown_tokens (GSList *unknown_tokens)
|
|
|
|
{
|
|
|
|
GimpConfigToken *token;
|
|
|
|
GSList *list;
|
2001-12-08 23:56:40 +08:00
|
|
|
|
2001-12-10 00:18:15 +08:00
|
|
|
for (list = unknown_tokens; list; list = g_slist_next (list))
|
2001-12-08 23:56:40 +08:00
|
|
|
{
|
2001-12-10 00:18:15 +08:00
|
|
|
token = (GimpConfigToken *) list->data;
|
|
|
|
|
|
|
|
g_free (token->key);
|
|
|
|
g_free (token->value);
|
|
|
|
g_free (token);
|
2001-12-08 23:56:40 +08:00
|
|
|
}
|
2001-12-10 00:18:15 +08:00
|
|
|
|
|
|
|
g_slist_free (unknown_tokens);
|
2001-12-08 23:56:40 +08:00
|
|
|
}
|