summaryrefslogtreecommitdiff
path: root/xfce4-session-logout/main.c
blob: 23ecfdc7277e21216d0eaebca086fb715b95ba4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* $Id$ */
/*-
 * Copyright (c) 2004,2008 Brian Tarricone <kelnos@xfce.org>
 * Copyright (c) 2004 Benedikt Meurer <benny@xfce.org>
 * All rights reserved.
 *
 * 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
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef HAVE_MEMORY_H
#include <memory.h>
#endif
#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif

#include <gio/gio.h>

#include <gtk/gtk.h>
#include <libxfce4ui/libxfce4ui.h>
#include <libxfce4util/libxfce4util.h>


gboolean opt_logout = FALSE;
gboolean opt_halt = FALSE;
gboolean opt_reboot = FALSE;
gboolean opt_suspend = FALSE;
gboolean opt_hibernate = FALSE;
gboolean opt_hybrid_sleep = FALSE;
gboolean opt_switch_user = FALSE;
gboolean opt_fast = FALSE;
gboolean opt_version = FALSE;

enum
{
  XFSM_SHUTDOWN_ASK = 0,
  XFSM_SHUTDOWN_LOGOUT,
  XFSM_SHUTDOWN_HALT,
  XFSM_SHUTDOWN_REBOOT,
  XFSM_SHUTDOWN_SUSPEND,
  XFSM_SHUTDOWN_HIBERNATE,
  XFSM_SHUTDOWN_HYBRID_SLEEP,
  XFSM_SHUTDOWN_SWITCH_USER
};

static GOptionEntry option_entries[] =
{
  { "logout", 'l', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &opt_logout,
    N_("Log out without displaying the logout dialog"),
    NULL
  },
  { "halt", 'h', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &opt_halt,
    N_("Halt without displaying the logout dialog"),
    NULL
  },
  { "reboot", 'r', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &opt_reboot,
    N_("Reboot without displaying the logout dialog"),
    NULL
  },
  { "suspend", 's', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &opt_suspend,
    N_("Suspend without displaying the logout dialog"),
    NULL
  },
  { "hibernate", 'i', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &opt_hibernate,
    N_("Hibernate without displaying the logout dialog"),
    NULL
  },
  { "hybrid-sleep", 'b', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &opt_hybrid_sleep,
    N_("Hybrid Sleep without displaying the logout dialog"),
    NULL
  },
  { "switch-user", 'u', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &opt_switch_user,
    N_("Switch user without displaying the logout dialog"),
    NULL
  },
  { "fast", 'f', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &opt_fast,
    N_("Log out quickly; don't save the session"),
    NULL
  },
  { "version", 'V', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &opt_version,
    N_("Print version information and exit"),
    NULL
  },
  { NULL }
};


static void
xfce_session_logout_notify_error (const gchar *message,
                                  GError      *error,
                                  gboolean     have_display)
{
  if (G_LIKELY (have_display))
    {
      xfce_dialog_show_error (NULL, error, "%s", message);
    }
  else
    {
      g_printerr (PACKAGE_NAME ": %s (%s).\n", message,
                  error != NULL ? error->message : _("Unknown error"));
    }
}



int
main (int argc, char **argv)
{
  GDBusProxy      *proxy = NULL;
  GError          *err = NULL;
  gboolean         have_display;
  gboolean         show_dialog;
  GVariant        *result = NULL;
  guint            shutdown_type;
  gboolean         allow_save;

  xfce_textdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR, "UTF-8");

  have_display = gtk_init_with_args (&argc, &argv, NULL, option_entries, PACKAGE, NULL);

  if (opt_version)
    {
      g_print ("%s %s (Xfce %s)\n\n", PACKAGE_NAME, PACKAGE_VERSION, xfce_version_string ());
      g_print ("%s\n", "Copyright (c) 2004-2020");
      g_print ("\t%s\n\n", _("The Xfce development team. All rights reserved."));
      g_print ("%s\n", _("Written by Benedikt Meurer <benny@xfce.org>"));
      g_print ("%s\n\n", _("and Brian Tarricone <kelnos@xfce.org>."));
      g_print (_("Please report bugs to <%s>."), PACKAGE_BUGREPORT);
      g_print ("\n");
      return EXIT_SUCCESS;
    }

  /* save the session, unless fast is provided */
  allow_save = !opt_fast;

  /* create messsage */
  proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
                                         G_DBUS_PROXY_FLAGS_NONE,
                                         NULL,
                                         "org.xfce.SessionManager",
                                         "/org/xfce/SessionManager",
                                         "org.xfce.Session.Manager",
                                         NULL,
                                         &err);
  if (proxy == NULL)
    {
      xfce_session_logout_notify_error (_("Received error while trying to log out"), err, have_display);
      g_error_free (err);
      return EXIT_FAILURE;
    }

  if (opt_halt)
    {
      result = g_dbus_proxy_call_sync (proxy, "Shutdown",
                                       g_variant_new ("(b)", allow_save),
                                       G_DBUS_CALL_FLAGS_NONE,
                                       -1,
                                       NULL,
                                       &err);
    }
  else if (opt_reboot)
    {
      result = g_dbus_proxy_call_sync (proxy, "Restart",
                                       g_variant_new ("(b)", allow_save),
                                       G_DBUS_CALL_FLAGS_NONE,
                                       -1,
                                       NULL,
                                       &err);
    }
  else if (opt_suspend)
    {
      result = g_dbus_proxy_call_sync (proxy, "Suspend",
                                       g_variant_new("()"),
                                       G_DBUS_CALL_FLAGS_NONE,
                                       -1,
                                       NULL,
                                       &err);
    }
  else if (opt_hibernate)
    {
      result = g_dbus_proxy_call_sync (proxy, "Hibernate",
                                       g_variant_new("()"),
                                       G_DBUS_CALL_FLAGS_NONE,
                                       -1,
                                       NULL,
                                       &err);
    }
  else if (opt_hybrid_sleep)
    {
      result = g_dbus_proxy_call_sync (proxy, "HybridSleep",
                                       g_variant_new("()"),
                                       G_DBUS_CALL_FLAGS_NONE,
                                       -1,
                                       NULL,
                                       &err);
    }
  else if (opt_switch_user)
    {
      result = g_dbus_proxy_call_sync (proxy, "SwitchUser",
                                       g_variant_new("()"),
                                       G_DBUS_CALL_FLAGS_NONE,
                                       -1,
                                       NULL,
                                       &err);
    }
  else
    {
      show_dialog = !opt_logout;
      result = g_dbus_proxy_call_sync (proxy, "Logout",
                                       g_variant_new("(bb)",show_dialog, allow_save),
                                       G_DBUS_CALL_FLAGS_NONE,
                                       -1,
                                       NULL,
                                       &err);
    }

  /* fallback for the old 4.8 API dbus, so users can logout if
   * they upgraded their system, see bug #8630 */
  if (!result)
    {
      if (err != NULL)
        {
          g_print (_("Received error while trying to log out, error was %s"), err->message);
        }
      g_clear_error (&err);

      if (opt_logout)
        shutdown_type = XFSM_SHUTDOWN_LOGOUT;
      else if (opt_halt)
        shutdown_type = XFSM_SHUTDOWN_HALT;
      else if (opt_reboot)
        shutdown_type = XFSM_SHUTDOWN_REBOOT;
      else if (opt_suspend)
        shutdown_type = XFSM_SHUTDOWN_SUSPEND;
      else if (opt_hibernate)
        shutdown_type = XFSM_SHUTDOWN_HIBERNATE;
      else if (opt_hybrid_sleep)
        shutdown_type = XFSM_SHUTDOWN_HYBRID_SLEEP;
      else if (opt_switch_user)
        shutdown_type = XFSM_SHUTDOWN_SWITCH_USER;
      else
        shutdown_type = XFSM_SHUTDOWN_ASK;

      result = g_dbus_proxy_call_sync (proxy, "Shutdown",
                                       g_variant_new("(yb)",shutdown_type, allow_save),
                                       G_DBUS_CALL_FLAGS_NONE,
                                       -1,
                                       NULL,
                                       &err);
    }


  if (proxy != NULL)
    g_object_unref (proxy);

  if (!result)
    {
      xfce_session_logout_notify_error (_("Received error while trying to log out"), err, have_display);
      g_clear_error (&err);
      return EXIT_FAILURE;
    }
  else
    {
      g_variant_unref (result);
    }

  return EXIT_SUCCESS;
}