[gnome-builder] environment: add IdeEnvironment



commit b62d5429251227dd7bb815acc1fcf0244dd13d6d
Author: Christian Hergert <chergert redhat com>
Date:   Sun Feb 14 20:36:31 2016 -0800

    environment: add IdeEnvironment
    
    IdeEnvironment is a GListModel to manage environment variables. It seems
    a bit heavy handed for a glorified array of key=value, but it vastly
    simplifies the environment editor case.

 libide/Makefile.am                |    4 +
 libide/ide-environment-variable.c |  179 +++++++++++++++++++++++++
 libide/ide-environment-variable.h |   41 ++++++
 libide/ide-environment.c          |  265 +++++++++++++++++++++++++++++++++++++
 libide/ide-environment.h          |   47 +++++++
 5 files changed, 536 insertions(+), 0 deletions(-)
---
diff --git a/libide/Makefile.am b/libide/Makefile.am
index 657d914..2f0461b 100644
--- a/libide/Makefile.am
+++ b/libide/Makefile.am
@@ -83,6 +83,10 @@ libide_1_0_la_public_sources = \
        ide-diagnostics.h \
        ide-enums.c \
        ide-enums.h \
+       ide-environment.c \
+       ide-environment.h \
+       ide-environment-variable.c \
+       ide-environment-variable.h \
        ide-executable.c \
        ide-executable.h \
        ide-executer.c \
diff --git a/libide/ide-environment-variable.c b/libide/ide-environment-variable.c
new file mode 100644
index 0000000..58d6da7
--- /dev/null
+++ b/libide/ide-environment-variable.c
@@ -0,0 +1,179 @@
+/* ide-environment-variable.c
+ *
+ * Copyright (C) 2016 Christian Hergert <christian hergert me>
+ *
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "ide-environment-variable.h"
+
+struct _IdeEnvironmentVariable
+{
+  GObject  parent_instance;
+  gchar   *key;
+  gchar   *value;
+};
+
+G_DEFINE_TYPE (IdeEnvironmentVariable, ide_environment_variable, G_TYPE_OBJECT)
+
+enum {
+  PROP_0,
+  PROP_KEY,
+  PROP_VALUE,
+  LAST_PROP
+};
+
+static GParamSpec *properties [LAST_PROP];
+
+static void
+ide_environment_variable_finalize (GObject *object)
+{
+  IdeEnvironmentVariable *self = (IdeEnvironmentVariable *)object;
+
+  g_clear_pointer (&self->key, g_free);
+  g_clear_pointer (&self->value, g_free);
+
+  G_OBJECT_CLASS (ide_environment_variable_parent_class)->finalize (object);
+}
+
+static void
+ide_environment_variable_get_property (GObject    *object,
+                                       guint       prop_id,
+                                       GValue     *value,
+                                       GParamSpec *pspec)
+{
+  IdeEnvironmentVariable *self = IDE_ENVIRONMENT_VARIABLE(object);
+
+  switch (prop_id)
+    {
+    case PROP_KEY:
+      g_value_set_string (value, self->key);
+      break;
+
+    case PROP_VALUE:
+      g_value_set_string (value, self->value);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+    }
+}
+
+static void
+ide_environment_variable_set_property (GObject      *object,
+                                       guint         prop_id,
+                                       const GValue *value,
+                                       GParamSpec   *pspec)
+{
+  IdeEnvironmentVariable *self = IDE_ENVIRONMENT_VARIABLE(object);
+
+  switch (prop_id)
+    {
+    case PROP_KEY:
+      ide_environment_variable_set_key (self, g_value_get_string (value));
+      break;
+
+    case PROP_VALUE:
+      ide_environment_variable_set_value (self, g_value_get_string (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+    }
+}
+
+static void
+ide_environment_variable_class_init (IdeEnvironmentVariableClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->finalize = ide_environment_variable_finalize;
+  object_class->get_property = ide_environment_variable_get_property;
+  object_class->set_property = ide_environment_variable_set_property;
+
+  properties [PROP_KEY] =
+    g_param_spec_string ("key",
+                         "Key",
+                         "The key for the environment variable",
+                         NULL,
+                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  properties [PROP_VALUE] =
+    g_param_spec_string ("value",
+                         "Value",
+                         "The value for the environment variable",
+                         NULL,
+                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_properties (object_class, LAST_PROP, properties);
+}
+
+static void
+ide_environment_variable_init (IdeEnvironmentVariable *self)
+{
+}
+
+const gchar *
+ide_environment_variable_get_key (IdeEnvironmentVariable *self)
+{
+  g_return_val_if_fail (IDE_IS_ENVIRONMENT_VARIABLE (self), NULL);
+
+  return self->key;
+}
+
+void
+ide_environment_variable_set_key (IdeEnvironmentVariable *self,
+                                  const gchar            *key)
+{
+  g_return_if_fail (IDE_IS_ENVIRONMENT_VARIABLE (self));
+
+  if (g_strcmp0 (key, self->key) != 0)
+    {
+      g_free (self->key);
+      self->key = g_strdup (key);
+      g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_KEY]);
+    }
+}
+
+const gchar *
+ide_environment_variable_get_value (IdeEnvironmentVariable *self)
+{
+  g_return_val_if_fail (IDE_IS_ENVIRONMENT_VARIABLE (self), NULL);
+
+  return self->value;
+}
+
+void
+ide_environment_variable_set_value (IdeEnvironmentVariable *self,
+                                    const gchar            *value)
+{
+  g_return_if_fail (IDE_IS_ENVIRONMENT_VARIABLE (self));
+
+  if (g_strcmp0 (value, self->value) != 0)
+    {
+      g_free (self->value);
+      self->value = g_strdup (value);
+      g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_VALUE]);
+    }
+}
+
+IdeEnvironmentVariable *
+ide_environment_variable_new (const gchar *key,
+                              const gchar *value)
+{
+  return g_object_new (IDE_TYPE_ENVIRONMENT_VARIABLE,
+                       "key", key,
+                       "value", value,
+                       NULL);
+}
diff --git a/libide/ide-environment-variable.h b/libide/ide-environment-variable.h
new file mode 100644
index 0000000..910086b
--- /dev/null
+++ b/libide/ide-environment-variable.h
@@ -0,0 +1,41 @@
+/* ide-environment-variable.h
+ *
+ * Copyright (C) 2016 Christian Hergert <christian hergert me>
+ *
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef IDE_ENVIRONMENT_VARIABLE_H
+#define IDE_ENVIRONMENT_VARIABLE_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_ENVIRONMENT_VARIABLE (ide_environment_variable_get_type())
+
+G_DECLARE_FINAL_TYPE (IdeEnvironmentVariable, ide_environment_variable, IDE, ENVIRONMENT_VARIABLE, GObject)
+
+IdeEnvironmentVariable *ide_environment_variable_new       (const gchar            *key,
+                                                            const gchar            *value);
+const gchar            *ide_environment_variable_get_key   (IdeEnvironmentVariable *self);
+void                    ide_environment_variable_set_key   (IdeEnvironmentVariable *self,
+                                                            const gchar            *key);
+const gchar            *ide_environment_variable_get_value (IdeEnvironmentVariable *self);
+void                    ide_environment_variable_set_value (IdeEnvironmentVariable *self,
+                                                            const gchar            *value);
+
+G_END_DECLS
+
+#endif /* IDE_ENVIRONMENT_VARIABLE_H */
diff --git a/libide/ide-environment.c b/libide/ide-environment.c
new file mode 100644
index 0000000..3bdbdae
--- /dev/null
+++ b/libide/ide-environment.c
@@ -0,0 +1,265 @@
+/* ide-environment.c
+ *
+ * Copyright (C) 2016 Christian Hergert <christian hergert me>
+ *
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "ide-environment.h"
+#include "ide-environment-variable.h"
+
+struct _IdeEnvironment
+{
+  GObject    parent_instance;
+  GPtrArray *variables;
+};
+
+static void list_model_iface_init (GListModelInterface *iface);
+
+G_DEFINE_TYPE_EXTENDED (IdeEnvironment, ide_environment, G_TYPE_OBJECT, 0,
+                        G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, list_model_iface_init))
+
+static void
+ide_environment_finalize (GObject *object)
+{
+  IdeEnvironment *self = (IdeEnvironment *)object;
+
+  g_clear_pointer (&self->variables, g_ptr_array_unref);
+
+  G_OBJECT_CLASS (ide_environment_parent_class)->finalize (object);
+}
+
+static void
+ide_environment_class_init (IdeEnvironmentClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->finalize = ide_environment_finalize;
+}
+
+static void
+ide_environment_init (IdeEnvironment *self)
+{
+  self->variables = g_ptr_array_new_with_free_func (g_object_unref);
+}
+
+static GType
+ide_environment_get_item_type (GListModel *model)
+{
+  return IDE_TYPE_ENVIRONMENT_VARIABLE;
+}
+
+static gpointer
+ide_environment_get_item (GListModel *model,
+                          guint       position)
+{
+  IdeEnvironment *self = (IdeEnvironment *)model;
+
+  g_return_val_if_fail (IDE_IS_ENVIRONMENT (self), NULL);
+  g_return_val_if_fail (position < self->variables->len, NULL);
+
+  return g_object_ref (g_ptr_array_index (self->variables, position));
+}
+
+static guint
+ide_environment_get_n_items (GListModel *model)
+{
+  IdeEnvironment *self = (IdeEnvironment *)model;
+
+  g_return_val_if_fail (IDE_IS_ENVIRONMENT (self), 0);
+
+  return self->variables->len;
+}
+
+static void
+list_model_iface_init (GListModelInterface *iface)
+{
+  iface->get_n_items = ide_environment_get_n_items;
+  iface->get_item = ide_environment_get_item;
+  iface->get_item_type = ide_environment_get_item_type;
+}
+
+void
+ide_environment_setenv (IdeEnvironment *self,
+                        const gchar    *key,
+                        const gchar    *value)
+{
+  guint i;
+
+  g_return_if_fail (IDE_IS_ENVIRONMENT (self));
+  g_return_if_fail (key != NULL);
+
+  for (i = 0; i < self->variables->len; i++)
+    {
+      IdeEnvironmentVariable *var = g_ptr_array_index (self->variables, i);
+      const gchar *var_key = ide_environment_variable_get_key (var);
+
+      if (g_strcmp0 (key, var_key) == 0)
+        {
+          if (value == NULL)
+            {
+              g_ptr_array_remove_index (self->variables, i);
+              g_list_model_items_changed (G_LIST_MODEL (self), i, 1, 0);
+              return;
+            }
+
+          ide_environment_variable_set_value (var, value);
+          return;
+        }
+    }
+
+  if (value != NULL)
+    {
+      IdeEnvironmentVariable *var;
+      guint position = self->variables->len;
+
+      var = g_object_new (IDE_TYPE_ENVIRONMENT_VARIABLE,
+                          "key", key,
+                          "value", value,
+                          NULL);
+      g_ptr_array_add (self->variables, var);
+      g_list_model_items_changed (G_LIST_MODEL (self), position, 0, 1);
+    }
+}
+
+const gchar *
+ide_environment_getenv (IdeEnvironment *self,
+                        const gchar    *key)
+{
+  guint i;
+
+  g_return_val_if_fail (IDE_IS_ENVIRONMENT (self), NULL);
+  g_return_val_if_fail (key != NULL, NULL);
+
+  for (i = 0; i < self->variables->len; i++)
+    {
+      IdeEnvironmentVariable *var = g_ptr_array_index (self->variables, i);
+      const gchar *var_key = ide_environment_variable_get_key (var);
+
+      if (g_strcmp0 (key, var_key) == 0)
+        return ide_environment_variable_get_value (var);
+    }
+
+  return NULL;
+}
+
+/**
+ * ide_environment_get_environ:
+ * @self: An #IdeEnvironment
+ *
+ * Gets the environment as a set of key=value pairs, suitable for use
+ * in various GLib process functions.
+ *
+ * Returns: (transfer full): A newly allocated string array.
+ */
+gchar **
+ide_environment_get_environ (IdeEnvironment *self)
+{
+  GPtrArray *ar;
+  guint i;
+
+  g_return_val_if_fail (IDE_IS_ENVIRONMENT (self), NULL);
+
+  ar = g_ptr_array_new ();
+
+  for (i = 0; i < self->variables->len; i++)
+    {
+      IdeEnvironmentVariable *var = g_ptr_array_index (self->variables, i);
+      const gchar *key = ide_environment_variable_get_key (var);
+      const gchar *value = ide_environment_variable_get_value (var);
+
+      if (value == NULL)
+        value = "";
+
+      if (key != NULL)
+        g_ptr_array_add (ar, g_strdup_printf ("%s=%s", key, value));
+    }
+
+  g_ptr_array_add (ar, NULL);
+
+  return (gchar **)g_ptr_array_free (ar, FALSE);
+}
+
+IdeEnvironment *
+ide_environment_new (void)
+{
+  return g_object_new (IDE_TYPE_ENVIRONMENT, NULL);
+}
+
+void
+ide_environment_remove (IdeEnvironment         *self,
+                        IdeEnvironmentVariable *variable)
+{
+  guint i;
+
+  g_return_if_fail (IDE_IS_ENVIRONMENT (self));
+  g_return_if_fail (IDE_IS_ENVIRONMENT_VARIABLE (variable));
+
+  for (i = 0; i < self->variables->len; i++)
+    {
+      IdeEnvironmentVariable *item = g_ptr_array_index (self->variables, i);
+
+      if (item == variable)
+        {
+          g_ptr_array_remove_index (self->variables, i);
+          g_list_model_items_changed (G_LIST_MODEL (self), i, 1, 0);
+          break;
+        }
+    }
+}
+
+void
+ide_environment_append (IdeEnvironment         *self,
+                        IdeEnvironmentVariable *variable)
+{
+  guint position;
+
+  g_return_if_fail (IDE_IS_ENVIRONMENT (self));
+  g_return_if_fail (IDE_IS_ENVIRONMENT_VARIABLE (variable));
+
+  position = self->variables->len;
+
+  g_ptr_array_add (self->variables, g_object_ref (variable));
+  g_list_model_items_changed (G_LIST_MODEL (self), position, 0, 1);
+}
+
+/**
+ * ide_environment_copy:
+ * @self: An #IdeEnvironment
+ *
+ * Copies the contents of #IdeEnvironment into a newly allocated #IdeEnvironment.
+ *
+ * Returns: (transfer full): An #IdeEnvironment.
+ */
+IdeEnvironment *
+ide_environment_copy (IdeEnvironment *self)
+{
+  IdeEnvironment *copy;
+  guint i;
+
+  g_return_val_if_fail (IDE_IS_ENVIRONMENT (self), NULL);
+
+  copy = ide_environment_new ();
+
+  for (i = 0; i < self->variables->len; i++)
+    {
+      IdeEnvironmentVariable *var = g_ptr_array_index (self->variables, i);
+      const gchar *key = ide_environment_variable_get_key (var);
+      const gchar *value = ide_environment_variable_get_value (var);
+
+      ide_environment_setenv (copy, key, value);
+    }
+
+  return copy;
+}
diff --git a/libide/ide-environment.h b/libide/ide-environment.h
new file mode 100644
index 0000000..823786c
--- /dev/null
+++ b/libide/ide-environment.h
@@ -0,0 +1,47 @@
+/* ide-environment.h
+ *
+ * Copyright (C) 2016 Christian Hergert <christian hergert me>
+ *
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef IDE_ENVIRONMENT_H
+#define IDE_ENVIRONMENT_H
+
+#include <gio/gio.h>
+
+#include "ide-environment-variable.h"
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_ENVIRONMENT (ide_environment_get_type())
+
+G_DECLARE_FINAL_TYPE (IdeEnvironment, ide_environment, IDE, ENVIRONMENT, GObject)
+
+IdeEnvironment *ide_environment_new         (void);
+void            ide_environment_setenv      (IdeEnvironment         *self,
+                                             const gchar            *key,
+                                             const gchar            *value);
+const gchar    *ide_environment_getenv      (IdeEnvironment         *self,
+                                             const gchar            *key);
+gchar         **ide_environment_get_environ (IdeEnvironment         *self);
+void            ide_environment_append      (IdeEnvironment         *self,
+                                             IdeEnvironmentVariable *variable);
+void            ide_environment_remove      (IdeEnvironment         *self,
+                                             IdeEnvironmentVariable *variable);
+IdeEnvironment *ide_environment_copy        (IdeEnvironment         *self);
+
+G_END_DECLS
+
+#endif /* IDE_ENVIRONMENT_H */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]