2001-12-19 03:23:26 +08:00
|
|
|
/* The GIMP -- an image manipulation program
|
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
2002-03-24 01:58:57 +08:00
|
|
|
* GimpRc, the object for GIMPs user configuration file gimprc.
|
|
|
|
* Copyright (C) 2001-2002 Sven Neumann <sven@gimp.org>
|
2001-12-19 03:23:26 +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"
|
|
|
|
|
2002-09-07 06:25:19 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
2002-05-24 21:52:07 +08:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2001-12-19 03:23:26 +08:00
|
|
|
#include <glib-object.h>
|
|
|
|
|
2002-09-07 06:25:19 +08:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
|
2002-05-22 21:45:35 +08:00
|
|
|
#include "libgimpbase/gimpbase.h"
|
2001-12-19 03:23:26 +08:00
|
|
|
#include "libgimpbase/gimpbase.h"
|
|
|
|
|
2002-11-19 04:50:31 +08:00
|
|
|
#include "config-types.h"
|
|
|
|
|
2001-12-19 03:23:26 +08:00
|
|
|
#include "gimpconfig.h"
|
2001-12-19 04:40:50 +08:00
|
|
|
#include "gimpconfig-deserialize.h"
|
2002-05-22 21:45:35 +08:00
|
|
|
#include "gimpconfig-params.h"
|
2002-05-06 06:11:34 +08:00
|
|
|
#include "gimpconfig-serialize.h"
|
|
|
|
#include "gimpconfig-utils.h"
|
2001-12-19 03:23:26 +08:00
|
|
|
#include "gimprc.h"
|
|
|
|
|
2001-12-29 00:26:54 +08:00
|
|
|
#include "libgimp/gimpintl.h"
|
|
|
|
|
2001-12-19 03:23:26 +08:00
|
|
|
|
2002-05-06 06:11:34 +08:00
|
|
|
static void gimp_rc_config_iface_init (gpointer iface,
|
|
|
|
gpointer iface_data);
|
2002-05-15 19:05:32 +08:00
|
|
|
static gboolean gimp_rc_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);
|
2002-05-06 06:11:34 +08:00
|
|
|
static gboolean gimp_rc_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-06 06:11:34 +08:00
|
|
|
static GObject * gimp_rc_duplicate (GObject *object);
|
2001-12-19 03:23:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
GType
|
|
|
|
gimp_rc_get_type (void)
|
|
|
|
{
|
|
|
|
static GType rc_type = 0;
|
|
|
|
|
|
|
|
if (! rc_type)
|
|
|
|
{
|
|
|
|
static const GTypeInfo rc_info =
|
|
|
|
{
|
|
|
|
sizeof (GimpRcClass),
|
|
|
|
NULL, /* base_init */
|
|
|
|
NULL, /* base_finalize */
|
|
|
|
NULL, /* class_init */
|
|
|
|
NULL, /* class_finalize */
|
|
|
|
NULL, /* class_data */
|
|
|
|
sizeof (GimpRc),
|
|
|
|
0, /* n_preallocs */
|
|
|
|
NULL /* instance_init */
|
|
|
|
};
|
|
|
|
static const GInterfaceInfo rc_iface_info =
|
|
|
|
{
|
|
|
|
gimp_rc_config_iface_init,
|
|
|
|
NULL, /* iface_finalize */
|
|
|
|
NULL /* iface_data */
|
|
|
|
};
|
|
|
|
|
|
|
|
rc_type = g_type_register_static (GIMP_TYPE_GUI_CONFIG,
|
|
|
|
"GimpRc",
|
|
|
|
&rc_info, 0);
|
|
|
|
|
|
|
|
g_type_add_interface_static (rc_type,
|
|
|
|
GIMP_TYPE_CONFIG_INTERFACE,
|
|
|
|
&rc_iface_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_rc_config_iface_init (gpointer iface,
|
|
|
|
gpointer iface_data)
|
|
|
|
{
|
|
|
|
GimpConfigInterface *config_iface = (GimpConfigInterface *) iface;
|
|
|
|
|
2001-12-19 04:40:50 +08:00
|
|
|
config_iface->serialize = gimp_rc_serialize;
|
|
|
|
config_iface->deserialize = gimp_rc_deserialize;
|
2002-05-06 06:11:34 +08:00
|
|
|
config_iface->duplicate = gimp_rc_duplicate;
|
2001-12-19 03:23:26 +08:00
|
|
|
}
|
|
|
|
|
2002-05-15 19:05:32 +08:00
|
|
|
static gboolean
|
2001-12-19 03:23:26 +08:00
|
|
|
gimp_rc_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-19 03:23:26 +08:00
|
|
|
{
|
2002-05-15 19:59:51 +08:00
|
|
|
gboolean success;
|
|
|
|
|
|
|
|
if (data && GIMP_IS_RC (data))
|
|
|
|
success = gimp_config_serialize_changed_properties (object,
|
|
|
|
G_OBJECT (data),
|
2002-05-24 10:53:20 +08:00
|
|
|
fd,
|
|
|
|
indent_level);
|
2002-05-15 19:59:51 +08:00
|
|
|
else
|
2002-05-24 10:53:20 +08:00
|
|
|
success = gimp_config_serialize_properties (object, fd, indent_level);
|
2002-05-15 19:59:51 +08:00
|
|
|
|
2002-05-24 21:52:07 +08:00
|
|
|
if (success)
|
|
|
|
success = (write (fd, "\n", 1) != -1);
|
|
|
|
|
2002-05-15 19:59:51 +08:00
|
|
|
if (success)
|
2002-05-24 10:53:20 +08:00
|
|
|
success = gimp_config_serialize_unknown_tokens (object, fd, indent_level);
|
2002-05-15 19:59:51 +08:00
|
|
|
|
|
|
|
return success;
|
2001-12-19 03:23:26 +08:00
|
|
|
}
|
|
|
|
|
2001-12-19 04:40:50 +08:00
|
|
|
static gboolean
|
|
|
|
gimp_rc_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-19 04:40:50 +08:00
|
|
|
{
|
2002-05-27 22:04:21 +08:00
|
|
|
return gimp_config_deserialize_properties (object, scanner, nest_level, TRUE);
|
2001-12-19 04:40:50 +08:00
|
|
|
}
|
|
|
|
|
2002-05-06 06:11:34 +08:00
|
|
|
static void
|
|
|
|
gimp_rc_duplicate_unknown_token (const gchar *key,
|
|
|
|
const gchar *value,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
gimp_config_add_unknown_token (G_OBJECT (user_data), key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GObject *
|
|
|
|
gimp_rc_duplicate (GObject *object)
|
|
|
|
{
|
|
|
|
GObject *dup = g_object_new (GIMP_TYPE_RC, NULL);
|
|
|
|
|
|
|
|
gimp_config_copy_properties (object, dup);
|
|
|
|
|
|
|
|
gimp_config_foreach_unknown_token (object,
|
|
|
|
gimp_rc_duplicate_unknown_token,
|
|
|
|
dup);
|
|
|
|
|
|
|
|
return dup;
|
|
|
|
}
|
|
|
|
|
2002-02-08 22:05:10 +08:00
|
|
|
/**
|
|
|
|
* gimp_rc_new:
|
|
|
|
*
|
|
|
|
* Creates a new #GimpRc object with default configuration values.
|
|
|
|
*
|
|
|
|
* Return value: the newly generated #GimpRc object.
|
|
|
|
**/
|
2001-12-19 03:23:26 +08:00
|
|
|
GimpRc *
|
|
|
|
gimp_rc_new (void)
|
|
|
|
{
|
2001-12-29 00:26:54 +08:00
|
|
|
return GIMP_RC (g_object_new (GIMP_TYPE_RC, NULL));
|
|
|
|
}
|
|
|
|
|
2002-05-06 06:11:34 +08:00
|
|
|
/**
|
|
|
|
* gimp_rc_query:
|
|
|
|
* @rc: a #GimpRc object.
|
|
|
|
* @key: a string used as a key for the lookup.
|
|
|
|
*
|
|
|
|
* This function looks up @key in the object properties of @rc. If
|
|
|
|
* there's a matching property, a string representation of its value
|
|
|
|
* is returned. If no property is found, the list of unknown tokens
|
|
|
|
* attached to the @rc object is searched.
|
|
|
|
*
|
|
|
|
* Return value: a newly allocated string representing the value or %NULL
|
|
|
|
* if the key couldn't be found.
|
|
|
|
**/
|
|
|
|
gchar *
|
|
|
|
gimp_rc_query (GimpRc *rc,
|
|
|
|
const gchar *key)
|
|
|
|
{
|
|
|
|
GObjectClass *klass;
|
2002-05-22 21:45:35 +08:00
|
|
|
GObject *rc_object;
|
2002-05-06 06:11:34 +08:00
|
|
|
GParamSpec **property_specs;
|
|
|
|
GParamSpec *prop_spec;
|
|
|
|
guint i, n_property_specs;
|
|
|
|
gchar *retval = NULL;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GIMP_IS_RC (rc), NULL);
|
|
|
|
g_return_val_if_fail (key != NULL, NULL);
|
|
|
|
|
2002-05-22 21:45:35 +08:00
|
|
|
rc_object = G_OBJECT (rc);
|
2002-05-06 06:11:34 +08:00
|
|
|
klass = G_OBJECT_GET_CLASS (rc);
|
|
|
|
|
|
|
|
property_specs = g_object_class_list_properties (klass, &n_property_specs);
|
|
|
|
|
|
|
|
if (!property_specs)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (i = 0, prop_spec = NULL; i < n_property_specs && !prop_spec; i++)
|
|
|
|
{
|
|
|
|
prop_spec = property_specs[i];
|
|
|
|
|
2002-05-22 21:45:35 +08:00
|
|
|
if (! (prop_spec->flags & GIMP_PARAM_SERIALIZE) ||
|
2002-05-06 06:11:34 +08:00
|
|
|
strcmp (prop_spec->name, key))
|
|
|
|
{
|
|
|
|
prop_spec = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prop_spec)
|
|
|
|
{
|
|
|
|
GString *str = g_string_new (NULL);
|
|
|
|
GValue value = { 0, };
|
|
|
|
|
|
|
|
g_value_init (&value, prop_spec->value_type);
|
2002-05-22 21:45:35 +08:00
|
|
|
g_object_get_property (rc_object, prop_spec->name, &value);
|
2002-05-06 06:11:34 +08:00
|
|
|
|
2002-05-06 19:04:39 +08:00
|
|
|
if (gimp_config_serialize_value (&value, str, FALSE))
|
2002-05-06 06:11:34 +08:00
|
|
|
retval = g_string_free (str, FALSE);
|
|
|
|
else
|
|
|
|
g_string_free (str, TRUE);
|
|
|
|
|
|
|
|
g_value_unset (&value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-05-22 21:45:35 +08:00
|
|
|
retval = g_strdup (gimp_config_lookup_unknown_token (rc_object, key));
|
2002-05-06 06:11:34 +08:00
|
|
|
}
|
|
|
|
|
2002-05-06 07:02:15 +08:00
|
|
|
g_free (property_specs);
|
|
|
|
|
2002-05-06 06:11:34 +08:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2002-11-19 04:50:31 +08:00
|
|
|
void
|
|
|
|
gimp_rc_load (GimpRc *gimprc)
|
|
|
|
{
|
|
|
|
gchar *filename;
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
g_return_if_fail (GIMP_IS_RC (gimprc));
|
|
|
|
|
|
|
|
filename = g_build_filename (gimp_sysconf_directory (), "gimprc", NULL);
|
|
|
|
|
|
|
|
g_printerr ("parsing '%s' ... \n", filename);
|
|
|
|
if (!gimp_config_deserialize (G_OBJECT (gimprc), filename, NULL, &error))
|
|
|
|
{
|
|
|
|
if (error->code != GIMP_CONFIG_ERROR_OPEN_ENOENT)
|
|
|
|
g_message (error->message);
|
|
|
|
|
|
|
|
g_clear_error (&error);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (filename);
|
|
|
|
|
|
|
|
filename = gimp_personal_rc_file ("gimprc");
|
|
|
|
|
|
|
|
g_printerr ("parsing '%s' ... \n", filename);
|
|
|
|
if (!gimp_config_deserialize (G_OBJECT (gimprc), filename, NULL, &error))
|
|
|
|
{
|
|
|
|
if (error->code != GIMP_CONFIG_ERROR_OPEN_ENOENT)
|
|
|
|
g_message (error->message);
|
|
|
|
|
|
|
|
g_clear_error (&error);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (filename);
|
|
|
|
}
|
|
|
|
|
2002-02-08 22:05:10 +08:00
|
|
|
/**
|
2002-05-15 19:59:51 +08:00
|
|
|
* gimp_rc_save:
|
|
|
|
* @user_rc: the current #GimpRc.
|
|
|
|
* @global_rc: the global #GimpRC.
|
|
|
|
*
|
|
|
|
* Saves the users gimprc file. If you pass a global GimpRC, only the
|
|
|
|
* differences between the global and the users configuration is saved.
|
2002-02-08 22:05:10 +08:00
|
|
|
**/
|
2002-05-15 19:59:51 +08:00
|
|
|
void
|
|
|
|
gimp_rc_save (GimpRc *user_rc,
|
|
|
|
GimpRc *global_rc)
|
2001-12-29 00:26:54 +08:00
|
|
|
{
|
2001-12-29 06:58:14 +08:00
|
|
|
const gchar *top =
|
2002-05-15 19:59:51 +08:00
|
|
|
"# GIMP gimprc\n"
|
|
|
|
"#\n"
|
2001-12-29 06:58:14 +08:00
|
|
|
"# This is your personal gimprc file. Any variable defined in this file\n"
|
|
|
|
"# takes precedence over the value defined in the system-wide gimprc:\n"
|
2002-03-24 01:58:57 +08:00
|
|
|
"# ";
|
2001-12-29 06:58:14 +08:00
|
|
|
const gchar *bottom =
|
|
|
|
"\n"
|
2002-03-24 01:58:57 +08:00
|
|
|
"# Most values can be set within The GIMP by changing some options in\n"
|
2002-05-22 01:33:04 +08:00
|
|
|
"# the Preferences dialog.\n";
|
2002-05-15 19:59:51 +08:00
|
|
|
const gchar *footer =
|
|
|
|
"# end of gimprc\n";
|
2001-12-29 06:58:14 +08:00
|
|
|
|
2002-05-15 19:59:51 +08:00
|
|
|
gchar *header;
|
|
|
|
gchar *filename;
|
|
|
|
gchar *system_filename;
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
g_return_if_fail (GIMP_IS_RC (user_rc));
|
|
|
|
g_return_if_fail (global_rc == NULL || GIMP_IS_RC (global_rc));
|
|
|
|
|
|
|
|
system_filename = g_build_filename (gimp_sysconf_directory (),
|
|
|
|
"gimprc", NULL);
|
|
|
|
header = g_strconcat (top, system_filename, bottom, NULL);
|
|
|
|
g_free (system_filename);
|
2001-12-29 06:58:14 +08:00
|
|
|
|
2002-05-15 19:59:51 +08:00
|
|
|
filename = gimp_personal_rc_file ("gimprc");
|
2002-05-15 19:05:32 +08:00
|
|
|
|
2002-05-15 19:59:51 +08:00
|
|
|
if (! gimp_config_serialize (G_OBJECT (user_rc),
|
|
|
|
filename, header, footer, global_rc, &error))
|
|
|
|
{
|
|
|
|
g_message (error->message);
|
|
|
|
g_error_free (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (filename);
|
|
|
|
g_free (header);
|
2001-12-29 06:58:14 +08:00
|
|
|
}
|