logdiag/src/ld-symbol-library.c

192 lines
4.1 KiB
C
Raw Normal View History

2010-09-13 19:24:53 +02:00
/*
* ld-symbol-library.c
2010-09-13 19:24:53 +02:00
*
* 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"
2010-09-13 19:24:53 +02:00
/**
* SECTION:ld-symbol-library
2010-09-13 19:24:53 +02:00
* @short_description: A symbol library.
* @see_also: #LdSymbol, #LdSymbolCategory
2010-09-13 19:24:53 +02:00
*
* #LdSymbolLibrary is used for loading symbols from their files.
2010-09-13 19:24:53 +02:00
*/
/*
* LdSymbolLibraryPrivate:
2010-09-13 19:24:53 +02:00
* @lua_state: Lua state.
*/
struct _LdSymbolLibraryPrivate
2010-09-13 19:24:53 +02:00
{
lua_State *lua_state;
};
G_DEFINE_TYPE (LdSymbolLibrary, ld_symbol_library, G_TYPE_OBJECT);
2010-09-13 19:24:53 +02:00
static void
ld_symbol_library_finalize (GObject *gobject);
2010-09-13 19:24:53 +02:00
static void
ld_symbol_library_class_init (LdSymbolLibraryClass *klass)
2010-09-13 19:24:53 +02:00
{
GObjectClass *object_class;
object_class = G_OBJECT_CLASS (klass);
object_class->finalize = ld_symbol_library_finalize;
2010-09-13 19:24:53 +02:00
/**
* LdSymbolLibrary::changed:
2010-09-13 19:24:53 +02:00
* @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));
2010-09-13 19:24:53 +02:00
}
static void
ld_symbol_library_init (LdSymbolLibrary *self)
2010-09-13 19:24:53 +02:00
{
self->priv = G_TYPE_INSTANCE_GET_PRIVATE
(self, LD_TYPE_SYMBOL_LIBRARY, LdSymbolLibraryPrivate);
2010-09-13 19:24:53 +02:00
/* 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);
2010-09-13 19:24:53 +02:00
}
static void
ld_symbol_library_finalize (GObject *gobject)
2010-09-13 19:24:53 +02:00
{
LdSymbolLibrary *self;
2010-09-13 19:24:53 +02:00
self = LD_SYMBOL_LIBRARY (gobject);
2010-09-13 19:24:53 +02:00
g_hash_table_destroy (self->categories);
/* Chain up to the parent class. */
G_OBJECT_CLASS (ld_symbol_library_parent_class)->finalize (gobject);
2010-09-13 19:24:53 +02:00
}
/**
* ld_symbol_library_new:
2010-09-13 19:24:53 +02:00
*
* Create an instance.
*/
LdSymbolLibrary *
ld_symbol_library_new (void)
2010-09-13 19:24:53 +02:00
{
return g_object_new (LD_TYPE_SYMBOL_LIBRARY, NULL);
2010-09-13 19:24:53 +02:00
}
/*
* 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)
2010-09-13 19:24:53 +02:00
{
LdSymbolCategory *cat;
2010-09-13 19:24:53 +02:00
gchar *icon_file;
g_return_val_if_fail (LD_IS_SYMBOL_LIBRARY (self), NULL);
2010-09-13 19:24:53 +02:00
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;
2010-09-13 19:24:53 +02:00
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);
2010-09-13 19:24:53 +02:00
cat->name = g_strdup (name);
cat->image_path = icon_file;
return cat;
}
/**
* ld_symbol_library_load:
2010-09-13 19:24:53 +02:00
* @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)
2010-09-13 19:24:53 +02:00
{
GDir *dir;
const gchar *item;
g_return_val_if_fail (LD_IS_SYMBOL_LIBRARY (self), FALSE);
2010-09-13 19:24:53 +02:00
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;
2010-09-13 19:24:53 +02:00
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;
}
/**
* ld_symbol_library_clear:
* @self: A symbol library object.
2010-09-13 19:24:53 +02:00
*
* Clears all the contents.
*/
void
ld_symbol_library_clear (LdSymbolLibrary *self)
2010-09-13 19:24:53 +02:00
{
g_return_if_fail (LD_IS_SYMBOL_LIBRARY (self));
2010-09-13 19:24:53 +02:00
g_hash_table_remove_all (self->categories);
return;
}