summaryrefslogtreecommitdiff
path: root/tools/gnome-session-save.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/gnome-session-save.c')
-rw-r--r--tools/gnome-session-save.c214
1 files changed, 82 insertions, 132 deletions
diff --git a/tools/gnome-session-save.c b/tools/gnome-session-save.c
index 1c9789cb..ccd98222 100644
--- a/tools/gnome-session-save.c
+++ b/tools/gnome-session-save.c
@@ -2,6 +2,7 @@
* save-session.c - Small program to talk to session manager.
Copyright (C) 1998 Tom Tromey
+ Copyright (C) 2008 Red Hat, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -21,26 +22,23 @@
#include <config.h>
+#include <unistd.h>
+#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <glib/gi18n.h>
#include <gtk/gtk.h>
-#include <libgnomeui/gnome-client.h>
-#include <libgnomeui/gnome-ui-init.h>
-
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
-#include <X11/SM/SMlib.h>
-
#define GSM_SERVICE_DBUS "org.gnome.SessionManager"
#define GSM_PATH_DBUS "/org/gnome/SessionManager"
#define GSM_INTERFACE_DBUS "org.gnome.SessionManager"
/* True if killing. */
-static gboolean zap = FALSE;
+static gboolean do_logout = FALSE;
/* True if we should use dialog boxes */
static gboolean gui = FALSE;
@@ -50,11 +48,9 @@ static gboolean silent = FALSE;
static char *session_name = NULL;
-static IceConn ice_conn = NULL;
-
-static const GOptionEntry options[] = {
+static GOptionEntry options[] = {
{"session-name", 's', 0, G_OPTION_ARG_STRING, &session_name, N_("Set the current session name"), N_("NAME")},
- {"kill", '\0', 0, G_OPTION_ARG_NONE, &zap, N_("Kill session"), NULL},
+ {"kill", '\0', 0, G_OPTION_ARG_NONE, &do_logout, N_("Kill session"), NULL},
{"gui", '\0', 0, G_OPTION_ARG_NONE, &gui, N_("Use dialog boxes for errors"), NULL},
{"silent", '\0', 0, G_OPTION_ARG_NONE, &silent, N_("Do not require confirmation"), NULL},
{NULL}
@@ -63,53 +59,6 @@ static const GOptionEntry options[] = {
static int exit_status = 0;
static void
-ping_reply (IceConn ice_conn,
- IcePointer clientData)
-{
- gtk_main_quit ();
-}
-
-static void
-ice_ping (void)
-{
- /* We can't exit immediately, because the trash mode above
- * might be discarded. So we do the equivalent of an XSync.
- */
- if (ice_conn) {
- IcePing (ice_conn, ping_reply, NULL);
- } else {
- g_warning ("save complete, but we don't have an ice connection.");
- }
-}
-
-static void
-save_complete (GnomeClient* client, gpointer data)
-{
- /* We could expose more of the arguments to the user if we wanted
- to. Some of them aren't particularly useful. Interestingly,
- there is no way to request a shutdown without a save. */
- gnome_client_request_save (client, GNOME_SAVE_BOTH, zap,
- silent ? GNOME_INTERACT_NONE : GNOME_INTERACT_ANY,
- 0, 1);
-
- ice_ping ();
-}
-
-static void
-die_cb (GnomeClient *client,
- gpointer data)
-{
- ice_ping ();
-}
-
-static void
-cancelled_cb (GnomeClient *client,
- gpointer data)
-{
- ice_ping ();
-}
-
-static void
display_error (const char *message)
{
if (gui && !silent) {
@@ -118,6 +67,8 @@ display_error (const char *message)
dialog = gtk_message_dialog_new (NULL, 0, GTK_MESSAGE_ERROR,
GTK_BUTTONS_OK, message);
+ /*gtk_window_set_default_icon_name (GTK_STOCK_SAVE);*/
+
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
} else {
@@ -125,42 +76,17 @@ display_error (const char *message)
}
}
-
-/*
- * we do this so we can pick up our IceConnection to do a ping on
- */
-static void
-ice_connection_watch (IceConn connection,
- IcePointer client_data,
- Bool opening,
- IcePointer *watch_data)
-{
- if (opening) {
- if (!ice_conn) {
- ice_conn = connection;
- } else {
- g_message ("Second ICE connection opened: ignoring.");
- }
- } else {
- if (ice_conn == connection) {
- ice_conn = NULL;
- } else {
- g_message ("Second ICE connection closed: ignoring.");
- }
- }
-}
-
static DBusGProxy *
-get_bus_proxy (DBusGConnection *connection)
+get_sm_proxy (DBusGConnection *connection)
{
- DBusGProxy *bus_proxy;
+ DBusGProxy *sm_proxy;
- bus_proxy = dbus_g_proxy_new_for_name (connection,
+ sm_proxy = dbus_g_proxy_new_for_name (connection,
GSM_SERVICE_DBUS,
GSM_PATH_DBUS,
GSM_INTERFACE_DBUS);
- return bus_proxy;
+ return sm_proxy;
}
static DBusGConnection *
@@ -180,12 +106,11 @@ get_session_bus (void)
}
static void
-set_session_name (GnomeClient *client,
- const char *session_name)
+set_session_name (const char *session_name)
{
DBusGConnection *bus;
- DBusGProxy *bus_proxy = NULL;
- GError *error = NULL;
+ DBusGProxy *sm_proxy;
+ GError *error;
gboolean res;
bus = get_session_bus ();
@@ -195,15 +120,14 @@ set_session_name (GnomeClient *client,
goto out;
}
- bus_proxy = get_bus_proxy (bus);
- dbus_g_connection_unref (bus);
-
- if (bus_proxy == NULL) {
+ sm_proxy = get_sm_proxy (bus);
+ if (sm_proxy == NULL) {
display_error (_("Could not connect to the session manager"));
goto out;
}
- res = dbus_g_proxy_call (bus_proxy,
+ error = NULL;
+ res = dbus_g_proxy_call (sm_proxy,
"SetName",
&error,
G_TYPE_STRING, session_name,
@@ -223,59 +147,85 @@ set_session_name (GnomeClient *client,
}
out:
- if (bus_proxy != NULL) {
- g_object_unref (bus_proxy);
+ if (sm_proxy != NULL) {
+ g_object_unref (sm_proxy);
}
}
-int
-main (int argc, char *argv[])
+static void
+logout_session (gboolean show_confirmation)
{
- GnomeClient *client;
- GOptionContext *goption_context;
+ DBusGConnection *bus;
+ DBusGProxy *sm_proxy;
+ GError *error;
+ gboolean res;
+ guint flags;
- /* Initialize the i18n stuff */
- bindtextdomain (GETTEXT_PACKAGE, LOCALE_DIR);
- bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
- textdomain (GETTEXT_PACKAGE);
+ sm_proxy = NULL;
- IceAddConnectionWatch (ice_connection_watch, NULL);
+ bus = get_session_bus ();
+ if (bus == NULL) {
+ display_error (_("Could not connect to the session manager"));
+ goto out;
+ }
- goption_context = g_option_context_new (N_(" - Save the current session"));
- g_option_context_set_translation_domain (goption_context, GETTEXT_PACKAGE);
- g_option_context_add_main_entries (goption_context, options, GETTEXT_PACKAGE);
+ sm_proxy = get_sm_proxy (bus);
+ if (sm_proxy == NULL) {
+ display_error (_("Could not connect to the session manager"));
+ goto out;
+ }
- gnome_program_init ("gnome-session-save", VERSION,
- LIBGNOMEUI_MODULE, argc, argv,
- GNOME_PARAM_GOPTION_CONTEXT, goption_context,
- NULL);
+ flags = 0;
+ if (! show_confirmation) {
+ flags |= 1;
+ }
- gtk_window_set_default_icon_name (GTK_STOCK_SAVE);
+ error = NULL;
+ res = dbus_g_proxy_call (sm_proxy,
+ "Logout",
+ &error,
+ G_TYPE_UINT, flags,
+ G_TYPE_INVALID,
+ G_TYPE_INVALID);
- client = gnome_master_client ();
+ if (!res) {
+ if (error != NULL) {
+ g_warning ("Failed to call logout: %s",
+ error->message);
+ g_error_free (error);
+ } else {
+ g_warning ("Failed to call logout");
+ }
- if (!GNOME_CLIENT_CONNECTED (client)) {
- display_error (_("Could not connect to the session manager"));
- return 1;
+ goto out;
}
- gnome_client_set_restart_style (client, GNOME_RESTART_NEVER);
+ out:
+ if (sm_proxy != NULL) {
+ g_object_unref (sm_proxy);
+ }
+}
- if (session_name != NULL) {
- set_session_name (client, session_name);
+int
+main (int argc, char *argv[])
+{
+ GError *error;
+
+ /* Initialize the i18n stuff */
+ bindtextdomain (GETTEXT_PACKAGE, LOCALE_DIR);
+ bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
+ textdomain (GETTEXT_PACKAGE);
+
+ error = NULL;
+ if (! gtk_init_with_args (&argc, &argv, NULL, options, NULL, &error)) {
+ g_warning ("Unable to start: %s", error->message);
+ g_error_free (error);
+ exit (1);
}
- /* Wait until our request is acknowledged:
- * gnome-session queues requests but does not honour them if the
- * requesting client is dead when the save starts. */
- g_signal_connect (client, "save_complete",
- G_CALLBACK (save_complete), GINT_TO_POINTER (1));
- g_signal_connect (client, "die",
- G_CALLBACK (die_cb), GINT_TO_POINTER (2));
- g_signal_connect (client, "shutdown_cancelled",
- G_CALLBACK (cancelled_cb), GINT_TO_POINTER (3));
-
- gtk_main ();
+ if (do_logout) {
+ logout_session (! silent);
+ }
return exit_status;
}