nautilus-cd-burner r2221 - in trunk: . src



Author: mccann
Date: Wed Aug 20 02:26:09 2008
New Revision: 2221
URL: http://svn.gnome.org/viewvc/nautilus-cd-burner?rev=2221&view=rev

Log:
2008-08-19  William Jon McCann  <jmccann redhat com>

	* src/ncb-operation.c (get_sm_proxy), (get_gpm_proxy),
	(inhibit_sm), (uninhibit_sm), (inhibit_gpm), (uninhibit_gpm),
	(inhibit_suspend), (uninhibit_suspend), (burn_cd):
	Clean up the way gpm was inhibited.  Add code to inhibit the
	SessionManager.



Modified:
   trunk/ChangeLog
   trunk/src/ncb-operation.c

Modified: trunk/src/ncb-operation.c
==============================================================================
--- trunk/src/ncb-operation.c	(original)
+++ trunk/src/ncb-operation.c	Wed Aug 20 02:26:09 2008
@@ -54,6 +54,14 @@
 
 #define BURN_URI "burn:///"
 
+#define SM_DBUS_NAME      "org.gnome.SessionManager"
+#define SM_DBUS_PATH      "/org/gnome/SessionManager"
+#define SM_DBUS_INTERFACE "org.gnome.SessionManager"
+
+#define GPM_DBUS_NAME      "org.freedesktop.PowerManagement"
+#define GPM_DBUS_PATH      "/org/freedesktop/PowerManagement/Inhibit"
+#define GPM_DBUS_INTERFACE "org.freedesktop.PowerManagement.Inhibit"
+
 struct NcbOperationPrivate
 {
         NcbOperationDoneFunc           done_cb;
@@ -61,6 +69,9 @@
         guint                          do_idle_id;
         guint                          callback_idle_id;
 
+        guint                          gpm_inhibit_cookie;
+        guint                          sm_inhibit_cookie;
+
         NcbSelection                  *selection;
 
         GtkWidget                     *progress_dialog;
@@ -1590,99 +1601,234 @@
         return TRUE;
 }
 
-static guint
-inhibit_suspend (NcbOperation *operation) {
+static DBusGProxy *
+get_sm_proxy (void)
+{
+        GError          *error;
+        DBusGConnection *bus;
         DBusGProxy      *proxy;
-        DBusGConnection *conn = NULL;
-        gboolean         result;
-        GError          *error = NULL;
-        guint            inhibit_cookie = 0;
 
-        conn = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
+        error = NULL;
+        bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
+        if (bus == NULL) {
+                g_warning ("Cannot connect to session bus: %s", error->message);
+                g_error_free (error);
+                return NULL;
+        }
+
+        proxy = dbus_g_proxy_new_for_name (bus,
+                                           SM_DBUS_NAME,
+                                           SM_DBUS_PATH,
+                                           SM_DBUS_INTERFACE);
+        return proxy;
+}
+
+static DBusGProxy *
+get_gpm_proxy (void)
+{
+        GError          *error;
+        DBusGConnection *bus;
+        DBusGProxy      *proxy;
+
+        error = NULL;
+
+        bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
         if (error != NULL) {
-                g_warning ("DBUS cannot connect : %s", error->message);
+                g_warning ("Cannot connect to session bus: %s", error->message);
                 g_error_free (error);
+                return NULL;
         }
-        if (conn != NULL) {
-                char                 *disc_name;
-                char                 *message;
-                NautilusBurnDrive    *drive;
-                NautilusBurnMediaType media_type;
-
-                proxy = dbus_g_proxy_new_for_name (conn,
-                                                "org.freedesktop.PowerManagement",
-                                                "/org/freedesktop/PowerManagement/Inhibit",
-                                                "org.freedesktop.PowerManagement.Inhibit");
-                if (proxy != NULL) {
-                        ncb_selection_get_label (operation->priv->selection, &disc_name);
-                        drive = ncb_selection_peek_drive (operation->priv->selection);
 
-                        if (nautilus_burn_drive_get_drive_type (drive) == NAUTILUS_BURN_DRIVE_TYPE_FILE) {
-                                message = g_strdup_printf (_("Writing CD/DVD-Image \'%s\'."), disc_name);
-                        }
-                        media_type = nautilus_burn_drive_get_media_type (drive);
-                        if (media_type == NAUTILUS_BURN_MEDIA_TYPE_BUSY
-                                || media_type == NAUTILUS_BURN_MEDIA_TYPE_ERROR) {
-                                message = g_strdup_printf (_("Writing disc \'%s\'."), disc_name);
-                        } else {
-                                message = g_strdup_printf (_("Writing %s \'%s\'."), nautilus_burn_drive_media_type_get_string (media_type), disc_name);
-                        }
+        proxy = dbus_g_proxy_new_for_name (bus,
+                                           GPM_DBUS_NAME,
+                                           GPM_DBUS_PATH,
+                                           GPM_DBUS_INTERFACE);
+
+        return proxy;
+}
+typedef enum {
+        GSM_INHIBITOR_FLAG_LOGOUT      = 1 << 0,
+        GSM_INHIBITOR_FLAG_SWITCH_USER = 1 << 1,
+        GSM_INHIBITOR_FLAG_SUSPEND     = 1 << 2
+} GsmInhibitFlag;
+
+static void
+inhibit_sm (NcbOperation *operation,
+            const char   *reason)
+{
+        GError     *error;
+        gboolean    res;
+        const char *app_id;
+        guint       toplevel_xid;
+        guint       flags;
+        DBusGProxy *proxy;
 
-                        result = dbus_g_proxy_call (proxy,
-                                                    "Inhibit",
-                                                    &error,
-                                                    G_TYPE_STRING,
-                                                    _("CD/DVD Creator"),
-                                                    G_TYPE_STRING,
-                                                    message,
-                                                    G_TYPE_INVALID,
-                                                    G_TYPE_UINT,
-                                                    &inhibit_cookie,
-                                                    G_TYPE_INVALID);
-                        g_free (message);
-                        g_free (disc_name);
-                        if (! result) {
-                                g_warning ("Failed to inhibit the system from suspending: %s.", error->message);
-                                g_error_free (error);
-                        }
-                        g_object_unref (G_OBJECT (proxy));
-                }
+        proxy = get_sm_proxy ();
+        if (proxy == NULL) {
+                return;
         }
-        return inhibit_cookie;
+
+        app_id = "nautilus-cd-burner";
+
+        toplevel_xid = 0;
+        flags = GSM_INHIBITOR_FLAG_LOGOUT
+                | GSM_INHIBITOR_FLAG_SWITCH_USER
+                | GSM_INHIBITOR_FLAG_SUSPEND;
+
+        error = NULL;
+        res = dbus_g_proxy_call (proxy,
+                                 "Inhibit",
+                                 &error,
+                                 G_TYPE_STRING, app_id,
+                                 G_TYPE_UINT, toplevel_xid,
+                                 G_TYPE_STRING, reason,
+                                 G_TYPE_UINT, flags,
+                                 G_TYPE_INVALID,
+                                 G_TYPE_UINT, &operation->priv->sm_inhibit_cookie,
+                                 G_TYPE_INVALID);
+        if (! res) {
+                g_warning ("Failed to inhibit: %s", error->message);
+                g_error_free (error);
+        }
+
+        g_object_unref (proxy);
+
 }
 
 static void
-uninhibit_suspend (guint inhibit_cookie) {
-        DBusGProxy      *proxy;
-        DBusGConnection *conn = NULL;
-        gboolean         result;
-        GError          *error = NULL;
+uninhibit_sm (NcbOperation *operation)
+{
+        GError     *error;
+        gboolean    res;
+        const char *app_id;
+        guint       toplevel_xid;
+        guint       flags;
+        DBusGProxy *proxy;
 
-        conn = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
-        if (error != NULL) {
-                g_warning ("DBUS cannot connect : %s", error->message);
+        proxy = get_sm_proxy ();
+        if (proxy == NULL) {
+                return;
+        }
+
+        app_id = "nautilus-cd-burner";
+
+        toplevel_xid = 0;
+        flags = GSM_INHIBITOR_FLAG_LOGOUT
+                | GSM_INHIBITOR_FLAG_SWITCH_USER
+                | GSM_INHIBITOR_FLAG_SUSPEND;
+
+        error = NULL;
+        res = dbus_g_proxy_call (proxy,
+                                 "Uninhibit",
+                                 &error,
+                                 G_TYPE_UINT, operation->priv->sm_inhibit_cookie,
+                                 G_TYPE_INVALID,
+                                 G_TYPE_INVALID);
+        if (! res) {
+                g_warning ("Failed to uninhibit: %s", error->message);
                 g_error_free (error);
         }
-        if (conn != NULL) {
-                proxy = dbus_g_proxy_new_for_name (conn,
-                                                "org.freedesktop.PowerManagement",
-                                                "/org/freedesktop/PowerManagement/Inhibit",
-                                                "org.freedesktop.PowerManagement.Inhibit");
-                if (proxy != NULL) {
-                        result = dbus_g_proxy_call (proxy,
-                                                    "UnInhibit",
-                                                    &error,
-                                                    G_TYPE_UINT,
-                                                    inhibit_cookie,
-                                                    G_TYPE_INVALID,
-                                                    G_TYPE_INVALID);
-                        if (! result) {
-                                g_warning ("Failed to deactivate suspending inhibition: %s.", error->message);
-                                g_error_free (error);
-                        }
-                        g_object_unref (G_OBJECT (proxy));
+
+        operation->priv->sm_inhibit_cookie = 0;
+        g_object_unref (proxy);
+}
+
+static void
+inhibit_gpm (NcbOperation *operation,
+             const char   *reason)
+{
+        DBusGProxy           *proxy;
+        gboolean              result;
+        GError               *error;
+
+        proxy = get_gpm_proxy ();
+        if (proxy == NULL) {
+                return;
+        }
+
+        result = dbus_g_proxy_call (proxy,
+                                    "Inhibit",
+                                    &error,
+                                    G_TYPE_STRING,
+                                    _("CD/DVD Creator"),
+                                    G_TYPE_STRING,
+                                    reason,
+                                    G_TYPE_INVALID,
+                                    G_TYPE_UINT,
+                                    &operation->priv->gpm_inhibit_cookie,
+                                    G_TYPE_INVALID);
+        if (! result) {
+                g_warning ("Failed to inhibit PowerManager from suspending: %s.", error->message);
+                g_error_free (error);
+        }
+
+        g_object_unref (proxy);
+}
+
+static void
+uninhibit_gpm (NcbOperation *operation)
+{
+        DBusGProxy           *proxy;
+        gboolean              result;
+        GError               *error;
+
+        proxy = get_gpm_proxy ();
+        if (proxy == NULL) {
+                return;
+        }
+
+        result = dbus_g_proxy_call (proxy,
+                                    "UnInhibit",
+                                    &error,
+                                    G_TYPE_UINT,
+                                    operation->priv->gpm_inhibit_cookie,
+                                    G_TYPE_INVALID,
+                                    G_TYPE_INVALID);
+        if (! result) {
+                g_warning ("Failed to uninhibit PowerManager from suspending: %s.", error->message);
+                g_error_free (error);
+        }
+
+        g_object_unref (proxy);
+        operation->priv->gpm_inhibit_cookie = 0;
+}
+
+static void
+inhibit_suspend (NcbOperation *operation)
+{
+        char                 *disc_name;
+        char                 *reason;
+        NautilusBurnDrive    *drive;
+        NautilusBurnMediaType media_type;
+
+        ncb_selection_get_label (operation->priv->selection, &disc_name);
+        drive = ncb_selection_peek_drive (operation->priv->selection);
+
+        if (nautilus_burn_drive_get_drive_type (drive) == NAUTILUS_BURN_DRIVE_TYPE_FILE) {
+                reason = g_strdup_printf (_("Writing CD/DVD-Image \'%s\'."), disc_name);
+        } else {
+                media_type = nautilus_burn_drive_get_media_type (drive);
+                if (media_type == NAUTILUS_BURN_MEDIA_TYPE_BUSY
+                    || media_type == NAUTILUS_BURN_MEDIA_TYPE_ERROR) {
+                        reason = g_strdup_printf (_("Writing disc \'%s\'."), disc_name);
+                } else {
+                        reason = g_strdup_printf (_("Writing %s \'%s\'."), nautilus_burn_drive_media_type_get_string (media_type), disc_name);
                 }
         }
+
+        inhibit_gpm (operation, reason);
+        inhibit_sm (operation, reason);
+
+        g_free (reason);
+        g_free (disc_name);
+}
+
+
+static void
+uninhibit_suspend (NcbOperation *operation)
+{
+        uninhibit_gpm (operation);
+        uninhibit_sm (operation);
 }
 
 static int
@@ -1692,9 +1838,8 @@
         NautilusBurnDrive *drive;
         NcbSelectionSource source_type;
         char              *source_name;
-        int                inhibit_cookie;
 
-        inhibit_cookie = inhibit_suspend (operation);
+        inhibit_suspend (operation);
 
         drive = ncb_selection_peek_drive (operation->priv->selection);
 
@@ -1743,7 +1888,7 @@
 
  out:
 
-        uninhibit_suspend (inhibit_cookie);
+        uninhibit_suspend (operation);
 
         return res;
 }



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