aboutsummaryrefslogtreecommitdiff
path: root/src/ld-symbol-library.c
blob: 2f5f9d344e797b4c2115c6afc9368ffe19391695 (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
/*
 * 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 "config.h"

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


/**
 * SECTION:ld-symbol-library
 * @short_description: A symbol library.
 * @see_also: #LdSymbol, #LdSymbolCategory
 *
 * #LdSymbolLibrary is used for loading symbols from their files.
 */

/*
 * LdSymbolLibraryPrivate:
 * @script_state: State of the scripting language.
 */
struct _LdSymbolLibraryPrivate
{
	gpointer script_state;
};

G_DEFINE_TYPE (LdSymbolLibrary, ld_symbol_library, G_TYPE_OBJECT);

static void
ld_symbol_library_finalize (GObject *gobject);


static void
ld_symbol_library_class_init (LdSymbolLibraryClass *klass)
{
	GObjectClass *object_class;

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

/**
 * LdSymbolLibrary::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 (LdSymbolLibraryPrivate));
}

static void
ld_symbol_library_init (LdSymbolLibrary *self)
{
	self->priv = G_TYPE_INSTANCE_GET_PRIVATE
		(self, LD_TYPE_SYMBOL_LIBRARY, LdSymbolLibraryPrivate);

	/* TODO */
	self->priv->script_state = NULL;

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

static void
ld_symbol_library_finalize (GObject *gobject)
{
	LdSymbolLibrary *self;

	self = LD_SYMBOL_LIBRARY (gobject);

	g_hash_table_destroy (self->categories);

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

/**
 * ld_symbol_library_new:
 *
 * Create an instance.
 */
LdSymbolLibrary *
ld_symbol_library_new (void)
{
	return g_object_new (LD_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 LdSymbolCategory *
load_category (LdSymbolLibrary *self, const char *path, const char *name)
{
	LdSymbolCategory *cat;
	gchar *icon_file;

	g_return_val_if_fail (LD_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 = ld_symbol_category_new (self);
	cat->name = g_strdup (name);
	cat->image_path = icon_file;
	return cat;
}

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

	g_return_val_if_fail (LD_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)))
	{
		LdSymbolCategory *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);

		changed = TRUE;
	}
	g_dir_close (dir);

	if (changed)
		g_signal_emit (self,
			LD_SYMBOL_LIBRARY_GET_CLASS (self)->changed_signal, 0);

	return TRUE;
}

/**
 * ld_symbol_library_clear:
 * @self: A symbol library object.
 *
 * Clears all the contents.
 */
void
ld_symbol_library_clear (LdSymbolLibrary *self)
{
	g_return_if_fail (LD_IS_SYMBOL_LIBRARY (self));

	g_hash_table_remove_all (self->categories);

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