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;
|
|
|
|
const St = imports.gi.St;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
const UPDeviceType = {
|
|
|
|
UNKNOWN: 0,
|
|
|
|
AC_POWER: 1,
|
|
|
|
BATTERY: 2,
|
|
|
|
UPS: 3,
|
|
|
|
MONITOR: 4,
|
|
|
|
MOUSE: 5,
|
|
|
|
KEYBOARD: 6,
|
|
|
|
PDA: 7,
|
2010-11-19 12:57:35 -05:00
|
|
|
PHONE: 8,
|
|
|
|
MEDIA_PLAYER: 9,
|
|
|
|
TABLET: 10,
|
|
|
|
COMPUTER: 11
|
2010-11-15 16:45:17 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
const UPDeviceState = {
|
|
|
|
UNKNOWN: 0,
|
|
|
|
CHARGING: 1,
|
|
|
|
DISCHARGING: 2,
|
|
|
|
EMPTY: 3,
|
|
|
|
FULLY_CHARGED: 4,
|
|
|
|
PENDING_CHARGE: 5,
|
|
|
|
PENDING_DISCHARGE: 6
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2011-08-16 08:28:53 -04:00
|
|
|
this._proxy = new PowerManagerProxy(Gio.DBus.session, BUS_NAME, OBJECT_PATH);
|
2010-11-15 16:45:17 -05:00
|
|
|
|
|
|
|
this._deviceItems = [ ];
|
|
|
|
this._hasPrimary = false;
|
|
|
|
this._primaryDeviceId = null;
|
2010-11-20 12:19:32 -05:00
|
|
|
|
2011-03-01 20:03:25 -05:00
|
|
|
this._batteryItem = new PopupMenu.PopupMenuItem('', { reactive: false });
|
2012-11-30 11:54:18 -05:00
|
|
|
this._primaryPercentage = new St.Label({ style_class: 'popup-battery-percentage' });
|
2010-11-17 14:10:12 -05:00
|
|
|
this._batteryItem.addActor(this._primaryPercentage, { align: St.Align.END });
|
2010-11-15 16:45:17 -05:00
|
|
|
this.menu.addMenuItem(this._batteryItem);
|
2010-11-20 12:19:32 -05:00
|
|
|
|
2011-08-19 14:42:20 -04:00
|
|
|
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
|
2010-11-15 16:45:17 -05:00
|
|
|
this._otherDevicePosition = 2;
|
2010-11-20 12:19:32 -05:00
|
|
|
|
2011-08-23 10:14:55 -04:00
|
|
|
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
|
|
|
|
this.menu.addSettingsAction(_("Power Settings"), 'gnome-power-panel.desktop');
|
2010-11-15 16:45:17 -05:00
|
|
|
|
2012-01-29 16:19:38 -05:00
|
|
|
this._proxy.connect('g-properties-changed',
|
|
|
|
Lang.bind(this, this._devicesChanged));
|
2010-11-15 16:45:17 -05:00
|
|
|
this._devicesChanged();
|
|
|
|
},
|
|
|
|
|
|
|
|
_readPrimaryDevice: 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) {
|
|
|
|
this._hasPrimary = false;
|
|
|
|
this._primaryDeviceId = null;
|
|
|
|
this._batteryItem.actor.hide();
|
|
|
|
return;
|
|
|
|
}
|
2011-08-16 08:28:53 -04:00
|
|
|
let [[device_id, device_type, icon, percentage, state, seconds]] = result;
|
2010-11-15 16:45:17 -05:00
|
|
|
if (device_type == UPDeviceType.BATTERY) {
|
|
|
|
this._hasPrimary = true;
|
2010-11-26 16:22:07 -05:00
|
|
|
let time = Math.round(seconds / 60);
|
2011-02-19 09:40:53 -05:00
|
|
|
if (time == 0) {
|
|
|
|
// 0 is reported when UPower does not have enough data
|
|
|
|
// to estimate battery life
|
|
|
|
this._batteryItem.label.text = _("Estimating...");
|
|
|
|
} else {
|
|
|
|
let minutes = time % 60;
|
|
|
|
let hours = Math.floor(time / 60);
|
|
|
|
let timestring;
|
2012-11-08 23:09:53 -05:00
|
|
|
if (time >= 60) {
|
2011-02-19 09:40:53 -05:00
|
|
|
if (minutes == 0) {
|
2011-04-30 09:16:13 -04:00
|
|
|
timestring = ngettext("%d hour remaining", "%d hours remaining", hours).format(hours);
|
2011-02-19 09:40:53 -05:00
|
|
|
} else {
|
|
|
|
/* TRANSLATORS: this is a time string, as in "%d hours %d minutes remaining" */
|
|
|
|
let template = _("%d %s %d %s remaining");
|
|
|
|
|
2011-04-30 09:16:13 -04:00
|
|
|
timestring = template.format (hours, ngettext("hour", "hours", hours), minutes, ngettext("minute", "minutes", minutes));
|
2011-02-19 09:40:53 -05:00
|
|
|
}
|
|
|
|
} else
|
2011-04-30 09:16:13 -04:00
|
|
|
timestring = ngettext("%d minute remaining", "%d minutes remaining", minutes).format(minutes);
|
2011-02-19 09:40:53 -05:00
|
|
|
this._batteryItem.label.text = timestring;
|
|
|
|
}
|
2011-09-07 08:25:02 -04:00
|
|
|
this._primaryPercentage.text = C_("percent of battery remaining", "%d%%").format(Math.round(percentage));
|
2010-11-15 16:45:17 -05:00
|
|
|
this._batteryItem.actor.show();
|
|
|
|
} else {
|
|
|
|
this._hasPrimary = false;
|
|
|
|
this._batteryItem.actor.hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
this._primaryDeviceId = device_id;
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
|
|
|
_readOtherDevices: function() {
|
2011-08-16 08:28:53 -04:00
|
|
|
this._proxy.GetDevicesRemote(Lang.bind(this, function(result, error) {
|
2010-11-15 16:45:17 -05:00
|
|
|
this._deviceItems.forEach(function(i) { i.destroy(); });
|
|
|
|
this._deviceItems = [];
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let position = 0;
|
2011-08-16 08:28:53 -04:00
|
|
|
let [devices] = result;
|
2010-11-15 16:45:17 -05:00
|
|
|
for (let i = 0; i < devices.length; i++) {
|
|
|
|
let [device_id, device_type] = devices[i];
|
|
|
|
if (device_type == UPDeviceType.AC_POWER || device_id == this._primaryDeviceId)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
let item = new DeviceItem (devices[i]);
|
|
|
|
this._deviceItems.push(item);
|
|
|
|
this.menu.addMenuItem(item, this._otherDevicePosition + position);
|
|
|
|
position++;
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
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);
|
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
|
|
|
},
|
|
|
|
|
|
|
|
_devicesChanged: function() {
|
|
|
|
this._syncIcon();
|
2010-11-15 16:45:17 -05:00
|
|
|
this._readPrimaryDevice();
|
|
|
|
this._readOtherDevices();
|
|
|
|
}
|
2011-11-20 09:38:48 -05:00
|
|
|
});
|
2010-11-15 16:45:17 -05:00
|
|
|
|
2011-11-20 08:10:48 -05:00
|
|
|
const DeviceItem = new Lang.Class({
|
|
|
|
Name: 'DeviceItem',
|
|
|
|
Extends: PopupMenu.PopupBaseMenuItem,
|
2010-11-15 16:45:17 -05:00
|
|
|
|
|
|
|
_init: function(device) {
|
2011-11-20 08:10:48 -05:00
|
|
|
this.parent({ reactive: false });
|
2010-11-15 16:45:17 -05:00
|
|
|
|
2010-11-19 12:57:35 -05:00
|
|
|
let [device_id, device_type, icon, percentage, state, time] = device;
|
2010-11-15 16:45:17 -05:00
|
|
|
|
|
|
|
this._box = new St.BoxLayout({ style_class: 'popup-device-menu-item' });
|
2010-11-19 12:57:35 -05:00
|
|
|
this._label = new St.Label({ text: this._deviceTypeToString(device_type) });
|
2010-11-15 16:45:17 -05:00
|
|
|
|
2011-10-07 17:35:38 -04:00
|
|
|
this._icon = new St.Icon({ gicon: Gio.icon_new_for_string(icon),
|
2010-11-15 16:45:17 -05:00
|
|
|
style_class: 'popup-menu-icon' });
|
|
|
|
|
|
|
|
this._box.add_actor(this._icon);
|
|
|
|
this._box.add_actor(this._label);
|
|
|
|
this.addActor(this._box);
|
|
|
|
|
2012-11-30 11:54:18 -05:00
|
|
|
let percentLabel = new St.Label({ text: C_("percent of battery remaining", "%d%%").format(Math.round(percentage)),
|
|
|
|
style_class: 'popup-battery-percentage' });
|
2010-11-17 14:10:12 -05:00
|
|
|
this.addActor(percentLabel, { align: St.Align.END });
|
2012-07-31 11:39:32 -04:00
|
|
|
//FIXME: ideally we would like to expose this._label and percentLabel
|
|
|
|
this.actor.label_actor = percentLabel;
|
2010-11-15 16:45:17 -05:00
|
|
|
},
|
|
|
|
|
2010-11-19 12:57:35 -05:00
|
|
|
_deviceTypeToString: function(type) {
|
|
|
|
switch (type) {
|
|
|
|
case UPDeviceType.AC_POWER:
|
|
|
|
return _("AC adapter");
|
|
|
|
case UPDeviceType.BATTERY:
|
|
|
|
return _("Laptop battery");
|
|
|
|
case UPDeviceType.UPS:
|
|
|
|
return _("UPS");
|
|
|
|
case UPDeviceType.MONITOR:
|
|
|
|
return _("Monitor");
|
|
|
|
case UPDeviceType.MOUSE:
|
|
|
|
return _("Mouse");
|
|
|
|
case UPDeviceType.KEYBOARD:
|
|
|
|
return _("Keyboard");
|
|
|
|
case UPDeviceType.PDA:
|
|
|
|
return _("PDA");
|
|
|
|
case UPDeviceType.PHONE:
|
|
|
|
return _("Cell phone");
|
|
|
|
case UPDeviceType.MEDIA_PLAYER:
|
|
|
|
return _("Media player");
|
|
|
|
case UPDeviceType.TABLET:
|
|
|
|
return _("Tablet");
|
|
|
|
case UPDeviceType.COMPUTER:
|
|
|
|
return _("Computer");
|
|
|
|
default:
|
2012-06-01 08:44:40 -04:00
|
|
|
return C_("device", "Unknown");
|
2010-11-19 12:57:35 -05:00
|
|
|
}
|
2010-11-15 16:45:17 -05:00
|
|
|
}
|
2011-11-20 08:10:48 -05:00
|
|
|
});
|