2011-09-28 13:16:26 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2023-07-10 09:53:00 +00:00
|
|
|
|
|
|
|
import Clutter from 'gi://Clutter';
|
|
|
|
import Gio from 'gi://Gio';
|
|
|
|
import GLib from 'gi://GLib';
|
|
|
|
import GObject from 'gi://GObject';
|
|
|
|
import Meta from 'gi://Meta';
|
|
|
|
import Shell from 'gi://Shell';
|
|
|
|
import St from 'gi://St';
|
|
|
|
|
|
|
|
import * as Dialog from './dialog.js';
|
|
|
|
import * as Main from './main.js';
|
|
|
|
import * as ModalDialog from './modalDialog.js';
|
|
|
|
import * as ShellEntry from './shellEntry.js';
|
|
|
|
import * as Util from '../misc/util.js';
|
|
|
|
import * as History from '../misc/history.js';
|
2008-11-19 23:21:42 +00:00
|
|
|
|
2010-05-05 21:05:42 +00:00
|
|
|
const HISTORY_KEY = 'command-history';
|
2010-03-17 15:22:27 +00:00
|
|
|
|
2011-03-21 13:06:35 +00:00
|
|
|
const LOCKDOWN_SCHEMA = 'org.gnome.desktop.lockdown';
|
|
|
|
const DISABLE_COMMAND_LINE_KEY = 'disable-command-line';
|
|
|
|
|
2011-06-01 11:39:11 +00:00
|
|
|
const TERMINAL_SCHEMA = 'org.gnome.desktop.default-applications.terminal';
|
|
|
|
const EXEC_KEY = 'exec';
|
|
|
|
const EXEC_ARG_KEY = 'exec-arg';
|
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
export const RunDialog = GObject.registerClass(
|
2019-05-23 20:45:44 +00:00
|
|
|
class RunDialog extends ModalDialog.ModalDialog {
|
|
|
|
_init() {
|
2020-01-27 15:31:51 +00:00
|
|
|
super._init({
|
|
|
|
styleClass: 'run-dialog',
|
|
|
|
destroyOnClose: false,
|
|
|
|
});
|
2009-08-03 21:52:45 +00:00
|
|
|
|
2014-06-24 19:17:09 +00:00
|
|
|
this._lockdownSettings = new Gio.Settings({ schema_id: LOCKDOWN_SCHEMA });
|
|
|
|
this._terminalSettings = new Gio.Settings({ schema_id: TERMINAL_SCHEMA });
|
2017-10-31 00:38:18 +00:00
|
|
|
global.settings.connect('changed::development-tools', () => {
|
2010-05-05 21:05:42 +00:00
|
|
|
this._enableInternalCommands = global.settings.get_boolean('development-tools');
|
2017-10-31 00:38:18 +00:00
|
|
|
});
|
2010-05-05 21:05:42 +00:00
|
|
|
this._enableInternalCommands = global.settings.get_boolean('development-tools');
|
2010-03-17 15:22:27 +00:00
|
|
|
|
2019-01-29 19:36:54 +00:00
|
|
|
this._internalCommands = {
|
2019-01-28 00:42:00 +00:00
|
|
|
'lg': () => Main.createLookingGlass().open(),
|
2009-08-26 22:43:44 +00:00
|
|
|
|
2019-01-29 19:36:54 +00:00
|
|
|
'r': this._restart.bind(this),
|
2009-08-26 22:43:44 +00:00
|
|
|
|
2019-01-29 19:36:54 +00:00
|
|
|
// Developer brain backwards compatibility
|
|
|
|
'restart': this._restart.bind(this),
|
2009-09-15 21:40:26 +00:00
|
|
|
|
2021-07-16 19:39:51 +00:00
|
|
|
'debugexit': () => global.context.terminate(),
|
2011-01-04 19:34:54 +00:00
|
|
|
|
2019-01-29 19:36:54 +00:00
|
|
|
// rt is short for "reload theme"
|
|
|
|
'rt': () => {
|
|
|
|
Main.reloadThemeResource();
|
|
|
|
Main.loadTheme();
|
|
|
|
},
|
2009-08-03 21:52:45 +00:00
|
|
|
|
2019-01-29 19:36:54 +00:00
|
|
|
'check_cloexec_fds': () => {
|
|
|
|
Shell.util_check_cloexec_fds();
|
|
|
|
},
|
|
|
|
};
|
2009-09-08 18:04:18 +00:00
|
|
|
|
2019-12-08 12:09:25 +00:00
|
|
|
let title = _('Run a Command');
|
|
|
|
|
|
|
|
let content = new Dialog.MessageDialogContent({ title });
|
|
|
|
this.contentLayout.add_actor(content);
|
2008-12-01 19:51:43 +00:00
|
|
|
|
2020-01-27 15:31:51 +00:00
|
|
|
let entry = new St.Entry({
|
|
|
|
style_class: 'run-dialog-entry',
|
|
|
|
can_focus: true,
|
|
|
|
});
|
2011-10-11 22:38:24 +00:00
|
|
|
ShellEntry.addContextMenu(entry);
|
2009-09-08 18:04:18 +00:00
|
|
|
|
2010-02-26 16:32:38 +00:00
|
|
|
this._entryText = entry.clutter_text;
|
2019-12-08 12:09:25 +00:00
|
|
|
content.add_child(entry);
|
2011-03-15 20:05:40 +00:00
|
|
|
this.setInitialKeyFocus(this._entryText);
|
2008-12-01 19:51:43 +00:00
|
|
|
|
2019-12-08 12:09:25 +00:00
|
|
|
let defaultDescriptionText = _('Press ESC to close');
|
2009-09-08 18:04:18 +00:00
|
|
|
|
2019-12-08 12:09:25 +00:00
|
|
|
this._descriptionLabel = new St.Label({
|
|
|
|
style_class: 'run-dialog-description',
|
|
|
|
text: defaultDescriptionText,
|
2020-01-27 15:31:51 +00:00
|
|
|
});
|
2019-12-08 12:09:25 +00:00
|
|
|
content.add_child(this._descriptionLabel);
|
2009-09-08 18:04:18 +00:00
|
|
|
|
|
|
|
this._commandError = false;
|
|
|
|
|
2009-12-17 22:12:46 +00:00
|
|
|
this._pathCompleter = new Gio.FilenameCompleter();
|
2011-02-13 16:42:04 +00:00
|
|
|
|
2020-01-27 15:31:51 +00:00
|
|
|
this._history = new History.HistoryManager({
|
|
|
|
gsettingsKey: HISTORY_KEY,
|
|
|
|
entry: this._entryText,
|
|
|
|
});
|
2019-09-14 15:02:29 +00:00
|
|
|
this._entryText.connect('activate', o => {
|
2018-08-15 13:39:58 +00:00
|
|
|
this.popModal();
|
|
|
|
this._run(o.get_text(),
|
2020-01-27 15:31:51 +00:00
|
|
|
Clutter.get_current_event().get_state() & Clutter.ModifierType.CONTROL_MASK);
|
2018-08-15 13:39:58 +00:00
|
|
|
if (!this._commandError ||
|
|
|
|
!this.pushModal())
|
|
|
|
this.close();
|
|
|
|
});
|
2017-10-31 00:38:18 +00:00
|
|
|
this._entryText.connect('key-press-event', (o, e) => {
|
2009-09-08 20:58:57 +00:00
|
|
|
let symbol = e.get_key_symbol();
|
2019-11-05 19:37:28 +00:00
|
|
|
if (symbol === Clutter.KEY_Tab) {
|
2009-12-17 22:12:46 +00:00
|
|
|
let text = o.get_text();
|
|
|
|
let prefix;
|
|
|
|
if (text.lastIndexOf(' ') == -1)
|
|
|
|
prefix = text;
|
|
|
|
else
|
|
|
|
prefix = text.substr(text.lastIndexOf(' ') + 1);
|
|
|
|
let postfix = this._getCompletion(prefix);
|
|
|
|
if (postfix != null && postfix.length > 0) {
|
|
|
|
o.insert_text(postfix, -1);
|
|
|
|
o.set_cursor_position(text.length + postfix.length);
|
|
|
|
}
|
2013-11-29 18:17:34 +00:00
|
|
|
return Clutter.EVENT_STOP;
|
2009-12-17 22:12:46 +00:00
|
|
|
}
|
2013-11-29 18:17:34 +00:00
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2017-10-31 00:38:18 +00:00
|
|
|
});
|
2019-12-08 12:09:25 +00:00
|
|
|
this._entryText.connect('text-changed', () => {
|
|
|
|
this._descriptionLabel.set_text(defaultDescriptionText);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
vfunc_key_release_event(event) {
|
|
|
|
if (event.keyval === Clutter.KEY_Escape) {
|
|
|
|
this.close();
|
|
|
|
return Clutter.EVENT_STOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2008-12-01 19:51:43 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_getCommandCompletion(text) {
|
2013-03-07 02:10:41 +00:00
|
|
|
function _getCommon(s1, s2) {
|
|
|
|
if (s1 == null)
|
|
|
|
return s2;
|
|
|
|
|
|
|
|
let k = 0;
|
|
|
|
for (; k < s1.length && k < s2.length; k++) {
|
|
|
|
if (s1[k] != s2[k])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (k == 0)
|
|
|
|
return '';
|
|
|
|
return s1.substr(0, k);
|
|
|
|
}
|
|
|
|
|
|
|
|
let paths = GLib.getenv('PATH').split(':');
|
|
|
|
paths.push(GLib.get_home_dir());
|
2017-10-31 00:38:18 +00:00
|
|
|
let someResults = paths.map(path => {
|
2013-03-07 02:10:41 +00:00
|
|
|
let results = [];
|
2013-03-18 14:53:11 +00:00
|
|
|
try {
|
|
|
|
let file = Gio.File.new_for_path(path);
|
|
|
|
let fileEnum = file.enumerate_children('standard::name', Gio.FileQueryInfoFlags.NONE, null);
|
|
|
|
let info;
|
|
|
|
while ((info = fileEnum.next_file(null))) {
|
|
|
|
let name = info.get_name();
|
|
|
|
if (name.slice(0, text.length) == text)
|
|
|
|
results.push(name);
|
|
|
|
}
|
2018-07-15 01:17:42 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.NOT_FOUND) &&
|
|
|
|
!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.NOT_DIRECTORY))
|
|
|
|
log(e);
|
2013-03-07 02:10:41 +00:00
|
|
|
}
|
2019-02-04 12:22:41 +00:00
|
|
|
return results;
|
2013-03-07 02:10:41 +00:00
|
|
|
});
|
2017-10-31 00:38:18 +00:00
|
|
|
let results = someResults.reduce((a, b) => a.concat(b), []);
|
2014-08-13 22:13:20 +00:00
|
|
|
|
|
|
|
if (!results.length)
|
|
|
|
return null;
|
|
|
|
|
2013-03-07 02:10:41 +00:00
|
|
|
let common = results.reduce(_getCommon, null);
|
|
|
|
return common.substr(text.length);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2013-03-07 02:10:41 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_getCompletion(text) {
|
2019-08-20 00:51:42 +00:00
|
|
|
if (text.includes('/'))
|
2009-12-17 22:12:46 +00:00
|
|
|
return this._pathCompleter.get_completion_suffix(text);
|
2019-08-20 00:51:42 +00:00
|
|
|
else
|
2013-03-07 02:10:41 +00:00
|
|
|
return this._getCommandCompletion(text);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2009-12-17 22:12:46 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_run(input, inTerminal) {
|
2021-02-11 18:35:07 +00:00
|
|
|
input = this._history.addItem(input); // trims input
|
2010-02-26 19:07:44 +00:00
|
|
|
let command = input;
|
2010-03-17 15:22:27 +00:00
|
|
|
|
2009-10-01 21:44:16 +00:00
|
|
|
this._commandError = false;
|
2009-09-14 17:45:49 +00:00
|
|
|
let f;
|
|
|
|
if (this._enableInternalCommands)
|
2010-02-26 19:07:44 +00:00
|
|
|
f = this._internalCommands[input];
|
2009-09-14 17:45:49 +00:00
|
|
|
else
|
|
|
|
f = null;
|
2009-08-03 21:52:45 +00:00
|
|
|
if (f) {
|
|
|
|
f();
|
2021-02-07 18:12:57 +00:00
|
|
|
} else {
|
2008-12-01 19:51:43 +00:00
|
|
|
try {
|
2011-06-01 11:39:11 +00:00
|
|
|
if (inTerminal) {
|
|
|
|
let exec = this._terminalSettings.get_string(EXEC_KEY);
|
2019-01-31 13:43:52 +00:00
|
|
|
let execArg = this._terminalSettings.get_string(EXEC_ARG_KEY);
|
2022-02-07 14:14:06 +00:00
|
|
|
command = `${exec} ${execArg} ${input}`;
|
2011-06-01 11:39:11 +00:00
|
|
|
}
|
2010-11-17 16:43:08 +00:00
|
|
|
Util.trySpawnCommandLine(command);
|
2008-12-01 19:51:43 +00:00
|
|
|
} catch (e) {
|
2010-02-26 19:07:44 +00:00
|
|
|
// Mmmh, that failed - see if @input matches an existing file
|
|
|
|
let path = null;
|
|
|
|
if (input.charAt(0) == '/') {
|
|
|
|
path = input;
|
2021-02-07 18:12:57 +00:00
|
|
|
} else if (input) {
|
2010-02-26 19:07:44 +00:00
|
|
|
if (input.charAt(0) == '~')
|
|
|
|
input = input.slice(1);
|
2022-02-07 14:14:06 +00:00
|
|
|
path = `${GLib.get_home_dir()}/${input}`;
|
2010-02-26 19:07:44 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 18:12:57 +00:00
|
|
|
if (path && GLib.file_test(path, GLib.FileTest.EXISTS)) {
|
2010-02-26 19:07:44 +00:00
|
|
|
let file = Gio.file_new_for_path(path);
|
2011-07-06 19:43:49 +00:00
|
|
|
try {
|
|
|
|
Gio.app_info_launch_default_for_uri(file.get_uri(),
|
2020-01-27 15:31:51 +00:00
|
|
|
global.create_app_launch_context(0, -1));
|
2019-08-20 00:20:08 +00:00
|
|
|
} catch (err) {
|
2011-07-06 19:43:49 +00:00
|
|
|
// The exception from gjs contains an error string like:
|
|
|
|
// Error invoking Gio.app_info_launch_default_for_uri: No application
|
|
|
|
// is registered as handling this file
|
|
|
|
// We are only interested in the part after the first colon.
|
2019-08-20 00:20:08 +00:00
|
|
|
let message = err.message.replace(/[^:]*: *(.+)/, '$1');
|
2011-07-06 19:43:49 +00:00
|
|
|
this._showError(message);
|
2010-12-06 19:41:45 +00:00
|
|
|
}
|
2011-07-06 19:43:49 +00:00
|
|
|
} else {
|
|
|
|
this._showError(e.message);
|
2010-02-26 19:07:44 +00:00
|
|
|
}
|
2008-12-01 19:51:43 +00:00
|
|
|
}
|
2008-11-19 23:21:42 +00:00
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2008-11-19 23:21:42 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_showError(message) {
|
2011-07-06 19:43:49 +00:00
|
|
|
this._commandError = true;
|
2019-12-08 12:09:25 +00:00
|
|
|
this._descriptionLabel.set_text(message);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-07-06 19:43:49 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_restart() {
|
2015-02-27 18:34:45 +00:00
|
|
|
if (Meta.is_wayland_compositor()) {
|
2022-05-31 11:10:16 +00:00
|
|
|
this._showError(_('Restart is not available on Wayland'));
|
2015-02-27 18:34:45 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-05-08 22:56:23 +00:00
|
|
|
this._shouldFadeOut = false;
|
|
|
|
this.close();
|
2022-05-31 10:59:56 +00:00
|
|
|
Meta.restart(_('Restarting…'), global.context);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2014-05-08 22:56:23 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
open() {
|
2011-02-13 16:42:04 +00:00
|
|
|
this._history.lastItem();
|
2010-12-06 19:41:06 +00:00
|
|
|
this._entryText.set_text('');
|
|
|
|
this._commandError = false;
|
2010-03-17 15:22:27 +00:00
|
|
|
|
2011-03-21 13:06:35 +00:00
|
|
|
if (this._lockdownSettings.get_boolean(DISABLE_COMMAND_LINE_KEY))
|
2021-10-30 22:08:12 +00:00
|
|
|
return false;
|
2011-03-21 13:06:35 +00:00
|
|
|
|
2021-10-30 22:08:12 +00:00
|
|
|
return super.open();
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2019-05-23 20:45:44 +00:00
|
|
|
});
|