[gedit] Use GFileInfo instead of several getters for the loader.



commit 4bb0acc263872e63ed9f2f948df0999a573d8ce3
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Tue Nov 24 17:21:05 2009 +0100

    Use GFileInfo instead of several getters for the loader.
    
    Removed get_readonly, get_mtime, get_content_type and get_file_size.
    Instead of that we use GFileInfo which save us quite amount of code.

 gedit/gedit-document-loader.c     |   68 ++++++++++---------------
 gedit/gedit-document-loader.h     |   20 ++------
 gedit/gedit-document.c            |   36 ++++++++++---
 gedit/gedit-gio-document-loader.c |  100 +++++--------------------------------
 4 files changed, 71 insertions(+), 153 deletions(-)
---
diff --git a/gedit/gedit-document-loader.c b/gedit/gedit-document-loader.c
index f528eb0..661e639 100644
--- a/gedit/gedit-document-loader.c
+++ b/gedit/gedit-document-loader.c
@@ -126,15 +126,33 @@ gedit_document_loader_finalize (GObject *object)
 
 	g_free (loader->uri);
 
+	if (loader->info)
+		g_object_unref (loader->info);
+
 	G_OBJECT_CLASS (gedit_document_loader_parent_class)->finalize (object);
 }
 
 static void
+gedit_document_loader_dispose (GObject *object)
+{
+	GeditDocumentLoader *loader = GEDIT_DOCUMENT_LOADER (object);
+
+	if (loader->info != NULL)
+	{
+		g_object_unref (loader->info);
+		loader->info = NULL;
+	}
+
+	G_OBJECT_CLASS (gedit_document_loader_parent_class)->dispose (object);
+}
+
+static void
 gedit_document_loader_class_init (GeditDocumentLoaderClass *klass)
 {
 	GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
 	object_class->finalize = gedit_document_loader_finalize;
+	object_class->dispose = gedit_document_loader_dispose;
 	object_class->get_property = gedit_document_loader_get_property;
 	object_class->set_property = gedit_document_loader_set_property;
 
@@ -425,32 +443,6 @@ gedit_document_loader_get_uri (GeditDocumentLoader *loader)
 	return loader->uri;
 }
 
-/* it may return NULL, it's up to gedit-document handle it */
-const gchar *
-gedit_document_loader_get_content_type (GeditDocumentLoader *loader)
-{
-	g_return_val_if_fail (GEDIT_IS_DOCUMENT_LOADER (loader), NULL);
-
-	return GEDIT_DOCUMENT_LOADER_GET_CLASS (loader)->get_content_type (loader);
-}
-
-time_t
-gedit_document_loader_get_mtime (GeditDocumentLoader *loader)
-{
-	g_return_val_if_fail (GEDIT_IS_DOCUMENT_LOADER (loader), 0);
-
-	return GEDIT_DOCUMENT_LOADER_GET_CLASS (loader)->get_mtime (loader);
-}
-
-/* Returns 0 if file size is unknown */
-goffset
-gedit_document_loader_get_file_size (GeditDocumentLoader *loader)
-{
-	g_return_val_if_fail (GEDIT_IS_DOCUMENT_LOADER (loader), 0);
-
-	return GEDIT_DOCUMENT_LOADER_GET_CLASS (loader)->get_file_size (loader);
-}
-
 goffset
 gedit_document_loader_get_bytes_read (GeditDocumentLoader *loader)
 {
@@ -459,22 +451,6 @@ gedit_document_loader_get_bytes_read (GeditDocumentLoader *loader)
 	return GEDIT_DOCUMENT_LOADER_GET_CLASS (loader)->get_bytes_read (loader);
 }
 
-/* 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. */
-gboolean
-gedit_document_loader_get_readonly (GeditDocumentLoader *loader)
-{
-	g_return_val_if_fail (GEDIT_IS_DOCUMENT_LOADER (loader), FALSE);
-
-	/* if configuration says the scheme is not writable, do not query the
-	   actual loader object, and return TRUE */
-	if (!gedit_utils_uri_has_writable_scheme (loader->uri))
-		return TRUE;
-
-	return GEDIT_DOCUMENT_LOADER_GET_CLASS (loader)->get_readonly (loader);
-}
-
 const GeditEncoding *
 gedit_document_loader_get_encoding (GeditDocumentLoader *loader)
 {
@@ -488,3 +464,11 @@ gedit_document_loader_get_encoding (GeditDocumentLoader *loader)
 
 	return loader->auto_detected_encoding;
 }
+
+GFileInfo *
+gedit_document_loader_get_info (GeditDocumentLoader *loader)
+{
+	g_return_val_if_fail (GEDIT_IS_DOCUMENT_LOADER (loader), NULL);
+
+	return loader->info;
+}
diff --git a/gedit/gedit-document-loader.h b/gedit/gedit-document-loader.h
index 97f6a3d..ed8fa13 100644
--- a/gedit/gedit-document-loader.h
+++ b/gedit/gedit-document-loader.h
@@ -62,6 +62,7 @@ struct _GeditDocumentLoader
 	gboolean		  used;
 
 	/* Info on the current file */
+	GFileInfo		 *info;
 	gchar			 *uri;
 	const GeditEncoding	 *encoding;
 	const GeditEncoding	 *metadata_encoding;
@@ -85,11 +86,7 @@ struct _GeditDocumentLoaderClass
 	/* VTable */
 	void			(* load)		(GeditDocumentLoader *loader);
 	gboolean		(* cancel)		(GeditDocumentLoader *loader);
-	const gchar *		(* get_content_type)	(GeditDocumentLoader *loader);
-	time_t			(* get_mtime)		(GeditDocumentLoader *loader);
-	goffset			(* get_file_size)	(GeditDocumentLoader *loader);
 	goffset			(* get_bytes_read)	(GeditDocumentLoader *loader);
-	gboolean		(* get_readonly)	(GeditDocumentLoader *loader);
 };
 
 /*
@@ -123,21 +120,14 @@ GeditDocument		*gedit_document_loader_get_document	(GeditDocumentLoader *loader)
 #define STDIN_URI "stdin:" 
 const gchar		*gedit_document_loader_get_uri		(GeditDocumentLoader *loader);
 
-const gchar		*gedit_document_loader_get_content_type	(GeditDocumentLoader *loader);
-
-time_t 			 gedit_document_loader_get_mtime 	(GeditDocumentLoader *loader);
-
-/* In the case the loader does not know if the file is readonly, for example for most
-remote files, the function returns FALSE */
-gboolean		 gedit_document_loader_get_readonly	(GeditDocumentLoader *loader);
-
 const GeditEncoding	*gedit_document_loader_get_encoding	(GeditDocumentLoader *loader);
 
-/* Returns 0 if file size is unknown */
-goffset			 gedit_document_loader_get_file_size	(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 
+   and also the metadata*/
+GFileInfo		*gedit_document_loader_get_info		(GeditDocumentLoader *loader);
+
 G_END_DECLS
 
 #endif  /* __GEDIT_DOCUMENT_LOADER_H__  */
diff --git a/gedit/gedit-document.c b/gedit/gedit-document.c
index ccbb1bd..ce8500d 100644
--- a/gedit/gedit-document.c
+++ b/gedit/gedit-document.c
@@ -1039,16 +1039,30 @@ document_loader_loaded (GeditDocumentLoader *loader,
 	if (error == NULL)
 	{
 		GtkTextIter iter;
-		const gchar *content_type;
+		GFileInfo *info;
+		const gchar *content_type = NULL;
+		gboolean read_only = FALSE;
 
-		content_type = gedit_document_loader_get_content_type (loader);
+		info = gedit_document_loader_get_info (loader);
 
-		doc->priv->mtime = gedit_document_loader_get_mtime (loader);
+		if (info)
+		{
+			if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE))
+				content_type = g_file_info_get_attribute_string (info,
+										 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
 
-		g_get_current_time (&doc->priv->time_of_last_save_or_load);
+			if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_TIME_MODIFIED))
+				doc->priv->mtime = g_file_info_get_attribute_uint64 (info,
+										     G_FILE_ATTRIBUTE_TIME_MODIFIED);
 
-		set_readonly (doc,
-		      gedit_document_loader_get_readonly (loader));
+			if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE))
+				read_only = !g_file_info_get_attribute_boolean (info,
+										G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE);
+		}
+
+		set_readonly (doc, read_only);
+
+		g_get_current_time (&doc->priv->time_of_last_save_or_load);
 
 		set_encoding (doc, 
 			      gedit_document_loader_get_encoding (loader),
@@ -1134,10 +1148,16 @@ document_loader_loading (GeditDocumentLoader *loader,
 	}
 	else
 	{
-		goffset size;
+		goffset size = 0;
 		goffset read;
+		GFileInfo *info;
+
+		info = gedit_document_loader_get_info (loader);
+
+		if (info && g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
+			size = g_file_info_get_attribute_uint64 (info,
+								 G_FILE_ATTRIBUTE_STANDARD_SIZE);
 
-		size = gedit_document_loader_get_file_size (loader);
 		read = gedit_document_loader_get_bytes_read (loader);
 
 		g_signal_emit (doc, 
diff --git a/gedit/gedit-gio-document-loader.c b/gedit/gedit-gio-document-loader.c
index 1bc65bb..d829f5c 100644
--- a/gedit/gedit-gio-document-loader.c
+++ b/gedit/gedit-gio-document-loader.c
@@ -64,11 +64,7 @@ typedef struct
 
 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_content_type	(GeditDocumentLoader *loader);
-static time_t	    gedit_gio_document_loader_get_mtime		(GeditDocumentLoader *loader);
-static goffset	    gedit_gio_document_loader_get_file_size	(GeditDocumentLoader *loader);
-static goffset	    gedit_gio_document_loader_get_bytes_read	(GeditDocumentLoader *loader);
-static gboolean	    gedit_gio_document_loader_get_readonly	(GeditDocumentLoader *loader);
+static goffset      gedit_gio_document_loader_get_bytes_read	(GeditDocumentLoader *loader);
 
 static void open_async_read (AsyncData *async);
 
@@ -77,7 +73,6 @@ struct _GeditGioDocumentLoaderPrivate
 	/* Info on the current file */
 	GFile            *gfile;
 
-	GFileInfo        *info;
 	goffset           bytes_read;
 
 	/* Handle for remote files */
@@ -103,9 +98,6 @@ gedit_gio_document_loader_finalize (GObject *object)
 		g_cancellable_cancel (priv->cancellable);
 		g_object_unref (priv->cancellable);
 	}
-
-	if (priv->info)
-		g_object_unref (priv->info);
 	
 	if (priv->stream)
 		g_object_unref (priv->stream);
@@ -131,11 +123,7 @@ gedit_gio_document_loader_class_init (GeditGioDocumentLoaderClass *klass)
 
 	loader_class->load = gedit_gio_document_loader_load;
 	loader_class->cancel = gedit_gio_document_loader_cancel;
-	loader_class->get_content_type = gedit_gio_document_loader_get_content_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));
 }
@@ -287,12 +275,14 @@ static void
 finish_query_info (AsyncData *async)
 {
 	GeditGioDocumentLoader *gvloader;
+	GFileInfo *info;
 	
 	gvloader = async->loader;
+	info = GEDIT_DOCUMENT_LOADER (gvloader)->info;
 
 	/* if it's not a regular file, error out... */
-	if (g_file_info_has_attribute (gvloader->priv->info, G_FILE_ATTRIBUTE_STANDARD_TYPE) &&
-	    g_file_info_get_file_type (gvloader->priv->info) != G_FILE_TYPE_REGULAR)
+	if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE) &&
+	    g_file_info_get_file_type (info) != G_FILE_TYPE_REGULAR)
 	{
 		g_set_error (&gvloader->priv->error,
 			     G_IO_ERROR,
@@ -314,6 +304,7 @@ query_info_cb (GFile        *source,
 	       AsyncData    *async)
 {
 	GeditGioDocumentLoader *gvloader;
+	GFileInfo *info;
 	GError *error = NULL;
 
 	gedit_debug (DEBUG_LOADER);
@@ -328,16 +319,18 @@ query_info_cb (GFile        *source,
 	gvloader = async->loader;
 
 	/* finish the info query */
-	gvloader->priv->info = g_file_query_info_finish (gvloader->priv->gfile,
-	                                                 res, 
-	                                                 &error);
+	info = g_file_query_info_finish (gvloader->priv->gfile,
+	                                 res,
+	                                 &error);
 
-	if (gvloader->priv->info == NULL)
+	if (info == NULL)
 	{
 		/* propagate the error and clean up */
 		async_failed (async, error);
 		return;
 	}
+
+	GEDIT_DOCUMENT_LOADER (gvloader)->info = info;
 	
 	finish_query_info (async);
 }
@@ -480,56 +473,6 @@ gedit_gio_document_loader_load (GeditDocumentLoader *loader)
 	open_async_read (async);
 }
 
-static const gchar *
-gedit_gio_document_loader_get_content_type (GeditDocumentLoader *loader)
-{
-	GeditGioDocumentLoader *gvloader = GEDIT_GIO_DOCUMENT_LOADER (loader);
-
-	if (gvloader->priv->info != NULL &&
-	    g_file_info_has_attribute (gvloader->priv->info,
-				       G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE))
-	{
-		return g_file_info_get_content_type (gvloader->priv->info);
-	}
-
-	return NULL;
-}
-
-static time_t
-gedit_gio_document_loader_get_mtime (GeditDocumentLoader *loader)
-{
-	GeditGioDocumentLoader *gvloader = GEDIT_GIO_DOCUMENT_LOADER (loader);
-
-	if (gvloader->priv->info != NULL &&
-	    g_file_info_has_attribute (gvloader->priv->info,
-				       G_FILE_ATTRIBUTE_TIME_MODIFIED))
-	{
-		GTimeVal timeval;
-
-		g_file_info_get_modification_time (gvloader->priv->info, &timeval);
-
-		return timeval.tv_sec;
-	}
-
-	return 0;
-}
-
-/* Returns 0 if file size is unknown */
-static goffset
-gedit_gio_document_loader_get_file_size (GeditDocumentLoader *loader)
-{
-	GeditGioDocumentLoader *gvloader = GEDIT_GIO_DOCUMENT_LOADER (loader);
-
-	if (gvloader->priv->info != NULL &&
-	    g_file_info_has_attribute (gvloader->priv->info,
-				       G_FILE_ATTRIBUTE_STANDARD_SIZE))
-	{
-		return g_file_info_get_size (gvloader->priv->info);
-	}
-
-	return 0;
-}
-
 static goffset
 gedit_gio_document_loader_get_bytes_read (GeditDocumentLoader *loader)
 {
@@ -555,22 +498,3 @@ gedit_gio_document_loader_cancel (GeditDocumentLoader *loader)
 
 	return TRUE;
 }
-
-/* 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 *gvloader = GEDIT_GIO_DOCUMENT_LOADER (loader);
-
-	if (gvloader->priv->info != NULL &&
-	    g_file_info_has_attribute (gvloader->priv->info,
-				       G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE))
-	{
-		return !g_file_info_get_attribute_boolean (gvloader->priv->info,
-							   G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE);
-	}
-
-	return FALSE;
-}



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