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
|
|
|
|
* Copyright (C) 2001 Sven Neumann <sven@gimp.org>
|
|
|
|
*
|
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"
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <glib-object.h>
|
|
|
|
|
|
|
|
#include "gimpconfig.h"
|
|
|
|
#include "gimpconfig-serialize.h"
|
2001-12-08 00:10:53 +08:00
|
|
|
#include "gimpconfig-deserialize.h"
|
2001-11-27 11:52:11 +08:00
|
|
|
|
2001-12-09 11:00:32 +08:00
|
|
|
#define GIMP_CONFIG_UNKNOWN_TOKENS "gimp-config-unknown-tokens"
|
|
|
|
|
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
|
|
|
|
2001-12-08 00:10:53 +08:00
|
|
|
static void gimp_config_iface_serialize (GObject *object,
|
|
|
|
FILE *file);
|
|
|
|
static gboolean gimp_config_iface_deserialize (GObject *object,
|
|
|
|
GScanner *scanner);
|
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
|
|
|
{
|
2001-12-08 00:10:53 +08:00
|
|
|
gimp_config_iface->serialize = gimp_config_iface_serialize;
|
|
|
|
gimp_config_iface->deserialize = gimp_config_iface_deserialize;
|
2001-11-27 11:52:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-12-08 00:10:53 +08:00
|
|
|
gimp_config_iface_serialize (GObject *object,
|
|
|
|
FILE *file)
|
2001-11-27 11:52:11 +08:00
|
|
|
{
|
2001-12-08 00:10:53 +08:00
|
|
|
gimp_config_serialize_properties (object, file);
|
|
|
|
}
|
2001-11-27 11:52:11 +08:00
|
|
|
|
2001-12-08 00:10:53 +08:00
|
|
|
static gboolean
|
|
|
|
gimp_config_iface_deserialize (GObject *object,
|
|
|
|
GScanner *scanner)
|
|
|
|
{
|
|
|
|
return gimp_config_deserialize_properties (object, scanner);
|
2001-11-27 11:52:11 +08:00
|
|
|
}
|
|
|
|
|
2001-12-08 00:10:53 +08:00
|
|
|
gboolean
|
|
|
|
gimp_config_serialize (GObject *object,
|
|
|
|
const gchar *filename)
|
2001-11-27 11:52:11 +08:00
|
|
|
{
|
2001-12-08 00:10:53 +08:00
|
|
|
GimpConfigInterface *gimp_config_iface;
|
|
|
|
FILE *file;
|
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);
|
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
|
|
|
|
2001-12-08 00:10:53 +08:00
|
|
|
file = fopen (filename, "w");
|
2001-11-27 11:52:11 +08:00
|
|
|
|
2001-12-08 00:10:53 +08:00
|
|
|
if (!file)
|
|
|
|
return FALSE;
|
2001-11-27 11:52:11 +08:00
|
|
|
|
2001-12-08 00:10:53 +08:00
|
|
|
gimp_config_iface->serialize (object, file);
|
|
|
|
|
|
|
|
fclose (file);
|
|
|
|
|
|
|
|
return TRUE;
|
2001-11-27 11:52:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
2001-12-08 00:10:53 +08:00
|
|
|
gimp_config_deserialize (GObject *object,
|
|
|
|
const gchar *filename)
|
2001-11-27 11:52:11 +08:00
|
|
|
{
|
2001-12-08 00:10:53 +08:00
|
|
|
GimpConfigInterface *gimp_config_iface;
|
|
|
|
gint fd;
|
|
|
|
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);
|
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);
|
|
|
|
|
|
|
|
fd = open (filename, O_RDONLY);
|
2001-11-27 11:52:11 +08:00
|
|
|
|
|
|
|
if (fd == -1)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
scanner = g_scanner_new (NULL);
|
|
|
|
|
|
|
|
scanner->config->cset_identifier_first = ( G_CSET_a_2_z "-" G_CSET_A_2_Z );
|
|
|
|
scanner->config->cset_identifier_nth = ( G_CSET_a_2_z "-" G_CSET_A_2_Z );
|
|
|
|
|
|
|
|
g_scanner_input_file (scanner, fd);
|
2001-12-08 00:10:53 +08:00
|
|
|
scanner->input_name = filename;
|
2001-11-27 11:52:11 +08:00
|
|
|
|
2001-12-08 00:10:53 +08:00
|
|
|
success = gimp_config_iface->deserialize (object, scanner);
|
2001-11-27 11:52:11 +08:00
|
|
|
|
|
|
|
g_scanner_destroy (scanner);
|
|
|
|
close (fd);
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
2001-12-08 23:56:40 +08:00
|
|
|
|
2001-12-09 11:00:32 +08:00
|
|
|
void
|
|
|
|
gimp_config_add_unknown_token (GObject *object,
|
|
|
|
gchar *key,
|
|
|
|
gchar *value)
|
|
|
|
{
|
|
|
|
GHashTable *unknown_tokens;
|
|
|
|
|
|
|
|
g_return_if_fail (G_IS_OBJECT (object));
|
|
|
|
g_return_if_fail (key != NULL);
|
|
|
|
g_return_if_fail (value != NULL);
|
|
|
|
|
|
|
|
unknown_tokens =
|
|
|
|
(GHashTable *) g_object_get_data (object, GIMP_CONFIG_UNKNOWN_TOKENS);
|
|
|
|
|
|
|
|
if (!unknown_tokens)
|
|
|
|
{
|
|
|
|
unknown_tokens = g_hash_table_new_full (g_str_hash, g_str_equal,
|
|
|
|
g_free, g_free);
|
|
|
|
g_object_set_data_full (object, GIMP_CONFIG_UNKNOWN_TOKENS,
|
|
|
|
unknown_tokens,
|
|
|
|
(GDestroyNotify) g_hash_table_destroy);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_hash_table_replace (unknown_tokens, key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
const gchar *
|
|
|
|
gimp_config_lookup_unknown_token (GObject *object,
|
|
|
|
const gchar *key)
|
|
|
|
{
|
|
|
|
GHashTable *unknown_tokens;
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_OBJECT (object), NULL);
|
|
|
|
g_return_val_if_fail (key != NULL, NULL);
|
|
|
|
|
|
|
|
unknown_tokens =
|
|
|
|
(GHashTable *) g_object_get_data (object, GIMP_CONFIG_UNKNOWN_TOKENS);
|
|
|
|
|
|
|
|
if (!unknown_tokens)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return (const gchar *) g_hash_table_lookup (unknown_tokens, key);
|
|
|
|
}
|
2001-12-08 23:56:40 +08:00
|
|
|
|
|
|
|
/* for debugging only */
|
|
|
|
|
|
|
|
void
|
|
|
|
gimp_config_debug_notify_callback (GObject *object,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
g_return_if_fail (G_IS_OBJECT (object));
|
|
|
|
g_return_if_fail (G_IS_PARAM_SPEC (pspec));
|
|
|
|
|
|
|
|
if (g_value_type_transformable (pspec->value_type, G_TYPE_STRING))
|
|
|
|
{
|
|
|
|
GValue src = { 0, };
|
|
|
|
GValue dest = { 0, };
|
|
|
|
|
|
|
|
g_value_init (&src, pspec->value_type);
|
|
|
|
g_object_get_property (object, pspec->name, &src);
|
|
|
|
|
|
|
|
g_value_init (&dest, G_TYPE_STRING);
|
|
|
|
g_value_transform (&src, &dest);
|
|
|
|
|
2001-12-09 11:00:32 +08:00
|
|
|
g_print ("%s::%s -> %s\n",
|
2001-12-08 23:56:40 +08:00
|
|
|
g_type_name (G_TYPE_FROM_INSTANCE (object)), pspec->name,
|
|
|
|
g_value_get_string (&dest));
|
|
|
|
|
|
|
|
g_value_unset (&src);
|
|
|
|
g_value_unset (&dest);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_print ("%s: %s changed\n",
|
|
|
|
g_type_name (G_TYPE_FROM_INSTANCE (object)), pspec->name);
|
|
|
|
}
|
|
|
|
}
|