forked from micheleg/dash-to-dock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
locations.js
265 lines (234 loc) · 9.17 KB
/
locations.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const Gtk = imports.gi.Gtk;
const Shell = imports.gi.Shell;
const Signals = imports.signals;
// Use __ () and N__() for the extension gettext domain, and reuse
// the shell domain with the default _() and N_()
const Gettext = imports.gettext.domain('dashtodock');
const __ = Gettext.gettext;
const N__ = function(e) { return e };
const Me = imports.misc.extensionUtils.getCurrentExtension();
const Utils = Me.imports.utils;
/**
* This class maintains a Shell.App representing the Trash and keeps it
* up-to-date as the trash fills and is emptied over time.
*/
var Trash = class DashToDock_Trash {
constructor() {
this._file = Gio.file_new_for_uri('trash://');
try {
this._monitor = this._file.monitor_directory(0, null);
this._signalId = this._monitor.connect(
'changed',
this._onTrashChange.bind(this));
} catch (e) {
log(`Unable to monitor trash: ${e}`);
}
this._lastEmpty = true;
this._empty = true;
this._onTrashChange();
}
destroy() {
if (this._monitor) {
this._monitor.disconnect(this._signalId);
this._monitor.run_dispose();
}
this._file.run_dispose();
}
_onTrashChange() {
try {
let children = this._file.enumerate_children('*', 0, null);
this._empty = children.next_file(null) == null;
children.close(null);
} catch (e) {
log(`Unable to enumerate trash children: ${e}`);
}
this._ensureApp();
}
_ensureApp() {
if (this._trashApp == null ||
this._lastEmpty != this._empty) {
let trashKeys = new GLib.KeyFile();
trashKeys.set_string('Desktop Entry', 'Name', __('Trash'));
trashKeys.set_string('Desktop Entry', 'Icon',
this._empty ? 'user-trash' : 'user-trash-full');
trashKeys.set_string('Desktop Entry', 'Type', 'Application');
trashKeys.set_string('Desktop Entry', 'Exec', 'gio open trash:///');
trashKeys.set_string('Desktop Entry', 'StartupNotify', 'false');
trashKeys.set_string('Desktop Entry', 'XdtdUri', 'trash:///');
if (!this._empty) {
trashKeys.set_string('Desktop Entry', 'Actions', 'empty-trash;');
trashKeys.set_string('Desktop Action empty-trash', 'Name', __('Empty Trash'));
trashKeys.set_string('Desktop Action empty-trash', 'Exec',
'dbus-send --print-reply --dest=org.gnome.Nautilus /org/gnome/Nautilus org.gnome.Nautilus.FileOperations.EmptyTrash');
}
let trashAppInfo = Gio.DesktopAppInfo.new_from_keyfile(trashKeys);
this._trashApp = new Shell.App({appInfo: trashAppInfo});
this._lastEmpty = this._empty;
this.emit('changed');
}
}
getApp() {
return this._trashApp;
}
}
Signals.addSignalMethods(Trash.prototype);
/**
* This class maintains Shell.App representations for removable devices
* plugged into the system, and keeps the list of Apps up-to-date as
* devices come and go and are mounted and unmounted.
*/
var Removables = class DashToDock_Removables {
constructor() {
this._signalsHandler = new Utils.GlobalSignalsHandler();
this._monitor = Gio.VolumeMonitor.get();
this._volumeApps = []
this._mountApps = []
this._monitor.get_volumes().forEach(
(volume) => {
this._onVolumeAdded(this._monitor, volume);
}
);
this._monitor.get_mounts().forEach(
(mount) => {
this._onMountAdded(this._monitor, mount);
}
);
this._signalsHandler.add([
this._monitor,
'mount-added',
this._onMountAdded.bind(this)
], [
this._monitor,
'mount-removed',
this._onMountRemoved.bind(this)
], [
this._monitor,
'volume-added',
this._onVolumeAdded.bind(this)
], [
this._monitor,
'volume-removed',
this._onVolumeRemoved.bind(this)
]);
}
destroy() {
this._signalsHandler.destroy();
this._monitor.run_dispose();
}
_getWorkingIconName(icon) {
if (icon instanceof Gio.ThemedIcon) {
let iconTheme = Gtk.IconTheme.get_default();
let names = icon.get_names();
for (let i = 0; i < names.length; i++) {
let iconName = names[i];
if (iconTheme.has_icon(iconName)) {
return iconName;
}
}
return '';
} else {
return icon.to_string();
}
}
_onVolumeAdded(monitor, volume) {
if (!volume.can_mount()) {
return;
}
let activationRoot = volume.get_activation_root();
if (!activationRoot) {
// Can't offer to mount a device if we don't know
// where to mount it.
// These devices are usually ejectable so you
// don't normally unmount them anyway.
return;
}
let uri = GLib.uri_unescape_string(activationRoot.get_uri(), null);
let volumeKeys = new GLib.KeyFile();
volumeKeys.set_string('Desktop Entry', 'Name', volume.get_name());
volumeKeys.set_string('Desktop Entry', 'Icon', this._getWorkingIconName(volume.get_icon()));
volumeKeys.set_string('Desktop Entry', 'Type', 'Application');
volumeKeys.set_string('Desktop Entry', 'Exec', 'gio open "' + uri + '"');
volumeKeys.set_string('Desktop Entry', 'StartupNotify', 'false');
volumeKeys.set_string('Desktop Entry', 'Actions', 'mount;');
volumeKeys.set_string('Desktop Action mount', 'Name', __('Mount'));
volumeKeys.set_string('Desktop Action mount', 'Exec', 'gio mount "' + uri + '"');
let volumeAppInfo = Gio.DesktopAppInfo.new_from_keyfile(volumeKeys);
let volumeApp = new Shell.App({appInfo: volumeAppInfo});
this._volumeApps.push(volumeApp);
this.emit('changed');
}
_onVolumeRemoved(monitor, volume) {
for (let i = 0; i < this._volumeApps.length; i++) {
let app = this._volumeApps[i];
if (app.get_name() == volume.get_name()) {
this._volumeApps.splice(i, 1);
}
}
this.emit('changed');
}
_onMountAdded(monitor, mount) {
// Filter out uninteresting mounts
if (!mount.can_eject() && !mount.can_unmount())
return;
if (mount.is_shadowed())
return;
let volume = mount.get_volume();
if (!volume || volume.get_identifier('class') == 'network') {
return;
}
let escapedUri = mount.get_root().get_uri()
let uri = GLib.uri_unescape_string(escapedUri, null);
let mountKeys = new GLib.KeyFile();
mountKeys.set_string('Desktop Entry', 'Name', mount.get_name());
mountKeys.set_string('Desktop Entry', 'Icon',
this._getWorkingIconName(volume.get_icon()));
mountKeys.set_string('Desktop Entry', 'Type', 'Application');
mountKeys.set_string('Desktop Entry', 'Exec', 'gio open "' + uri + '"');
mountKeys.set_string('Desktop Entry', 'StartupNotify', 'false');
mountKeys.set_string('Desktop Entry', 'XdtdUri', escapedUri);
mountKeys.set_string('Desktop Entry', 'Actions', 'unmount;');
if (mount.can_eject()) {
mountKeys.set_string('Desktop Action unmount', 'Name', __('Eject'));
mountKeys.set_string('Desktop Action unmount', 'Exec',
'gio mount -e "' + uri + '"');
} else {
mountKeys.set_string('Desktop Entry', 'Actions', 'unmount;');
mountKeys.set_string('Desktop Action unmount', 'Name', __('Unmount'));
mountKeys.set_string('Desktop Action unmount', 'Exec',
'gio mount -u "' + uri + '"');
}
let mountAppInfo = Gio.DesktopAppInfo.new_from_keyfile(mountKeys);
let mountApp = new Shell.App({appInfo: mountAppInfo});
this._mountApps.push(mountApp);
this.emit('changed');
}
_onMountRemoved(monitor, mount) {
for (let i = 0; i < this._mountApps.length; i++) {
let app = this._mountApps[i];
if (app.get_name() == mount.get_name()) {
this._mountApps.splice(i, 1);
}
}
this.emit('changed');
}
getApps() {
// When we have both a volume app and a mount app, we prefer
// the mount app.
let apps = new Map();
this._volumeApps.map(function(app) {
apps.set(app.get_name(), app);
});
this._mountApps.map(function(app) {
apps.set(app.get_name(), app);
});
let ret = [];
for (let app of apps.values()) {
ret.push(app);
}
return ret;
}
}
Signals.addSignalMethods(Removables.prototype);