|
| 1 | +// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*- |
| 2 | +/* exported Component */ |
| 3 | + |
| 4 | +const { Gio, GLib } = imports.gi; |
| 5 | +const Params = imports.misc.params; |
| 6 | + |
| 7 | +const LoginManager = imports.misc.loginManager; |
| 8 | +const Main = imports.ui.main; |
| 9 | +const CinnamonMountOperation = imports.ui.cinnamonMountOperation; |
| 10 | + |
| 11 | +// GSettings keys |
| 12 | +const SETTINGS_SCHEMA = 'org.cinnamon.desktop.media-handling'; |
| 13 | +const SETTING_ENABLE_AUTOMOUNT = 'automount'; |
| 14 | + |
| 15 | +var AUTORUN_EXPIRE_TIMEOUT_SECS = 10; |
| 16 | + |
| 17 | +var AutomountManager = class { |
| 18 | + constructor() { |
| 19 | + this._settings = new Gio.Settings({ schema_id: SETTINGS_SCHEMA }); |
| 20 | + this._activeOperations = new Map(); |
| 21 | + this._volumeQueue = []; |
| 22 | + |
| 23 | + this._loginManager = LoginManager.getLoginManager(); |
| 24 | + this._loginManager.connect('active-changed', (lm, active) => { |
| 25 | + if (active) |
| 26 | + this._drainVolumeQueue(); |
| 27 | + }); |
| 28 | + |
| 29 | + Main.screensaverController.connect('locked-changed', (ctrl, locked) => { |
| 30 | + if (!locked) |
| 31 | + this._drainVolumeQueue(); |
| 32 | + }); |
| 33 | + |
| 34 | + this._volumeMonitor = Gio.VolumeMonitor.get(); |
| 35 | + this.enable(); |
| 36 | + } |
| 37 | + |
| 38 | + enable() { |
| 39 | + this._volumeMonitor.connectObject( |
| 40 | + 'volume-added', this._onVolumeAdded.bind(this), |
| 41 | + 'volume-removed', this._onVolumeRemoved.bind(this), |
| 42 | + 'drive-connected', this._onDriveConnected.bind(this), |
| 43 | + 'drive-disconnected', this._onDriveDisconnected.bind(this), |
| 44 | + 'drive-eject-button', this._onDriveEjectButton.bind(this), this); |
| 45 | + |
| 46 | + this._mountAllId = GLib.idle_add(GLib.PRIORITY_DEFAULT, this._startupMountAll.bind(this)); |
| 47 | + GLib.Source.set_name_by_id(this._mountAllId, '[cinnamon] this._startupMountAll'); |
| 48 | + } |
| 49 | + |
| 50 | + disable() { |
| 51 | + this._volumeMonitor.disconnectObject(this); |
| 52 | + |
| 53 | + if (this._mountAllId > 0) { |
| 54 | + GLib.source_remove(this._mountAllId); |
| 55 | + this._mountAllId = 0; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + _drainVolumeQueue() { |
| 60 | + while (this._volumeQueue.length > 0) { |
| 61 | + let volume = this._volumeQueue.shift(); |
| 62 | + this._checkAndMountVolume(volume, { |
| 63 | + checkSession: false, |
| 64 | + }); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + _startupMountAll() { |
| 69 | + let volumes = this._volumeMonitor.get_volumes(); |
| 70 | + volumes.forEach(volume => { |
| 71 | + this._checkAndMountVolume(volume, { |
| 72 | + checkSession: false, |
| 73 | + useMountOp: false, |
| 74 | + allowAutorun: false, |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + this._mountAllId = 0; |
| 79 | + return GLib.SOURCE_REMOVE; |
| 80 | + } |
| 81 | + |
| 82 | + _onDriveConnected() { |
| 83 | + if (!this._loginManager.sessionIsActive) |
| 84 | + return; |
| 85 | + |
| 86 | + let player = global.display.get_sound_player(); |
| 87 | + player.play_from_theme('device-added-media', |
| 88 | + _("External drive connected"), |
| 89 | + null); |
| 90 | + } |
| 91 | + |
| 92 | + _onDriveDisconnected() { |
| 93 | + if (!this._loginManager.sessionIsActive) |
| 94 | + return; |
| 95 | + |
| 96 | + let player = global.display.get_sound_player(); |
| 97 | + player.play_from_theme('device-removed-media', |
| 98 | + _("External drive disconnected"), |
| 99 | + null); |
| 100 | + } |
| 101 | + |
| 102 | + _onDriveEjectButton(monitor, drive) { |
| 103 | + if (!this._loginManager.sessionIsActive) |
| 104 | + return; |
| 105 | + |
| 106 | + if (drive.can_stop()) { |
| 107 | + drive.stop(Gio.MountUnmountFlags.FORCE, null, null, |
| 108 | + (o, res) => { |
| 109 | + try { |
| 110 | + drive.stop_finish(res); |
| 111 | + } catch (e) { |
| 112 | + log(`Unable to stop the drive after drive-eject-button ${e.toString()}`); |
| 113 | + } |
| 114 | + }); |
| 115 | + } else if (drive.can_eject()) { |
| 116 | + drive.eject_with_operation(Gio.MountUnmountFlags.FORCE, null, null, |
| 117 | + (o, res) => { |
| 118 | + try { |
| 119 | + drive.eject_with_operation_finish(res); |
| 120 | + } catch (e) { |
| 121 | + log(`Unable to eject the drive after drive-eject-button ${e.toString()}`); |
| 122 | + } |
| 123 | + }); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + _onVolumeAdded(monitor, volume) { |
| 128 | + this._checkAndMountVolume(volume); |
| 129 | + } |
| 130 | + |
| 131 | + _checkAndMountVolume(volume, params) { |
| 132 | + params = Params.parse(params, { |
| 133 | + checkSession: true, |
| 134 | + useMountOp: true, |
| 135 | + allowAutorun: true, |
| 136 | + }); |
| 137 | + |
| 138 | + if (params.checkSession) { |
| 139 | + if (!this._loginManager.sessionIsActive) |
| 140 | + return; |
| 141 | + |
| 142 | + if (Main.screensaverController.locked) { |
| 143 | + this._volumeQueue.push(volume); |
| 144 | + return; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + if (volume.get_mount()) |
| 149 | + return; |
| 150 | + |
| 151 | + if (!this._settings.get_boolean(SETTING_ENABLE_AUTOMOUNT) || |
| 152 | + !volume.should_automount() || |
| 153 | + !volume.can_mount()) { |
| 154 | + this._allowAutorun(volume); |
| 155 | + this._allowAutorunExpire(volume); |
| 156 | + |
| 157 | + return; |
| 158 | + } |
| 159 | + |
| 160 | + if (params.useMountOp) { |
| 161 | + let operation = new CinnamonMountOperation.CinnamonMountOperation(volume); |
| 162 | + this._mountVolume(volume, operation, params.allowAutorun); |
| 163 | + } else { |
| 164 | + this._mountVolume(volume, null, params.allowAutorun); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + _mountVolume(volume, operation, allowAutorun) { |
| 169 | + if (allowAutorun) |
| 170 | + this._allowAutorun(volume); |
| 171 | + |
| 172 | + const mountOp = operation?.mountOp ?? null; |
| 173 | + this._activeOperations.set(volume, operation); |
| 174 | + |
| 175 | + volume.mount(0, mountOp, null, |
| 176 | + this._onVolumeMounted.bind(this)); |
| 177 | + } |
| 178 | + |
| 179 | + _onVolumeMounted(volume, res) { |
| 180 | + this._allowAutorunExpire(volume); |
| 181 | + |
| 182 | + try { |
| 183 | + volume.mount_finish(res); |
| 184 | + this._closeOperation(volume); |
| 185 | + } catch (e) { |
| 186 | + // Errors here do not have any specific codes we can parse, but the error message |
| 187 | + // comes from udisks and will not be translated, so should be reliable (used this way |
| 188 | + // in other projects as well). |
| 189 | + if (e.message.includes('No key available with this passphrase') || |
| 190 | + e.message.includes('No key available to unlock device') || |
| 191 | + e.message.includes('Failed to activate device: Incorrect passphrase') || |
| 192 | + e.message.includes('Failed to load device\'s parameters: Invalid argument')) { |
| 193 | + this._reaskPassword(volume); |
| 194 | + } else { |
| 195 | + if (e.message.includes('Compiled against a version of libcryptsetup that does not support the VeraCrypt PIM setting')) { |
| 196 | + Main.notifyError(_("Unable to unlock volume"), |
| 197 | + _("The installed udisks version does not support the PIM setting")); |
| 198 | + } |
| 199 | + |
| 200 | + if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.FAILED_HANDLED)) |
| 201 | + log(`Unable to mount volume ${volume.get_name()}: ${e.toString()}`); |
| 202 | + this._closeOperation(volume); |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + _onVolumeRemoved(monitor, volume) { |
| 208 | + if (volume._allowAutorunExpireId && volume._allowAutorunExpireId > 0) { |
| 209 | + GLib.source_remove(volume._allowAutorunExpireId); |
| 210 | + delete volume._allowAutorunExpireId; |
| 211 | + } |
| 212 | + |
| 213 | + this._volumeQueue = this._volumeQueue.filter(v => v !== volume); |
| 214 | + } |
| 215 | + |
| 216 | + _reaskPassword(volume) { |
| 217 | + let prevOperation = this._activeOperations.get(volume); |
| 218 | + const existingDialog = prevOperation?.borrowDialog(); |
| 219 | + let operation = |
| 220 | + new CinnamonMountOperation.CinnamonMountOperation(volume, { existingDialog }); |
| 221 | + this._mountVolume(volume, operation); |
| 222 | + } |
| 223 | + |
| 224 | + _closeOperation(volume) { |
| 225 | + let operation = this._activeOperations.get(volume); |
| 226 | + if (!operation) |
| 227 | + return; |
| 228 | + operation.close(); |
| 229 | + this._activeOperations.delete(volume); |
| 230 | + } |
| 231 | + |
| 232 | + _allowAutorun(volume) { |
| 233 | + volume.allowAutorun = true; |
| 234 | + } |
| 235 | + |
| 236 | + _allowAutorunExpire(volume) { |
| 237 | + if (volume._allowAutorunExpireId && volume._allowAutorunExpireId > 0) { |
| 238 | + GLib.source_remove(volume._allowAutorunExpireId); |
| 239 | + delete volume._allowAutorunExpireId; |
| 240 | + } |
| 241 | + |
| 242 | + let id = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, AUTORUN_EXPIRE_TIMEOUT_SECS, () => { |
| 243 | + volume.allowAutorun = false; |
| 244 | + delete volume._allowAutorunExpireId; |
| 245 | + return GLib.SOURCE_REMOVE; |
| 246 | + }); |
| 247 | + volume._allowAutorunExpireId = id; |
| 248 | + GLib.Source.set_name_by_id(id, '[cinnamon] volume.allowAutorun'); |
| 249 | + } |
| 250 | +}; |
0 commit comments