summaryrefslogtreecommitdiff
path: root/liblogdiag/ld-diagram.c
blob: bac45dbd6abdb2f2b57ac4677f95e021215d7b11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
/*
 * ld-diagram.c
 *
 * This file is a part of logdiag.
 * Copyright Přemysl Janouch 2010 - 2011. All rights reserved.
 *
 * See the file LICENSE for licensing information.
 *
 */

#include "liblogdiag.h"
#include "config.h"


/**
 * SECTION:ld-diagram
 * @short_description: A model for diagrams
 * @see_also: #LdDiagramView
 *
 * #LdDiagram is a model used for storing diagrams.
 */

/*
 * LdDiagramPrivate:
 * @modified: whether the diagram has been modified.
 * @lock_history: whether the history stacks are currently locked.
 * @in_user_action: how many times a user action has been initiated.
 * @undo_stack: a stack of actions that can be undone,
 *              each containing a #GList of #LdUndoAction subactions.
 * @redo_stack: a stack of undone actions that can be redone,
 *              each containing a #GList of #LdUndoAction subactions.
 * @objects: all objects in the diagram.
 * @selection: all currently selected objects.
 */
struct _LdDiagramPrivate
{
	gboolean modified;
	gboolean lock_history;
	guint in_user_action;
	GList *undo_stack;
	GList *redo_stack;

	GList *objects;
	GList *selection;
};

typedef struct _ObjectActionData ObjectActionData;

/*
 * ObjectActionData:
 * @self: an #LdDiagram object.
 * @object: an #LdDiagramObject object.
 * @pos: the position at which the object has been inserted or removed.
 */
struct _ObjectActionData
{
	gpointer self;
	LdDiagramObject *object;
	gint pos;
};

enum
{
	PROP_0,
	PROP_MODIFIED,
	PROP_CAN_UNDO,
	PROP_CAN_REDO
};

static void ld_diagram_get_property (GObject *object, guint property_id,
	GValue *value, GParamSpec *pspec);
static void ld_diagram_set_property (GObject *object, guint property_id,
	const GValue *value, GParamSpec *pspec);
static void ld_diagram_dispose (GObject *gobject);
static void ld_diagram_finalize (GObject *gobject);
static void ld_diagram_real_changed (LdDiagram *self);
static void ld_diagram_clear_internal (LdDiagram *self, gboolean emit_signals);

static gboolean write_signature (GOutputStream *stream, GError **error);

static gboolean check_node (JsonNode *node, JsonNodeType type,
	const gchar *id, GError **error);
static gboolean deserialize_diagram (LdDiagram *self, JsonNode *root,
	GError **error);
static LdDiagramObject *deserialize_object (JsonObject *object_storage);

static JsonNode *serialize_diagram (LdDiagram *self);
static JsonNode *serialize_object (LdDiagramObject *object);
static const gchar *get_object_class_string (GType type);

static void push_undo_action (LdDiagram *self, LdUndoAction *action);
static void destroy_action_stack (GList **stack);

static void on_object_changed (LdDiagramObject *object,
	LdUndoAction *action, gpointer user_data);
static void on_object_notify_storage (LdDiagramObject *object,
	GParamSpec *pspec, gpointer user_data);

static void on_object_action_insert (gpointer user_data);
static void on_object_action_remove (gpointer user_data);
static void on_object_action_destroy (gpointer user_data);

static void install_object (LdDiagramObject *object, LdDiagram *self);
static void uninstall_object (LdDiagramObject *object, LdDiagram *self);
static void ld_diagram_unselect_all_internal (LdDiagram *self);


G_DEFINE_TYPE (LdDiagram, ld_diagram, G_TYPE_OBJECT);

static void
ld_diagram_class_init (LdDiagramClass *klass)
{
	GObjectClass *object_class;
	GParamSpec *pspec;

	object_class = G_OBJECT_CLASS (klass);
	object_class->get_property = ld_diagram_get_property;
	object_class->set_property = ld_diagram_set_property;
	object_class->dispose = ld_diagram_dispose;
	object_class->finalize = ld_diagram_finalize;

	klass->changed = ld_diagram_real_changed;

/**
 * LdDiagram:modified:
 *
 * Whether the diagram has been modified.
 */
	pspec = g_param_spec_boolean ("modified", "Modified",
		"Whether the diagram has been modified.",
		FALSE, G_PARAM_READWRITE);
	g_object_class_install_property (object_class, PROP_MODIFIED, pspec);

/**
 * LdDiagram:can-undo:
 *
 * Whether any action can be undone.
 */
	pspec = g_param_spec_boolean ("can-undo", "Can undo",
		"Whether any action can be undone.",
		FALSE, G_PARAM_READABLE);
	g_object_class_install_property (object_class, PROP_CAN_UNDO, pspec);

/**
 * LdDiagram:can-redo:
 *
 * Whether any undone action can be redone.
 */
	pspec = g_param_spec_boolean ("can-redo", "Can redo",
		"Whether any undone action can be redone.",
		FALSE, G_PARAM_READABLE);
	g_object_class_install_property (object_class, PROP_CAN_REDO, pspec);

/**
 * LdDiagram::changed:
 * @self: an #LdDiagram object.
 *
 * Contents of the diagram have changed.
 */
	klass->changed_signal = g_signal_new
		("changed", G_TYPE_FROM_CLASS (klass),
		G_SIGNAL_RUN_LAST,
		G_STRUCT_OFFSET (LdDiagramClass, changed), NULL, NULL,
		g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);

/**
 * LdDiagram::selection-changed:
 * @self: an #LdDiagram object.
 *
 * The current selection has changed.
 */
	klass->selection_changed_signal = g_signal_new
		("selection-changed", G_TYPE_FROM_CLASS (klass),
		G_SIGNAL_RUN_LAST,
		G_STRUCT_OFFSET (LdDiagramClass, selection_changed), NULL, NULL,
		g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);

	g_type_class_add_private (klass, sizeof (LdDiagramPrivate));
}

static void
ld_diagram_init (LdDiagram *self)
{
	self->priv = G_TYPE_INSTANCE_GET_PRIVATE
		(self, LD_TYPE_DIAGRAM, LdDiagramPrivate);
}

static void
ld_diagram_get_property (GObject *object, guint property_id,
	GValue *value, GParamSpec *pspec)
{
	LdDiagram *self;

	self = LD_DIAGRAM (object);
	switch (property_id)
	{
	case PROP_MODIFIED:
		g_value_set_boolean (value, ld_diagram_get_modified (self));
		break;
	case PROP_CAN_UNDO:
		g_value_set_boolean (value, ld_diagram_can_undo (self));
		break;
	case PROP_CAN_REDO:
		g_value_set_boolean (value, ld_diagram_can_redo (self));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
	}
}

static void
ld_diagram_set_property (GObject *object, guint property_id,
	const GValue *value, GParamSpec *pspec)
{
	LdDiagram *self;

	self = LD_DIAGRAM (object);
	switch (property_id)
	{
	case PROP_MODIFIED:
		ld_diagram_set_modified (self, g_value_get_boolean (value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
	}
}

static void
ld_diagram_dispose (GObject *gobject)
{
	LdDiagram *self;

	self = LD_DIAGRAM (gobject);
	ld_diagram_clear_internal (self, FALSE);

	/* Chain up to the parent class. */
	G_OBJECT_CLASS (ld_diagram_parent_class)->dispose (gobject);
}

static void
ld_diagram_finalize (GObject *gobject)
{
	/* Chain up to the parent class. */
	G_OBJECT_CLASS (ld_diagram_parent_class)->finalize (gobject);
}

static void
ld_diagram_real_changed (LdDiagram *self)
{
	g_return_if_fail (LD_IS_DIAGRAM (self));

	ld_diagram_set_modified (self, TRUE);
}


/**
 * ld_diagram_error_quark:
 *
 * Registers an error quark for #LdDiagram if necessary.
 *
 * Return value: the error quark used for #LdDiagram errors.
 */
GQuark
ld_diagram_error_quark (void)
{
	return g_quark_from_static_string ("ld-diagram-error-quark");
}

/**
 * ld_diagram_new:
 *
 * Create an instance.
 */
LdDiagram *
ld_diagram_new (void)
{
	return g_object_new (LD_TYPE_DIAGRAM, NULL);
}

/**
 * ld_diagram_clear:
 * @self: an #LdDiagram object.
 *
 * Clear the whole diagram, including it's objects and history.
 */
void
ld_diagram_clear (LdDiagram *self)
{
	g_return_if_fail (LD_IS_DIAGRAM (self));
	ld_diagram_clear_internal (self, TRUE);
}

static void
ld_diagram_clear_internal (LdDiagram *self, gboolean emit_signals)
{
	gboolean changed = FALSE;
	gboolean selection_changed = FALSE;

	if (self->priv->selection)
	{
		ld_diagram_unselect_all_internal (self);
		selection_changed = TRUE;
	}

	if (self->priv->objects)
	{
		g_list_foreach (self->priv->objects, (GFunc) uninstall_object, self);
		g_list_free (self->priv->objects);
		self->priv->objects = NULL;
		changed = TRUE;
	}

	destroy_action_stack (&self->priv->undo_stack);
	destroy_action_stack (&self->priv->redo_stack);

	if (emit_signals)
	{
		g_object_notify (G_OBJECT (self), "can-undo");
		g_object_notify (G_OBJECT (self), "can-redo");

		if (changed)
			g_signal_emit (self,
				LD_DIAGRAM_GET_CLASS (self)->changed_signal, 0);
		if (selection_changed)
			g_signal_emit (self,
				LD_DIAGRAM_GET_CLASS (self)->selection_changed_signal, 0);
	}
}

/**
 * ld_diagram_load_from_file:
 * @self: an #LdDiagram object.
 * @filename: a filename.
 * @error: (allow-none): return location for a #GError, or %NULL.
 *
 * Clear the diagram and load a file into it.
 *
 * Return value: %TRUE if the file could be loaded, %FALSE otherwise.
 */
gboolean
ld_diagram_load_from_file (LdDiagram *self,
	const gchar *filename, GError **error)
{
	JsonParser *parser;
	GError *local_error;

	g_return_val_if_fail (LD_IS_DIAGRAM (self), FALSE);
	g_return_val_if_fail (filename != NULL, FALSE);

	parser = json_parser_new ();

	local_error = NULL;
	json_parser_load_from_file (parser, filename, &local_error);
	if (local_error)
	{
		g_propagate_error (error, local_error);
		g_object_unref (parser);
		return FALSE;
	}

	ld_diagram_clear (self);

	self->priv->lock_history = TRUE;

	local_error = NULL;
	deserialize_diagram (self, json_parser_get_root (parser), &local_error);

	self->priv->lock_history = FALSE;

	g_object_unref (parser);
	if (local_error)
	{
		g_propagate_error (error, local_error);
		return FALSE;
	}
	return TRUE;
}

/**
 * ld_diagram_save_to_file:
 * @self: an #LdDiagram object.
 * @filename: a filename.
 * @error: (allow-none): return location for a #GError, or %NULL.
 *
 * Save the diagram into a file.
 *
 * Return value: %TRUE if the diagram could be saved, %FALSE otherwise.
 */
gboolean
ld_diagram_save_to_file (LdDiagram *self,
	const gchar *filename, GError **error)
{
	GFile *file;
	GFileOutputStream *file_stream;
	JsonGenerator *generator;
	JsonNode *root;
	gchar *buffer;
	gsize length;
	GError *local_error;

	g_return_val_if_fail (LD_IS_DIAGRAM (self), FALSE);
	g_return_val_if_fail (filename != NULL, FALSE);

	file = g_file_new_for_path (filename);

	local_error = NULL;
	file_stream = g_file_replace (file, NULL, FALSE,
		G_FILE_CREATE_NONE, NULL, &local_error);
	g_object_unref (file);

	if (local_error)
	{
		g_propagate_error (error, local_error);
		return FALSE;
	}

	local_error = NULL;
	write_signature (G_OUTPUT_STREAM (file_stream), &local_error);
	if (local_error)
	{
		g_object_unref (file_stream);
		g_propagate_error (error, local_error);
		return FALSE;
	}

	generator = json_generator_new ();
	g_object_set (generator, "pretty", TRUE, NULL);

	root = serialize_diagram (self);
	json_generator_set_root (generator, root);
	json_node_free (root);

	local_error = NULL;
	buffer = json_generator_to_data (generator, &length);
	g_output_stream_write (G_OUTPUT_STREAM (file_stream),
		buffer, length, NULL, &local_error);
	g_object_unref (file_stream);
	g_object_unref (generator);
	g_free (buffer);

	if (local_error)
	{
		g_propagate_error (error, local_error);
		return FALSE;
	}
	return TRUE;
}

static gboolean
write_signature (GOutputStream *stream, GError **error)
{
	static const gchar signature[] = "/* logdiag diagram */\n";
	GError *local_error = NULL;

	g_output_stream_write (stream, signature, sizeof (signature) - 1,
		NULL, &local_error);
	if (local_error)
	{
		g_propagate_error (error, local_error);
		return FALSE;
	}
	return TRUE;
}

static gboolean
check_node (JsonNode *node, JsonNodeType type, const gchar *id, GError **error)
{
	if (!node)
	{
		g_set_error (error, LD_DIAGRAM_ERROR, LD_DIAGRAM_ERROR_DIAGRAM_CORRUPT,
			"%s is missing", id);
		return FALSE;
	}
	if (!JSON_NODE_HOLDS (node, type))
	{
		g_set_error (error, LD_DIAGRAM_ERROR, LD_DIAGRAM_ERROR_DIAGRAM_CORRUPT,
			"%s is of wrong type", id);
		return FALSE;
	}
	return TRUE;
}

static gboolean
deserialize_diagram (LdDiagram *self, JsonNode *root, GError **error)
{
	JsonObject *root_object;
	JsonNode *objects_node;
	GList *iter;

	if (!check_node (root, JSON_NODE_OBJECT, "the root node", error))
		return FALSE;

	root_object = json_node_get_object (root);
	objects_node = json_object_get_member (root_object, "objects");
	if (!check_node (objects_node, JSON_NODE_ARRAY,
		"the `objects' array", error))
		return FALSE;

	iter = json_array_get_elements (json_node_get_array (objects_node));
	for (; iter; iter = g_list_next (iter))
	{
		GError *node_error = NULL;

		check_node (iter->data, JSON_NODE_OBJECT, "object node", &node_error);
		if (node_error)
		{
			g_warning ("%s", node_error->message);
			g_error_free (node_error);
		}
		else
			/* FIXME: Appending is slow. */
			ld_diagram_insert_object (self,
				deserialize_object (json_node_get_object (iter->data)), -1);
	}
	return TRUE;
}

static LdDiagramObject *
deserialize_object (JsonObject *object_storage)
{
	JsonNode *object_type_node;
	const gchar *type;

	json_object_ref (object_storage);
	object_type_node = json_object_get_member (object_storage, "type");

	if (!object_type_node || !JSON_NODE_HOLDS_VALUE (object_type_node))
		goto deserialize_object_default;

	type = json_node_get_string (object_type_node);
	if (!g_strcmp0 ("symbol", type))
		return LD_DIAGRAM_OBJECT (ld_diagram_symbol_new (object_storage));
	if (!g_strcmp0 ("connection", type))
		return LD_DIAGRAM_OBJECT (ld_diagram_connection_new (object_storage));

deserialize_object_default:
	/* Anything we can't identify is just an indefinite object. */
	return ld_diagram_object_new (object_storage);
}

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 ();
	for (iter = self->priv->objects; iter; iter = g_list_next (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_CONNECTION)
		return "connection";
	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:
 * @self: an #LdDiagram object.
 *
 * Return value: the modification status of diagram.
 */
gboolean
ld_diagram_get_modified (LdDiagram *self)
{
	g_return_val_if_fail (LD_IS_DIAGRAM (self), FALSE);
	return self->priv->modified;
}

/**
 * ld_diagram_set_modified:
 * @self: an #LdDiagram object.
 * @value: whether the diagram has been modified.
 *
 * Set the modification status of diagram.
 */
void
ld_diagram_set_modified (LdDiagram *self, gboolean value)
{
	g_return_if_fail (LD_IS_DIAGRAM (self));
	self->priv->modified = value;

	g_object_notify (G_OBJECT (self), "modified");
}

static void
on_object_changed (LdDiagramObject *object,
	LdUndoAction *action, gpointer user_data)
{
	LdDiagram *self;

	self = LD_DIAGRAM (user_data);
	push_undo_action (self, action);

	g_signal_emit (self,
		LD_DIAGRAM_GET_CLASS (self)->changed_signal, 0);
}

static void
on_object_notify_storage (LdDiagramObject *object,
	GParamSpec *pspec, gpointer user_data)
{
	g_warning ("storage of a diagram object has changed");
}

/**
 * ld_diagram_can_undo:
 * @self: an #LdDiagram object.
 *
 * Return value: whether any action can be undone.
 */
gboolean
ld_diagram_can_undo (LdDiagram *self)
{
	return self->priv->undo_stack != NULL;
}

/**
 * ld_diagram_can_redo:
 * @self: an #LdDiagram object.
 *
 * Return value: whether any undone action can be redone.
 */
gboolean
ld_diagram_can_redo (LdDiagram *self)
{
	return self->priv->redo_stack != NULL;
}

static void
push_undo_action (LdDiagram *self, LdUndoAction *action)
{
	GList **undo_list;

	if (self->priv->lock_history)
		return;
	if (self->priv->redo_stack)
		destroy_action_stack (&self->priv->redo_stack);

	if (!self->priv->in_user_action)
		self->priv->undo_stack = g_list_prepend (self->priv->undo_stack, NULL);
	undo_list = (GList **) &self->priv->undo_stack->data;

	g_object_ref (action);
	*undo_list = g_list_prepend (*undo_list, action);

	g_object_notify (G_OBJECT (self), "can-undo");
	g_object_notify (G_OBJECT (self), "can-redo");
}

static void
destroy_action_stack (GList **stack)
{
	GList *action, *sub;

	for (action = *stack; action; action = g_list_next (action))
	{
		for (sub = action->data; sub; sub = g_list_next (sub))
			g_object_unref (sub->data);
		g_list_free (action->data);
	}
	g_list_free (*stack);
	*stack = NULL;
}

/**
 * ld_diagram_undo:
 * @self: an #LdDiagram object.
 *
 * Undo the last action.
 */
void
ld_diagram_undo (LdDiagram *self)
{
	GList *action, *sub;

	g_return_if_fail (LD_IS_DIAGRAM (self));
	g_return_if_fail (self->priv->in_user_action == 0);

	if (!self->priv->undo_stack)
		return;

	self->priv->lock_history = TRUE;

	action = self->priv->undo_stack;
	self->priv->undo_stack = g_list_remove_link (action, action);
	for (sub = action->data; sub; sub = g_list_next (sub))
		ld_undo_action_undo (sub->data);
	self->priv->redo_stack = g_list_concat (action, self->priv->redo_stack);

	self->priv->lock_history = FALSE;

	g_object_notify (G_OBJECT (self), "can-undo");
	g_object_notify (G_OBJECT (self), "can-redo");

	g_signal_emit (self,
		LD_DIAGRAM_GET_CLASS (self)->changed_signal, 0);
}

/**
 * ld_diagram_redo:
 * @self: an #LdDiagram object.
 *
 * Redo the last undone action.
 */
void
ld_diagram_redo (LdDiagram *self)
{
	GList *action, *sub;

	g_return_if_fail (LD_IS_DIAGRAM (self));
	g_return_if_fail (self->priv->in_user_action == 0);

	if (!self->priv->redo_stack)
		return;

	self->priv->lock_history = TRUE;

	action = self->priv->redo_stack;
	self->priv->redo_stack = g_list_remove_link (action, action);
	for (sub = g_list_last (action->data); sub; sub = g_list_previous (sub))
		ld_undo_action_redo (sub->data);
	self->priv->undo_stack = g_list_concat (action, self->priv->undo_stack);

	self->priv->lock_history = FALSE;

	g_object_notify (G_OBJECT (self), "can-undo");
	g_object_notify (G_OBJECT (self), "can-redo");

	g_signal_emit (self,
		LD_DIAGRAM_GET_CLASS (self)->changed_signal, 0);
}

/**
 * ld_diagram_begin_user_action:
 * @self: an #LdDiagram object.
 *
 * Begin an indivisible user action. This function can be called
 * multiple times. Each call has to be ended with a call to
 * ld_diagram_end_user_action().
 */
void
ld_diagram_begin_user_action (LdDiagram *self)
{
	g_return_if_fail (LD_IS_DIAGRAM (self));

	/* Push an empty action on the stack. */
	if (!self->priv->in_user_action++)
		self->priv->undo_stack = g_list_prepend (self->priv->undo_stack, NULL);
}

/**
 * ld_diagram_end_user_action:
 * @self: an #LdDiagram object.
 *
 * End an indivisible user action.
 */
void
ld_diagram_end_user_action (LdDiagram *self)
{
	g_return_if_fail (LD_IS_DIAGRAM (self));
	g_return_if_fail (self->priv->in_user_action > 0);

	/* If the action on the stack is empty, discard it. */
	if (!--self->priv->in_user_action && !self->priv->undo_stack->data)
		self->priv->undo_stack = g_list_delete_link
			(self->priv->undo_stack, self->priv->undo_stack);
}

static void
on_object_action_remove (gpointer user_data)
{
	ObjectActionData *data;

	data = user_data;
	g_return_if_fail (data->self != NULL);
	ld_diagram_remove_object
		((LdDiagram *) data->self, data->object);
}

static void
on_object_action_insert (gpointer user_data)
{
	ObjectActionData *data;

	data = user_data;
	g_return_if_fail (data->self != NULL);
	ld_diagram_insert_object
		((LdDiagram *) data->self, data->object, data->pos);
}

static void
on_object_action_destroy (gpointer user_data)
{
	ObjectActionData *data;

	data = user_data;
	if (data->self)
		g_object_remove_weak_pointer (G_OBJECT (data->self), &data->self);
	g_object_unref (data->object);
	g_slice_free (ObjectActionData, data);
}

static void
install_object (LdDiagramObject *object, LdDiagram *self)
{
	g_signal_connect (object, "changed",
		G_CALLBACK (on_object_changed), self);
	g_signal_connect (object, "notify::storage",
		G_CALLBACK (on_object_notify_storage), self);
	g_object_ref (object);
}

static void
uninstall_object (LdDiagramObject *object, LdDiagram *self)
{
	g_signal_handlers_disconnect_by_func (object,
		on_object_changed, self);
	g_signal_handlers_disconnect_by_func (object,
		on_object_notify_storage, self);
	g_object_unref (object);
}

/**
 * ld_diagram_get_objects:
 * @self: an #LdDiagram object.
 *
 * Return value: (element-type LdDiagramObject): a list of all objects
 *               in the diagram. Do not modify.
 */
GList *
ld_diagram_get_objects (LdDiagram *self)
{
	g_return_val_if_fail (LD_IS_DIAGRAM (self), NULL);
	return self->priv->objects;
}

/**
 * ld_diagram_insert_object:
 * @self: an #LdDiagram object.
 * @object: the object to be inserted.
 * @pos: the position at which the object is to be inserted.
 *       Negative values will append to the end.
 *
 * Insert an object into the diagram.
 */
void
ld_diagram_insert_object (LdDiagram *self, LdDiagramObject *object, gint pos)
{
	LdUndoAction *action;
	ObjectActionData *action_data;

	g_return_if_fail (LD_IS_DIAGRAM (self));
	g_return_if_fail (LD_IS_DIAGRAM_OBJECT (object));

	if (g_list_find (self->priv->objects, object))
		return;

	self->priv->objects = g_list_insert (self->priv->objects, object, pos);
	install_object (object, self);

	action_data = g_slice_new (ObjectActionData);
	action_data->self = self;
	g_object_add_weak_pointer (G_OBJECT (self), &action_data->self);
	action_data->object = g_object_ref (object);
	action_data->pos = pos;

	action = ld_undo_action_new (on_object_action_remove,
		on_object_action_insert, on_object_action_destroy, action_data);
	push_undo_action (self, action);
	g_object_unref (action);

	g_signal_emit (self,
		LD_DIAGRAM_GET_CLASS (self)->changed_signal, 0);
}

/**
 * ld_diagram_remove_object:
 * @self: an #LdDiagram object.
 * @object: the object to be removed.
 *
 * Remove an object from the diagram.
 */
void
ld_diagram_remove_object (LdDiagram *self, LdDiagramObject *object)
{
	LdUndoAction *action;
	ObjectActionData *action_data;
	guint pos;
	GList *link;

	g_return_if_fail (LD_IS_DIAGRAM (self));
	g_return_if_fail (LD_IS_DIAGRAM_OBJECT (object));

	pos = 0;
	for (link = self->priv->objects; link; link = g_list_next (link))
	{
		if (link->data == object)
			break;
		pos++;
	}
	if (!link)
		return;

	ld_diagram_unselect (self, object);

	self->priv->objects = g_list_delete_link (self->priv->objects, link);
	uninstall_object (object, self);

	action_data = g_slice_new (ObjectActionData);
	action_data->self = self;
	g_object_add_weak_pointer (G_OBJECT (self), &action_data->self);
	action_data->object = g_object_ref (object);
	action_data->pos = pos;

	action = ld_undo_action_new (on_object_action_insert,
		on_object_action_remove, on_object_action_destroy, action_data);
	push_undo_action (self, action);
	g_object_unref (action);

	g_signal_emit (self,
		LD_DIAGRAM_GET_CLASS (self)->changed_signal, 0);
}

/**
 * ld_diagram_get_selection:
 * @self: an #LdDiagram object.
 *
 * Return value: (element-type LdDiagramObject): a list of objects that
 *               are currently selected in the diagram. Do not modify.
 */
GList *
ld_diagram_get_selection (LdDiagram *self)
{
	g_return_val_if_fail (LD_IS_DIAGRAM (self), NULL);
	return self->priv->selection;
}

/**
 * ld_diagram_remove_selection:
 * @self: an #LdDiagram object.
 *
 * Remove selected objects from the diagram.
 */
void
ld_diagram_remove_selection (LdDiagram *self)
{
	GList *selection_copy, *iter;

	g_return_if_fail (LD_IS_DIAGRAM (self));

	/* We still retain references in the object list. */
	selection_copy = g_list_copy (self->priv->selection);
	ld_diagram_unselect_all (self);

	ld_diagram_begin_user_action (self);
	for (iter = selection_copy; iter; iter = g_list_next (iter))
		ld_diagram_remove_object (self, LD_DIAGRAM_OBJECT (iter->data));
	ld_diagram_end_user_action (self);

	g_list_free (selection_copy);
}

/**
 * ld_diagram_select:
 * @self: an #LdDiagram object.
 * @object: the object to be added to the selection.
 *
 * Add an object to selection.
 */
void
ld_diagram_select (LdDiagram *self, LdDiagramObject *object)
{
	g_return_if_fail (LD_IS_DIAGRAM (self));
	g_return_if_fail (LD_IS_DIAGRAM_OBJECT (object));
	g_return_if_fail (g_list_find (self->priv->objects, object) != NULL);

	if (g_list_find (self->priv->selection, object))
		return;

	self->priv->selection = g_list_insert (self->priv->selection, object, 0);
	g_object_ref (object);

	g_signal_emit (self,
		LD_DIAGRAM_GET_CLASS (self)->selection_changed_signal, 0);
}

/**
 * ld_diagram_unselect:
 * @self: an #LdDiagram object.
 * @object: the object to be removed from the selection.
 *
 * Remove an object from the selection.
 */
void
ld_diagram_unselect (LdDiagram *self, LdDiagramObject *object)
{
	GList *link;

	g_return_if_fail (LD_IS_DIAGRAM (self));
	g_return_if_fail (LD_IS_DIAGRAM_OBJECT (object));

	if (!(link = g_list_find (self->priv->selection, object)))
		return;

	self->priv->selection = g_list_delete_link (self->priv->selection, link);
	g_object_unref (object);

	g_signal_emit (self,
		LD_DIAGRAM_GET_CLASS (self)->selection_changed_signal, 0);
}

/**
 * ld_diagram_select_all:
 * @self: an #LdDiagram object.
 *
 * Include all objects in the document to the selection.
 */
void
ld_diagram_select_all (LdDiagram *self)
{
	g_return_if_fail (LD_IS_DIAGRAM (self));

	ld_diagram_unselect_all_internal (self);
	self->priv->selection = g_list_copy (self->priv->objects);
	g_list_foreach (self->priv->selection, (GFunc) g_object_ref, NULL);

	g_signal_emit (self,
		LD_DIAGRAM_GET_CLASS (self)->selection_changed_signal, 0);
}

/**
 * ld_diagram_unselect_all:
 * @self: an #LdDiagram object.
 *
 * Remove all objects from the current selection.
 */
void
ld_diagram_unselect_all (LdDiagram *self)
{
	g_return_if_fail (LD_IS_DIAGRAM (self));

	if (!self->priv->selection)
		return;

	ld_diagram_unselect_all_internal (self);

	g_signal_emit (self,
		LD_DIAGRAM_GET_CLASS (self)->selection_changed_signal, 0);
}

static void
ld_diagram_unselect_all_internal (LdDiagram *self)
{
	g_list_foreach (self->priv->selection, (GFunc) g_object_unref, NULL);
	g_list_free (self->priv->selection);
	self->priv->selection = NULL;
}