2011-09-28 09:16:26 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2010-07-24 07:57:53 -04:00
|
|
|
|
|
|
|
const Clutter = imports.gi.Clutter;
|
|
|
|
const GLib = imports.gi.GLib;
|
2013-11-26 08:06:41 -05:00
|
|
|
const Gio = imports.gi.Gio;
|
2012-07-30 13:09:42 -04:00
|
|
|
const GnomeBluetooth = imports.gi.GnomeBluetooth;
|
2010-07-24 07:57:53 -04:00
|
|
|
const Lang = imports.lang;
|
|
|
|
const St = imports.gi.St;
|
|
|
|
|
|
|
|
const Main = imports.ui.main;
|
|
|
|
const PanelMenu = imports.ui.panelMenu;
|
|
|
|
const PopupMenu = imports.ui.popupMenu;
|
|
|
|
|
2013-11-26 08:06:41 -05:00
|
|
|
const BUS_NAME = 'org.gnome.SettingsDaemon.Rfkill';
|
|
|
|
const OBJECT_PATH = '/org/gnome/SettingsDaemon/Rfkill';
|
|
|
|
|
|
|
|
const RfkillManagerInterface = '<node> \
|
|
|
|
<interface name="org.gnome.SettingsDaemon.Rfkill"> \
|
|
|
|
<property name="BluetoothAirplaneMode" type="b" access="readwrite" /> \
|
|
|
|
</interface> \
|
|
|
|
</node>';
|
|
|
|
|
|
|
|
const RfkillManagerProxy = Gio.DBusProxy.makeProxyWrapper(RfkillManagerInterface);
|
|
|
|
|
2011-11-20 09:38:48 -05:00
|
|
|
const Indicator = new Lang.Class({
|
|
|
|
Name: 'BTIndicator',
|
2013-06-06 17:27:25 -04:00
|
|
|
Extends: PanelMenu.SystemIndicator,
|
2010-07-24 07:57:53 -04:00
|
|
|
|
|
|
|
_init: function() {
|
2013-06-06 17:27:25 -04:00
|
|
|
this.parent();
|
|
|
|
|
2013-07-19 06:05:47 -04:00
|
|
|
this._indicator = this._addIndicator();
|
|
|
|
this._indicator.icon_name = 'bluetooth-active-symbolic';
|
2010-07-24 07:57:53 -04:00
|
|
|
|
2013-11-26 08:06:41 -05:00
|
|
|
this._proxy = new RfkillManagerProxy(Gio.DBus.session, BUS_NAME, OBJECT_PATH,
|
|
|
|
Lang.bind(this, function(proxy, error) {
|
|
|
|
if (error) {
|
|
|
|
log(error.message);
|
|
|
|
return;
|
|
|
|
}
|
2014-01-28 15:47:37 -05:00
|
|
|
|
|
|
|
this._sync();
|
2013-11-26 08:06:41 -05:00
|
|
|
}));
|
2014-01-28 15:47:37 -05:00
|
|
|
this._proxy.connect('g-properties-changed', Lang.bind(this, this._sync));
|
2013-11-26 08:06:41 -05:00
|
|
|
|
2013-07-15 13:51:42 -04:00
|
|
|
// The Bluetooth menu only appears when Bluetooth is in use,
|
|
|
|
// so just statically build it with a "Turn Off" menu item.
|
|
|
|
this._item = new PopupMenu.PopupSubMenuMenuItem(_("Bluetooth"), true);
|
|
|
|
this._item.icon.icon_name = 'bluetooth-active-symbolic';
|
|
|
|
this._item.menu.addAction(_("Turn Off"), Lang.bind(this, function() {
|
2013-11-26 08:06:41 -05:00
|
|
|
this._proxy.BluetoothAirplaneMode = true;
|
2010-07-24 07:57:53 -04:00
|
|
|
}));
|
2013-07-15 13:51:42 -04:00
|
|
|
this._item.menu.addSettingsAction(_("Bluetooth Settings"), 'gnome-bluetooth-panel.desktop');
|
2013-09-21 12:57:41 -04:00
|
|
|
this.menu.addMenuItem(this._item);
|
2010-07-24 07:57:53 -04:00
|
|
|
|
2013-11-26 08:46:13 -05:00
|
|
|
this._client = new GnomeBluetooth.Client();
|
|
|
|
this._model = this._client.get_model();
|
|
|
|
this._model.connect('row-changed', Lang.bind(this, this._sync));
|
|
|
|
this._model.connect('row-deleted', Lang.bind(this, this._sync));
|
|
|
|
this._model.connect('row-inserted', Lang.bind(this, this._sync));
|
2013-07-15 13:51:42 -04:00
|
|
|
this._sync();
|
2010-07-24 07:57:53 -04:00
|
|
|
},
|
|
|
|
|
2013-11-26 08:46:13 -05:00
|
|
|
_getDefaultAdapter: function() {
|
|
|
|
let [ret, iter] = this._model.get_iter_first();
|
|
|
|
while (ret) {
|
|
|
|
let isDefault = this._model.get_value(iter,
|
|
|
|
GnomeBluetooth.Column.DEFAULT);
|
|
|
|
if (isDefault)
|
|
|
|
return iter;
|
|
|
|
ret = this._model.iter_next(iter);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
_getNConnectedDevices: function() {
|
|
|
|
let adapter = this._getDefaultAdapter();
|
|
|
|
if (!adapter)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
let nDevices = 0;
|
|
|
|
let [ret, iter] = this._model.iter_children(adapter);
|
|
|
|
while (ret) {
|
|
|
|
let isConnected = this._model.get_value(iter,
|
|
|
|
GnomeBluetooth.Column.CONNECTED);
|
|
|
|
if (isConnected)
|
|
|
|
nDevices++;
|
|
|
|
ret = this._model.iter_next(iter);
|
|
|
|
}
|
|
|
|
return nDevices;
|
|
|
|
},
|
|
|
|
|
2013-07-15 13:51:42 -04:00
|
|
|
_sync: function() {
|
2013-11-26 08:46:13 -05:00
|
|
|
let nDevices = this._getNConnectedDevices();
|
2010-07-24 07:57:53 -04:00
|
|
|
|
2014-01-28 15:47:37 -05:00
|
|
|
this._indicator.visible = nDevices > 0;
|
|
|
|
this._item.actor.visible = !this._proxy.BluetoothAirplaneMode;
|
2010-07-24 07:57:53 -04:00
|
|
|
|
2014-01-28 15:47:37 -05:00
|
|
|
if (nDevices > 0)
|
2013-10-03 08:26:12 -04:00
|
|
|
this._item.status.text = ngettext("%d Connected Device", "%d Connected Devices", nDevices).format(nDevices);
|
2014-01-28 15:47:37 -05:00
|
|
|
else
|
|
|
|
this._item.status.text = _("Not Connected");
|
2010-07-24 07:57:53 -04:00
|
|
|
},
|
2011-11-20 10:12:02 -05:00
|
|
|
});
|