513534f45a447843c1107180d3616f7d25846304
diff --git a/configure.ac b/configure.ac
index 75d1f35..8fdceb4 100644
--- a/configure.ac
+++ b/configure.ac
@@ -166,6 +166,7 @@ dnl ================================================================
 
 PKG_CHECK_MODULES(GEDIT, [
 	glib-2.0 >= 2.14.0
+	gio-2.0 >= 2.15.1
 	gtk+-2.0 >= 2.12.0
 	gtksourceview-2.0 >= 2.0.0
 	libgnomeui-2.0 >= 2.16.0
diff --git a/gedit/Makefile.am b/gedit/Makefile.am
index be824d6..6ec8529 100644
--- a/gedit/Makefile.am
+++ b/gedit/Makefile.am
@@ -65,6 +65,7 @@ NOINST_H_FILES =			\
 	gedit-document-loader.h		\
 	gedit-mmap-document-loader.h	\
 	gedit-gnomevfs-document-loader.h\
+	gedit-gio-document-loader.h	\
 	gedit-document-saver.h		\
 	gedit-local-document-saver.h	\
 	gedit-gnomevfs-document-saver.h	\
@@ -130,6 +131,7 @@ libgedit_la_SOURCES = 			\
 	gedit-document-loader.c		\
 	gedit-mmap-document-loader.c	\
 	gedit-gnomevfs-document-loader.c\
+	gedit-gio-document-loader.c	\
 	gedit-document-saver.c		\
 	gedit-local-document-saver.c	\
 	gedit-gnomevfs-document-saver.c	\
diff --git a/gedit/gedit-document-loader.c b/gedit/gedit-document-loader.c
index 3ffc131..6cf5bd6 100644
--- a/gedit/gedit-document-loader.c
+++ b/gedit/gedit-document-loader.c
@@ -43,8 +43,9 @@
 #include "gedit-marshal.h"
 
 /* Those are for the the gedit_document_loader_new() factory */
-#include "gedit-mmap-document-loader.h"
-#include "gedit-gnomevfs-document-loader.h"
+//#include "gedit-mmap-document-loader.h"
+//#include "gedit-gnomevfs-document-loader.h"
+#include "gedit-gio-document-loader.h"
 
 G_DEFINE_ABSTRACT_TYPE(GeditDocumentLoader, gedit_document_loader, G_TYPE_OBJECT)
 
@@ -362,10 +363,13 @@ gedit_document_loader_new (GeditDocument       *doc,
 
 	g_return_val_if_fail (GEDIT_IS_DOCUMENT (doc), NULL);
 
+#if 0
 	if (gedit_utils_uri_has_file_scheme (uri))
 		loader_type = GEDIT_TYPE_MMAP_DOCUMENT_LOADER;
 	else
 		loader_type = GEDIT_TYPE_GNOMEVFS_DOCUMENT_LOADER;
+#endif
+	loader_type = GEDIT_TYPE_GIO_DOCUMENT_LOADER;
 
 	loader = GEDIT_DOCUMENT_LOADER (g_object_new (loader_type,
 						      "document", doc,
diff --git a/gedit/gedit-gio-document-loader.c b/gedit/gedit-gio-document-loader.c
new file mode 100644
index 0000000..962608a
--- /dev/null
+++ b/gedit/gedit-gio-document-loader.c
@@ -0,0 +1,334 @@
+/*
+ * gedit-gio-document-loader.c
+ * This file is part of gedit
+ *
+ * Copyright (C) 2007 - Paolo Maggi, Steve Frécinaux
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+ 
+/*
+ * Modified by the gedit Team, 2005-2007. See the AUTHORS file for a
+ * list of people on the gedit Team.
+ * See the ChangeLog files for a list of changes.
+ *
+ * $Id$
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <glib/gi18n.h>
+#include <glib/gstdio.h>
+#include <gio/gio.h>
+
+#include "gedit-gio-document-loader.h"
+#include "gedit-debug.h"
+#include "gedit-metadata-manager.h"
+#include "gedit-utils.h"
+
+static void		 gedit_gio_document_loader_load			(GeditDocumentLoader *loader);
+static gboolean		 gedit_gio_document_loader_cancel		(GeditDocumentLoader *loader);
+static const gchar	*gedit_gio_document_loader_get_mime_type	(GeditDocumentLoader *loader);
+static time_t		 gedit_gio_document_loader_get_mtime		(GeditDocumentLoader *loader);
+static GnomeVFSFileSize	 gedit_gio_document_loader_get_file_size	(GeditDocumentLoader *loader);
+static GnomeVFSFileSize	 gedit_gio_document_loader_get_bytes_read	(GeditDocumentLoader *loader);
+static gboolean		 gedit_gio_document_loader_get_readonly		(GeditDocumentLoader *loader);
+
+#define READ_CHUNK_SIZE 8192
+
+struct _GeditGIODocumentLoaderPrivate
+{
+	/* Info on the current file */
+	GFile                *file;
+
+	GFileInfo            *info;
+	GInputStream         *stream;
+	GCancellable         *cancellable;
+	goffset               bytes_read;
+
+	gchar                *buffer;
+
+	GError               *error;
+};
+
+G_DEFINE_TYPE(GeditGIODocumentLoader, gedit_gio_document_loader, GEDIT_TYPE_DOCUMENT_LOADER)
+
+static void
+gedit_gio_document_loader_finalize (GObject *object)
+{
+	GeditGIODocumentLoaderPrivate *priv;
+
+	priv = GEDIT_GIO_DOCUMENT_LOADER (object)->priv;
+
+	if (priv->info)
+		g_object_unref (priv->info);
+
+	g_free (priv->buffer);
+
+	if (priv->file)
+		g_object_unref (priv->file);
+
+	if (priv->cancellable)
+		g_object_unref (priv->cancellable);
+
+	if (priv->error)
+		g_error_free (priv->error);
+
+	G_OBJECT_CLASS (gedit_gio_document_loader_parent_class)->finalize (object);
+}
+
+static void
+gedit_gio_document_loader_class_init (GeditGIODocumentLoaderClass *klass)
+{
+	GObjectClass *object_class = G_OBJECT_CLASS (klass);
+	GeditDocumentLoaderClass *loader_class = GEDIT_DOCUMENT_LOADER_CLASS (klass);
+
+	object_class->finalize = gedit_gio_document_loader_finalize;
+
+	loader_class->load = gedit_gio_document_loader_load;
+	loader_class->cancel = gedit_gio_document_loader_cancel;
+	loader_class->get_mime_type = gedit_gio_document_loader_get_mime_type;
+	loader_class->get_mtime = gedit_gio_document_loader_get_mtime;
+	loader_class->get_file_size = gedit_gio_document_loader_get_file_size;
+	loader_class->get_bytes_read = gedit_gio_document_loader_get_bytes_read;
+	loader_class->get_readonly = gedit_gio_document_loader_get_readonly;
+
+	g_type_class_add_private (object_class, sizeof(GeditGIODocumentLoaderPrivate));
+}
+
+static void
+gedit_gio_document_loader_init (GeditGIODocumentLoader *gloader)
+{
+	gloader->priv = G_TYPE_INSTANCE_GET_PRIVATE (gloader,
+						     GEDIT_TYPE_GIO_DOCUMENT_LOADER,
+						     GeditGIODocumentLoaderPrivate);
+	gloader->priv->cancellable = g_cancellable_new ();
+	gloader->priv->error = NULL;
+}
+
+static void
+async_close_callback (GInputStream *stream,
+		      GAsyncResult *res,
+		      gpointer      data)
+{
+	/* We don't really care about this, but let's be clean */
+	g_input_stream_close_finish (stream, res, NULL);
+}
+
+static void
+load_completed_or_failed (GeditGIODocumentLoader *gloader)
+{
+	if (gloader->priv->stream != NULL)
+	{
+		g_input_stream_close_async (gloader->priv->stream,
+					    G_PRIORITY_DEFAULT,
+					    gloader->priv->cancellable,
+					    (GAsyncReadyCallback) async_close_callback,
+					    NULL);
+
+		g_free (gloader->priv->buffer);
+		gloader->priv->buffer = NULL;
+	}
+
+	gedit_document_loader_loading (GEDIT_DOCUMENT_LOADER (gloader),
+				       TRUE,
+				       gloader->priv->error);
+}
+
+static void
+async_read_callback (GInputStream           *stream,
+		     GAsyncResult           *res,
+		     GeditGIODocumentLoader *gloader)
+{
+	gsize bytes_read =
+		g_input_stream_read_finish (stream, res, &gloader->priv->error);
+
+	gloader->priv->bytes_read += bytes_read;
+
+	if (bytes_read == 0)
+	{
+		gedit_document_loader_update_document_contents (
+						GEDIT_DOCUMENT_LOADER (gloader),
+						gloader->priv->buffer,
+						gloader->priv->bytes_read,
+						&gloader->priv->error);
+
+		load_completed_or_failed (gloader);
+		return;
+	}
+
+
+
+	gloader->priv->buffer = g_realloc (gloader->priv->buffer,
+					   gloader->priv->bytes_read + READ_CHUNK_SIZE);
+
+	g_input_stream_read_async (gloader->priv->stream,
+				   gloader->priv->buffer + gloader->priv->bytes_read,
+				   READ_CHUNK_SIZE,
+				   G_PRIORITY_DEFAULT,
+				   gloader->priv->cancellable,
+				   (GAsyncReadyCallback) async_read_callback,
+				   gloader);
+
+	gedit_document_loader_loading (GEDIT_DOCUMENT_LOADER (gloader),
+				       FALSE,
+				       NULL);
+}
+
+static void
+async_query_info_callback (GInputStream           *stream,
+			   GAsyncResult           *res,
+			   GeditGIODocumentLoader *gloader)
+{
+	gloader->priv->info =
+		g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
+						       res,
+						       &gloader->priv->error);
+
+	gloader->priv->buffer = g_malloc (READ_CHUNK_SIZE);
+	g_input_stream_read_async (gloader->priv->stream,
+				   gloader->priv->buffer,
+				   READ_CHUNK_SIZE,
+				   G_PRIORITY_DEFAULT,
+				   gloader->priv->cancellable,
+				   (GAsyncReadyCallback) async_read_callback,
+				   gloader);
+
+	gedit_document_loader_loading (GEDIT_DOCUMENT_LOADER (gloader),
+				       FALSE,
+				       NULL);
+}
+
+static void
+async_open_callback (GFile                  *file,
+		     GAsyncResult           *res,
+		     GeditGIODocumentLoader *gloader)
+{
+	gloader->priv->stream = G_INPUT_STREAM (
+			g_file_read_finish (file, res, &gloader->priv->error));
+
+	if (gloader->priv->stream == NULL)
+	{
+		load_completed_or_failed (gloader);
+		return;
+	}
+
+	g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (gloader->priv->stream),
+					      "standard::content-type;standard::size;"
+					      "time::modified;access::can-write",
+					      G_PRIORITY_DEFAULT,
+					      gloader->priv->cancellable,
+					      (GAsyncReadyCallback) async_query_info_callback,
+					      gloader);
+
+	gedit_document_loader_loading (GEDIT_DOCUMENT_LOADER (gloader),
+				       FALSE,
+				       NULL);
+
+}
+
+static void
+gedit_gio_document_loader_load (GeditDocumentLoader *loader)
+{
+	GeditGIODocumentLoader *gloader = GEDIT_GIO_DOCUMENT_LOADER (loader);
+
+	g_print ("GIO yay\n");
+
+	gloader->priv->file = g_file_new_for_uri (loader->uri);
+
+	g_file_read_async (gloader->priv->file,
+			   G_PRIORITY_DEFAULT,
+			   gloader->priv->cancellable,
+			   (GAsyncReadyCallback) async_open_callback,
+			   gloader);
+
+	gedit_document_loader_loading (GEDIT_DOCUMENT_LOADER (gloader),
+				       FALSE,
+				       NULL);
+}
+
+static const gchar *
+gedit_gio_document_loader_get_mime_type (GeditDocumentLoader *loader)
+{
+	GeditGIODocumentLoader *gloader = GEDIT_GIO_DOCUMENT_LOADER (loader);
+
+	if (gloader->priv->info != NULL)
+		return g_file_info_get_content_type (gloader->priv->info);
+	else
+		return NULL;
+}
+
+static time_t
+gedit_gio_document_loader_get_mtime (GeditDocumentLoader *loader)
+{
+	GeditGIODocumentLoader *gloader = GEDIT_GIO_DOCUMENT_LOADER (loader);
+
+	if (gloader->priv->info != NULL)
+	{
+		GTimeVal tv;
+		g_file_info_get_modification_time (gloader->priv->info, &tv);
+		return tv.tv_sec;
+	}
+	else
+		return 0;
+}
+
+/* Returns 0 if file size is unknown */
+static GnomeVFSFileSize
+gedit_gio_document_loader_get_file_size (GeditDocumentLoader *loader)
+{
+	GeditGIODocumentLoader *gloader = GEDIT_GIO_DOCUMENT_LOADER (loader);
+
+	if (gloader->priv->info != NULL)
+		return g_file_info_get_size (gloader->priv->info);
+	else
+		return 0;
+}
+
+static GnomeVFSFileSize
+gedit_gio_document_loader_get_bytes_read (GeditDocumentLoader *loader)
+{
+	GeditGIODocumentLoader *gloader = GEDIT_GIO_DOCUMENT_LOADER (loader);
+
+	return gloader->priv->bytes_read;
+}
+
+static gboolean
+gedit_gio_document_loader_cancel (GeditDocumentLoader *loader)
+{
+	GeditGIODocumentLoader *gloader = GEDIT_GIO_DOCUMENT_LOADER (loader);
+
+	g_cancellable_cancel (gloader->priv->cancellable);
+
+	return g_cancellable_is_cancelled (gloader->priv->cancellable);
+}
+
+/* In the case the loader does not know if the file is readonly, for example
+   for most remote files, the function returns FALSE, so that we can try writing
+   and if needed handle the error. */
+static gboolean
+gedit_gio_document_loader_get_readonly (GeditDocumentLoader *loader)
+{
+	GeditGIODocumentLoader *gloader = GEDIT_GIO_DOCUMENT_LOADER (loader);
+
+	if (gloader->priv->info != NULL)
+		return !g_file_info_get_attribute_boolean (gloader->priv->info,
+							   "access::can-write");
+	else
+		return FALSE;
+}
diff --git a/gedit/gedit-gio-document-loader.h b/gedit/gedit-gio-document-loader.h
new file mode 100644
index 0000000..ef879b3
--- /dev/null
+++ b/gedit/gedit-gio-document-loader.h
@@ -0,0 +1,77 @@
+/*
+ * gedit-gio-document-loader.h
+ * This file is part of gedit
+ *
+ * Copyright (C) 2007 - Paolo Maggi, Steve Frécinaux
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, 
+ * Boston, MA 02111-1307, USA.
+ */
+ 
+/*
+ * Modified by the gedit Team, 2005-2007. See the AUTHORS file for a
+ * list of people on the gedit Team.
+ * See the ChangeLog files for a list of changes.
+ *
+ * $Id$
+ */
+
+#ifndef __GEDIT_GIO_DOCUMENT_LOADER_H__
+#define __GEDIT_GIO_DOCUMENT_LOADER_H__
+
+#include <gedit/gedit-document.h>
+#include "gedit-document-loader.h"
+
+G_BEGIN_DECLS
+
+/*
+ * Type checking and casting macros
+ */
+#define GEDIT_TYPE_GIO_DOCUMENT_LOADER              (gedit_gio_document_loader_get_type())
+#define GEDIT_GIO_DOCUMENT_LOADER(obj)              (G_TYPE_CHECK_INSTANCE_CAST((obj), GEDIT_TYPE_GIO_DOCUMENT_LOADER, GeditGIODocumentLoader))
+#define GEDIT_GIO_DOCUMENT_LOADER_CLASS(klass)      (G_TYPE_CHECK_CLASS_CAST((klass), GEDIT_TYPE_GIO_DOCUMENT_LOADER, GeditGIODocumentLoaderClass))
+#define GEDIT_IS_GIO_DOCUMENT_LOADER(obj)           (G_TYPE_CHECK_INSTANCE_TYPE((obj), GEDIT_TYPE_GIO_DOCUMENT_LOADER))
+#define GEDIT_IS_GIO_DOCUMENT_LOADER_CLASS(klass)   (G_TYPE_CHECK_CLASS_TYPE ((klass), GEDIT_TYPE_GIO_DOCUMENT_LOADER))
+#define GEDIT_GIO_DOCUMENT_LOADER_GET_CLASS(obj)    (G_TYPE_INSTANCE_GET_CLASS((obj), GEDIT_TYPE_GIO_DOCUMENT_LOADER, GeditGIODocumentLoaderClass))
+
+/* Private structure type */
+typedef struct _GeditGIODocumentLoaderPrivate GeditGIODocumentLoaderPrivate;
+
+/*
+ * Main object structure
+ */
+typedef struct _GeditGIODocumentLoader GeditGIODocumentLoader;
+
+struct _GeditGIODocumentLoader
+{
+	GeditDocumentLoader loader;
+
+	/*< private > */
+	GeditGIODocumentLoaderPrivate *priv;
+};
+
+/*
+ * Class definition
+ */
+typedef GeditDocumentLoaderClass GeditGIODocumentLoaderClass;
+
+/*
+ * Public methods
+ */
+GType 		 	 gedit_gio_document_loader_get_type	(void) G_GNUC_CONST;
+
+G_END_DECLS
+
+#endif  /* __GEDIT_GIO_DOCUMENT_LOADER_H__  */
