2010-11-15 16:45:17 -05:00
|
|
|
/* -*- mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil -*- */
|
|
|
|
|
|
|
|
const Gio = imports.gi.Gio;
|
|
|
|
const DBus = imports.dbus;
|
|
|
|
const Lang = imports.lang;
|
|
|
|
const Mainloop = imports.mainloop;
|
|
|
|
const Shell = imports.gi.Shell;
|
|
|
|
const St = imports.gi.St;
|
|
|
|
|
2011-06-21 18:26:57 -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;
|
2010-11-17 11:43:08 -05:00
|
|
|
const Util = imports.misc.util;
|
2010-11-15 16:45:17 -05:00
|
|
|
|
2011-07-04 07:09:06 -04:00
|
|
|
const BUS_NAME = 'org.gnome.SettingsDaemon';
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
const PowerManagerInterface = {
|
2011-07-04 07:09:06 -04:00
|
|
|
name: 'org.gnome.SettingsDaemon.Power',
|
2010-11-15 16:45:17 -05:00
|
|
|
methods: [
|
|
|
|
{ name: 'GetDevices', inSignature: '', outSignature: 'a(susbut)' },
|
|
|
|
{ name: 'GetPrimaryDevice', inSignature: '', outSignature: '(susbut)' },
|
|
|
|
],
|
|
|
|
signals: [
|
2011-02-26 13:21:10 -05:00
|
|
|
{ name: 'Changed', inSignature: '' },
|
2010-11-15 16:45:17 -05:00
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{ name: 'Icon', signature: 's', access: 'read' },
|
|
|
|
]
|
|
|
|
};
|
|
|
|
let PowerManagerProxy = DBus.makeProxyClass(PowerManagerInterface);
|
|
|
|
|
|
|
|
function Indicator() {
|
|
|
|
this._init.apply(this, arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
Indicator.prototype = {
|
|
|
|
__proto__: PanelMenu.SystemStatusButton.prototype,
|
|
|
|
|
|
|
|
_init: function() {
|
|
|
|
PanelMenu.SystemStatusButton.prototype._init.call(this, 'battery-missing');
|
|
|
|
this._proxy = new PowerManagerProxy(DBus.session, BUS_NAME, OBJECT_PATH);
|
|
|
|
|
|
|
|
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 });
|
2010-11-20 12:19:32 -05:00
|
|
|
this._primaryPercentage = new St.Label();
|
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
|
|
|
|
2010-11-15 16:45:17 -05:00
|
|
|
this._deviceSep = new PopupMenu.PopupSeparatorMenuItem();
|
|
|
|
this.menu.addMenuItem(this._deviceSep);
|
|
|
|
this._otherDevicePosition = 2;
|
|
|
|
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
|
2010-11-20 12:19:32 -05:00
|
|
|
|
2010-11-15 16:45:17 -05:00
|
|
|
this.menu.addAction(_("Power Settings"),function() {
|
2011-06-21 18:26:57 -04:00
|
|
|
Main.overview.hide();
|
2011-03-10 12:19:10 -05:00
|
|
|
let app = Shell.AppSystem.get_default().get_app('gnome-power-panel.desktop');
|
|
|
|
app.activate(-1);
|
2010-11-15 16:45:17 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
this._proxy.connect('Changed', Lang.bind(this, this._devicesChanged));
|
|
|
|
this._devicesChanged();
|
|
|
|
},
|
|
|
|
|
|
|
|
_readPrimaryDevice: function() {
|
|
|
|
this._proxy.GetPrimaryDeviceRemote(Lang.bind(this, function(device, error) {
|
|
|
|
if (error) {
|
|
|
|
this._checkError(error);
|
|
|
|
this._hasPrimary = false;
|
|
|
|
this._primaryDeviceId = null;
|
|
|
|
this._batteryItem.actor.hide();
|
|
|
|
this._deviceSep.actor.hide();
|
|
|
|
return;
|
|
|
|
}
|
2010-11-26 16:22:07 -05:00
|
|
|
let [device_id, device_type, icon, percentage, state, seconds] = device;
|
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;
|
|
|
|
if (time > 60) {
|
|
|
|
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;
|
|
|
|
}
|
2010-11-26 16:22:07 -05:00
|
|
|
this._primaryPercentage.text = Math.round(percentage) + '%';
|
2010-11-15 16:45:17 -05:00
|
|
|
this._batteryItem.actor.show();
|
|
|
|
if (this._deviceItems.length > 0)
|
|
|
|
this._deviceSep.actor.show();
|
|
|
|
} else {
|
|
|
|
this._hasPrimary = false;
|
|
|
|
this._batteryItem.actor.hide();
|
|
|
|
this._deviceSep.actor.hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
this._primaryDeviceId = device_id;
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
|
|
|
_readOtherDevices: function() {
|
|
|
|
this._proxy.GetDevicesRemote(Lang.bind(this, function(devices, error) {
|
|
|
|
this._deviceItems.forEach(function(i) { i.destroy(); });
|
|
|
|
this._deviceItems = [];
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
this._checkError(error);
|
|
|
|
this._deviceSep.actor.hide();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let position = 0;
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._hasPrimary && position > 0)
|
|
|
|
this._deviceSep.actor.show();
|
|
|
|
else
|
|
|
|
this._deviceSep.actor.hide();
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
|
|
|
_devicesChanged: function() {
|
|
|
|
this._proxy.GetRemote('Icon', Lang.bind(this, function(icon, error) {
|
|
|
|
if (icon) {
|
|
|
|
let gicon = Shell.util_icon_from_string (icon);
|
|
|
|
this.setGIcon(gicon);
|
|
|
|
this.actor.show();
|
|
|
|
} else {
|
|
|
|
this._checkError(error);
|
|
|
|
this.menu.close();
|
|
|
|
this.actor.hide();
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
this._readPrimaryDevice();
|
|
|
|
this._readOtherDevices();
|
|
|
|
},
|
|
|
|
|
|
|
|
_checkError: function(error) {
|
|
|
|
if (!this._restarted && error && error.message.match(/org\.freedesktop\.DBus\.Error\.(UnknownMethod|InvalidArgs)/)) {
|
2010-12-17 15:30:12 -05:00
|
|
|
Util.killall('gnome-power-manager');
|
2010-11-17 11:43:08 -05:00
|
|
|
Util.spawn(['gnome-power-manager']);
|
2010-11-15 16:45:17 -05:00
|
|
|
this._restarted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function DeviceItem() {
|
|
|
|
this._init.apply(this, arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceItem.prototype = {
|
|
|
|
__proto__: PopupMenu.PopupBaseMenuItem.prototype,
|
|
|
|
|
|
|
|
_init: function(device) {
|
2011-03-01 20:03:25 -05:00
|
|
|
PopupMenu.PopupBaseMenuItem.prototype._init.call(this, { 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
|
|
|
|
2010-11-19 12:57:35 -05:00
|
|
|
this._icon = new St.Icon({ gicon: Shell.util_icon_from_string(icon),
|
2010-11-15 16:45:17 -05:00
|
|
|
icon_type: St.IconType.SYMBOLIC,
|
|
|
|
style_class: 'popup-menu-icon' });
|
|
|
|
|
|
|
|
this._box.add_actor(this._icon);
|
|
|
|
this._box.add_actor(this._label);
|
|
|
|
this.addActor(this._box);
|
|
|
|
|
2010-11-17 16:58:00 -05:00
|
|
|
let percentLabel = new St.Label({ text: '%d%%'.format(Math.round(percentage)) });
|
2010-11-17 14:10:12 -05:00
|
|
|
this.addActor(percentLabel, { align: St.Align.END });
|
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:
|
|
|
|
return _("Unknown");
|
|
|
|
}
|
2010-11-15 16:45:17 -05:00
|
|
|
}
|
|
|
|
}
|