You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

236 lines
7.0 KiB

  1. /* $Id$ */
  2. /*-
  3. * Copyright (c) 2004,2008 Brian Tarricone <kelnos@xfce.org>
  4. * Copyright (c) 2004 Benedikt Meurer <benny@xfce.org>
  5. * All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2, or (at your option)
  10. * any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  20. * MA 02110-1301 USA.
  21. */
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. #ifdef HAVE_MEMORY_H
  26. #include <memory.h>
  27. #endif
  28. #include <stdio.h>
  29. #ifdef HAVE_STDLIB_H
  30. #include <stdlib.h>
  31. #endif
  32. #ifdef HAVE_STRING_H
  33. #include <string.h>
  34. #endif
  35. #include <dbus/dbus-glib.h>
  36. #include <dbus/dbus-glib-lowlevel.h>
  37. #include <gtk/gtk.h>
  38. #include <libxfce4ui/libxfce4ui.h>
  39. #include <libxfce4util/libxfce4util.h>
  40. gboolean opt_logout = FALSE;
  41. gboolean opt_halt = FALSE;
  42. gboolean opt_reboot = FALSE;
  43. gboolean opt_suspend = FALSE;
  44. gboolean opt_hibernate = FALSE;
  45. gboolean opt_fast = FALSE;
  46. gboolean opt_version = FALSE;
  47. enum
  48. {
  49. XFSM_SHUTDOWN_ASK = 0,
  50. XFSM_SHUTDOWN_LOGOUT,
  51. XFSM_SHUTDOWN_HALT,
  52. XFSM_SHUTDOWN_REBOOT,
  53. XFSM_SHUTDOWN_SUSPEND,
  54. XFSM_SHUTDOWN_HIBERNATE
  55. };
  56. static GOptionEntry option_entries[] =
  57. {
  58. { "logout", 'l', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &opt_logout,
  59. N_("Log out without displaying the logout dialog"),
  60. NULL
  61. },
  62. { "halt", 'h', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &opt_halt,
  63. N_("Halt without displaying the logout dialog"),
  64. NULL
  65. },
  66. { "reboot", 'r', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &opt_reboot,
  67. N_("Reboot without displaying the logout dialog"),
  68. NULL
  69. },
  70. { "suspend", 's', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &opt_suspend,
  71. N_("Suspend without displaying the logout dialog"),
  72. NULL
  73. },
  74. { "hibernate", 'h', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &opt_hibernate,
  75. N_("Hibernate without displaying the logout dialog"),
  76. NULL
  77. },
  78. { "fast", 'f', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &opt_fast,
  79. N_("Log out quickly; don't save the session"),
  80. NULL
  81. },
  82. { "version", 'V', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &opt_version,
  83. N_("Print version information and exit"),
  84. NULL
  85. },
  86. { NULL }
  87. };
  88. static void
  89. xfce_session_logout_notify_error (const gchar *message,
  90. GError *error,
  91. gboolean have_display)
  92. {
  93. if (G_LIKELY (have_display))
  94. {
  95. xfce_dialog_show_error (NULL, error, "%s", message);
  96. }
  97. else
  98. {
  99. g_printerr (PACKAGE_NAME ": %s (%s).\n", message,
  100. error != NULL ? error->message : _("Unknown error"));
  101. }
  102. }
  103. int
  104. main (int argc, char **argv)
  105. {
  106. DBusGConnection *conn;
  107. DBusGProxy *proxy = NULL;
  108. GError *err = NULL;
  109. gboolean have_display;
  110. gboolean show_dialog;
  111. gboolean result = FALSE;
  112. guint shutdown_type;
  113. gboolean allow_save;
  114. xfce_textdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR, "UTF-8");
  115. have_display = gtk_init_with_args (&argc, &argv, "", option_entries, PACKAGE, NULL);
  116. if (opt_version)
  117. {
  118. g_print ("%s %s (Xfce %s)\n\n", PACKAGE_NAME, PACKAGE_VERSION, xfce_version_string ());
  119. g_print ("%s\n", "Copyright (c) 2004-2012");
  120. g_print ("\t%s\n\n", _("The Xfce development team. All rights reserved."));
  121. g_print ("%s\n", _("Written by Benedikt Meurer <benny@xfce.org>"));
  122. g_print ("%s\n\n", _("and Brian Tarricone <kelnos@xfce.org>."));
  123. g_print (_("Please report bugs to <%s>."), PACKAGE_BUGREPORT);
  124. g_print ("\n");
  125. return EXIT_SUCCESS;
  126. }
  127. /* open session bus */
  128. conn = dbus_g_bus_get (DBUS_BUS_SESSION, &err);
  129. if (conn == NULL)
  130. {
  131. xfce_session_logout_notify_error (_("Unable to contact D-Bus session bus"), err, have_display);
  132. g_error_free (err);
  133. return EXIT_FAILURE;
  134. }
  135. /* save the session, unless fast is provided */
  136. allow_save = !opt_fast;
  137. /* create messsage */
  138. proxy = dbus_g_proxy_new_for_name_owner (conn,
  139. "org.xfce.SessionManager",
  140. "/org/xfce/SessionManager",
  141. "org.xfce.Session.Manager",
  142. &err);
  143. if (proxy != NULL)
  144. {
  145. if (opt_halt)
  146. {
  147. result = dbus_g_proxy_call (proxy, "Shutdown", &err,
  148. G_TYPE_BOOLEAN, allow_save,
  149. G_TYPE_INVALID, G_TYPE_INVALID);
  150. }
  151. else if (opt_reboot)
  152. {
  153. result = dbus_g_proxy_call (proxy, "Restart", &err,
  154. G_TYPE_BOOLEAN, allow_save,
  155. G_TYPE_INVALID, G_TYPE_INVALID);
  156. }
  157. else if (opt_suspend)
  158. {
  159. result = dbus_g_proxy_call (proxy, "Suspend", &err,
  160. G_TYPE_INVALID, G_TYPE_INVALID);
  161. }
  162. else if (opt_hibernate)
  163. {
  164. result = dbus_g_proxy_call (proxy, "Hibernate", &err,
  165. G_TYPE_INVALID, G_TYPE_INVALID);
  166. }
  167. else
  168. {
  169. show_dialog = !opt_logout;
  170. result = dbus_g_proxy_call (proxy, "Logout", &err,
  171. G_TYPE_BOOLEAN, show_dialog,
  172. G_TYPE_BOOLEAN, allow_save,
  173. G_TYPE_INVALID, G_TYPE_INVALID);
  174. }
  175. }
  176. /* fallback for the old 4.8 API dbus, so users can logout if
  177. * they upgraded their system, see bug #8630 */
  178. if (!result && g_error_matches (err, DBUS_GERROR, DBUS_GERROR_UNKNOWN_METHOD))
  179. {
  180. g_clear_error (&err);
  181. if (opt_logout)
  182. shutdown_type = XFSM_SHUTDOWN_LOGOUT;
  183. else if (opt_halt)
  184. shutdown_type = XFSM_SHUTDOWN_HALT;
  185. else if (opt_reboot)
  186. shutdown_type = XFSM_SHUTDOWN_REBOOT;
  187. else if (opt_suspend)
  188. shutdown_type = XFSM_SHUTDOWN_SUSPEND;
  189. else if (opt_hibernate)
  190. shutdown_type = XFSM_SHUTDOWN_HIBERNATE;
  191. else
  192. shutdown_type = XFSM_SHUTDOWN_ASK;
  193. result = dbus_g_proxy_call (proxy, "Shutdown", &err,
  194. G_TYPE_UINT, shutdown_type,
  195. G_TYPE_BOOLEAN, allow_save,
  196. G_TYPE_INVALID, G_TYPE_INVALID);
  197. }
  198. if (proxy != NULL)
  199. g_object_unref (proxy);
  200. if (!result)
  201. {
  202. xfce_session_logout_notify_error (_("Received error while trying to log out"), err, have_display);
  203. g_error_free (err);
  204. return EXIT_FAILURE;
  205. }
  206. return EXIT_SUCCESS;
  207. }