~indicator-applet-developers/indicator-session/trunk.0.1

« back to all changes in this revision

Viewing changes to src/gtk-dialog/gtk-logout-helper.c

  • Committer: Ted Gould
  • Date: 2009-07-27 04:47:27 UTC
  • mfrom: (9.1.94 fusa.libindicator)
  • Revision ID: ted@canonical.com-20090727044727-m04ul5d8sxqt2pd4
Merging in the branch to port everything to libindicator and fleshing thigns out.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
#include <glib.h>
 
3
#include <gtk/gtk.h>
 
4
#include <dbus/dbus-glib.h>
 
5
#include "logout-dialog.h"
 
6
#include "ck-pk-helper.h"
 
7
 
 
8
static void
 
9
session_action (LogoutDialogAction action)
 
10
{
 
11
        DBusGConnection * sbus;
 
12
        DBusGProxy * sm_proxy;
 
13
        GError * error = NULL;
 
14
        gboolean res = FALSE;
 
15
        
 
16
        sbus = dbus_g_bus_get(DBUS_BUS_SESSION, NULL); 
 
17
        if (sbus == NULL) {
 
18
                g_warning("Unable to get DBus session bus.");
 
19
                return;
 
20
        }
 
21
        sm_proxy = dbus_g_proxy_new_for_name_owner (sbus,
 
22
                                                "org.gnome.SessionManager",
 
23
                                                "/org/gnome/SessionManager",
 
24
                                                "org.gnome.SessionManager",
 
25
                                                        &error);
 
26
        if (sm_proxy == NULL) {
 
27
                g_warning("Unable to get DBus proxy to SessionManager interface: %s", error->message);
 
28
                g_error_free(error);
 
29
                return;
 
30
        }               
 
31
        
 
32
        g_clear_error (&error);
 
33
        
 
34
        if (action == LOGOUT_DIALOG_LOGOUT) {
 
35
                res = dbus_g_proxy_call_with_timeout (sm_proxy, "Logout", INT_MAX, &error, 
 
36
                                                                                          G_TYPE_UINT, 1, G_TYPE_INVALID, G_TYPE_INVALID);
 
37
        } else if (action == LOGOUT_DIALOG_SHUTDOWN) {
 
38
                res = dbus_g_proxy_call_with_timeout (sm_proxy, "RequestShutdown", INT_MAX, &error, 
 
39
                                                                                          G_TYPE_INVALID, G_TYPE_INVALID);
 
40
        } else if (action == LOGOUT_DIALOG_RESTART) {
 
41
                res = dbus_g_proxy_call_with_timeout (sm_proxy, "RequestReboot", INT_MAX, &error, 
 
42
                                                                                          G_TYPE_INVALID, G_TYPE_INVALID);
 
43
        } else {
 
44
                g_warning ("Unknown session action");
 
45
        }
 
46
        
 
47
        if (!res) {
 
48
                if (error != NULL) {
 
49
                        g_warning ("SessionManager action failed: %s", error->message);
 
50
                } else {
 
51
                        g_warning ("SessionManager action failed: unknown error");
 
52
                }
 
53
        }
 
54
        
 
55
        g_object_unref(sm_proxy);
 
56
        
 
57
        if (error != NULL) {
 
58
                g_error_free(error);
 
59
        }
 
60
        
 
61
        return;
 
62
}       
 
63
 
 
64
static LogoutDialogAction type = LOGOUT_DIALOG_LOGOUT;
 
65
 
 
66
static gboolean
 
67
option_logout (const gchar * arg, const gchar * value, gpointer data, GError * error)
 
68
{
 
69
        type = LOGOUT_DIALOG_LOGOUT;
 
70
        return TRUE;
 
71
}
 
72
 
 
73
static gboolean
 
74
option_shutdown (const gchar * arg, const gchar * value, gpointer data, GError * error)
 
75
{
 
76
        type = LOGOUT_DIALOG_SHUTDOWN;
 
77
        return TRUE;
 
78
}
 
79
 
 
80
static gboolean
 
81
option_restart (const gchar * arg, const gchar * value, gpointer data, GError * error)
 
82
{
 
83
        type = LOGOUT_DIALOG_RESTART;
 
84
        return TRUE;
 
85
}
 
86
 
 
87
static GOptionEntry options[] = {
 
88
        {"logout",     'l',  G_OPTION_FLAG_NO_ARG,  G_OPTION_ARG_CALLBACK,  option_logout,   "Log out of the current session",   NULL},
 
89
        {"shutdown",   's',  G_OPTION_FLAG_NO_ARG,  G_OPTION_ARG_CALLBACK,  option_shutdown, "Shutdown the entire system",       NULL},
 
90
        {"restart",    'r',  G_OPTION_FLAG_NO_ARG,  G_OPTION_ARG_CALLBACK,  option_restart,  "Restart the system",               NULL},
 
91
 
 
92
        {NULL}
 
93
};
 
94
 
 
95
int
 
96
main (int argc, char * argv[])
 
97
{
 
98
        gtk_init(&argc, &argv);
 
99
 
 
100
        GError * error = NULL;
 
101
        GOptionContext * context = g_option_context_new(" - logout of the current session");
 
102
        g_option_context_add_main_entries(context, options, "gtk-logout-helper");
 
103
        g_option_context_add_group(context, gtk_get_option_group(TRUE));
 
104
        g_option_context_set_help_enabled(context, TRUE);
 
105
 
 
106
        if (!g_option_context_parse(context, &argc, &argv, &error)) {
 
107
                g_debug("Option parsing failed: %s", error->message);
 
108
                g_error_free(error);
 
109
                return 1;
 
110
        }
 
111
 
 
112
        GtkWidget * dialog = NULL;
 
113
        if (!pk_require_auth(type)) {   
 
114
                dialog = logout_dialog_new(type);
 
115
        }
 
116
 
 
117
        if (dialog != NULL) {
 
118
                GtkResponseType response = gtk_dialog_run(GTK_DIALOG(dialog));
 
119
                gtk_widget_hide(dialog);
 
120
 
 
121
                if (response == GTK_RESPONSE_HELP) {
 
122
                        type = LOGOUT_DIALOG_RESTART;
 
123
                        response = GTK_RESPONSE_OK;
 
124
                }
 
125
 
 
126
                if (response != GTK_RESPONSE_OK) {
 
127
                        return 0;
 
128
                }
 
129
        }
 
130
 
 
131
        session_action(type);
 
132
 
 
133
        return 0;
 
134
}