[gedit/compression] Implemented full support for compressed files



commit 673694e8f7ea149f573ae25df3f6d0b27286a1e4
Author: Jesse van den Kieboom <jessevdk gnome org>
Date:   Thu May 13 14:06:33 2010 +0200

    Implemented full support for compressed files
    
    This adds a CompressionType similar to NewlineType.

 gedit/gedit-commands-file.c               |   10 ++-
 gedit/gedit-document-loader.c             |   93 ++++++++++++++++--
 gedit/gedit-document-loader.h             |    4 +
 gedit/gedit-document-saver.c              |   34 +++++-
 gedit/gedit-document-saver.h              |   11 +-
 gedit/gedit-document.c                    |  140 ++++++++++++++++++-------
 gedit/gedit-document.h                    |   31 ++++--
 gedit/gedit-file-chooser-dialog.c         |  157 +++++++++++++++++++++++++++--
 gedit/gedit-file-chooser-dialog.h         |    6 +
 gedit/gedit-marshal.list                  |    2 +-
 gedit/gedit-tab.c                         |   18 ++-
 gedit/gedit-tab.h                         |    3 +-
 plugin-loaders/python/bindings/gedit.defs |   17 +++
 13 files changed, 438 insertions(+), 88 deletions(-)
---
diff --git a/gedit/gedit-commands-file.c b/gedit/gedit-commands-file.c
index 1afeb9f..6e33963 100644
--- a/gedit/gedit-commands-file.c
+++ b/gedit/gedit-commands-file.c
@@ -553,6 +553,7 @@ save_dialog_response_cb (GeditFileChooserDialog *dialog,
 	gpointer data;
 	GSList *tabs_to_save_as;
 	GeditDocumentNewlineType newline_type;
+	GeditDocumentCompressionType compression_type;
 
 	gedit_debug (DEBUG_COMMANDS);
 
@@ -572,6 +573,8 @@ save_dialog_response_cb (GeditFileChooserDialog *dialog,
 	encoding = gedit_file_chooser_dialog_get_encoding (dialog);
 	newline_type = gedit_file_chooser_dialog_get_newline_type (dialog);
 
+	compression_type = gedit_file_chooser_dialog_get_compression_type (dialog);
+
 	gtk_widget_destroy (GTK_WIDGET (dialog));
 
 	if (tab != NULL)
@@ -595,7 +598,7 @@ save_dialog_response_cb (GeditFileChooserDialog *dialog,
 		 * even if the saving fails... */
 		 _gedit_window_set_default_location (window, file);
 
-		_gedit_tab_save_as (tab, file, encoding, newline_type);
+		_gedit_tab_save_as (tab, file, encoding, newline_type, compression_type);
 	}
 
 	g_object_unref (file);
@@ -685,6 +688,7 @@ file_save_as (GeditTab    *tab,
 	gboolean uri_set = FALSE;
 	const GeditEncoding *encoding;
 	GeditDocumentNewlineType newline_type;
+	GeditDocumentCompressionType compression_type;
 
 	g_return_if_fail (GEDIT_IS_TAB (tab));
 	g_return_if_fail (GEDIT_IS_WINDOW (window));
@@ -758,6 +762,7 @@ file_save_as (GeditTab    *tab,
 	g_return_if_fail (encoding != NULL);
 
 	newline_type = gedit_document_get_newline_type (doc);
+	compression_type = gedit_document_get_compression_type (doc);
 
 	gedit_file_chooser_dialog_set_encoding (GEDIT_FILE_CHOOSER_DIALOG (save_dialog),
 						encoding);
@@ -765,6 +770,9 @@ file_save_as (GeditTab    *tab,
 	gedit_file_chooser_dialog_set_newline_type (GEDIT_FILE_CHOOSER_DIALOG (save_dialog),
 	                                            newline_type);
 
+	gedit_file_chooser_dialog_set_compression_type (GEDIT_FILE_CHOOSER_DIALOG (save_dialog),
+	                                                compression_type);
+
 	g_object_set_data (G_OBJECT (save_dialog),
 			   GEDIT_TAB_TO_SAVE_AS,
 			   tab);
diff --git a/gedit/gedit-document-loader.c b/gedit/gedit-document-loader.c
index 54ab1a3..ac8daa9 100644
--- a/gedit/gedit-document-loader.c
+++ b/gedit/gedit-document-loader.c
@@ -78,7 +78,8 @@ enum
 	PROP_DOCUMENT,
 	PROP_LOCATION,
 	PROP_ENCODING,
-	PROP_NEWLINE_TYPE
+	PROP_NEWLINE_TYPE,
+	PROP_COMPRESSION_TYPE
 };
 
 #define READ_CHUNK_SIZE 8192
@@ -109,6 +110,7 @@ struct _GeditDocumentLoaderPrivate
 	const GeditEncoding	 *encoding;
 	const GeditEncoding	 *auto_detected_encoding;
 	GeditDocumentNewlineType  auto_detected_newline_type;
+	GeditDocumentCompressionType auto_detected_compression_type;
 	
 	goffset                   bytes_read;
 
@@ -120,6 +122,7 @@ struct _GeditDocumentLoaderPrivate
 	gchar                     buffer[READ_CHUNK_SIZE];
 
 	GError                   *error;
+	gboolean                  guess_content_type_from_content;
 };
 
 G_DEFINE_TYPE(GeditDocumentLoader, gedit_document_loader, G_TYPE_OBJECT)
@@ -149,6 +152,9 @@ gedit_document_loader_set_property (GObject      *object,
 		case PROP_NEWLINE_TYPE:
 			loader->priv->auto_detected_newline_type = g_value_get_enum (value);
 			break;
+		case PROP_COMPRESSION_TYPE:
+			loader->priv->auto_detected_compression_type = g_value_get_enum (value);
+			break;
 		default:
 			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 			break;
@@ -177,6 +183,9 @@ gedit_document_loader_get_property (GObject    *object,
 		case PROP_NEWLINE_TYPE:
 			g_value_set_enum (value, loader->priv->auto_detected_newline_type);
 			break;
+		case PROP_COMPRESSION_TYPE:
+			g_value_set_enum (value, loader->priv->auto_detected_compression_type);
+			break;
 		default:
 			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 			break;
@@ -287,6 +296,18 @@ gedit_document_loader_class_init (GeditDocumentLoaderClass *klass)
 	                                                    GEDIT_TYPE_DOCUMENT_NEWLINE_TYPE,
 	                                                    GEDIT_DOCUMENT_NEWLINE_TYPE_LF,
 	                                                    G_PARAM_READWRITE |
+	                                                    G_PARAM_CONSTRUCT |
+	                                                    G_PARAM_STATIC_NAME |
+	                                                    G_PARAM_STATIC_BLURB));
+
+	g_object_class_install_property (object_class, PROP_COMPRESSION_TYPE,
+	                                 g_param_spec_enum ("compression-type",
+	                                                    "Compression type",
+	                                                    "The compression type",
+	                                                    GEDIT_TYPE_DOCUMENT_COMPRESSION_TYPE,
+	                                                    GEDIT_DOCUMENT_COMPRESSION_TYPE_NONE,
+	                                                    G_PARAM_READWRITE |
+	                                                    G_PARAM_CONSTRUCT |
 	                                                    G_PARAM_STATIC_NAME |
 	                                                    G_PARAM_STATIC_BLURB));
 
@@ -310,10 +331,6 @@ gedit_document_loader_init (GeditDocumentLoader *loader)
 {
 	loader->priv = GEDIT_DOCUMENT_LOADER_GET_PRIVATE (loader);
 
-	loader->priv->used = FALSE;
-	loader->priv->auto_detected_newline_type = GEDIT_DOCUMENT_NEWLINE_TYPE_DEFAULT;
-	loader->priv->converter = NULL;
-	loader->priv->error = NULL;
 	loader->priv->enc_settings = g_settings_new ("org.gnome.gedit.preferences.encodings");
 }
 
@@ -502,6 +519,32 @@ write_file_chunk (AsyncData *async)
 }
 
 static void
+guess_content_type (GeditDocumentLoader *loader,
+                    gsize                bytes_read)
+{
+	if (loader->priv->info != NULL && bytes_read > 0)
+	{
+		gchar *content_type;
+
+		content_type = g_content_type_guess (NULL,
+		                                     (const guchar *)loader->priv->buffer,
+		                                     bytes_read,
+		                                     NULL);
+
+		if (content_type != NULL)
+		{
+			g_file_info_set_attribute_string (loader->priv->info,
+			                                  G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
+			                                  content_type);
+
+			return;
+		}
+	}
+
+	loader->priv->guess_content_type_from_content = FALSE;
+}
+
+static void
 async_read_cb (GInputStream *stream,
 	       GAsyncResult *res,
 	       AsyncData    *async)
@@ -541,6 +584,14 @@ async_read_cb (GInputStream *stream,
 		return;
 	}
 
+	/* guess the content type from the data if needed */
+	if (loader->priv->bytes_read == 0 &&
+	    loader->priv->guess_content_type_from_content)
+	{
+		guess_content_type (async->loader,
+		                    async->read);
+	}
+
 	/* Bump the size. */
 	loader->priv->bytes_read += async->read;
 
@@ -663,10 +714,15 @@ finish_query_info (AsyncData *async)
 		                                            G_CONVERTER (decompressor));
 
 		g_object_unref (decompressor);
+
+		loader->priv->auto_detected_compression_type = GEDIT_DOCUMENT_COMPRESSION_TYPE_GZIP;
+		loader->priv->guess_content_type_from_content = TRUE;
 	}
 	else
 	{
 		base_stream = g_object_ref (loader->priv->stream);
+
+		loader->priv->auto_detected_compression_type = GEDIT_DOCUMENT_COMPRESSION_TYPE_NONE;
 	}
 
 	g_object_unref (loader->priv->stream);
@@ -688,6 +744,7 @@ query_info_cb (GFile        *source,
 {
 	GFileInfo *info;
 	GError *error = NULL;
+	GeditDocumentLoaderPrivate *priv;
 
 	gedit_debug (DEBUG_LOADER);
 
@@ -698,8 +755,10 @@ query_info_cb (GFile        *source,
 		return;
 	}	
 
+	priv = async->loader->priv;
+
 	/* finish the info query */
-	info = g_file_query_info_finish (async->loader->priv->location,
+	info = g_file_query_info_finish (priv->location,
 	                                 res,
 	                                 &error);
 
@@ -710,8 +769,8 @@ query_info_cb (GFile        *source,
 		return;
 	}
 
-	async->loader->priv->info = info;
-	
+	priv->info = info;
+
 	finish_query_info (async);
 }
 
@@ -956,6 +1015,15 @@ gedit_document_loader_get_newline_type (GeditDocumentLoader *loader)
 	return loader->priv->auto_detected_newline_type;
 }
 
+GeditDocumentCompressionType
+gedit_document_loader_get_compression_type (GeditDocumentLoader *loader)
+{
+	g_return_val_if_fail (GEDIT_IS_DOCUMENT_LOADER (loader),
+			      GEDIT_DOCUMENT_COMPRESSION_TYPE_NONE);
+
+	return loader->priv->auto_detected_compression_type;
+}
+
 GFileInfo *
 gedit_document_loader_get_info (GeditDocumentLoader *loader)
 {
@@ -963,4 +1031,13 @@ gedit_document_loader_get_info (GeditDocumentLoader *loader)
 
 	return loader->priv->info;
 }
+
+gboolean
+gedit_document_loader_get_content_type_is_guessed (GeditDocumentLoader *loader)
+{
+	g_return_val_if_fail (GEDIT_IS_DOCUMENT_LOADER (loader), FALSE);
+
+	return loader->priv->guess_content_type_from_content;
+}
+
 /* ex:ts=8:noet: */
diff --git a/gedit/gedit-document-loader.h b/gedit/gedit-document-loader.h
index 644af20..0c5c069 100644
--- a/gedit/gedit-document-loader.h
+++ b/gedit/gedit-document-loader.h
@@ -108,6 +108,10 @@ const GeditEncoding	*gedit_document_loader_get_encoding	(GeditDocumentLoader *lo
 
 GeditDocumentNewlineType gedit_document_loader_get_newline_type (GeditDocumentLoader *loader);
 
+GeditDocumentCompressionType gedit_document_loader_get_compression_type (GeditDocumentLoader *loader);
+
+gboolean                 gedit_document_loader_get_content_type_is_guessed (GeditDocumentLoader *loader);
+
 goffset			 gedit_document_loader_get_bytes_read	(GeditDocumentLoader *loader);
 
 /* You can get from the info: content_type, time_modified, standard_size, access_can_write 
diff --git a/gedit/gedit-document-saver.c b/gedit/gedit-document-saver.c
index 9d7da09..fb62d7d 100644
--- a/gedit/gedit-document-saver.c
+++ b/gedit/gedit-document-saver.c
@@ -64,6 +64,7 @@ enum {
 	PROP_LOCATION,
 	PROP_ENCODING,
 	PROP_NEWLINE_TYPE,
+	PROP_COMPRESSION_TYPE,
 	PROP_FLAGS
 };
 
@@ -98,6 +99,7 @@ struct _GeditDocumentSaverPrivate
 	GFile			 *location;
 	const GeditEncoding      *encoding;
 	GeditDocumentNewlineType  newline_type;
+	GeditDocumentCompressionType compression_type;
 
 	GeditDocumentSaveFlags    flags;
 
@@ -142,6 +144,9 @@ gedit_document_saver_set_property (GObject      *object,
 		case PROP_NEWLINE_TYPE:
 			saver->priv->newline_type = g_value_get_enum (value);
 			break;
+		case PROP_COMPRESSION_TYPE:
+			saver->priv->compression_type = g_value_get_enum (value);
+			break;
 		case PROP_FLAGS:
 			saver->priv->flags = g_value_get_flags (value);
 			break;
@@ -173,6 +178,9 @@ gedit_document_saver_get_property (GObject    *object,
 		case PROP_NEWLINE_TYPE:
 			g_value_set_enum (value, saver->priv->newline_type);
 			break;
+		case PROP_COMPRESSION_TYPE:
+			g_value_set_enum (value, saver->priv->compression_type);
+			break;
 		case PROP_FLAGS:
 			g_value_set_flags (value, saver->priv->flags);
 			break;
@@ -316,6 +324,18 @@ gedit_document_saver_class_init (GeditDocumentSaverClass *klass)
 					                    G_PARAM_CONSTRUCT_ONLY));
 
 	g_object_class_install_property (object_class,
+					 PROP_COMPRESSION_TYPE,
+					 g_param_spec_enum ("compression-type",
+					                    "Compression type",
+					                    "The compression type",
+					                    GEDIT_TYPE_DOCUMENT_COMPRESSION_TYPE,
+					                    GEDIT_DOCUMENT_COMPRESSION_TYPE_NONE,
+					                    G_PARAM_READWRITE |
+					                    G_PARAM_STATIC_NAME |
+					                    G_PARAM_STATIC_BLURB |
+					                    G_PARAM_CONSTRUCT_ONLY));
+
+	g_object_class_install_property (object_class,
 					 PROP_FLAGS,
 					 g_param_spec_flags ("flags",
 							     "Flags",
@@ -352,11 +372,12 @@ gedit_document_saver_init (GeditDocumentSaver *saver)
 }
 
 GeditDocumentSaver *
-gedit_document_saver_new (GeditDocument           *doc,
-			  GFile                   *location,
-			  const GeditEncoding     *encoding,
-			  GeditDocumentNewlineType newline_type,
-			  GeditDocumentSaveFlags   flags)
+gedit_document_saver_new (GeditDocument                *doc,
+			  GFile                        *location,
+			  const GeditEncoding          *encoding,
+			  GeditDocumentNewlineType      newline_type,
+			  GeditDocumentCompressionType  compression_type,
+			  GeditDocumentSaveFlags        flags)
 {
 	g_return_val_if_fail (GEDIT_IS_DOCUMENT (doc), NULL);
 
@@ -368,6 +389,7 @@ gedit_document_saver_new (GeditDocument           *doc,
 						   "location", location,
 						   "encoding", encoding,
 						   "newline_type", newline_type,
+						   "compression_type", compression_type,
 						   "flags", flags,
 						   NULL));
 }
@@ -720,7 +742,7 @@ async_replace_ready_callback (GFile        *source,
 
 	content_type = gedit_document_get_content_type (saver->priv->document);
 
-	if (g_strcmp0 (content_type, "application/x-gzip") == 0)
+	if (saver->priv->compression_type == GEDIT_DOCUMENT_COMPRESSION_TYPE_GZIP)
 	{
 		GZlibCompressor *compressor;
 
diff --git a/gedit/gedit-document-saver.h b/gedit/gedit-document-saver.h
index 9b10241..ba8f2e6 100644
--- a/gedit/gedit-document-saver.h
+++ b/gedit/gedit-document-saver.h
@@ -82,11 +82,12 @@ struct _GeditDocumentSaverClass
 GType 		 	 gedit_document_saver_get_type		(void) G_GNUC_CONST;
 
 /* If enconding == NULL, the encoding will be autodetected */
-GeditDocumentSaver 	*gedit_document_saver_new 		(GeditDocument           *doc,
-								 GFile                   *location,
-								 const GeditEncoding     *encoding,
-								 GeditDocumentNewlineType newline_type,
-								 GeditDocumentSaveFlags   flags);
+GeditDocumentSaver 	*gedit_document_saver_new 		(GeditDocument                *doc,
+								 GFile                        *location,
+								 const GeditEncoding          *encoding,
+								 GeditDocumentNewlineType      newline_type,
+								 GeditDocumentCompressionType  compression_type,
+								 GeditDocumentSaveFlags        flags);
 
 void			 gedit_document_saver_saving		(GeditDocumentSaver *saver,
 								 gboolean            completed,
diff --git a/gedit/gedit-document.c b/gedit/gedit-document.c
index 4572a26..4860157 100644
--- a/gedit/gedit-document.c
+++ b/gedit/gedit-document.c
@@ -78,28 +78,29 @@ PROFILE (static GTimer *timer = NULL)
 
 #define GEDIT_DOCUMENT_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), GEDIT_TYPE_DOCUMENT, GeditDocumentPrivate))
 
-static void	gedit_document_load_real	(GeditDocument          *doc,
-						 GFile                  *location,
-						 const GeditEncoding    *encoding,
-						 gint                    line_pos,
-						 gint                    column_pos,
-						 gboolean                create);
-static void	gedit_document_save_real	(GeditDocument           *doc,
-						 GFile                   *location,
-						 const GeditEncoding     *encoding,
-						 GeditDocumentNewlineType newline_type,
-						 GeditDocumentSaveFlags   flags);
-static void	to_search_region_range 		(GeditDocument *doc,
-						 GtkTextIter   *start, 
-						 GtkTextIter   *end);
-static void 	insert_text_cb		 	(GeditDocument *doc, 
-						 GtkTextIter   *pos,
-						 const gchar   *text, 
-						 gint           length);
+static void	gedit_document_load_real	(GeditDocument                *doc,
+						 GFile                        *location,
+						 const GeditEncoding          *encoding,
+						 gint                          line_pos,
+						 gint                          column_pos,
+						 gboolean                      create);
+static void	gedit_document_save_real	(GeditDocument                *doc,
+						 GFile                        *location,
+						 const GeditEncoding          *encoding,
+						 GeditDocumentNewlineType      newline_type,
+						 GeditDocumentCompressionType  compression_type,
+						 GeditDocumentSaveFlags        flags);
+static void	to_search_region_range 		(GeditDocument                *doc,
+						 GtkTextIter                  *start,
+						 GtkTextIter                  *end);
+static void 	insert_text_cb		 	(GeditDocument                *doc,
+						 GtkTextIter                  *pos,
+						 const gchar                  *text,
+						 gint                          length);
 						 
-static void	delete_range_cb 		(GeditDocument *doc, 
-						 GtkTextIter   *start,
-						 GtkTextIter   *end);
+static void	delete_range_cb 		(GeditDocument                *doc,
+						 GtkTextIter                  *start,
+						 GtkTextIter                  *end);
 			     
 struct _GeditDocumentPrivate
 {
@@ -124,6 +125,7 @@ struct _GeditDocumentPrivate
 	gint	     num_of_lines_search_text;
 
 	GeditDocumentNewlineType newline_type;
+	GeditDocumentCompressionType compression_type;
 
 	/* Temp data while loading */
 	GeditDocumentLoader *loader;
@@ -162,7 +164,8 @@ enum {
 	PROP_ENCODING,
 	PROP_CAN_SEARCH_AGAIN,
 	PROP_ENABLE_SEARCH_HIGHLIGHTING,
-	PROP_NEWLINE_TYPE
+	PROP_NEWLINE_TYPE,
+	PROP_COMPRESSION_TYPE
 };
 
 enum {
@@ -240,6 +243,18 @@ set_newline_type (GeditDocument           *doc,
 }
 
 static void
+set_compression_type (GeditDocument *doc,
+                      GeditDocumentCompressionType compression_type)
+{
+	if (doc->priv->compression_type != compression_type)
+	{
+		doc->priv->compression_type = compression_type;
+
+		g_object_notify (G_OBJECT (doc), "compression-type");
+	}
+}
+
+static void
 gedit_document_dispose (GObject *object)
 {
 	GeditDocument *doc = GEDIT_DOCUMENT (object); 
@@ -376,6 +391,9 @@ gedit_document_get_property (GObject    *object,
 		case PROP_NEWLINE_TYPE:
 			g_value_set_enum (value, doc->priv->newline_type);
 			break;
+		case PROP_COMPRESSION_TYPE:
+			g_value_set_enum (value, doc->priv->compression_type);
+			break;
 		default:
 			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 			break;
@@ -400,6 +418,10 @@ gedit_document_set_property (GObject      *object,
 			set_newline_type (doc,
 					  g_value_get_enum (value));
 			break;
+		case PROP_COMPRESSION_TYPE:
+			set_compression_type (doc,
+			                      g_value_get_enum (value));
+			break;
 		case PROP_SHORTNAME:
 			gedit_document_set_short_name_for_display (doc,
 			                                           g_value_get_string (value));
@@ -545,6 +567,24 @@ gedit_document_class_init (GeditDocumentClass *klass)
 	                                                    GEDIT_TYPE_DOCUMENT_NEWLINE_TYPE,
 	                                                    GEDIT_DOCUMENT_NEWLINE_TYPE_LF,
 	                                                    G_PARAM_READWRITE |
+	                                                    G_PARAM_CONSTRUCT |
+	                                                    G_PARAM_STATIC_NAME |
+	                                                    G_PARAM_STATIC_BLURB));
+
+	/**
+	 * GeditDocument:compression-type:
+	 *
+	 * The :compression-type property determines how to compress the
+	 * document when saving
+	 */
+	g_object_class_install_property (object_class, PROP_COMPRESSION_TYPE,
+	                                 g_param_spec_enum ("compression-type",
+	                                                    "Compression type",
+	                                                    "The save compression type",
+	                                                    GEDIT_TYPE_DOCUMENT_COMPRESSION_TYPE,
+	                                                    GEDIT_DOCUMENT_COMPRESSION_TYPE_NONE,
+	                                                    G_PARAM_READWRITE |
+	                                                    G_PARAM_CONSTRUCT |
 	                                                    G_PARAM_STATIC_NAME |
 	                                                    G_PARAM_STATIC_BLURB));
 
@@ -633,14 +673,15 @@ gedit_document_class_init (GeditDocumentClass *klass)
 			      G_SIGNAL_RUN_LAST,
 			      G_STRUCT_OFFSET (GeditDocumentClass, save),
 			      NULL, NULL,
-			      gedit_marshal_VOID__OBJECT_BOXED_ENUM_FLAGS,
+			      gedit_marshal_VOID__OBJECT_BOXED_ENUM_ENUM_FLAGS,
 			      G_TYPE_NONE,
-			      4,
+			      5,
 			      G_TYPE_FILE,
 			      /* we rely on the fact that the GeditEncoding pointer stays
 			       * the same forever */
 			      GEDIT_TYPE_ENCODING | G_SIGNAL_TYPE_STATIC_SCOPE,
 			      GEDIT_TYPE_DOCUMENT_NEWLINE_TYPE,
+			      GEDIT_TYPE_DOCUMENT_COMPRESSION_TYPE,
 			      GEDIT_TYPE_DOCUMENT_SAVE_FLAGS);
 
 	document_signals[SAVING] =
@@ -936,8 +977,6 @@ gedit_document_init (GeditDocument *doc)
 
 	doc->priv->encoding = gedit_encoding_get_utf8 ();
 
-	doc->priv->newline_type = GEDIT_DOCUMENT_NEWLINE_TYPE_DEFAULT;
-
 	g_settings_get (doc->priv->editor_settings, GEDIT_SETTINGS_MAX_UNDO_ACTIONS,
 			"u", &undo_actions);
 	bracket_matching = g_settings_get_boolean (doc->priv->editor_settings,
@@ -1320,6 +1359,9 @@ document_loader_loaded (GeditDocumentLoader *loader,
 		set_newline_type (doc,
 		                  gedit_document_loader_get_newline_type (loader));
 
+		set_compression_type (doc,
+		                      gedit_document_loader_get_compression_type (loader));
+
 		restore_cursor = g_settings_get_boolean (doc->priv->editor_settings,
 							 GEDIT_SETTINGS_RESTORE_CURSOR_POSITION);
 
@@ -1368,6 +1410,11 @@ document_loader_loaded (GeditDocumentLoader *loader,
 		}
 
 		gtk_text_buffer_place_cursor (GTK_TEXT_BUFFER (doc), &iter);
+
+		if (gedit_document_loader_get_content_type_is_guessed (loader))
+		{
+			doc->priv->language_set_by_user = TRUE;
+		}
 	}
 
 	/* special case creating a named new doc */
@@ -1580,18 +1627,22 @@ document_saver_saving (GeditDocumentSaver *saver,
 }
 
 static void
-gedit_document_save_real (GeditDocument           *doc,
-			  GFile                   *location,
-			  const GeditEncoding     *encoding,
-			  GeditDocumentNewlineType newline_type,
-			  GeditDocumentSaveFlags   flags)
+gedit_document_save_real (GeditDocument                *doc,
+			  GFile                        *location,
+			  const GeditEncoding          *encoding,
+			  GeditDocumentNewlineType      newline_type,
+			  GeditDocumentCompressionType  compression_type,
+			  GeditDocumentSaveFlags        flags)
 {
 	g_return_if_fail (doc->priv->saver == NULL);
 
 	/* create a saver, it will be destroyed once saving is complete */
-	doc->priv->saver = gedit_document_saver_new (doc, location, encoding,
-						     newline_type,
-						     flags);
+	doc->priv->saver = gedit_document_saver_new (doc,
+	                                             location,
+	                                             encoding,
+	                                             newline_type,
+	                                             compression_type,
+	                                             flags);
 
 	g_signal_connect (doc->priv->saver,
 			  "saving",
@@ -1626,6 +1677,7 @@ gedit_document_save (GeditDocument          *doc,
 		       doc->priv->location,
 		       doc->priv->encoding,
 		       doc->priv->newline_type,
+		       doc->priv->compression_type,
 		       flags);
 }
 
@@ -1641,11 +1693,12 @@ gedit_document_save (GeditDocument          *doc,
  * to be emitted.
  */
 void
-gedit_document_save_as (GeditDocument           *doc,
-			GFile                   *location,
-			const GeditEncoding     *encoding,
-			GeditDocumentNewlineType newline_type,
-			GeditDocumentSaveFlags   flags)
+gedit_document_save_as (GeditDocument                *doc,
+			GFile                        *location,
+			const GeditEncoding          *encoding,
+			GeditDocumentNewlineType      newline_type,
+			GeditDocumentCompressionType  compression_type,
+			GeditDocumentSaveFlags        flags)
 {
 	g_return_if_fail (GEDIT_IS_DOCUMENT (doc));
 	g_return_if_fail (G_IS_FILE (location));
@@ -1659,6 +1712,7 @@ gedit_document_save_as (GeditDocument           *doc,
 		       location,
 		       encoding,
 		       newline_type,
+		       compression_type,
 		       flags | GEDIT_DOCUMENT_SAVE_IGNORE_MTIME);
 }
 
@@ -2582,6 +2636,14 @@ gedit_document_get_newline_type (GeditDocument *doc)
 	return doc->priv->newline_type;
 }
 
+GeditDocumentCompressionType
+gedit_document_get_compression_type (GeditDocument *doc)
+{
+	g_return_val_if_fail (GEDIT_IS_DOCUMENT (doc), 0);
+
+	return doc->priv->compression_type;
+}
+
 void
 _gedit_document_set_mount_operation_factory (GeditDocument 	       *doc,
 					    GeditMountOperationFactory	callback,
diff --git a/gedit/gedit-document.h b/gedit/gedit-document.h
index fe6b623..4955a63 100644
--- a/gedit/gedit-document.h
+++ b/gedit/gedit-document.h
@@ -82,6 +82,12 @@ typedef enum
 
 } GeditSearchFlags;
 
+typedef enum
+{
+	GEDIT_DOCUMENT_COMPRESSION_TYPE_NONE,
+	GEDIT_DOCUMENT_COMPRESSION_TYPE_GZIP
+} GeditDocumentCompressionType;
+
 /**
  * GeditDocumentSaveFlags:
  * @GEDIT_DOCUMENT_SAVE_IGNORE_MTIME: save file despite external modifications.
@@ -140,11 +146,12 @@ struct _GeditDocumentClass
 					 const GError     *error);
 
 	/* Document save */
-	void (* save)			(GeditDocument           *document,
-					 GFile                   *location,
-					 const GeditEncoding     *encoding,
-					 GeditDocumentNewlineType newline_type,
-					 GeditDocumentSaveFlags   flags);
+	void (* save)			(GeditDocument                *document,
+					 GFile                        *location,
+					 const GeditEncoding          *encoding,
+					 GeditDocumentNewlineType      newline_type,
+					 GeditDocumentCompressionType  compression_type,
+					 GeditDocumentSaveFlags        flags);
 
 	void (* saving)			(GeditDocument    *document,
 					 goffset	   size,
@@ -214,11 +221,12 @@ gboolean	 gedit_document_load_cancel	(GeditDocument       *doc);
 void		 gedit_document_save 		(GeditDocument       *doc,
 						 GeditDocumentSaveFlags flags);
 
-void		 gedit_document_save_as 	(GeditDocument           *doc,	
-						 GFile                   *location, 
-						 const GeditEncoding     *encoding,
-						 GeditDocumentNewlineType newline_type,
-						 GeditDocumentSaveFlags   flags);
+void		 gedit_document_save_as 	(GeditDocument                *doc,
+						 GFile                        *location,
+						 const GeditEncoding          *encoding,
+						 GeditDocumentNewlineType      newline_type,
+						 GeditDocumentCompressionType  compression_type,
+						 GeditDocumentSaveFlags        flags);
 
 gboolean	 gedit_document_is_untouched 	(GeditDocument       *doc);
 gboolean	 gedit_document_is_untitled 	(GeditDocument       *doc);
@@ -279,6 +287,9 @@ gboolean	 gedit_document_get_enable_search_highlighting
 GeditDocumentNewlineType
 		 gedit_document_get_newline_type (GeditDocument *doc);
 
+GeditDocumentCompressionType
+		 gedit_document_get_compression_type (GeditDocument *doc);
+
 gchar		*gedit_document_get_metadata	(GeditDocument *doc,
 						 const gchar   *key);
 
diff --git a/gedit/gedit-file-chooser-dialog.c b/gedit/gedit-file-chooser-dialog.c
index 84de7fb..1181247 100644
--- a/gedit/gedit-file-chooser-dialog.c
+++ b/gedit/gedit-file-chooser-dialog.c
@@ -62,6 +62,10 @@ struct _GeditFileChooserDialogPrivate
 	GtkWidget *newline_label;
 	GtkWidget *newline_combo;
 	GtkListStore *newline_store;
+
+	GtkWidget *compression_label;
+	GtkWidget *compression_combo;
+	GtkListStore *compression_store;
 };
 
 G_DEFINE_TYPE(GeditFileChooserDialog, gedit_file_chooser_dialog, GTK_TYPE_FILE_CHOOSER_DIALOG)
@@ -217,6 +221,94 @@ create_newline_combo (GeditFileChooserDialog *dialog)
 }
 
 static void
+update_compression_visibility (GeditFileChooserDialog *dialog)
+{
+	if (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog)) == GTK_FILE_CHOOSER_ACTION_SAVE)
+	{
+		gtk_widget_show (dialog->priv->compression_label);
+		gtk_widget_show (dialog->priv->compression_combo);
+	}
+	else
+	{
+		gtk_widget_hide (dialog->priv->compression_label);
+		gtk_widget_hide (dialog->priv->compression_combo);
+	}
+}
+
+static void
+compression_combo_append (GtkComboBox                  *combo,
+                          GtkListStore                 *store,
+                          GtkTreeIter                  *iter,
+                          const gchar                  *label,
+                          GeditDocumentCompressionType  compression_type)
+{
+	gtk_list_store_append (store, iter);
+	gtk_list_store_set (store, iter, 0, label, 1, compression_type, -1);
+
+	if (compression_type == GEDIT_DOCUMENT_COMPRESSION_TYPE_NONE)
+	{
+		gtk_combo_box_set_active_iter (combo, iter);
+	}
+}
+
+static void
+create_compression_combo (GeditFileChooserDialog *dialog)
+{
+	GtkWidget *label, *combo;
+	GtkListStore *store;
+	GtkCellRenderer *renderer;
+	GtkTreeIter iter;
+
+	label = gtk_label_new_with_mnemonic (_("_Compression:"));
+	gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
+
+	store = gtk_list_store_new (2, G_TYPE_STRING, GEDIT_TYPE_DOCUMENT_COMPRESSION_TYPE);
+	combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
+	renderer = gtk_cell_renderer_text_new ();
+
+	gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo),
+	                            renderer,
+	                            TRUE);
+
+	gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo),
+	                               renderer,
+	                               "text",
+	                               0);
+
+	compression_combo_append (GTK_COMBO_BOX (combo),
+	                          store,
+	                          &iter,
+	                          _("None"),
+	                          GEDIT_DOCUMENT_COMPRESSION_TYPE_NONE);
+
+	compression_combo_append (GTK_COMBO_BOX (combo),
+	                          store,
+	                          &iter,
+	                          _("GZip"),
+	                          GEDIT_DOCUMENT_COMPRESSION_TYPE_GZIP);
+
+	gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo);
+
+	gtk_box_pack_start (GTK_BOX (dialog->priv->extra_widget),
+	                    label,
+	                    FALSE,
+	                    TRUE,
+	                    0);
+
+	gtk_box_pack_start (GTK_BOX (dialog->priv->extra_widget),
+	                    combo,
+	                    TRUE,
+	                    TRUE,
+	                    0);
+
+	dialog->priv->compression_combo = combo;
+	dialog->priv->compression_label = label;
+	dialog->priv->compression_store = store;
+
+	update_compression_visibility (dialog);
+}
+
+static void
 create_extra_widget (GeditFileChooserDialog *dialog)
 {
 	dialog->priv->extra_widget = gtk_hbox_new (FALSE, 6);
@@ -225,6 +317,7 @@ create_extra_widget (GeditFileChooserDialog *dialog)
 
 	create_option_menu (dialog);
 	create_newline_combo (dialog);
+	create_compression_combo (dialog);
 
 	gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (dialog),
 					   dialog->priv->extra_widget);
@@ -259,6 +352,7 @@ action_changed (GeditFileChooserDialog *dialog,
 	}
 
 	update_newline_visibility (dialog);
+	update_compression_visibility (dialog);
 }
 
 static void
@@ -524,17 +618,14 @@ gedit_file_chooser_dialog_get_encoding (GeditFileChooserDialog *dialog)
 				GEDIT_ENCODINGS_COMBO_BOX (dialog->priv->option_menu));
 }
 
-void
-gedit_file_chooser_dialog_set_newline_type (GeditFileChooserDialog  *dialog,
-					    GeditDocumentNewlineType newline_type)
+static void
+set_enum_combo (GtkComboBox *combo,
+                gint         value)
 {
 	GtkTreeIter iter;
 	GtkTreeModel *model;
 
-	g_return_if_fail (GEDIT_IS_FILE_CHOOSER_DIALOG (dialog));
-	g_return_if_fail (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog)) == GTK_FILE_CHOOSER_ACTION_SAVE);
-
-	model = GTK_TREE_MODEL (dialog->priv->newline_store);
+	model = gtk_combo_box_get_model (combo);
 
 	if (!gtk_tree_model_get_iter_first (model, &iter))
 	{
@@ -543,19 +634,29 @@ gedit_file_chooser_dialog_set_newline_type (GeditFileChooserDialog  *dialog,
 
 	do
 	{
-		GeditDocumentNewlineType nt;
+		gint nt;
 
 		gtk_tree_model_get (model, &iter, 1, &nt, -1);
 
-		if (newline_type == nt)
+		if (value == nt)
 		{
-			gtk_combo_box_set_active_iter (GTK_COMBO_BOX (dialog->priv->newline_combo),
-			                               &iter);
+			gtk_combo_box_set_active_iter (combo, &iter);
 			break;
 		}
 	} while (gtk_tree_model_iter_next (model, &iter));
 }
 
+void
+gedit_file_chooser_dialog_set_newline_type (GeditFileChooserDialog  *dialog,
+					    GeditDocumentNewlineType newline_type)
+{
+	g_return_if_fail (GEDIT_IS_FILE_CHOOSER_DIALOG (dialog));
+	g_return_if_fail (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog)) == GTK_FILE_CHOOSER_ACTION_SAVE);
+
+	set_enum_combo (GTK_COMBO_BOX (dialog->priv->newline_combo),
+	                newline_type);
+}
+
 GeditDocumentNewlineType
 gedit_file_chooser_dialog_get_newline_type (GeditFileChooserDialog *dialog)
 {
@@ -577,4 +678,38 @@ gedit_file_chooser_dialog_get_newline_type (GeditFileChooserDialog *dialog)
 
 	return newline_type;
 }
+
+void
+gedit_file_chooser_dialog_set_compression_type (GeditFileChooserDialog       *dialog,
+                                                GeditDocumentCompressionType  compression_type)
+{
+	g_return_if_fail (GEDIT_IS_FILE_CHOOSER_DIALOG (dialog));
+	g_return_if_fail (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog)) == GTK_FILE_CHOOSER_ACTION_SAVE);
+
+	set_enum_combo (GTK_COMBO_BOX (dialog->priv->compression_combo),
+	                compression_type);
+}
+
+GeditDocumentCompressionType
+gedit_file_chooser_dialog_get_compression_type (GeditFileChooserDialog *dialog)
+{
+	GtkTreeIter iter;
+	GeditDocumentCompressionType compression_type;
+
+	g_return_val_if_fail (GEDIT_IS_FILE_CHOOSER_DIALOG (dialog), GEDIT_DOCUMENT_COMPRESSION_TYPE_NONE);
+	g_return_val_if_fail (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog)) == GTK_FILE_CHOOSER_ACTION_SAVE,
+	                      GEDIT_DOCUMENT_COMPRESSION_TYPE_NONE);
+
+	gtk_combo_box_get_active_iter (GTK_COMBO_BOX (dialog->priv->compression_combo),
+	                               &iter);
+
+	gtk_tree_model_get (GTK_TREE_MODEL (dialog->priv->compression_store),
+	                    &iter,
+	                    1,
+	                    &compression_type,
+	                    -1);
+
+	return compression_type;
+}
+
 /* ex:ts=8:noet: */
diff --git a/gedit/gedit-file-chooser-dialog.h b/gedit/gedit-file-chooser-dialog.h
index c249673..650b3a4 100644
--- a/gedit/gedit-file-chooser-dialog.h
+++ b/gedit/gedit-file-chooser-dialog.h
@@ -84,6 +84,12 @@ void		 gedit_file_chooser_dialog_set_newline_type (GeditFileChooserDialog  *dial
 GeditDocumentNewlineType
 		 gedit_file_chooser_dialog_get_newline_type (GeditFileChooserDialog *dialog);
 
+void		 gedit_file_chooser_dialog_set_compression_type (GeditFileChooserDialog  *dialog,
+							         GeditDocumentCompressionType compression_type);
+
+GeditDocumentCompressionType
+		 gedit_file_chooser_dialog_get_compression_type (GeditFileChooserDialog *dialog);
+
 G_END_DECLS
 
 #endif /* __GEDIT_FILE_CHOOSER_DIALOG_H__ */
diff --git a/gedit/gedit-marshal.list b/gedit/gedit-marshal.list
index 8a46a0b..dec851b 100644
--- a/gedit/gedit-marshal.list
+++ b/gedit/gedit-marshal.list
@@ -5,7 +5,7 @@ VOID:BOOLEAN,POINTER
 VOID:BOXED,BOXED
 VOID:OBJECT
 VOID:POINTER
-VOID:OBJECT,BOXED,ENUM,FLAGS
+VOID:OBJECT,BOXED,ENUM,ENUM,FLAGS
 VOID:OBJECT,BOXED,INT,BOOLEAN
 VOID:UINT,POINTER
 VOID:UINT64,UINT64
diff --git a/gedit/gedit-tab.c b/gedit/gedit-tab.c
index 07f0ca4..ba8cf87 100644
--- a/gedit/gedit-tab.c
+++ b/gedit/gedit-tab.c
@@ -1276,6 +1276,7 @@ recoverable_saving_error_message_area_response (GtkWidget *message_area,
 					tab->priv->tmp_save_location,
 					tab->priv->tmp_encoding,
 					gedit_document_get_newline_type (doc),
+					gedit_document_get_compression_type (doc),
 					tab->priv->save_flags);
 	}
 	else
@@ -2199,10 +2200,11 @@ gedit_tab_auto_save (GeditTab *tab)
 }
 
 void
-_gedit_tab_save_as (GeditTab                 *tab,
-                    GFile                    *location,
-                    const GeditEncoding      *encoding,
-                    GeditDocumentNewlineType  newline_type)
+_gedit_tab_save_as (GeditTab                     *tab,
+                    GFile                        *location,
+                    const GeditEncoding          *encoding,
+                    GeditDocumentNewlineType      newline_type,
+                    GeditDocumentCompressionType  compression_type)
 {
 	GeditDocument *doc;
 	GeditDocumentSaveFlags save_flags;
@@ -2248,8 +2250,12 @@ _gedit_tab_save_as (GeditTab                 *tab,
 	if (tab->priv->auto_save_timeout > 0)
 		remove_auto_save_timeout (tab);
 
-	gedit_document_save_as (doc, location, encoding, newline_type,
-				tab->priv->save_flags);
+	gedit_document_save_as (doc,
+	                        location,
+	                        encoding,
+	                        newline_type,
+	                        compression_type,
+	                        tab->priv->save_flags);
 }
 
 #define GEDIT_PAGE_SETUP_KEY "gedit-page-setup-key"
diff --git a/gedit/gedit-tab.h b/gedit/gedit-tab.h
index acb632f..9087836 100644
--- a/gedit/gedit-tab.h
+++ b/gedit/gedit-tab.h
@@ -149,7 +149,8 @@ void		 _gedit_tab_save		(GeditTab            *tab);
 void		 _gedit_tab_save_as		(GeditTab            *tab,
 						 GFile               *location,
 						 const GeditEncoding *encoding,
-						 GeditDocumentNewlineType newline_type);
+						 GeditDocumentNewlineType newline_type,
+						 GeditDocumentCompressionType compression_type);
 
 void		 _gedit_tab_print		(GeditTab            *tab);
 void		 _gedit_tab_print_preview	(GeditTab            *tab);
diff --git a/plugin-loaders/python/bindings/gedit.defs b/plugin-loaders/python/bindings/gedit.defs
index b010b7c..9bfe7b8 100644
--- a/plugin-loaders/python/bindings/gedit.defs
+++ b/plugin-loaders/python/bindings/gedit.defs
@@ -114,6 +114,16 @@
   )
 )
 
+(define-enum CompressionType
+  (in-module "Gedit")
+  (c-name "GeditDocumentCompressionType")
+  (gtype-id "GEDIT_TYPE_DOCUMENT_COMPRESSION_TYPE")
+  (values
+    '("none" "GEDIT_DOCUMENT_COMPRESSION_TYPE_NONE")
+    '("gzip" "GEDIT_DOCUMENT_COMPRESSION_TYPE_GZIP")
+  )
+)
+
 (define-flags DocumentSaveFlags
   (in-module "Gedit")
   (c-name "GeditDocumentSaveFlags")
@@ -290,6 +300,12 @@
   (return-type "GeditDocumentNewlineType")
 )
 
+(define-method get_compression_type
+  (of-object "GeditDocument")
+  (c-name "gedit_document_get_compression_type")
+  (return-type "GeditDocumentCompressionType")
+)
+
 (define-method get_uri_for_display
   (of-object "GeditDocument")
   (c-name "gedit_document_get_uri_for_display")
@@ -356,6 +372,7 @@
     '("GFile*" "location")
     '("const-GeditEncoding*" "encoding")
     '("GeditDocumentNewlineType" "newline_type")
+    '("GeditDocumentCompressionType" "compression_type")
     '("GeditDocumentSaveFlags" "flags")
   )
 )



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