[nautilus] Bug 334806 – Inhibit power-manager when copying files



commit 37e7ceca45aea48129cf55ea2583f23330d26b91
Author: A. Walton <awalton gnome org>
Date:   Mon Aug 10 13:01:09 2009 -0400

    Bug 334806 â?? Inhibit power-manager when copying files
    
    Introduces code to inhibit GNOME power manager
    whenever a file operation which may not complete
    quickly is running.

 libnautilus-private/nautilus-file-operations.c |   27 +++++-
 libnautilus-private/nautilus-file-utilities.c  |  118 ++++++++++++++++++++++++
 libnautilus-private/nautilus-file-utilities.h  |    4 +
 3 files changed, 147 insertions(+), 2 deletions(-)
---
diff --git a/libnautilus-private/nautilus-file-operations.c b/libnautilus-private/nautilus-file-operations.c
index 70f7308..5051c72 100644
--- a/libnautilus-private/nautilus-file-operations.c
+++ b/libnautilus-private/nautilus-file-operations.c
@@ -76,6 +76,7 @@ typedef struct {
 	GTimer *time;
 	GtkWindow *parent_window;
 	int screen_num;
+	int inhibit_cookie;
 	NautilusProgressInfo *progress;
 	GCancellable *cancellable;
 	GHashTable *skip_files;
@@ -915,7 +916,7 @@ init_common (gsize job_size,
 	common->progress = nautilus_progress_info_new ();
 	common->cancellable = nautilus_progress_info_get_cancellable (common->progress);
 	common->time = g_timer_new ();
-
+	common->inhibit_cookie = -1;
 	common->screen_num = 0;
 	if (parent_window) {
 		screen = gtk_widget_get_screen (GTK_WIDGET (parent_window));
@@ -930,8 +931,12 @@ finalize_common (CommonJob *common)
 {
 	nautilus_progress_info_finish (common->progress);
 
-	g_timer_destroy (common->time);
+	if (common->inhibit_cookie != -1) {
+		nautilus_uninhibit_power_manager (common->inhibit_cookie);
+	}
 
+	common->inhibit_cookie = -1;
+	g_timer_destroy (common->time);
 	eel_remove_weak_pointer (&common->parent_window);
 	if (common->skip_files) {
 		g_hash_table_destroy (common->skip_files);
@@ -1243,6 +1248,12 @@ run_question (CommonJob *job,
 }
 
 static void
+inhibit_power_manager (CommonJob *job, const char *message)
+{
+	job->inhibit_cookie = nautilus_inhibit_power_manager (message);
+}
+
+static void
 abort_job (CommonJob *job)
 {
 	g_cancellable_cancel (job->cancellable);
@@ -1931,6 +1942,12 @@ trash_or_delete_internal (GList                  *files,
 	job->user_cancel = FALSE;
 	job->done_callback = done_callback;
 	job->done_callback_data = done_callback_data;
+
+	if (try_trash) {
+		inhibit_power_manager ((CommonJob *)job, _("Trashing Files"));
+	} else {
+		inhibit_power_manager ((CommonJob *)job, _("Deleting Files"));
+	}
 	
 	g_io_scheduler_push_job (delete_job,
 			   job,
@@ -4396,6 +4413,8 @@ nautilus_file_operations_copy (GList *files,
 	}
 	job->debuting_files = g_hash_table_new_full (g_file_hash, (GEqualFunc)g_file_equal, g_object_unref, NULL);
 
+	inhibit_power_manager ((CommonJob *)job, _("Copying Files"));
+
 	g_io_scheduler_push_job (copy_job,
 			   job,
 			   NULL, /* destroy notify */
@@ -4936,6 +4955,8 @@ nautilus_file_operations_move (GList *files,
 	}
 	job->debuting_files = g_hash_table_new_full (g_file_hash, (GEqualFunc)g_file_equal, g_object_unref, NULL);
 
+	inhibit_power_manager ((CommonJob *)job, _("Moving Files"));
+
 	g_io_scheduler_push_job (move_job,
 				 job,
 				 NULL, /* destroy notify */
@@ -6050,6 +6071,8 @@ nautilus_file_operations_empty_trash (GtkWidget *parent_view)
 	job->trash_dirs = g_list_prepend (job->trash_dirs,
 					  g_file_new_for_uri ("trash:"));
 	job->should_confirm = TRUE;
+
+	inhibit_power_manager ((CommonJob *)job, _("Emptying Trash"));
 	
 	g_io_scheduler_push_job (empty_trash_job,
 			   job,
diff --git a/libnautilus-private/nautilus-file-utilities.c b/libnautilus-private/nautilus-file-utilities.c
index 9f60dd3..aff876d 100644
--- a/libnautilus-private/nautilus-file-utilities.c
+++ b/libnautilus-private/nautilus-file-utilities.c
@@ -38,6 +38,7 @@
 #include <glib/gi18n.h>
 #include <glib/gstdio.h>
 #include <gio/gio.h>
+#include <dbus/dbus-glib.h>
 #include <unistd.h>
 #include <stdlib.h>
 
@@ -999,6 +1000,123 @@ nautilus_is_file_roller_installed (void)
 	return installed > 0 ? TRUE : FALSE;
 }
 
+#define GSM_NAME  "org.gnome.SessionManager"
+#define GSM_PATH "/org/gnome/SessionManager"
+
+/* The following values come from
+ * http://www.gnome.org/~mccann/gnome-session/docs/gnome-session.html#org.gnome.SessionManager.Inhibit 
+ */
+#define INHIBIT_LOGOUT 1
+#define INHIBIT_SUSPEND 4
+
+static DBusGProxy *_gsm_proxy = NULL;
+
+static DBusGProxy *
+get_power_manager_proxy (void)
+{
+	if (!_gsm_proxy)
+	{
+		DBusGConnection *bus;
+		GError *error;
+
+		bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
+		if (!bus)
+		{
+			g_warning ("Could not connect to session bus: %s",
+				   error->message);
+			g_error_free (error);
+			return NULL;
+		}
+
+		_gsm_proxy = dbus_g_proxy_new_for_name (bus,
+						        GSM_NAME,
+						        GSM_PATH,
+						        GSM_NAME);
+		dbus_g_connection_unref (bus);
+
+		if (!_gsm_proxy)
+		{
+			g_warning ("Creating DBus proxy failed.");
+			return NULL;
+		}
+	}
+
+	return _gsm_proxy;
+}
+
+/**
+ * nautilus_inhibit_power_manager:
+ * @message: a human readable message for the reason why power management
+ *       is being suspended.
+ *
+ * Inhibits the power manager from logging out or suspending the machine
+ * (e.g. whenever Nautilus is doing file operations).
+ *
+ * Returns: an integer cookie, which must be passed to
+ *    nautilus_uninhibit_power_manager() to resume
+ *    normal power management.
+ */
+int
+nautilus_inhibit_power_manager (const char *message)
+{
+	DBusGProxy *proxy;
+	GError *error;
+	int cookie;
+
+	proxy = get_power_manager_proxy ();
+
+	g_return_val_if_fail (proxy != NULL, -1);
+
+	error = NULL;
+	cookie = -1;
+	if (!dbus_g_proxy_call (proxy, "Inhibit", &error,
+				G_TYPE_STRING, "Nautilus",
+				G_TYPE_UINT, 0,
+				G_TYPE_STRING, message,
+				G_TYPE_UINT, INHIBIT_LOGOUT | INHIBIT_SUSPEND,
+				G_TYPE_INVALID,
+				G_TYPE_UINT, &cookie,
+				G_TYPE_INVALID))
+	{
+		g_warning ("Could not inhibit power management: %s", error->message);
+		g_error_free (error);
+		return -1;
+	}
+
+	return cookie;
+}
+
+/**
+ * nautilus_uninhibit_power_manager:
+ * @cookie: the cookie value returned by nautilus_inhibit_power_manager()
+ *
+ * Uninhibits power management. This function must be called after the task
+ * which inhibited power management has finished, or the system will not
+ * return to normal power management.
+ */
+void
+nautilus_uninhibit_power_manager (gint cookie)
+{
+	DBusGProxy *proxy;
+	GError *error;
+	g_return_if_fail (cookie < 0);
+
+	proxy = get_power_manager_proxy ();
+
+	g_return_if_fail (proxy != NULL);
+
+	error = NULL;
+
+	if (!dbus_g_proxy_call (proxy, "Uninhibit", &error,
+                                G_TYPE_UINT, cookie,
+                                G_TYPE_INVALID,
+                                G_TYPE_INVALID))
+	{
+		g_warning ("Could not uninhibit power management: %s", error->message);
+		g_error_free (error);
+	}
+}
+
 /* Returns TRUE if the file is in XDG_DATA_DIRS or
    in "~/.gnome2/". This is used for deciding
    if a desktop file is "trusted" based on the path */
diff --git a/libnautilus-private/nautilus-file-utilities.h b/libnautilus-private/nautilus-file-utilities.h
index 0482aae..d6cb8c1 100644
--- a/libnautilus-private/nautilus-file-utilities.h
+++ b/libnautilus-private/nautilus-file-utilities.h
@@ -82,6 +82,10 @@ char *   nautilus_get_data_file_path                 (const char *partial_path);
 
 gboolean nautilus_is_file_roller_installed           (void);
 
+/* Inhibit/Uninhibit GNOME Power Manager */
+int    nautilus_inhibit_power_manager                (const char *message) G_GNUC_WARN_UNUSED_RESULT;
+void     nautilus_uninhibit_power_manager            (int cookie);
+
 /* Return an allocated file name that is guranteed to be unique, but
  * tries to make the name readable to users.
  * This isn't race-free, so don't use for security-related things



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