power: Use UPower directly instead of gnome-settings-daemon

UPower master exports a display device that can be used to
compute whether to show a status icon, and what we should show.

https://bugzilla.gnome.org/show_bug.cgi?id=710273
This commit is contained in:
Bastien Nocera 2013-10-17 16:45:17 +02:00
parent 37c8132632
commit 0b8c0c202e

View File

@ -8,20 +8,20 @@ const Main = imports.ui.main;
const PanelMenu = imports.ui.panelMenu; const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu; const PopupMenu = imports.ui.popupMenu;
const BUS_NAME = 'org.gnome.SettingsDaemon.Power'; const BUS_NAME = 'org.freedesktop.UPower';
const OBJECT_PATH = '/org/gnome/SettingsDaemon/Power'; const OBJECT_PATH = '/org/freedesktop/UPower/devices/DisplayDevice';
const PowerManagerInterface = <interface name="org.gnome.SettingsDaemon.Power"> const DisplayDeviceInterface = <interface name="org.freedesktop.UPower.Device">
<method name="GetDevices"> <property name="Type" type="u" access="read"/>
<arg type="a(susdut)" direction="out" /> <property name="State" type="u" access="read"/>
</method> <property name="Percentage" type="d" access="read"/>
<method name="GetPrimaryDevice"> <property name="TimeToEmpty" type="x" access="read"/>
<arg type="(susdut)" direction="out" /> <property name="TimeToFull" type="x" access="read"/>
</method> <property name="IsPresent" type="b" access="read"/>
<property name="Icon" type="s" access="read" /> <property name="IconName" type="s" access="read"/>
</interface>; </interface>;
const PowerManagerProxy = Gio.DBusProxy.makeProxyWrapper(PowerManagerInterface); const PowerManagerProxy = Gio.DBusProxy.makeProxyWrapper(DisplayDeviceInterface);
const Indicator = new Lang.Class({ const Indicator = new Lang.Class({
Name: 'PowerIndicator', Name: 'PowerIndicator',
@ -32,7 +32,7 @@ const Indicator = new Lang.Class({
this._indicator = this._addIndicator(); this._indicator = this._addIndicator();
this._proxy = new PowerManagerProxy(Gio.DBus.session, BUS_NAME, OBJECT_PATH, this._proxy = new PowerManagerProxy(Gio.DBus.system, BUS_NAME, OBJECT_PATH,
Lang.bind(this, function(proxy, error) { Lang.bind(this, function(proxy, error) {
if (error) { if (error) {
log(error.message); log(error.message);
@ -43,7 +43,7 @@ const Indicator = new Lang.Class({
this._sync(); this._sync();
})); }));
this._item = new PopupMenu.PopupSubMenuMenuItem(_("Battery"), true); this._item = new PopupMenu.PopupSubMenuMenuItem("", true);
this._item.menu.addSettingsAction(_("Power Settings"), 'gnome-power-panel.desktop'); this._item.menu.addSettingsAction(_("Power Settings"), 'gnome-power-panel.desktop');
this.menu.addMenuItem(this._item); this.menu.addMenuItem(this._item);
@ -56,11 +56,18 @@ const Indicator = new Lang.Class({
this.menu.setSensitive(sensitive); this.menu.setSensitive(sensitive);
}, },
_statusForDevice: function(device) { _getStatus: function() {
let [device_id, device_type, icon, percentage, state, seconds] = device; let seconds = 0;
if (state == UPower.DeviceState.FULLY_CHARGED) if (this._proxy.State == UPower.DeviceState.FULLY_CHARGED)
return _("Fully Charged"); return _("Fully Charged");
else if (this._proxy.State == UPower.DeviceState.CHARGING)
seconds = this._proxy.TimeToFull;
else if (this_proxy.State == UPower.DeviceState.DISCHARGING)
seconds = this._proxy.TimeToEmpty;
// state is one of PENDING_CHARGING, PENDING_DISCHARGING
else
return _("Estimating…");
let time = Math.round(seconds / 60); let time = Math.round(seconds / 60);
if (time == 0) { if (time == 0) {
@ -82,34 +89,33 @@ const Indicator = new Lang.Class({
return _("%d\u2236%02d Until Full (%d%%)").format(hours, minutes, percentage); return _("%d\u2236%02d Until Full (%d%%)").format(hours, minutes, percentage);
} }
// state is one of PENDING_CHARGING, PENDING_DISCHARGING return null;
return _("Estimating…");
}, },
_sync: function() { _sync: function() {
function isBattery(result) { // Do we have batteries or a UPS?
if (!result) let visible = this._proxy.IsPresent;
return false; if (visible) {
this._item.actor.show();
let [device] = result; } else {
let [, deviceType] = device; // If there's no battery, then we use the power icon.
return (deviceType == UPower.DeviceKind.BATTERY); this._item.actor.hide();
this._indicator.icon_name = 'system-shutdown-symbolic';
return;
} }
this._proxy.GetPrimaryDeviceRemote(Lang.bind(this, function(result, error) { // The icons
if (isBattery(result)) { let icon = this._proxy.IconName;
let [device] = result; this._indicator.icon_name = icon;
let [,, icon] = device; this._item.icon.icon_name = icon;
let gicon = Gio.icon_new_for_string(icon);
this._indicator.gicon = gicon; // The status label
this._item.icon.gicon = gicon; this._item.status.text = this._getStatus();
this._item.status.text = this._statusForDevice(device);
this._item.actor.show(); // The sub-menu heading
} else { if (this._proxy.Type == UPower.DeviceKind.UPS)
// If there's no battery, then we use the power icon. this._item.label.text = _("UPS");
this._indicator.icon_name = 'system-shutdown-symbolic'; else
this._item.actor.hide(); this._item.label.text = _("Battery");
}
}));
}, },
}); });