[gnome-builder] libide: create IdeFile if it doesn't already exist.



commit 482524f57b99e011b68227189a89d44e621a7d52
Author: Christian Hergert <christian hergert me>
Date:   Wed Feb 11 20:27:30 2015 -0800

    libide: create IdeFile if it doesn't already exist.

 libide/ide-project-files.c |   49 +++++++++++++++++++++++++++++++++++++------
 1 files changed, 42 insertions(+), 7 deletions(-)
---
diff --git a/libide/ide-project-files.c b/libide/ide-project-files.c
index 6f0edcb..79e8246 100644
--- a/libide/ide-project-files.c
+++ b/libide/ide-project-files.c
@@ -19,16 +19,40 @@
 #include "ide-project-file.h"
 #include "ide-project-files.h"
 
-G_DEFINE_TYPE (IdeProjectFiles, ide_project_files, IDE_TYPE_PROJECT_ITEM)
+typedef struct
+{
+  GHashTable *files_by_path;
+} IdeProjectFilesPrivate;
+
+G_DEFINE_TYPE_WITH_PRIVATE (IdeProjectFiles, ide_project_files,
+                            IDE_TYPE_PROJECT_ITEM)
+
+static void
+ide_project_files_dispose (GObject *object)
+{
+  IdeProjectFiles *self = (IdeProjectFiles *)object;
+  IdeProjectFilesPrivate *priv = ide_project_files_get_instance_private (self);
+
+  g_clear_pointer (&priv->files_by_path, g_hash_table_unref);
+
+  G_OBJECT_CLASS (ide_project_files_parent_class)->dispose (object);
+}
 
 static void
 ide_project_files_class_init (IdeProjectFilesClass *klass)
 {
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->dispose = ide_project_files_dispose;
 }
 
 static void
 ide_project_files_init (IdeProjectFiles *self)
 {
+  IdeProjectFilesPrivate *priv = ide_project_files_get_instance_private (self);
+
+  priv->files_by_path = g_hash_table_new_full (g_str_hash, g_str_equal,
+                                               g_free, g_object_unref);
 }
 
 static IdeProjectItem *
@@ -68,12 +92,17 @@ IdeFile *
 ide_project_files_get_file_for_path (IdeProjectFiles *self,
                                      const gchar     *path)
 {
+  IdeProjectFilesPrivate *priv = ide_project_files_get_instance_private (self);
   IdeProjectItem *item = (IdeProjectItem *)self;
+  IdeFile *file;
   gchar **parts;
   gsize i;
 
   g_return_val_if_fail (IDE_IS_PROJECT_FILES (self), NULL);
 
+  if ((file = g_hash_table_lookup (priv->files_by_path, path)))
+    return file;
+
   parts = g_strsplit (path, G_DIR_SEPARATOR_S, 0);
 
   for (i = 0; item && parts [i]; i++)
@@ -81,12 +110,18 @@ ide_project_files_get_file_for_path (IdeProjectFiles *self,
 
   if (item)
     {
-      IdeFile *file = NULL;
-
-      g_warning ("TODO: Get/find/create idefile for %s\n", ide_project_file_get_name (IDE_PROJECT_FILE 
(item)));
-
-      return file;
+      IdeContext *context;
+      GFile *gfile;
+
+      context = ide_object_get_context (IDE_OBJECT (self));
+      gfile = ide_project_file_get_file (IDE_PROJECT_FILE (item));
+      file = g_object_new (IDE_TYPE_FILE,
+                           "context", context,
+                           "file", gfile,
+                           NULL);
+      if (file)
+        g_hash_table_insert (priv->files_by_path, g_strdup (path), file);
     }
 
-  return NULL;
+  return file;
 }


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