2011-09-28 13:16:26 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2008-11-19 23:21:42 +00:00
|
|
|
|
|
|
|
const Clutter = imports.gi.Clutter;
|
2009-12-17 22:12:46 +00:00
|
|
|
const Gio = imports.gi.Gio;
|
2009-08-10 19:49:25 +00:00
|
|
|
const GLib = imports.gi.GLib;
|
2009-08-03 21:52:45 +00:00
|
|
|
const Lang = imports.lang;
|
2009-09-15 21:40:26 +00:00
|
|
|
const Meta = imports.gi.Meta;
|
2010-02-09 17:42:07 +00:00
|
|
|
const St = imports.gi.St;
|
2009-02-02 23:02:16 +00:00
|
|
|
const Shell = imports.gi.Shell;
|
|
|
|
const Signals = imports.signals;
|
2008-11-19 23:21:42 +00:00
|
|
|
|
2011-01-17 21:30:12 +00:00
|
|
|
const FileUtils = imports.misc.fileUtils;
|
2008-11-19 23:21:42 +00:00
|
|
|
const Main = imports.ui.main;
|
2010-12-06 19:41:06 +00:00
|
|
|
const ModalDialog = imports.ui.modalDialog;
|
2011-10-11 22:38:24 +00:00
|
|
|
const ShellEntry = imports.ui.shellEntry;
|
2010-03-17 14:36:57 +00:00
|
|
|
const Tweener = imports.ui.tweener;
|
2010-11-17 16:43:08 +00:00
|
|
|
const Util = imports.misc.util;
|
2011-02-13 16:42:04 +00:00
|
|
|
const History = imports.misc.history;
|
2008-11-19 23:21:42 +00:00
|
|
|
|
2017-07-18 17:47:27 +00:00
|
|
|
var MAX_FILE_DELETED_BEFORE_INVALID = 10;
|
2009-12-17 22:12:46 +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';
|
|
|
|
|
2017-07-18 17:47:27 +00:00
|
|
|
var DIALOG_GROW_TIME = 0.1;
|
2010-12-06 19:41:45 +00:00
|
|
|
|
2017-07-18 17:41:25 +00:00
|
|
|
var RunDialog = new Lang.Class({
|
2011-11-20 16:07:14 +00:00
|
|
|
Name: 'RunDialog',
|
|
|
|
Extends: ModalDialog.ModalDialog,
|
2008-11-19 23:21:42 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_init() {
|
2013-04-06 15:09:06 +00:00
|
|
|
this.parent({ 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
|
|
|
|
2017-10-31 00:38:18 +00:00
|
|
|
this._internalCommands = { 'lg': () => {
|
2009-09-15 19:53:07 +00:00
|
|
|
Main.createLookingGlass().open();
|
2017-10-31 00:38:18 +00:00
|
|
|
},
|
2009-08-26 22:43:44 +00:00
|
|
|
|
2017-12-02 00:27:35 +00:00
|
|
|
'r': this._restart.bind(this),
|
2009-08-26 22:43:44 +00:00
|
|
|
|
|
|
|
// Developer brain backwards compatibility
|
2017-12-02 00:27:35 +00:00
|
|
|
'restart': this._restart.bind(this),
|
2009-09-15 21:40:26 +00:00
|
|
|
|
2017-10-31 00:38:18 +00:00
|
|
|
'debugexit': () => {
|
2011-05-05 22:09:59 +00:00
|
|
|
Meta.quit(Meta.ExitCode.ERROR);
|
2017-10-31 00:38:18 +00:00
|
|
|
},
|
2011-01-04 19:34:54 +00:00
|
|
|
|
|
|
|
// rt is short for "reload theme"
|
2017-10-31 00:38:18 +00:00
|
|
|
'rt': () => {
|
2015-10-21 17:27:19 +00:00
|
|
|
Main.reloadThemeResource();
|
2011-01-04 19:34:54 +00:00
|
|
|
Main.loadTheme();
|
2017-10-31 00:38:18 +00:00
|
|
|
}
|
2009-08-03 21:52:45 +00:00
|
|
|
};
|
|
|
|
|
2009-09-08 18:04:18 +00:00
|
|
|
|
2010-02-26 16:32:38 +00:00
|
|
|
let label = new St.Label({ style_class: 'run-dialog-label',
|
2012-10-29 16:33:21 +00:00
|
|
|
text: _("Enter a Command") });
|
2009-09-08 18:04:18 +00:00
|
|
|
|
2013-11-18 12:53:27 +00:00
|
|
|
this.contentLayout.add(label, { x_fill: false,
|
|
|
|
x_align: St.Align.START,
|
|
|
|
y_align: St.Align.START });
|
2008-12-01 19:51:43 +00:00
|
|
|
|
2012-10-29 18:21:48 +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
|
|
|
|
2012-03-10 01:27:19 +00:00
|
|
|
entry.label_actor = label;
|
|
|
|
|
2010-02-26 16:32:38 +00:00
|
|
|
this._entryText = entry.clutter_text;
|
2010-12-06 19:41:06 +00:00
|
|
|
this.contentLayout.add(entry, { y_align: St.Align.START });
|
2011-03-15 20:05:40 +00:00
|
|
|
this.setInitialKeyFocus(this._entryText);
|
2008-12-01 19:51:43 +00:00
|
|
|
|
2011-01-27 20:26:58 +00:00
|
|
|
this._errorBox = new St.BoxLayout({ style_class: 'run-dialog-error-box' });
|
2009-09-08 18:04:18 +00:00
|
|
|
|
2010-12-06 19:41:06 +00:00
|
|
|
this.contentLayout.add(this._errorBox, { expand: true });
|
2009-09-08 18:04:18 +00:00
|
|
|
|
2012-10-16 16:52:01 +00:00
|
|
|
let errorIcon = new St.Icon({ icon_name: 'dialog-error-symbolic',
|
|
|
|
icon_size: 24,
|
|
|
|
style_class: 'run-dialog-error-icon' });
|
2009-09-08 18:04:18 +00:00
|
|
|
|
2011-01-27 20:26:58 +00:00
|
|
|
this._errorBox.add(errorIcon, { y_align: St.Align.MIDDLE });
|
2009-09-08 18:04:18 +00:00
|
|
|
|
|
|
|
this._commandError = false;
|
|
|
|
|
2010-02-26 16:32:38 +00:00
|
|
|
this._errorMessage = new St.Label({ style_class: 'run-dialog-error-label' });
|
|
|
|
this._errorMessage.clutter_text.line_wrap = true;
|
2009-09-08 18:04:18 +00:00
|
|
|
|
2011-01-27 20:56:12 +00:00
|
|
|
this._errorBox.add(this._errorMessage, { expand: true,
|
2013-11-18 12:53:27 +00:00
|
|
|
x_align: St.Align.START,
|
|
|
|
x_fill: false,
|
2011-01-27 20:56:12 +00:00
|
|
|
y_align: St.Align.MIDDLE,
|
|
|
|
y_fill: false });
|
2009-09-08 18:04:18 +00:00
|
|
|
|
|
|
|
this._errorBox.hide();
|
2008-12-01 19:51:43 +00:00
|
|
|
|
2017-12-02 00:27:35 +00:00
|
|
|
this.setButtons([{ action: this.close.bind(this),
|
2012-10-30 14:00:07 +00:00
|
|
|
label: _("Close"),
|
|
|
|
key: Clutter.Escape }]);
|
2012-10-29 16:33:21 +00:00
|
|
|
|
2009-12-17 22:12:46 +00:00
|
|
|
this._pathCompleter = new Gio.FilenameCompleter();
|
2011-02-13 16:42:04 +00:00
|
|
|
|
2011-02-28 17:22:12 +00:00
|
|
|
this._history = new History.HistoryManager({ gsettingsKey: HISTORY_KEY,
|
|
|
|
entry: this._entryText });
|
2018-08-15 13:39:58 +00:00
|
|
|
this._entryText.connect('activate', (o) => {
|
|
|
|
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 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();
|
2009-12-17 22:12:46 +00:00
|
|
|
if (symbol == Clutter.Tab) {
|
|
|
|
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
|
|
|
});
|
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);
|
|
|
|
}
|
|
|
|
} catch (e if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.NOT_FOUND) &&
|
|
|
|
!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.NOT_DIRECTORY))) {
|
|
|
|
log(e);
|
|
|
|
} finally {
|
|
|
|
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 00:03:21 +00:00
|
|
|
_getCompletion(text) {
|
2009-12-17 22:12:46 +00:00
|
|
|
if (text.indexOf('/') != -1) {
|
|
|
|
return this._pathCompleter.get_completion_suffix(text);
|
|
|
|
} else {
|
2013-03-07 02:10:41 +00:00
|
|
|
return this._getCommandCompletion(text);
|
2009-12-17 22:12:46 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_run(input, inTerminal) {
|
2010-02-26 19:07:44 +00:00
|
|
|
let command = input;
|
2010-03-17 15:22:27 +00:00
|
|
|
|
2011-02-13 16:42:04 +00:00
|
|
|
this._history.addItem(input);
|
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();
|
2010-02-26 19:07:44 +00:00
|
|
|
} else if (input) {
|
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);
|
|
|
|
let exec_arg = this._terminalSettings.get_string(EXEC_ARG_KEY);
|
|
|
|
command = exec + ' ' + exec_arg + ' ' + input;
|
|
|
|
}
|
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;
|
|
|
|
} else {
|
|
|
|
if (input.charAt(0) == '~')
|
|
|
|
input = input.slice(1);
|
|
|
|
path = GLib.get_home_dir() + '/' + input;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GLib.file_test(path, GLib.FileTest.EXISTS)) {
|
|
|
|
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(),
|
2014-01-19 17:34:32 +00:00
|
|
|
global.create_app_launch_context(0, -1));
|
2011-07-06 19:43:49 +00:00
|
|
|
} catch (e) {
|
|
|
|
// 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.
|
|
|
|
let message = e.message.replace(/[^:]*: *(.+)/, '$1');
|
|
|
|
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
|
|
|
}
|
2008-12-01 19:51:43 +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;
|
|
|
|
|
|
|
|
this._errorMessage.set_text(message);
|
|
|
|
|
|
|
|
if (!this._errorBox.visible) {
|
|
|
|
let [errorBoxMinHeight, errorBoxNaturalHeight] = this._errorBox.get_preferred_height(-1);
|
|
|
|
|
|
|
|
let parentActor = this._errorBox.get_parent();
|
|
|
|
Tweener.addTween(parentActor,
|
|
|
|
{ height: parentActor.height + errorBoxNaturalHeight,
|
|
|
|
time: DIALOG_GROW_TIME,
|
|
|
|
transition: 'easeOutQuad',
|
2017-10-31 00:38:18 +00:00
|
|
|
onComplete: () => {
|
|
|
|
parentActor.set_height(-1);
|
|
|
|
this._errorBox.show();
|
|
|
|
}
|
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()) {
|
2016-11-17 21:10:00 +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();
|
2014-07-17 06:48:26 +00:00
|
|
|
Meta.restart(_("Restarting…"));
|
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._errorBox.hide();
|
|
|
|
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))
|
|
|
|
return;
|
|
|
|
|
2011-11-20 16:07:14 +00:00
|
|
|
this.parent();
|
2008-12-01 19:51:43 +00:00
|
|
|
},
|
2011-11-20 16:07:14 +00:00
|
|
|
});
|
2008-11-20 15:40:44 +00:00
|
|
|
Signals.addSignalMethods(RunDialog.prototype);
|