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"
|
|
|
|
|
2001-12-13 09:55:37 +08:00
|
|
|
#include <errno.h>
|
2001-11-27 11:52:11 +08:00
|
|
|
#include <fcntl.h>
|
2001-12-13 09:55:37 +08:00
|
|
|
#include <string.h>
|
2001-11-27 11:52:11 +08:00
|
|
|
#include <unistd.h>
|
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>
|
|
|
|
|
|
|
|
#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-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
|
|
|
|
2001-12-08 00:10:53 +08:00
|
|
|
static void gimp_config_iface_serialize (GObject *object,
|
2001-12-17 09:01:34 +08:00
|
|
|
gint fd);
|
2001-12-08 00:10:53 +08:00
|
|
|
static gboolean gimp_config_iface_deserialize (GObject *object,
|
2001-12-10 00:18:15 +08:00
|
|
|
GScanner *scanner,
|
|
|
|
gboolean store_unknown);
|
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-17 09:01:34 +08:00
|
|
|
gimp_config_iface_serialize (GObject *object,
|
|
|
|
gint fd)
|
2001-11-27 11:52:11 +08:00
|
|
|
{
|
2001-12-13 09:55:37 +08:00
|
|
|
gimp_config_serialize_properties (object, fd);
|
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,
|
2001-12-10 00:18:15 +08:00
|
|
|
GScanner *scanner,
|
|
|
|
gboolean store_unknown)
|
2001-12-08 00:10:53 +08:00
|
|
|
{
|
2001-12-10 00:18:15 +08:00
|
|
|
return gimp_config_deserialize_properties (object,
|
|
|
|
scanner,
|
|
|
|
store_unknown);
|
2001-11-27 11:52:11 +08:00
|
|
|
}
|
|
|
|
|
2001-12-08 00:10:53 +08:00
|
|
|
gboolean
|
2001-12-13 09:55:37 +08:00
|
|
|
gimp_config_serialize (GObject *object,
|
2001-12-17 09:01:34 +08:00
|
|
|
const gchar *filename)
|
2001-11-27 11:52:11 +08:00
|
|
|
{
|
2001-12-08 00:10:53 +08:00
|
|
|
GimpConfigInterface *gimp_config_iface;
|
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);
|
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-17 09:01:34 +08:00
|
|
|
fd = open (filename, O_WRONLY | O_CREAT,
|
|
|
|
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
|
2001-11-27 11:52:11 +08:00
|
|
|
|
2001-12-13 09:55:37 +08:00
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
g_message (_("Failed to open file '%s': %s"),
|
|
|
|
filename, g_strerror (errno));
|
|
|
|
return FALSE;
|
|
|
|
}
|
2001-11-27 11:52:11 +08:00
|
|
|
|
2001-12-17 09:01:34 +08:00
|
|
|
gimp_config_iface->serialize (object, fd);
|
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
|
|
|
|
|
|
|
return TRUE;
|
2001-11-27 11:52:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
2001-12-13 09:55:37 +08:00
|
|
|
gimp_config_deserialize (GObject *object,
|
|
|
|
const gchar *filename,
|
|
|
|
gboolean store_unknown)
|
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)
|
2001-12-13 09:55:37 +08:00
|
|
|
{
|
|
|
|
g_message (_("Failed to open file '%s': %s"),
|
|
|
|
filename, g_strerror (errno));
|
|
|
|
return FALSE;
|
|
|
|
}
|
2001-11-27 11:52:11 +08:00
|
|
|
|
|
|
|
scanner = g_scanner_new (NULL);
|
|
|
|
|
2001-12-10 20:32:28 +08:00
|
|
|
scanner->config->cset_identifier_first = ( G_CSET_a_2_z );
|
|
|
|
scanner->config->cset_identifier_nth = ( G_CSET_a_2_z "-_" );
|
2001-11-27 11:52:11 +08:00
|
|
|
|
|
|
|
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-10 00:18:15 +08:00
|
|
|
success = gimp_config_iface->deserialize (object, scanner, store_unknown);
|
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-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);
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
g_return_if_fail (value != 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)
|
|
|
|
{
|
|
|
|
/* FIXME: should we emit a warning here ?? */
|
|
|
|
g_free (token->value);
|
|
|
|
token->value = g_strdup (value);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
}
|