2011-09-28 09:16:26 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2008-11-19 18:21:42 -05:00
|
|
|
|
|
|
|
const Clutter = imports.gi.Clutter;
|
2009-12-17 17:12:46 -05:00
|
|
|
const Gio = imports.gi.Gio;
|
2009-08-10 15:49:25 -04:00
|
|
|
const GLib = imports.gi.GLib;
|
2009-08-03 17:52:45 -04:00
|
|
|
const Lang = imports.lang;
|
2009-09-15 17:40:26 -04:00
|
|
|
const Meta = imports.gi.Meta;
|
2010-02-09 12:42:07 -05:00
|
|
|
const St = imports.gi.St;
|
2009-02-02 18:02:16 -05:00
|
|
|
const Shell = imports.gi.Shell;
|
|
|
|
const Signals = imports.signals;
|
2008-11-19 18:21:42 -05:00
|
|
|
|
2011-01-17 16:30:12 -05:00
|
|
|
const FileUtils = imports.misc.fileUtils;
|
2008-11-19 18:21:42 -05:00
|
|
|
const Main = imports.ui.main;
|
2010-12-06 14:41:06 -05:00
|
|
|
const ModalDialog = imports.ui.modalDialog;
|
2011-10-11 18:38:24 -04:00
|
|
|
const ShellEntry = imports.ui.shellEntry;
|
2010-03-17 10:36:57 -04:00
|
|
|
const Tweener = imports.ui.tweener;
|
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 18:21:42 -05:00
|
|
|
|
2009-12-17 17:12:46 -05:00
|
|
|
const MAX_FILE_DELETED_BEFORE_INVALID = 10;
|
|
|
|
|
2010-05-05 17:05:42 -04:00
|
|
|
const HISTORY_KEY = 'command-history';
|
2010-03-17 11:22:27 -04:00
|
|
|
|
2011-03-21 09:06:35 -04:00
|
|
|
const LOCKDOWN_SCHEMA = 'org.gnome.desktop.lockdown';
|
|
|
|
const DISABLE_COMMAND_LINE_KEY = 'disable-command-line';
|
|
|
|
|
2011-06-01 07:39:11 -04:00
|
|
|
const TERMINAL_SCHEMA = 'org.gnome.desktop.default-applications.terminal';
|
|
|
|
const EXEC_KEY = 'exec';
|
|
|
|
const EXEC_ARG_KEY = 'exec-arg';
|
|
|
|
|
2010-12-06 14:41:45 -05:00
|
|
|
const DIALOG_GROW_TIME = 0.1;
|
|
|
|
|
2009-12-17 17:12:46 -05:00
|
|
|
function CommandCompleter() {
|
|
|
|
this._init();
|
|
|
|
}
|
|
|
|
|
|
|
|
CommandCompleter.prototype = {
|
|
|
|
_init : function() {
|
|
|
|
this._changedCount = 0;
|
|
|
|
this._paths = GLib.getenv('PATH').split(':');
|
2010-02-26 14:07:44 -05:00
|
|
|
this._paths.push(GLib.get_home_dir());
|
2009-12-17 17:12:46 -05:00
|
|
|
this._valid = false;
|
|
|
|
this._updateInProgress = false;
|
|
|
|
this._childs = new Array(this._paths.length);
|
|
|
|
this._monitors = new Array(this._paths.length);
|
|
|
|
for (let i = 0; i < this._paths.length; i++) {
|
|
|
|
this._childs[i] = [];
|
|
|
|
let file = Gio.file_new_for_path(this._paths[i]);
|
2009-12-19 00:41:05 -05:00
|
|
|
let info;
|
|
|
|
try {
|
|
|
|
info = file.query_info(Gio.FILE_ATTRIBUTE_STANDARD_TYPE, Gio.FileQueryInfoFlags.NONE, null);
|
|
|
|
} catch (e) {
|
|
|
|
// FIXME catchall
|
|
|
|
this._paths[i] = null;
|
|
|
|
continue;
|
|
|
|
}
|
2009-12-17 17:12:46 -05:00
|
|
|
|
|
|
|
if (info.get_attribute_uint32(Gio.FILE_ATTRIBUTE_STANDARD_TYPE) != Gio.FileType.DIRECTORY)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
this._paths[i] = file.get_path();
|
|
|
|
this._monitors[i] = file.monitor_directory(Gio.FileMonitorFlags.NONE, null);
|
|
|
|
if (this._monitors[i] != null) {
|
2010-05-13 15:46:04 -04:00
|
|
|
this._monitors[i].connect('changed', Lang.bind(this, this._onChanged));
|
2009-12-17 17:12:46 -05:00
|
|
|
}
|
|
|
|
}
|
2009-12-19 00:41:05 -05:00
|
|
|
this._paths = this._paths.filter(function(a) {
|
|
|
|
return a != null;
|
|
|
|
});
|
2009-12-17 17:12:46 -05:00
|
|
|
this._update(0);
|
|
|
|
},
|
|
|
|
|
|
|
|
update : function() {
|
|
|
|
if (this._valid)
|
|
|
|
return;
|
|
|
|
this._update(0);
|
|
|
|
},
|
|
|
|
|
|
|
|
_update : function(i) {
|
|
|
|
if (i == 0 && this._updateInProgress)
|
|
|
|
return;
|
|
|
|
this._updateInProgress = true;
|
|
|
|
this._changedCount = 0;
|
|
|
|
this._i = i;
|
|
|
|
if (i >= this._paths.length) {
|
|
|
|
this._valid = true;
|
|
|
|
this._updateInProgress = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let file = Gio.file_new_for_path(this._paths[i]);
|
|
|
|
this._childs[this._i] = [];
|
2011-01-17 16:30:12 -05:00
|
|
|
FileUtils.listDirAsync(file, Lang.bind(this, function (files) {
|
|
|
|
for (let i = 0; i < files.length; i++) {
|
|
|
|
this._childs[this._i].push(files[i].get_name());
|
|
|
|
}
|
|
|
|
this._update(this._i + 1);
|
|
|
|
}));
|
2009-12-17 17:12:46 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
_onChanged : function(m, f, of, type) {
|
|
|
|
if (!this._valid)
|
|
|
|
return;
|
|
|
|
let path = f.get_parent().get_path();
|
|
|
|
let k = undefined;
|
|
|
|
for (let i = 0; i < this._paths.length; i++) {
|
|
|
|
if (this._paths[i] == path)
|
|
|
|
k = i;
|
|
|
|
}
|
|
|
|
if (k === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (type == Gio.FileMonitorEvent.CREATED) {
|
|
|
|
this._childs[k].push(f.get_basename());
|
|
|
|
}
|
|
|
|
if (type == Gio.FileMonitorEvent.DELETED) {
|
|
|
|
this._changedCount++;
|
|
|
|
if (this._changedCount > MAX_FILE_DELETED_BEFORE_INVALID) {
|
|
|
|
this._valid = false;
|
|
|
|
}
|
|
|
|
let name = f.get_basename();
|
|
|
|
this._childs[k] = this._childs[k].filter(function(e) {
|
|
|
|
return e != name;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (type == Gio.FileMonitorEvent.UNMOUNTED) {
|
|
|
|
this._childs[k] = [];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
getCompletion: function(text) {
|
2010-05-13 15:46:04 -04:00
|
|
|
let common = '';
|
2009-12-17 17:12:46 -05:00
|
|
|
let notInit = true;
|
|
|
|
if (!this._valid) {
|
|
|
|
this._update(0);
|
|
|
|
return common;
|
|
|
|
}
|
|
|
|
function _getCommon(s1, s2) {
|
|
|
|
let k = 0;
|
|
|
|
for (; k < s1.length && k < s2.length; k++) {
|
|
|
|
if (s1[k] != s2[k])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (k == 0)
|
2010-05-13 15:46:04 -04:00
|
|
|
return '';
|
2009-12-17 17:12:46 -05:00
|
|
|
return s1.substr(0, k);
|
|
|
|
}
|
|
|
|
function _hasPrefix(s1, prefix) {
|
|
|
|
return s1.indexOf(prefix) == 0;
|
|
|
|
}
|
|
|
|
for (let i = 0; i < this._childs.length; i++) {
|
|
|
|
for (let k = 0; k < this._childs[i].length; k++) {
|
|
|
|
if (!_hasPrefix(this._childs[i][k], text))
|
|
|
|
continue;
|
|
|
|
if (notInit) {
|
|
|
|
common = this._childs[i][k];
|
|
|
|
notInit = false;
|
|
|
|
}
|
|
|
|
common = _getCommon(common, this._childs[i][k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (common.length)
|
|
|
|
return common.substr(text.length);
|
|
|
|
return common;
|
|
|
|
}
|
|
|
|
};
|
2008-11-19 18:21:42 -05:00
|
|
|
|
|
|
|
function RunDialog() {
|
|
|
|
this._init();
|
2010-03-15 09:50:05 -04:00
|
|
|
}
|
2008-11-19 18:21:42 -05:00
|
|
|
|
|
|
|
RunDialog.prototype = {
|
2010-12-06 14:41:06 -05:00
|
|
|
__proto__: ModalDialog.ModalDialog.prototype,
|
2008-12-01 14:51:43 -05:00
|
|
|
_init : function() {
|
2010-12-06 14:41:06 -05:00
|
|
|
ModalDialog.ModalDialog.prototype._init.call(this, { styleClass: 'run-dialog' });
|
2009-08-03 17:52:45 -04:00
|
|
|
|
2011-03-21 09:06:35 -04:00
|
|
|
this._lockdownSettings = new Gio.Settings({ schema: LOCKDOWN_SCHEMA });
|
2011-06-01 07:39:11 -04:00
|
|
|
this._terminalSettings = new Gio.Settings({ schema: TERMINAL_SCHEMA });
|
2010-05-05 17:05:42 -04:00
|
|
|
global.settings.connect('changed::development-tools', Lang.bind(this, function () {
|
|
|
|
this._enableInternalCommands = global.settings.get_boolean('development-tools');
|
2010-03-17 11:22:27 -04:00
|
|
|
}));
|
2010-05-05 17:05:42 -04:00
|
|
|
this._enableInternalCommands = global.settings.get_boolean('development-tools');
|
2010-03-17 11:22:27 -04:00
|
|
|
|
2009-08-02 03:46:01 -04:00
|
|
|
this._internalCommands = { 'lg':
|
|
|
|
Lang.bind(this, function() {
|
2009-09-15 15:53:07 -04:00
|
|
|
Main.createLookingGlass().open();
|
2009-08-02 03:46:01 -04:00
|
|
|
}),
|
2009-08-26 18:43:44 -04:00
|
|
|
|
|
|
|
'r': Lang.bind(this, function() {
|
|
|
|
global.reexec_self();
|
|
|
|
}),
|
|
|
|
|
|
|
|
// Developer brain backwards compatibility
|
2009-08-02 03:46:01 -04:00
|
|
|
'restart': Lang.bind(this, function() {
|
2009-08-03 17:52:45 -04:00
|
|
|
global.reexec_self();
|
2009-09-15 17:40:26 -04:00
|
|
|
}),
|
|
|
|
|
|
|
|
'debugexit': Lang.bind(this, function() {
|
2011-05-05 18:09:59 -04:00
|
|
|
Meta.quit(Meta.ExitCode.ERROR);
|
2011-01-04 14:34:54 -05:00
|
|
|
}),
|
|
|
|
|
|
|
|
// rt is short for "reload theme"
|
|
|
|
'rt': Lang.bind(this, function() {
|
|
|
|
Main.loadTheme();
|
2009-08-03 17:52:45 -04:00
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2009-09-08 14:04:18 -04:00
|
|
|
|
2010-02-26 11:32:38 -05:00
|
|
|
let label = new St.Label({ style_class: 'run-dialog-label',
|
|
|
|
text: _("Please enter a command:") });
|
2009-09-08 14:04:18 -04:00
|
|
|
|
2010-12-06 14:41:06 -05:00
|
|
|
this.contentLayout.add(label, { y_align: St.Align.START });
|
2008-12-01 14:51:43 -05:00
|
|
|
|
2010-02-26 11:32:38 -05:00
|
|
|
let entry = new St.Entry({ style_class: 'run-dialog-entry' });
|
2011-10-11 18:38:24 -04:00
|
|
|
ShellEntry.addContextMenu(entry);
|
2009-09-08 14:04:18 -04:00
|
|
|
|
2010-02-26 11:32:38 -05:00
|
|
|
this._entryText = entry.clutter_text;
|
2010-12-06 14:41:06 -05:00
|
|
|
this.contentLayout.add(entry, { y_align: St.Align.START });
|
2011-03-15 16:05:40 -04:00
|
|
|
this.setInitialKeyFocus(this._entryText);
|
2008-12-01 14:51:43 -05:00
|
|
|
|
2011-01-27 15:26:58 -05:00
|
|
|
this._errorBox = new St.BoxLayout({ style_class: 'run-dialog-error-box' });
|
2009-09-08 14:04:18 -04:00
|
|
|
|
2010-12-06 14:41:06 -05:00
|
|
|
this.contentLayout.add(this._errorBox, { expand: true });
|
2009-09-08 14:04:18 -04:00
|
|
|
|
2011-01-27 15:26:58 -05:00
|
|
|
let errorIcon = new St.Icon({ icon_name: 'dialog-error', icon_size: 24, style_class: 'run-dialog-error-icon' });
|
2009-09-08 14:04:18 -04:00
|
|
|
|
2011-01-27 15:26:58 -05:00
|
|
|
this._errorBox.add(errorIcon, { y_align: St.Align.MIDDLE });
|
2009-09-08 14:04:18 -04:00
|
|
|
|
|
|
|
this._commandError = false;
|
|
|
|
|
2010-02-26 11:32:38 -05:00
|
|
|
this._errorMessage = new St.Label({ style_class: 'run-dialog-error-label' });
|
|
|
|
this._errorMessage.clutter_text.line_wrap = true;
|
2009-09-08 14:04:18 -04:00
|
|
|
|
2011-01-27 15:56:12 -05:00
|
|
|
this._errorBox.add(this._errorMessage, { expand: true,
|
|
|
|
y_align: St.Align.MIDDLE,
|
|
|
|
y_fill: false });
|
2009-09-08 14:04:18 -04:00
|
|
|
|
|
|
|
this._errorBox.hide();
|
2008-12-01 14:51:43 -05:00
|
|
|
|
2009-12-17 17:12:46 -05:00
|
|
|
this._pathCompleter = new Gio.FilenameCompleter();
|
|
|
|
this._commandCompleter = new CommandCompleter();
|
|
|
|
this._group.connect('notify::visible', Lang.bind(this._commandCompleter, this._commandCompleter.update));
|
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 });
|
2010-02-26 11:32:38 -05:00
|
|
|
this._entryText.connect('key-press-event', Lang.bind(this, function(o, e) {
|
2009-09-08 16:58:57 -04:00
|
|
|
let symbol = e.get_key_symbol();
|
2010-03-31 09:29:11 -04:00
|
|
|
if (symbol == Clutter.Return || symbol == Clutter.KP_Enter) {
|
2011-03-18 11:28:18 -04:00
|
|
|
this.popModal();
|
2010-03-24 13:15:50 -04:00
|
|
|
if (Shell.get_event_state(e) & Clutter.ModifierType.CONTROL_MASK)
|
2010-02-26 11:32:38 -05:00
|
|
|
this._run(o.get_text(), true);
|
|
|
|
else
|
|
|
|
this._run(o.get_text(), false);
|
|
|
|
if (!this._commandError)
|
2011-03-18 11:28:18 -04:00
|
|
|
this.close();
|
|
|
|
else {
|
|
|
|
if (!this.pushModal())
|
|
|
|
this.close();
|
|
|
|
}
|
2011-03-11 10:42:20 -05:00
|
|
|
return true;
|
2010-02-26 11:32:38 -05:00
|
|
|
}
|
2009-08-03 17:52:45 -04:00
|
|
|
if (symbol == Clutter.Escape) {
|
2011-03-18 11:28:18 -04:00
|
|
|
this.close();
|
2009-08-03 17:52:45 -04:00
|
|
|
return true;
|
|
|
|
}
|
2009-12-17 17:12:46 -05:00
|
|
|
if (symbol == Clutter.slash) {
|
|
|
|
// Need preload data before get completion. GFilenameCompleter load content of parent directory.
|
|
|
|
// Parent directory for /usr/include/ is /usr/. So need to add fake name('a').
|
|
|
|
let text = o.get_text().concat('/a');
|
|
|
|
let prefix;
|
|
|
|
if (text.lastIndexOf(' ') == -1)
|
|
|
|
prefix = text;
|
|
|
|
else
|
|
|
|
prefix = text.substr(text.lastIndexOf(' ') + 1);
|
|
|
|
this._getCompletion(prefix);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
if (postfix[postfix.length - 1] == '/')
|
|
|
|
this._getCompletion(text + postfix + 'a');
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2009-08-03 17:52:45 -04:00
|
|
|
return false;
|
|
|
|
}));
|
2008-12-01 14:51:43 -05:00
|
|
|
},
|
|
|
|
|
2009-12-17 17:12:46 -05:00
|
|
|
_getCompletion : function(text) {
|
|
|
|
if (text.indexOf('/') != -1) {
|
|
|
|
return this._pathCompleter.get_completion_suffix(text);
|
|
|
|
} else {
|
|
|
|
return this._commandCompleter.getCompletion(text);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-02-26 14:07:44 -05:00
|
|
|
_run : function(input, inTerminal) {
|
|
|
|
let command = input;
|
2010-03-17 11:22:27 -04:00
|
|
|
|
2011-02-13 11:42:04 -05:00
|
|
|
this._history.addItem(input);
|
2009-10-01 17:44:16 -04:00
|
|
|
this._commandError = false;
|
2009-09-14 13:45:49 -04:00
|
|
|
let f;
|
|
|
|
if (this._enableInternalCommands)
|
2010-02-26 14:07:44 -05: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 14:07:44 -05:00
|
|
|
} else if (input) {
|
2008-12-01 14:51:43 -05:00
|
|
|
try {
|
2011-06-01 07:39:11 -04: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 11:43:08 -05:00
|
|
|
Util.trySpawnCommandLine(command);
|
2008-12-01 14:51:43 -05:00
|
|
|
} catch (e) {
|
2010-02-26 14:07:44 -05: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 15:43:49 -04:00
|
|
|
try {
|
|
|
|
Gio.app_info_launch_default_for_uri(file.get_uri(),
|
|
|
|
global.create_app_launch_context());
|
|
|
|
} 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 14:41:45 -05:00
|
|
|
}
|
2011-07-06 15:43:49 -04:00
|
|
|
} else {
|
|
|
|
this._showError(e.message);
|
2010-02-26 14:07:44 -05:00
|
|
|
}
|
2008-12-01 14:51:43 -05:00
|
|
|
}
|
2008-11-19 18:21:42 -05:00
|
|
|
}
|
2008-12-01 14:51:43 -05:00
|
|
|
},
|
2008-11-19 18:21:42 -05:00
|
|
|
|
2011-07-06 15:43:49 -04:00
|
|
|
_showError : function(message) {
|
|
|
|
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',
|
|
|
|
onComplete: Lang.bind(this,
|
|
|
|
function() {
|
|
|
|
parentActor.set_height(-1);
|
|
|
|
this._errorBox.show();
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-12-06 14:41:06 -05:00
|
|
|
open: function() {
|
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 11:22:27 -04:00
|
|
|
|
2011-03-21 09:06:35 -04:00
|
|
|
if (this._lockdownSettings.get_boolean(DISABLE_COMMAND_LINE_KEY))
|
|
|
|
return;
|
|
|
|
|
2010-12-06 14:41:06 -05:00
|
|
|
ModalDialog.ModalDialog.prototype.open.call(this);
|
2008-12-01 14:51:43 -05:00
|
|
|
},
|
2008-11-24 14:07:18 -05:00
|
|
|
|
2008-11-19 18:21:42 -05:00
|
|
|
};
|
2008-11-20 10:40:44 -05:00
|
|
|
Signals.addSignalMethods(RunDialog.prototype);
|