[gnome-builder/wip/libide] libide: add helper to get specific unsaved file



commit 22024b8930b43c6e161bb672dd5f1af81c2fa11b
Author: Christian Hergert <christian hergert me>
Date:   Sun Feb 15 00:17:04 2015 -0800

    libide: add helper to get specific unsaved file

 libide/ide-unsaved-files.c |   74 +++++++++++++++++++++++++++++++++++++++++++-
 libide/ide-unsaved-files.h |   44 +++++++++++++------------
 2 files changed, 96 insertions(+), 22 deletions(-)
---
diff --git a/libide/ide-unsaved-files.c b/libide/ide-unsaved-files.c
index dea344b..228bbe2 100644
--- a/libide/ide-unsaved-files.c
+++ b/libide/ide-unsaved-files.c
@@ -16,6 +16,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <glib/gstdio.h>
 #include <string.h>
 
 #include "ide-context.h"
@@ -30,6 +31,8 @@ typedef struct
   gint64  sequence;
   GFile  *file;
   GBytes *content;
+  gchar  *temp_path;
+  gint    temp_fd;
 } UnsavedFile;
 
 typedef struct
@@ -68,6 +71,9 @@ unsaved_file_free (gpointer data)
     {
       g_object_unref (uf->file);
       g_bytes_unref (uf->content);
+      g_unlink (uf->temp_path);
+      g_free (uf->temp_path);
+      g_close (uf->temp_fd, NULL);
       g_slice_free (UnsavedFile, uf);
     }
 }
@@ -91,6 +97,10 @@ unsaved_file_save (UnsavedFile  *uf,
 {
   gboolean ret;
 
+  g_assert (uf);
+  g_assert (uf->content);
+  g_assert (path);
+
   ret = g_file_set_contents (path,
                              g_bytes_get_data (uf->content, NULL),
                              g_bytes_get_size (uf->content),
@@ -400,6 +410,28 @@ ide_unsaved_files_remove (IdeUnsavedFiles *self,
     }
 }
 
+static void
+setup_tempfile (GFile  *file,
+                gint   *temp_fd,
+                gchar **temp_path)
+{
+  g_autoptr(gchar) name = NULL;
+  const gchar *suffix;
+  gchar *template;
+
+  g_assert (G_IS_FILE (file));
+  g_assert (temp_fd);
+  g_assert (temp_path);
+
+  *temp_fd = -1;
+  *temp_path = NULL;
+
+  name = g_file_get_basename (file);
+  suffix = strrchr (name, '.') ?: "";
+  template = g_strdup_printf ("builder_codeassistant_XXXXXX%s", suffix);
+  *temp_fd = g_file_open_tmp (template, temp_path, NULL);
+}
+
 void
 ide_unsaved_files_update (IdeUnsavedFiles *self,
                           GFile           *file,
@@ -431,6 +463,7 @@ ide_unsaved_files_update (IdeUnsavedFiles *self,
               g_clear_pointer (&unsaved->content, g_bytes_unref);
               unsaved->content = g_bytes_ref (content);
               unsaved->sequence = priv->sequence;
+              setup_tempfile (file, &unsaved->temp_fd, &unsaved->temp_path);
             }
 
           /*
@@ -489,7 +522,8 @@ ide_unsaved_files_get_unsaved_files (IdeUnsavedFiles *self)
       UnsavedFile *uf;
 
       uf = g_ptr_array_index (priv->unsaved_files, i);
-      item = _ide_unsaved_file_new (uf->file, uf->content, uf->sequence);
+      item = _ide_unsaved_file_new (uf->file, uf->content, uf->temp_path,
+                                    uf->sequence);
 
       g_ptr_array_add (ar, item);
     }
@@ -497,6 +531,44 @@ ide_unsaved_files_get_unsaved_files (IdeUnsavedFiles *self)
   return ar;
 }
 
+/**
+ * ide_unsaved_files_get_unsaved_file:
+ *
+ * Retrieves the unsaved file content for a particular file. If no unsaved
+ * file content is registered, %NULL is returned.
+ *
+ * Returns: (nullable) (transfer full): An #IdeUnsavedFile or %NULL.
+ */
+IdeUnsavedFile *
+ide_unsaved_files_get_unsaved_file (IdeUnsavedFiles *self,
+                                    GFile           *file)
+{
+  IdeUnsavedFilesPrivate *priv;
+  IdeUnsavedFile *ret = NULL;
+  gsize i;
+
+  g_return_val_if_fail (IDE_IS_UNSAVED_FILES (self), NULL);
+
+  priv = ide_unsaved_files_get_instance_private (self);
+
+  for (i = 0; i < priv->unsaved_files->len; i++)
+    {
+      IdeUnsavedFile *item;
+      UnsavedFile *uf;
+
+      uf = g_ptr_array_index (priv->unsaved_files, i);
+
+      if (g_file_equal (uf->file, file))
+        {
+          ret = _ide_unsaved_file_new (uf->file, uf->content, uf->temp_path,
+                                       uf->sequence);
+          break;
+        }
+    }
+
+  return ret;
+}
+
 gint64
 ide_unsaved_files_get_sequence (IdeUnsavedFiles *self)
 {
diff --git a/libide/ide-unsaved-files.h b/libide/ide-unsaved-files.h
index 7f99722..b9e5a58 100644
--- a/libide/ide-unsaved-files.h
+++ b/libide/ide-unsaved-files.h
@@ -32,27 +32,29 @@ struct _IdeUnsavedFiles
   IdeObject parent_instance;
 };
 
-void       ide_unsaved_files_update            (IdeUnsavedFiles      *self,
-                                                GFile                *file,
-                                                GBytes               *content);
-void       ide_unsaved_files_remove            (IdeUnsavedFiles      *self,
-                                                GFile                *file);
-void       ide_unsaved_files_save_async        (IdeUnsavedFiles      *files,
-                                                GCancellable         *cancellable,
-                                                GAsyncReadyCallback   callback,
-                                                gpointer              user_data);
-gboolean   ide_unsaved_files_save_finish       (IdeUnsavedFiles      *files,
-                                                GAsyncResult         *result,
-                                                GError              **error);
-void       ide_unsaved_files_restore_async     (IdeUnsavedFiles      *files,
-                                                GCancellable         *cancellable,
-                                                GAsyncReadyCallback   callback,
-                                                gpointer              user_data);
-gboolean   ide_unsaved_files_restore_finish    (IdeUnsavedFiles      *files,
-                                                GAsyncResult         *result,
-                                                GError              **error);
-GPtrArray *ide_unsaved_files_get_unsaved_files (IdeUnsavedFiles      *files);
-gint64     ide_unsaved_files_get_sequence      (IdeUnsavedFiles      *files);
+void            ide_unsaved_files_update            (IdeUnsavedFiles      *self,
+                                                     GFile                *file,
+                                                     GBytes               *content);
+void            ide_unsaved_files_remove            (IdeUnsavedFiles      *self,
+                                                     GFile                *file);
+void            ide_unsaved_files_save_async        (IdeUnsavedFiles      *files,
+                                                     GCancellable         *cancellable,
+                                                     GAsyncReadyCallback   callback,
+                                                     gpointer              user_data);
+gboolean        ide_unsaved_files_save_finish       (IdeUnsavedFiles      *files,
+                                                     GAsyncResult         *result,
+                                                     GError              **error);
+void            ide_unsaved_files_restore_async     (IdeUnsavedFiles      *files,
+                                                     GCancellable         *cancellable,
+                                                     GAsyncReadyCallback   callback,
+                                                     gpointer              user_data);
+gboolean        ide_unsaved_files_restore_finish    (IdeUnsavedFiles      *files,
+                                                     GAsyncResult         *result,
+                                                     GError              **error);
+GPtrArray      *ide_unsaved_files_get_unsaved_files (IdeUnsavedFiles      *files);
+gint64          ide_unsaved_files_get_sequence      (IdeUnsavedFiles      *files);
+IdeUnsavedFile *ide_unsaved_files_get_unsaved_file  (IdeUnsavedFiles      *self,
+                                                     GFile                *file);
 
 G_END_DECLS
 


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