Basic implementation of saving.

When the objects are stored, ensure that they have a "type" member
that makes it possible to determine their class/purpose.
This commit is contained in:
Přemysl Eric Janouch 2011-01-15 18:02:13 +01:00
parent 1201caf8a4
commit e9da09f196

View File

@ -50,6 +50,9 @@ static void ld_diagram_dispose (GObject *gobject);
static void ld_diagram_finalize (GObject *gobject); static void ld_diagram_finalize (GObject *gobject);
static gboolean write_signature (GOutputStream *stream, GError **error); static gboolean write_signature (GOutputStream *stream, GError **error);
static JsonNode *serialize_diagram (LdDiagram *self);
static JsonNode *serialize_object (LdDiagramObject *object);
static const gchar *get_object_class_string (GType type);
static void ld_diagram_real_changed (LdDiagram *self); static void ld_diagram_real_changed (LdDiagram *self);
static void ld_diagram_clear_internal (LdDiagram *self); static void ld_diagram_clear_internal (LdDiagram *self);
@ -307,12 +310,10 @@ ld_diagram_save_to_file (LdDiagram *self,
return FALSE; return FALSE;
} }
/* TODO: Implement saving for real. This is just a stub. */
generator = json_generator_new (); generator = json_generator_new ();
g_object_set (generator, "pretty", TRUE, NULL); g_object_set (generator, "pretty", TRUE, NULL);
/* XXX: json-glib dislikes empty objects. */ root = serialize_diagram (self);
root = json_node_new (JSON_NODE_OBJECT);
json_generator_set_root (generator, root); json_generator_set_root (generator, root);
json_node_free (root); json_node_free (root);
@ -346,6 +347,58 @@ write_signature (GOutputStream *stream, GError **error)
return TRUE; return TRUE;
} }
static JsonNode *
serialize_diagram (LdDiagram *self)
{
JsonNode *root_node;
JsonObject *root_object;
JsonArray *objects_array;
GList *iter;
root_node = json_node_new (JSON_NODE_OBJECT);
root_object = json_object_new ();
json_node_take_object (root_node, root_object);
objects_array = json_array_new ();
iter = g_list_last (self->priv->objects);
for (; iter; iter = g_list_previous (iter))
json_array_add_element (objects_array,
serialize_object (LD_DIAGRAM_OBJECT (iter->data)));
json_object_set_int_member (root_object, "version", 1);
json_object_set_array_member (root_object, "objects", objects_array);
return root_node;
}
static JsonNode *
serialize_object (LdDiagramObject *object)
{
JsonNode *object_node, *object_type_node;
JsonObject *object_storage;
object_node = json_node_new (JSON_NODE_OBJECT);
object_storage = ld_diagram_object_get_storage (object);
object_type_node = json_object_get_member (object_storage, "type");
if (!object_type_node || !JSON_NODE_HOLDS_VALUE (object_type_node))
json_object_set_string_member (object_storage,
"type", get_object_class_string (G_OBJECT_TYPE (object)));
json_node_set_object (object_node, object_storage);
return object_node;
}
static const gchar *
get_object_class_string (GType type)
{
if (type == LD_TYPE_DIAGRAM_SYMBOL)
return "symbol";
if (type != LD_TYPE_DIAGRAM_OBJECT)
/* We don't know our own type, that's just plain wrong. */
g_warn_if_reached ();
return "object";
}
/** /**
* ld_diagram_get_modified: * ld_diagram_get_modified:
* @self: An #LdDiagram object. * @self: An #LdDiagram object.