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',
|
|
|
|
Extends: PanelMenu.SystemStatusButton,
|
2010-11-15 16:45:17 -05:00
|
|
|
|
|
|
|
_init: function() {
|
2012-05-30 09:58:37 -04:00
|
|
|
this.parent('battery-missing-symbolic', _("Battery"));
|
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>)
|
|
|
|
return _("%d\u2236%d Remaining (%d%%)".format(hours, minutes, percentage));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state == UPower.DeviceState.CHARGING) {
|
|
|
|
// Translators: this is <hours>:<minutes> Until Full (<percentage>)
|
|
|
|
return _("%d\u2236%d Until Full (%d%%)".format(hours, minutes, percentage));
|
|
|
|
}
|
|
|
|
|
|
|
|
// state is one of PENDING_CHARGING, PENDING_DISCHARGING
|
|
|
|
return _("Estimating…");
|
2010-11-15 16:45:17 -05:00
|
|
|
},
|
|
|
|
|
2013-05-22 14:19:00 -04:00
|
|
|
_syncStatusLabel: function() {
|
2011-08-16 08:28:53 -04:00
|
|
|
this._proxy.GetPrimaryDeviceRemote(Lang.bind(this, function(result, error) {
|
2010-11-15 16:45:17 -05:00
|
|
|
if (error) {
|
2013-05-22 14:19:00 -04:00
|
|
|
this._item.actor.hide();
|
2010-11-15 16:45:17 -05:00
|
|
|
return;
|
|
|
|
}
|
2013-05-22 14:11:34 -04:00
|
|
|
|
2013-05-22 14:19:00 -04:00
|
|
|
let [device] = result;
|
|
|
|
let [device_id, device_type] = device;
|
2013-05-22 14:18:39 -04:00
|
|
|
if (device_type == UPower.DeviceKind.BATTERY) {
|
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-05-22 14:19:00 -04:00
|
|
|
this._item.actor.hide();
|
2010-11-15 16:45:17 -05:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
2012-08-26 10:05:46 -04:00
|
|
|
_syncIcon: function() {
|
2011-08-16 08:28:53 -04:00
|
|
|
let icon = this._proxy.Icon;
|
2012-08-31 22:21:34 -04:00
|
|
|
let hasIcon = false;
|
2012-08-26 10:05:46 -04:00
|
|
|
|
2012-08-31 22:21:34 -04:00
|
|
|
if (icon) {
|
2011-08-16 08:28:53 -04:00
|
|
|
let gicon = Gio.icon_new_for_string(icon);
|
|
|
|
this.setGIcon(gicon);
|
2013-05-22 14:19:00 -04:00
|
|
|
this._item.icon.gicon = gicon;
|
2012-08-31 22:21:34 -04:00
|
|
|
hasIcon = true;
|
2011-08-16 08:28:53 -04:00
|
|
|
}
|
2012-08-26 10:05:46 -04:00
|
|
|
this.mainIcon.visible = hasIcon;
|
2012-09-01 08:42:53 -04:00
|
|
|
this.actor.visible = hasIcon;
|
2012-08-26 10:05:46 -04:00
|
|
|
},
|
|
|
|
|
2013-05-22 14:19:00 -04:00
|
|
|
_sync: function() {
|
2012-08-26 10:05:46 -04:00
|
|
|
this._syncIcon();
|
2013-05-22 14:19:00 -04:00
|
|
|
this._syncStatusLabel();
|
2010-11-15 16:45:17 -05:00
|
|
|
}
|
2011-11-20 08:10:48 -05:00
|
|
|
});
|