summaryrefslogtreecommitdiff
path: root/libnautilus-private/nautilus-file-utilities.c
diff options
context:
space:
mode:
authorA. Walton <awalton@gnome.org>2009-08-10 13:01:09 -0400
committerA. Walton <awalton@gnome.org>2009-08-10 13:01:09 -0400
commit37e7ceca45aea48129cf55ea2583f23330d26b91 (patch)
treeb59135bc583c6ac462c5ec7ffc0f2ea775bb0359 /libnautilus-private/nautilus-file-utilities.c
parent4dd0fdef1d77cb6b9be83348ff5c00293e14a991 (diff)
downloadnautilus-37e7ceca45aea48129cf55ea2583f23330d26b91.tar.gz
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.
Diffstat (limited to 'libnautilus-private/nautilus-file-utilities.c')
-rw-r--r--libnautilus-private/nautilus-file-utilities.c118
1 files changed, 118 insertions, 0 deletions
diff --git a/libnautilus-private/nautilus-file-utilities.c b/libnautilus-private/nautilus-file-utilities.c
index 9f60dd388..aff876df4 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 */