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;
|
|
|
|
|
2012-12-19 16:06:20 +00:00
|
|
|
const BUS_NAME = 'org.gnome.SettingsDaemon.Power';
|
2011-07-04 11:09:06 +00:00
|
|
|
const OBJECT_PATH = '/org/gnome/SettingsDaemon/Power';
|
2010-11-15 21:45:17 +00:00
|
|
|
|
2011-08-16 12:28:53 +00: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 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-01-28 15:38:45 +00:00
|
|
|
this._proxy = new PowerManagerProxy(Gio.DBus.session, 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-05-22 18:19:00 +00: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 14:01:05 +00:00
|
|
|
return _("%d\u2236%02d Remaining (%d%%)").format(hours, minutes, percentage);
|
2013-05-22 18:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (state == UPower.DeviceState.CHARGING) {
|
|
|
|
// Translators: this is <hours>:<minutes> Until Full (<percentage>)
|
2013-09-05 14:01:05 +00:00
|
|
|
return _("%d\u2236%02d Until Full (%d%%)").format(hours, minutes, percentage);
|
2013-05-22 18:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// state is one of PENDING_CHARGING, PENDING_DISCHARGING
|
|
|
|
return _("Estimating…");
|
2010-11-15 21:45:17 +00:00
|
|
|
},
|
|
|
|
|
2013-05-22 18:19:00 +00:00
|
|
|
_syncStatusLabel: function() {
|
2011-08-16 12:28:53 +00:00
|
|
|
this._proxy.GetPrimaryDeviceRemote(Lang.bind(this, function(result, error) {
|
2010-11-15 21:45:17 +00:00
|
|
|
if (error) {
|
2013-05-22 18:19:00 +00:00
|
|
|
this._item.actor.hide();
|
2010-11-15 21:45:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-05-22 18:11:34 +00:00
|
|
|
|
2013-05-22 18:19:00 +00:00
|
|
|
let [device] = result;
|
|
|
|
let [device_id, device_type] = device;
|
2013-05-22 18:18:39 +00:00
|
|
|
if (device_type == UPower.DeviceKind.BATTERY) {
|
2013-05-22 18:19:00 +00:00
|
|
|
this._item.status.text = this._statusForDevice(device);
|
|
|
|
this._item.actor.show();
|
2010-11-15 21:45:17 +00:00
|
|
|
} else {
|
2013-05-22 18:19:00 +00:00
|
|
|
this._item.actor.hide();
|
2010-11-15 21:45:17 +00:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
2012-08-26 14:05:46 +00:00
|
|
|
_syncIcon: function() {
|
2011-08-16 12:28:53 +00:00
|
|
|
let icon = this._proxy.Icon;
|
2012-09-01 02:21:34 +00:00
|
|
|
if (icon) {
|
2011-08-16 12:28:53 +00:00
|
|
|
let gicon = Gio.icon_new_for_string(icon);
|
2013-06-06 21:27:25 +00:00
|
|
|
this._indicator.gicon = gicon;
|
2013-05-22 18:19:00 +00:00
|
|
|
this._item.icon.gicon = gicon;
|
2013-06-06 21:27:25 +00:00
|
|
|
} else {
|
2013-06-17 22:19:20 +00:00
|
|
|
// If there's no battery, then we use the power icon.
|
|
|
|
this._indicator.icon_name = 'system-shutdown-symbolic';
|
2011-08-16 12:28:53 +00:00
|
|
|
}
|
2012-08-26 14:05:46 +00:00
|
|
|
},
|
|
|
|
|
2013-05-22 18:19:00 +00:00
|
|
|
_sync: function() {
|
2012-08-26 14:05:46 +00:00
|
|
|
this._syncIcon();
|
2013-05-22 18:19:00 +00:00
|
|
|
this._syncStatusLabel();
|
2010-11-15 21:45:17 +00:00
|
|
|
}
|
2011-11-20 13:10:48 +00:00
|
|
|
});
|