aboutsummaryrefslogtreecommitdiff
path: root/src/ld-document.c
blob: dbd367e06b68a7791da46e7431df0eccc3a33c75 (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
/*
 * ld-document.c
 *
 * This file is a part of logdiag.
 * Copyright Přemysl Janouch 2010. All rights reserved.
 *
 * See the file LICENSE for licensing information.
 *
 */

#include <gtk/gtk.h>
#include <json-glib/json-glib.h>

#include "config.h"

#include "ld-document-object.h"
#include "ld-document.h"


/**
 * SECTION:ld-document
 * @short_description: A document object.
 * @see_also: #LdCanvas
 *
 * #LdDocument is a model for storing documents.
 */

/*
 * LdDocumentPrivate:
 * @objects: All the objects in the document.
 * @selection: All currently selected objects.
 * @connections: Connections between objects.
 */
struct _LdDocumentPrivate
{
	GSList *objects;
	GSList *selection;
	GSList *connections;
};

G_DEFINE_TYPE (LdDocument, ld_document, G_TYPE_OBJECT);

static void
ld_document_finalize (GObject *gobject);


static void
ld_document_class_init (LdDocumentClass *klass)
{
	GObjectClass *object_class;

	object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = ld_document_finalize;

/**
 * LdDocument::changed:
 * @document: The document object.
 *
 * Contents of the document have changed.
 */
	klass->changed_signal = g_signal_new
		("changed", G_TYPE_FROM_CLASS (klass),
		G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS,
		0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);

	g_type_class_add_private (klass, sizeof (LdDocumentPrivate));
}

static void
ld_document_init (LdDocument *self)
{
	self->priv = G_TYPE_INSTANCE_GET_PRIVATE
		(self, LD_TYPE_DOCUMENT, LdDocumentPrivate);
}

static void
ld_document_finalize (GObject *gobject)
{
	LdDocument *self;

	self = LD_DOCUMENT (gobject);
	ld_document_clear (self);

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

/**
 * ld_document_new:
 *
 * Create an instance.
 */
LdDocument *
ld_document_new (void)
{
	return g_object_new (LD_TYPE_DOCUMENT, NULL);
}

/**
 * ld_document_clear:
 * @self: An #LdDocument object.
 *
 * Clear the whole document with it's objects and selection.
 */
void
ld_document_clear (LdDocument *self)
{
	g_return_if_fail (LD_IS_DOCUMENT (self));

	g_slist_free (self->priv->connections);
	self->priv->connections = NULL;

	g_slist_foreach (self->priv->selection, (GFunc) g_object_unref, NULL);
	g_slist_free (self->priv->selection);
	self->priv->selection = NULL;

	g_slist_foreach (self->priv->objects, (GFunc) g_object_unref, NULL);
	g_slist_free (self->priv->objects);
	self->priv->objects = NULL;

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

/**
 * ld_document_load_from_file:
 * @self: An #LdDocument object.
 * @filename: A filename.
 * @error: Return location for a GError, or NULL.
 *
 * Load a file into the document.
 *
 * Return value: TRUE if the file could be loaded, FALSE otherwise.
 */
gboolean
ld_document_load_from_file (LdDocument *self,
	const gchar *filename, GError *error)
{
	g_return_val_if_fail (LD_IS_DOCUMENT (self), FALSE);

	/* TODO */
	return FALSE;
}

/**
 * ld_document_save_to_file:
 * @self: An #LdDocument object.
 * @filename: A filename.
 * @error: Return location for a GError, or NULL.
 *
 * Save the document into a file.
 *
 * Return value: TRUE if the document could be saved, FALSE otherwise.
 */
gboolean
ld_document_save_to_file (LdDocument *self,
	const gchar *filename, GError *error)
{
	g_return_val_if_fail (LD_IS_DOCUMENT (self), FALSE);

	/* TODO */
	return FALSE;
}

/**
 * ld_document_get_objects:
 * @self: An #LdDocument object.
 *
 * Get a list of objects in the document.
 * You mustn't make any changes to the list.
 */
GSList *
ld_document_get_objects (LdDocument *self)
{
	g_return_val_if_fail (LD_IS_DOCUMENT (self), NULL);
	return self->priv->objects;
}

/**
 * ld_document_insert_object:
 * @self: An #LdDocument 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 document.
 */
void
ld_document_insert_object (LdDocument *self, LdDocumentObject *object, gint pos)
{
	g_return_if_fail (LD_IS_DOCUMENT (self));
	g_return_if_fail (LD_IS_DOCUMENT_OBJECT (object));

	if (!g_slist_find (self->priv->objects, object))
	{
		self->priv->objects =
			g_slist_insert (self->priv->objects, object, pos);
		g_object_ref (object);
	}
	g_signal_emit (self,
		LD_DOCUMENT_GET_CLASS (self)->changed_signal, 0);
}

/**
 * ld_document_remove_object:
 * @self: An #LdDocument object.
 * @object: The object to be removed.
 *
 * Remove an object from the document.
 */
void
ld_document_remove_object (LdDocument *self, LdDocumentObject *object)
{
	g_return_if_fail (LD_IS_DOCUMENT (self));
	g_return_if_fail (LD_IS_DOCUMENT_OBJECT (object));

	if (g_slist_find (self->priv->objects, object))
	{
		ld_document_selection_remove (self, object);

		self->priv->objects = g_slist_remove (self->priv->objects, object);
		g_object_unref (object);
	}
	g_signal_emit (self,
		LD_DOCUMENT_GET_CLASS (self)->changed_signal, 0);
}

/**
 * ld_document_get_selection:
 * @self: An #LdDocument object.
 *
 * Get a list of objects that are currently selected in the document.
 * You mustn't make any changes to the list.
 */
GSList *
ld_document_get_selection (LdDocument *self)
{
	g_return_val_if_fail (LD_IS_DOCUMENT (self), NULL);
	return self->priv->selection;
}

/**
 * ld_document_selection_add:
 * @self: An #LdDocument object.
 * @object: The object to be added to the selection.
 * @pos: The position at which the object is to be inserted.
 *       Negative values will append to the end.
 *
 * Add an object to selection.
 */
void
ld_document_selection_add (LdDocument *self, LdDocumentObject *object, gint pos)
{
	g_return_if_fail (LD_IS_DOCUMENT (self));
	g_return_if_fail (LD_IS_DOCUMENT_OBJECT (object));

	if (!g_slist_find (self->priv->selection, object)
		&& g_slist_find (self->priv->objects, object))
	{
		self->priv->selection =
			g_slist_insert (self->priv->selection, object, pos);
		g_object_ref (object);
	}
	g_signal_emit (self,
		LD_DOCUMENT_GET_CLASS (self)->changed_signal, 0);
}

/**
 * ld_document_selection_remove:
 * @self: An #LdDocument object.
 * @object: The object to be removed from the selection.
 *
 * Remove an object from the selection.
 */
void
ld_document_selection_remove (LdDocument *self, LdDocumentObject *object)
{
	g_return_if_fail (LD_IS_DOCUMENT (self));
	g_return_if_fail (LD_IS_DOCUMENT_OBJECT (object));

	if (g_slist_find (self->priv->selection, object))
	{
		self->priv->selection = g_slist_remove (self->priv->selection, object);
		g_object_unref (object);
	}
	g_signal_emit (self,
		LD_DOCUMENT_GET_CLASS (self)->changed_signal, 0);
}