Debian Codegraph @

extensions/drive-menu/extension.js

raw
1 // SPDX-FileCopyrightText: 2011 Giovanni Campagna <[email protected]>
2 // SPDX-FileCopyrightText: 2018 Florian Müllner <[email protected]>
3 //
4 // SPDX-License-Identifier: GPL-2.0-or-later
5
6 // Drive menu extension
7 import Clutter from 'gi://Clutter';
8 import Gio from 'gi://Gio';
9 import GObject from 'gi://GObject';
10 import Shell from 'gi://Shell';
11 import St from 'gi://St';
12
13 import {Extension, gettext as _} from 'resource:///org/gnome/shell/extensions/extension.js';
14
15 import * as Main from 'resource:///org/gnome/shell/ui/main.js';
16 import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js';
17 import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js';
18 import * as ShellMountOperation from 'resource:///org/gnome/shell/ui/shellMountOperation.js';
19
20 Gio._promisify(Gio.File.prototype, 'query_filesystem_info_async');
21
22 class MountMenuItem extends PopupMenu.PopupBaseMenuItem {
23 static {
24 GObject.registerClass(this);
25 }
26
27 constructor(mount) {
28 super({
29 style_class: 'drive-menu-item',
30 });
31
32 this.label = new St.Label({
33 text: mount.get_name(),
34 x_expand: true,
35 y_align: Clutter.ActorAlign.CENTER,
36 });
37 this.add_child(this.label);
38 this.label_actor = this.label;
39
40 this.mount = mount;
41
42 const ejectIcon = new St.Icon({
43 icon_name: 'media-eject-symbolic',
44 style_class: 'popup-menu-icon',
45 });
46 const ejectButton = new St.Button({
47 child: ejectIcon,
48 style_class: 'button',
49 });
50 ejectButton.connect('clicked', this._eject.bind(this));
51 this.add_child(ejectButton);
52
53 this.hide();
54
55 mount.connectObject('changed',
56 () => this._syncVisibility(), this);
57 this._syncVisibility();
58 }
59
60 async _isInteresting() {
61 if (!this.mount.can_eject() && !this.mount.can_unmount())
62 return false;
63 if (this.mount.is_shadowed())
64 return false;
65
66 const volume = this.mount.get_volume();
67
68 if (volume)
69 return volume.get_identifier('class') !== 'network';
70
71 const root = this.mount.get_root();
72
73 try {
74 const attr = Gio.FILE_ATTRIBUTE_FILESYSTEM_REMOTE;
75 const info = await root.query_filesystem_info_async(attr, null);
76 return !info.get_attribute_boolean(attr);
77 } catch (e) {
78 log(`Failed to query filesystem: ${e.message}`);
79 }
80
81 // Hack, fall back to looking at GType
82 return Gio._LocalFilePrototype.isPrototypeOf(root);
83 }
84
85 async _syncVisibility() {
86 this.visible = await this._isInteresting();
87 }
88
89 _eject() {
90 const unmountArgs = [
91 Gio.MountUnmountFlags.NONE,
92 new ShellMountOperation.ShellMountOperation(this.mount).mountOp,
93 null, // Gio.Cancellable
94 ];
95
96 if (this.mount.can_eject()) {
97 this.mount.eject_with_operation(...unmountArgs,
98 this._ejectFinish.bind(this));
99 } else {
100 this.mount.unmount_with_operation(...unmountArgs,
101 this._unmountFinish.bind(this));
102 }
103 }
104
105 _unmountFinish(mount, result) {
106 try {
107 mount.unmount_with_operation_finish(result);
108 } catch (e) {
109 this._reportFailure(e);
110 }
111 }
112
113 _ejectFinish(mount, result) {
114 try {
115 mount.eject_with_operation_finish(result);
116 } catch (e) {
117 this._reportFailure(e);
118 }
119 }
120
121 _reportFailure(exception) {
122 // TRANSLATORS: %s is the filesystem name
123 const msg = _('Ejecting drive “%s” failed:').format(this.mount.get_name());
124 Main.notifyError(msg, exception.message);
125 }
126
127 activate(event) {
128 const uri = this.mount.get_root().get_uri();
129 const context = global.create_app_launch_context(event.get_time(), -1);
130 Gio.AppInfo.launch_default_for_uri(uri, context);
131
132 super.activate(event);
133 }
134 }
135
136 class DriveMenu extends PanelMenu.Button {
137 static {
138 GObject.registerClass(this);
139 }
140
141 constructor() {
142 super(0.5, _('Removable devices'));
143
144 const icon = new St.Icon({
145 icon_name: 'media-eject-symbolic',
146 style_class: 'system-status-icon',
147 });
148
149 this.add_child(icon);
150
151 this._monitor = Gio.VolumeMonitor.get();
152 this._monitor.connectObject(
153 'mount-added', (monitor, mount) => this._addMount(mount),
154 'mount-removed', (monitor, mount) => {
155 this._removeMount(mount);
156 this._updateMenuVisibility();
157 }, this);
158
159 this._mounts = [];
160
161 this._monitor.get_mounts().forEach(this._addMount.bind(this));
162
163 this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
164 this.menu.addAction(_('Open Files'), event => {
165 const appSystem = Shell.AppSystem.get_default();
166 const app = appSystem.lookup_app('org.gnome.Nautilus.desktop');
167 app.activate_full(-1, event.get_time());
168 });
169
170 this._updateMenuVisibility();
171 }
172
173 _updateMenuVisibility() {
174 if (this._mounts.filter(i => i.visible).length > 0)
175 this.show();
176 else
177 this.hide();
178 }
179
180 _addMount(mount) {
181 const item = new MountMenuItem(mount);
182 this._mounts.unshift(item);
183 this.menu.addMenuItem(item, 0);
184
185 item.connect('notify::visible', () => this._updateMenuVisibility());
186 }
187
188 _removeMount(mount) {
189 for (let i = 0; i < this._mounts.length; i++) {
190 const item = this._mounts[i];
191 if (item.mount === mount) {
192 item.destroy();
193 this._mounts.splice(i, 1);
194 return;
195 }
196 }
197 log('Removing a mount that was never added to the menu');
198 }
199 }
200
201 export default class PlaceMenuExtension extends Extension {
202 enable() {
203 this._indicator = new DriveMenu();
204 Main.panel.addToStatusArea('drive-menu', this._indicator);
205 }
206
207 disable() {
208 this._indicator.destroy();
209 this._indicator = null;
210 }
211 }