aboutsummaryrefslogtreecommitdiff
path: root/liblogdiag/ld-symbol-category.c
blob: 52763e2a56e51ffe7ed0819bd644847b724519a3 (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
/*
 * ld-symbol-category.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-symbol-category
 * @short_description: A category of symbols
 * @see_also: #LdSymbol, #LdLibrary
 *
 * #LdSymbolCategory represents a category of #LdSymbol objects.
 */

/*
 * LdSymbolCategoryPrivate:
 * @name: the name of this category.
 * @image_path: path to the image for this category.
 * @symbols: (element-type LdSymbol *): symbols in this category.
 * @subcategories: (element-type LdSymbolCategory *) children of this category.
 */
struct _LdSymbolCategoryPrivate
{
	gchar *name;
	gchar *human_name;
	gchar *image_path;
	GSList *symbols;
	GSList *subcategories;
};

enum
{
	PROP_0,
	PROP_NAME,
	PROP_HUMAN_NAME,
	PROP_IMAGE_PATH
};

static void ld_symbol_category_get_property (GObject *object, guint property_id,
	GValue *value, GParamSpec *pspec);
static void ld_symbol_category_set_property (GObject *object, guint property_id,
	const GValue *value, GParamSpec *pspec);
static void ld_symbol_category_finalize (GObject *gobject);

static void on_category_notify_name (LdSymbolCategory *category,
	GParamSpec *pspec, gpointer user_data);


G_DEFINE_TYPE (LdSymbolCategory, ld_symbol_category, G_TYPE_OBJECT);

static void
ld_symbol_category_class_init (LdSymbolCategoryClass *klass)
{
	GObjectClass *object_class;
	GParamSpec *pspec;

	object_class = G_OBJECT_CLASS (klass);
	object_class->get_property = ld_symbol_category_get_property;
	object_class->set_property = ld_symbol_category_set_property;
	object_class->finalize = ld_symbol_category_finalize;

/**
 * LdSymbolCategory:name:
 *
 * The name of this symbol category.
 */
	pspec = g_param_spec_string ("name", "Name",
		"The name of this symbol category.",
		"", G_PARAM_READWRITE);
	g_object_class_install_property (object_class, PROP_NAME, pspec);

/**
 * LdSymbolCategory:human-name:
 *
 * The localized human name of this symbol category.
 */
	pspec = g_param_spec_string ("human-name", "Human name",
		"The localized human name of this symbol category.",
		"", G_PARAM_READWRITE);
	g_object_class_install_property (object_class, PROP_HUMAN_NAME, pspec);

/**
 * LdSymbolCategory:image-path:
 *
 * Path to an image file representing this category.
 */
	pspec = g_param_spec_string ("image-path", "Image path",
		"Path to an image file representing this category.",
		"", G_PARAM_READWRITE);
	g_object_class_install_property (object_class, PROP_IMAGE_PATH, pspec);

	g_type_class_add_private (klass, sizeof (LdSymbolCategoryPrivate));
}

static void
ld_symbol_category_init (LdSymbolCategory *self)
{
	self->priv = G_TYPE_INSTANCE_GET_PRIVATE
		(self, LD_TYPE_SYMBOL_CATEGORY, LdSymbolCategoryPrivate);
}

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

	self = LD_SYMBOL_CATEGORY (object);
	switch (property_id)
	{
	case PROP_NAME:
		g_value_set_string (value, ld_symbol_category_get_name (self));
		break;
	case PROP_HUMAN_NAME:
		g_value_set_string (value, ld_symbol_category_get_human_name (self));
		break;
	case PROP_IMAGE_PATH:
		g_value_set_string (value, ld_symbol_category_get_image_path (self));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
	}
}

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

	self = LD_SYMBOL_CATEGORY (object);
	switch (property_id)
	{
	case PROP_NAME:
		ld_symbol_category_set_name (self, g_value_get_string (value));
		break;
	case PROP_HUMAN_NAME:
		ld_symbol_category_set_human_name (self, g_value_get_string (value));
		break;
	case PROP_IMAGE_PATH:
		ld_symbol_category_set_image_path (self, g_value_get_string (value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
	}
}

static void
uninstall_category_cb (LdSymbolCategory *category, LdSymbolCategory *self)
{
	g_signal_handlers_disconnect_by_func (category,
		on_category_notify_name, self);
	g_object_unref (category);
}

static void
ld_symbol_category_finalize (GObject *gobject)
{
	LdSymbolCategory *self;

	self = LD_SYMBOL_CATEGORY (gobject);

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

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

	g_slist_foreach (self->priv->subcategories,
		(GFunc) uninstall_category_cb, self);
	g_slist_free (self->priv->subcategories);

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


/**
 * ld_symbol_category_new:
 * @name: the name of the new category.
 * @human_name: the localized human name of the new category.
 *
 * Create an instance.
 */
LdSymbolCategory *
ld_symbol_category_new (const gchar *name, const gchar *human_name)
{
	LdSymbolCategory *cat;

	g_return_val_if_fail (name != NULL, NULL);
	g_return_val_if_fail (human_name != NULL, NULL);

	cat = g_object_new (LD_TYPE_SYMBOL_CATEGORY, NULL);
	cat->priv->name = g_strdup (name);
	cat->priv->human_name = g_strdup (human_name);

	return cat;
}

/**
 * ld_symbol_category_set_name:
 * @self: an #LdSymbolCategory object.
 * @name: the new name for this category.
 */
void
ld_symbol_category_set_name (LdSymbolCategory *self, const gchar *name)
{
	g_return_if_fail (LD_IS_SYMBOL_CATEGORY (self));
	g_return_if_fail (name != NULL);

	if (self->priv->name)
		g_free (self->priv->name);
	self->priv->name = g_strdup (name);

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

/**
 * ld_symbol_category_get_name:
 * @self: an #LdSymbolCategory object.
 *
 * Return the name of this category.
 */
const gchar *
ld_symbol_category_get_name (LdSymbolCategory *self)
{
	g_return_val_if_fail (LD_IS_SYMBOL_CATEGORY (self), NULL);
	return self->priv->name;
}

/**
 * ld_symbol_category_set_human_name:
 * @self: an #LdSymbolCategory object.
 * @human_name: the new localized human name for this category.
 */
void
ld_symbol_category_set_human_name (LdSymbolCategory *self,
	const gchar *human_name)
{
	g_return_if_fail (LD_IS_SYMBOL_CATEGORY (self));
	g_return_if_fail (human_name != NULL);

	if (self->priv->human_name)
		g_free (self->priv->human_name);
	self->priv->human_name = g_strdup (human_name);

	g_object_notify (G_OBJECT (self), "human-name");
}

/**
 * ld_symbol_category_get_human_name:
 * @self: an #LdSymbolCategory object.
 *
 * Return the localized human name of this category.
 */
const gchar *
ld_symbol_category_get_human_name (LdSymbolCategory *self)
{
	g_return_val_if_fail (LD_IS_SYMBOL_CATEGORY (self), NULL);
	return self->priv->human_name;
}

/**
 * ld_symbol_category_set_image_path:
 * @self: an #LdSymbolCategory object.
 * @image_path: (allow-none): The new path to the image for this category.
 */
void
ld_symbol_category_set_image_path (LdSymbolCategory *self,
	const gchar *image_path)
{
	g_return_if_fail (LD_IS_SYMBOL_CATEGORY (self));

	if (self->priv->image_path)
		g_free (self->priv->image_path);
	self->priv->image_path = g_strdup (image_path);

	g_object_notify (G_OBJECT (self), "image-path");
}

/**
 * ld_symbol_category_get_image_path:
 * @self: an #LdSymbolCategory object.
 *
 * Return value: (allow-none): filesystem path to the image for this category.
 */
const gchar *
ld_symbol_category_get_image_path (LdSymbolCategory *self)
{
	g_return_val_if_fail (LD_IS_SYMBOL_CATEGORY (self), NULL);
	return self->priv->image_path;
}

/**
 * ld_symbol_category_insert_symbol:
 * @self: an #LdSymbolCategory object.
 * @symbol: the symbol to be inserted.
 * @pos: the position at which the symbol will be inserted.
 *       Negative values will append to the end of list.
 *
 * Insert a symbol into the category.
 *
 * Return value: %TRUE if successful (no name collisions).
 */
gboolean
ld_symbol_category_insert_symbol (LdSymbolCategory *self,
	LdSymbol *symbol, gint pos)
{
	const gchar *name;
	const GSList *iter;

	g_return_val_if_fail (LD_IS_SYMBOL_CATEGORY (self), FALSE);
	g_return_val_if_fail (LD_IS_SYMBOL (symbol), FALSE);

	/* Check for name collisions. */
	name = ld_symbol_get_name (symbol);
	for (iter = self->priv->symbols; iter; iter = iter->next)
	{
		if (!strcmp (name, ld_symbol_get_name (iter->data)))
		{
			g_warning ("attempted to insert multiple `%s' symbols into"
				" category `%s'", name, ld_symbol_category_get_name (self));
			return FALSE;
		}
	}

	self->priv->symbols = g_slist_insert (self->priv->symbols, symbol, pos);
	g_object_ref (symbol);
	return TRUE;
}

/**
 * ld_symbol_category_remove_symbol:
 * @self: an #LdSymbolCategory object.
 * @symbol: the symbol to be removed.
 *
 * Removes a symbol from the category.
 */
void
ld_symbol_category_remove_symbol (LdSymbolCategory *self,
	LdSymbol *symbol)
{
	g_return_if_fail (LD_IS_SYMBOL_CATEGORY (self));
	g_return_if_fail (LD_IS_SYMBOL (symbol));

	if (g_slist_find (self->priv->symbols, symbol))
	{
		self->priv->symbols = g_slist_remove (self->priv->symbols, symbol);
		g_object_unref (symbol);
	}
}

/**
 * ld_symbol_category_get_symbols:
 * @self: an #LdSymbolCategory object.
 *
 * Return value: (element-type LdSymbol *): a list of symbols.  Do not modify.
 */
const GSList *
ld_symbol_category_get_symbols (LdSymbolCategory *self)
{
	g_return_val_if_fail (LD_IS_SYMBOL_CATEGORY (self), NULL);
	return self->priv->symbols;
}


static void
on_category_notify_name (LdSymbolCategory *category,
	GParamSpec *pspec, gpointer user_data)
{
	/* XXX: We could disown the category if a name collision has occured. */
	g_warning ("name of a library subcategory has changed");
}

/**
 * ld_symbol_category_insert_subcategory:
 * @self: an #LdSymbolCategory object.
 * @category: the category to be inserted.
 * @pos: the position at which the category will be inserted.
 *       Negative values will append to the end of list.
 *
 * Insert a subcategory into the category.
 *
 * Return value: %TRUE if successful (no name collisions).
 */
gboolean
ld_symbol_category_insert_subcategory (LdSymbolCategory *self,
	LdSymbolCategory *category, gint pos)
{
	const gchar *name;
	const GSList *iter;

	g_return_val_if_fail (LD_IS_SYMBOL_CATEGORY (self), FALSE);
	g_return_val_if_fail (LD_IS_SYMBOL_CATEGORY (category), FALSE);

	/* Check for name collisions. */
	name = ld_symbol_category_get_name (category);
	for (iter = self->priv->subcategories; iter; iter = iter->next)
	{
		if (!strcmp (name, ld_symbol_category_get_name (iter->data)))
		{
			g_warning ("attempted to insert multiple `%s' subcategories into"
				" category `%s'", name, ld_symbol_category_get_name (self));
			return FALSE;
		}
	}

	g_signal_connect (category, "notify::name",
		G_CALLBACK (on_category_notify_name), self);
	self->priv->subcategories
		= g_slist_insert (self->priv->subcategories, category, pos);
	g_object_ref (category);
	return TRUE;
}

/**
 * ld_symbol_category_remove_subcategory:
 * @self: an #LdSymbolCategory object.
 * @category: the category to be removed.
 *
 * Removes a subcategory from the category.
 */
void
ld_symbol_category_remove_subcategory (LdSymbolCategory *self,
	LdSymbolCategory *category)
{
	g_return_if_fail (LD_IS_SYMBOL_CATEGORY (self));
	g_return_if_fail (LD_IS_SYMBOL_CATEGORY (category));

	if (g_slist_find (self->priv->subcategories, category))
	{
		g_signal_handlers_disconnect_by_func (category,
			on_category_notify_name, self);
		self->priv->subcategories
			= g_slist_remove (self->priv->subcategories, category);
		g_object_unref (category);
	}
}

/**
 * ld_symbol_category_get_subcategories:
 * @self: an #LdSymbolCategory object.
 *
 * Return value: (element-type LdSymbolCategory *):
 *               a list of subcategories.  Do not modify.
 */
const GSList *
ld_symbol_category_get_subcategories (LdSymbolCategory *self)
{
	g_return_val_if_fail (LD_IS_SYMBOL_CATEGORY (self), NULL);
	return self->priv->subcategories;
}