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-06-20 19:16:40 +00:00
|
|
|
|
2019-02-09 03:21:36 +00:00
|
|
|
const { Gio, GLib } = imports.gi;
|
2011-06-20 19:16:40 +00:00
|
|
|
const Params = imports.misc.params;
|
2012-08-17 12:18:53 +00:00
|
|
|
|
|
|
|
const GnomeSession = imports.misc.gnomeSession;
|
2019-04-07 21:35:07 +00:00
|
|
|
const Main = imports.ui.main;
|
2011-06-22 20:43:16 +00:00
|
|
|
const ShellMountOperation = imports.ui.shellMountOperation;
|
2012-06-26 09:26:36 +00:00
|
|
|
|
2017-07-18 17:47:27 +00:00
|
|
|
var GNOME_SESSION_AUTOMOUNT_INHIBIT = 16;
|
2011-06-20 19:16:40 +00:00
|
|
|
|
|
|
|
// GSettings keys
|
|
|
|
const SETTINGS_SCHEMA = 'org.gnome.desktop.media-handling';
|
|
|
|
const SETTING_ENABLE_AUTOMOUNT = 'automount';
|
|
|
|
|
2017-07-18 17:47:27 +00:00
|
|
|
var AUTORUN_EXPIRE_TIMEOUT_SECS = 10;
|
2011-06-22 13:45:03 +00:00
|
|
|
|
2017-10-31 01:19:44 +00:00
|
|
|
var AutomountManager = class {
|
|
|
|
constructor() {
|
2014-06-24 19:17:09 +00:00
|
|
|
this._settings = new Gio.Settings({ schema_id: SETTINGS_SCHEMA });
|
2011-06-20 19:16:40 +00:00
|
|
|
this._volumeQueue = [];
|
2018-10-05 17:09:28 +00:00
|
|
|
this._activeOperations = new Map();
|
2012-06-26 09:26:36 +00:00
|
|
|
this._session = new GnomeSession.SessionManager();
|
|
|
|
this._session.connectSignal('InhibitorAdded',
|
2017-12-02 00:27:35 +00:00
|
|
|
this._InhibitorsChanged.bind(this));
|
2012-06-26 09:26:36 +00:00
|
|
|
this._session.connectSignal('InhibitorRemoved',
|
2017-12-02 00:27:35 +00:00
|
|
|
this._InhibitorsChanged.bind(this));
|
2012-06-26 09:26:36 +00:00
|
|
|
this._inhibited = false;
|
2011-06-20 19:16:40 +00:00
|
|
|
|
2012-09-03 01:23:50 +00:00
|
|
|
this._volumeMonitor = Gio.VolumeMonitor.get();
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-06-20 19:16:40 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
enable() {
|
2017-12-02 00:27:35 +00:00
|
|
|
this._volumeAddedId = this._volumeMonitor.connect('volume-added', this._onVolumeAdded.bind(this));
|
|
|
|
this._volumeRemovedId = this._volumeMonitor.connect('volume-removed', this._onVolumeRemoved.bind(this));
|
|
|
|
this._driveConnectedId = this._volumeMonitor.connect('drive-connected', this._onDriveConnected.bind(this));
|
|
|
|
this._driveDisconnectedId = this._volumeMonitor.connect('drive-disconnected', this._onDriveDisconnected.bind(this));
|
|
|
|
this._driveEjectButtonId = this._volumeMonitor.connect('drive-eject-button', this._onDriveEjectButton.bind(this));
|
2011-06-20 19:16:40 +00:00
|
|
|
|
2019-08-19 18:50:33 +00:00
|
|
|
this._mountAllId = GLib.idle_add(GLib.PRIORITY_DEFAULT, this._startupMountAll.bind(this));
|
2014-04-10 17:26:52 +00:00
|
|
|
GLib.Source.set_name_by_id(this._mountAllId, '[gnome-shell] this._startupMountAll');
|
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._volumeAddedId);
|
|
|
|
this._volumeMonitor.disconnect(this._volumeRemovedId);
|
|
|
|
this._volumeMonitor.disconnect(this._driveConnectedId);
|
|
|
|
this._volumeMonitor.disconnect(this._driveDisconnectedId);
|
|
|
|
this._volumeMonitor.disconnect(this._driveEjectButtonId);
|
2011-06-20 19:16:40 +00:00
|
|
|
|
2012-09-03 01:23:50 +00:00
|
|
|
if (this._mountAllId > 0) {
|
2019-08-19 18:50:33 +00:00
|
|
|
GLib.source_remove(this._mountAllId);
|
2012-09-03 01:23:50 +00:00
|
|
|
this._mountAllId = 0;
|
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-06-20 19:16:40 +00:00
|
|
|
|
2019-01-31 14:08:10 +00:00
|
|
|
_InhibitorsChanged(_object, _senderName, [_inhibitor]) {
|
2012-06-26 09:26:36 +00:00
|
|
|
this._session.IsInhibitedRemote(GNOME_SESSION_AUTOMOUNT_INHIBIT,
|
2017-10-31 00:38:18 +00:00
|
|
|
(result, error) => {
|
2019-08-20 00:51:42 +00:00
|
|
|
if (!error)
|
2017-10-31 00:38:18 +00:00
|
|
|
this._inhibited = result[0];
|
|
|
|
});
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-06-26 09:26:36 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_startupMountAll() {
|
2011-06-20 19:16:40 +00:00
|
|
|
let volumes = this._volumeMonitor.get_volumes();
|
2017-10-31 00:38:18 +00:00
|
|
|
volumes.forEach(volume => {
|
2011-06-20 19:16:40 +00:00
|
|
|
this._checkAndMountVolume(volume, { checkSession: false,
|
2012-06-19 18:49:40 +00:00
|
|
|
useMountOp: false,
|
|
|
|
allowAutorun: false });
|
2017-10-31 00:38:18 +00:00
|
|
|
});
|
2011-06-20 19:16:40 +00:00
|
|
|
|
2012-09-03 01:23:50 +00:00
|
|
|
this._mountAllId = 0;
|
2013-11-29 00:45:39 +00:00
|
|
|
return GLib.SOURCE_REMOVE;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-06-20 19:16:40 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onDriveConnected() {
|
2011-06-27 15:51:23 +00:00
|
|
|
// if we're not in the current ConsoleKit session,
|
|
|
|
// or screensaver is active, don't play sounds
|
2013-02-03 21:24:33 +00:00
|
|
|
if (!this._session.SessionIsActive)
|
2012-02-11 03:51:42 +00:00
|
|
|
return;
|
2011-06-27 15:51:23 +00:00
|
|
|
|
2018-12-13 19:30:22 +00:00
|
|
|
let player = global.display.get_sound_player();
|
|
|
|
player.play_from_theme('device-added-media',
|
|
|
|
_("External drive connected"),
|
|
|
|
null);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-06-27 15:51:23 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onDriveDisconnected() {
|
2011-06-27 15:51:23 +00:00
|
|
|
// if we're not in the current ConsoleKit session,
|
|
|
|
// or screensaver is active, don't play sounds
|
2013-02-03 21:24:33 +00:00
|
|
|
if (!this._session.SessionIsActive)
|
2012-02-11 03:51:42 +00:00
|
|
|
return;
|
2011-06-27 15:51:23 +00:00
|
|
|
|
2019-03-04 13:36:13 +00:00
|
|
|
let player = global.display.get_sound_player();
|
|
|
|
player.play_from_theme('device-removed-media',
|
|
|
|
_("External drive disconnected"),
|
|
|
|
null);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-06-27 15:51:23 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onDriveEjectButton(monitor, drive) {
|
2011-06-27 16:13:54 +00:00
|
|
|
// TODO: this code path is not tested, as the GVfs volume monitor
|
|
|
|
// doesn't emit this signal just yet.
|
2013-02-03 21:24:33 +00:00
|
|
|
if (!this._session.SessionIsActive)
|
2011-06-27 16:13:54 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// we force stop/eject in this case, so we don't have to pass a
|
|
|
|
// mount operation object
|
|
|
|
if (drive.can_stop()) {
|
2019-01-29 19:36:54 +00:00
|
|
|
drive.stop(Gio.MountUnmountFlags.FORCE, null, null,
|
2019-08-20 00:20:08 +00:00
|
|
|
(o, res) => {
|
2019-01-29 19:36:54 +00:00
|
|
|
try {
|
|
|
|
drive.stop_finish(res);
|
|
|
|
} catch (e) {
|
2020-02-14 15:10:34 +00:00
|
|
|
log('Unable to stop the drive after drive-eject-button %s'.format(e.toString()));
|
2019-01-29 19:36:54 +00:00
|
|
|
}
|
|
|
|
});
|
2011-06-27 16:13:54 +00:00
|
|
|
} else if (drive.can_eject()) {
|
2019-01-29 19:36:54 +00:00
|
|
|
drive.eject_with_operation(Gio.MountUnmountFlags.FORCE, null, null,
|
2019-08-20 00:20:08 +00:00
|
|
|
(o, res) => {
|
2019-01-29 19:36:54 +00:00
|
|
|
try {
|
|
|
|
drive.eject_with_operation_finish(res);
|
|
|
|
} catch (e) {
|
2020-02-14 15:10:34 +00:00
|
|
|
log('Unable to eject the drive after drive-eject-button %s'.format(e.toString()));
|
2019-01-29 19:36:54 +00:00
|
|
|
}
|
|
|
|
});
|
2011-06-27 16:13:54 +00:00
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-06-27 16:13:54 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onVolumeAdded(monitor, volume) {
|
2011-06-20 19:16:40 +00:00
|
|
|
this._checkAndMountVolume(volume);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-06-20 19:16:40 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_checkAndMountVolume(volume, params) {
|
2011-06-20 19:16:40 +00:00
|
|
|
params = Params.parse(params, { checkSession: true,
|
2012-06-19 18:49:40 +00:00
|
|
|
useMountOp: true,
|
|
|
|
allowAutorun: true });
|
2011-06-20 19:16:40 +00:00
|
|
|
|
|
|
|
if (params.checkSession) {
|
|
|
|
// if we're not in the current ConsoleKit session,
|
|
|
|
// don't attempt automount
|
2013-02-03 21:24:33 +00:00
|
|
|
if (!this._session.SessionIsActive)
|
2011-06-20 19:16:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-26 09:26:36 +00:00
|
|
|
if (this._inhibited)
|
|
|
|
return;
|
|
|
|
|
2011-09-28 20:29:45 +00:00
|
|
|
// Volume is already mounted, don't bother.
|
|
|
|
if (volume.get_mount())
|
|
|
|
return;
|
|
|
|
|
2011-06-22 13:45:03 +00:00
|
|
|
if (!this._settings.get_boolean(SETTING_ENABLE_AUTOMOUNT) ||
|
|
|
|
!volume.should_automount() ||
|
|
|
|
!volume.can_mount()) {
|
2011-09-28 20:29:45 +00:00
|
|
|
// allow the autorun to run anyway; this can happen if the
|
2019-09-12 15:26:08 +00:00
|
|
|
// mount gets added programmatically later, even if
|
2011-06-22 13:45:03 +00:00
|
|
|
// should_automount() or can_mount() are false, like for
|
|
|
|
// blank optical media.
|
|
|
|
this._allowAutorun(volume);
|
|
|
|
this._allowAutorunExpire(volume);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-22 20:43:16 +00:00
|
|
|
if (params.useMountOp) {
|
|
|
|
let operation = new ShellMountOperation.ShellMountOperation(volume);
|
2012-06-20 00:22:26 +00:00
|
|
|
this._mountVolume(volume, operation, params.allowAutorun);
|
2011-06-22 20:43:16 +00:00
|
|
|
} else {
|
2012-06-19 18:49:40 +00:00
|
|
|
this._mountVolume(volume, null, params.allowAutorun);
|
2011-06-22 20:43:16 +00:00
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-06-20 19:16:40 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_mountVolume(volume, operation, allowAutorun) {
|
2012-06-19 18:49:40 +00:00
|
|
|
if (allowAutorun)
|
|
|
|
this._allowAutorun(volume);
|
|
|
|
|
2012-06-20 00:22:26 +00:00
|
|
|
let mountOp = operation ? operation.mountOp : null;
|
2018-10-05 17:09:28 +00:00
|
|
|
this._activeOperations.set(volume, operation);
|
2012-06-20 00:22:26 +00:00
|
|
|
|
|
|
|
volume.mount(0, mountOp, null,
|
2017-12-02 00:27:35 +00:00
|
|
|
this._onVolumeMounted.bind(this));
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-06-20 19:16:40 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onVolumeMounted(volume, res) {
|
2011-06-22 13:45:03 +00:00
|
|
|
this._allowAutorunExpire(volume);
|
|
|
|
|
2011-06-20 19:16:40 +00:00
|
|
|
try {
|
|
|
|
volume.mount_finish(res);
|
2012-06-20 00:22:26 +00:00
|
|
|
this._closeOperation(volume);
|
2011-06-20 19:16:40 +00:00
|
|
|
} catch (e) {
|
2012-06-20 00:22:26 +00:00
|
|
|
// FIXME: we will always get G_IO_ERROR_FAILED from the gvfs udisks
|
2018-10-11 17:14:25 +00:00
|
|
|
// backend, see https://bugs.freedesktop.org/show_bug.cgi?id=51271
|
|
|
|
// To reask the password if the user input was empty or wrong, we
|
|
|
|
// will check for corresponding error messages. However, these
|
|
|
|
// error strings are not unique for the cases in the comments below.
|
|
|
|
if (e.message.includes('No key available with this passphrase') || // cryptsetup
|
|
|
|
e.message.includes('No key available to unlock device') || // udisks (no password)
|
2019-04-07 21:35:07 +00:00
|
|
|
// libblockdev wrong password opening LUKS device
|
|
|
|
e.message.includes('Failed to activate device: Incorrect passphrase') ||
|
|
|
|
// cryptsetup returns EINVAL in many cases, including wrong TCRYPT password/parameters
|
|
|
|
e.message.includes('Failed to load device\'s parameters: Invalid argument')) {
|
2011-07-12 15:29:49 +00:00
|
|
|
this._reaskPassword(volume);
|
2012-06-20 00:22:26 +00:00
|
|
|
} else {
|
2019-04-07 21:35:07 +00:00
|
|
|
if (e.message.includes('Compiled against a version of libcryptsetup that does not support the VeraCrypt PIM setting')) {
|
|
|
|
Main.notifyError(_("Unable to unlock volume"),
|
|
|
|
_("The installed udisks version does not support the PIM setting"));
|
|
|
|
}
|
|
|
|
|
2012-06-20 20:35:29 +00:00
|
|
|
if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.FAILED_HANDLED))
|
2020-02-14 15:10:34 +00:00
|
|
|
log('Unable to mount volume %s: %s'.format(volume.get_name(), e.toString()));
|
2012-06-20 00:22:26 +00:00
|
|
|
this._closeOperation(volume);
|
|
|
|
}
|
2011-06-20 19:16:40 +00:00
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-06-20 19:16:40 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onVolumeRemoved(monitor, volume) {
|
2018-07-04 14:55:28 +00:00
|
|
|
if (volume._allowAutorunExpireId && volume._allowAutorunExpireId > 0) {
|
2019-08-19 18:50:33 +00:00
|
|
|
GLib.source_remove(volume._allowAutorunExpireId);
|
2018-07-04 14:55:28 +00:00
|
|
|
delete volume._allowAutorunExpireId;
|
|
|
|
}
|
2019-09-12 15:26:08 +00:00
|
|
|
this._volumeQueue =
|
2019-08-19 19:38:51 +00:00
|
|
|
this._volumeQueue.filter(element => element != volume);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-06-22 13:45:03 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_reaskPassword(volume) {
|
2018-10-05 17:09:28 +00:00
|
|
|
let prevOperation = this._activeOperations.get(volume);
|
|
|
|
let existingDialog = prevOperation ? prevOperation.borrowDialog() : null;
|
2019-09-12 15:26:08 +00:00
|
|
|
let operation =
|
2012-06-20 00:22:26 +00:00
|
|
|
new ShellMountOperation.ShellMountOperation(volume,
|
2019-08-19 19:06:04 +00:00
|
|
|
{ existingDialog });
|
2012-06-20 00:22:26 +00:00
|
|
|
this._mountVolume(volume, operation);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-06-20 00:22:26 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_closeOperation(volume) {
|
2018-10-05 17:09:28 +00:00
|
|
|
let operation = this._activeOperations.get(volume);
|
|
|
|
if (!operation)
|
|
|
|
return;
|
|
|
|
operation.close();
|
|
|
|
this._activeOperations.delete(volume);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-07-12 15:29:49 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_allowAutorun(volume) {
|
2011-06-22 13:45:03 +00:00
|
|
|
volume.allowAutorun = true;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-06-22 13:45:03 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_allowAutorunExpire(volume) {
|
2019-08-19 18:50:33 +00:00
|
|
|
let id = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, AUTORUN_EXPIRE_TIMEOUT_SECS, () => {
|
2011-06-22 13:45:03 +00:00
|
|
|
volume.allowAutorun = false;
|
2018-07-04 14:55:28 +00:00
|
|
|
delete volume._allowAutorunExpireId;
|
2013-11-29 00:45:39 +00:00
|
|
|
return GLib.SOURCE_REMOVE;
|
2011-06-22 13:45:03 +00:00
|
|
|
});
|
2018-07-04 14:55:28 +00:00
|
|
|
volume._allowAutorunExpireId = id;
|
2014-04-10 17:26:52 +00:00
|
|
|
GLib.Source.set_name_by_id(id, '[gnome-shell] volume.allowAutorun');
|
2011-06-20 19:16:40 +00:00
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
};
|
2017-07-18 17:47:27 +00:00
|
|
|
var Component = AutomountManager;
|