2011-09-28 13:16:26 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
import Clutter from 'gi://Clutter';
|
|
|
|
import Gio from 'gi://Gio';
|
|
|
|
import GObject from 'gi://GObject';
|
|
|
|
import St from 'gi://St';
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
import * as GnomeSession from '../../misc/gnomeSession.js';
|
|
|
|
import * as Main from '../main.js';
|
|
|
|
import * as MessageTray from '../messageTray.js';
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2022-06-23 15:02:55 +00:00
|
|
|
Gio._promisify(Gio.Mount.prototype, 'guess_content_type');
|
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
import {loadInterfaceXML} from '../../misc/fileUtils.js';
|
2018-09-06 00:55:20 +00:00
|
|
|
|
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';
|
|
|
|
|
2023-07-30 12:56:59 +00:00
|
|
|
/** @enum {number} */
|
2023-07-10 09:53:00 +00:00
|
|
|
const AutorunSetting = {
|
2011-07-11 13:31:56 +00:00
|
|
|
RUN: 0,
|
|
|
|
IGNORE: 1,
|
|
|
|
FILES: 2,
|
2019-08-20 21:43:54 +00:00
|
|
|
ASK: 3,
|
2011-07-11 13:31:56 +00:00
|
|
|
};
|
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
|
2019-08-19 19:38:51 +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
|
|
|
|
2019-08-19 19:38:51 +00:00
|
|
|
return volume.get_identifier("class") == "network";
|
2012-09-16 17:54:25 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2019-09-12 15:26:08 +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) {
|
2022-12-16 21:33:10 +00:00
|
|
|
log(`Unable to launch the app ${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
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
class ContentTypeDiscoverer {
|
2022-06-23 15:02:55 +00:00
|
|
|
constructor() {
|
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
|
|
|
|
2022-06-23 15:02:55 +00:00
|
|
|
async 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
|
|
|
|
2011-07-12 13:47:43 +00:00
|
|
|
let contentTypes = [];
|
2022-06-23 15:02:55 +00: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();
|
2023-04-21 08:02:16 +00:00
|
|
|
[contentTypes] = await hotplugSniffer.SniffURIAsync(root.get_uri());
|
2022-06-23 15:02:55 +00:00
|
|
|
}
|
2011-07-12 14:37:14 +00:00
|
|
|
}
|
|
|
|
|
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(
|
2020-04-03 23:52:29 +00:00
|
|
|
type => type !== 'x-content/win32-software');
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2022-06-23 15:02:55 +00:00
|
|
|
const apps = [];
|
2017-10-31 00:38:18 +00:00
|
|
|
contentTypes.forEach(type => {
|
2022-06-23 15:02:55 +00:00
|
|
|
const app = Gio.app_info_get_default_for_type(type, false);
|
2011-07-12 14:37:14 +00:00
|
|
|
|
|
|
|
if (app)
|
|
|
|
apps.push(app);
|
|
|
|
});
|
|
|
|
|
2022-06-23 15:02:55 +00:00
|
|
|
if (apps.length === 0)
|
2011-07-12 14:37:14 +00:00
|
|
|
apps.push(Gio.app_info_get_default_for_type('inode/directory', false));
|
|
|
|
|
2022-06-23 15:02:55 +00:00
|
|
|
return [apps, contentTypes];
|
2011-07-12 13:47:43 +00:00
|
|
|
}
|
2023-07-10 09:53:00 +00:00
|
|
|
}
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
class AutorunManager {
|
2017-10-31 01:19:44 +00:00
|
|
|
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() {
|
2021-08-15 22:36:59 +00:00
|
|
|
this._volumeMonitor.connectObject(
|
|
|
|
'mount-added', this._onMountAdded.bind(this),
|
|
|
|
'mount-removed', this._onMountRemoved.bind(this), 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() {
|
2021-08-15 22:36:59 +00:00
|
|
|
this._volumeMonitor.disconnectObject(this);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2022-06-23 15:02:55 +00:00
|
|
|
async _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;
|
|
|
|
|
2022-06-23 15:02:55 +00:00
|
|
|
const discoverer = new ContentTypeDiscoverer();
|
|
|
|
const [apps, contentTypes] = await discoverer.guessContentTypes(mount);
|
|
|
|
this._dispatcher.addMount(mount, apps, contentTypes);
|
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
|
|
|
}
|
2023-07-10 09:53:00 +00:00
|
|
|
}
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
class AutorunDispatcher {
|
2017-10-31 01:19:44 +00:00
|
|
|
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) {
|
2019-08-19 19:38:51 +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) {
|
2019-09-12 15:26:08 +00:00
|
|
|
// if we already have a source showing for this
|
2011-07-11 13:31:56 +00:00
|
|
|
// mount, return
|
|
|
|
if (this._getSourceForMount(mount))
|
|
|
|
return;
|
2019-09-12 15:26:08 +00:00
|
|
|
|
2011-07-11 13:31:56 +00:00
|
|
|
// 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;
|
|
|
|
|
2019-08-20 00:51:42 +00:00
|
|
|
if (setting == AutorunSetting.RUN)
|
2011-10-03 20:17:32 +00:00
|
|
|
app = Gio.app_info_get_default_for_type(contentTypes[0], false);
|
2019-08-20 00:51:42 +00:00
|
|
|
else if (setting == AutorunSetting.FILES)
|
2011-07-11 13:31:56 +00: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 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);
|
2019-09-12 15:26:08 +00:00
|
|
|
|
2011-07-12 13:47:43 +00:00
|
|
|
// if we aren't tracking this mount, don't do anything
|
|
|
|
if (!source)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// destroy the notification source
|
|
|
|
source.destroy();
|
|
|
|
}
|
2023-07-10 09:53:00 +00:00
|
|
|
}
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
const AutorunSource = GObject.registerClass(
|
2019-05-13 21:32:31 +00:00
|
|
|
class AutorunSource extends MessageTray.Source {
|
|
|
|
_init(manager, mount, apps) {
|
|
|
|
super._init(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);
|
2019-05-13 21:32:31 +00:00
|
|
|
this.showNotification(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
|
|
|
}
|
2019-05-13 21:32:31 +00:00
|
|
|
});
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
const AutorunNotification = GObject.registerClass(
|
2019-05-13 21:32:31 +00:00
|
|
|
class AutorunNotification extends MessageTray.Notification {
|
|
|
|
_init(manager, source) {
|
|
|
|
super._init(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) {
|
2019-10-17 21:40:24 +00:00
|
|
|
let box = new St.BoxLayout({
|
|
|
|
x_expand: true,
|
|
|
|
x_align: Clutter.ActorAlign.START,
|
|
|
|
});
|
2020-03-29 21:51:13 +00:00
|
|
|
const icon = new St.Icon({
|
|
|
|
gicon: app.get_icon(),
|
|
|
|
style_class: 'hotplug-notification-item-icon',
|
|
|
|
});
|
2011-07-12 13:47:43 +00:00
|
|
|
box.add(icon);
|
|
|
|
|
2019-02-12 14:02:09 +00:00
|
|
|
let label = new St.Bin({
|
2019-10-17 21:40:24 +00:00
|
|
|
child: new St.Label({
|
|
|
|
text: _("Open with %s").format(app.get_name()),
|
|
|
|
y_align: Clutter.ActorAlign.CENTER,
|
|
|
|
}),
|
2019-02-12 14:02:09 +00:00
|
|
|
});
|
2011-07-12 13:47:43 +00:00
|
|
|
box.add(label);
|
|
|
|
|
2020-03-29 21:51:13 +00: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 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
|
|
|
}
|
2019-05-13 21:32:31 +00:00
|
|
|
});
|
2011-07-12 13:47:43 +00:00
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
export {AutorunManager as Component};
|