2011-09-28 13:16:26 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2019-01-31 14:07:06 +00:00
|
|
|
/* exported Component */
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2019-02-09 03:21:36 +00:00
|
|
|
const { Gio, St } = imports.gi;
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2013-02-08 16:25:08 +00:00
|
|
|
const GnomeSession = imports.misc.gnomeSession;
|
2011-07-12 13:47:43 +00:00
|
|
|
const Main = imports.ui.main;
|
|
|
|
const MessageTray = imports.ui.messageTray;
|
|
|
|
|
2018-09-06 00:55:20 +00:00
|
|
|
const { loadInterfaceXML } = imports.misc.fileUtils;
|
|
|
|
|
2011-07-12 13:47:43 +00:00
|
|
|
// GSettings keys
|
|
|
|
const SETTINGS_SCHEMA = 'org.gnome.desktop.media-handling';
|
|
|
|
const SETTING_DISABLE_AUTORUN = 'autorun-never';
|
2011-07-11 13:31:56 +00:00
|
|
|
const SETTING_START_APP = 'autorun-x-content-start-app';
|
|
|
|
const SETTING_IGNORE = 'autorun-x-content-ignore';
|
|
|
|
const SETTING_OPEN_FOLDER = 'autorun-x-content-open-folder';
|
|
|
|
|
2017-07-18 17:47:27 +00:00
|
|
|
var AutorunSetting = {
|
2011-07-11 13:31:56 +00:00
|
|
|
RUN: 0,
|
|
|
|
IGNORE: 1,
|
|
|
|
FILES: 2,
|
|
|
|
ASK: 3
|
|
|
|
};
|
2011-07-12 13:47:43 +00:00
|
|
|
|
|
|
|
// misc utils
|
2015-02-12 14:15:51 +00:00
|
|
|
function shouldAutorunMount(mount) {
|
2011-07-12 13:47:43 +00:00
|
|
|
let root = mount.get_root();
|
|
|
|
let volume = mount.get_volume();
|
|
|
|
|
2015-02-12 14:15:51 +00:00
|
|
|
if (!volume || !volume.allowAutorun)
|
2012-06-19 19:17:07 +00:00
|
|
|
return false;
|
|
|
|
|
2013-07-01 20:52:26 +00:00
|
|
|
if (root.is_native() && isMountRootHidden(root))
|
2011-07-12 13:47:43 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isMountRootHidden(root) {
|
|
|
|
let path = root.get_path();
|
|
|
|
|
|
|
|
// skip any mounts in hidden directory hierarchies
|
2018-07-14 20:56:22 +00:00
|
|
|
return (path.includes('/.'));
|
2011-07-12 13:47:43 +00:00
|
|
|
}
|
|
|
|
|
2012-09-16 17:54:25 +00:00
|
|
|
function isMountNonLocal(mount) {
|
2012-10-16 17:50:13 +00:00
|
|
|
// If the mount doesn't have an associated volume, that means it's
|
|
|
|
// an uninteresting filesystem. Most devices that we care about will
|
|
|
|
// have a mount, like media players and USB sticks.
|
2012-09-16 17:54:25 +00:00
|
|
|
let volume = mount.get_volume();
|
|
|
|
if (volume == null)
|
2012-10-16 17:50:13 +00:00
|
|
|
return true;
|
2012-09-16 17:54:25 +00:00
|
|
|
|
|
|
|
return (volume.get_identifier("class") == "network");
|
|
|
|
}
|
|
|
|
|
2011-07-12 13:47:43 +00:00
|
|
|
function startAppForMount(app, mount) {
|
|
|
|
let files = [];
|
|
|
|
let root = mount.get_root();
|
2011-07-11 13:31:56 +00:00
|
|
|
let retval = false;
|
|
|
|
|
2011-07-12 13:47:43 +00:00
|
|
|
files.push(root);
|
|
|
|
|
|
|
|
try {
|
2011-07-11 13:31:56 +00:00
|
|
|
retval = app.launch(files,
|
2017-07-15 01:42:06 +00:00
|
|
|
global.create_app_launch_context(0, -1));
|
2011-07-12 13:47:43 +00:00
|
|
|
} catch (e) {
|
2019-01-30 00:18:24 +00:00
|
|
|
log(`Unable to launch the application ${app.get_name()}: ${e}`);
|
2011-07-12 13:47:43 +00:00
|
|
|
}
|
2011-07-11 13:31:56 +00:00
|
|
|
|
|
|
|
return retval;
|
2011-07-12 13:47:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************/
|
|
|
|
|
2018-09-06 00:55:20 +00:00
|
|
|
const HotplugSnifferIface = loadInterfaceXML('org.gnome.Shell.HotplugSniffer');
|
2011-08-16 12:28:53 +00:00
|
|
|
const HotplugSnifferProxy = Gio.DBusProxy.makeProxyWrapper(HotplugSnifferIface);
|
|
|
|
function HotplugSniffer() {
|
|
|
|
return new HotplugSnifferProxy(Gio.DBus.session,
|
2011-07-12 14:37:14 +00:00
|
|
|
'org.gnome.Shell.HotplugSniffer',
|
|
|
|
'/org/gnome/Shell/HotplugSniffer');
|
2011-08-16 12:28:53 +00:00
|
|
|
}
|
2011-07-12 14:37:14 +00:00
|
|
|
|
2017-10-31 01:19:44 +00:00
|
|
|
var ContentTypeDiscoverer = class {
|
|
|
|
constructor(callback) {
|
2011-07-12 13:47:43 +00:00
|
|
|
this._callback = callback;
|
2014-06-24 19:17:09 +00:00
|
|
|
this._settings = new Gio.Settings({ schema_id: SETTINGS_SCHEMA });
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
guessContentTypes(mount) {
|
2012-09-16 17:24:25 +00:00
|
|
|
let autorunEnabled = !this._settings.get_boolean(SETTING_DISABLE_AUTORUN);
|
2012-09-16 17:54:25 +00:00
|
|
|
let shouldScan = autorunEnabled && !isMountNonLocal(mount);
|
2012-09-16 17:24:25 +00:00
|
|
|
|
2012-09-16 17:54:25 +00:00
|
|
|
if (shouldScan) {
|
2012-09-16 17:24:25 +00:00
|
|
|
// guess mount's content types using GIO
|
|
|
|
mount.guess_content_type(false, null,
|
2017-12-02 00:27:35 +00:00
|
|
|
this._onContentTypeGuessed.bind(this));
|
2012-09-16 17:24:25 +00:00
|
|
|
} else {
|
|
|
|
this._emitCallback(mount, []);
|
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onContentTypeGuessed(mount, res) {
|
2011-07-12 13:47:43 +00:00
|
|
|
let contentTypes = [];
|
|
|
|
|
|
|
|
try {
|
|
|
|
contentTypes = mount.guess_content_type_finish(res);
|
|
|
|
} catch (e) {
|
2019-01-30 00:18:24 +00:00
|
|
|
log(`Unable to guess content types on added mount ${mount.get_name()}: ${e}`);
|
2011-07-12 13:47:43 +00:00
|
|
|
}
|
|
|
|
|
2011-07-12 14:37:14 +00:00
|
|
|
if (contentTypes.length) {
|
|
|
|
this._emitCallback(mount, contentTypes);
|
|
|
|
} else {
|
|
|
|
let root = mount.get_root();
|
|
|
|
|
|
|
|
let hotplugSniffer = new HotplugSniffer();
|
2011-08-16 12:28:53 +00:00
|
|
|
hotplugSniffer.SniffURIRemote(root.get_uri(),
|
2019-09-12 22:27:56 +00:00
|
|
|
([contentTypes]) => {
|
|
|
|
this._emitCallback(mount, contentTypes);
|
|
|
|
});
|
2011-07-12 14:37:14 +00:00
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-07-12 14:37:14 +00:00
|
|
|
|
2019-07-11 23:31:38 +00:00
|
|
|
_emitCallback(mount, contentTypes = []) {
|
2011-07-12 13:47:43 +00:00
|
|
|
// we're not interested in win32 software content types here
|
2017-10-31 00:38:18 +00:00
|
|
|
contentTypes = contentTypes.filter(
|
|
|
|
type => (type != 'x-content/win32-software')
|
|
|
|
);
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2011-07-12 14:37:14 +00:00
|
|
|
let apps = [];
|
2017-10-31 00:38:18 +00:00
|
|
|
contentTypes.forEach(type => {
|
2011-07-12 14:37:14 +00:00
|
|
|
let app = Gio.app_info_get_default_for_type(type, false);
|
|
|
|
|
|
|
|
if (app)
|
|
|
|
apps.push(app);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (apps.length == 0)
|
|
|
|
apps.push(Gio.app_info_get_default_for_type('inode/directory', false));
|
|
|
|
|
|
|
|
this._callback(mount, apps, contentTypes);
|
2011-07-12 13:47:43 +00:00
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
};
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2017-10-31 01:19:44 +00:00
|
|
|
var AutorunManager = class {
|
|
|
|
constructor() {
|
2013-02-08 16:25:08 +00:00
|
|
|
this._session = new GnomeSession.SessionManager();
|
2011-07-12 13:47:43 +00:00
|
|
|
this._volumeMonitor = Gio.VolumeMonitor.get();
|
|
|
|
|
2015-02-12 14:38:17 +00:00
|
|
|
this._dispatcher = new AutorunDispatcher(this);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
enable() {
|
2017-12-02 00:27:35 +00:00
|
|
|
this._mountAddedId = this._volumeMonitor.connect('mount-added', this._onMountAdded.bind(this));
|
|
|
|
this._mountRemovedId = this._volumeMonitor.connect('mount-removed', this._onMountRemoved.bind(this));
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-09-03 01:23:50 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
disable() {
|
2012-09-03 01:23:50 +00:00
|
|
|
this._volumeMonitor.disconnect(this._mountAddedId);
|
|
|
|
this._volumeMonitor.disconnect(this._mountRemovedId);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onMountAdded(monitor, mount) {
|
2011-06-20 19:16:40 +00:00
|
|
|
// don't do anything if our session is not the currently
|
|
|
|
// active one
|
2013-02-03 21:24:33 +00:00
|
|
|
if (!this._session.SessionIsActive)
|
2011-06-20 19:16:40 +00:00
|
|
|
return;
|
|
|
|
|
2017-10-31 00:38:18 +00:00
|
|
|
let discoverer = new ContentTypeDiscoverer((mount, apps, contentTypes) => {
|
2015-02-12 14:38:17 +00:00
|
|
|
this._dispatcher.addMount(mount, apps, contentTypes);
|
2017-10-31 00:38:18 +00:00
|
|
|
});
|
2015-02-12 14:15:51 +00:00
|
|
|
discoverer.guessContentTypes(mount);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onMountRemoved(monitor, mount) {
|
2015-02-12 14:38:17 +00:00
|
|
|
this._dispatcher.removeMount(mount);
|
2015-02-19 11:58:26 +00:00
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
};
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2017-10-31 01:19:44 +00:00
|
|
|
var AutorunDispatcher = class {
|
|
|
|
constructor(manager) {
|
2012-09-03 01:23:50 +00:00
|
|
|
this._manager = manager;
|
2011-07-12 13:47:43 +00:00
|
|
|
this._sources = [];
|
2014-06-24 19:17:09 +00:00
|
|
|
this._settings = new Gio.Settings({ schema_id: SETTINGS_SCHEMA });
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_getAutorunSettingForType(contentType) {
|
2011-07-11 13:31:56 +00:00
|
|
|
let runApp = this._settings.get_strv(SETTING_START_APP);
|
2018-07-14 20:56:22 +00:00
|
|
|
if (runApp.includes(contentType))
|
2011-07-11 13:31:56 +00:00
|
|
|
return AutorunSetting.RUN;
|
|
|
|
|
|
|
|
let ignore = this._settings.get_strv(SETTING_IGNORE);
|
2018-07-14 20:56:22 +00:00
|
|
|
if (ignore.includes(contentType))
|
2011-07-11 13:31:56 +00:00
|
|
|
return AutorunSetting.IGNORE;
|
|
|
|
|
|
|
|
let openFiles = this._settings.get_strv(SETTING_OPEN_FOLDER);
|
2018-07-14 20:56:22 +00:00
|
|
|
if (openFiles.includes(contentType))
|
2011-07-11 13:31:56 +00:00
|
|
|
return AutorunSetting.FILES;
|
|
|
|
|
|
|
|
return AutorunSetting.ASK;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-07-11 13:31:56 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_getSourceForMount(mount) {
|
2017-10-31 00:38:18 +00:00
|
|
|
let filtered = this._sources.filter(source => (source.mount == mount));
|
2011-07-12 13:47:43 +00:00
|
|
|
|
|
|
|
// we always make sure not to add two sources for the same
|
|
|
|
// mount in addMount(), so it's safe to assume filtered.length
|
|
|
|
// is always either 1 or 0.
|
|
|
|
if (filtered.length == 1)
|
|
|
|
return filtered[0];
|
|
|
|
|
|
|
|
return null;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_addSource(mount, apps) {
|
2011-07-11 13:31:56 +00:00
|
|
|
// if we already have a source showing for this
|
|
|
|
// mount, return
|
|
|
|
if (this._getSourceForMount(mount))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// add a new source
|
2015-02-12 14:38:17 +00:00
|
|
|
this._sources.push(new AutorunSource(this._manager, mount, apps));
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-07-11 13:31:56 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
addMount(mount, apps, contentTypes) {
|
2011-07-12 13:47:43 +00:00
|
|
|
// if autorun is disabled globally, return
|
|
|
|
if (this._settings.get_boolean(SETTING_DISABLE_AUTORUN))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// if the mount doesn't want to be autorun, return
|
2015-02-12 14:15:51 +00:00
|
|
|
if (!shouldAutorunMount(mount))
|
2011-07-12 13:47:43 +00:00
|
|
|
return;
|
|
|
|
|
2017-09-17 09:37:52 +00:00
|
|
|
let setting;
|
|
|
|
if (contentTypes.length > 0)
|
|
|
|
setting = this._getAutorunSettingForType(contentTypes[0]);
|
|
|
|
else
|
|
|
|
setting = AutorunSetting.ASK;
|
2011-07-11 13:31:56 +00:00
|
|
|
|
|
|
|
// check at the settings for the first content type
|
|
|
|
// to see whether we should ask
|
|
|
|
if (setting == AutorunSetting.IGNORE)
|
|
|
|
return; // return right away
|
|
|
|
|
|
|
|
let success = false;
|
|
|
|
let app = null;
|
|
|
|
|
|
|
|
if (setting == AutorunSetting.RUN) {
|
2011-10-03 20:17:32 +00:00
|
|
|
app = Gio.app_info_get_default_for_type(contentTypes[0], false);
|
2011-07-11 13:31:56 +00:00
|
|
|
} else if (setting == AutorunSetting.FILES) {
|
|
|
|
app = Gio.app_info_get_default_for_type('inode/directory', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (app)
|
|
|
|
success = startAppForMount(app, mount);
|
|
|
|
|
|
|
|
// we fallback here also in case the settings did not specify 'ask',
|
|
|
|
// but we failed launching the default app or the default file manager
|
|
|
|
if (!success)
|
2011-07-12 14:37:14 +00:00
|
|
|
this._addSource(mount, apps);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
removeMount(mount) {
|
2011-07-12 13:47:43 +00:00
|
|
|
let source = this._getSourceForMount(mount);
|
|
|
|
|
|
|
|
// if we aren't tracking this mount, don't do anything
|
|
|
|
if (!source)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// destroy the notification source
|
|
|
|
source.destroy();
|
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
};
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2017-10-31 01:19:44 +00:00
|
|
|
var AutorunSource = class extends MessageTray.Source {
|
|
|
|
constructor(manager, mount, apps) {
|
|
|
|
super(mount.get_name());
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2012-09-03 01:23:50 +00:00
|
|
|
this._manager = manager;
|
2011-07-12 13:47:43 +00:00
|
|
|
this.mount = mount;
|
2011-07-12 14:37:14 +00:00
|
|
|
this.apps = apps;
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2015-02-12 14:38:17 +00:00
|
|
|
this._notification = new AutorunNotification(this._manager, this);
|
2011-07-12 13:47:43 +00:00
|
|
|
|
|
|
|
// add ourselves as a source, and popup the notification
|
|
|
|
Main.messageTray.add(this);
|
|
|
|
this.notify(this._notification);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
getIcon() {
|
2012-09-15 06:10:15 +00:00
|
|
|
return this.mount.get_icon();
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2015-11-16 19:27:08 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_createPolicy() {
|
2015-11-16 19:27:08 +00:00
|
|
|
return new MessageTray.NotificationApplicationPolicy('org.gnome.Nautilus');
|
2011-07-12 13:47:43 +00:00
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
};
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2017-10-31 01:19:44 +00:00
|
|
|
var AutorunNotification = class extends MessageTray.Notification {
|
|
|
|
constructor(manager, source) {
|
|
|
|
super(source, source.title);
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2012-09-03 01:23:50 +00:00
|
|
|
this._manager = manager;
|
2011-07-12 13:47:43 +00:00
|
|
|
this._mount = source.mount;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2015-02-17 02:40:25 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
createBanner() {
|
2015-02-17 02:40:25 +00:00
|
|
|
let banner = new MessageTray.NotificationBanner(this);
|
|
|
|
|
2017-10-31 00:38:18 +00:00
|
|
|
this.source.apps.forEach(app => {
|
2011-07-12 14:37:14 +00:00
|
|
|
let actor = this._buttonForApp(app);
|
2011-07-12 13:47:43 +00:00
|
|
|
|
|
|
|
if (actor)
|
2015-02-17 02:40:25 +00:00
|
|
|
banner.addButton(actor);
|
2017-10-31 00:38:18 +00:00
|
|
|
});
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2015-02-17 02:40:25 +00:00
|
|
|
return banner;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_buttonForApp(app) {
|
2011-07-12 13:47:43 +00:00
|
|
|
let box = new St.BoxLayout();
|
|
|
|
let icon = new St.Icon({ gicon: app.get_icon(),
|
|
|
|
style_class: 'hotplug-notification-item-icon' });
|
|
|
|
box.add(icon);
|
|
|
|
|
2019-02-12 14:02:09 +00:00
|
|
|
let label = new St.Bin({
|
|
|
|
y_align: St.Align.MIDDLE,
|
|
|
|
child: new St.Label({ text: _("Open with %s").format(app.get_name()) }),
|
|
|
|
});
|
2011-07-12 13:47:43 +00:00
|
|
|
box.add(label);
|
|
|
|
|
|
|
|
let button = new St.Button({ child: box,
|
2011-07-12 14:37:14 +00:00
|
|
|
x_fill: true,
|
|
|
|
x_align: St.Align.START,
|
2015-02-17 02:40:25 +00:00
|
|
|
x_expand: true,
|
2011-07-12 13:47:43 +00:00
|
|
|
button_mask: St.ButtonMask.ONE,
|
2015-01-16 14:33:56 +00:00
|
|
|
style_class: 'hotplug-notification-item button' });
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2017-10-31 00:38:18 +00:00
|
|
|
button.connect('clicked', () => {
|
2011-07-12 13:47:43 +00:00
|
|
|
startAppForMount(app, this._mount);
|
|
|
|
this.destroy();
|
2017-10-31 00:38:18 +00:00
|
|
|
});
|
2011-07-12 13:47:43 +00:00
|
|
|
|
|
|
|
return button;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
activate() {
|
2017-10-31 01:19:44 +00:00
|
|
|
super.activate();
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2015-02-12 14:38:17 +00:00
|
|
|
let app = Gio.app_info_get_default_for_type('inode/directory', false);
|
|
|
|
startAppForMount(app, this._mount);
|
2011-07-12 13:47:43 +00:00
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
};
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2017-07-18 17:47:27 +00:00
|
|
|
var Component = AutorunManager;
|