Advertisement
Guest User

EndSessionDialog

a guest
Jun 12th, 2014
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Vala 7.53 KB | None | 0 0
  1. //
  2. //  Copyright (C) 2014 Tom Beckmann
  3. //
  4. //  This program is free software: you can redistribute it and/or modify
  5. //  it under the terms of the GNU General Public License as published by
  6. //  the Free Software Foundation, either version 3 of the License, or
  7. //  (at your option) any later version.
  8. //
  9. //  This program is distributed in the hope that it will be useful,
  10. //  but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. //  GNU General Public License for more details.
  13. //
  14. //  You should have received a copy of the GNU General Public License
  15. //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16. //
  17.  
  18. // compile with: valac --pkg granite EndSessionDialog.vala -X -DGETTEXT_PACKAGE="gala"
  19.  
  20. // docs taken from unity indicator-session's
  21. // src/backend-dbus/org.gnome.SessionManager.EndSessionDialog.xml
  22.  
  23. const string GETTEXT_PACKAGE = "gala";
  24.  
  25. namespace Gala
  26. {
  27.     enum EndSessionDialogType {
  28.         LOGOUT = 0,
  29.         SHUTDOWN = 1,
  30.         RESTART = 2
  31.     }
  32.  
  33.     /**
  34.      * Private class wrapping most of the Gtk part of this dialog
  35.      */
  36.     class Dialog : Gtk.Dialog
  37.     {
  38.         /**
  39.          * Confirm that logout has been clicked
  40.          */
  41.         public signal void confirmed_logout ();
  42.  
  43.         /**
  44.          * Confirm that reboot has been clicked
  45.          */
  46.         public signal void confirmed_reboot ();
  47.  
  48.         /**
  49.          * Confirm that shutdown has been clicked
  50.          */
  51.         public signal void confirmed_shutdown ();
  52.  
  53.         /**
  54.          * Type of the dialog. See constructor for more info.
  55.          */
  56.         public EndSessionDialogType dialog_type { get; construct; }
  57.  
  58.         /**
  59.          * Creates a new shutdown dialog
  60.          *
  61.          * @param type Set the type of this dialog. 0 creates a logout one,
  62.          *             1 creates a shutdown one and 2 will create a combined
  63.          *             shutdown/reboot dialog.
  64.          */
  65.         public Dialog (EndSessionDialogType type)
  66.         {
  67.             Object (dialog_type: type);
  68.         }
  69.  
  70.         construct
  71.         {
  72.             string icon_name, heading_text, button_text;
  73.  
  74.             // the restart type is currently used by the indicator for what is
  75.             // labelled shutdown because of unity's implementation of it
  76.             // apparently. So we got to adjust to that until they fix this.
  77.             switch (dialog_type) {
  78.                 case EndSessionDialogType.LOGOUT:
  79.                     icon_name = "system-log-out";
  80.                     heading_text = _("Are you sure you want to Log Out?");
  81.                     button_text = _("Log Out");
  82.                     break;
  83.                 case EndSessionDialogType.SHUTDOWN:
  84.                 case EndSessionDialogType.RESTART:
  85.                     icon_name = "system-shutdown";
  86.                     heading_text = _("Are you sure you want to Shut Down?");
  87.                     button_text = _("Shut Down");
  88.                     break;
  89.                 /*case EndSessionDialogType.RESTART:
  90.                     icon_name = "system-reboot";
  91.                     heading_text = _("Are you sure you want to Restart?");
  92.                     button_text = _("Restart");
  93.                     break;*/
  94.                 default:
  95.                     warn_if_reached ();
  96.                     break;
  97.             }
  98.  
  99.             set_position (Gtk.WindowPosition.CENTER_ALWAYS);
  100.  
  101.             var heading = new Gtk.Label ("<span weight='bold' size='larger'>" +
  102.                 heading_text + "</span>");
  103.             heading.get_style_context ().add_class ("larger");
  104.             heading.use_markup = true;
  105.             heading.xalign = 0;
  106.  
  107.             var grid = new Gtk.Grid ();
  108.             grid.column_spacing = 12;
  109.             grid.row_spacing = 12;
  110.             grid.margin_left = grid.margin_right = grid.margin_bottom = 12;
  111.             grid.attach (new Gtk.Image.from_icon_name (icon_name, Gtk.IconSize.DIALOG), 0, 0, 1, 2);
  112.             grid.attach (heading, 1, 0, 1, 1);
  113.             grid.attach (new Gtk.Label (_("This will close any open app and turn off your device.")), 1, 1, 1, 1);
  114.  
  115.             // the indicator does not have a separate item for restart, that's
  116.             // why we show both shutdown and restart for the restart action
  117.             // (which is sent for shutdown as described above)
  118.             if (dialog_type == EndSessionDialogType.RESTART) {
  119.                 var confirm_restart = add_button (_("Restart"), Gtk.ResponseType.OK) as Gtk.Button;
  120.                 confirm_restart.clicked.connect (() => {
  121.                     confirmed_reboot ();
  122.                     destroy ();
  123.                 });
  124.             }
  125.  
  126.             var cancel = add_button (_("Cancel"), Gtk.ResponseType.CANCEL) as Gtk.Button;
  127.             cancel.clicked.connect (() => { destroy (); });
  128.  
  129.             var confirm = add_button (button_text, Gtk.ResponseType.OK) as Gtk.Button;
  130.             confirm.get_style_context ().add_class ("destructive-action");
  131.             confirm.clicked.connect (() => {
  132.                 if (dialog_type == EndSessionDialogType.RESTART
  133.                     || dialog_type == EndSessionDialogType.SHUTDOWN)
  134.                     confirmed_shutdown ();
  135.                 else
  136.                     confirmed_logout ();
  137.  
  138.                 destroy ();
  139.             });
  140.             set_default (confirm);
  141.  
  142.             get_content_area ().add (grid);
  143.  
  144.             var action_area = get_action_area ();
  145.             action_area.margin_right = 6;
  146.             action_area.margin_bottom = 6;
  147.         }
  148.     }
  149.  
  150.     [DBus (name = "org.gnome.SessionManager.EndSessionDialog")]
  151.     public class EndSessionDialog : Object
  152.     {
  153.         /**
  154.          * Owns the Unity DBus and registers an instance of the EndSessionDialog
  155.          *
  156.          * @param wm The window manager
  157.          */
  158.         [DBus (visible = false)]
  159.         public static void register ()
  160.         {
  161.             Bus.own_name (BusType.SESSION, "com.canonical.Unity",
  162.                 BusNameOwnerFlags.REPLACE, (connection) => {
  163.                     connection.register_object ("/org/gnome/SessionManager/EndSessionDialog",
  164.                         new EndSessionDialog ());
  165.                 },
  166.                 () => { },
  167.                 () => { warning ("Could not acquire Unity bus."); });
  168.         }
  169.  
  170.         /**
  171.          * Confirm that logout has been clicked
  172.          */
  173.         public signal void confirmed_logout ();
  174.  
  175.         /**
  176.          * Confirm that reboot has been clicked
  177.          */
  178.         public signal void confirmed_reboot ();
  179.  
  180.         /**
  181.          * Confirm that shutdown has been clicked
  182.          */
  183.         public signal void confirmed_shutdown ();
  184.  
  185.         /**
  186.          * The dialog has been cancelled
  187.          */
  188.         public signal void canceled ();
  189.  
  190.         /**
  191.          * The dialog has been closed
  192.          */
  193.         public signal void closed ();
  194.  
  195.         public EndSessionDialog ()
  196.         {
  197.         }
  198.  
  199.         /**
  200.          * This function opens a dialog which asks the user for confirmation
  201.          * a logout, poweroff or reboot action. The dialog has a timeout
  202.          * after which the action is automatically taken, and it should show
  203.          * the inhibitors to the user.
  204.          *
  205.          * @param type                   The type of dialog to show.
  206.          *                               0 for logout, 1 for shutdown, 2 for restart.
  207.          * @param timestamp              Timestamp of the user-initiated event which
  208.          *                               triggered the call, or 0 if the call was not
  209.          *                               triggered by an event.
  210.          * @param seconds_to_stay_open   The number of seconds which the dialog should
  211.          *                               stay open before automatic action is taken.
  212.          * @param inhibitor_object_paths The object paths of all inhibitors that
  213.          *                               are registered for the action.
  214.          */
  215.         public void open (uint type, uint timestamp, uint seconds_to_stay_open,
  216.             ObjectPath[] inhibitor_object_paths) throws IOError
  217.         {
  218.             // note on the implementation: the unity indicator currently does not use
  219.             // the seconds_to_stay_open and inhibitor_object_paths parameters, so we
  220.             // ignore them here for now as well.
  221.  
  222.             if (type > 2)
  223.                 throw new IOError.INVALID_ARGUMENT ("Invalid type requested");
  224.  
  225.             var dialog = new Dialog ((EndSessionDialogType) type);
  226.             dialog.show_all ();
  227.             dialog.destroy.connect (() => {
  228.                 closed ();
  229.             });
  230.             dialog.confirmed_logout.connect (() => { confirmed_logout (); });
  231.             dialog.confirmed_shutdown.connect (() => { confirmed_shutdown (); });
  232.             dialog.confirmed_reboot.connect (() => { confirmed_reboot (); });
  233.         }
  234.     }
  235. }
  236.  
  237. void main (string[] args)
  238. {
  239.     Gtk.init (ref args);
  240.  
  241.     Gala.EndSessionDialog.register ();
  242.  
  243.     Gtk.main ();
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement