2011-09-28 13:16:26 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2010-11-15 21:45:17 +00:00
|
|
|
|
|
|
|
const Gio = imports.gi.Gio;
|
|
|
|
const Lang = imports.lang;
|
2013-05-22 18:18:39 +00:00
|
|
|
const UPower = imports.gi.UPowerGlib;
|
2010-11-15 21:45:17 +00:00
|
|
|
|
2013-05-22 18:19:00 +00:00
|
|
|
const Main = imports.ui.main;
|
2010-11-15 21:45:17 +00:00
|
|
|
const PanelMenu = imports.ui.panelMenu;
|
|
|
|
const PopupMenu = imports.ui.popupMenu;
|
|
|
|
|
2013-10-17 14:45:17 +00:00
|
|
|
const BUS_NAME = 'org.freedesktop.UPower';
|
|
|
|
const OBJECT_PATH = '/org/freedesktop/UPower/devices/DisplayDevice';
|
|
|
|
|
2013-10-24 21:51:58 +00:00
|
|
|
const DisplayDeviceInterface = '<node> \
|
|
|
|
<interface name="org.freedesktop.UPower.Device"> \
|
|
|
|
<property name="Type" type="u" access="read"/> \
|
|
|
|
<property name="State" type="u" access="read"/> \
|
|
|
|
<property name="Percentage" type="d" access="read"/> \
|
|
|
|
<property name="TimeToEmpty" type="x" access="read"/> \
|
|
|
|
<property name="TimeToFull" type="x" access="read"/> \
|
|
|
|
<property name="IsPresent" type="b" access="read"/> \
|
|
|
|
<property name="IconName" type="s" access="read"/> \
|
|
|
|
</interface> \
|
|
|
|
</node>';
|
2011-08-16 12:28:53 +00:00
|
|
|
|
2013-10-17 14:45:17 +00:00
|
|
|
const PowerManagerProxy = Gio.DBusProxy.makeProxyWrapper(DisplayDeviceInterface);
|
2010-11-15 21:45:17 +00:00
|
|
|
|
2011-11-20 14:38:48 +00:00
|
|
|
const Indicator = new Lang.Class({
|
|
|
|
Name: 'PowerIndicator',
|
2013-06-06 21:27:25 +00:00
|
|
|
Extends: PanelMenu.SystemIndicator,
|
2010-11-15 21:45:17 +00:00
|
|
|
|
|
|
|
_init: function() {
|
2013-06-06 21:27:25 +00:00
|
|
|
this.parent();
|
|
|
|
|
2013-07-19 10:05:47 +00:00
|
|
|
this._indicator = this._addIndicator();
|
2011-11-20 14:38:48 +00:00
|
|
|
|
2013-10-17 14:45:17 +00:00
|
|
|
this._proxy = new PowerManagerProxy(Gio.DBus.system, BUS_NAME, OBJECT_PATH,
|
2013-05-22 18:11:34 +00:00
|
|
|
Lang.bind(this, function(proxy, error) {
|
|
|
|
if (error) {
|
|
|
|
log(error.message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._proxy.connect('g-properties-changed',
|
2013-05-22 18:19:00 +00:00
|
|
|
Lang.bind(this, this._sync));
|
|
|
|
this._sync();
|
2013-05-22 18:11:34 +00:00
|
|
|
}));
|
|
|
|
|
2013-10-17 14:45:17 +00:00
|
|
|
this._item = new PopupMenu.PopupSubMenuMenuItem("", true);
|
2013-05-22 18:19:00 +00:00
|
|
|
this._item.menu.addSettingsAction(_("Power Settings"), 'gnome-power-panel.desktop');
|
|
|
|
this.menu.addMenuItem(this._item);
|
|
|
|
|
|
|
|
Main.sessionMode.connect('updated', Lang.bind(this, this._sessionUpdated));
|
|
|
|
this._sessionUpdated();
|
|
|
|
},
|
|
|
|
|
|
|
|
_sessionUpdated: function() {
|
|
|
|
let sensitive = !Main.sessionMode.isLocked && !Main.sessionMode.isGreeter;
|
|
|
|
this.menu.setSensitive(sensitive);
|
|
|
|
},
|
|
|
|
|
2013-10-17 14:45:17 +00:00
|
|
|
_getStatus: function() {
|
|
|
|
let seconds = 0;
|
2013-05-22 18:19:00 +00:00
|
|
|
|
2013-10-17 14:45:17 +00:00
|
|
|
if (this._proxy.State == UPower.DeviceState.FULLY_CHARGED)
|
2013-05-22 18:19:00 +00:00
|
|
|
return _("Fully Charged");
|
2013-10-17 14:45:17 +00:00
|
|
|
else if (this._proxy.State == UPower.DeviceState.CHARGING)
|
|
|
|
seconds = this._proxy.TimeToFull;
|
2013-10-17 16:11:06 +00:00
|
|
|
else if (this._proxy.State == UPower.DeviceState.DISCHARGING)
|
2013-10-17 14:45:17 +00:00
|
|
|
seconds = this._proxy.TimeToEmpty;
|
|
|
|
// state is one of PENDING_CHARGING, PENDING_DISCHARGING
|
|
|
|
else
|
|
|
|
return _("Estimating…");
|
2013-05-22 18:19:00 +00:00
|
|
|
|
|
|
|
let time = Math.round(seconds / 60);
|
|
|
|
if (time == 0) {
|
|
|
|
// 0 is reported when UPower does not have enough data
|
|
|
|
// to estimate battery life
|
|
|
|
return _("Estimating…");
|
|
|
|
}
|
|
|
|
|
|
|
|
let minutes = time % 60;
|
|
|
|
let hours = Math.floor(time / 60);
|
|
|
|
|
2013-10-19 15:59:32 +00:00
|
|
|
if (this._proxy.State == UPower.DeviceState.DISCHARGING) {
|
2013-05-22 18:19:00 +00:00
|
|
|
// Translators: this is <hours>:<minutes> Remaining (<percentage>)
|
2013-10-19 15:59:32 +00:00
|
|
|
return _("%d\u2236%02d Remaining (%d%%)").format(hours, minutes, this._proxy.Percentage);
|
2013-05-22 18:19:00 +00:00
|
|
|
}
|
|
|
|
|
2013-10-19 15:59:32 +00:00
|
|
|
if (this._proxy.State == UPower.DeviceState.CHARGING) {
|
2013-05-22 18:19:00 +00:00
|
|
|
// Translators: this is <hours>:<minutes> Until Full (<percentage>)
|
2013-10-19 15:59:32 +00:00
|
|
|
return _("%d\u2236%02d Until Full (%d%%)").format(hours, minutes, this._proxy.Percentage);
|
2013-05-22 18:19:00 +00:00
|
|
|
}
|
|
|
|
|
2013-10-17 14:45:17 +00:00
|
|
|
return null;
|
2010-11-15 21:45:17 +00:00
|
|
|
},
|
|
|
|
|
2013-10-10 15:11:24 +00:00
|
|
|
_sync: function() {
|
2013-10-17 14:45:17 +00:00
|
|
|
// Do we have batteries or a UPS?
|
|
|
|
let visible = this._proxy.IsPresent;
|
|
|
|
if (visible) {
|
|
|
|
this._item.actor.show();
|
|
|
|
} else {
|
|
|
|
// If there's no battery, then we use the power icon.
|
|
|
|
this._item.actor.hide();
|
|
|
|
this._indicator.icon_name = 'system-shutdown-symbolic';
|
|
|
|
return;
|
2013-10-10 15:11:24 +00:00
|
|
|
}
|
|
|
|
|
2013-10-17 14:45:17 +00:00
|
|
|
// The icons
|
|
|
|
let icon = this._proxy.IconName;
|
|
|
|
this._indicator.icon_name = icon;
|
|
|
|
this._item.icon.icon_name = icon;
|
|
|
|
|
|
|
|
// The status label
|
|
|
|
this._item.status.text = this._getStatus();
|
|
|
|
|
|
|
|
// The sub-menu heading
|
|
|
|
if (this._proxy.Type == UPower.DeviceKind.UPS)
|
|
|
|
this._item.label.text = _("UPS");
|
|
|
|
else
|
|
|
|
this._item.label.text = _("Battery");
|
2010-11-15 21:45:17 +00:00
|
|
|
},
|
2011-11-20 13:10:48 +00:00
|
|
|
});
|