2011-09-28 09:16:26 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2019-01-31 15:07:06 +01:00
|
|
|
/* exported RunDialog */
|
2008-11-19 23:21:42 +00:00
|
|
|
|
2019-05-23 15:45:44 -05:00
|
|
|
const { Clutter, Gio, GLib, GObject, Meta, Shell, St } = imports.gi;
|
2008-11-19 23:21:42 +00:00
|
|
|
|
|
|
|
const Main = imports.ui.main;
|
2010-12-06 14:41:06 -05:00
|
|
|
const ModalDialog = imports.ui.modalDialog;
|
2011-10-12 00:38:24 +02:00
|
|
|
const ShellEntry = imports.ui.shellEntry;
|
2010-11-17 11:43:08 -05:00
|
|
|
const Util = imports.misc.util;
|
2011-02-13 11:42:04 -05:00
|
|
|
const History = imports.misc.history;
|
2008-11-19 23:21:42 +00:00
|
|
|
|
2010-05-05 23:05:42 +02:00
|
|
|
const HISTORY_KEY = 'command-history';
|
2010-03-17 18:22:27 +03:00
|
|
|
|
2011-03-21 14:06:35 +01:00
|
|
|
const LOCKDOWN_SCHEMA = 'org.gnome.desktop.lockdown';
|
|
|
|
const DISABLE_COMMAND_LINE_KEY = 'disable-command-line';
|
|
|
|
|
2011-06-01 13:39:11 +02:00
|
|
|
const TERMINAL_SCHEMA = 'org.gnome.desktop.default-applications.terminal';
|
|
|
|
const EXEC_KEY = 'exec';
|
|
|
|
const EXEC_ARG_KEY = 'exec-arg';
|
|
|
|
|
2019-08-02 01:13:10 +02:00
|
|
|
var DIALOG_GROW_TIME = 100;
|
2010-12-06 14:41:45 -05:00
|
|
|
|
2019-05-23 15:45:44 -05:00
|
|
|
var RunDialog = GObject.registerClass(
|
|
|
|
class RunDialog extends ModalDialog.ModalDialog {
|
|
|
|
_init() {
|
|
|
|
super._init({ styleClass: 'run-dialog',
|
|
|
|
destroyOnClose: false });
|
2009-08-03 17:52:45 -04:00
|
|
|
|
2014-06-24 15:17:09 -04:00
|
|
|
this._lockdownSettings = new Gio.Settings({ schema_id: LOCKDOWN_SCHEMA });
|
|
|
|
this._terminalSettings = new Gio.Settings({ schema_id: TERMINAL_SCHEMA });
|
2017-10-31 01:38:18 +01:00
|
|
|
global.settings.connect('changed::development-tools', () => {
|
2010-05-05 23:05:42 +02:00
|
|
|
this._enableInternalCommands = global.settings.get_boolean('development-tools');
|
2017-10-31 01:38:18 +01:00
|
|
|
});
|
2010-05-05 23:05:42 +02:00
|
|
|
this._enableInternalCommands = global.settings.get_boolean('development-tools');
|
2010-03-17 18:22:27 +03:00
|
|
|
|
2019-01-29 20:36:54 +01:00
|
|
|
this._internalCommands = {
|
2019-01-28 01:42:00 +01:00
|
|
|
'lg': () => Main.createLookingGlass().open(),
|
2009-08-26 18:43:44 -04:00
|
|
|
|
2019-01-29 20:36:54 +01:00
|
|
|
'r': this._restart.bind(this),
|
2009-08-26 18:43:44 -04:00
|
|
|
|
2019-01-29 20:36:54 +01:00
|
|
|
// Developer brain backwards compatibility
|
|
|
|
'restart': this._restart.bind(this),
|
2009-09-15 17:40:26 -04:00
|
|
|
|
2019-01-28 01:42:00 +01:00
|
|
|
'debugexit': () => Meta.quit(Meta.ExitCode.ERROR),
|
2011-01-04 14:34:54 -05:00
|
|
|
|
2019-01-29 20:36:54 +01:00
|
|
|
// rt is short for "reload theme"
|
|
|
|
'rt': () => {
|
|
|
|
Main.reloadThemeResource();
|
|
|
|
Main.loadTheme();
|
|
|
|
},
|
2009-08-03 17:52:45 -04:00
|
|
|
|
2019-01-29 20:36:54 +01:00
|
|
|
'check_cloexec_fds': () => {
|
|
|
|
Shell.util_check_cloexec_fds();
|
|
|
|
},
|
|
|
|
};
|
2009-09-08 20:04:18 +02:00
|
|
|
|
2010-02-26 19:32:38 +03:00
|
|
|
let label = new St.Label({ style_class: 'run-dialog-label',
|
2012-10-29 17:33:21 +01:00
|
|
|
text: _("Enter a Command") });
|
2009-09-08 20:04:18 +02:00
|
|
|
|
2019-10-21 20:44:00 +02:00
|
|
|
this.contentLayout.add_child(label);
|
2008-12-01 19:51:43 +00:00
|
|
|
|
2012-10-29 19:21:48 +01:00
|
|
|
let entry = new St.Entry({ style_class: 'run-dialog-entry',
|
|
|
|
can_focus: true });
|
2011-10-12 00:38:24 +02:00
|
|
|
ShellEntry.addContextMenu(entry);
|
2009-09-08 20:04:18 +02:00
|
|
|
|
2012-03-10 02:27:19 +01:00
|
|
|
entry.label_actor = label;
|
|
|
|
|
2010-02-26 19:32:38 +03:00
|
|
|
this._entryText = entry.clutter_text;
|
2019-10-21 20:44:00 +02:00
|
|
|
this.contentLayout.add_child(entry);
|
2011-03-15 16:05:40 -04:00
|
|
|
this.setInitialKeyFocus(this._entryText);
|
2008-12-01 19:51:43 +00:00
|
|
|
|
2011-01-27 15:26:58 -05:00
|
|
|
this._errorBox = new St.BoxLayout({ style_class: 'run-dialog-error-box' });
|
2009-09-08 20:04:18 +02:00
|
|
|
|
2019-10-21 20:44:00 +02:00
|
|
|
this.contentLayout.add_child(this._errorBox);
|
2009-09-08 20:04:18 +02:00
|
|
|
|
2012-10-16 12:52:01 -04:00
|
|
|
let errorIcon = new St.Icon({ icon_name: 'dialog-error-symbolic',
|
|
|
|
icon_size: 24,
|
2019-10-21 20:44:00 +02:00
|
|
|
style_class: 'run-dialog-error-icon',
|
|
|
|
y_align: Clutter.ActorAlign.CENTER });
|
2009-09-08 20:04:18 +02:00
|
|
|
|
2019-10-21 20:44:00 +02:00
|
|
|
this._errorBox.add_child(errorIcon);
|
2009-09-08 20:04:18 +02:00
|
|
|
|
|
|
|
this._commandError = false;
|
|
|
|
|
2019-10-21 20:44:00 +02:00
|
|
|
this._errorMessage = new St.Label({
|
|
|
|
style_class: 'run-dialog-error-label',
|
|
|
|
y_align: Clutter.ActorAlign.CENTER,
|
|
|
|
});
|
2010-02-26 19:32:38 +03:00
|
|
|
this._errorMessage.clutter_text.line_wrap = true;
|
2009-09-08 20:04:18 +02:00
|
|
|
|
2019-10-21 20:44:00 +02:00
|
|
|
this._errorBox.add_child(this._errorMessage);
|
2009-09-08 20:04:18 +02:00
|
|
|
|
|
|
|
this._errorBox.hide();
|
2008-12-01 19:51:43 +00:00
|
|
|
|
2019-02-12 15:02:09 +01:00
|
|
|
this.setButtons([{
|
|
|
|
action: this.close.bind(this),
|
|
|
|
label: _("Close"),
|
2019-11-05 20:37:28 +01:00
|
|
|
key: Clutter.KEY_Escape,
|
2019-02-12 15:02:09 +01:00
|
|
|
}]);
|
2012-10-29 17:33:21 +01:00
|
|
|
|
2009-12-18 01:12:46 +03:00
|
|
|
this._pathCompleter = new Gio.FilenameCompleter();
|
2011-02-13 11:42:04 -05:00
|
|
|
|
2011-02-28 12:22:12 -05:00
|
|
|
this._history = new History.HistoryManager({ gsettingsKey: HISTORY_KEY,
|
|
|
|
entry: this._entryText });
|
2019-09-14 17:02:29 +02:00
|
|
|
this._entryText.connect('activate', o => {
|
2018-08-15 15:39:58 +02:00
|
|
|
this.popModal();
|
|
|
|
this._run(o.get_text(),
|
|
|
|
Clutter.get_current_event().get_state() & Clutter.ModifierType.CONTROL_MASK);
|
|
|
|
if (!this._commandError ||
|
|
|
|
!this.pushModal())
|
|
|
|
this.close();
|
|
|
|
});
|
2017-10-31 01:38:18 +01:00
|
|
|
this._entryText.connect('key-press-event', (o, e) => {
|
2009-09-08 16:58:57 -04:00
|
|
|
let symbol = e.get_key_symbol();
|
2019-11-05 20:37:28 +01:00
|
|
|
if (symbol === Clutter.KEY_Tab) {
|
2009-12-18 01:12:46 +03: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-18 01:12:46 +03:00
|
|
|
}
|
2013-11-29 18:17:34 +00:00
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2017-10-31 01:38:18 +01:00
|
|
|
});
|
2017-10-31 02:19:44 +01:00
|
|
|
}
|
2008-12-01 19:51:43 +00:00
|
|
|
|
2017-10-31 01:03:21 +01:00
|
|
|
_getCommandCompletion(text) {
|
2013-03-06 21:10:41 -05: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 01:38:18 +01:00
|
|
|
let someResults = paths.map(path => {
|
2013-03-06 21:10:41 -05:00
|
|
|
let results = [];
|
2013-03-18 15:53:11 +01: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 03:17:42 +02:00
|
|
|
} catch (e) {
|
|
|
|
if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.NOT_FOUND) &&
|
|
|
|
!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.NOT_DIRECTORY))
|
|
|
|
log(e);
|
2013-03-06 21:10:41 -05:00
|
|
|
}
|
2019-02-04 13:22:41 +01:00
|
|
|
return results;
|
2013-03-06 21:10:41 -05:00
|
|
|
});
|
2017-10-31 01:38:18 +01:00
|
|
|
let results = someResults.reduce((a, b) => a.concat(b), []);
|
2014-08-13 15:13:20 -07:00
|
|
|
|
|
|
|
if (!results.length)
|
|
|
|
return null;
|
|
|
|
|
2013-03-06 21:10:41 -05:00
|
|
|
let common = results.reduce(_getCommon, null);
|
|
|
|
return common.substr(text.length);
|
2017-10-31 02:19:44 +01:00
|
|
|
}
|
2013-03-06 21:10:41 -05:00
|
|
|
|
2017-10-31 01:03:21 +01:00
|
|
|
_getCompletion(text) {
|
2019-08-20 02:51:42 +02:00
|
|
|
if (text.includes('/'))
|
2009-12-18 01:12:46 +03:00
|
|
|
return this._pathCompleter.get_completion_suffix(text);
|
2019-08-20 02:51:42 +02:00
|
|
|
else
|
2013-03-06 21:10:41 -05:00
|
|
|
return this._getCommandCompletion(text);
|
2017-10-31 02:19:44 +01:00
|
|
|
}
|
2009-12-18 01:12:46 +03:00
|
|
|
|
2017-10-31 01:03:21 +01:00
|
|
|
_run(input, inTerminal) {
|
2010-02-26 20:07:44 +01:00
|
|
|
let command = input;
|
2010-03-17 18:22:27 +03:00
|
|
|
|
2011-02-13 11:42:04 -05:00
|
|
|
this._history.addItem(input);
|
2009-10-01 23:44:16 +02:00
|
|
|
this._commandError = false;
|
2009-09-14 13:45:49 -04:00
|
|
|
let f;
|
|
|
|
if (this._enableInternalCommands)
|
2010-02-26 20:07:44 +01:00
|
|
|
f = this._internalCommands[input];
|
2009-09-14 13:45:49 -04:00
|
|
|
else
|
|
|
|
f = null;
|
2009-08-03 17:52:45 -04:00
|
|
|
if (f) {
|
|
|
|
f();
|
2010-02-26 20:07:44 +01:00
|
|
|
} else if (input) {
|
2008-12-01 19:51:43 +00:00
|
|
|
try {
|
2011-06-01 13:39:11 +02:00
|
|
|
if (inTerminal) {
|
|
|
|
let exec = this._terminalSettings.get_string(EXEC_KEY);
|
2019-01-31 14:43:52 +01:00
|
|
|
let execArg = this._terminalSettings.get_string(EXEC_ARG_KEY);
|
2019-01-30 01:18:24 +01:00
|
|
|
command = `${exec} ${execArg} ${input}`;
|
2011-06-01 13:39:11 +02:00
|
|
|
}
|
2010-11-17 11:43:08 -05:00
|
|
|
Util.trySpawnCommandLine(command);
|
2008-12-01 19:51:43 +00:00
|
|
|
} catch (e) {
|
2010-02-26 20:07:44 +01:00
|
|
|
// Mmmh, that failed - see if @input matches an existing file
|
|
|
|
let path = null;
|
|
|
|
if (input.charAt(0) == '/') {
|
|
|
|
path = input;
|
|
|
|
} else {
|
|
|
|
if (input.charAt(0) == '~')
|
|
|
|
input = input.slice(1);
|
2019-10-29 19:21:21 +01:00
|
|
|
path = `${GLib.get_home_dir()}/${input}`;
|
2010-02-26 20:07:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (GLib.file_test(path, GLib.FileTest.EXISTS)) {
|
|
|
|
let file = Gio.file_new_for_path(path);
|
2011-07-06 20:43:49 +01:00
|
|
|
try {
|
|
|
|
Gio.app_info_launch_default_for_uri(file.get_uri(),
|
2014-01-19 18:34:32 +01:00
|
|
|
global.create_app_launch_context(0, -1));
|
2019-08-20 02:20:08 +02:00
|
|
|
} catch (err) {
|
2011-07-06 20:43:49 +01: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 02:20:08 +02:00
|
|
|
let message = err.message.replace(/[^:]*: *(.+)/, '$1');
|
2011-07-06 20:43:49 +01:00
|
|
|
this._showError(message);
|
2010-12-06 14:41:45 -05:00
|
|
|
}
|
2011-07-06 20:43:49 +01:00
|
|
|
} else {
|
|
|
|
this._showError(e.message);
|
2010-02-26 20:07:44 +01:00
|
|
|
}
|
2008-12-01 19:51:43 +00:00
|
|
|
}
|
2008-11-19 23:21:42 +00:00
|
|
|
}
|
2017-10-31 02:19:44 +01:00
|
|
|
}
|
2008-11-19 23:21:42 +00:00
|
|
|
|
2017-10-31 01:03:21 +01:00
|
|
|
_showError(message) {
|
2011-07-06 20:43:49 +01:00
|
|
|
this._commandError = true;
|
|
|
|
|
|
|
|
this._errorMessage.set_text(message);
|
|
|
|
|
|
|
|
if (!this._errorBox.visible) {
|
2019-02-01 14:41:55 +01:00
|
|
|
let [, errorBoxNaturalHeight] = this._errorBox.get_preferred_height(-1);
|
2011-07-06 20:43:49 +01:00
|
|
|
|
|
|
|
let parentActor = this._errorBox.get_parent();
|
2018-07-20 21:46:19 +02:00
|
|
|
let height = parentActor.height + errorBoxNaturalHeight;
|
|
|
|
parentActor.ease({
|
|
|
|
height,
|
|
|
|
duration: DIALOG_GROW_TIME,
|
|
|
|
mode: Clutter.AnimationMode.EASE_OUT_QUAD,
|
|
|
|
onComplete: () => {
|
|
|
|
parentActor.set_height(-1);
|
|
|
|
this._errorBox.show();
|
2019-08-20 23:43:54 +02:00
|
|
|
},
|
2018-07-20 21:46:19 +02:00
|
|
|
});
|
2011-07-06 20:43:49 +01:00
|
|
|
}
|
2017-10-31 02:19:44 +01:00
|
|
|
}
|
2011-07-06 20:43:49 +01:00
|
|
|
|
2017-10-31 01:03:21 +01:00
|
|
|
_restart() {
|
2015-02-27 19:34:45 +01:00
|
|
|
if (Meta.is_wayland_compositor()) {
|
2016-11-17 22:10:00 +01:00
|
|
|
this._showError(_("Restart is not available on Wayland"));
|
2015-02-27 19:34:45 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-05-08 18:56:23 -04:00
|
|
|
this._shouldFadeOut = false;
|
|
|
|
this.close();
|
2014-07-17 09:48:26 +03:00
|
|
|
Meta.restart(_("Restarting…"));
|
2017-10-31 02:19:44 +01:00
|
|
|
}
|
2014-05-08 18:56:23 -04:00
|
|
|
|
2017-10-31 01:03:21 +01:00
|
|
|
open() {
|
2011-02-13 11:42:04 -05:00
|
|
|
this._history.lastItem();
|
2010-12-06 14:41:06 -05:00
|
|
|
this._errorBox.hide();
|
|
|
|
this._entryText.set_text('');
|
|
|
|
this._commandError = false;
|
2010-03-17 18:22:27 +03:00
|
|
|
|
2011-03-21 14:06:35 +01:00
|
|
|
if (this._lockdownSettings.get_boolean(DISABLE_COMMAND_LINE_KEY))
|
|
|
|
return;
|
|
|
|
|
2017-10-31 02:19:44 +01:00
|
|
|
super.open();
|
|
|
|
}
|
2019-05-23 15:45:44 -05:00
|
|
|
});
|