2011-09-28 09:16:26 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2011-07-12 09:47:43 -04:00
|
|
|
|
|
|
|
const Lang = imports.lang;
|
|
|
|
const Gio = imports.gi.Gio;
|
|
|
|
const St = imports.gi.St;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
// 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';
|
|
|
|
|
|
|
|
const AutorunSetting = {
|
|
|
|
RUN: 0,
|
|
|
|
IGNORE: 1,
|
|
|
|
FILES: 2,
|
|
|
|
ASK: 3
|
|
|
|
};
|
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
|
|
|
|
return (path.indexOf('/.') != -1);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
return (volume.get_identifier("class") == "network");
|
|
|
|
}
|
|
|
|
|
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 {
|
2011-07-11 09:31:56 -04:00
|
|
|
retval = app.launch(files,
|
2016-10-19 09:43:37 -04:00
|
|
|
global.create_app_launch_context(0, -1),
|
|
|
|
false)
|
2011-07-12 09:47:43 -04:00
|
|
|
} catch (e) {
|
|
|
|
log('Unable to launch the application ' + app.get_name()
|
|
|
|
+ ': ' + e.toString());
|
|
|
|
}
|
2011-07-11 09:31:56 -04:00
|
|
|
|
|
|
|
return retval;
|
2011-07-12 09:47:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************/
|
|
|
|
|
2013-10-24 17:51:58 -04:00
|
|
|
const HotplugSnifferIface = '<node> \
|
|
|
|
<interface name="org.gnome.Shell.HotplugSniffer"> \
|
|
|
|
<method name="SniffURI"> \
|
|
|
|
<arg type="s" direction="in" /> \
|
|
|
|
<arg type="as" direction="out" /> \
|
|
|
|
</method> \
|
|
|
|
</interface> \
|
|
|
|
</node>';
|
2011-07-12 10:37:14 -04:00
|
|
|
|
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
|
|
|
|
2011-11-20 12:56:27 -05:00
|
|
|
const ContentTypeDiscoverer = new Lang.Class({
|
|
|
|
Name: 'ContentTypeDiscoverer',
|
2011-07-12 09:47:43 -04:00
|
|
|
|
|
|
|
_init: function(callback) {
|
|
|
|
this._callback = callback;
|
2014-06-24 15:17:09 -04:00
|
|
|
this._settings = new Gio.Settings({ schema_id: SETTINGS_SCHEMA });
|
2011-07-12 09:47:43 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
guessContentTypes: function(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
|
|
|
|
2012-09-16 13:54:25 -04:00
|
|
|
if (shouldScan) {
|
2012-09-16 13:24:25 -04:00
|
|
|
// guess mount's content types using GIO
|
|
|
|
mount.guess_content_type(false, null,
|
|
|
|
Lang.bind(this,
|
|
|
|
this._onContentTypeGuessed));
|
|
|
|
} else {
|
|
|
|
this._emitCallback(mount, []);
|
|
|
|
}
|
2011-07-12 09:47:43 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
_onContentTypeGuessed: function(mount, res) {
|
|
|
|
let contentTypes = [];
|
|
|
|
|
|
|
|
try {
|
|
|
|
contentTypes = mount.guess_content_type_finish(res);
|
|
|
|
} catch (e) {
|
|
|
|
log('Unable to guess content types on added mount ' + mount.get_name()
|
|
|
|
+ ': ' + e.toString());
|
|
|
|
}
|
|
|
|
|
2011-07-12 10:37:14 -04:00
|
|
|
if (contentTypes.length) {
|
|
|
|
this._emitCallback(mount, contentTypes);
|
|
|
|
} else {
|
|
|
|
let root = mount.get_root();
|
|
|
|
|
|
|
|
let hotplugSniffer = new HotplugSniffer();
|
2011-08-16 08:28:53 -04:00
|
|
|
hotplugSniffer.SniffURIRemote(root.get_uri(),
|
|
|
|
Lang.bind(this, function([contentTypes]) {
|
2011-07-12 10:37:14 -04:00
|
|
|
this._emitCallback(mount, contentTypes);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_emitCallback: function(mount, contentTypes) {
|
|
|
|
if (!contentTypes)
|
|
|
|
contentTypes = [];
|
|
|
|
|
2011-07-12 09:47:43 -04:00
|
|
|
// we're not interested in win32 software content types here
|
|
|
|
contentTypes = contentTypes.filter(function(type) {
|
|
|
|
return (type != 'x-content/win32-software');
|
|
|
|
});
|
|
|
|
|
2011-07-12 10:37:14 -04:00
|
|
|
let apps = [];
|
|
|
|
contentTypes.forEach(function(type) {
|
|
|
|
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 09:47:43 -04:00
|
|
|
}
|
2011-11-20 12:56:27 -05:00
|
|
|
});
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2011-11-20 12:56:27 -05:00
|
|
|
const AutorunManager = new Lang.Class({
|
|
|
|
Name: 'AutorunManager',
|
2011-07-12 09:47:43 -04:00
|
|
|
|
|
|
|
_init: function() {
|
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);
|
2012-09-02 21:23:50 -04:00
|
|
|
},
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2012-09-04 20:24:56 -04:00
|
|
|
enable: function() {
|
2012-09-02 21:23:50 -04:00
|
|
|
this._mountAddedId = this._volumeMonitor.connect('mount-added', Lang.bind(this, this._onMountAdded));
|
|
|
|
this._mountRemovedId = this._volumeMonitor.connect('mount-removed', Lang.bind(this, this._onMountRemoved));
|
|
|
|
},
|
|
|
|
|
|
|
|
disable: function() {
|
|
|
|
this._volumeMonitor.disconnect(this._mountAddedId);
|
|
|
|
this._volumeMonitor.disconnect(this._mountRemovedId);
|
|
|
|
},
|
2011-07-12 09:47:43 -04:00
|
|
|
|
|
|
|
_onMountAdded: function(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;
|
|
|
|
|
2015-02-12 09:15:51 -05:00
|
|
|
let discoverer = new ContentTypeDiscoverer(Lang.bind(this, function(mount, apps, contentTypes) {
|
2015-02-12 09:38:17 -05:00
|
|
|
this._dispatcher.addMount(mount, apps, contentTypes);
|
2015-02-12 09:15:51 -05:00
|
|
|
}));
|
|
|
|
discoverer.guessContentTypes(mount);
|
2011-07-12 09:47:43 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
_onMountRemoved: function(monitor, mount) {
|
2015-02-12 09:38:17 -05:00
|
|
|
this._dispatcher.removeMount(mount);
|
2015-02-19 06:58:26 -05:00
|
|
|
}
|
2011-11-20 12:56:27 -05:00
|
|
|
});
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2015-02-12 09:38:17 -05:00
|
|
|
const AutorunDispatcher = new Lang.Class({
|
|
|
|
Name: 'AutorunDispatcher',
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2012-09-02 21:23:50 -04:00
|
|
|
_init: function(manager) {
|
|
|
|
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 });
|
2011-07-12 09:47:43 -04:00
|
|
|
},
|
|
|
|
|
2011-07-11 09:31:56 -04:00
|
|
|
_getAutorunSettingForType: function(contentType) {
|
|
|
|
let runApp = this._settings.get_strv(SETTING_START_APP);
|
|
|
|
if (runApp.indexOf(contentType) != -1)
|
|
|
|
return AutorunSetting.RUN;
|
|
|
|
|
|
|
|
let ignore = this._settings.get_strv(SETTING_IGNORE);
|
|
|
|
if (ignore.indexOf(contentType) != -1)
|
|
|
|
return AutorunSetting.IGNORE;
|
|
|
|
|
|
|
|
let openFiles = this._settings.get_strv(SETTING_OPEN_FOLDER);
|
|
|
|
if (openFiles.indexOf(contentType) != -1)
|
|
|
|
return AutorunSetting.FILES;
|
|
|
|
|
|
|
|
return AutorunSetting.ASK;
|
|
|
|
},
|
|
|
|
|
2011-07-12 09:47:43 -04:00
|
|
|
_getSourceForMount: function(mount) {
|
|
|
|
let filtered =
|
|
|
|
this._sources.filter(function (source) {
|
|
|
|
return (source.mount == mount);
|
|
|
|
});
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
},
|
|
|
|
|
2011-07-12 10:37:14 -04:00
|
|
|
_addSource: function(mount, apps) {
|
2011-07-11 09:31:56 -04:00
|
|
|
// if we already have a source showing for this
|
|
|
|
// mount, return
|
|
|
|
if (this._getSourceForMount(mount))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// add a new source
|
2015-02-12 09:38:17 -05:00
|
|
|
this._sources.push(new AutorunSource(this._manager, mount, apps));
|
2011-07-11 09:31:56 -04:00
|
|
|
},
|
|
|
|
|
2011-07-12 10:37:14 -04:00
|
|
|
addMount: function(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;
|
|
|
|
|
2011-07-11 09:31:56 -04:00
|
|
|
let setting = this._getAutorunSettingForType(contentTypes[0]);
|
|
|
|
|
|
|
|
// 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 16:17:32 -04:00
|
|
|
app = Gio.app_info_get_default_for_type(contentTypes[0], false);
|
2011-07-11 09:31:56 -04: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 10:37:14 -04:00
|
|
|
this._addSource(mount, apps);
|
2011-07-12 09:47:43 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
removeMount: function(mount) {
|
|
|
|
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();
|
|
|
|
}
|
2011-11-20 12:56:27 -05:00
|
|
|
});
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2015-02-12 09:38:17 -05:00
|
|
|
const AutorunSource = new Lang.Class({
|
|
|
|
Name: 'AutorunSource',
|
2011-11-20 10:12:02 -05:00
|
|
|
Extends: MessageTray.Source,
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2012-09-02 21:23:50 -04:00
|
|
|
_init: function(manager, mount, apps) {
|
|
|
|
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
|
|
|
|
2011-10-08 18:00:32 -04:00
|
|
|
this.parent(mount.get_name());
|
|
|
|
|
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);
|
|
|
|
this.notify(this._notification);
|
|
|
|
},
|
|
|
|
|
2012-09-15 02:10:15 -04:00
|
|
|
getIcon: function() {
|
|
|
|
return this.mount.get_icon();
|
2015-11-16 14:27:08 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
_createPolicy: function() {
|
|
|
|
return new MessageTray.NotificationApplicationPolicy('org.gnome.Nautilus');
|
2011-07-12 09:47:43 -04:00
|
|
|
}
|
2011-11-20 10:12:02 -05:00
|
|
|
});
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2015-02-12 09:38:17 -05:00
|
|
|
const AutorunNotification = new Lang.Class({
|
|
|
|
Name: 'AutorunNotification',
|
2011-11-20 10:12:02 -05:00
|
|
|
Extends: MessageTray.Notification,
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2012-09-02 21:23:50 -04:00
|
|
|
_init: function(manager, source) {
|
2015-02-12 09:38:17 -05:00
|
|
|
this.parent(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;
|
2015-02-16 21:40:25 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
createBanner: function() {
|
|
|
|
let banner = new MessageTray.NotificationBanner(this);
|
|
|
|
|
|
|
|
this.source.apps.forEach(Lang.bind(this, function (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);
|
2011-07-12 09:47:43 -04:00
|
|
|
}));
|
|
|
|
|
2015-02-16 21:40:25 -05:00
|
|
|
return banner;
|
2011-07-12 09:47:43 -04:00
|
|
|
},
|
|
|
|
|
2011-07-12 10:37:14 -04:00
|
|
|
_buttonForApp: function(app) {
|
2011-07-12 09:47:43 -04:00
|
|
|
let box = new St.BoxLayout();
|
|
|
|
let icon = new St.Icon({ gicon: app.get_icon(),
|
|
|
|
style_class: 'hotplug-notification-item-icon' });
|
|
|
|
box.add(icon);
|
|
|
|
|
|
|
|
let label = new St.Bin({ y_align: St.Align.MIDDLE,
|
|
|
|
child: new St.Label
|
2011-12-03 06:56:04 -05:00
|
|
|
({ text: _("Open with %s").format(app.get_name()) })
|
2011-07-12 09:47:43 -04:00
|
|
|
});
|
|
|
|
box.add(label);
|
|
|
|
|
|
|
|
let button = new St.Button({ child: box,
|
2011-07-12 10:37:14 -04:00
|
|
|
x_fill: true,
|
|
|
|
x_align: St.Align.START,
|
2015-02-16 21:40:25 -05:00
|
|
|
x_expand: true,
|
2011-07-12 09:47:43 -04:00
|
|
|
button_mask: St.ButtonMask.ONE,
|
2015-01-16 09:33:56 -05:00
|
|
|
style_class: 'hotplug-notification-item button' });
|
2011-07-12 09:47:43 -04:00
|
|
|
|
|
|
|
button.connect('clicked', Lang.bind(this, function() {
|
|
|
|
startAppForMount(app, this._mount);
|
|
|
|
this.destroy();
|
|
|
|
}));
|
|
|
|
|
|
|
|
return button;
|
|
|
|
},
|
|
|
|
|
2015-02-18 18:00:25 -05:00
|
|
|
activate: function() {
|
2015-02-12 09:38:17 -05:00
|
|
|
this.parent();
|
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
|
|
|
}
|
2011-11-20 10:12:02 -05:00
|
|
|
});
|
2011-07-12 09:47:43 -04:00
|
|
|
|
2012-09-02 21:23:50 -04:00
|
|
|
const Component = AutorunManager;
|