grim/gplate

5978a3cd8f76
Parents 47939ccab6e9
Children 38c821a4e7c8
Added a new GPlateDirectoryVariable
--- a/gplate/variables/Makefile.am Fri Jul 18 02:12:56 2008 -0500
+++ b/gplate/variables/Makefile.am Fri Jul 18 02:13:15 2008 -0500
@@ -8,6 +8,7 @@
gplatevariablesinc_HEADERS=\
gplate-dictionary-variable.h \
+ gplate-directory-variable.h \
gplate-file-variable.h \
gplate-object-variable.h
@@ -15,6 +16,7 @@
libgplate_variables_la_SOURCES=\
gplate-dictionary-variable.c \
+ gplate-directory-variable.h \
gplate-file-variable.c \
gplate-object-variable.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/gplate/variables/gplate-directory-variable.c Fri Jul 18 02:13:15 2008 -0500
@@ -0,0 +1,267 @@
+/*
+ * Copyright (C) 2007-2008 Gary Kramlich <grim@reaperworld.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ */
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif /* HAVE_CONFIG_H */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include <gplate/gplate-collection.h>
+
+#include <gplate/variables/gplate-directory-variable.h>
+
+#define GPLATE_DIRECTORY_VARIABLE_GET_PRIVATE(obj) \
+ (G_TYPE_INSTANCE_GET_PRIVATE((obj), GPLATE_TYPE_DIRECTORY_VARIABLE, GPlateDirectoryVariablePrivate))
+
+/******************************************************************************
+ * Enums
+ *****************************************************************************/
+enum {
+ PROP_ZERO,
+ PROP_PATH,
+ PROP_LAST,
+};
+
+/******************************************************************************
+ * Structs
+ *****************************************************************************/
+typedef struct {
+ gchar *path;
+} GPlateDirectoryVariablePrivate;
+
+/******************************************************************************
+ * Globals
+ *****************************************************************************/
+static GObjectClass *parent_class = NULL;
+
+/******************************************************************************
+ * Private API
+ *****************************************************************************/
+static void
+gplate_directory_variable_set_path(GPlateDirectoryVariable *directory_variable,
+ const gchar *path)
+{
+ GPlateDirectoryVariablePrivate *priv = NULL;
+
+ g_return_if_fail(GPLATE_IS_DIRECTORY_VARIABLE(directory_variable));
+ g_return_if_fail(path);
+
+ priv = GPLATE_DIRECTORY_VARIABLE_GET_PRIVATE(directory_variable);
+
+ g_free(priv->path);
+
+ priv->path = g_strdup(path);
+
+ gplate_directory_variable_refresh(directory_variable);
+}
+
+static void
+gplate_directory_variable_real_refresh(GPlateDirectoryVariable *directory_variable) {
+ GPlateDirectoryVariablePrivate *priv = NULL;
+ GPlateCollection *collection = NULL;
+ GDir *dir = NULL;
+ const gchar *entry = NULL;
+
+ g_return_if_fail(GPLATE_IS_DIRECTORY_VARIABLE(directory_variable));
+
+ priv = GPLATE_DIRECTORY_VARIABLE_GET_PRIVATE(directory_variable);
+ collection = GPLATE_COLLECTION(directory_variable);
+
+ /* open the directory */
+ dir = g_dir_open(priv->path, 0, NULL);
+ if(!dir)
+ return;
+
+ /* clear out the old junk, this might not be necessary, but then we'd need
+ * to manually track which files were removed, and i'm not doing that right
+ * now :P
+ */
+ gplate_collection_remove_all(GPLATE_COLLECTION(directory_variable));
+
+ /* now add all our files */
+ while((entry = g_dir_read_name(dir)) != NULL) {
+ GPlateVariable *variable = NULL;
+ gchar *filename = NULL;
+
+ filename = g_build_filename(priv->path, entry, NULL);
+ variable = gplate_file_variable_new(entry, filename);
+ g_free(filename);
+
+ if(GPLATE_IS_VARIABLE(variable))
+ gplate_collection_add_variable(collection, variable);
+ }
+
+ /* close that shit back up, NOW! */
+ g_dir_close(dir);
+}
+
+/******************************************************************************
+ * Object Stuff
+ *****************************************************************************/
+static void
+gplate_directory_variable_get_property(GObject *obj, guint param_id, GValue *value,
+ GParamSpec *pspec)
+{
+ GPlateDirectoryVariable *var = GPLATE_DIRECTORY_VARIABLE(obj);
+
+ switch(param_id) {
+ case PROP_PATH:
+ g_value_set_string(value, gplate_directory_variable_get_path(var));
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
+ break;
+ }
+}
+
+static void
+gplate_directory_variable_set_property(GObject *obj, guint param_id,
+ const GValue *value, GParamSpec *pspec)
+{
+ GPlateDirectoryVariable *var = GPLATE_DIRECTORY_VARIABLE(obj);
+
+ switch(param_id) {
+ case PROP_PATH:
+ gplate_directory_variable_set_path(var, g_value_get_string(value));
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
+ break;
+ }
+}
+
+static void
+gplate_directory_variable_finalize(GObject *obj) {
+ GPlateDirectoryVariablePrivate *priv = NULL;
+
+ priv = GPLATE_DIRECTORY_VARIABLE_GET_PRIVATE(obj);
+
+ g_free(priv->path);
+
+ G_OBJECT_CLASS(parent_class)->finalize(obj);
+}
+
+static void
+gplate_directory_variable_class_init(GPlateVariableClass *klass) {
+ GPlateDirectoryVariableClass *fvar_class = GPLATE_DIRECTORY_VARIABLE_CLASS(klass);
+ GObjectClass *obj_class = G_OBJECT_CLASS(klass);
+ GParamSpec *pspec = NULL;
+
+ g_type_class_add_private(klass, sizeof(GPlateDirectoryVariablePrivate));
+
+ parent_class = g_type_class_peek_parent(klass);
+
+ obj_class->get_property = gplate_directory_variable_get_property;
+ obj_class->set_property = gplate_directory_variable_set_property;
+ obj_class->finalize = gplate_directory_variable_finalize;
+
+ klass->get_value = NULL;
+
+ fvar_class->refresh = gplate_directory_variable_real_refresh;
+
+ pspec = g_param_spec_string("path", "path",
+ "The path for this variable.",
+ NULL,
+ G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
+ g_object_class_install_property(obj_class, PROP_PATH, pspec);
+}
+
+/******************************************************************************
+ * GPlateVariable API
+ *****************************************************************************/
+GType
+gplate_directory_variable_get_gtype(void) {
+ static GType type = 0;
+
+ if(type == 0) {
+ static const GTypeInfo info = {
+ sizeof(GPlateDirectoryVariableClass),
+ NULL,
+ NULL,
+ (GClassInitFunc)gplate_directory_variable_class_init,
+ NULL,
+ NULL,
+ sizeof(GPlateDirectoryVariable),
+ 0,
+ NULL,
+ };
+
+ type = g_type_register_static(GPLATE_TYPE_DICTIONARY_VARIABLE,
+ "GPlateDirectoryVariable",
+ &info, 0);
+ }
+
+ return type;
+}
+
+/**
+ * gplate_directory_variable_new:
+ * @name: The name of the directory to create.
+ * @path: The name of the directory.
+ *
+ * Creates a new #GPlateDirectoryVariable named @name.
+ *
+ * Return Value: The new #GPlateDirectoryVariable.
+ */
+GPlateVariable *
+gplate_directory_variable_new(const gchar *name, const gchar *path) {
+ return g_object_new(GPLATE_TYPE_DIRECTORY_VARIABLE,
+ "name", name,
+ "path", path,
+ NULL);
+}
+
+/**
+ * gplate_directory_variable_get_path:
+ * @directory_variable: The #GPlateDirectoryVariable.
+ *
+ * Gets the path of @directory_variable.
+ *
+ * Return Value: The path of @directory_variable.
+ */
+const gchar *
+gplate_directory_variable_get_path(const GPlateDirectoryVariable *directory_variable) {
+ GPlateDirectoryVariablePrivate *priv = NULL;
+
+ g_return_val_if_fail(GPLATE_IS_DIRECTORY_VARIABLE(directory_variable), NULL);
+
+ priv = GPLATE_DIRECTORY_VARIABLE_GET_PRIVATE(directory_variable);
+
+ return priv->path;
+}
+
+/**
+ * gplate_directory_variable_refresh:
+ * @directory_variable: The #GPlateDirectoryVariable.
+ *
+ * Refreshes all of the attributes store in @directory_variable.
+ */
+void
+gplate_directory_variable_refresh(GPlateDirectoryVariable *directory_variable) {
+ GPlateDirectoryVariableClass *klass = NULL;
+
+ g_return_if_fail(GPLATE_IS_DIRECTORY_VARIABLE(directory_variable));
+
+ klass = GPLATE_DIRECTORY_VARIABLE_GET_CLASS(directory_variable);
+
+ if(klass && klass->refresh)
+ klass->refresh(directory_variable);
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/gplate/variables/gplate-directory-variable.h Fri Jul 18 02:13:15 2008 -0500
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) 2007-2008 Gary Kramlich <grim@reaperworld.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ */
+#ifndef GPLATE_DIRECTORY_VARIABLE_H
+#define GPLATE_DIRECTORY_VARIABLE_H
+
+#include <gplate/variables/gplate-dictionary-variable.h>
+
+#define GPLATE_TYPE_DIRECTORY_VARIABLE (gplate_directory_variable_get_gtype())
+#define GPLATE_DIRECTORY_VARIABLE(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GPLATE_TYPE_DIRECTORY_VARIABLE, GPlateDirectoryVariable))
+#define GPLATE_DIRECTORY_VARIABLE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GPLATE_TYPE_DIRECTORY_VARIABLE, GPlateDirectoryVariableClass))
+#define GPLATE_IS_DIRECTORY_VARIABLE(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GPLATE_TYPE_DIRECTORY_VARIABLE))
+#define GPLATE_IS_DIRECTORY_VARIABLE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GPLATE_TYPE_DIRECTORY_VARIABLE))
+#define GPLATE_DIRECTORY_VARIABLE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GPLATE_TYPE_DIRECTORY_VARIABLE, GPlateDirectoryVariableClass))
+
+typedef struct _GPlateDirectoryVariable GPlateDirectoryVariable;
+typedef struct _GPlateDirectoryVariableClass GPlateDirectoryVariableClass;
+
+struct _GPlateDirectoryVariable {
+ GPlateDictionaryVariable gparent;
+
+ void (*_gplate_reserved1)(void);
+ void (*_gplate_reserved2)(void);
+ void (*_gplate_reserved3)(void);
+ void (*_gplate_reserved4)(void);
+};
+
+struct _GPlateDirectoryVariableClass {
+ GPlateDictionaryVariableClass parent;
+
+ void (*refresh)(GPlateDirectoryVariable *directory_variable);
+
+ void (*_gplate_reserved1)(void);
+ void (*_gplate_reserved2)(void);
+ void (*_gplate_reserved3)(void);
+ void (*_gplate_reserved4)(void);
+};
+
+G_BEGIN_DECLS
+
+GType gplate_directory_variable_get_gtype(void);
+
+GPlateVariable *gplate_directory_variable_new(const gchar *name, const gchar *path);
+
+const gchar *gplate_directory_variable_get_path(const GPlateDirectoryVariable *directory_variable);
+
+void gplate_directory_variable_refresh(GPlateDirectoryVariable *directory_variable);
+
+G_END_DECLS
+
+#endif /* GPLATE_DIRECTORY_VARIABLE_H */