[gthumb] show the current disk free space in the status bar



commit 8420d4fd3e76942f8cb5af509209bfb2dc25e125
Author: Paolo Bacchilega <paobac src gnome org>
Date:   Fri Aug 12 17:10:10 2011 +0200

    show the current disk free space in the status bar
    
    [new feature]

 gthumb/gth-browser.c     |   74 +++++++++++++++++++++-------
 gthumb/gth-file-source.c |  120 +++++++++++++++++++++++++++++++++++++++++++++-
 gthumb/gth-file-source.h |   21 +++++++--
 gthumb/gth-statusbar.c   |    2 +-
 4 files changed, 192 insertions(+), 25 deletions(-)
---
diff --git a/gthumb/gth-browser.c b/gthumb/gth-browser.c
index d55f713..c9e17fd 100644
--- a/gthumb/gth-browser.c
+++ b/gthumb/gth-browser.c
@@ -936,21 +936,40 @@ _gth_browser_get_fast_file_type (GthBrowser *browser,
 }
 
 
+
 static void
-_gth_browser_update_statusbar_list_info (GthBrowser *browser)
+get_free_space_ready_cb (GthFileSource *file_source,
+			 guint64        total_size,
+			 guint64        free_space,
+			 GError        *error,
+			 gpointer       data)
 {
-	GList   *file_list;
-	int      n_total;
-	goffset  size_total;
-	GList   *scan;
-	int      n_selected;
-	goffset  size_selected;
-	GList   *selected;
-	char    *size_total_formatted;
-	char    *size_selected_formatted;
-	char    *text_total;
-	char    *text_selected;
-	char    *text;
+	GthBrowser *browser = data;
+	GList      *file_list;
+	int         n_total;
+	goffset     size_total;
+	GList      *scan;
+	int         n_selected;
+	goffset     size_selected;
+	GList      *selected;
+	char       *size_total_formatted;
+	char       *size_selected_formatted;
+	char       *text_free;
+	char       *text_total;
+	char       *text_selected;
+	GString    *text;
+
+	if (error == NULL) {
+		char *free_space_formatted;
+
+		free_space_formatted = g_format_size_for_display (free_space);
+		text_free = g_strdup_printf (_("%s of free space"), free_space_formatted);
+
+		g_free (free_space_formatted);
+	}
+	else
+		text_free = NULL;
+
 
 	/* total */
 
@@ -987,13 +1006,20 @@ _gth_browser_update_statusbar_list_info (GthBrowser *browser)
 	size_selected_formatted = g_format_size_for_display (size_selected);
 	text_total = g_strdup_printf (g_dngettext (NULL, "%d file (%s)", "%d files (%s)", n_total), n_total, size_total_formatted);
 	text_selected = g_strdup_printf (g_dngettext (NULL, "%d file selected (%s)", "%d files selected (%s)", n_selected), n_selected, size_selected_formatted);
-	text = g_strconcat (text_total,
-			    ((n_selected == 0) ? NULL : ", "),
-			    text_selected,
-			    NULL);
-	gth_statusbar_set_list_info (GTH_STATUSBAR (browser->priv->statusbar), text);
 
-	g_free (text);
+	text = g_string_new (text_total);
+	if (n_selected > 0) {
+		g_string_append (text, STATUSBAR_SEPARATOR);
+		g_string_append (text, text_selected);
+	}
+	if (text_free != NULL) {
+		g_string_append (text, STATUSBAR_SEPARATOR);
+		g_string_append (text, text_free);
+	}
+	gth_statusbar_set_list_info (GTH_STATUSBAR (browser->priv->statusbar), text->str);
+
+	g_string_free (text, TRUE);
+	g_free (text_free);
 	g_free (text_selected);
 	g_free (text_total);
 	g_free (size_selected_formatted);
@@ -1001,6 +1027,16 @@ _gth_browser_update_statusbar_list_info (GthBrowser *browser)
 }
 
 
+static void
+_gth_browser_update_statusbar_list_info (GthBrowser *browser)
+{
+	gth_file_source_get_free_space (browser->priv->location_source,
+					browser->priv->location->file,
+					get_free_space_ready_cb,
+					browser);
+}
+
+
 typedef struct {
 	GthBrowser    *browser;
 	GthFileData   *requested_folder;
diff --git a/gthumb/gth-file-source.c b/gthumb/gth-file-source.c
index 175ba83..8d6bc2b 100644
--- a/gthumb/gth-file-source.c
+++ b/gthumb/gth-file-source.c
@@ -54,7 +54,8 @@ typedef enum {
 	FILE_SOURCE_OP_RENAME,
 	FILE_SOURCE_OP_COPY,
 	FILE_SOURCE_OP_REORDER,
-	FILE_SOURCE_OP_REMOVE
+	FILE_SOURCE_OP_REMOVE,
+	FILE_SOURCE_OP_GET_FREE_SPACE
 } FileSourceOp;
 
 
@@ -140,6 +141,13 @@ typedef struct {
 
 
 typedef struct {
+	GFile              *location;
+	SpaceReadyCallback  callback;
+	gpointer            data;
+} GetFreeSpaceData;
+
+
+typedef struct {
 	GthFileSource *file_source;
 	FileSourceOp   op;
 	union {
@@ -152,6 +160,7 @@ typedef struct {
 		WriteMetadataData  write_metadata;
 		ReadMetadataData   read_metadata;
 		RemoveData         remove;
+		GetFreeSpaceData   get_free_space;
 	} data;
 } FileSourceAsyncOp;
 
@@ -193,6 +202,9 @@ file_source_async_op_free (FileSourceAsyncOp *async_op)
 		_g_object_unref (async_op->data.remove.location);
 		_g_object_list_unref (async_op->data.remove.file_list);
 		break;
+	case FILE_SOURCE_OP_GET_FREE_SPACE:
+		_g_object_unref (async_op->data.get_free_space.location);
+		break;
 	}
 
 	g_free (async_op);
@@ -407,6 +419,25 @@ gth_file_source_queue_remove (GthFileSource *file_source,
 
 
 static void
+gth_file_source_queue_get_free_space (GthFileSource      *file_source,
+				      GFile              *location,
+				      SpaceReadyCallback  callback,
+				      gpointer            data)
+{
+	FileSourceAsyncOp *async_op;
+
+	async_op = g_new0 (FileSourceAsyncOp, 1);
+	async_op->file_source = file_source;
+	async_op->op = FILE_SOURCE_OP_GET_FREE_SPACE;
+	async_op->data.get_free_space.location = g_file_dup (location);
+	async_op->data.get_free_space.callback = callback;
+	async_op->data.get_free_space.data = data;
+
+	file_source->priv->queue = g_list_append (file_source->priv->queue, async_op);
+}
+
+
+static void
 gth_file_source_exec_next_in_queue (GthFileSource *file_source)
 {
 	GList             *head;
@@ -494,6 +525,13 @@ gth_file_source_exec_next_in_queue (GthFileSource *file_source)
 					async_op->data.remove.permanently,
 					async_op->data.remove.parent);
 		break;
+
+	case FILE_SOURCE_OP_GET_FREE_SPACE:
+		gth_file_source_get_free_space (file_source,
+						async_op->data.get_free_space.location,
+						async_op->data.get_free_space.callback,
+						async_op->data.get_free_space.data);
+		break;
 	}
 
 	file_source_async_op_free (async_op);
@@ -581,6 +619,67 @@ base_write_metadata (GthFileSource *file_source,
 }
 
 
+typedef struct {
+	GFile              *location;
+	GthFileSource      *file_source;
+	SpaceReadyCallback  callback;
+	gpointer            data;
+} BaseFreeSpaceData;
+
+
+static void
+get_free_space_ready_cb (GObject      *source_object,
+			 GAsyncResult *res,
+			 gpointer      user_data)
+{
+	BaseFreeSpaceData *free_space_data = user_data;
+	GFileInfo         *info;
+	GError            *error = NULL;
+	guint64            total_size = 0;
+	guint64            free_space = 0;
+
+	info = g_file_query_filesystem_info_finish (free_space_data->location, res, &error);
+	if (info != NULL) {
+		total_size = g_file_info_get_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE);
+		free_space = g_file_info_get_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE);
+		g_object_unref (info);
+	}
+
+	free_space_data->callback (free_space_data->file_source,
+				   total_size,
+				   free_space,
+				   error,
+				   free_space_data->data);
+
+	if (error != NULL)
+		g_error_free (error);
+	g_object_unref (free_space_data->location);
+	g_free (free_space_data);
+}
+
+
+static void
+base_get_free_space (GthFileSource        *file_source,
+		     GFile                *location,
+		     SpaceReadyCallback    callback,
+		     gpointer              data)
+{
+	BaseFreeSpaceData *free_space_data;
+
+	free_space_data = g_new0 (BaseFreeSpaceData, 1);
+	free_space_data->location = g_object_ref (location);
+	free_space_data->file_source = file_source;
+	free_space_data->callback = callback;
+	free_space_data->data = data;
+	g_file_query_filesystem_info_async (location,
+					    G_FILE_ATTRIBUTE_FILESYSTEM_SIZE "," G_FILE_ATTRIBUTE_FILESYSTEM_FREE,
+					    G_PRIORITY_DEFAULT,
+					    file_source->priv->cancellable,
+					    get_free_space_ready_cb,
+					    free_space_data);
+}
+
+
 /* -- base_read_metadata -- */
 
 
@@ -774,6 +873,7 @@ gth_file_source_class_init (GthFileSourceClass *class)
 	class->is_reorderable = base_is_reorderable;
 	class->reorder = base_reorder;
 	class->remove = base_remove;
+	class->get_free_space = base_get_free_space;
 }
 
 
@@ -1256,3 +1356,21 @@ gth_file_source_remove (GthFileSource *file_source,
 	g_cancellable_reset (file_source->priv->cancellable);
 	GTH_FILE_SOURCE_GET_CLASS (G_OBJECT (file_source))->remove (file_source, location, file_list, permanently, parent);
 }
+
+
+void
+gth_file_source_get_free_space (GthFileSource      *file_source,
+				GFile              *location,
+				SpaceReadyCallback  callback,
+				gpointer            data)
+{
+	g_return_if_fail (location != NULL);
+	g_return_if_fail (callback != NULL);
+
+	if (gth_file_source_is_active (file_source)) {
+		gth_file_source_queue_get_free_space (file_source, location, callback, data);
+		return;
+	}
+	g_cancellable_reset (file_source->priv->cancellable);
+	GTH_FILE_SOURCE_GET_CLASS (G_OBJECT (file_source))->get_free_space (file_source, location, callback, data);
+}
diff --git a/gthumb/gth-file-source.h b/gthumb/gth-file-source.h
index 73f6c03..504c00e 100644
--- a/gthumb/gth-file-source.h
+++ b/gthumb/gth-file-source.h
@@ -43,10 +43,15 @@ typedef struct _GthFileSource         GthFileSource;
 typedef struct _GthFileSourcePrivate  GthFileSourcePrivate;
 typedef struct _GthFileSourceClass    GthFileSourceClass;
 
-typedef void (*ListReady) (GthFileSource *file_source,
-			   GList         *files,
-			   GError        *error,
-			   gpointer       data);
+typedef void (*ListReady)          (GthFileSource *file_source,
+				    GList         *files,
+				    GError        *error,
+				    gpointer       data);
+typedef void (*SpaceReadyCallback) (GthFileSource *file_source,
+				    guint64        total_size,
+				    guint64        free_space,
+				    GError        *error,
+				    gpointer       data);
 
 struct _GthFileSource
 {
@@ -127,6 +132,10 @@ struct _GthFileSourceClass
 					       GList                *file_list, /* GthFileData list */
 					       gboolean              permanently,
 					       GtkWindow            *parent);
+	void         (*get_free_space)        (GthFileSource        *file_source,
+					       GFile                *location,
+					       SpaceReadyCallback    callback,
+					       gpointer              data);
 };
 
 GType          gth_file_source_get_type              (void) G_GNUC_CONST;
@@ -211,6 +220,10 @@ void           gth_file_source_remove                (GthFileSource        *file
 		       	       	       	       	      GList                *file_list /* GthFileData list */,
 		       	       	       	       	      gboolean              permanently,
 		       	       	       	       	      GtkWindow            *parent);
+void           gth_file_source_get_free_space        (GthFileSource        *file_source,
+						      GFile                *location,
+						      SpaceReadyCallback    callback,
+						      gpointer              data);
 
 /*< protected >*/
 
diff --git a/gthumb/gth-statusbar.c b/gthumb/gth-statusbar.c
index 13b3a7f..99a23a9 100644
--- a/gthumb/gth-statusbar.c
+++ b/gthumb/gth-statusbar.c
@@ -102,7 +102,7 @@ gth_statusbar_init (GthStatusbar *statusbar)
 	image = gtk_image_new_from_stock (GTK_STOCK_STOP, GTK_ICON_SIZE_MENU);
 	gtk_widget_show (image);
 	gtk_container_add (GTK_CONTAINER (statusbar->priv->stop_button), image);
-	 */
+	*/
 
 	/* Secondary text */
 



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