2011-09-28 09:16:26 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2011-06-20 15:16:40 -04:00
|
|
|
|
|
|
|
const Lang = imports.lang;
|
|
|
|
const Mainloop = imports.mainloop;
|
2012-02-10 22:51:42 -05:00
|
|
|
const GLib = imports.gi.GLib;
|
2011-06-20 15:16:40 -04:00
|
|
|
const Gio = imports.gi.Gio;
|
|
|
|
const Params = imports.misc.params;
|
2012-02-10 22:51:42 -05:00
|
|
|
const Shell = imports.gi.Shell;
|
2012-08-17 08:18:53 -04:00
|
|
|
|
|
|
|
const GnomeSession = imports.misc.gnomeSession;
|
2011-06-20 15:16:40 -04:00
|
|
|
const Main = imports.ui.main;
|
2011-06-22 16:43:16 -04:00
|
|
|
const ShellMountOperation = imports.ui.shellMountOperation;
|
2012-06-26 05:26:36 -04:00
|
|
|
|
2017-07-18 13:47:27 -04:00
|
|
|
var GNOME_SESSION_AUTOMOUNT_INHIBIT = 16;
|
2011-06-20 15:16:40 -04:00
|
|
|
|
|
|
|
// GSettings keys
|
|
|
|
const SETTINGS_SCHEMA = 'org.gnome.desktop.media-handling';
|
|
|
|
const SETTING_ENABLE_AUTOMOUNT = 'automount';
|
|
|
|
|
2017-07-18 13:47:27 -04:00
|
|
|
var AUTORUN_EXPIRE_TIMEOUT_SECS = 10;
|
2011-06-22 09:45:03 -04:00
|
|
|
|
2017-07-18 13:41:25 -04:00
|
|
|
var AutomountManager = new Lang.Class({
|
2011-11-20 12:56:27 -05:00
|
|
|
Name: 'AutomountManager',
|
2011-06-20 15:16:40 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_init() {
|
2014-06-24 15:17:09 -04:00
|
|
|
this._settings = new Gio.Settings({ schema_id: SETTINGS_SCHEMA });
|
2011-06-20 15:16:40 -04:00
|
|
|
this._volumeQueue = [];
|
2018-10-05 13:09:28 -04:00
|
|
|
this._activeOperations = new Map();
|
2012-06-26 05:26:36 -04:00
|
|
|
this._session = new GnomeSession.SessionManager();
|
|
|
|
this._session.connectSignal('InhibitorAdded',
|
2017-12-01 19:27:35 -05:00
|
|
|
this._InhibitorsChanged.bind(this));
|
2012-06-26 05:26:36 -04:00
|
|
|
this._session.connectSignal('InhibitorRemoved',
|
2017-12-01 19:27:35 -05:00
|
|
|
this._InhibitorsChanged.bind(this));
|
2012-06-26 05:26:36 -04:00
|
|
|
this._inhibited = false;
|
2011-06-20 15:16:40 -04:00
|
|
|
|
2012-09-02 21:23:50 -04:00
|
|
|
this._volumeMonitor = Gio.VolumeMonitor.get();
|
|
|
|
},
|
2011-06-20 15:16:40 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
enable() {
|
2017-12-01 19:27:35 -05: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 15:16:40 -04:00
|
|
|
|
2017-12-01 19:27:35 -05:00
|
|
|
this._mountAllId = Mainloop.idle_add(this._startupMountAll.bind(this));
|
2014-04-10 13:26:52 -04:00
|
|
|
GLib.Source.set_name_by_id(this._mountAllId, '[gnome-shell] this._startupMountAll');
|
2012-09-02 21:23:50 -04:00
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
disable() {
|
2012-09-02 21:23:50 -04: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 15:16:40 -04:00
|
|
|
|
2012-09-02 21:23:50 -04:00
|
|
|
if (this._mountAllId > 0) {
|
|
|
|
Mainloop.source_remove(this._mountAllId);
|
|
|
|
this._mountAllId = 0;
|
|
|
|
}
|
2011-06-20 15:16:40 -04:00
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_InhibitorsChanged(object, senderName, [inhibtor]) {
|
2012-06-26 05:26:36 -04:00
|
|
|
this._session.IsInhibitedRemote(GNOME_SESSION_AUTOMOUNT_INHIBIT,
|
2017-10-30 20:38:18 -04:00
|
|
|
(result, error) => {
|
|
|
|
if (!error) {
|
|
|
|
this._inhibited = result[0];
|
|
|
|
}
|
|
|
|
});
|
2012-06-26 05:26:36 -04:00
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_startupMountAll() {
|
2011-06-20 15:16:40 -04:00
|
|
|
let volumes = this._volumeMonitor.get_volumes();
|
2017-10-30 20:38:18 -04:00
|
|
|
volumes.forEach(volume => {
|
2011-06-20 15:16:40 -04:00
|
|
|
this._checkAndMountVolume(volume, { checkSession: false,
|
2012-06-19 14:49:40 -04:00
|
|
|
useMountOp: false,
|
|
|
|
allowAutorun: false });
|
2017-10-30 20:38:18 -04:00
|
|
|
});
|
2011-06-20 15:16:40 -04:00
|
|
|
|
2012-09-02 21:23:50 -04:00
|
|
|
this._mountAllId = 0;
|
2013-11-28 19:45:39 -05:00
|
|
|
return GLib.SOURCE_REMOVE;
|
2011-06-20 15:16:40 -04:00
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onDriveConnected() {
|
2011-06-27 11:51:23 -04:00
|
|
|
// if we're not in the current ConsoleKit session,
|
|
|
|
// or screensaver is active, don't play sounds
|
2013-02-03 16:24:33 -05:00
|
|
|
if (!this._session.SessionIsActive)
|
2012-02-10 22:51:42 -05:00
|
|
|
return;
|
2011-06-27 11:51:23 -04:00
|
|
|
|
2018-12-13 14:30:22 -05:00
|
|
|
let player = global.display.get_sound_player();
|
|
|
|
player.play_from_theme('device-added-media',
|
|
|
|
_("External drive connected"),
|
|
|
|
null);
|
2011-06-27 11:51:23 -04:00
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onDriveDisconnected() {
|
2011-06-27 11:51:23 -04:00
|
|
|
// if we're not in the current ConsoleKit session,
|
|
|
|
// or screensaver is active, don't play sounds
|
2013-02-03 16:24:33 -05:00
|
|
|
if (!this._session.SessionIsActive)
|
2012-02-10 22:51:42 -05:00
|
|
|
return;
|
2011-06-27 11:51:23 -04:00
|
|
|
|
2018-12-13 14:30:22 -05:00
|
|
|
let sound = global.display.get_sound();
|
|
|
|
sound.play_from_theme('device-removed-media',
|
|
|
|
_("External drive disconnected"),
|
|
|
|
null);
|
2011-06-27 11:51:23 -04:00
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onDriveEjectButton(monitor, drive) {
|
2011-06-27 12:13:54 -04:00
|
|
|
// TODO: this code path is not tested, as the GVfs volume monitor
|
|
|
|
// doesn't emit this signal just yet.
|
2013-02-03 16:24:33 -05:00
|
|
|
if (!this._session.SessionIsActive)
|
2011-06-27 12:13:54 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
// we force stop/eject in this case, so we don't have to pass a
|
|
|
|
// mount operation object
|
|
|
|
if (drive.can_stop()) {
|
|
|
|
drive.stop
|
|
|
|
(Gio.MountUnmountFlags.FORCE, null, null,
|
2017-10-30 20:38:18 -04:00
|
|
|
(drive, res) => {
|
2011-06-27 12:13:54 -04:00
|
|
|
try {
|
|
|
|
drive.stop_finish(res);
|
|
|
|
} catch (e) {
|
|
|
|
log("Unable to stop the drive after drive-eject-button " + e.toString());
|
|
|
|
}
|
2017-10-30 20:38:18 -04:00
|
|
|
});
|
2011-06-27 12:13:54 -04:00
|
|
|
} else if (drive.can_eject()) {
|
|
|
|
drive.eject_with_operation
|
|
|
|
(Gio.MountUnmountFlags.FORCE, null, null,
|
2017-10-30 20:38:18 -04:00
|
|
|
(drive, res) => {
|
2011-06-27 12:13:54 -04:00
|
|
|
try {
|
|
|
|
drive.eject_with_operation_finish(res);
|
|
|
|
} catch (e) {
|
|
|
|
log("Unable to eject the drive after drive-eject-button " + e.toString());
|
|
|
|
}
|
2017-10-30 20:38:18 -04:00
|
|
|
});
|
2011-06-27 12:13:54 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onVolumeAdded(monitor, volume) {
|
2011-06-20 15:16:40 -04:00
|
|
|
this._checkAndMountVolume(volume);
|
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_checkAndMountVolume(volume, params) {
|
2011-06-20 15:16:40 -04:00
|
|
|
params = Params.parse(params, { checkSession: true,
|
2012-06-19 14:49:40 -04:00
|
|
|
useMountOp: true,
|
|
|
|
allowAutorun: true });
|
2011-06-20 15:16:40 -04:00
|
|
|
|
|
|
|
if (params.checkSession) {
|
|
|
|
// if we're not in the current ConsoleKit session,
|
|
|
|
// don't attempt automount
|
2013-02-03 16:24:33 -05:00
|
|
|
if (!this._session.SessionIsActive)
|
2011-06-20 15:16:40 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-26 05:26:36 -04:00
|
|
|
if (this._inhibited)
|
|
|
|
return;
|
|
|
|
|
2011-09-28 16:29:45 -04:00
|
|
|
// Volume is already mounted, don't bother.
|
|
|
|
if (volume.get_mount())
|
|
|
|
return;
|
|
|
|
|
2011-06-22 09:45:03 -04:00
|
|
|
if (!this._settings.get_boolean(SETTING_ENABLE_AUTOMOUNT) ||
|
|
|
|
!volume.should_automount() ||
|
|
|
|
!volume.can_mount()) {
|
2011-09-28 16:29:45 -04:00
|
|
|
// allow the autorun to run anyway; this can happen if the
|
2011-06-22 09:45:03 -04:00
|
|
|
// mount gets added programmatically later, even if
|
|
|
|
// should_automount() or can_mount() are false, like for
|
|
|
|
// blank optical media.
|
|
|
|
this._allowAutorun(volume);
|
|
|
|
this._allowAutorunExpire(volume);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-22 16:43:16 -04:00
|
|
|
if (params.useMountOp) {
|
|
|
|
let operation = new ShellMountOperation.ShellMountOperation(volume);
|
2012-06-19 20:22:26 -04:00
|
|
|
this._mountVolume(volume, operation, params.allowAutorun);
|
2011-06-22 16:43:16 -04:00
|
|
|
} else {
|
2012-06-19 14:49:40 -04:00
|
|
|
this._mountVolume(volume, null, params.allowAutorun);
|
2011-06-22 16:43:16 -04:00
|
|
|
}
|
2011-06-20 15:16:40 -04:00
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_mountVolume(volume, operation, allowAutorun) {
|
2012-06-19 14:49:40 -04:00
|
|
|
if (allowAutorun)
|
|
|
|
this._allowAutorun(volume);
|
|
|
|
|
2012-06-19 20:22:26 -04:00
|
|
|
let mountOp = operation ? operation.mountOp : null;
|
2018-10-05 13:09:28 -04:00
|
|
|
this._activeOperations.set(volume, operation);
|
2012-06-19 20:22:26 -04:00
|
|
|
|
|
|
|
volume.mount(0, mountOp, null,
|
2017-12-01 19:27:35 -05:00
|
|
|
this._onVolumeMounted.bind(this));
|
2011-06-20 15:16:40 -04:00
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onVolumeMounted(volume, res) {
|
2011-06-22 09:45:03 -04:00
|
|
|
this._allowAutorunExpire(volume);
|
|
|
|
|
2011-06-20 15:16:40 -04:00
|
|
|
try {
|
|
|
|
volume.mount_finish(res);
|
2012-06-19 20:22:26 -04:00
|
|
|
this._closeOperation(volume);
|
2011-06-20 15:16:40 -04:00
|
|
|
} catch (e) {
|
2012-06-19 20:22:26 -04:00
|
|
|
// FIXME: we will always get G_IO_ERROR_FAILED from the gvfs udisks
|
2018-10-11 13:14:25 -04: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)
|
|
|
|
e.message.includes('Error unlocking')) { // udisks (wrong password)
|
2011-07-12 11:29:49 -04:00
|
|
|
this._reaskPassword(volume);
|
2012-06-19 20:22:26 -04:00
|
|
|
} else {
|
2012-06-20 16:35:29 -04:00
|
|
|
if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.FAILED_HANDLED))
|
|
|
|
log('Unable to mount volume ' + volume.get_name() + ': ' + e.toString());
|
|
|
|
|
2012-06-19 20:22:26 -04:00
|
|
|
this._closeOperation(volume);
|
|
|
|
}
|
2011-06-20 15:16:40 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onVolumeRemoved(monitor, volume) {
|
2018-07-04 10:55:28 -04:00
|
|
|
if (volume._allowAutorunExpireId && volume._allowAutorunExpireId > 0) {
|
|
|
|
Mainloop.source_remove(volume._allowAutorunExpireId);
|
|
|
|
delete volume._allowAutorunExpireId;
|
|
|
|
}
|
2011-06-20 15:16:40 -04:00
|
|
|
this._volumeQueue =
|
2017-10-30 20:38:18 -04:00
|
|
|
this._volumeQueue.filter(element => (element != volume));
|
2011-06-22 09:45:03 -04:00
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_reaskPassword(volume) {
|
2018-10-05 13:09:28 -04:00
|
|
|
let prevOperation = this._activeOperations.get(volume);
|
|
|
|
let existingDialog = prevOperation ? prevOperation.borrowDialog() : null;
|
2012-06-19 20:22:26 -04:00
|
|
|
let operation =
|
|
|
|
new ShellMountOperation.ShellMountOperation(volume,
|
|
|
|
{ existingDialog: existingDialog });
|
|
|
|
this._mountVolume(volume, operation);
|
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_closeOperation(volume) {
|
2018-10-05 13:09:28 -04:00
|
|
|
let operation = this._activeOperations.get(volume);
|
|
|
|
if (!operation)
|
|
|
|
return;
|
|
|
|
operation.close();
|
|
|
|
this._activeOperations.delete(volume);
|
2011-07-12 11:29:49 -04:00
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_allowAutorun(volume) {
|
2011-06-22 09:45:03 -04:00
|
|
|
volume.allowAutorun = true;
|
|
|
|
},
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_allowAutorunExpire(volume) {
|
2017-10-30 20:38:18 -04:00
|
|
|
let id = Mainloop.timeout_add_seconds(AUTORUN_EXPIRE_TIMEOUT_SECS, () => {
|
2011-06-22 09:45:03 -04:00
|
|
|
volume.allowAutorun = false;
|
2018-07-04 10:55:28 -04:00
|
|
|
delete volume._allowAutorunExpireId;
|
2013-11-28 19:45:39 -05:00
|
|
|
return GLib.SOURCE_REMOVE;
|
2011-06-22 09:45:03 -04:00
|
|
|
});
|
2018-07-04 10:55:28 -04:00
|
|
|
volume._allowAutorunExpireId = id;
|
2014-04-10 13:26:52 -04:00
|
|
|
GLib.Source.set_name_by_id(id, '[gnome-shell] volume.allowAutorun');
|
2011-06-20 15:16:40 -04:00
|
|
|
}
|
2011-11-20 12:56:27 -05:00
|
|
|
});
|
2017-07-18 13:47:27 -04:00
|
|
|
var Component = AutomountManager;
|