[gtksourceview/wip/loader-saver] FileSaver: do not depend on GtkSourceFile
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtksourceview/wip/loader-saver] FileSaver: do not depend on GtkSourceFile
- Date: Mon, 24 Mar 2014 20:08:46 +0000 (UTC)
commit be269522751e0f6afd2fcb2d66c2c7e5c04da8d8
Author: Sébastien Wilmet <swilmet gnome org>
Date: Fri Mar 14 18:55:41 2014 +0100
FileSaver: do not depend on GtkSourceFile
gtksourceview/gtksourcefilesaver.c | 234 ++++++++++++++++++++++--------------
gtksourceview/gtksourcefilesaver.h | 26 +----
2 files changed, 145 insertions(+), 115 deletions(-)
---
diff --git a/gtksourceview/gtksourcefilesaver.c b/gtksourceview/gtksourcefilesaver.c
index 73cf2b0..9f4fce3 100644
--- a/gtksourceview/gtksourcefilesaver.c
+++ b/gtksourceview/gtksourcefilesaver.c
@@ -26,15 +26,13 @@
* It uses a GtkSourceBufferInputStream as input, create converter(s) if needed
* for the encoding and the compression, and write the contents to a
* GOutputStream (the file).
- * The FileSaver has properties for all the settings relevant for saving a file.
- * It can not rely on a GtkSourceFile, because its properties can change during
- * the saving.
*/
#include "gtksourcefilesaver.h"
#include "gtksourcebufferinputstream.h"
#include "gtksourceencoding.h"
#include "gtksourceview-typebuiltins.h"
+#include "gtksourceview-i18n.h"
#if 0
#define DEBUG(x) (x)
@@ -49,23 +47,28 @@
enum
{
PROP_0,
+ PROP_BUFFER,
PROP_FILE,
PROP_ENCODING,
PROP_NEWLINE_TYPE,
PROP_COMPRESSION_TYPE,
PROP_ENSURE_TRAILING_NEWLINE,
- PROP_FLAGS
+ PROP_CREATE_BACKUP
};
struct _GtkSourceFileSaverPrivate
{
- GtkSourceFile *file;
+ /* Weak reference to the source_buffer. A subclass of GtkSourceBuffer
+ * can have the ownership of the FileSaver. And a FileSaver can be used
+ * several times, so the object can be kept around until the subclass of
+ * GtkSourceBuffer is disposed.
+ */
+ GtkSourceBuffer *source_buffer;
+ GFile *file;
+
const GtkSourceEncoding *encoding;
GtkSourceNewlineType newline_type;
GtkSourceCompressionType compression_type;
- GtkSourceFileSaveFlags flags;
-
- GTimeVal old_mtime;
GTask *task;
@@ -93,6 +96,7 @@ struct _GtkSourceFileSaverPrivate
GFileInfo *info;
guint ensure_trailing_newline : 1;
+ guint create_backup : 1;
guint tried_mount : 1;
};
@@ -112,16 +116,23 @@ gtk_source_file_saver_set_property (GObject *object,
switch (prop_id)
{
+ case PROP_BUFFER:
+ g_assert (saver->priv->source_buffer == NULL);
+ saver->priv->source_buffer = g_value_get_object (value);
+ g_object_add_weak_pointer (G_OBJECT (saver->priv->source_buffer),
+ (gpointer *)&saver->priv->source_buffer);
+ break;
+
case PROP_FILE:
g_assert (saver->priv->file == NULL);
- saver->priv->file = g_value_get_object (value);
+ saver->priv->file = g_value_dup_object (value);
break;
case PROP_ENCODING:
- g_assert (saver->priv->encoding == NULL);
saver->priv->encoding = g_value_get_boxed (value);
if (saver->priv->encoding == NULL)
{
+ /* FIXME ok? */
saver->priv->encoding = gtk_source_encoding_get_utf8 ();
}
break;
@@ -138,8 +149,8 @@ gtk_source_file_saver_set_property (GObject *object,
saver->priv->ensure_trailing_newline = g_value_get_boolean (value);
break;
- case PROP_FLAGS:
- saver->priv->flags = g_value_get_flags (value);
+ case PROP_CREATE_BACKUP:
+ saver->priv->create_backup = g_value_get_boolean (value);
break;
default:
@@ -150,14 +161,18 @@ gtk_source_file_saver_set_property (GObject *object,
static void
gtk_source_file_saver_get_property (GObject *object,
- guint prop_id,
- GValue *value,
- GParamSpec *pspec)
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
{
GtkSourceFileSaver *saver = GTK_SOURCE_FILE_SAVER (object);
switch (prop_id)
{
+ case PROP_BUFFER:
+ g_value_set_object (value, saver->priv->source_buffer);
+ break;
+
case PROP_FILE:
g_value_set_object (value, saver->priv->file);
break;
@@ -178,8 +193,8 @@ gtk_source_file_saver_get_property (GObject *object,
g_value_set_boolean (value, saver->priv->ensure_trailing_newline);
break;
- case PROP_FLAGS:
- g_value_set_flags (value, saver->priv->flags);
+ case PROP_CREATE_BACKUP:
+ g_value_set_boolean (value, saver->priv->create_backup);
break;
default:
@@ -193,14 +208,21 @@ gtk_source_file_saver_dispose (GObject *object)
{
GtkSourceFileSaverPrivate *priv = GTK_SOURCE_FILE_SAVER (object)->priv;
+ if (priv->source_buffer != NULL)
+ {
+ g_object_remove_weak_pointer (G_OBJECT (priv->source_buffer),
+ (gpointer *)&priv->source_buffer);
+
+ priv->source_buffer = NULL;
+ }
+
+ g_clear_object (&priv->file);
g_clear_object (&priv->output_stream);
g_clear_object (&priv->input_stream);
g_clear_object (&priv->task);
g_clear_object (&priv->info);
g_clear_error (&priv->error);
- priv->file = NULL;
-
G_OBJECT_CLASS (gtk_source_file_saver_parent_class)->dispose (object);
}
@@ -213,68 +235,126 @@ gtk_source_file_saver_class_init (GtkSourceFileSaverClass *klass)
object_class->set_property = gtk_source_file_saver_set_property;
object_class->get_property = gtk_source_file_saver_get_property;
+ /**
+ * GtkSourceFileSaver:buffer:
+ *
+ * The #GtkSourceBuffer to save.
+ *
+ * Since: 3.14
+ */
+ g_object_class_install_property (object_class,
+ PROP_BUFFER,
+ g_param_spec_object ("buffer",
+ "GtkSourceBuffer",
+ NULL,
+ GTK_SOURCE_TYPE_BUFFER,
+ G_PARAM_READWRITE |
+ G_PARAM_CONSTRUCT_ONLY |
+ G_PARAM_STATIC_STRINGS));
+
+ /**
+ * GtkSourceFileSaver:file:
+ *
+ * The #GFile where to save the buffer.
+ *
+ * Since: 3.14
+ */
g_object_class_install_property (object_class,
PROP_FILE,
g_param_spec_object ("file",
- "File",
- "The associated GtkSourceFile",
- GTK_SOURCE_TYPE_FILE,
+ "GFile",
+ NULL,
+ G_TYPE_FILE,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
+ /**
+ * GtkSourceFileSaver:encoding:
+ *
+ * The file's encoding.
+ *
+ * Since: 3.14
+ */
g_object_class_install_property (object_class,
PROP_ENCODING,
g_param_spec_boxed ("encoding",
- "Encoding",
- "The encoding of the saved file",
+ "GtkSourceEncoding",
+ NULL,
GTK_SOURCE_TYPE_ENCODING,
G_PARAM_READWRITE |
- G_PARAM_CONSTRUCT_ONLY |
+ G_PARAM_CONSTRUCT |
G_PARAM_STATIC_STRINGS));
+ /**
+ * GtkSourceFileSaver:newline-type:
+ *
+ * The newline type.
+ *
+ * Since: 3.14
+ */
g_object_class_install_property (object_class,
PROP_NEWLINE_TYPE,
g_param_spec_enum ("newline-type",
- "Newline type",
- "The type of line ending",
+ _("Newline Type"),
+ NULL,
GTK_SOURCE_TYPE_NEWLINE_TYPE,
GTK_SOURCE_NEWLINE_TYPE_LF,
G_PARAM_READWRITE |
- G_PARAM_CONSTRUCT_ONLY |
+ G_PARAM_CONSTRUCT |
G_PARAM_STATIC_STRINGS));
+ /**
+ * GtkSourceFileSaver:compression-type:
+ *
+ * The compression type.
+ *
+ * Since: 3.14
+ */
g_object_class_install_property (object_class,
PROP_COMPRESSION_TYPE,
g_param_spec_enum ("compression-type",
- "Compression type",
- "The compression type",
+ _("Compression Type"),
+ NULL,
GTK_SOURCE_TYPE_COMPRESSION_TYPE,
GTK_SOURCE_COMPRESSION_TYPE_NONE,
G_PARAM_READWRITE |
- G_PARAM_CONSTRUCT_ONLY |
+ G_PARAM_CONSTRUCT |
G_PARAM_STATIC_STRINGS));
+ /**
+ * GtkSourceFileSaver:ensure-trailing-newline:
+ *
+ * Ensure the file ends with a trailing newline.
+ *
+ * Since: 3.14
+ */
g_object_class_install_property (object_class,
PROP_ENSURE_TRAILING_NEWLINE,
g_param_spec_boolean ("ensure-trailing-newline",
- "Ensure Trailing Newline",
- "Ensure the buffer ends with a trailing
newline",
+ _("Ensure Trailing Newline"),
+ NULL,
TRUE,
G_PARAM_READWRITE |
- G_PARAM_CONSTRUCT_ONLY |
+ G_PARAM_CONSTRUCT |
G_PARAM_STATIC_STRINGS));
+ /**
+ * GtkSourceFileSaver:create-backup:
+ *
+ * Create a backup before saving the file.
+ *
+ * Since: 3.14
+ */
g_object_class_install_property (object_class,
- PROP_FLAGS,
- g_param_spec_flags ("flags",
- "Flags",
- "The flags for the saving operation",
- GTK_SOURCE_TYPE_FILE_SAVE_FLAGS,
- 0,
- G_PARAM_READWRITE |
- G_PARAM_CONSTRUCT_ONLY |
- G_PARAM_STATIC_STRINGS));
+ PROP_CREATE_BACKUP,
+ g_param_spec_boolean ("create-backup",
+ _("Create Backup"),
+ NULL,
+ FALSE,
+ G_PARAM_READWRITE |
+ G_PARAM_CONSTRUCT |
+ G_PARAM_STATIC_STRINGS));
}
static void
@@ -349,7 +429,7 @@ cancel_output_stream (GtkSourceFileSaver *saver)
*/
static void
-query_info_cb (GFile *location,
+query_info_cb (GFile *file,
GAsyncResult *result,
GtkSourceFileSaver *saver)
{
@@ -359,8 +439,9 @@ query_info_cb (GFile *location,
g_print ("Finished query info on file\n");
});
+ /* TODO update mtime stored in GtkSourceBuffer */
g_clear_object (&saver->priv->info);
- saver->priv->info = g_file_query_info_finish (location, result, &error);
+ saver->priv->info = g_file_query_info_finish (file, result, &error);
if (error != NULL)
{
@@ -381,7 +462,6 @@ close_output_stream_cb (GOutputStream *output_stream,
GtkSourceFileSaver *saver)
{
GError *error = NULL;
- GFile *location;
DEBUG ({
g_print ("%s\n", G_STRFUNC);
@@ -407,9 +487,7 @@ close_output_stream_cb (GOutputStream *output_stream,
g_print ("Query info on file\n");
});
- location = gtk_source_file_get_location (saver->priv->file);
-
- g_file_query_info_async (location,
+ g_file_query_info_async (saver->priv->file,
QUERY_ATTRIBUTES,
G_FILE_QUERY_INFO_NONE,
g_task_get_priority (saver->priv->task),
@@ -564,21 +642,20 @@ read_file_chunk (GtkSourceFileSaver *saver)
}
static void
-replace_file_cb (GFile *location,
+replace_file_cb (GFile *file,
GAsyncResult *result,
GtkSourceFileSaver *saver)
{
GFileOutputStream *file_output_stream;
GOutputStream *output_stream;
GtkSourceBufferInputStream *buffer_stream;
- GtkTextBuffer *buffer;
GError *error = NULL;
DEBUG ({
g_print ("%s\n", G_STRFUNC);
});
- file_output_stream = g_file_replace_finish (location, result, &error);
+ file_output_stream = g_file_replace_finish (file, result, &error);
if (error != NULL)
{
@@ -646,9 +723,7 @@ replace_file_cb (GFile *location,
saver->priv->output_stream = G_OUTPUT_STREAM (output_stream);
}
- buffer = GTK_TEXT_BUFFER (gtk_source_file_get_buffer (saver->priv->file));
-
- saver->priv->input_stream = _gtk_source_buffer_input_stream_new (buffer,
+ saver->priv->input_stream = _gtk_source_buffer_input_stream_new (GTK_TEXT_BUFFER
(saver->priv->source_buffer),
saver->priv->newline_type,
saver->priv->ensure_trailing_newline);
@@ -665,19 +740,14 @@ replace_file_cb (GFile *location,
static void
begin_write (GtkSourceFileSaver *saver)
{
- GFile *location;
- gboolean create_backup = (saver->priv->flags & GTK_SOURCE_FILE_SAVE_CREATE_BACKUP) != 0;
-
DEBUG ({
g_print ("Start replacing file contents\n");
g_print ("Make backup: %s\n", make_backup ? "yes" : "no");
});
- location = gtk_source_file_get_location (saver->priv->file);
-
- g_file_replace_async (location,
+ g_file_replace_async (saver->priv->file,
NULL,
- create_backup,
+ saver->priv->create_backup,
G_FILE_CREATE_NONE,
g_task_get_priority (saver->priv->task),
g_task_get_cancellable (saver->priv->task),
@@ -686,7 +756,7 @@ begin_write (GtkSourceFileSaver *saver)
}
static void
-mount_cb (GFile *location,
+mount_cb (GFile *file,
GAsyncResult *result,
GtkSourceFileSaver *saver)
{
@@ -696,7 +766,7 @@ mount_cb (GFile *location,
g_print ("%s\n", G_STRFUNC);
});
- g_file_mount_enclosing_volume_finish (location, result, &error);
+ g_file_mount_enclosing_volume_finish (file, result, &error);
if (error != NULL)
{
@@ -709,7 +779,6 @@ mount_cb (GFile *location,
static void
recover_not_mounted (GtkSourceFileSaver *saver)
{
- GFile *location;
GMountOperation *mount_operation = _gtk_source_file_create_mount_operation (saver->priv->file);
DEBUG ({
@@ -718,9 +787,7 @@ recover_not_mounted (GtkSourceFileSaver *saver)
saver->priv->tried_mount = TRUE;
- location = gtk_source_file_get_location (saver->priv->file);
-
- g_file_mount_enclosing_volume (location,
+ g_file_mount_enclosing_volume (saver->priv->file,
G_MOUNT_MOUNT_NONE,
mount_operation,
g_task_get_cancellable (saver->priv->task),
@@ -731,22 +798,15 @@ recover_not_mounted (GtkSourceFileSaver *saver)
}
GtkSourceFileSaver *
-gtk_source_file_saver_new (GtkSourceFile *file,
- const GtkSourceEncoding *encoding,
- GtkSourceNewlineType newline_type,
- GtkSourceCompressionType compression_type,
- gboolean ensure_trailing_newline,
- GtkSourceFileSaveFlags flags)
+gtk_source_file_saver_new (GtkSourceBuffer *buffer,
+ GFile *file)
{
- g_return_val_if_fail (GTK_SOURCE_IS_FILE (file), NULL);
+ g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), NULL);
+ g_return_val_if_fail (G_IS_FILE (file), NULL);
return g_object_new (GTK_SOURCE_TYPE_FILE_SAVER,
+ "buffer", buffer,
"file", file,
- "encoding", encoding,
- "newline_type", newline_type,
- "compression_type", compression_type,
- "ensure-trailing-newline", ensure_trailing_newline,
- "flags", flags,
NULL);
}
@@ -762,14 +822,14 @@ gtk_source_file_saver_save_async (GtkSourceFileSaver *saver,
g_return_if_fail (GTK_SOURCE_IS_FILE_SAVER (saver));
g_return_if_fail (saver->priv->task == NULL);
- saver->priv->task = g_task_new (saver->priv->file, cancellable, callback, user_data);
+ saver->priv->task = g_task_new (saver, cancellable, callback, user_data);
g_task_set_priority (saver->priv->task, io_priority);
saver->priv->progress_cb = progress_callback;
saver->priv->progress_cb_data = progress_callback_data;
DEBUG ({
- g_print ("Starting save\n");
+ g_print ("Start saving\n");
});
begin_write (saver);
@@ -782,15 +842,7 @@ gtk_source_file_saver_save_finish (GtkSourceFileSaver *saver,
{
g_return_val_if_fail (GTK_SOURCE_IS_FILE_SAVER (saver), FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
- g_return_val_if_fail (g_task_is_valid (result, saver->priv->file), FALSE);
+ g_return_val_if_fail (g_task_is_valid (result, saver), FALSE);
return g_task_propagate_boolean (G_TASK (result), error);
}
-
-GFileInfo *
-gtk_source_file_saver_get_info (GtkSourceFileSaver *saver)
-{
- g_return_val_if_fail (GTK_SOURCE_IS_FILE_SAVER (saver), NULL);
-
- return saver->priv->info;
-}
diff --git a/gtksourceview/gtksourcefilesaver.h b/gtksourceview/gtksourcefilesaver.h
index 46952de..9317ee9 100644
--- a/gtksourceview/gtksourcefilesaver.h
+++ b/gtksourceview/gtksourcefilesaver.h
@@ -27,7 +27,6 @@
#include <gtk/gtk.h>
#include <gtksourceview/gtksourcetypes.h>
-#include <gtksourceview/gtksourcebuffer.h>
G_BEGIN_DECLS
@@ -41,21 +40,6 @@ G_BEGIN_DECLS
typedef struct _GtkSourceFileSaverClass GtkSourceFileSaverClass;
typedef struct _GtkSourceFileSaverPrivate GtkSourceFileSaverPrivate;
-/**
- * GtkSourceFileSaveFlags:
- * @GTK_SOURCE_FILE_SAVE_CREATE_BACKUP: create a backup before saving the file.
- * @GTK_SOURCE_FILE_SAVE_IGNORE_INVALID_CHARS: do not save invalid characters.
- *
- * FIXME ignore invalid chars is not yet used in gsv
- *
- * Since: 3.14
- */
-typedef enum
-{
- GTK_SOURCE_FILE_SAVE_CREATE_BACKUP = 1 << 0,
- GTK_SOURCE_FILE_SAVE_IGNORE_INVALID_CHARS = 1 << 1
-} GtkSourceFileSaveFlags;
-
struct _GtkSourceFileSaver
{
GObject object;
@@ -70,12 +54,8 @@ struct _GtkSourceFileSaverClass
GType gtk_source_file_saver_get_type (void) G_GNUC_CONST;
-GtkSourceFileSaver *gtk_source_file_saver_new (GtkSourceFile *file,
- const GtkSourceEncoding *encoding,
- GtkSourceNewlineType newline_type,
- GtkSourceCompressionType compression_type,
- gboolean
ensure_trailing_newline,
- GtkSourceFileSaveFlags flags);
+GtkSourceFileSaver *gtk_source_file_saver_new (GtkSourceBuffer *buffer,
+ GFile *file);
void gtk_source_file_saver_save_async (GtkSourceFileSaver *saver,
gint io_priority,
@@ -89,8 +69,6 @@ gboolean gtk_source_file_saver_save_finish (GtkSourceFileSaver
*saver,
GAsyncResult *result,
GError **error);
-GFileInfo *gtk_source_file_saver_get_info (GtkSourceFileSaver *saver);
-
G_END_DECLS
#endif /* __GTK_SOURCE_FILE_SAVER_H__ */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]