2011-09-28 13:16:26 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2011-06-20 19:16:40 +00:00
|
|
|
|
|
|
|
const Lang = imports.lang;
|
|
|
|
const Mainloop = imports.mainloop;
|
2012-02-11 03:51:42 +00:00
|
|
|
const GLib = imports.gi.GLib;
|
2011-06-20 19:16:40 +00:00
|
|
|
const Gio = imports.gi.Gio;
|
|
|
|
const Params = imports.misc.params;
|
2012-02-11 03:51:42 +00:00
|
|
|
const Shell = imports.gi.Shell;
|
2012-08-17 12:18:53 +00:00
|
|
|
|
|
|
|
const GnomeSession = imports.misc.gnomeSession;
|
2012-08-18 12:05:52 +00:00
|
|
|
const LoginManager = imports.misc.loginManager;
|
2011-06-20 19:16:40 +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
|
|
|
|
|
|
|
const 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';
|
|
|
|
|
2011-06-22 13:45:03 +00:00
|
|
|
const AUTORUN_EXPIRE_TIMEOUT_SECS = 10;
|
|
|
|
|
2011-11-20 17:56:27 +00:00
|
|
|
const AutomountManager = new Lang.Class({
|
|
|
|
Name: 'AutomountManager',
|
2011-06-20 19:16:40 +00:00
|
|
|
|
|
|
|
_init: function() {
|
|
|
|
this._settings = new Gio.Settings({ schema: SETTINGS_SCHEMA });
|
|
|
|
this._volumeQueue = [];
|
2012-06-26 09:26:36 +00:00
|
|
|
this._session = new GnomeSession.SessionManager();
|
|
|
|
this._session.connectSignal('InhibitorAdded',
|
|
|
|
Lang.bind(this, this._InhibitorsChanged));
|
|
|
|
this._session.connectSignal('InhibitorRemoved',
|
|
|
|
Lang.bind(this, this._InhibitorsChanged));
|
|
|
|
this._inhibited = false;
|
2011-06-20 19:16:40 +00:00
|
|
|
|
2012-08-18 12:05:52 +00:00
|
|
|
this._loginManager = LoginManager.getLoginManager();
|
2012-09-03 01:23:50 +00:00
|
|
|
this._volumeMonitor = Gio.VolumeMonitor.get();
|
|
|
|
},
|
2011-06-20 19:16:40 +00:00
|
|
|
|
2012-09-03 01:23:50 +00:00
|
|
|
enable: function() {
|
|
|
|
this._volumeAddedId = this._volumeMonitor.connect('volume-added', Lang.bind(this, this._onVolumeAdded));
|
|
|
|
this._volumeRemovedId = this._volumeMonitor.connect('volume-removed', Lang.bind(this, this._onVolumeRemoved));
|
|
|
|
this._driveConnectedId = this._volumeMonitor.connect('drive-connected', Lang.bind(this, this._onDriveConnected));
|
|
|
|
this._driveDisconnectedId = this._volumeMonitor.connect('drive-disconnected', Lang.bind(this, this._onDriveDisconnected));
|
|
|
|
this._driveEjectButtonId = this._volumeMonitor.connect('drive-eject-button', Lang.bind(this, this._onDriveEjectButton));
|
2011-06-20 19:16:40 +00:00
|
|
|
|
2012-09-03 01:23:50 +00:00
|
|
|
this._mountAllId = Mainloop.idle_add(Lang.bind(this, this._startupMountAll));
|
|
|
|
},
|
|
|
|
|
|
|
|
disable: function() {
|
|
|
|
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) {
|
|
|
|
Mainloop.source_remove(this._mountAllId);
|
|
|
|
this._mountAllId = 0;
|
|
|
|
}
|
2011-06-20 19:16:40 +00:00
|
|
|
},
|
|
|
|
|
2012-06-26 09:26:36 +00:00
|
|
|
_InhibitorsChanged: function(object, senderName, [inhibtor]) {
|
|
|
|
this._session.IsInhibitedRemote(GNOME_SESSION_AUTOMOUNT_INHIBIT,
|
|
|
|
Lang.bind(this,
|
|
|
|
function(result, error) {
|
|
|
|
if (!error) {
|
|
|
|
this._inhibited = result[0];
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
2011-06-20 19:16:40 +00:00
|
|
|
_startupMountAll: function() {
|
|
|
|
let volumes = this._volumeMonitor.get_volumes();
|
|
|
|
volumes.forEach(Lang.bind(this, function(volume) {
|
|
|
|
this._checkAndMountVolume(volume, { checkSession: false,
|
2012-06-19 18:49:40 +00:00
|
|
|
useMountOp: false,
|
|
|
|
allowAutorun: false });
|
2011-06-20 19:16:40 +00:00
|
|
|
}));
|
|
|
|
|
2012-09-03 01:23:50 +00:00
|
|
|
this._mountAllId = 0;
|
2011-06-20 19:16:40 +00:00
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2011-06-27 15:51:23 +00:00
|
|
|
_onDriveConnected: function() {
|
|
|
|
// if we're not in the current ConsoleKit session,
|
|
|
|
// or screensaver is active, don't play sounds
|
2012-08-18 12:05:52 +00:00
|
|
|
if (!this._loginManager.sessionActive)
|
2012-02-11 03:51:42 +00:00
|
|
|
return;
|
2011-06-27 15:51:23 +00:00
|
|
|
|
|
|
|
global.play_theme_sound(0, 'device-added-media');
|
|
|
|
},
|
|
|
|
|
|
|
|
_onDriveDisconnected: function() {
|
|
|
|
// if we're not in the current ConsoleKit session,
|
|
|
|
// or screensaver is active, don't play sounds
|
2012-08-18 12:05:52 +00:00
|
|
|
if (!this._loginManager.sessionActive)
|
2012-02-11 03:51:42 +00:00
|
|
|
return;
|
2011-06-27 15:51:23 +00:00
|
|
|
|
|
|
|
global.play_theme_sound(0, 'device-removed-media');
|
|
|
|
},
|
|
|
|
|
2011-06-27 16:13:54 +00:00
|
|
|
_onDriveEjectButton: function(monitor, drive) {
|
|
|
|
// TODO: this code path is not tested, as the GVfs volume monitor
|
|
|
|
// doesn't emit this signal just yet.
|
2012-08-18 12:05:52 +00:00
|
|
|
if (!this._loginManager.sessionActive)
|
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()) {
|
|
|
|
drive.stop
|
|
|
|
(Gio.MountUnmountFlags.FORCE, null, null,
|
|
|
|
Lang.bind(this, function(drive, res) {
|
|
|
|
try {
|
|
|
|
drive.stop_finish(res);
|
|
|
|
} catch (e) {
|
|
|
|
log("Unable to stop the drive after drive-eject-button " + e.toString());
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
} else if (drive.can_eject()) {
|
|
|
|
drive.eject_with_operation
|
|
|
|
(Gio.MountUnmountFlags.FORCE, null, null,
|
|
|
|
Lang.bind(this, function(drive, res) {
|
|
|
|
try {
|
|
|
|
drive.eject_with_operation_finish(res);
|
|
|
|
} catch (e) {
|
|
|
|
log("Unable to eject the drive after drive-eject-button " + e.toString());
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-06-20 19:16:40 +00:00
|
|
|
_onVolumeAdded: function(monitor, volume) {
|
|
|
|
this._checkAndMountVolume(volume);
|
|
|
|
},
|
|
|
|
|
|
|
|
_checkAndMountVolume: function(volume, params) {
|
|
|
|
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
|
2012-08-18 12:05:52 +00:00
|
|
|
if (!this._loginManager.sessionActive)
|
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
|
2011-06-22 13:45:03 +00: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 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
|
|
|
}
|
2011-06-20 19:16:40 +00:00
|
|
|
},
|
|
|
|
|
2012-06-19 18:49:40 +00:00
|
|
|
_mountVolume: function(volume, operation, allowAutorun) {
|
|
|
|
if (allowAutorun)
|
|
|
|
this._allowAutorun(volume);
|
|
|
|
|
2012-06-20 00:22:26 +00:00
|
|
|
let mountOp = operation ? operation.mountOp : null;
|
|
|
|
volume._operation = operation;
|
|
|
|
|
|
|
|
volume.mount(0, mountOp, null,
|
2011-06-20 19:16:40 +00:00
|
|
|
Lang.bind(this, this._onVolumeMounted));
|
|
|
|
},
|
|
|
|
|
|
|
|
_onVolumeMounted: function(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
|
|
|
|
// backend in this case, see
|
|
|
|
// https://bugs.freedesktop.org/show_bug.cgi?id=51271
|
|
|
|
if (e.message.indexOf('No key available with this passphrase') != -1) {
|
2011-07-12 15:29:49 +00:00
|
|
|
this._reaskPassword(volume);
|
2012-06-20 00:22:26 +00:00
|
|
|
} else {
|
2012-06-20 20:35:29 +00:00
|
|
|
if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.FAILED_HANDLED))
|
|
|
|
log('Unable to mount volume ' + volume.get_name() + ': ' + e.toString());
|
|
|
|
|
2012-06-20 00:22:26 +00:00
|
|
|
this._closeOperation(volume);
|
|
|
|
}
|
2011-06-20 19:16:40 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_onVolumeRemoved: function(monitor, volume) {
|
|
|
|
this._volumeQueue =
|
|
|
|
this._volumeQueue.filter(function(element) {
|
|
|
|
return (element != volume);
|
|
|
|
});
|
2011-06-22 13:45:03 +00:00
|
|
|
},
|
|
|
|
|
2011-07-12 15:29:49 +00:00
|
|
|
_reaskPassword: function(volume) {
|
2012-06-20 00:22:26 +00:00
|
|
|
let existingDialog = volume._operation ? volume._operation.borrowDialog() : null;
|
|
|
|
let operation =
|
|
|
|
new ShellMountOperation.ShellMountOperation(volume,
|
|
|
|
{ existingDialog: existingDialog });
|
|
|
|
this._mountVolume(volume, operation);
|
|
|
|
},
|
|
|
|
|
|
|
|
_closeOperation: function(volume) {
|
|
|
|
if (volume._operation)
|
|
|
|
volume._operation.close();
|
2011-07-12 15:29:49 +00:00
|
|
|
},
|
|
|
|
|
2011-06-22 13:45:03 +00:00
|
|
|
_allowAutorun: function(volume) {
|
|
|
|
volume.allowAutorun = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
_allowAutorunExpire: function(volume) {
|
|
|
|
Mainloop.timeout_add_seconds(AUTORUN_EXPIRE_TIMEOUT_SECS, function() {
|
|
|
|
volume.allowAutorun = false;
|
|
|
|
return false;
|
|
|
|
});
|
2011-06-20 19:16:40 +00:00
|
|
|
}
|
2011-11-20 17:56:27 +00:00
|
|
|
});
|
2012-09-03 01:23:50 +00:00
|
|
|
const Component = AutomountManager;
|