aboutsummaryrefslogtreecommitdiff
path: root/src/ld-symbol-library.c
blob: 8876dd8723892c8ecd3bbd7ff837c36e5cce588b (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
/*
 * ld-symbol-library.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 <lua.h>
/* #include <lauxlib.h> */

#include "config.h"

#include "ld-symbol-library.h"
#include "ld-symbol-category.h"
#include "ld-symbol.h"

/* ===== Symbol library ==================================================== */

/**
 * SECTION:ld-symbol-library
 * @short_description: A symbol library.
 * @see_also: #LogdiagSymbol, #LogdiagSymbolCategory
 *
 * #LogdiagSymbolLibrary is used for loading symbols from their files.
 */

/*
 * LogdiagSymbolLibraryPrivate:
 * @lua_state: Lua state.
 */
struct _LogdiagSymbolLibraryPrivate
{
	lua_State *lua_state;
};

G_DEFINE_TYPE (LogdiagSymbolLibrary, logdiag_symbol_library, G_TYPE_OBJECT);

static void
logdiag_symbol_library_finalize (GObject *gobject);


static void
logdiag_symbol_library_class_init (LogdiagSymbolLibraryClass *klass)
{
	GObjectClass *object_class;

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

/**
 * LogdiagSymbolLibrary::changed:
 * @library: The library object.
 *
 * Contents of the library have changed.
 */
	klass->changed_signal = g_signal_new
		("changed", G_TYPE_FROM_CLASS (object_class),
		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 (LogdiagSymbolLibraryPrivate));
}

static void
logdiag_symbol_library_init (LogdiagSymbolLibrary *self)
{
	self->priv = G_TYPE_INSTANCE_GET_PRIVATE
		(self, LOGDIAG_TYPE_SYMBOL_LIBRARY, LogdiagSymbolLibraryPrivate);

	/* TODO: lua */
	self->priv->lua_state = NULL;

	self->categories = g_hash_table_new_full (g_str_hash, g_str_equal,
		(GDestroyNotify) g_free, (GDestroyNotify) g_object_unref);
}

static void
logdiag_symbol_library_finalize (GObject *gobject)
{
	LogdiagSymbolLibrary *self;

	self = LOGDIAG_SYMBOL_LIBRARY (gobject);

	g_hash_table_destroy (self->categories);

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

/**
 * logdiag_symbol_library_new:
 *
 * Create an instance.
 */
LogdiagSymbolLibrary *
logdiag_symbol_library_new (void)
{
	return g_object_new (LOGDIAG_TYPE_SYMBOL_LIBRARY, NULL);
}

/*
 * load_category:
 * @self: A symbol library object.
 * @path: The path to the category.
 * @name: The default name of the category.
 *
 * Loads a category into the library.
 */
static LogdiagSymbolCategory *
load_category (LogdiagSymbolLibrary *self, const char *path, const char *name)
{
	LogdiagSymbolCategory *cat;
	gchar *icon_file;

	g_return_val_if_fail (LOGDIAG_IS_SYMBOL_LIBRARY (self), NULL);
	g_return_val_if_fail (path != NULL, NULL);
	g_return_val_if_fail (name != NULL, NULL);

	if (!g_file_test (path, G_FILE_TEST_IS_DIR))
		return NULL;

	icon_file = g_build_filename (path, "icon.svg", NULL);
	if (!g_file_test (icon_file, G_FILE_TEST_IS_REGULAR))
	{
		g_warning ("The category in %s has no icon.", path);
		g_free (icon_file);
		return NULL;
	}

	/* TODO: Search for category.json and read the category name from it. */
	/* TODO: Search for xyz.lua and load the objects into the category. */

	cat = logdiag_symbol_category_new (self);
	cat->name = g_strdup (name);
	cat->image_path = icon_file;
	return cat;
}

/**
 * logdiag_symbol_library_load:
 * @self: A symbol library object.
 * @directory: A directory to be loaded.
 *
 * Load the contents of a directory into the library.
 */
gboolean
logdiag_symbol_library_load (LogdiagSymbolLibrary *self, const char *path)
{
	GDir *dir;
	const gchar *item;

	g_return_val_if_fail (LOGDIAG_IS_SYMBOL_LIBRARY (self), FALSE);
	g_return_val_if_fail (path != NULL, FALSE);

	dir = g_dir_open (path, 0, NULL);
	if (!dir)
		return FALSE;

	while ((item = g_dir_read_name (dir)))
	{
		LogdiagSymbolCategory *cat;
		gchar *categ_path;

		categ_path = g_build_filename (path, item, NULL);
		cat = load_category (self, categ_path, item);
		if (cat)
			g_hash_table_insert (self->categories, cat->name, cat);
		g_free (categ_path);
	}
	g_dir_close (dir);
	return TRUE;
}

/**
 * logdiag_symbol_library_clear:
 * @self: A symbol library object.
 *
 * Clears all the contents.
 */
void
logdiag_symbol_library_clear (LogdiagSymbolLibrary *self)
{
	g_return_if_fail (LOGDIAG_IS_SYMBOL_LIBRARY (self));

	g_hash_table_remove_all (self->categories);
	return;
}


/* ===== Symbol category =================================================== */

/**
 * SECTION:ld-symbol-category
 * @short_description: A category of symbols.
 * @see_also: #LogdiagSymbol, #LogdiagSymbolLibrary
 *
 * #LogdiagSymbolCategory represents a category of #LogdiagSymbol objects.
 */

G_DEFINE_TYPE (LogdiagSymbolCategory, logdiag_symbol_category, G_TYPE_OBJECT);

static void
logdiag_symbol_category_finalize (GObject *gobject);


static void
logdiag_symbol_category_class_init (LogdiagSymbolCategoryClass *klass)
{
	GObjectClass *object_class;

	object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = logdiag_symbol_category_finalize;
}

static void
logdiag_symbol_category_init (LogdiagSymbolCategory *self)
{
	/* TODO: use _new_full, correct equal and specify destroy functions. */
	/* XXX: How's the situation with subcategory names and symbol names
	 *      within the same hashtable?
	 */
	self->children = g_hash_table_new (g_str_hash, g_str_equal);
}

static void
logdiag_symbol_category_finalize (GObject *gobject)
{
	LogdiagSymbolCategory *self;

	self = LOGDIAG_SYMBOL_CATEGORY (gobject);

	if (self->name)
		g_free (self->name);
	if (self->image_path)
		g_free (self->image_path);

	g_object_unref (self->parent);
	g_hash_table_destroy (self->children);

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

/**
 * logdiag_symbol_category_new:
 * @parent: The parent library for this category.
 *
 * Create an instance.
 */
LogdiagSymbolCategory *
logdiag_symbol_category_new (LogdiagSymbolLibrary *parent)
{
	LogdiagSymbolCategory *cat;

	cat = g_object_new (LOGDIAG_TYPE_SYMBOL_CATEGORY, NULL);

	cat->parent = parent;
	g_object_ref (parent);

	return cat;
}


/* ===== Symbol ============================================================ */

/**
 * SECTION:ld-symbol
 * @short_description: A symbol.
 * @see_also: #LogdiagDocument, #LogdiagCanvas
 *
 * #LogdiagSymbol represents a symbol in the #LogdiagDocument that is in turn
 * drawn onto the #LogdiagCanvas.
 */

/*
 * LogdiagSymbolPrivate:
 * @library: The parent LogdiagSymbolLibrary.
 * The library contains the real function for rendering.
 */
struct _LogdiagSymbolPrivate
{
	LogdiagSymbolLibrary *library;
};

G_DEFINE_TYPE (LogdiagSymbol, logdiag_symbol, G_TYPE_OBJECT);

static void
logdiag_symbol_finalize (GObject *gobject);


static void
logdiag_symbol_class_init (LogdiagSymbolClass *klass)
{
	GObjectClass *object_class;

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

	g_type_class_add_private (klass, sizeof (LogdiagSymbolPrivate));
}

static void
logdiag_symbol_init (LogdiagSymbol *self)
{
	self->priv = G_TYPE_INSTANCE_GET_PRIVATE
		(self, LOGDIAG_TYPE_SYMBOL_LIBRARY, LogdiagSymbolPrivate);
}

static void
logdiag_symbol_finalize (GObject *gobject)
{
	LogdiagSymbol *self;

	self = LOGDIAG_SYMBOL (gobject);
	g_object_unref (self->priv->library);

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

/**
 * logdiag_symbol_new:
 * @library: A library object.
 * @filename: The file from which the symbol will be loaded.
 *
 * Load a symbol from a file into the library.
 */
LogdiagSymbol *logdiag_symbol_new (LogdiagSymbolLibrary *library,
	const gchar *filename)
{
	LogdiagSymbol *symbol;

	symbol = g_object_new (LOGDIAG_TYPE_SYMBOL, NULL);
	/* TODO: Use the filename, Luke. */

	symbol->priv->library = library;
	g_object_ref (library);
}

/**
 * logdiag_symbol_build_identifier:
 * @self: A symbol object.
 *
 * Build an identifier for the symbol.
 * The identifier is in the format "Category/Category/Symbol".
 */
char *
logdiag_symbol_build_identifier (LogdiagSymbol *self)
{
	return NULL;
}

/**
 * logdiag_symbol_draw:
 * @self: A symbol object.
 * @surface: A cairo surface to be drawn on.
 * @param: Parameters for the symbol in a table.
 * @x: The X coordinate on the surface.
 * @y: The Y coordinate on the surface.
 * @zoom: Zoom ratio.
 *
 * Draw the symbol onto a Cairo surface.
 */
void
logdiag_symbol_draw (LogdiagSymbol *self, cairo_t *surface,
	GHashTable *param, gint x, gint y, gdouble zoom)
{
	return;
}