Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.74 KB | None | 0 0
  1. /*
  2. A dialog to ask the user about the various logout options that
  3. are available.
  4.  
  5. Copyright 2010 Canonical Ltd.
  6.  
  7. Authors:
  8.     Ted Gould <ted@canonical.com>
  9.  
  10. This program is free software: you can redistribute it and/or modify it
  11. under the terms of the GNU General Public License version 3, as published
  12. by the Free Software Foundation.
  13.  
  14. This program is distributed in the hope that it will be useful, but
  15. WITHOUT ANY WARRANTY; without even the implied warranties of
  16. MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
  17. PURPOSE.  See the GNU General Public License for more details.
  18.  
  19. You should have received a copy of the GNU General Public License along
  20. with this program.  If not, see <http://www.gnu.org/licenses/>.
  21. */
  22.  
  23. #ifdef HAVE_CONFIG_H
  24. #include "config.h"
  25. #endif
  26.  #include <stdio.h>      /* printf */
  27. #include <stdlib.h>
  28. #include <glib/gi18n.h>
  29.  
  30. #include "consolekit-manager-client.h"
  31. #include "dialog.h"
  32.  
  33. /* Strings */
  34. int val;
  35. static const gchar * title_strings[LOGOUT_DIALOG_TYPE_CNT] = {
  36.     /* LOGOUT_DIALOG_LOGOUT, */     NC_("title", "Log Out"),
  37.     /* LOGOUT_DIALOG_RESTART, */    NC_("title", "Restart"),
  38.     /* LOGOUT_DIALOG_SHUTDOWN, */   NC_("title", "Shut Down")
  39. };
  40.  
  41. static const gchar * body_strings[LOGOUT_DIALOG_TYPE_CNT] = {
  42.     /* LOGOUT_DIALOG_LOGOUT, */     N_("Are you sure you want to close all programs and log out of the computer?"),
  43.     /* LOGOUT_DIALOG_RESTART, */    N_("Are you sure you want to close all programs and restart the computer?"),
  44.     /* LOGOUT_DIALOG_SHUTDOWN, */   N_("Are you sure you want to close all programs and shut down the computer?")
  45. };
  46.  
  47. static const gchar * button_strings[LOGOUT_DIALOG_TYPE_CNT] = {
  48.     /* LOGOUT_DIALOG_LOGOUT, */     NC_("button", "Log Out"),
  49.     /* LOGOUT_DIALOG_RESTART, */    NC_("button", "Restart"),
  50.     /* LOGOUT_DIALOG_SHUTDOWN, */   NC_("button", "Shut Down")
  51. };
  52.  
  53. /* TRANSLATORS: These strings have an ellipsis so that the user knows
  54.    they are also going to get a password dialog to do the action. */
  55. static const gchar * button_auth_strings[LOGOUT_DIALOG_TYPE_CNT] = {
  56.     /* LOGOUT_DIALOG_LOGOUT, */     NC_("button auth", "Log Out"),
  57.     /* LOGOUT_DIALOG_RESTART, */    NC_("button auth", "Restart…"),
  58.     /* LOGOUT_DIALOG_SHUTDOWN, */   NC_("button auth", "Shut Down…")
  59. };
  60.  
  61. /* TRANSLATORS: This button appears on the logout dialog when
  62.    there are updates that require restart.  It will do a restart
  63.    in place of a log out. */
  64. static const gchar * restart_updates = N_("Restart Instead");
  65. static const gchar * restart_auth = N_("Restart Instead…");
  66. static const gchar * body_logout_update = N_("Some software updates won’t apply until the computer next restarts.");
  67.  
  68. static const gchar * icon_strings[LOGOUT_DIALOG_TYPE_CNT] = {
  69.     /* LOGOUT_DIALOG_LOGOUT, */     "system-log-out",
  70.     /* LOGOUT_DIALOG_RESTART, */    "system-restart",
  71.     /* LOGOUT_DIALOG_SHUTDOWN, */   "system-shutdown"
  72. };
  73.  
  74. int logout_command(){
  75.     val=system ("killall gnome-session");
  76.         return val;
  77. }
  78. int restart_command(){
  79.     val=system ("/opt/extras.ubuntu.com/power-commands/bin/power restart");
  80.         return val;
  81. }
  82. int shutdown_command(){
  83.     val=system ("/opt/extras.ubuntu.com/power-commands/bin/power shutdown");
  84.         return val;
  85. }
  86.  
  87. typedef struct _LogoutDialogPrivate LogoutDialogPrivate;
  88. struct _LogoutDialogPrivate {
  89.     guint type;
  90. };
  91.  
  92. #define LOGOUT_DIALOG_GET_PRIVATE(o) \
  93. (G_TYPE_INSTANCE_GET_PRIVATE ((o), LOGOUT_DIALOG_TYPE, LogoutDialogPrivate))
  94.  
  95. static void logout_dialog_class_init (LogoutDialogClass *klass);
  96. static void logout_dialog_init       (LogoutDialog *self);
  97. static void logout_dialog_dispose    (GObject *object);
  98. static void logout_dialog_finalize   (GObject *object);
  99.  
  100. G_DEFINE_TYPE (LogoutDialog, logout_dialog, GTK_TYPE_MESSAGE_DIALOG);
  101.  
  102. static void
  103. logout_dialog_class_init (LogoutDialogClass *klass)
  104. {
  105.     GObjectClass *object_class = G_OBJECT_CLASS (klass);
  106.  
  107.     g_type_class_add_private (klass, sizeof (LogoutDialogPrivate));
  108.  
  109.     object_class->dispose = logout_dialog_dispose;
  110.     object_class->finalize = logout_dialog_finalize;
  111.  
  112.     return;
  113. }
  114.  
  115. static void
  116. logout_dialog_init (LogoutDialog *self)
  117. {
  118.  
  119.     return;
  120. }
  121.  
  122. static void
  123. logout_dialog_dispose (GObject *object)
  124. {
  125.  
  126.  
  127.     G_OBJECT_CLASS (logout_dialog_parent_class)->dispose (object);
  128.     return;
  129. }
  130.  
  131. static void
  132. logout_dialog_finalize (GObject *object)
  133. {
  134.  
  135.  
  136.     G_OBJECT_CLASS (logout_dialog_parent_class)->finalize (object);
  137.     return;
  138. }
  139.  
  140. /* Checks for updates that would signal that a restart is
  141.    required for them to apply */
  142. static gboolean
  143. check_restart_required (void)
  144. {
  145.     return g_file_test("/var/run/reboot-required", G_FILE_TEST_EXISTS);
  146. }
  147.  
  148. /* Checks with console kit to see if we can do what we want */
  149. static gboolean
  150. ck_check_allowed (LogoutDialogType type)
  151. {
  152.     DBusGConnection * system_bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL);
  153.     g_return_val_if_fail(system_bus != NULL, TRUE);
  154.  
  155.     DBusGProxy * ck_proxy = dbus_g_proxy_new_for_name (system_bus,
  156.                                                        "org.freedesktop.ConsoleKit",
  157.                                                        "/org/freedesktop/ConsoleKit/Manager",
  158.                                                        "org.freedesktop.ConsoleKit.Manager");
  159.     g_return_val_if_fail(ck_proxy != NULL, TRUE);
  160.  
  161.     gboolean retval = TRUE;
  162.     switch (type) {
  163.     case LOGOUT_DIALOG_TYPE_LOGOUT:
  164.     logout_command();      
  165.     break;
  166.     case LOGOUT_DIALOG_TYPE_RESTART:
  167.     restart_command();     
  168.     break;
  169.     case LOGOUT_DIALOG_TYPE_SHUTDOWN:
  170.         shutdown_command();
  171.         break;
  172.     default:
  173.         break;
  174.     }
  175.  
  176.     g_object_unref(ck_proxy);
  177.  
  178.     return retval;
  179. }
  180.  
  181. LogoutDialog *
  182. logout_dialog_new (LogoutDialogType type)
  183. {
  184.     GtkWidget * image = gtk_image_new_from_icon_name(icon_strings[type], GTK_ICON_SIZE_DIALOG);
  185.     gtk_widget_show(image);
  186.  
  187.     LogoutDialog * dialog = LOGOUT_DIALOG(g_object_new(LOGOUT_DIALOG_TYPE,
  188.                                           /* Window */
  189.                                           "icon-name", icon_strings[type],
  190.                                           "modal", TRUE,
  191.                                           "resizable", FALSE,
  192.                                           "title", g_dpgettext2 (NULL, "title", title_strings[type]),
  193.                                           "window-position", GTK_WIN_POS_CENTER_ALWAYS,
  194.                                           /* Message Dialog */
  195.                                           "buttons", GTK_BUTTONS_NONE,
  196.                                           "image", image,
  197.                                           "message-type", GTK_MESSAGE_OTHER,
  198.                                           "text", _(body_strings[type]),
  199.                                           NULL));
  200.  
  201.     gtk_window_set_keep_above(GTK_WINDOW(dialog), TRUE);
  202.  
  203.     gboolean allowed = FALSE;
  204.     if (type == LOGOUT_DIALOG_TYPE_LOG_OUT) {
  205.         allowed = ck_check_allowed(LOGOUT_DIALOG_TYPE_LOGOUT);//logout_command();
  206.     } else {
  207.         allowed = ck_check_allowed(type);
  208.     }
  209.  
  210.     gboolean restart_required = FALSE;
  211.     if (type == LOGOUT_DIALOG_TYPE_LOG_OUT) {
  212.         restart_required = check_restart_required();
  213.     }
  214.  
  215.     const gchar * button_text;
  216.     if (allowed) {
  217.         button_text = g_dpgettext2 (NULL, "button", button_strings[type]);
  218.     } else {
  219.         button_text = g_dpgettext2 (NULL, "button auth", button_auth_strings[type]);
  220.     }
  221.  
  222.     if (restart_required) {
  223.         const gchar * restart_req;
  224.         if (allowed) {
  225.             restart_req = restart_updates;
  226.         } else {
  227.             restart_req = restart_auth;
  228.         }
  229.  
  230.         g_object_set(dialog, "secondary-text", _(body_logout_update), NULL);
  231.  
  232.         gtk_dialog_add_buttons(GTK_DIALOG(dialog),
  233.                                _(restart_req), GTK_RESPONSE_HELP,
  234.                                _("Cancel"), GTK_RESPONSE_CANCEL,
  235.                                button_text, GTK_RESPONSE_OK,
  236.                                NULL);
  237.     } else {
  238.         gtk_dialog_add_buttons(GTK_DIALOG(dialog),
  239.                                _("Cancel"), GTK_RESPONSE_CANCEL,
  240.                                button_text, GTK_RESPONSE_OK,
  241.                                NULL);
  242.     }
  243.  
  244.   if (type == LOGOUT_DIALOG_TYPE_SHUTDOWN){
  245.     const gchar * restart_text;
  246.         restart_text = g_dpgettext2 (NULL, "button", button_strings[LOGOUT_DIALOG_TYPE_RESTART]);
  247.         gtk_dialog_add_button (GTK_DIALOG(dialog), restart_text, GTK_RESPONSE_HELP);    
  248.   }
  249.  
  250.     gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);
  251.  
  252.         /* The following  is a workaround to fix an issue in GtkMessageDialog
  253.            in which the user can tab through the text in addition to
  254.            the buttons. */
  255.         GtkWidget *message_area = gtk_message_dialog_get_message_area(GTK_MESSAGE_DIALOG(dialog));
  256.         GList *children = gtk_container_get_children(GTK_CONTAINER(message_area));
  257.         GList *l;
  258.  
  259.         for (l = children; l != NULL; l = g_list_next (l))
  260.         {
  261.                 GtkWidget *child = l->data;
  262.                 gtk_widget_set_can_focus(child, FALSE);
  263.         }
  264.  
  265.         g_list_free (children);
  266.  
  267.     return dialog;
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement