[gtksourceview/wip/loader-saver: 13/28] FileSaver: add setting: ignore invalid chars



commit 153d7ec23a188c91e4406f7b06a7510b3ead0af9
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Mon Jun 9 18:28:07 2014 +0200

    FileSaver: add setting: ignore invalid chars

 docs/reference/gtksourceview-3.0-sections.txt |    6 ++
 gtksourceview/gtksourcebuffer-private.h       |    3 +
 gtksourceview/gtksourcebuffer.c               |   23 ++++++
 gtksourceview/gtksourcefileloader.h           |    3 +-
 gtksourceview/gtksourcefilesaver.c            |   95 ++++++++++++++++++++++++-
 gtksourceview/gtksourcefilesaver.h            |   23 ++++++
 6 files changed, 149 insertions(+), 4 deletions(-)
---
diff --git a/docs/reference/gtksourceview-3.0-sections.txt b/docs/reference/gtksourceview-3.0-sections.txt
index 883a6f4..74ac019 100644
--- a/docs/reference/gtksourceview-3.0-sections.txt
+++ b/docs/reference/gtksourceview-3.0-sections.txt
@@ -290,6 +290,9 @@ gtk_source_file_loader_error_quark
 <FILE>filesaver</FILE>
 <TITLE>GtkSourceFileSaver</TITLE>
 GtkSourceFileSaver
+GTK_SOURCE_FILE_SAVER_ERROR
+GtkSourceFileSaverError
+<SUBSECTION>
 gtk_source_file_saver_new
 gtk_source_file_saver_get_file
 gtk_source_file_saver_get_location
@@ -301,6 +304,8 @@ gtk_source_file_saver_set_compression_type
 gtk_source_file_saver_get_compression_type
 gtk_source_file_saver_set_create_backup
 gtk_source_file_saver_get_create_backup
+gtk_source_file_saver_set_ignore_invalid_chars
+gtk_source_file_saver_get_ignore_invalid_chars
 gtk_source_file_saver_save_async
 gtk_source_file_saver_save_finish
 <SUBSECTION Standard>
@@ -313,6 +318,7 @@ GTK_SOURCE_TYPE_FILE_SAVER
 GtkSourceFileSaverClass
 GtkSourceFileSaverPrivate
 gtk_source_file_saver_get_type
+gtk_source_file_saver_error_quark
 </SECTION>
 
 <SECTION>
diff --git a/gtksourceview/gtksourcebuffer-private.h b/gtksourceview/gtksourcebuffer-private.h
index f24df08..9f1dae5 100644
--- a/gtksourceview/gtksourcebuffer-private.h
+++ b/gtksourceview/gtksourcebuffer-private.h
@@ -55,6 +55,9 @@ void                   _gtk_source_buffer_set_as_invalid_character    (GtkSourceBuffer      
  *buf
                                                                         const GtkTextIter      *start,
                                                                         const GtkTextIter      *end);
 
+G_GNUC_INTERNAL
+gboolean                _gtk_source_buffer_has_invalid_chars           (GtkSourceBuffer        *buffer);
+
 G_END_DECLS
 
 #endif /* __GTK_SOURCE_BUFFER_PRIVATE_H__ */
diff --git a/gtksourceview/gtksourcebuffer.c b/gtksourceview/gtksourcebuffer.c
index 3435351..c0bfc4c 100644
--- a/gtksourceview/gtksourcebuffer.c
+++ b/gtksourceview/gtksourcebuffer.c
@@ -2570,6 +2570,29 @@ _gtk_source_buffer_set_as_invalid_character (GtkSourceBuffer   *buffer,
                                   end);
 }
 
+gboolean
+_gtk_source_buffer_has_invalid_chars (GtkSourceBuffer *buffer)
+{
+       GtkTextIter start;
+
+       g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), FALSE);
+
+       if (buffer->priv->invalid_char_tag == NULL)
+       {
+               return FALSE;
+       }
+
+       gtk_text_buffer_get_start_iter (GTK_TEXT_BUFFER (buffer), &start);
+
+       if (gtk_text_iter_begins_tag (&start, buffer->priv->invalid_char_tag) ||
+           gtk_text_iter_forward_to_tag_toggle (&start, buffer->priv->invalid_char_tag))
+       {
+               return TRUE;
+       }
+
+       return FALSE;
+}
+
 /**
  * gtk_source_buffer_set_implicit_trailing_newline:
  * @buffer: a #GtkSourceBuffer.
diff --git a/gtksourceview/gtksourcefileloader.h b/gtksourceview/gtksourcefileloader.h
index 7bc8f4a..5526e70 100644
--- a/gtksourceview/gtksourcefileloader.h
+++ b/gtksourceview/gtksourcefileloader.h
@@ -51,8 +51,7 @@ typedef struct _GtkSourceFileLoaderPrivate GtkSourceFileLoaderPrivate;
  * @GTK_SOURCE_FILE_LOADER_ERROR_CONVERSION_FALLBACK: There was an encoding
  * conversion error and it was needed to use a fallback character.
  *
- * An error code used with %GTK_SOURCE_FILE_LOADER_ERROR in a #GError returned
- * from a file loading related function.
+ * An error code used with the %GTK_SOURCE_FILE_LOADER_ERROR domain.
  */
 typedef enum
 {
diff --git a/gtksourceview/gtksourcefilesaver.c b/gtksourceview/gtksourcefilesaver.c
index c8d1820..4f814c0 100644
--- a/gtksourceview/gtksourcefilesaver.c
+++ b/gtksourceview/gtksourcefilesaver.c
@@ -26,6 +26,7 @@
 #include "gtksourcefile.h"
 #include "gtksourcebufferinputstream.h"
 #include "gtksourceencoding.h"
+#include "gtksourcebuffer-private.h"
 #include "gtksourceview-typebuiltins.h"
 #include "gtksourceview-i18n.h"
 
@@ -63,7 +64,8 @@ enum
        PROP_ENCODING,
        PROP_NEWLINE_TYPE,
        PROP_COMPRESSION_TYPE,
-       PROP_CREATE_BACKUP
+       PROP_CREATE_BACKUP,
+       PROP_IGNORE_INVALID_CHARS
 };
 
 struct _GtkSourceFileSaverPrivate
@@ -109,6 +111,7 @@ struct _GtkSourceFileSaverPrivate
        GFileInfo *info;
 
        guint create_backup : 1;
+       guint ignore_invalid_chars : 1;
        guint tried_mount : 1;
 };
 
@@ -156,6 +159,10 @@ gtk_source_file_saver_set_property (GObject      *object,
                        gtk_source_file_saver_set_create_backup (saver, g_value_get_boolean (value));
                        break;
 
+               case PROP_IGNORE_INVALID_CHARS:
+                       gtk_source_file_saver_set_ignore_invalid_chars (saver, g_value_get_boolean (value));
+                       break;
+
                default:
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                        break;
@@ -196,6 +203,10 @@ gtk_source_file_saver_get_property (GObject    *object,
                        g_value_set_boolean (value, saver->priv->create_backup);
                        break;
 
+               case PROP_IGNORE_INVALID_CHARS:
+                       g_value_set_boolean (value, saver->priv->ignore_invalid_chars);
+                       break;
+
                default:
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                        break;
@@ -370,6 +381,25 @@ gtk_source_file_saver_class_init (GtkSourceFileSaverClass *klass)
                                                               G_PARAM_READWRITE |
                                                               G_PARAM_CONSTRUCT |
                                                               G_PARAM_STATIC_STRINGS));
+
+       /**
+        * GtkSourceFileSaver:ignore-invalid-chars:
+        *
+        * Whether to ignore invalid characters. If this property is %FALSE and
+        * if the buffer contains invalid characters, an error will be returned
+        * when attempting to save the file.
+        *
+        * Since: 3.14
+        */
+       g_object_class_install_property (object_class,
+                                        PROP_IGNORE_INVALID_CHARS,
+                                        g_param_spec_boolean ("ignore-invalid-chars",
+                                                              _("Ignore invalid characters"),
+                                                              "",
+                                                              FALSE,
+                                                              G_PARAM_READWRITE |
+                                                              G_PARAM_CONSTRUCT |
+                                                              G_PARAM_STATIC_STRINGS));
 }
 
 static void
@@ -454,7 +484,7 @@ query_info_cb (GFile              *file,
               g_print ("Finished query info on file\n");
        });
 
-       /* TODO update mtime stored in GtkSourceBuffer */
+       /* TODO update mtime stored in GtkSourceFile */
        g_clear_object (&saver->priv->info);
        saver->priv->info = g_file_query_info_finish (file, result, &error);
 
@@ -806,6 +836,19 @@ recover_not_mounted (GtkSourceFileSaver *saver)
        g_object_unref (mount_operation);
 }
 
+GQuark
+gtk_source_file_saver_error_quark (void)
+{
+       static GQuark quark = 0;
+
+       if (G_UNLIKELY (quark == 0))
+       {
+               quark = g_quark_from_static_string ("gtk-source-file-saver-error");
+       }
+
+       return quark;
+}
+
 /**
  * gtk_source_file_saver_new:
  * @file: the #GtkSourceFile.
@@ -1019,6 +1062,44 @@ gtk_source_file_saver_get_create_backup (GtkSourceFileSaver *saver)
 }
 
 /**
+ * gtk_source_file_saver_set_ignore_invalid_chars:
+ * @saver: a #GtkSourceFileSaver.
+ * @ignore_invalid_chars: whether to ignore invalid characters.
+ *
+ * Since: 3.14
+ */
+void
+gtk_source_file_saver_set_ignore_invalid_chars (GtkSourceFileSaver *saver,
+                                               gboolean            ignore_invalid_chars)
+{
+       g_return_if_fail (GTK_SOURCE_IS_FILE_SAVER (saver));
+       g_return_if_fail (saver->priv->task == NULL);
+
+       ignore_invalid_chars = ignore_invalid_chars != FALSE;
+
+       if (saver->priv->ignore_invalid_chars != ignore_invalid_chars)
+       {
+               saver->priv->ignore_invalid_chars = ignore_invalid_chars;
+               g_object_notify (G_OBJECT (saver), "ignore-invalid-chars");
+       }
+}
+
+/**
+ * gtk_source_file_saver_get_ignore_invalid_chars:
+ * @saver: a #GtkSourceFileSaver.
+ *
+ * Returns: whether to ignore invalid characters.
+ * Since: 3.14
+ */
+gboolean
+gtk_source_file_saver_get_ignore_invalid_chars (GtkSourceFileSaver *saver)
+{
+       g_return_val_if_fail (GTK_SOURCE_IS_FILE_SAVER (saver), FALSE);
+
+       return saver->priv->ignore_invalid_chars;
+}
+
+/**
  * gtk_source_file_saver_save_async:
  * @saver: a #GtkSourceFileSaver.
  * @io_priority: the I/O priority of the request. E.g. %G_PRIORITY_LOW,
@@ -1071,6 +1152,16 @@ gtk_source_file_saver_save_async (GtkSourceFileSaver     *saver,
        saver->priv->progress_cb = progress_callback;
        saver->priv->progress_cb_data = progress_callback_data;
 
+       if (!saver->priv->ignore_invalid_chars &&
+           _gtk_source_buffer_has_invalid_chars (buffer))
+       {
+               g_task_return_new_error (saver->priv->task,
+                                        GTK_SOURCE_FILE_SAVER_ERROR,
+                                        GTK_SOURCE_FILE_SAVER_ERROR_INVALID_CHARS,
+                                        "The buffer contains invalid characters");
+               return;
+       }
+
        DEBUG ({
               g_print ("Start saving\n");
        });
diff --git a/gtksourceview/gtksourcefilesaver.h b/gtksourceview/gtksourcefilesaver.h
index b2b1d19..0a65ff5 100644
--- a/gtksourceview/gtksourcefilesaver.h
+++ b/gtksourceview/gtksourcefilesaver.h
@@ -41,6 +41,20 @@ G_BEGIN_DECLS
 typedef struct _GtkSourceFileSaverClass   GtkSourceFileSaverClass;
 typedef struct _GtkSourceFileSaverPrivate GtkSourceFileSaverPrivate;
 
+#define GTK_SOURCE_FILE_SAVER_ERROR gtk_source_file_saver_error_quark ()
+
+/**
+ * GtkSourceFileSaverError:
+ * @GTK_SOURCE_FILE_SAVER_ERROR_INVALID_CHARS: The buffer contains invalid
+ *   characters.
+ *
+ * An error code used with the %GTK_SOURCE_FILE_SAVER_ERROR domain.
+ */
+typedef enum
+{
+       GTK_SOURCE_FILE_SAVER_ERROR_INVALID_CHARS
+} GtkSourceFileSaverError;
+
 struct _GtkSourceFileSaver
 {
        GObject object;
@@ -55,6 +69,8 @@ struct _GtkSourceFileSaverClass
 
 GType                   gtk_source_file_saver_get_type         (void) G_GNUC_CONST;
 
+GQuark                  gtk_source_file_saver_error_quark      (void);
+
 GtkSourceFileSaver     *gtk_source_file_saver_new              (GtkSourceFile            *file,
                                                                 GFile                    *location);
 
@@ -86,6 +102,13 @@ void                         gtk_source_file_saver_set_create_backup
 gboolean                gtk_source_file_saver_get_create_backup
                                                                (GtkSourceFileSaver       *saver);
 
+void                    gtk_source_file_saver_set_ignore_invalid_chars
+                                                               (GtkSourceFileSaver       *saver,
+                                                                gboolean                  
ignore_invalid_chars);
+
+gboolean                gtk_source_file_saver_get_ignore_invalid_chars
+                                                               (GtkSourceFileSaver       *saver);
+
 void                    gtk_source_file_saver_save_async       (GtkSourceFileSaver       *saver,
                                                                 gint                      io_priority,
                                                                 GCancellable             *cancellable,


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