logdiag/src/ld-library.c

204 lines
4.2 KiB
C
Raw Normal View History

2010-09-13 19:24:53 +02:00
/*
2010-09-25 16:14:09 +02:00
* ld-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 "config.h"
#include "ld-symbol.h"
#include "ld-symbol-category.h"
#include "ld-library.h"
2010-09-25 16:55:07 +02:00
#include "ld-lua.h"
2010-09-13 19:24:53 +02:00
/**
2010-09-25 16:14:09 +02:00
* SECTION:ld-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
*
2010-09-25 16:14:09 +02:00
* #LdLibrary is used for loading symbols from their files.
2010-09-13 19:24:53 +02:00
*/
/*
2010-09-25 16:14:09 +02:00
* LdLibraryPrivate:
* @script_state: State of the scripting language.
2010-09-13 19:24:53 +02:00
*/
2010-09-25 16:14:09 +02:00
struct _LdLibraryPrivate
2010-09-13 19:24:53 +02:00
{
2010-09-25 16:55:07 +02:00
LdLua *lua;
2010-09-13 19:24:53 +02:00
};
2010-09-25 16:14:09 +02:00
G_DEFINE_TYPE (LdLibrary, ld_library, G_TYPE_OBJECT);
2010-09-13 19:24:53 +02:00
static void
2010-09-25 16:14:09 +02:00
ld_library_finalize (GObject *gobject);
2010-09-13 19:24:53 +02:00
static void
2010-09-25 16:14:09 +02:00
ld_library_class_init (LdLibraryClass *klass)
2010-09-13 19:24:53 +02:00
{
GObjectClass *object_class;
object_class = G_OBJECT_CLASS (klass);
2010-09-25 16:14:09 +02:00
object_class->finalize = ld_library_finalize;
2010-09-13 19:24:53 +02:00
/**
2010-09-25 16:14:09 +02:00
* LdLibrary::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);
2010-09-25 16:14:09 +02:00
g_type_class_add_private (klass, sizeof (LdLibraryPrivate));
2010-09-13 19:24:53 +02:00
}
static void
2010-09-25 16:14:09 +02:00
ld_library_init (LdLibrary *self)
2010-09-13 19:24:53 +02:00
{
self->priv = G_TYPE_INSTANCE_GET_PRIVATE
2010-09-25 16:14:09 +02:00
(self, LD_TYPE_LIBRARY, LdLibraryPrivate);
2010-09-13 19:24:53 +02:00
2010-09-25 16:55:07 +02:00
self->priv->lua = ld_lua_new ();
2010-09-13 19:24:53 +02:00
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
2010-09-25 16:14:09 +02:00
ld_library_finalize (GObject *gobject)
2010-09-13 19:24:53 +02:00
{
2010-09-25 16:14:09 +02:00
LdLibrary *self;
2010-09-13 19:24:53 +02:00
2010-09-25 16:14:09 +02:00
self = LD_LIBRARY (gobject);
2010-09-13 19:24:53 +02:00
2010-09-25 16:55:07 +02:00
g_object_unref (self->priv->lua);
2010-09-13 19:24:53 +02:00
g_hash_table_destroy (self->categories);
/* Chain up to the parent class. */
2010-09-25 16:14:09 +02:00
G_OBJECT_CLASS (ld_library_parent_class)->finalize (gobject);
2010-09-13 19:24:53 +02:00
}
/**
2010-09-25 16:14:09 +02:00
* ld_library_new:
2010-09-13 19:24:53 +02:00
*
* Create an instance.
*/
2010-09-25 16:14:09 +02:00
LdLibrary *
ld_library_new (void)
2010-09-13 19:24:53 +02:00
{
2010-09-25 16:14:09 +02:00
return g_object_new (LD_TYPE_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 *
2010-09-25 16:14:09 +02:00
load_category (LdLibrary *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;
2010-09-25 16:14:09 +02:00
g_return_val_if_fail (LD_IS_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 (name);
ld_symbol_category_set_image_path (cat, icon_file);
g_free (icon_file);
2010-09-13 19:24:53 +02:00
return cat;
}
/**
2010-09-25 16:14:09 +02:00
* ld_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
2010-09-25 16:14:09 +02:00
ld_library_load (LdLibrary *self, const char *path)
2010-09-13 19:24:53 +02:00
{
GDir *dir;
const gchar *item;
gboolean changed = FALSE;
2010-09-13 19:24:53 +02:00
2010-09-25 16:14:09 +02:00
g_return_val_if_fail (LD_IS_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,
g_strdup (ld_symbol_category_get_name (cat)), cat);
2010-09-13 19:24:53 +02:00
g_free (categ_path);
changed = TRUE;
2010-09-13 19:24:53 +02:00
}
g_dir_close (dir);
if (changed)
g_signal_emit (self,
2010-09-25 16:14:09 +02:00
LD_LIBRARY_GET_CLASS (self)->changed_signal, 0);
2010-09-13 19:24:53 +02:00
return TRUE;
}
/**
2010-09-25 16:14:09 +02:00
* ld_library_clear:
* @self: A symbol library object.
2010-09-13 19:24:53 +02:00
*
* Clears all the contents.
*/
void
2010-09-25 16:14:09 +02:00
ld_library_clear (LdLibrary *self)
2010-09-13 19:24:53 +02:00
{
2010-09-25 16:14:09 +02:00
g_return_if_fail (LD_IS_LIBRARY (self));
2010-09-13 19:24:53 +02:00
g_hash_table_remove_all (self->categories);
g_signal_emit (self,
2010-09-25 16:14:09 +02:00
LD_LIBRARY_GET_CLASS (self)->changed_signal, 0);
2010-09-13 19:24:53 +02:00
}