Commit b10ade1a authored by Michael Natterer's avatar Michael Natterer 😴 Committed by Michael Natterer

app/config/gimpconfig.[ch] added "gint indent_level" to all serialization

2002-05-24  Michael Natterer  <mitch@gimp.org>

	* app/config/gimpconfig.[ch]
	* app/config/gimpconfig-serialize.[ch]: added "gint indent_level"
	to all serialization functions. Added gimp_config_string_indent()
	which indents a GString.

	* app/config/gimprc.c
	* app/core/gimpdocumentlist.c
	* app/core/gimpparasitelist.c: changed accordingly.

	* app/core/gimpcontext.c: don't crash when serializing NULL object
	properties.

	* app/config/gimpscanner.[ch]: added gimp_scanner_parse_identifier().

	* app/core/gimpcontainer.c: implement GimpConfigInterface.
	Container serialization works, deserialization is not implemented yet.

	* app/widgets/gimpdevices.[ch]: made
	GimpDeviceManager->device_info_list a GimpList, not GList, added
	testing functions for (de)serializing it.

	* app/gui/menus.c
	* app/gui/test-commands.[ch]: call the new test functions from
	the <Toolbox>/File/Debug menu.
parent 3a7654ec
2002-05-24 Michael Natterer <mitch@gimp.org>
* app/config/gimpconfig.[ch]
* app/config/gimpconfig-serialize.[ch]: added "gint indent_level"
to all serialization functions. Added gimp_config_string_indent()
which indents a GString.
* app/config/gimprc.c
* app/core/gimpdocumentlist.c
* app/core/gimpparasitelist.c: changed accordingly.
* app/core/gimpcontext.c: don't crash when serializing NULL object
properties.
* app/config/gimpscanner.[ch]: added gimp_scanner_parse_identifier().
* app/core/gimpcontainer.c: implement GimpConfigInterface.
Container serialization works, deserialization is not implemented yet.
* app/widgets/gimpdevices.[ch]: made
GimpDeviceManager->device_info_list a GimpList, not GList, added
testing functions for (de)serializing it.
* app/gui/menus.c
* app/gui/test-commands.[ch]: call the new test functions from
the <Toolbox>/File/Debug menu.
2002-05-24 Michael Natterer <mitch@gimp.org>
* app/display/gimpdisplayshell-callbacks.c: copied some code from
......
......@@ -61,13 +61,15 @@ static void serialize_unknown_token (const gchar *key,
**/
gboolean
gimp_config_serialize_properties (GObject *object,
gint fd)
gint fd,
gint indent_level)
{
GObjectClass *klass;
GParamSpec **property_specs;
guint n_property_specs;
guint i;
GString *str;
gboolean property_written = FALSE;
g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
......@@ -89,11 +91,19 @@ gimp_config_serialize_properties (GObject *object,
if (! (prop_spec->flags & GIMP_PARAM_SERIALIZE))
continue;
g_string_printf (str, "(%s ", prop_spec->name);
if (property_written)
g_string_assign (str, "\n");
else
g_string_assign (str, "");
gimp_config_string_indent (str, indent_level);
g_string_append_printf (str, "(%s ", prop_spec->name);
if (gimp_config_serialize_property (object, prop_spec, str, TRUE))
{
g_string_append (str, ")\n");
g_string_append (str, ")");
property_written = TRUE;
if (write (fd, str->str, str->len) == -1)
return FALSE;
......@@ -126,13 +136,15 @@ gimp_config_serialize_properties (GObject *object,
gboolean
gimp_config_serialize_changed_properties (GObject *new,
GObject *old,
gint fd)
gint fd,
gint indent_level)
{
GObjectClass *klass;
GParamSpec **property_specs;
guint n_property_specs;
guint i;
GString *str;
gboolean property_written = FALSE;
g_return_val_if_fail (G_IS_OBJECT (new), FALSE);
g_return_val_if_fail (G_IS_OBJECT (old), FALSE);
......@@ -167,12 +179,20 @@ gimp_config_serialize_changed_properties (GObject *new,
if (!gimp_config_values_equal (&new_value, &old_value))
{
g_string_printf (str, "(%s ", prop_spec->name);
if (property_written)
g_string_assign (str, "\n");
else
g_string_assign (str, "");
gimp_config_string_indent (str, indent_level);
g_string_append_printf (str, "(%s ", prop_spec->name);
if (gimp_config_serialize_value (&new_value, str, TRUE))
{
g_string_append (str, ")\n");
property_written = TRUE;
if (write (fd, str->str, str->len) == -1)
return FALSE;
}
......@@ -316,7 +336,8 @@ gimp_config_serialize_value (const GValue *value,
**/
gboolean
gimp_config_serialize_unknown_tokens (GObject *object,
gint fd)
gint fd,
gint indent_level)
{
GString *str;
......
......@@ -24,12 +24,15 @@
gboolean gimp_config_serialize_properties (GObject *object,
gint fd);
gint fd,
gint indent_level);
gboolean gimp_config_serialize_changed_properties (GObject *new,
GObject *old,
gint fd);
gint fd,
gint indent_level);
gboolean gimp_config_serialize_unknown_tokens (GObject *object,
gint fd);
gint fd,
gint indent_level);
gboolean gimp_config_serialize_value (const GValue *value,
GString *str,
gboolean escaped);
......
......@@ -49,6 +49,7 @@ static void gimp_config_iface_init (GimpConfigInterface *gimp_config_iface);
static gboolean gimp_config_iface_serialize (GObject *object,
gint fd,
gint indent_level,
gpointer data);
static gboolean gimp_config_iface_deserialize (GObject *object,
GScanner *scanner,
......@@ -101,9 +102,10 @@ gimp_config_iface_init (GimpConfigInterface *gimp_config_iface)
static gboolean
gimp_config_iface_serialize (GObject *object,
gint fd,
gint indent_level,
gpointer data)
{
return gimp_config_serialize_properties (object, fd);
return gimp_config_serialize_properties (object, fd, indent_level);
}
static gboolean
......@@ -221,7 +223,7 @@ gimp_config_serialize (GObject *object,
write (fd, "\n", 1) != -1);
if (success)
success = gimp_config_iface->serialize (object, fd, data);
success = gimp_config_iface->serialize (object, fd, 0, data);
if (success && footer)
success = (write (fd, "\n", 1) != -1 &&
......@@ -333,6 +335,19 @@ gimp_config_error_quark (void)
return q;
}
void
gimp_config_string_indent (GString *string,
gint indent_level)
{
gint indent;
g_return_if_fail (string != NULL);
g_return_if_fail (indent_level > 0);
for (indent = 0; indent < indent_level; indent++)
g_string_append (string, " ");
}
static void
gimp_config_scanner_message (GScanner *scanner,
gchar *message,
......
......@@ -42,6 +42,7 @@ struct _GimpConfigInterface
gboolean (* serialize) (GObject *object,
gint fd,
gint indent_level,
gpointer data);
gboolean (* deserialize) (GObject *object,
GScanner *scanner,
......@@ -96,7 +97,10 @@ void gimp_config_foreach_unknown_token (GObject *object,
#define GIMP_CONFIG_ERROR (gimp_config_error_quark ())
GQuark gimp_config_error_quark (void) G_GNUC_CONST;
GQuark gimp_config_error_quark (void) G_GNUC_CONST;
void gimp_config_string_indent (GString *string,
gint indent_level);
#endif /* __GIMP_CONFIG_H__ */
......@@ -40,6 +40,7 @@ static void gimp_rc_config_iface_init (gpointer iface,
gpointer iface_data);
static gboolean gimp_rc_serialize (GObject *object,
gint fd,
gint indent_level,
gpointer data);
static gboolean gimp_rc_deserialize (GObject *object,
GScanner *scanner,
......@@ -99,6 +100,7 @@ gimp_rc_config_iface_init (gpointer iface,
static gboolean
gimp_rc_serialize (GObject *object,
gint fd,
gint indent_level,
gpointer data)
{
gboolean success;
......@@ -106,12 +108,13 @@ gimp_rc_serialize (GObject *object,
if (data && GIMP_IS_RC (data))
success = gimp_config_serialize_changed_properties (object,
G_OBJECT (data),
fd);
fd,
indent_level);
else
success = gimp_config_serialize_properties (object, fd);
success = gimp_config_serialize_properties (object, fd, indent_level);
if (success)
success = gimp_config_serialize_unknown_tokens (object, fd);
success = gimp_config_serialize_unknown_tokens (object, fd, indent_level);
return success;
}
......
......@@ -87,6 +87,21 @@ gimp_scanner_parse_token (GScanner *scanner,
return TRUE;
}
gboolean
gimp_scanner_parse_identifier (GScanner *scanner,
const gchar *identifier)
{
if (g_scanner_peek_next_token (scanner) != G_TOKEN_IDENTIFIER)
return FALSE;
g_scanner_get_next_token (scanner);
if (strcmp (scanner->value.v_identifier, identifier))
return FALSE;
return TRUE;
}
gboolean
gimp_scanner_parse_string (GScanner *scanner,
gchar **dest)
......
......@@ -29,6 +29,8 @@ void gimp_scanner_destroy (GScanner *scanner);
gboolean gimp_scanner_parse_token (GScanner *scanner,
GTokenType token);
gboolean gimp_scanner_parse_identifier (GScanner *scanner,
const gchar *identifier);
gboolean gimp_scanner_parse_string (GScanner *scanner,
gchar **dest);
gboolean gimp_scanner_parse_string_no_validate (GScanner *scanner,
......
......@@ -22,6 +22,9 @@
#include "config.h"
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <glib-object.h>
......@@ -30,6 +33,8 @@
#include "gimpcontainer.h"
#include "gimpmarshal.h"
#include "config/gimpconfig.h"
/* #define DEBUG_CONTAINER */
......@@ -70,21 +75,30 @@ enum
/* local function prototypes */
static void gimp_container_class_init (GimpContainerClass *klass);
static void gimp_container_init (GimpContainer *container);
static void gimp_container_class_init (GimpContainerClass *klass);
static void gimp_container_init (GimpContainer *container);
static void gimp_container_config_iface_init (GimpConfigInterface *config_iface);
static void gimp_container_dispose (GObject *object);
static void gimp_container_dispose (GObject *object);
static void gimp_container_set_property (GObject *object,
static void gimp_container_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec);
static void gimp_container_get_property (GObject *object,
static void gimp_container_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec);
static gsize gimp_container_get_memsize (GimpObject *object);
static gsize gimp_container_get_memsize (GimpObject *object);
static gboolean gimp_container_serialize (GObject *object,
gint fd,
gint indent_level,
gpointer data);
static gboolean gimp_container_deserialize (GObject *object,
GScanner *scanner,
gpointer data);
static void gimp_container_disconnect_callback (GimpObject *object,
gpointer data);
......@@ -135,10 +149,20 @@ gimp_container_get_type (void)
0, /* n_preallocs */
(GInstanceInitFunc) gimp_container_init,
};
static const GInterfaceInfo config_iface_info =
{
(GInterfaceInitFunc) gimp_container_config_iface_init,
NULL, /* iface_finalize */
NULL /* iface_data */
};
container_type = g_type_register_static (GIMP_TYPE_OBJECT,
"GimpContainer",
&container_info, 0);
g_type_add_interface_static (container_type,
GIMP_TYPE_CONFIG_INTERFACE,
&config_iface_info);
}
return container_type;
......@@ -248,6 +272,13 @@ gimp_container_init (GimpContainer *container)
container->freeze_count = 0;
}
static void
gimp_container_config_iface_init (GimpConfigInterface *config_iface)
{
config_iface->serialize = gimp_container_serialize;
config_iface->deserialize = gimp_container_deserialize;
}
static void
gimp_container_dispose (GObject *object)
{
......@@ -343,6 +374,107 @@ gimp_container_get_memsize (GimpObject *object)
return memsize + GIMP_OBJECT_CLASS (parent_class)->get_memsize (object);
}
typedef struct
{
gint fd;
gint indent_level;
gpointer data;
gboolean success;
} SerializeData;
static void
gimp_container_serialize_foreach (GObject *object,
SerializeData *serialize_data)
{
GimpConfigInterface *config_iface;
GString *str;
const gchar *name;
config_iface = GIMP_GET_CONFIG_INTERFACE (object);
if (! config_iface)
serialize_data->success = FALSE;
if (! serialize_data->success)
return;
str = g_string_new (NULL);
gimp_config_string_indent (str, serialize_data->indent_level);
g_string_append_printf (str, "(%s ",
g_type_name (G_TYPE_FROM_INSTANCE (object)));
name = gimp_object_get_name (GIMP_OBJECT (object));
if (name)
{
gchar *escaped;
escaped = g_strescape (name, NULL);
g_string_append_printf (str, "\"%s\"\n", escaped);
g_free (escaped);
}
else
{
g_string_append_printf (str, "NULL\n");
}
if (write (serialize_data->fd, str->str, str->len) == -1)
{
serialize_data->success = FALSE;
return;
}
serialize_data->success = config_iface->serialize (object,
serialize_data->fd,
serialize_data->indent_level + 1,
serialize_data->data);
if (! serialize_data->success)
return;
g_string_assign (str, ")\n");
if (write (serialize_data->fd, str->str, str->len) == -1)
serialize_data->success = FALSE;
}
static gboolean
gimp_container_serialize (GObject *object,
gint fd,
gint indent_level,
gpointer data)
{
GimpContainer *container;
SerializeData serialize_data;
container = GIMP_CONTAINER (object);
serialize_data.fd = fd;
serialize_data.indent_level = indent_level;
serialize_data.data = data;
serialize_data.success = TRUE;
gimp_container_foreach (container,
(GFunc) gimp_container_serialize_foreach,
&serialize_data);
return serialize_data.success;
}
static gboolean
gimp_container_deserialize (GObject *object,
GScanner *scanner,
gpointer data)
{
GimpContainer *container;
container = GIMP_CONTAINER (object);
return TRUE;
}
static void
gimp_container_disconnect_callback (GimpObject *object,
gpointer data)
......
......@@ -952,7 +952,6 @@ gimp_context_serialize_property (GObject *object,
{
GimpContext *context;
GimpObject *serialize_obj;
gchar *escaped;
context = GIMP_CONTEXT (object);
......@@ -970,11 +969,18 @@ gimp_context_serialize_property (GObject *object,
return FALSE;
}
escaped = g_strescape (gimp_object_get_name (serialize_obj), NULL);
g_string_append_printf (string, "\"%s\"", escaped);
if (serialize_obj)
{
gchar *escaped;
g_free (escaped);
escaped = g_strescape (gimp_object_get_name (serialize_obj), NULL);
g_string_append_printf (string, "\"%s\"", escaped);
g_free (escaped);
}
else
{
g_string_append_printf (string, "NULL");
}
return TRUE;
}
......@@ -1025,7 +1031,11 @@ gimp_context_deserialize_property (GObject *object,
return FALSE;
}
if (gimp_scanner_parse_string (scanner, &object_name))
if (gimp_scanner_parse_identifier (scanner, "NULL"))
{
g_value_set_object (value, NULL);
}
else if (gimp_scanner_parse_string (scanner, &object_name))
{
GimpObject *deserialize_obj;
......
......@@ -40,6 +40,7 @@ static void gimp_document_list_config_iface_init (gpointer iface,
gpointer iface_data);
static gboolean gimp_document_list_serialize (GObject *list,
gint fd,
gint indent_level,
gpointer data);
static gboolean gimp_document_list_deserialize (GObject *list,
GScanner *scanner,
......@@ -100,6 +101,7 @@ gimp_document_list_config_iface_init (gpointer iface,
static gboolean
gimp_document_list_serialize (GObject *document_list,
gint fd,
gint indent_level,
gpointer data)
{
GList *list;
......
......@@ -54,6 +54,7 @@ static void gimp_parasite_list_config_iface_init (gpointer iface,
gpointer iface_data);
static gboolean gimp_parasite_list_serialize (GObject *list,
gint fd,
gint indent_level,
gpointer data);
static gboolean gimp_parasite_list_deserialize (GObject *list,
GScanner *scanner,
......@@ -231,6 +232,7 @@ gimp_parasite_list_get_memsize (GimpObject *object)
static gboolean
gimp_parasite_list_serialize (GObject *list,
gint fd,
gint indent_level,
gpointer data)
{
if (GIMP_PARASITE_LIST (list)->table)
......
......@@ -240,6 +240,12 @@ static GimpItemFactoryEntry toolbox_entries[] =
{ { "/File/Debug/Deserialize User Context", NULL,
test_deserialize_context_cmd_callback, 0 },
NULL, NULL, NULL },
{ { "/File/Debug/Serialize test-devicerc", NULL,
test_serialize_devicerc_cmd_callback, 0 },
NULL, NULL, NULL },
{ { "/File/Debug/Deserialize test-devicerc", NULL,
test_deserialize_devicerc_cmd_callback, 0 },
NULL, NULL, NULL },
#endif
SEPARATOR ("/File/---"),
......
......@@ -43,6 +43,7 @@
#include "widgets/gimpdialogfactory.h"
#include "widgets/gimpdock.h"
#include "widgets/gimpimagedock.h"
#include "widgets/gimpdevices.h"
#include "widgets/gimpdockable.h"
#include "widgets/gimpdockbook.h"
#include "widgets/gimpdrawablelistview.h"
......@@ -339,3 +340,25 @@ test_deserialize_context_cmd_callback (GtkWidget *widget,
g_free (filename);
}
void
test_serialize_devicerc_cmd_callback (GtkWidget *widget,
gpointer data)
{
Gimp *gimp;
gimp = GIMP (data);
gimp_devices_save_test (gimp);
}
void
test_deserialize_devicerc_cmd_callback (GtkWidget *widget,
gpointer data)
{
Gimp *gimp;
gimp = GIMP (data);
gimp_devices_restore_test (gimp);
}
......@@ -29,6 +29,10 @@ void test_serialize_context_cmd_callback (GtkWidget *widget,
gpointer data);
void test_deserialize_context_cmd_callback (GtkWidget *widget,
gpointer data);
void test_serialize_devicerc_cmd_callback (GtkWidget *widget,
gpointer data);
void test_deserialize_devicerc_cmd_callback (GtkWidget *widget,
gpointer data);
#endif /* __TEST_COMMANDS_H__ */
......@@ -240,6 +240,12 @@ static GimpItemFactoryEntry toolbox_entries[] =
{ { "/File/Debug/Deserialize User Context", NULL,
test_deserialize_context_cmd_callback, 0 },
NULL, NULL, NULL },
{ { "/File/Debug/Serialize test-devicerc", NULL,
test_serialize_devicerc_cmd_callback, 0 },
NULL, NULL, NULL },
{ { "/File/Debug/Deserialize test-devicerc", NULL,
test_deserialize_devicerc_cmd_callback, 0 },
NULL, NULL, NULL },
#endif
SEPARATOR ("/File/---"),
......
......@@ -31,12 +31,14 @@
#include "core/gimp.h"
#include "core/gimpbrush.h"
#include "core/gimpcontainer.h"
#include "core/gimpdatafactory.h"
#include "core/gimpgradient.h"
#include "core/gimplist.h"
#include "core/gimppattern.h"
#include "core/gimptoolinfo.h"
#include "config/gimpconfig.h"
#include "gimpdeviceinfo.h"
#include "gimpdevices.h"
......@@ -50,7 +52,7 @@ typedef struct _GimpDeviceManager GimpDeviceManager;
struct _GimpDeviceManager
{
GList *device_info_list;
GimpContainer *device_info_list;
GdkDevice *current_device;
GimpDeviceChangeNotify change_notify;
};
......@@ -82,8 +84,10 @@ gimp_devices_init (Gimp *gimp,
GIMP_DEVICE_MANAGER_DATA_KEY, manager,
(GDestroyNotify) g_free);
manager->current_device = gdk_device_get_core_pointer ();
manager->change_notify = change_notify;
manager->device_info_list = gimp_list_new (GIMP_TYPE_DEVICE_INFO,
GIMP_CONTAINER_POLICY_STRONG);
manager->current_device = gdk_device_get_core_pointer ();
manager->change_notify = change_notify;
/* create device info structures for present devices */
for (list = gdk_devices_list (); list; list = g_list_next (list))
......@@ -91,9 +95,8 @@ gimp_devices_init (Gimp *gimp,
device = (GdkDevice *) list->data;
device_info = gimp_device_info_new (gimp, device->name);
manager->device_info_list = g_list_append (manager->device_info_list,
device_info);
gimp_container_add (manager->device_info_list, GIMP_OBJECT (device_info));
g_object_unref (G_OBJECT (device_info));
gimp_device_info_set_from_device (device_info, device);
}
......@@ -110,9 +113,7 @@ gimp_devices_exit (Gimp *gimp)
g_return_if_fail (manager != NULL);
g_list_foreach (manager->device_info_list, (GFunc) g_object_unref, NULL);
g_list_free (manager->device_info_list);
g_object_unref (G_OBJECT (manager->device_info_list));
g_object_set_data (G_OBJECT (gimp), GIMP_DEVICE_MANAGER_DATA_KEY, NULL);
}
......@@ -164,8 +165,8 @@ gimp_devices_save (Gimp *gimp)
if (fp)
{
g_list_foreach (manager->device_info_list,
(GFunc) gimp_device_info_save, fp);
gimp_container_foreach (manager->device_info_list,
(GFunc) gimp_device_info_save, fp);
fclose (fp);
}
else
......@@ -177,6 +178,53 @@ gimp_devices_save (Gimp *gimp)
g_free (filename);
}
void
gimp_devices_restore_test (Gimp *gimp)
{
GimpDeviceManager *manager;
gchar *filename;
GError *error = NULL;
g_return_if_fail (GIMP_IS_GIMP (gimp));
manager = gimp_device_manager_get (gimp);
g_return_if_fail (manager != NULL);
filename = gimp_personal_rc_file ("test-devicerc");
g_free (filename);
}
void
gimp_devices_save_test (Gimp *gimp)
{
GimpDeviceManager *manager;
gchar *filename;
GError *error = NULL;
g_return_if_fail (GIMP_IS_GIMP (gimp));
manager = gimp_device_manager_get (gimp);
g_return_if_fail (manager != NULL);
filename = gimp_personal_rc_file ("test-devicerc");
if (! gimp_config_serialize (G_OBJECT (manager->device_info_list),
filename,
"# test-devicerc",
"# end test-devicerc",
NULL,
&error))
{
g_message ("Could not write test-devicerc: %s", error->message);
g_clear_error (&error);
}
g_free (filename);
}
GdkDevice *
gimp_devices_get_current (Gimp *gimp)
{
......@@ -295,7 +343,6 @@ gimp_devices_rc_update (Gimp *gimp,
{
GimpDeviceManager *manager;