2023-07-10 05:53:00 -04:00
|
|
|
import Clutter from 'gi://Clutter';
|
|
|
|
import Gio from 'gi://Gio';
|
|
|
|
import GLib from 'gi://GLib';
|
|
|
|
import GObject from 'gi://GObject';
|
|
|
|
import Pango from 'gi://Pango';
|
|
|
|
import Shell from 'gi://Shell';
|
|
|
|
import St from 'gi://St';
|
2016-07-12 16:47:32 -04:00
|
|
|
|
2023-07-10 05:53:00 -04:00
|
|
|
import * as CheckBox from './checkBox.js';
|
|
|
|
import * as Dialog from './dialog.js';
|
|
|
|
import * as ModalDialog from './modalDialog.js';
|
2016-07-12 16:47:32 -04:00
|
|
|
|
2023-07-10 05:53:00 -04:00
|
|
|
import {loadInterfaceXML} from '../misc/fileUtils.js';
|
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
|
|
|
|
2023-07-30 08:56:59 -04:00
|
|
|
/** @enum {number} */
|
2023-07-10 05:53:00 -04:00
|
|
|
const DialogResponse = {
|
2016-07-12 16:47:32 -04:00
|
|
|
OK: 0,
|
|
|
|
CANCEL: 1,
|
2019-08-20 17:43:54 -04:00
|
|
|
CLOSED: 2,
|
2016-07-12 16:47:32 -04:00
|
|
|
};
|
|
|
|
|
2023-07-10 05:53:00 -04:00
|
|
|
const AccessDialog = GObject.registerClass(
|
2019-05-23 16:45:44 -04:00
|
|
|
class AccessDialog extends ModalDialog.ModalDialog {
|
2020-01-13 08:07:28 -05:00
|
|
|
_init(invocation, handle, title, description, body, options) {
|
2023-08-06 18:40:20 -04:00
|
|
|
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)
|
2022-08-10 05:56:14 -04:00
|
|
|
options[option] = options[option].deepUnpack();
|
2016-07-12 16:47:32 -04:00
|
|
|
|
2020-01-13 08:07:28 -05:00
|
|
|
this._buildLayout(title, description, body, options);
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2016-07-12 16:47:32 -04:00
|
|
|
|
2020-01-13 08:07:28 -05:00
|
|
|
_buildLayout(title, description, body, options) {
|
2016-07-12 16:47:32 -04:00
|
|
|
// No support for non-modal system dialogs, so ignore the option
|
2019-08-19 13:55:49 -04:00
|
|
|
// let modal = options['modal'] || true;
|
2022-11-16 04:10:37 -05:00
|
|
|
let denyLabel = options['deny_label'] || _('Deny');
|
|
|
|
let grantLabel = options['grant_label'] || _('Allow');
|
2016-07-12 16:47:32 -04:00
|
|
|
let choices = options['choices'] || [];
|
|
|
|
|
2023-08-06 18:40:20 -04:00
|
|
|
let content = new Dialog.MessageDialogContent({title, description});
|
2017-07-15 00:03:55 -04:00
|
|
|
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;
|
2023-06-08 00:53:07 -04:00
|
|
|
check.checked = selected === 'true';
|
2020-01-13 08:04:40 -05:00
|
|
|
content.add_child(check);
|
2016-07-12 16:47:32 -04:00
|
|
|
|
|
|
|
this._choices.set(id, check);
|
|
|
|
}
|
|
|
|
|
2023-01-16 15:41:24 -05:00
|
|
|
if (body) {
|
|
|
|
let bodyLabel = new St.Label({
|
|
|
|
text: body,
|
|
|
|
x_align: Clutter.ActorAlign.CENTER,
|
|
|
|
});
|
|
|
|
bodyLabel.clutter_text.ellipsize = Pango.EllipsizeMode.NONE;
|
|
|
|
bodyLabel.clutter_text.line_wrap = true;
|
|
|
|
content.add_child(bodyLabel);
|
|
|
|
}
|
2020-01-13 08:04:40 -05:00
|
|
|
|
2020-03-29 17:51:13 -04:00
|
|
|
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() {
|
2021-10-30 18:08:12 -04:00
|
|
|
if (!super.open())
|
|
|
|
return false;
|
2016-07-12 16:47:32 -04:00
|
|
|
|
|
|
|
let connection = this._invocation.get_connection();
|
|
|
|
this._requestExported = this._request.export(connection, this._handle);
|
2021-10-30 18:08:12 -04:00
|
|
|
return true;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2016-07-12 16:47:32 -04:00
|
|
|
|
2019-01-31 09:08:10 -05:00
|
|
|
CloseAsync(invocation, _params) {
|
2023-08-06 20:51:19 -04:00
|
|
|
if (this._invocation.get_sender() !== invocation.get_sender()) {
|
2023-08-06 19:45:22 -04:00
|
|
|
invocation.return_error_literal(
|
|
|
|
Gio.DBusError,
|
|
|
|
Gio.DBusError.ACCESS_DENIED,
|
|
|
|
'');
|
2016-07-12 16:47:32 -04:00
|
|
|
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 = {};
|
2023-08-06 20:51:19 -04:00
|
|
|
if (response === DialogResponse.OK) {
|
2016-07-12 16:47:32 -04:00
|
|
|
for (let [id, check] of this._choices) {
|
2019-07-16 05:24:13 -04:00
|
|
|
let checked = check.checked ? 'true' : 'false';
|
2016-07-12 16:47:32 -04:00
|
|
|
results[id] = new GLib.Variant('s', checked);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delay actual response until the end of the close animation (if any)
|
|
|
|
this.connect('closed', () => {
|
2023-08-06 19:45:22 -04:00
|
|
|
this._invocation.return_value(
|
|
|
|
new GLib.Variant('(ua{sv})', [response, results]));
|
2016-07-12 16:47:32 -04:00
|
|
|
});
|
|
|
|
this.close();
|
|
|
|
}
|
2019-05-23 16:45:44 -04:00
|
|
|
});
|
2016-07-12 16:47:32 -04:00
|
|
|
|
2023-07-10 05:53:00 -04:00
|
|
|
export class AccessDialogDBus {
|
2017-10-30 21:19:44 -04:00
|
|
|
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');
|
|
|
|
|
2021-06-05 08:22:00 -04:00
|
|
|
Gio.DBus.session.own_name('org.gnome.Shell.Portal', 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) {
|
2023-08-06 19:45:22 -04:00
|
|
|
invocation.return_error_literal(
|
|
|
|
Gio.DBusError,
|
|
|
|
Gio.DBusError.LIMITS_EXCEEDED,
|
|
|
|
'Already showing a system access dialog');
|
2016-07-12 16:47:32 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-13 08:07:28 -05:00
|
|
|
let [handle, appId, parentWindow_, title, description, body, options] = params;
|
2016-07-12 16:47:32 -04:00
|
|
|
// We probably want to use parentWindow and global.display.focus_window
|
|
|
|
// for this check in the future
|
2022-02-07 09:14:06 -05:00
|
|
|
if (appId && `${appId}.desktop` !== this._windowTracker.focus_app.id) {
|
2023-08-06 19:45:22 -04:00
|
|
|
invocation.return_error_literal(
|
|
|
|
Gio.DBusError,
|
|
|
|
Gio.DBusError.ACCESS_DENIED,
|
|
|
|
'Only the focused app is allowed to show a system access dialog');
|
2016-07-12 16:47:32 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-13 08:07:28 -05:00
|
|
|
let dialog = new AccessDialog(
|
|
|
|
invocation, handle, title, description, body, options);
|
2016-07-12 16:47:32 -04:00
|
|
|
dialog.open();
|
|
|
|
|
2019-08-19 16:20:35 -04:00
|
|
|
dialog.connect('closed', () => (this._accessDialog = null));
|
2016-07-12 16:47:32 -04:00
|
|
|
|
|
|
|
this._accessDialog = dialog;
|
|
|
|
}
|
2023-07-10 05:53:00 -04:00
|
|
|
}
|