[gnome-builder/wip/libide: 196/237] libide: add helpers to persist unsaved files to temporary files



commit 5d0a7a60d5581934b4f0989b9172039a86b8f7fa
Author: Christian Hergert <christian hergert me>
Date:   Sun Feb 15 00:17:25 2015 -0800

    libide: add helpers to persist unsaved files to temporary files

 libide/ide-internal.h     |    7 ++++---
 libide/ide-unsaved-file.c |   36 +++++++++++++++++++++++++++++++++---
 libide/ide-unsaved-file.h |   16 ++++++++++------
 3 files changed, 47 insertions(+), 12 deletions(-)
---
diff --git a/libide/ide-internal.h b/libide/ide-internal.h
index 9582a93..4a70e33 100644
--- a/libide/ide-internal.h
+++ b/libide/ide-internal.h
@@ -30,9 +30,10 @@ G_BEGIN_DECLS
 void _ide_project_set_name (IdeProject  *project,
                             const gchar *name);
 
-IdeUnsavedFile *_ide_unsaved_file_new (GFile  *file,
-                                       GBytes *content,
-                                       gint64  sequence);
+IdeUnsavedFile *_ide_unsaved_file_new (GFile       *file,
+                                       GBytes      *content,
+                                       const gchar *temp_path,
+                                       gint64       sequence);
 
 IdeClangTranslationUnit *_ide_clang_translation_unit_new (IdeContext        *contxt,
                                                           CXTranslationUnit  tu,
diff --git a/libide/ide-unsaved-file.c b/libide/ide-unsaved-file.c
index b2c34cd..260909f 100644
--- a/libide/ide-unsaved-file.c
+++ b/libide/ide-unsaved-file.c
@@ -26,13 +26,15 @@ struct _IdeUnsavedFile
   volatile gint  ref_count;
   GBytes        *content;
   GFile         *file;
+  gchar         *temp_path;
   gint64         sequence;
 };
 
 IdeUnsavedFile *
-_ide_unsaved_file_new (GFile  *file,
-                       GBytes *content,
-                       gint64  sequence)
+_ide_unsaved_file_new (GFile       *file,
+                       GBytes      *content,
+                       const gchar *temp_path,
+                       gint64       sequence)
 {
   IdeUnsavedFile *ret;
 
@@ -44,10 +46,37 @@ _ide_unsaved_file_new (GFile  *file,
   ret->file = g_object_ref (file);
   ret->content = g_bytes_ref (content);
   ret->sequence = sequence;
+  ret->temp_path = g_strdup (temp_path);
 
   return ret;
 }
 
+const gchar *
+ide_unsaved_file_get_temp_path (IdeUnsavedFile *self)
+{
+  g_return_val_if_fail (self, NULL);
+
+  return self->temp_path;
+}
+
+gboolean
+ide_unsaved_file_persist (IdeUnsavedFile  *self,
+                          GCancellable    *cancellable,
+                          GError         **error)
+{
+  g_return_val_if_fail (self, FALSE);
+  g_return_val_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable), FALSE);
+
+  /*
+   * TODO: Support cancellable.
+   */
+
+  return g_file_set_contents (self->temp_path,
+                              g_bytes_get_data (self->content, NULL),
+                              g_bytes_get_size (self->content),
+                              error);
+}
+
 gint64
 ide_unsaved_file_get_sequence (IdeUnsavedFile *self)
 {
@@ -75,6 +104,7 @@ ide_unsaved_file_unref (IdeUnsavedFile *self)
 
   if (g_atomic_int_dec_and_test (&self->ref_count))
     {
+      g_clear_pointer (&self->temp_path, g_free);
       g_clear_pointer (&self->content, g_bytes_unref);
       g_clear_object (&self->file);
       g_slice_free (IdeUnsavedFile, self);
diff --git a/libide/ide-unsaved-file.h b/libide/ide-unsaved-file.h
index 9593a96..a46e2f9 100644
--- a/libide/ide-unsaved-file.h
+++ b/libide/ide-unsaved-file.h
@@ -25,12 +25,16 @@
 
 G_BEGIN_DECLS
 
-GType           ide_unsaved_file_get_type     (void);
-IdeUnsavedFile *ide_unsaved_file_ref          (IdeUnsavedFile *self);
-void            ide_unsaved_file_unref        (IdeUnsavedFile *self);
-GBytes         *ide_unsaved_file_get_content  (IdeUnsavedFile *self);
-GFile          *ide_unsaved_file_get_file     (IdeUnsavedFile *self);
-gint64          ide_unsaved_file_get_sequence (IdeUnsavedFile *file);
+GType           ide_unsaved_file_get_type      (void);
+IdeUnsavedFile *ide_unsaved_file_ref           (IdeUnsavedFile  *self);
+void            ide_unsaved_file_unref         (IdeUnsavedFile  *self);
+GBytes         *ide_unsaved_file_get_content   (IdeUnsavedFile  *self);
+GFile          *ide_unsaved_file_get_file      (IdeUnsavedFile  *self);
+gint64          ide_unsaved_file_get_sequence  (IdeUnsavedFile  *self);
+const gchar    *ide_unsaved_file_get_temp_path (IdeUnsavedFile  *self);
+gboolean        ide_unsaved_file_persist       (IdeUnsavedFile  *self,
+                                                GCancellable    *cancellable,
+                                                GError         **error);
 
 G_END_DECLS
 


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