2011-09-28 09:16:26 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2019-01-31 09:07:06 -05:00
|
|
|
/* exported Component */
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2019-10-17 17:40:24 -04:00
|
|
|
const { Clutter, Gio, GObject, St } = imports.gi;
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2013-02-08 11:25:08 -05:00
|
|
|
const GnomeSession = imports.misc.gnomeSession;
|
2011-07-12 09:47:43 -04:00
|
|
|
const Main = imports.ui.main;
|
|
|
|
const MessageTray = imports.ui.messageTray;
|
|
|
|
|
2022-06-23 11:02:55 -04:00
|
|
|
Gio._promisify(Gio.Mount.prototype, 'guess_content_type');
|
|
|
|
|
2018-09-05 20:55:20 -04:00
|
|
|
const { loadInterfaceXML } = imports.misc.fileUtils;
|
|
|
|
|
2011-07-12 09:47:43 -04:00
|
|
|
// GSettings keys
|
|
|
|
const SETTINGS_SCHEMA = 'org.gnome.desktop.media-handling';
|
|
|
|
const SETTING_DISABLE_AUTORUN = 'autorun-never';
|
2011-07-11 09:31:56 -04: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 13:47:27 -04:00
|
|
|
var AutorunSetting = {
|
2011-07-11 09:31:56 -04:00
|
|
|
RUN: 0,
|
|
|
|
IGNORE: 1,
|
|
|
|
FILES: 2,
|
2019-08-20 17:43:54 -04:00
|
|
|
ASK: 3,
|
2011-07-11 09:31:56 -04:00
|
|
|
};
|
2011-07-12 09:47:43 -04:00
|
|
|
|
|
|
|
// misc utils
|
2015-02-12 09:15:51 -05:00
|
|
|
function shouldAutorunMount(mount) {
|
2011-07-12 09:47:43 -04:00
|
|
|
let root = mount.get_root();
|
|
|
|
let volume = mount.get_volume();
|
|
|
|
|
2015-02-12 09:15:51 -05:00
|
|
|
if (!volume || !volume.allowAutorun)
|
2012-06-19 15:17:07 -04:00
|
|
|
return false;
|
|
|
|
|
2013-07-01 16:52:26 -04:00
|
|
|
if (root.is_native() && isMountRootHidden(root))
|
2011-07-12 09:47:43 -04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isMountRootHidden(root) {
|
|
|
|
let path = root.get_path();
|
|
|
|
|
|
|
|
// skip any mounts in hidden directory hierarchies
|
2019-08-19 15:38:51 -04:00
|
|
|
return path.includes('/.');
|
2011-07-12 09:47:43 -04:00
|
|
|
}
|
|
|
|
|
2012-09-16 13:54:25 -04:00
|
|
|
function isMountNonLocal(mount) {
|
2012-10-16 13:50:13 -04: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 13:54:25 -04:00
|
|
|
let volume = mount.get_volume();
|
|
|
|
if (volume == null)
|
2012-10-16 13:50:13 -04:00
|
|
|
return true;
|
2012-09-16 13:54:25 -04:00
|
|
|
|
2019-08-19 15:38:51 -04:00
|
|
|
return volume.get_identifier("class") == "network";
|
2012-09-16 13:54:25 -04:00
|
|
|
}
|
|
|
|
|
2011-07-12 09:47:43 -04:00
|
|
|
function startAppForMount(app, mount) {
|
|
|
|
let files = [];
|
|
|
|
let root = mount.get_root();
|
2011-07-11 09:31:56 -04:00
|
|
|
let retval = false;
|
|
|
|
|
2011-07-12 09:47:43 -04:00
|
|
|
files.push(root);
|
|
|
|
|
|
|
|
try {
|
2019-09-12 11:26:08 -04:00
|
|
|
retval = app.launch(files,
|
2017-07-14 21:42:06 -04:00
|
|
|
global.create_app_launch_context(0, -1));
|
2011-07-12 09:47:43 -04:00
|
|
|
} catch (e) {
|
2022-02-07 09:14:06 -05:00
|
|
|
log(`Unable to launch the application ${app.get_name()}: ${e}`);
|
2011-07-12 09:47:43 -04:00
|
|
|
}
|
2011-07-11 09:31:56 -04:00
|
|
|
|
|
|
|
return retval;
|
2011-07-12 09:47:43 -04:00
|
|
|
}
|
|
|
|
|
2018-09-05 20:55:20 -04:00
|
|
|
const HotplugSnifferIface = loadInterfaceXML('org.gnome.Shell.HotplugSniffer');
|
2011-08-16 08:28:53 -04:00
|
|
|
const HotplugSnifferProxy = Gio.DBusProxy.makeProxyWrapper(HotplugSnifferIface);
|
|
|
|
function HotplugSniffer() {
|
|
|
|
return new HotplugSnifferProxy(Gio.DBus.session,
|
2011-07-12 10:37:14 -04:00
|
|
|
'org.gnome.Shell.HotplugSniffer',
|
|
|
|
'/org/gnome/Shell/HotplugSniffer');
|
2011-08-16 08:28:53 -04:00
|
|
|
}
|
2011-07-12 10:37:14 -04:00
|
|
|
|
2017-10-30 21:19:44 -04:00
|
|
|
var ContentTypeDiscoverer = class {
|
2022-06-23 11:02:55 -04:00
|
|
|
constructor() {
|
2014-06-24 15:17:09 -04:00
|
|
|
this._settings = new Gio.Settings({ schema_id: SETTINGS_SCHEMA });
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2022-06-23 11:02:55 -04:00
|
|
|
async guessContentTypes(mount) {
|
2012-09-16 13:24:25 -04:00
|
|
|
let autorunEnabled = !this._settings.get_boolean(SETTING_DISABLE_AUTORUN);
|
2012-09-16 13:54:25 -04:00
|
|
|
let shouldScan = autorunEnabled && !isMountNonLocal(mount);
|
2012-09-16 13:24:25 -04:00
|
|
|
|
2011-07-12 09:47:43 -04:00
|
|
|
let contentTypes = [];
|
2022-06-23 11:02:55 -04:00
|
|
|
if (shouldScan) {
|
|
|
|
try {
|
|
|
|
contentTypes = await mount.guess_content_type(false, null);
|
|
|
|
} catch (e) {
|
|
|
|
log(`Unable to guess content types on added mount ${mount.get_name()}: ${e}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (contentTypes.length === 0) {
|
|
|
|
const root = mount.get_root();
|
|
|
|
const hotplugSniffer = new HotplugSniffer();
|
|
|
|
[contentTypes] = hotplugSniffer.SniffURIAsync(root.get_uri());
|
|
|
|
}
|
2011-07-12 10:37:14 -04:00
|
|
|
}
|
|
|
|
|
2011-07-12 09:47:43 -04:00
|
|
|
// we're not interested in win32 software content types here
|
2017-10-30 20:38:18 -04:00
|
|
|
contentTypes = contentTypes.filter(
|
2020-04-03 19:52:29 -04:00
|
|
|
type => type !== 'x-content/win32-software');
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2022-06-23 11:02:55 -04:00
|
|
|
const apps = [];
|
2017-10-30 20:38:18 -04:00
|
|
|
contentTypes.forEach(type => {
|
2022-06-23 11:02:55 -04:00
|
|
|
const app = Gio.app_info_get_default_for_type(type, false);
|
2011-07-12 10:37:14 -04:00
|
|
|
|
|
|
|
if (app)
|
|
|
|
apps.push(app);
|
|
|
|
});
|
|
|
|
|
2022-06-23 11:02:55 -04:00
|
|
|
if (apps.length === 0)
|
2011-07-12 10:37:14 -04:00
|
|
|
apps.push(Gio.app_info_get_default_for_type('inode/directory', false));
|
|
|
|
|
2022-06-23 11:02:55 -04:00
|
|
|
return [apps, contentTypes];
|
2011-07-12 09:47:43 -04:00
|
|
|
}
|
2017-10-30 21:19:44 -04:00
|
|
|
};
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2017-10-30 21:19:44 -04:00
|
|
|
var AutorunManager = class {
|
|
|
|
constructor() {
|
2013-02-08 11:25:08 -05:00
|
|
|
this._session = new GnomeSession.SessionManager();
|
2011-07-12 09:47:43 -04:00
|
|
|
this._volumeMonitor = Gio.VolumeMonitor.get();
|
|
|
|
|
2015-02-12 09:38:17 -05:00
|
|
|
this._dispatcher = new AutorunDispatcher(this);
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
enable() {
|
2021-08-15 18:36:59 -04:00
|
|
|
this._volumeMonitor.connectObject(
|
|
|
|
'mount-added', this._onMountAdded.bind(this),
|
|
|
|
'mount-removed', this._onMountRemoved.bind(this), this);
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2012-09-02 21:23:50 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
disable() {
|
2021-08-15 18:36:59 -04:00
|
|
|
this._volumeMonitor.disconnectObject(this);
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2022-06-23 11:02:55 -04:00
|
|
|
async _onMountAdded(monitor, mount) {
|
2011-06-20 15:16:40 -04:00
|
|
|
// don't do anything if our session is not the currently
|
|
|
|
// active one
|
2013-02-03 16:24:33 -05:00
|
|
|
if (!this._session.SessionIsActive)
|
2011-06-20 15:16:40 -04:00
|
|
|
return;
|
|
|
|
|
2022-06-23 11:02:55 -04:00
|
|
|
const discoverer = new ContentTypeDiscoverer();
|
|
|
|
const [apps, contentTypes] = await discoverer.guessContentTypes(mount);
|
|
|
|
this._dispatcher.addMount(mount, apps, contentTypes);
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onMountRemoved(monitor, mount) {
|
2015-02-12 09:38:17 -05:00
|
|
|
this._dispatcher.removeMount(mount);
|
2015-02-19 06:58:26 -05:00
|
|
|
}
|
2017-10-30 21:19:44 -04:00
|
|
|
};
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2017-10-30 21:19:44 -04:00
|
|
|
var AutorunDispatcher = class {
|
|
|
|
constructor(manager) {
|
2012-09-02 21:23:50 -04:00
|
|
|
this._manager = manager;
|
2011-07-12 09:47:43 -04:00
|
|
|
this._sources = [];
|
2014-06-24 15:17:09 -04:00
|
|
|
this._settings = new Gio.Settings({ schema_id: SETTINGS_SCHEMA });
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_getAutorunSettingForType(contentType) {
|
2011-07-11 09:31:56 -04:00
|
|
|
let runApp = this._settings.get_strv(SETTING_START_APP);
|
2018-07-14 16:56:22 -04:00
|
|
|
if (runApp.includes(contentType))
|
2011-07-11 09:31:56 -04:00
|
|
|
return AutorunSetting.RUN;
|
|
|
|
|
|
|
|
let ignore = this._settings.get_strv(SETTING_IGNORE);
|
2018-07-14 16:56:22 -04:00
|
|
|
if (ignore.includes(contentType))
|
2011-07-11 09:31:56 -04:00
|
|
|
return AutorunSetting.IGNORE;
|
|
|
|
|
|
|
|
let openFiles = this._settings.get_strv(SETTING_OPEN_FOLDER);
|
2018-07-14 16:56:22 -04:00
|
|
|
if (openFiles.includes(contentType))
|
2011-07-11 09:31:56 -04:00
|
|
|
return AutorunSetting.FILES;
|
|
|
|
|
|
|
|
return AutorunSetting.ASK;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2011-07-11 09:31:56 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_getSourceForMount(mount) {
|
2019-08-19 15:38:51 -04:00
|
|
|
let filtered = this._sources.filter(source => source.mount == mount);
|
2011-07-12 09:47:43 -04: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-30 21:19:44 -04:00
|
|
|
}
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_addSource(mount, apps) {
|
2019-09-12 11:26:08 -04:00
|
|
|
// if we already have a source showing for this
|
2011-07-11 09:31:56 -04:00
|
|
|
// mount, return
|
|
|
|
if (this._getSourceForMount(mount))
|
|
|
|
return;
|
2019-09-12 11:26:08 -04:00
|
|
|
|
2011-07-11 09:31:56 -04:00
|
|
|
// add a new source
|
2015-02-12 09:38:17 -05:00
|
|
|
this._sources.push(new AutorunSource(this._manager, mount, apps));
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2011-07-11 09:31:56 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
addMount(mount, apps, contentTypes) {
|
2011-07-12 09:47:43 -04: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 09:15:51 -05:00
|
|
|
if (!shouldAutorunMount(mount))
|
2011-07-12 09:47:43 -04:00
|
|
|
return;
|
|
|
|
|
2017-09-17 05:37:52 -04:00
|
|
|
let setting;
|
|
|
|
if (contentTypes.length > 0)
|
|
|
|
setting = this._getAutorunSettingForType(contentTypes[0]);
|
|
|
|
else
|
|
|
|
setting = AutorunSetting.ASK;
|
2011-07-11 09:31:56 -04: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;
|
|
|
|
|
2019-08-19 20:51:42 -04:00
|
|
|
if (setting == AutorunSetting.RUN)
|
2011-10-03 16:17:32 -04:00
|
|
|
app = Gio.app_info_get_default_for_type(contentTypes[0], false);
|
2019-08-19 20:51:42 -04:00
|
|
|
else if (setting == AutorunSetting.FILES)
|
2011-07-11 09:31:56 -04:00
|
|
|
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 10:37:14 -04:00
|
|
|
this._addSource(mount, apps);
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
removeMount(mount) {
|
2011-07-12 09:47:43 -04:00
|
|
|
let source = this._getSourceForMount(mount);
|
2019-09-12 11:26:08 -04:00
|
|
|
|
2011-07-12 09:47:43 -04:00
|
|
|
// if we aren't tracking this mount, don't do anything
|
|
|
|
if (!source)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// destroy the notification source
|
|
|
|
source.destroy();
|
|
|
|
}
|
2017-10-30 21:19:44 -04:00
|
|
|
};
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2019-05-13 17:32:31 -04:00
|
|
|
var AutorunSource = GObject.registerClass(
|
|
|
|
class AutorunSource extends MessageTray.Source {
|
|
|
|
_init(manager, mount, apps) {
|
|
|
|
super._init(mount.get_name());
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2012-09-02 21:23:50 -04:00
|
|
|
this._manager = manager;
|
2011-07-12 09:47:43 -04:00
|
|
|
this.mount = mount;
|
2011-07-12 10:37:14 -04:00
|
|
|
this.apps = apps;
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2015-02-12 09:38:17 -05:00
|
|
|
this._notification = new AutorunNotification(this._manager, this);
|
2011-07-12 09:47:43 -04:00
|
|
|
|
|
|
|
// add ourselves as a source, and popup the notification
|
|
|
|
Main.messageTray.add(this);
|
2019-05-13 17:32:31 -04:00
|
|
|
this.showNotification(this._notification);
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
getIcon() {
|
2012-09-15 02:10:15 -04:00
|
|
|
return this.mount.get_icon();
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2015-11-16 14:27:08 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_createPolicy() {
|
2015-11-16 14:27:08 -05:00
|
|
|
return new MessageTray.NotificationApplicationPolicy('org.gnome.Nautilus');
|
2011-07-12 09:47:43 -04:00
|
|
|
}
|
2019-05-13 17:32:31 -04:00
|
|
|
});
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2019-05-13 17:32:31 -04:00
|
|
|
var AutorunNotification = GObject.registerClass(
|
|
|
|
class AutorunNotification extends MessageTray.Notification {
|
|
|
|
_init(manager, source) {
|
|
|
|
super._init(source, source.title);
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2012-09-02 21:23:50 -04:00
|
|
|
this._manager = manager;
|
2011-07-12 09:47:43 -04:00
|
|
|
this._mount = source.mount;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2015-02-16 21:40:25 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
createBanner() {
|
2015-02-16 21:40:25 -05:00
|
|
|
let banner = new MessageTray.NotificationBanner(this);
|
|
|
|
|
2017-10-30 20:38:18 -04:00
|
|
|
this.source.apps.forEach(app => {
|
2011-07-12 10:37:14 -04:00
|
|
|
let actor = this._buttonForApp(app);
|
2011-07-12 09:47:43 -04:00
|
|
|
|
|
|
|
if (actor)
|
2015-02-16 21:40:25 -05:00
|
|
|
banner.addButton(actor);
|
2017-10-30 20:38:18 -04:00
|
|
|
});
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2015-02-16 21:40:25 -05:00
|
|
|
return banner;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_buttonForApp(app) {
|
2019-10-17 17:40:24 -04:00
|
|
|
let box = new St.BoxLayout({
|
|
|
|
x_expand: true,
|
|
|
|
x_align: Clutter.ActorAlign.START,
|
|
|
|
});
|
2020-03-29 17:51:13 -04:00
|
|
|
const icon = new St.Icon({
|
|
|
|
gicon: app.get_icon(),
|
|
|
|
style_class: 'hotplug-notification-item-icon',
|
|
|
|
});
|
2011-07-12 09:47:43 -04:00
|
|
|
box.add(icon);
|
|
|
|
|
2019-02-12 09:02:09 -05:00
|
|
|
let label = new St.Bin({
|
2019-10-17 17:40:24 -04:00
|
|
|
child: new St.Label({
|
|
|
|
text: _("Open with %s").format(app.get_name()),
|
|
|
|
y_align: Clutter.ActorAlign.CENTER,
|
|
|
|
}),
|
2019-02-12 09:02:09 -05:00
|
|
|
});
|
2011-07-12 09:47:43 -04:00
|
|
|
box.add(label);
|
|
|
|
|
2020-03-29 17:51:13 -04:00
|
|
|
const button = new St.Button({
|
|
|
|
child: box,
|
|
|
|
x_expand: true,
|
|
|
|
button_mask: St.ButtonMask.ONE,
|
|
|
|
style_class: 'hotplug-notification-item button',
|
|
|
|
});
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2017-10-30 20:38:18 -04:00
|
|
|
button.connect('clicked', () => {
|
2011-07-12 09:47:43 -04:00
|
|
|
startAppForMount(app, this._mount);
|
|
|
|
this.destroy();
|
2017-10-30 20:38:18 -04:00
|
|
|
});
|
2011-07-12 09:47:43 -04:00
|
|
|
|
|
|
|
return button;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
activate() {
|
2017-10-30 21:19:44 -04:00
|
|
|
super.activate();
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2015-02-12 09:38:17 -05:00
|
|
|
let app = Gio.app_info_get_default_for_type('inode/directory', false);
|
|
|
|
startAppForMount(app, this._mount);
|
2011-07-12 09:47:43 -04:00
|
|
|
}
|
2019-05-13 17:32:31 -04:00
|
|
|
});
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2017-07-18 13:47:27 -04:00
|
|
|
var Component = AutorunManager;
|