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 PopupMenu = imports.ui.popupMenu;
const BUS_NAME = 'org.gnome.SettingsDaemon.Power';
const OBJECT_PATH = '/org/gnome/SettingsDaemon/Power';
const BUS_NAME = 'org.freedesktop.UPower';
const OBJECT_PATH = '/org/freedesktop/UPower/devices/DisplayDevice';
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" />
const DisplayDeviceInterface = <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>;
const PowerManagerProxy = Gio.DBusProxy.makeProxyWrapper(PowerManagerInterface);
const PowerManagerProxy = Gio.DBusProxy.makeProxyWrapper(DisplayDeviceInterface);
const Indicator = new Lang.Class({
Name: 'PowerIndicator',
@ -32,7 +32,7 @@ const Indicator = new Lang.Class({
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) {
if (error) {
log(error.message);
@ -43,7 +43,7 @@ const Indicator = new Lang.Class({
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.menu.addMenuItem(this._item);
@ -56,11 +56,18 @@ const Indicator = new Lang.Class({
this.menu.setSensitive(sensitive);
},
_statusForDevice: function(device) {
let [device_id, device_type, icon, percentage, state, seconds] = device;
_getStatus: function() {
let seconds = 0;
if (state == UPower.DeviceState.FULLY_CHARGED)
if (this._proxy.State == UPower.DeviceState.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);
if (time == 0) {
@ -82,34 +89,33 @@ const Indicator = new Lang.Class({
return _("%d\u2236%02d Until Full (%d%%)").format(hours, minutes, percentage);
}
// state is one of PENDING_CHARGING, PENDING_DISCHARGING
return _("Estimating…");
return null;
},
_sync: function() {
function isBattery(result) {
if (!result)
return false;
let [device] = result;
let [, deviceType] = device;
return (deviceType == UPower.DeviceKind.BATTERY);
// 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;
}
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;
this._item.status.text = this._statusForDevice(device);
this._item.actor.show();
} else {
// If there's no battery, then we use the power icon.
this._indicator.icon_name = 'system-shutdown-symbolic';
this._item.actor.hide();
}
}));
// 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");
},
});