2019-05-23 16:45:44 -04:00
|
|
|
const { Clutter, Gio, GLib, GObject, Shell } = imports.gi;
|
2016-07-12 16:47:32 -04:00
|
|
|
|
|
|
|
const CheckBox = imports.ui.checkBox;
|
2017-07-15 00:03:55 -04:00
|
|
|
const Dialog = imports.ui.dialog;
|
2016-07-12 16:47:32 -04:00
|
|
|
const ModalDialog = imports.ui.modalDialog;
|
|
|
|
|
2018-09-05 20:55:20 -04:00
|
|
|
const { loadInterfaceXML } = imports.misc.fileUtils;
|
2016-07-12 16:47:32 -04:00
|
|
|
|
2018-09-05 20:55:20 -04:00
|
|
|
const RequestIface = loadInterfaceXML('org.freedesktop.impl.portal.Request');
|
|
|
|
const AccessIface = loadInterfaceXML('org.freedesktop.impl.portal.Access');
|
2016-07-12 16:47:32 -04:00
|
|
|
|
2017-07-18 13:47:27 -04:00
|
|
|
var DialogResponse = {
|
2016-07-12 16:47:32 -04:00
|
|
|
OK: 0,
|
|
|
|
CANCEL: 1,
|
|
|
|
CLOSED: 2
|
|
|
|
};
|
|
|
|
|
2019-05-23 16:45:44 -04:00
|
|
|
var AccessDialog = GObject.registerClass(
|
|
|
|
class AccessDialog extends ModalDialog.ModalDialog {
|
|
|
|
_init(invocation, handle, title, subtitle, body, options) {
|
|
|
|
super._init({ styleClass: 'access-dialog' });
|
2016-07-12 16:47:32 -04:00
|
|
|
|
|
|
|
this._invocation = invocation;
|
|
|
|
this._handle = handle;
|
|
|
|
|
|
|
|
this._requestExported = false;
|
|
|
|
this._request = Gio.DBusExportedObject.wrapJSObject(RequestIface, this);
|
|
|
|
|
|
|
|
for (let option in options)
|
|
|
|
options[option] = options[option].deep_unpack();
|
|
|
|
|
|
|
|
this._buildLayout(title, subtitle, body, options);
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2016-07-12 16:47:32 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_buildLayout(title, subtitle, body, options) {
|
2016-07-12 16:47:32 -04:00
|
|
|
// No support for non-modal system dialogs, so ignore the option
|
|
|
|
//let modal = options['modal'] || true;
|
|
|
|
let denyLabel = options['deny_label'] || _("Deny Access");
|
|
|
|
let grantLabel = options['grant_label'] || _("Grant Access");
|
|
|
|
let iconName = options['icon'] || null;
|
|
|
|
let choices = options['choices'] || [];
|
|
|
|
|
2017-07-15 00:03:55 -04:00
|
|
|
let contentParams = { title, subtitle, body };
|
|
|
|
if (iconName)
|
|
|
|
contentParams.icon = new Gio.ThemedIcon({ name: iconName });
|
|
|
|
let content = new Dialog.MessageDialogContent(contentParams);
|
|
|
|
this.contentLayout.add_actor(content);
|
2016-07-12 16:47:32 -04:00
|
|
|
|
|
|
|
this._choices = new Map();
|
|
|
|
|
|
|
|
for (let i = 0; i < choices.length; i++) {
|
|
|
|
let [id, name, opts, selected] = choices[i];
|
|
|
|
if (opts.length > 0)
|
|
|
|
continue; // radio buttons, not implemented
|
|
|
|
|
|
|
|
let check = new CheckBox.CheckBox();
|
|
|
|
check.getLabelActor().text = name;
|
|
|
|
check.actor.checked = selected == "true";
|
2017-07-15 00:03:55 -04:00
|
|
|
content.insertBeforeBody(check.actor);
|
2016-07-12 16:47:32 -04:00
|
|
|
|
|
|
|
this._choices.set(id, check);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.addButton({ label: denyLabel,
|
|
|
|
action: () => {
|
|
|
|
this._sendResponse(DialogResponse.CANCEL);
|
|
|
|
},
|
|
|
|
key: Clutter.KEY_Escape });
|
|
|
|
this.addButton({ label: grantLabel,
|
|
|
|
action: () => {
|
|
|
|
this._sendResponse(DialogResponse.OK);
|
|
|
|
}});
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2016-07-12 16:47:32 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
open() {
|
2017-10-30 21:19:44 -04:00
|
|
|
super.open();
|
2016-07-12 16:47:32 -04:00
|
|
|
|
|
|
|
let connection = this._invocation.get_connection();
|
|
|
|
this._requestExported = this._request.export(connection, this._handle);
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2016-07-12 16:47:32 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
CloseAsync(invocation, params) {
|
2016-07-12 16:47:32 -04:00
|
|
|
if (this._invocation.get_sender() != invocation.get_sender()) {
|
|
|
|
invocation.return_error_literal(Gio.DBusError,
|
|
|
|
Gio.DBusError.ACCESS_DENIED,
|
|
|
|
'');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._sendResponse(DialogResponse.CLOSED);
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2016-07-12 16:47:32 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_sendResponse(response) {
|
2016-07-12 16:47:32 -04:00
|
|
|
if (this._requestExported)
|
|
|
|
this._request.unexport();
|
|
|
|
this._requestExported = false;
|
|
|
|
|
|
|
|
let results = {};
|
|
|
|
if (response == DialogResponse.OK) {
|
|
|
|
for (let [id, check] of this._choices) {
|
|
|
|
let checked = check.actor.checked ? 'true' : 'false';
|
|
|
|
results[id] = new GLib.Variant('s', checked);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delay actual response until the end of the close animation (if any)
|
|
|
|
this.connect('closed', () => {
|
|
|
|
this._invocation.return_value(new GLib.Variant('(ua{sv})',
|
|
|
|
[response, results]));
|
|
|
|
});
|
|
|
|
this.close();
|
|
|
|
}
|
2019-05-23 16:45:44 -04:00
|
|
|
});
|
2016-07-12 16:47:32 -04:00
|
|
|
|
2017-10-30 21:19:44 -04:00
|
|
|
var AccessDialogDBus = class {
|
|
|
|
constructor() {
|
2016-07-12 16:47:32 -04:00
|
|
|
this._accessDialog = null;
|
|
|
|
|
|
|
|
this._windowTracker = Shell.WindowTracker.get_default();
|
|
|
|
|
|
|
|
this._dbusImpl = Gio.DBusExportedObject.wrapJSObject(AccessIface, this);
|
|
|
|
this._dbusImpl.export(Gio.DBus.session, '/org/freedesktop/portal/desktop');
|
|
|
|
|
|
|
|
Gio.DBus.session.own_name('org.freedesktop.impl.portal.desktop.gnome', Gio.BusNameOwnerFlags.REPLACE, null, null);
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2016-07-12 16:47:32 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
AccessDialogAsync(params, invocation) {
|
2016-07-12 16:47:32 -04:00
|
|
|
if (this._accessDialog) {
|
|
|
|
invocation.return_error_literal(Gio.DBusError,
|
|
|
|
Gio.DBusError.LIMITS_EXCEEDED,
|
|
|
|
'Already showing a system access dialog');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let [handle, appId, parentWindow, title, subtitle, body, options] = params;
|
|
|
|
// We probably want to use parentWindow and global.display.focus_window
|
|
|
|
// for this check in the future
|
|
|
|
if (appId && appId + '.desktop' != this._windowTracker.focus_app.id) {
|
|
|
|
invocation.return_error_literal(Gio.DBusError,
|
|
|
|
Gio.DBusError.ACCESS_DENIED,
|
|
|
|
'Only the focused app is allowed to show a system access dialog');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let dialog = new AccessDialog(invocation, handle, title,
|
|
|
|
subtitle, body, options);
|
|
|
|
dialog.open();
|
|
|
|
|
|
|
|
dialog.connect('closed', () => { this._accessDialog = null; });
|
|
|
|
|
|
|
|
this._accessDialog = dialog;
|
|
|
|
}
|
2017-10-30 21:19:44 -04:00
|
|
|
};
|