2011-09-28 13:16:26 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2010-07-24 11:57:53 +00:00
|
|
|
|
|
|
|
const Clutter = imports.gi.Clutter;
|
|
|
|
const GLib = imports.gi.GLib;
|
|
|
|
const GnomeBluetoothApplet = imports.gi.GnomeBluetoothApplet;
|
2012-07-30 17:09:42 +00:00
|
|
|
const GnomeBluetooth = imports.gi.GnomeBluetooth;
|
2010-07-24 11:57:53 +00:00
|
|
|
const Lang = imports.lang;
|
|
|
|
const St = imports.gi.St;
|
|
|
|
|
|
|
|
const Main = imports.ui.main;
|
|
|
|
const MessageTray = imports.ui.messageTray;
|
|
|
|
const PanelMenu = imports.ui.panelMenu;
|
|
|
|
const PopupMenu = imports.ui.popupMenu;
|
|
|
|
|
2011-11-20 14:38:48 +00:00
|
|
|
const Indicator = new Lang.Class({
|
|
|
|
Name: 'BTIndicator',
|
2013-06-06 21:27:25 +00:00
|
|
|
Extends: PanelMenu.SystemIndicator,
|
2010-07-24 11:57:53 +00:00
|
|
|
|
|
|
|
_init: function() {
|
2013-06-06 21:27:25 +00:00
|
|
|
this.parent();
|
|
|
|
|
2013-07-19 10:05:47 +00:00
|
|
|
this._indicator = this._addIndicator();
|
|
|
|
this._indicator.icon_name = 'bluetooth-active-symbolic';
|
2010-07-24 11:57:53 +00:00
|
|
|
|
2013-07-15 17:51:42 +00: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() {
|
|
|
|
this._applet.killswitch_state = GnomeBluetooth.KillswitchState.SOFT_BLOCKED;
|
2010-07-24 11:57:53 +00:00
|
|
|
}));
|
2013-07-15 17:51:42 +00:00
|
|
|
this._item.menu.addSettingsAction(_("Bluetooth Settings"), 'gnome-bluetooth-panel.desktop');
|
2013-09-21 16:57:41 +00:00
|
|
|
this.menu.addMenuItem(this._item);
|
2010-07-24 11:57:53 +00:00
|
|
|
|
2013-07-15 17:51:42 +00:00
|
|
|
this._applet = new GnomeBluetoothApplet.Applet();
|
|
|
|
this._applet.connect('devices-changed', Lang.bind(this, this._sync));
|
|
|
|
this._sync();
|
2010-07-24 11:57:53 +00:00
|
|
|
|
|
|
|
this._applet.connect('pincode-request', Lang.bind(this, this._pinRequest));
|
|
|
|
this._applet.connect('confirm-request', Lang.bind(this, this._confirmRequest));
|
|
|
|
this._applet.connect('auth-request', Lang.bind(this, this._authRequest));
|
2013-05-22 14:27:02 +00:00
|
|
|
this._applet.connect('auth-service-request', Lang.bind(this, this._authServiceRequest));
|
2010-07-24 11:57:53 +00:00
|
|
|
this._applet.connect('cancel-request', Lang.bind(this, this._cancelRequest));
|
|
|
|
},
|
|
|
|
|
2013-07-15 17:51:42 +00:00
|
|
|
_sync: function() {
|
|
|
|
let connectedDevices = this._applet.get_devices().filter(function(device) {
|
|
|
|
return device.connected;
|
|
|
|
});
|
|
|
|
let nDevices = connectedDevices.length;
|
2010-07-24 11:57:53 +00:00
|
|
|
|
2013-07-15 17:51:42 +00:00
|
|
|
let on = nDevices > 0;
|
2013-06-06 21:27:25 +00:00
|
|
|
this._indicator.visible = on;
|
|
|
|
this._item.actor.visible = on;
|
2010-07-24 11:57:53 +00:00
|
|
|
|
2013-07-15 17:51:42 +00:00
|
|
|
if (on)
|
2013-10-03 12:26:12 +00:00
|
|
|
this._item.status.text = ngettext("%d Connected Device", "%d Connected Devices", nDevices).format(nDevices);
|
2010-07-24 11:57:53 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_ensureSource: function() {
|
|
|
|
if (!this._source) {
|
2012-08-31 21:25:26 +00:00
|
|
|
this._source = new MessageTray.Source(_("Bluetooth"), 'bluetooth-active');
|
2013-10-13 20:01:56 +00:00
|
|
|
this._source.policy = new MessageTray.NotificationApplicationPolicy('gnome-bluetooth-panel');
|
2010-07-24 11:57:53 +00:00
|
|
|
Main.messageTray.add(this._source);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-05-22 14:27:02 +00:00
|
|
|
_authRequest: function(applet, device_path, name, long_name) {
|
2010-07-24 11:57:53 +00:00
|
|
|
this._ensureSource();
|
2013-05-22 14:27:02 +00:00
|
|
|
this._source.notify(new AuthNotification(this._source, this._applet, device_path, name, long_name));
|
|
|
|
},
|
|
|
|
|
|
|
|
_authServiceRequest: function(applet, device_path, name, long_name, uuid) {
|
|
|
|
this._ensureSource();
|
|
|
|
this._source.notify(new AuthServiceNotification(this._source, this._applet, device_path, name, long_name, uuid));
|
2010-07-24 11:57:53 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_confirmRequest: function(applet, device_path, name, long_name, pin) {
|
|
|
|
this._ensureSource();
|
|
|
|
this._source.notify(new ConfirmNotification(this._source, this._applet, device_path, name, long_name, pin));
|
|
|
|
},
|
|
|
|
|
|
|
|
_pinRequest: function(applet, device_path, name, long_name, numeric) {
|
|
|
|
this._ensureSource();
|
|
|
|
this._source.notify(new PinNotification(this._source, this._applet, device_path, name, long_name, numeric));
|
|
|
|
},
|
|
|
|
|
|
|
|
_cancelRequest: function() {
|
|
|
|
this._source.destroy();
|
|
|
|
}
|
2011-11-20 14:38:48 +00:00
|
|
|
});
|
2010-07-24 11:57:53 +00:00
|
|
|
|
2011-11-20 15:12:02 +00:00
|
|
|
const AuthNotification = new Lang.Class({
|
|
|
|
Name: 'AuthNotification',
|
|
|
|
Extends: MessageTray.Notification,
|
2010-07-24 11:57:53 +00:00
|
|
|
|
2013-05-22 14:27:02 +00:00
|
|
|
_init: function(source, applet, device_path, name, long_name) {
|
|
|
|
this.parent(source,
|
|
|
|
_("Bluetooth"),
|
|
|
|
_("Authorization request from %s").format(name),
|
|
|
|
{ customContent: true });
|
|
|
|
this.setResident(true);
|
|
|
|
|
|
|
|
this._applet = applet;
|
|
|
|
this._devicePath = device_path;
|
|
|
|
this.addBody(_("Device %s wants to pair with this computer").format(long_name));
|
|
|
|
|
2013-10-14 03:08:16 +00:00
|
|
|
this.addAction('allow', _("Allow"));
|
|
|
|
this.addAction('deny', _("Deny"));
|
2013-05-22 14:27:02 +00:00
|
|
|
|
|
|
|
this.connect('action-invoked', Lang.bind(this, function(self, action) {
|
|
|
|
if (action == 'allow')
|
|
|
|
this._applet.agent_reply_confirm(this._devicePath, true);
|
|
|
|
else
|
|
|
|
this._applet.agent_reply_confirm(this._devicePath, false);
|
|
|
|
this.destroy();
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const AuthServiceNotification = new Lang.Class({
|
|
|
|
Name: 'AuthServiceNotification',
|
|
|
|
Extends: MessageTray.Notification,
|
|
|
|
|
2010-07-24 11:57:53 +00:00
|
|
|
_init: function(source, applet, device_path, name, long_name, uuid) {
|
2011-11-20 15:12:02 +00:00
|
|
|
this.parent(source,
|
|
|
|
_("Bluetooth"),
|
|
|
|
_("Authorization request from %s").format(name),
|
|
|
|
{ customContent: true });
|
2010-07-24 11:57:53 +00:00
|
|
|
this.setResident(true);
|
|
|
|
|
|
|
|
this._applet = applet;
|
|
|
|
this._devicePath = device_path;
|
|
|
|
this.addBody(_("Device %s wants access to the service '%s'").format(long_name, uuid));
|
|
|
|
|
2013-10-14 03:08:16 +00:00
|
|
|
this.addAction('always-grant', _("Always grant access"));
|
|
|
|
this.addAction('grant', _("Grant this time only"));
|
|
|
|
this.addAction('reject', _("Reject"));
|
2010-07-24 11:57:53 +00:00
|
|
|
|
|
|
|
this.connect('action-invoked', Lang.bind(this, function(self, action) {
|
|
|
|
switch (action) {
|
|
|
|
case 'always-grant':
|
2013-05-22 14:27:02 +00:00
|
|
|
this._applet.agent_reply_auth_service(this._devicePath, true, true);
|
2010-07-24 11:57:53 +00:00
|
|
|
break;
|
|
|
|
case 'grant':
|
2013-05-22 14:27:02 +00:00
|
|
|
this._applet.agent_reply_auth_service(this._devicePath, true, false);
|
2010-07-24 11:57:53 +00:00
|
|
|
break;
|
|
|
|
case 'reject':
|
|
|
|
default:
|
2013-05-22 14:27:02 +00:00
|
|
|
this._applet.agent_reply_auth_service(this._devicePath, false, false);
|
2010-07-24 11:57:53 +00:00
|
|
|
}
|
|
|
|
this.destroy();
|
|
|
|
}));
|
|
|
|
}
|
2011-11-20 15:12:02 +00:00
|
|
|
});
|
2010-07-24 11:57:53 +00:00
|
|
|
|
2011-11-20 15:12:02 +00:00
|
|
|
const ConfirmNotification = new Lang.Class({
|
|
|
|
Name: 'ConfirmNotification',
|
|
|
|
Extends: MessageTray.Notification,
|
2010-07-24 11:57:53 +00:00
|
|
|
|
|
|
|
_init: function(source, applet, device_path, name, long_name, pin) {
|
2011-11-20 15:12:02 +00:00
|
|
|
this.parent(source,
|
|
|
|
_("Bluetooth"),
|
2012-11-19 16:49:57 +00:00
|
|
|
/* Translators: argument is the device short name */
|
2011-11-20 15:12:02 +00:00
|
|
|
_("Pairing confirmation for %s").format(name),
|
|
|
|
{ customContent: true });
|
2010-07-24 11:57:53 +00:00
|
|
|
this.setResident(true);
|
|
|
|
|
|
|
|
this._applet = applet;
|
|
|
|
this._devicePath = device_path;
|
|
|
|
this.addBody(_("Device %s wants to pair with this computer").format(long_name));
|
2013-04-09 18:25:56 +00:00
|
|
|
this.addBody(_("Please confirm whether the Passkey '%06d' matches the one on the device.").format(pin));
|
2010-07-24 11:57:53 +00:00
|
|
|
|
2012-11-19 16:49:57 +00:00
|
|
|
/* Translators: this is the verb, not the noun */
|
2013-10-14 03:08:16 +00:00
|
|
|
this.addAction('matches', _("Matches"));
|
|
|
|
this.addAction('does-not-match', _("Does not match"));
|
2010-07-24 11:57:53 +00:00
|
|
|
|
|
|
|
this.connect('action-invoked', Lang.bind(this, function(self, action) {
|
|
|
|
if (action == 'matches')
|
|
|
|
this._applet.agent_reply_confirm(this._devicePath, true);
|
|
|
|
else
|
|
|
|
this._applet.agent_reply_confirm(this._devicePath, false);
|
|
|
|
this.destroy();
|
|
|
|
}));
|
|
|
|
}
|
2011-11-20 15:12:02 +00:00
|
|
|
});
|
2010-07-24 11:57:53 +00:00
|
|
|
|
2011-11-20 15:12:02 +00:00
|
|
|
const PinNotification = new Lang.Class({
|
|
|
|
Name: 'PinNotification',
|
|
|
|
Extends: MessageTray.Notification,
|
2010-07-24 11:57:53 +00:00
|
|
|
|
|
|
|
_init: function(source, applet, device_path, name, long_name, numeric) {
|
2011-11-20 15:12:02 +00:00
|
|
|
this.parent(source,
|
|
|
|
_("Bluetooth"),
|
|
|
|
_("Pairing request for %s").format(name),
|
|
|
|
{ customContent: true });
|
2010-07-24 11:57:53 +00:00
|
|
|
this.setResident(true);
|
|
|
|
|
|
|
|
this._applet = applet;
|
|
|
|
this._devicePath = device_path;
|
|
|
|
this._numeric = numeric;
|
|
|
|
this.addBody(_("Device %s wants to pair with this computer").format(long_name));
|
|
|
|
this.addBody(_("Please enter the PIN mentioned on the device."));
|
|
|
|
|
|
|
|
this._entry = new St.Entry();
|
|
|
|
this._entry.connect('key-release-event', Lang.bind(this, function(entry, event) {
|
|
|
|
let key = event.get_key_symbol();
|
|
|
|
if (key == Clutter.KEY_Return) {
|
2012-05-17 19:09:56 +00:00
|
|
|
if (this._canActivateOkButton())
|
2013-10-14 03:06:09 +00:00
|
|
|
this._ok();
|
2010-07-24 11:57:53 +00:00
|
|
|
return true;
|
|
|
|
} else if (key == Clutter.KEY_Escape) {
|
2013-10-14 03:06:09 +00:00
|
|
|
this._cancel();
|
2010-07-24 11:57:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}));
|
|
|
|
this.addActor(this._entry);
|
|
|
|
|
2013-10-14 03:06:09 +00:00
|
|
|
let okButton = this.addAction(_("OK"), Lang.bind(this, this._ok));
|
|
|
|
this.addAction(_("Cancel"), Lang.bind(this, this._cancel));
|
2010-07-24 11:57:53 +00:00
|
|
|
|
2013-10-14 02:56:26 +00:00
|
|
|
okButton.reactive = this._canActivateOkButton();
|
|
|
|
this._entry.clutter_text.connect('text-changed', Lang.bind(this, function() {
|
|
|
|
okButton.reactive = this._canActivateOkButton();
|
|
|
|
}));
|
2013-10-14 03:06:09 +00:00
|
|
|
},
|
2012-05-17 19:09:56 +00:00
|
|
|
|
2013-10-14 03:06:09 +00:00
|
|
|
_ok: function() {
|
|
|
|
if (this._numeric) {
|
|
|
|
let num = parseInt(this._entry.text);
|
|
|
|
if (isNaN(num)) {
|
|
|
|
// user reply was empty, or was invalid
|
|
|
|
// cancel the operation
|
|
|
|
num = -1;
|
2010-07-24 11:57:53 +00:00
|
|
|
}
|
2013-10-14 03:06:09 +00:00
|
|
|
this._applet.agent_reply_passkey(this._devicePath, num);
|
|
|
|
} else {
|
|
|
|
this._applet.agent_reply_pincode(this._devicePath, this._entry.text);
|
|
|
|
}
|
|
|
|
this.destroy();
|
|
|
|
},
|
|
|
|
|
|
|
|
_cancel: function() {
|
|
|
|
if (this._numeric)
|
|
|
|
this._applet.agent_reply_passkey(this._devicePath, -1);
|
|
|
|
else
|
|
|
|
this._applet.agent_reply_pincode(this._devicePath, null);
|
|
|
|
this.destroy();
|
2010-07-24 11:57:53 +00:00
|
|
|
},
|
|
|
|
|
2012-05-17 19:09:56 +00:00
|
|
|
_canActivateOkButton: function() {
|
|
|
|
// PINs have a fixed length of 6
|
2012-08-31 17:56:45 +00:00
|
|
|
if (this._numeric)
|
|
|
|
return this._entry.clutter_text.text.length == 6;
|
|
|
|
else
|
|
|
|
return true;
|
2010-07-24 11:57:53 +00:00
|
|
|
}
|
2011-11-20 15:12:02 +00:00
|
|
|
});
|