2011-09-28 13:16:26 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
|
|
|
/*
|
2011-09-06 13:05:40 +00:00
|
|
|
* Copyright 2011 Red Hat, Inc
|
|
|
|
*
|
|
|
|
* 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., 59 Temple Place - Suite 330, Boston, MA
|
|
|
|
* 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const Lang = imports.lang;
|
|
|
|
const UPowerGlib = imports.gi.UPowerGlib;
|
|
|
|
|
|
|
|
const ConsoleKit = imports.gdm.consoleKit;
|
2012-02-11 00:38:41 +00:00
|
|
|
const Systemd = imports.gdm.systemd;
|
|
|
|
|
2011-09-06 13:05:40 +00:00
|
|
|
const PanelMenu = imports.ui.panelMenu;
|
|
|
|
const PopupMenu = imports.ui.popupMenu;
|
|
|
|
|
2011-11-20 14:38:48 +00:00
|
|
|
const PowerMenuButton = new Lang.Class({
|
|
|
|
Name: 'PowerMenuButton',
|
|
|
|
Extends: PanelMenu.SystemStatusButton,
|
2011-09-06 13:05:40 +00:00
|
|
|
|
|
|
|
_init: function() {
|
2011-11-20 14:38:48 +00:00
|
|
|
this.parent('system-shutdown', null);
|
2011-09-06 13:05:40 +00:00
|
|
|
this._upClient = new UPowerGlib.Client();
|
|
|
|
|
2012-03-16 16:07:47 +00:00
|
|
|
if (Systemd.haveSystemd())
|
|
|
|
this._systemdLoginManager = new Systemd.SystemdLoginManager();
|
|
|
|
else
|
|
|
|
this._consoleKitManager = new ConsoleKit.ConsoleKitManager();
|
|
|
|
|
2011-09-06 13:05:40 +00:00
|
|
|
this._createSubMenu();
|
|
|
|
|
|
|
|
this._upClient.connect('notify::can-suspend',
|
|
|
|
Lang.bind(this, this._updateHaveSuspend));
|
|
|
|
this._updateHaveSuspend();
|
|
|
|
|
|
|
|
// ConsoleKit doesn't send notifications when shutdown/reboot
|
|
|
|
// are disabled, so we update the menu item each time the menu opens
|
|
|
|
this.menu.connect('open-state-changed', Lang.bind(this,
|
|
|
|
function(menu, open) {
|
|
|
|
if (open) {
|
|
|
|
this._updateHaveShutdown();
|
|
|
|
this._updateHaveRestart();
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
this._updateHaveShutdown();
|
|
|
|
this._updateHaveRestart();
|
|
|
|
},
|
|
|
|
|
|
|
|
_updateVisibility: function() {
|
2012-03-16 22:53:14 +00:00
|
|
|
let shouldBeVisible = (this._haveSuspend || this._haveShutdown || this._haveRestart);
|
|
|
|
this.actor.visible = shouldBeVisible;
|
2011-09-06 13:05:40 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_updateHaveShutdown: function() {
|
|
|
|
|
2012-02-11 00:38:41 +00:00
|
|
|
if (Systemd.haveSystemd()) {
|
|
|
|
this._systemdLoginManager.CanPowerOffRemote(Lang.bind(this,
|
|
|
|
function(result, error) {
|
|
|
|
if (!error)
|
|
|
|
this._haveShutdown = result != 'no';
|
|
|
|
else
|
|
|
|
this._haveShutdown = false;
|
|
|
|
|
2012-03-16 22:53:14 +00:00
|
|
|
this._powerOffItem.actor.visible = this._haveShutdown;
|
2012-02-11 00:38:41 +00:00
|
|
|
this._updateVisibility();
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
this._consoleKitManager.CanStopRemote(Lang.bind(this,
|
|
|
|
function(result, error) {
|
|
|
|
if (!error)
|
|
|
|
this._haveShutdown = result;
|
|
|
|
else
|
|
|
|
this._haveShutdown = false;
|
|
|
|
|
2012-03-16 22:53:14 +00:00
|
|
|
this._powerOffItem.actor.visible = this._haveShutdown;
|
2012-02-11 00:38:41 +00:00
|
|
|
this._updateVisibility();
|
|
|
|
}));
|
|
|
|
}
|
2011-09-06 13:05:40 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_updateHaveRestart: function() {
|
|
|
|
|
2012-02-11 00:38:41 +00:00
|
|
|
if (Systemd.haveSystemd()) {
|
|
|
|
this._systemdLoginManager.CanRebootRemote(Lang.bind(this,
|
|
|
|
function(result, error) {
|
|
|
|
if (!error)
|
|
|
|
this._haveRestart = result != 'no';
|
|
|
|
else
|
|
|
|
this._haveRestart = false;
|
|
|
|
|
2012-03-16 22:53:14 +00:00
|
|
|
this._restartItem.actor.visible = this._haveRestart;
|
2012-02-11 00:38:41 +00:00
|
|
|
this._updateVisibility();
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
this._consoleKitManager.CanRestartRemote(Lang.bind(this,
|
|
|
|
function(result, error) {
|
|
|
|
if (!error)
|
|
|
|
this._haveRestart = result;
|
|
|
|
else
|
|
|
|
this._haveRestart = false;
|
|
|
|
|
2012-03-16 22:53:14 +00:00
|
|
|
this._restartItem.actor.visible = this._haveRestart;
|
2012-02-11 00:38:41 +00:00
|
|
|
this._updateVisibility();
|
|
|
|
}));
|
|
|
|
}
|
2011-09-06 13:05:40 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_updateHaveSuspend: function() {
|
|
|
|
this._haveSuspend = this._upClient.get_can_suspend();
|
2012-03-16 22:53:14 +00:00
|
|
|
this._suspendItem.actor.visible = this._haveSuspend;
|
2011-09-06 13:05:40 +00:00
|
|
|
this._updateVisibility();
|
|
|
|
},
|
|
|
|
|
|
|
|
_createSubMenu: function() {
|
|
|
|
let item;
|
|
|
|
|
|
|
|
item = new PopupMenu.PopupMenuItem(_("Suspend"));
|
|
|
|
item.connect('activate', Lang.bind(this, this._onActivateSuspend));
|
|
|
|
this.menu.addMenuItem(item);
|
|
|
|
this._suspendItem = item;
|
|
|
|
|
|
|
|
item = new PopupMenu.PopupMenuItem(_("Restart"));
|
|
|
|
item.connect('activate', Lang.bind(this, this._onActivateRestart));
|
|
|
|
this.menu.addMenuItem(item);
|
|
|
|
this._restartItem = item;
|
|
|
|
|
|
|
|
item = new PopupMenu.PopupMenuItem(_("Power Off"));
|
|
|
|
item.connect('activate', Lang.bind(this, this._onActivatePowerOff));
|
|
|
|
this.menu.addMenuItem(item);
|
|
|
|
this._powerOffItem = item;
|
|
|
|
},
|
|
|
|
|
|
|
|
_onActivateSuspend: function() {
|
|
|
|
if (this._haveSuspend)
|
|
|
|
this._upClient.suspend_sync(null);
|
|
|
|
},
|
|
|
|
|
|
|
|
_onActivateRestart: function() {
|
2012-02-11 00:38:41 +00:00
|
|
|
if (!this._haveRestart)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (Systemd.haveSystemd())
|
|
|
|
this._systemdLoginManager.RebootRemote(true);
|
|
|
|
else
|
2011-09-06 13:05:40 +00:00
|
|
|
this._consoleKitManager.RestartRemote();
|
|
|
|
},
|
|
|
|
|
|
|
|
_onActivatePowerOff: function() {
|
2012-02-11 00:38:41 +00:00
|
|
|
if (!this._haveShutdown)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (Systemd.haveSystemd())
|
|
|
|
this._systemdLoginManager.PowerOffRemote(true);
|
|
|
|
else
|
2011-09-06 13:05:40 +00:00
|
|
|
this._consoleKitManager.StopRemote();
|
|
|
|
}
|
2011-11-20 14:38:48 +00:00
|
|
|
});
|