2011-09-28 09:16:26 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2010-11-15 16:45:17 -05:00
|
|
|
|
|
|
|
const Gio = imports.gi.Gio;
|
|
|
|
const Lang = imports.lang;
|
2013-05-22 14:18:39 -04:00
|
|
|
const UPower = imports.gi.UPowerGlib;
|
2010-11-15 16:45:17 -05:00
|
|
|
|
2013-05-22 14:19:00 -04:00
|
|
|
const Main = imports.ui.main;
|
2010-11-15 16:45:17 -05:00
|
|
|
const PanelMenu = imports.ui.panelMenu;
|
|
|
|
const PopupMenu = imports.ui.popupMenu;
|
|
|
|
|
2012-12-19 11:06:20 -05:00
|
|
|
const BUS_NAME = 'org.gnome.SettingsDaemon.Power';
|
2011-07-04 07:09:06 -04:00
|
|
|
const OBJECT_PATH = '/org/gnome/SettingsDaemon/Power';
|
2010-11-15 16:45:17 -05:00
|
|
|
|
2011-08-16 08:28:53 -04:00
|
|
|
const PowerManagerInterface = <interface name="org.gnome.SettingsDaemon.Power">
|
|
|
|
<method name="GetDevices">
|
|
|
|
<arg type="a(susdut)" direction="out" />
|
|
|
|
</method>
|
|
|
|
<method name="GetPrimaryDevice">
|
|
|
|
<arg type="(susdut)" direction="out" />
|
|
|
|
</method>
|
|
|
|
<property name="Icon" type="s" access="read" />
|
|
|
|
</interface>;
|
|
|
|
|
|
|
|
const PowerManagerProxy = Gio.DBusProxy.makeProxyWrapper(PowerManagerInterface);
|
2010-11-15 16:45:17 -05:00
|
|
|
|
2011-11-20 09:38:48 -05:00
|
|
|
const Indicator = new Lang.Class({
|
|
|
|
Name: 'PowerIndicator',
|
2013-06-06 17:27:25 -04:00
|
|
|
Extends: PanelMenu.SystemIndicator,
|
2010-11-15 16:45:17 -05:00
|
|
|
|
|
|
|
_init: function() {
|
2013-06-06 17:27:25 -04:00
|
|
|
this.parent();
|
|
|
|
|
2013-07-19 06:05:47 -04:00
|
|
|
this._indicator = this._addIndicator();
|
2011-11-20 09:38:48 -05:00
|
|
|
|
2013-01-28 10:38:45 -05:00
|
|
|
this._proxy = new PowerManagerProxy(Gio.DBus.session, BUS_NAME, OBJECT_PATH,
|
2013-05-22 14:11:34 -04:00
|
|
|
Lang.bind(this, function(proxy, error) {
|
|
|
|
if (error) {
|
|
|
|
log(error.message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._proxy.connect('g-properties-changed',
|
2013-05-22 14:19:00 -04:00
|
|
|
Lang.bind(this, this._sync));
|
|
|
|
this._sync();
|
2013-05-22 14:11:34 -04:00
|
|
|
}));
|
|
|
|
|
2013-05-22 14:19:00 -04:00
|
|
|
this._item = new PopupMenu.PopupSubMenuMenuItem(_("Battery"), true);
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
|
|
|
|
_statusForDevice: function(device) {
|
|
|
|
let [device_id, device_type, icon, percentage, state, seconds] = device;
|
|
|
|
|
|
|
|
if (state == UPower.DeviceState.FULLY_CHARGED)
|
|
|
|
return _("Fully Charged");
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (state == UPower.DeviceState.DISCHARGING) {
|
|
|
|
// Translators: this is <hours>:<minutes> Remaining (<percentage>)
|
2013-09-05 10:01:05 -04:00
|
|
|
return _("%d\u2236%02d Remaining (%d%%)").format(hours, minutes, percentage);
|
2013-05-22 14:19:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (state == UPower.DeviceState.CHARGING) {
|
|
|
|
// Translators: this is <hours>:<minutes> Until Full (<percentage>)
|
2013-09-05 10:01:05 -04:00
|
|
|
return _("%d\u2236%02d Until Full (%d%%)").format(hours, minutes, percentage);
|
2013-05-22 14:19:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// state is one of PENDING_CHARGING, PENDING_DISCHARGING
|
|
|
|
return _("Estimating…");
|
2010-11-15 16:45:17 -05:00
|
|
|
},
|
|
|
|
|
2013-10-10 11:11:24 -04:00
|
|
|
_sync: function() {
|
|
|
|
function isBattery(result) {
|
|
|
|
if (!result)
|
|
|
|
return false;
|
2013-05-22 14:11:34 -04:00
|
|
|
|
2013-05-22 14:19:00 -04:00
|
|
|
let [device] = result;
|
2013-10-10 11:11:24 -04:00
|
|
|
let [, deviceType] = device;
|
|
|
|
return (deviceType == UPower.DeviceKind.BATTERY);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._proxy.GetPrimaryDeviceRemote(Lang.bind(this, function(result, error) {
|
|
|
|
if (isBattery(result)) {
|
|
|
|
let [device] = result;
|
|
|
|
let [,, icon] = device;
|
|
|
|
let gicon = Gio.icon_new_for_string(icon);
|
|
|
|
this._indicator.gicon = gicon;
|
|
|
|
this._item.icon.gicon = gicon;
|
2013-05-22 14:19:00 -04:00
|
|
|
this._item.status.text = this._statusForDevice(device);
|
|
|
|
this._item.actor.show();
|
2010-11-15 16:45:17 -05:00
|
|
|
} else {
|
2013-10-10 11:11:24 -04:00
|
|
|
// If there's no battery, then we use the power icon.
|
|
|
|
this._indicator.icon_name = 'system-shutdown-symbolic';
|
2013-05-22 14:19:00 -04:00
|
|
|
this._item.actor.hide();
|
2010-11-15 16:45:17 -05:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
},
|
2011-11-20 08:10:48 -05:00
|
|
|
});
|