[gnome-builder] GbDocument: add save and save_as vfuncs to document.



commit 2c12f72a0eb3f8f6666123b587ada1e02ad31c24
Author: Christian Hergert <christian hergert me>
Date:   Fri Dec 12 19:23:46 2014 -0800

    GbDocument: add save and save_as vfuncs to document.

 src/documents/gb-document-stack.c |    4 +-
 src/documents/gb-document.c       |   57 +++++++++++++++++++++++---
 src/documents/gb-document.h       |   67 +++++++++++++++++++++----------
 src/editor/gb-editor-document.c   |   80 ++++++++++++++++++++++++++++--------
 src/editor/gb-editor-document.h   |    7 ---
 5 files changed, 160 insertions(+), 55 deletions(-)
---
diff --git a/src/documents/gb-document-stack.c b/src/documents/gb-document-stack.c
index b8119ef..4b107bd 100644
--- a/src/documents/gb-document-stack.c
+++ b/src/documents/gb-document-stack.c
@@ -459,7 +459,7 @@ gb_document_stack_save_activate (GSimpleAction *action,
       if (document)
         {
           if (gb_document_get_modified (document))
-            gb_document_save (document);
+            gb_document_save_async (document, NULL, NULL, NULL);
         }
     }
 }
@@ -485,7 +485,7 @@ gb_document_stack_save_as_activate (GSimpleAction *action,
       document = gb_document_view_get_document (priv->active_view);
 
       if (document)
-        gb_document_save_as (document, toplevel);
+        gb_document_save_as_async (document, toplevel, NULL, NULL, NULL);
     }
 }
 
diff --git a/src/documents/gb-document.c b/src/documents/gb-document.c
index 013b117..1fb270d 100644
--- a/src/documents/gb-document.c
+++ b/src/documents/gb-document.c
@@ -119,22 +119,65 @@ gb_document_create_view (GbDocument *document)
 }
 
 void
-gb_document_save (GbDocument *document)
+gb_document_save_async (GbDocument          *document,
+                        GCancellable        *cancellable,
+                        GAsyncReadyCallback  callback,
+                        gpointer             user_data)
 {
   g_return_if_fail (GB_IS_DOCUMENT (document));
+  g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
 
-  if (GB_DOCUMENT_GET_INTERFACE (document)->save)
-    GB_DOCUMENT_GET_INTERFACE (document)->save (document);
+  if (GB_DOCUMENT_GET_INTERFACE (document)->save_async)
+    GB_DOCUMENT_GET_INTERFACE (document)->save_async (document,
+                                                      cancellable,
+                                                      callback,
+                                                      user_data);
+}
+
+gboolean
+gb_document_save_finish (GbDocument    *document,
+                         GAsyncResult  *result,
+                         GError       **error)
+{
+  g_return_if_fail (GB_IS_DOCUMENT (document));
+  g_return_if_fail (G_IS_ASYNC_RESULT (result));
+
+  if (GB_DOCUMENT_GET_INTERFACE (document)->save_finish)
+    return GB_DOCUMENT_GET_INTERFACE (document)->
+      save_finish (document, result, error);
+
+  return TRUE;
 }
 
 void
-gb_document_save_as (GbDocument *document,
-                     GtkWidget  *toplevel)
+gb_document_save_as_async (GbDocument          *document,
+                           GtkWidget           *toplevel,
+                           GCancellable        *cancellable,
+                           GAsyncReadyCallback  callback,
+                           gpointer             user_data)
 {
   g_return_if_fail (GB_IS_DOCUMENT (document));
+  g_return_if_fail (GTK_IS_WIDGET (toplevel));
+  g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  if (GB_DOCUMENT_GET_INTERFACE (document)->save_as_async)
+    GB_DOCUMENT_GET_INTERFACE (document)->
+      save_as_async (document, toplevel, cancellable, callback, user_data);
+}
+
+gboolean
+gb_document_save_as_finish (GbDocument    *document,
+                            GAsyncResult  *result,
+                            GError       **error)
+{
+  g_return_if_fail (GB_IS_DOCUMENT (document));
+  g_return_if_fail (G_IS_ASYNC_RESULT (result));
+
+  if (GB_DOCUMENT_GET_INTERFACE (document)->save_as_finish)
+    return GB_DOCUMENT_GET_INTERFACE (document)->
+      save_as_finish (document, result, error);
 
-  if (GB_DOCUMENT_GET_INTERFACE (document)->save_as)
-    GB_DOCUMENT_GET_INTERFACE (document)->save_as (document, toplevel);
+  return TRUE;
 }
 
 static void
diff --git a/src/documents/gb-document.h b/src/documents/gb-document.h
index 55886aa..9fe3834 100644
--- a/src/documents/gb-document.h
+++ b/src/documents/gb-document.h
@@ -35,29 +35,54 @@ struct _GbDocumentInterface
 {
   GTypeInterface parent;
 
-  GtkWidget   *(*create_view)   (GbDocument *document);
-  gboolean     (*get_modified)  (GbDocument *document);
-  gboolean     (*get_mtime)     (GbDocument *document,
-                                 GTimeVal   *mtime);
-  gboolean     (*get_read_only) (GbDocument *document);
-  const gchar *(*get_title)     (GbDocument *document);
-  gboolean     (*is_untitled)   (GbDocument *document);
-  void         (*save)          (GbDocument *document);
-  void         (*save_as)       (GbDocument *document,
-                                 GtkWidget  *toplevel);
+  GtkWidget   *(*create_view)           (GbDocument *document);
+  gboolean     (*get_modified)          (GbDocument *document);
+  gboolean     (*get_mtime)             (GbDocument *document,
+                                         GTimeVal   *mtime);
+  gboolean     (*get_read_only)         (GbDocument *document);
+  const gchar *(*get_title)             (GbDocument *document);
+  gboolean     (*is_untitled)           (GbDocument *document);
+  void         (*save_async)            (GbDocument           *document,
+                                         GCancellable         *cancellable,
+                                         GAsyncReadyCallback   callback,
+                                         gpointer              user_data);
+  gboolean     (*save_finish)           (GbDocument           *document,
+                                         GAsyncResult         *result,
+                                         GError              **error);
+  void         (*save_as_async)         (GbDocument           *document,
+                                         GtkWidget            *toplevel,
+                                         GCancellable         *cancellable,
+                                         GAsyncReadyCallback   callback,
+                                         gpointer              user_data);
+  gboolean     (*save_as_finish)        (GbDocument           *document,
+                                         GAsyncResult         *result,
+                                         GError              **error);
 };
 
-GtkWidget   *gb_document_create_view   (GbDocument *document);
-gboolean     gb_document_get_modified  (GbDocument *document);
-gboolean     gb_document_get_mtime     (GbDocument *document,
-                                        GTimeVal   *mtime);
-gboolean     gb_document_get_read_only (GbDocument *document);
-const gchar *gb_document_get_title     (GbDocument *document);
-GType        gb_document_get_type      (void);
-gboolean     gb_document_is_untitled   (GbDocument *document);
-void         gb_document_save          (GbDocument *document);
-void         gb_document_save_as       (GbDocument *document,
-                                        GtkWidget  *toplevel);
+GtkWidget   *gb_document_create_view    (GbDocument           *document);
+gboolean     gb_document_get_modified   (GbDocument           *document);
+gboolean     gb_document_get_mtime      (GbDocument           *document,
+                                         GTimeVal             *mtime);
+gboolean     gb_document_get_read_only  (GbDocument           *document);
+const gchar *gb_document_get_title      (GbDocument           *document);
+GType        gb_document_get_type       (void);
+gboolean     gb_document_is_untitled    (GbDocument           *document);
+void         gb_document_save_async     (GbDocument           *document,
+                                         GCancellable         *cancellable,
+                                         GAsyncReadyCallback   callback,
+                                         gpointer              user_data);
+gboolean     gb_document_save_finish    (GbDocument           *document,
+                                         GAsyncResult         *result,
+                                         GError              **error);
+void         gb_document_save_as_async  (GbDocument           *document,
+                                         GtkWidget            *toplevel,
+                                         GCancellable         *cancellable,
+                                         GAsyncReadyCallback   callback,
+                                         gpointer              user_data);
+gboolean     gb_document_save_as_finish (GbDocument           *document,
+                                         GAsyncResult         *result,
+                                         GError              **error);
+
 
 G_END_DECLS
 
diff --git a/src/editor/gb-editor-document.c b/src/editor/gb-editor-document.c
index 5ce984d..198131f 100644
--- a/src/editor/gb-editor-document.c
+++ b/src/editor/gb-editor-document.c
@@ -814,13 +814,14 @@ cleanup:
   EXIT;
 }
 
-void
-gb_editor_document_save_async (GbEditorDocument      *document,
-                               GCancellable          *cancellable,
-                               GAsyncReadyCallback    callback,
-                               gpointer               user_data)
+static void
+gb_editor_document_save_async (GbDocument          *doc,
+                               GCancellable        *cancellable,
+                               GAsyncReadyCallback  callback,
+                               gpointer             user_data)
 {
   GtkSourceFileSaver *saver;
+  GbEditorDocument *document = (GbEditorDocument *)doc;
   GbEditorFileMarks *marks;
   GbEditorFileMark *mark;
   GFile *location;
@@ -879,10 +880,10 @@ gb_editor_document_save_async (GbEditorDocument      *document,
   EXIT;
 }
 
-gboolean
-gb_editor_document_save_finish (GbEditorDocument  *document,
-                                GAsyncResult      *result,
-                                GError           **error)
+static gboolean
+gb_editor_document_save_finish (GbDocument    *document,
+                                GAsyncResult  *result,
+                                GError       **error)
 {
   GTask *task = (GTask *)result;
 
@@ -893,23 +894,39 @@ gb_editor_document_save_finish (GbEditorDocument  *document,
 }
 
 static void
-gb_editor_document_save (GbDocument *document)
+gb_editor_document_save_as_cb (GObject      *object,
+                               GAsyncResult *result,
+                               gpointer      user_data)
 {
-  GbEditorDocument *self = (GbEditorDocument *)document;
+  GbDocument *document;
+  GTask *task = user_data;
+  GError *error = NULL;
 
-  g_return_if_fail (GB_IS_EDITOR_DOCUMENT (self));
+  g_return_if_fail (G_IS_ASYNC_RESULT (result));
+  g_return_if_fail (G_IS_TASK (task));
+
+  document = g_task_get_source_object (task);
+
+  if (!gb_editor_document_save_finish (document, result, &error))
+    g_task_return_error (task, error);
+  else
+    g_task_return_boolean (task, TRUE);
 
-  gb_editor_document_save_async (self, NULL, NULL, NULL);
+  g_object_unref (task);
 }
 
 static void
-gb_editor_document_save_as (GbDocument *document,
-                            GtkWidget  *toplevel)
+gb_editor_document_save_as_async (GbDocument          *document,
+                                  GtkWidget           *toplevel,
+                                  GCancellable        *cancellable,
+                                  GAsyncReadyCallback  callback,
+                                  gpointer             user_data)
 {
   GbEditorDocument *self = (GbEditorDocument *)document;
   const gchar *title;
   GtkDialog *dialog;
   GtkWidget *suggested;
+  GTask *task;
   GFile *chosen_file;
   guint response;
 
@@ -917,6 +934,8 @@ gb_editor_document_save_as (GbDocument *document,
 
   g_return_if_fail (GB_IS_EDITOR_DOCUMENT (self));
 
+  task = g_task_new (document, cancellable, callback, user_data);
+
   dialog = g_object_new (GTK_TYPE_FILE_CHOOSER_DIALOG,
                          "action", GTK_FILE_CHOOSER_ACTION_SAVE,
                          "do-overwrite-confirmation", TRUE,
@@ -949,15 +968,38 @@ gb_editor_document_save_as (GbDocument *document,
     {
       chosen_file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
       gtk_source_file_set_location (self->priv->file, chosen_file);
-      gb_editor_document_save_async (self, NULL, NULL, NULL);
+      gb_editor_document_save_async (GB_DOCUMENT (self),
+                                     cancellable,
+                                     gb_editor_document_save_as_cb,
+                                     task);
       g_clear_object (&chosen_file);
     }
+  else
+    {
+      g_task_return_new_error (task,
+                               G_IO_ERROR,
+                               G_IO_ERROR_NOT_FOUND,
+                               _("No file was selected for saving."));
+    }
 
   gtk_widget_destroy (GTK_WIDGET (dialog));
 
   EXIT;
 }
 
+static gboolean
+gb_editor_document_save_as_finish (GbDocument    *document,
+                                   GAsyncResult  *result,
+                                   GError       **error)
+{
+  GTask *task = (GTask *)result;
+
+  g_return_val_if_fail (GB_IS_EDITOR_DOCUMENT (document), FALSE);
+  g_return_val_if_fail (G_IS_TASK (task), FALSE);
+
+  return g_task_propagate_boolean (task, error);
+}
+
 static void
 gb_editor_document_restore_insert (GbEditorDocument *document)
 {
@@ -1458,6 +1500,8 @@ gb_editor_document_init_document (GbDocumentInterface *iface)
   iface->get_title = gb_editor_document_get_title;
   iface->is_untitled = gb_editor_document_is_untitled;
   iface->create_view = gb_editor_document_create_view;
-  iface->save = gb_editor_document_save;
-  iface->save_as = gb_editor_document_save_as;
+  iface->save_async = gb_editor_document_save_async;
+  iface->save_finish = gb_editor_document_save_finish;
+  iface->save_as_async = gb_editor_document_save_as_async;
+  iface->save_as_finish = gb_editor_document_save_as_finish;
 }
diff --git a/src/editor/gb-editor-document.h b/src/editor/gb-editor-document.h
index f4b4571..dd3eccf 100644
--- a/src/editor/gb-editor-document.h
+++ b/src/editor/gb-editor-document.h
@@ -75,13 +75,6 @@ void                   gb_editor_document_load_async                   (GbEditor
 gboolean               gb_editor_document_load_finish                  (GbEditorDocument       *document,
                                                                         GAsyncResult           *result,
                                                                         GError                **error);
-void                   gb_editor_document_save_async                   (GbEditorDocument       *document,
-                                                                        GCancellable           *cancellable,
-                                                                        GAsyncReadyCallback     callback,
-                                                                        gpointer                user_data);
-gboolean               gb_editor_document_save_finish                  (GbEditorDocument       *document,
-                                                                        GAsyncResult           *result,
-                                                                        GError                **error);
 void                   gb_editor_document_reformat                     (GbEditorDocument       *document);
 void                   gb_editor_document_check_externally_modified    (GbEditorDocument       *document);
 void                   gb_editor_document_reload                       (GbEditorDocument       *document);


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