[gedit/wip/reusable-code: 2/2] New property DocumentSaver:ensure-trailing-newline



commit 1e15e139fee48c4bdf5127f141e2e85ccc05a7c8
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Mon Sep 16 19:08:14 2013 +0200

    New property DocumentSaver:ensure-trailing-newline
    
    Remove the dependency to GSettings in DocumentSaver.
    
    The DocumentSaver has also the "flags" property. But
    GeditDocumentSaveFlags is a public API for gedit plugins. Here the
    modification is private.

 gedit/gedit-document-saver.c |   39 +++++++++++++++++++++++++--------------
 gedit/gedit-document-saver.h |    3 ++-
 gedit/gedit-document.c       |    8 +++++++-
 gedit/gedit-settings.h       |    1 +
 4 files changed, 35 insertions(+), 16 deletions(-)
---
diff --git a/gedit/gedit-document-saver.c b/gedit/gedit-document-saver.c
index 5e3cbbe..c493828 100644
--- a/gedit/gedit-document-saver.c
+++ b/gedit/gedit-document-saver.c
@@ -32,7 +32,6 @@
 #include "gedit-marshal.h"
 #include "gedit/gedit-utils.h"
 #include "gedit-enum-types.h"
-#include "gedit-settings.h"
 
 #define WRITE_CHUNK_SIZE 8192
 
@@ -60,7 +59,8 @@ enum {
        PROP_ENCODING,
        PROP_NEWLINE_TYPE,
        PROP_COMPRESSION_TYPE,
-       PROP_FLAGS
+       PROP_FLAGS,
+       PROP_ENSURE_TRAILING_NEWLINE
 };
 
 typedef struct
@@ -81,8 +81,6 @@ static void check_modified_async (AsyncData *async);
 
 struct _GeditDocumentSaverPrivate
 {
-       GSettings                *editor_settings;
-
        GFileInfo                *info;
        GeditDocument            *document;
 
@@ -105,6 +103,7 @@ struct _GeditDocumentSaverPrivate
        GError                   *error;
 
        guint                     used : 1;
+       guint                     ensure_trailing_newline : 1;
 };
 
 G_DEFINE_TYPE_WITH_PRIVATE (GeditDocumentSaver, gedit_document_saver, G_TYPE_OBJECT)
@@ -140,6 +139,9 @@ gedit_document_saver_set_property (GObject      *object,
                case PROP_FLAGS:
                        saver->priv->flags = g_value_get_flags (value);
                        break;
+               case PROP_ENSURE_TRAILING_NEWLINE:
+                       saver->priv->ensure_trailing_newline = g_value_get_boolean (value);
+                       break;
                default:
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                        break;
@@ -174,6 +176,9 @@ gedit_document_saver_get_property (GObject    *object,
                case PROP_FLAGS:
                        g_value_set_flags (value, saver->priv->flags);
                        break;
+               case PROP_ENSURE_TRAILING_NEWLINE:
+                       g_value_set_boolean (value, saver->priv->ensure_trailing_newline);
+                       break;
                default:
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                        break;
@@ -198,7 +203,6 @@ gedit_document_saver_dispose (GObject *object)
        g_clear_object (&priv->input);
        g_clear_object (&priv->info);
        g_clear_object (&priv->location);
-       g_clear_object (&priv->editor_settings);
 
        G_OBJECT_CLASS (gedit_document_saver_parent_class)->dispose (object);
 }
@@ -307,6 +311,17 @@ gedit_document_saver_class_init (GeditDocumentSaverClass *klass)
                                                             G_PARAM_READWRITE |
                                                             G_PARAM_CONSTRUCT_ONLY));
 
+       g_object_class_install_property (object_class,
+                                        PROP_ENSURE_TRAILING_NEWLINE,
+                                        g_param_spec_boolean ("ensure-trailing-newline",
+                                                              "Ensure Trailing Newline",
+                                                              "Ensure the document ends with a trailing 
newline",
+                                                              TRUE,
+                                                              G_PARAM_READWRITE |
+                                                              G_PARAM_STATIC_NAME |
+                                                              G_PARAM_STATIC_BLURB |
+                                                              G_PARAM_CONSTRUCT_ONLY));
+
        signals[SAVING] =
                g_signal_new ("saving",
                              G_OBJECT_CLASS_TYPE (object_class),
@@ -328,7 +343,6 @@ gedit_document_saver_init (GeditDocumentSaver *saver)
        saver->priv->cancellable = g_cancellable_new ();
        saver->priv->error = NULL;
        saver->priv->used = FALSE;
-       saver->priv->editor_settings = g_settings_new ("org.gnome.gedit.preferences.editor");
 }
 
 GeditDocumentSaver *
@@ -337,7 +351,8 @@ gedit_document_saver_new (GeditDocument                *doc,
                          const GeditEncoding          *encoding,
                          GeditDocumentNewlineType      newline_type,
                          GeditDocumentCompressionType  compression_type,
-                         GeditDocumentSaveFlags        flags)
+                         GeditDocumentSaveFlags        flags,
+                         gboolean                      ensure_trailing_newline)
 {
        g_return_val_if_fail (GEDIT_IS_DOCUMENT (doc), NULL);
 
@@ -351,6 +366,7 @@ gedit_document_saver_new (GeditDocument                *doc,
                                                   "newline_type", newline_type,
                                                   "compression_type", compression_type,
                                                   "flags", flags,
+                                                  "ensure-trailing-newline", ensure_trailing_newline,
                                                   NULL));
 }
 
@@ -718,7 +734,6 @@ async_replace_ready_callback (GFile        *source,
        GFileOutputStream *file_stream;
        GOutputStream *base_stream;
        GError *error = NULL;
-       gboolean ensure_trailing_newline;
 
        DEBUG ({
               g_print ("%s\n", G_STRFUNC);
@@ -790,13 +805,9 @@ async_replace_ready_callback (GFile        *source,
                saver->priv->stream = G_OUTPUT_STREAM (base_stream);
        }
 
-       ensure_trailing_newline = g_settings_get_boolean (saver->priv->editor_settings,
-                                                         "ensure-trailing-newline");
-
-
        saver->priv->input = gedit_document_input_stream_new (GTK_TEXT_BUFFER (saver->priv->document),
-                                                               saver->priv->newline_type,
-                                                               ensure_trailing_newline);
+                                                             saver->priv->newline_type,
+                                                             saver->priv->ensure_trailing_newline);
 
        saver->priv->size = gedit_document_input_stream_get_total_size (GEDIT_DOCUMENT_INPUT_STREAM 
(saver->priv->input));
 
diff --git a/gedit/gedit-document-saver.h b/gedit/gedit-document-saver.h
index 8564d77..8b559af 100644
--- a/gedit/gedit-document-saver.h
+++ b/gedit/gedit-document-saver.h
@@ -65,7 +65,8 @@ GeditDocumentSaver    *gedit_document_saver_new               (GeditDocument                
*d
                                                                 const GeditEncoding          *encoding,
                                                                 GeditDocumentNewlineType      newline_type,
                                                                 GeditDocumentCompressionType  
compression_type,
-                                                                GeditDocumentSaveFlags        flags);
+                                                                GeditDocumentSaveFlags        flags,
+                                                                gboolean                      
ensure_trailing_newline);
 
 void                    gedit_document_saver_saving            (GeditDocumentSaver  *saver,
                                                                 gboolean             completed,
diff --git a/gedit/gedit-document.c b/gedit/gedit-document.c
index f9e1b88..1226974 100644
--- a/gedit/gedit-document.c
+++ b/gedit/gedit-document.c
@@ -1815,6 +1815,8 @@ gedit_document_save_real (GeditDocument                *doc,
        }
        else
        {
+               gboolean ensure_trailing_newline;
+
                /* Do not make backups for remote files so they do not clutter
                 * remote systems. */
                if (!gedit_document_is_local (doc) ||
@@ -1824,13 +1826,17 @@ gedit_document_save_real (GeditDocument                *doc,
                        flags |= GEDIT_DOCUMENT_SAVE_IGNORE_BACKUP;
                }
 
+               ensure_trailing_newline = g_settings_get_boolean (doc->priv->editor_settings,
+                                                                 GEDIT_SETTINGS_ENSURE_TRAILING_NEWLINE);
+
                /* create a saver, it will be destroyed once saving is complete */
                doc->priv->saver = gedit_document_saver_new (doc,
                                                             location,
                                                             encoding,
                                                             newline_type,
                                                             compression_type,
-                                                            flags);
+                                                            flags,
+                                                            ensure_trailing_newline);
 
                g_signal_connect (doc->priv->saver,
                                  "saving",
diff --git a/gedit/gedit-settings.h b/gedit/gedit-settings.h
index b6067cc..c282e23 100644
--- a/gedit/gedit-settings.h
+++ b/gedit/gedit-settings.h
@@ -109,6 +109,7 @@ void                         gedit_settings_set_list                        (GSettings    
 *settings,
 #define GEDIT_SETTINGS_ENCODING_AUTO_DETECTED          "auto-detected"
 #define GEDIT_SETTINGS_ENCODING_SHOWN_IN_MENU          "shown-in-menu"
 #define GEDIT_SETTINGS_ACTIVE_PLUGINS                  "active-plugins"
+#define GEDIT_SETTINGS_ENSURE_TRAILING_NEWLINE         "ensure-trailing-newline"
 
 /* window state keys */
 #define GEDIT_SETTINGS_WINDOW_STATE                    "state"


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