2011-09-28 09:16:26 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2010-10-03 15:48:56 -04:00
|
|
|
|
2010-11-17 11:43:08 -05:00
|
|
|
const GLib = imports.gi.GLib;
|
|
|
|
|
|
|
|
const Main = imports.ui.main;
|
|
|
|
|
2011-04-13 09:18:00 -04:00
|
|
|
// http://daringfireball.net/2010/07/improved_regex_for_matching_urls
|
|
|
|
const _balancedParens = '\\((?:[^\\s()<>]+|(?:\\(?:[^\\s()<>]+\\)))*\\)';
|
2011-04-13 09:40:28 -04:00
|
|
|
const _leadingJunk = '[\\s`(\\[{\'\\"<\u00AB\u201C\u2018]';
|
2011-04-13 09:18:00 -04:00
|
|
|
const _notTrailingJunk = '[^\\s`!()\\[\\]{};:\'\\".,<>?\u00AB\u00BB\u201C\u201D\u2018\u2019]';
|
|
|
|
|
|
|
|
const _urlRegexp = new RegExp(
|
2011-04-13 09:40:28 -04:00
|
|
|
'(^|' + _leadingJunk + ')' +
|
|
|
|
'(' +
|
2011-04-13 09:18:00 -04:00
|
|
|
'(?:' +
|
2011-04-13 09:18:37 -04:00
|
|
|
'[a-z][\\w-]+://' + // scheme://
|
2011-04-13 09:18:00 -04:00
|
|
|
'|' +
|
|
|
|
'www\\d{0,3}[.]' + // www.
|
|
|
|
'|' +
|
|
|
|
'[a-z0-9.\\-]+[.][a-z]{2,4}/' + // foo.xx/
|
|
|
|
')' +
|
|
|
|
'(?:' + // one or more:
|
|
|
|
'[^\\s()<>]+' + // run of non-space non-()
|
|
|
|
'|' + // or
|
|
|
|
_balancedParens + // balanced parens
|
|
|
|
')+' +
|
|
|
|
'(?:' + // end with:
|
|
|
|
_balancedParens + // balanced parens
|
|
|
|
'|' + // or
|
|
|
|
_notTrailingJunk + // last non-junk char
|
|
|
|
')' +
|
|
|
|
')', 'gi');
|
2010-10-03 15:48:56 -04:00
|
|
|
|
|
|
|
// findUrls:
|
|
|
|
// @str: string to find URLs in
|
|
|
|
//
|
|
|
|
// Searches @str for URLs and returns an array of objects with %url
|
|
|
|
// properties showing the matched URL string, and %pos properties indicating
|
|
|
|
// the position within @str where the URL was found.
|
|
|
|
//
|
|
|
|
// Return value: the list of match objects, as described above
|
|
|
|
function findUrls(str) {
|
|
|
|
let res = [], match;
|
|
|
|
while ((match = _urlRegexp.exec(str)))
|
2011-04-13 09:40:28 -04:00
|
|
|
res.push({ url: match[2], pos: match.index + match[1].length });
|
2010-10-03 15:48:56 -04:00
|
|
|
return res;
|
|
|
|
}
|
2010-11-17 11:43:08 -05:00
|
|
|
|
|
|
|
// spawn:
|
|
|
|
// @argv: an argv array
|
|
|
|
//
|
|
|
|
// Runs @argv in the background, handling any errors that occur
|
|
|
|
// when trying to start the program.
|
|
|
|
function spawn(argv) {
|
|
|
|
try {
|
|
|
|
trySpawn(argv);
|
|
|
|
} catch (err) {
|
|
|
|
_handleSpawnError(argv[0], err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// spawnCommandLine:
|
|
|
|
// @command_line: a command line
|
|
|
|
//
|
|
|
|
// Runs @command_line in the background, handling any errors that
|
|
|
|
// occur when trying to parse or start the program.
|
|
|
|
function spawnCommandLine(command_line) {
|
|
|
|
try {
|
2011-06-23 18:04:12 -04:00
|
|
|
let [success, argv] = GLib.shell_parse_argv(command_line);
|
2010-11-17 11:43:08 -05:00
|
|
|
trySpawn(argv);
|
|
|
|
} catch (err) {
|
|
|
|
_handleSpawnError(command_line, err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// trySpawn:
|
|
|
|
// @argv: an argv array
|
|
|
|
//
|
|
|
|
// Runs @argv in the background. If launching @argv fails,
|
|
|
|
// this will throw an error.
|
|
|
|
function trySpawn(argv)
|
|
|
|
{
|
2012-05-09 21:23:19 -04:00
|
|
|
var success, pid;
|
2010-11-17 11:43:08 -05:00
|
|
|
try {
|
2012-05-09 21:23:19 -04:00
|
|
|
[success, pid] = GLib.spawn_async(null, argv, null,
|
|
|
|
GLib.SpawnFlags.SEARCH_PATH | GLib.SpawnFlags.DO_NOT_REAP_CHILD,
|
|
|
|
null, null);
|
2010-11-17 11:43:08 -05:00
|
|
|
} catch (err) {
|
2012-06-19 17:32:45 -04:00
|
|
|
/* Rewrite the error in case of ENOENT */
|
|
|
|
if (err.matches(GLib.SpawnError, GLib.SpawnError.NOENT)) {
|
|
|
|
throw new GLib.SpawnError({ code: GLib.SpawnError.NOENT,
|
|
|
|
message: _("Command not found") });
|
|
|
|
} else if (err instanceof GLib.Error) {
|
2011-01-27 15:26:58 -05:00
|
|
|
// The exception from gjs contains an error string like:
|
|
|
|
// Error invoking GLib.spawn_command_line_async: Failed to
|
|
|
|
// execute child process "foo" (No such file or directory)
|
|
|
|
// We are only interested in the part in the parentheses. (And
|
|
|
|
// we can't pattern match the text, since it gets localized.)
|
2012-06-19 17:32:45 -04:00
|
|
|
let message = err.message.replace(/.*\((.+)\)/, '$1');
|
|
|
|
throw new (err.constructor)({ code: err.code,
|
|
|
|
message: message });
|
|
|
|
} else {
|
|
|
|
throw err;
|
2011-01-27 15:26:58 -05:00
|
|
|
}
|
2010-11-17 11:43:08 -05:00
|
|
|
}
|
2012-05-09 21:23:19 -04:00
|
|
|
// Dummy child watch; we don't want to double-fork internally
|
|
|
|
// because then we lose the parent-child relationship, which
|
|
|
|
// can break polkit. See https://bugzilla.redhat.com//show_bug.cgi?id=819275
|
|
|
|
GLib.child_watch_add(GLib.PRIORITY_DEFAULT, pid, function () {}, null);
|
2010-11-17 11:43:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// trySpawnCommandLine:
|
|
|
|
// @command_line: a command line
|
|
|
|
//
|
|
|
|
// Runs @command_line in the background. If launching @command_line
|
|
|
|
// fails, this will throw an error.
|
|
|
|
function trySpawnCommandLine(command_line) {
|
2011-06-23 17:55:49 -04:00
|
|
|
let success, argv;
|
2010-11-17 11:43:08 -05:00
|
|
|
|
|
|
|
try {
|
2011-06-23 17:55:49 -04:00
|
|
|
[success, argv] = GLib.shell_parse_argv(command_line);
|
2010-11-17 11:43:08 -05:00
|
|
|
} catch (err) {
|
|
|
|
// Replace "Error invoking GLib.shell_parse_argv: " with
|
|
|
|
// something nicer
|
|
|
|
err.message = err.message.replace(/[^:]*: /, _("Could not parse command:") + "\n");
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
trySpawn(argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
function _handleSpawnError(command, err) {
|
|
|
|
let title = _("Execution of '%s' failed:").format(command);
|
2011-03-19 13:59:22 -04:00
|
|
|
Main.notifyError(title, err.message);
|
2010-11-17 11:43:08 -05:00
|
|
|
}
|
2010-12-17 15:30:12 -05:00
|
|
|
|
|
|
|
// killall:
|
|
|
|
// @processName: a process name
|
|
|
|
//
|
|
|
|
// Kills @processName. If no process with the given name is found,
|
|
|
|
// this will fail silently.
|
|
|
|
function killall(processName) {
|
|
|
|
try {
|
|
|
|
// pkill is more portable than killall, but on Linux at least
|
|
|
|
// it won't match if you pass more than 15 characters of the
|
|
|
|
// process name... However, if you use the '-f' flag to match
|
|
|
|
// the entire command line, it will work, but we have to be
|
|
|
|
// careful in that case that we can match
|
|
|
|
// '/usr/bin/processName' but not 'gedit processName.c' or
|
|
|
|
// whatever...
|
|
|
|
|
|
|
|
let argv = ['pkill', '-f', '^([^ ]*/)?' + processName + '($| )'];
|
2012-07-22 21:23:36 -04:00
|
|
|
GLib.spawn_sync(null, argv, null, GLib.SpawnFlags.SEARCH_PATH, null);
|
2010-12-17 15:30:12 -05:00
|
|
|
// It might be useful to return success/failure, but we'd need
|
|
|
|
// a wrapper around WIFEXITED and WEXITSTATUS. Since none of
|
|
|
|
// the current callers care, we don't bother.
|
|
|
|
} catch (e) {
|
|
|
|
logError(e, 'Failed to kill ' + processName);
|
|
|
|
}
|
|
|
|
}
|
2011-01-25 16:08:12 -05:00
|
|
|
|
|
|
|
// This was ported from network-manager-applet
|
|
|
|
// Copyright 2007 - 2011 Red Hat, Inc.
|
|
|
|
// Author: Dan Williams <dcbw@redhat.com>
|
|
|
|
|
|
|
|
const _IGNORED_WORDS = [
|
|
|
|
'Semiconductor',
|
|
|
|
'Components',
|
|
|
|
'Corporation',
|
|
|
|
'Communications',
|
|
|
|
'Company',
|
|
|
|
'Corp.',
|
|
|
|
'Corp',
|
|
|
|
'Co.',
|
|
|
|
'Inc.',
|
|
|
|
'Inc',
|
|
|
|
'Incorporated',
|
|
|
|
'Ltd.',
|
|
|
|
'Limited.',
|
2011-03-29 08:29:26 -04:00
|
|
|
'Intel',
|
2011-01-25 16:08:12 -05:00
|
|
|
'chipset',
|
|
|
|
'adapter',
|
|
|
|
'[hex]',
|
|
|
|
'NDIS',
|
|
|
|
'Module'
|
|
|
|
];
|
|
|
|
|
|
|
|
const _IGNORED_PHRASES = [
|
|
|
|
'Multiprotocol MAC/baseband processor',
|
|
|
|
'Wireless LAN Controller',
|
|
|
|
'Wireless LAN Adapter',
|
|
|
|
'Wireless Adapter',
|
|
|
|
'Network Connection',
|
|
|
|
'Wireless Cardbus Adapter',
|
|
|
|
'Wireless CardBus Adapter',
|
|
|
|
'54 Mbps Wireless PC Card',
|
|
|
|
'Wireless PC Card',
|
|
|
|
'Wireless PC',
|
|
|
|
'PC Card with XJACK(r) Antenna',
|
|
|
|
'Wireless cardbus',
|
|
|
|
'Wireless LAN PC Card',
|
|
|
|
'Technology Group Ltd.',
|
|
|
|
'Communication S.p.A.',
|
|
|
|
'Business Mobile Networks BV',
|
|
|
|
'Mobile Broadband Minicard Composite Device',
|
|
|
|
'Mobile Communications AB',
|
|
|
|
'(PC-Suite Mode)'
|
|
|
|
];
|
|
|
|
|
|
|
|
function fixupPCIDescription(desc) {
|
2011-03-29 08:29:26 -04:00
|
|
|
desc = desc.replace(/[_,]/, ' ');
|
2011-01-25 16:08:12 -05:00
|
|
|
|
|
|
|
/* Attempt to shorten ID by ignoring certain phrases */
|
|
|
|
for (let i = 0; i < _IGNORED_PHRASES.length; i++) {
|
|
|
|
let item = _IGNORED_PHRASES[i];
|
|
|
|
let pos = desc.indexOf(item);
|
|
|
|
if (pos != -1) {
|
|
|
|
let before = desc.substring(0, pos);
|
|
|
|
let after = desc.substring(pos + item.length, desc.length);
|
|
|
|
desc = before + after;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Attmept to shorten ID by ignoring certain individual words */
|
|
|
|
let words = desc.split(' ');
|
|
|
|
let out = [ ];
|
2011-03-29 08:29:26 -04:00
|
|
|
for (let i = 0; i < words.length; i++) {
|
2011-01-25 16:08:12 -05:00
|
|
|
let item = words[i];
|
|
|
|
|
|
|
|
// skip empty items (that come out from consecutive spaces)
|
|
|
|
if (item.length == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (_IGNORED_WORDS.indexOf(item) == -1) {
|
|
|
|
out.push(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out.join(' ');
|
|
|
|
}
|
2011-12-17 17:52:11 -05:00
|
|
|
|
|
|
|
// lowerBound:
|
|
|
|
// @array: an array or array-like object, already sorted
|
|
|
|
// according to @cmp
|
|
|
|
// @val: the value to add
|
|
|
|
// @cmp: a comparator (or undefined to compare as numbers)
|
|
|
|
//
|
|
|
|
// Returns the position of the first element that is not
|
|
|
|
// lower than @val, according to @cmp.
|
|
|
|
// That is, returns the first position at which it
|
|
|
|
// is possible to insert @val without violating the
|
|
|
|
// order.
|
|
|
|
// This is quite like an ordinary binary search, except
|
|
|
|
// that it doesn't stop at first element comparing equal.
|
|
|
|
|
|
|
|
function lowerBound(array, val, cmp) {
|
|
|
|
let min, max, mid, v;
|
|
|
|
cmp = cmp || function(a, b) { return a - b; };
|
|
|
|
|
|
|
|
if (array.length == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
min = 0; max = array.length;
|
|
|
|
while (min < (max - 1)) {
|
|
|
|
mid = Math.floor((min + max) / 2);
|
|
|
|
v = cmp(array[mid], val);
|
|
|
|
|
|
|
|
if (v < 0)
|
|
|
|
min = mid + 1;
|
|
|
|
else
|
|
|
|
max = mid;
|
|
|
|
}
|
|
|
|
|
2011-12-20 15:30:30 -05:00
|
|
|
return (min == max || cmp(array[min], val) < 0) ? max : min;
|
2011-12-17 17:52:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// insertSorted:
|
|
|
|
// @array: an array sorted according to @cmp
|
|
|
|
// @val: a value to insert
|
|
|
|
// @cmp: the sorting function
|
|
|
|
//
|
|
|
|
// Inserts @val into @array, preserving the
|
|
|
|
// sorting invariants.
|
|
|
|
// Returns the position at which it was inserted
|
|
|
|
function insertSorted(array, val, cmp) {
|
|
|
|
let pos = lowerBound(array, val, cmp);
|
|
|
|
array.splice(pos, 0, val);
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|