forked from The-Panacea-Projects/Gnomenu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
placeDisplay.js
408 lines (339 loc) · 14.3 KB
/
placeDisplay.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/* ========================================================================================================
* placeDisplay.js - Places Manager for Gnome Shell 3.6
* --------------------------------------------------------------------------------------------------------
* CREDITS: This code was copied from the Places Status Indicator extension and modified to provide the
* functions needed by GnoMenu. Many thanks to gcampax for a great extension.
* ========================================================================================================
*/
const _DEBUG_ = false;
const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
const Shell = imports.gi.Shell;
const Lang = imports.lang;
const Mainloop = imports.mainloop;
const Signals = imports.signals;
const St = imports.gi.St;
const DND = imports.ui.dnd;
const Main = imports.ui.main;
const Params = imports.misc.params;
const Search = imports.ui.search;
const Util = imports.misc.util;
const Gettext = imports.gettext.domain('gnomenu');
const _ = Gettext.gettext;
const N_ = function(x) { return x; }
let UseSymbolicIcons = false;
var PlaceInfo = class GnoMenu_PlaceInfo {
constructor() {
this._init.apply(this, arguments);
}
_init(kind, file, name, icon) {
this.kind = kind;
this.file = file;
if (_DEBUG_) global.log("PlacesInfo: _init - kind = "+kind);
if (_DEBUG_) global.log("PlacesInfo: _init - file = "+file);
//this.name = name || this._getFileName();
this.name = name ? name : this._getFileName();
if (_DEBUG_) global.log("PlacesInfo: _init - name = "+this.name);
this.icon = icon ? new Gio.ThemedIcon({ name: icon }) : this.getIcon();
if (_DEBUG_) global.log("PlacesInfo: _init - icon = "+this.icon);
}
isRemovable() {
return false;
}
launch(timestamp) {
//let time = global.get_current_time();
let launchContext = global.create_app_launch_context(0, -1);
launchContext.set_timestamp(timestamp);
try {
Gio.AppInfo.launch_default_for_uri(this.file.get_uri(),
launchContext);
} catch(e) {
if (e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.NOT_MOUNTED)) {
this.file.mount_enclosing_volume(0, null, null, function(file, result) {
file.mount_enclosing_volume_finish(result);
Gio.AppInfo.launch_default_for_uri(file.get_uri(), launchContext);
});
} else {
Main.notifyError(_("Failed to launch \"%s\"").format(this.name), e.message);
}
}
}
getIcon() {
if (_DEBUG_) global.log("PlacesInfo: getIcon");
try {
let info;
if (UseSymbolicIcons) {
info = this.file.query_info('standard::symbolic-icon', 0, null);
return info.get_symbolic_icon();
} else {
info = this.file.query_info("standard::icon", 0, null);
return info.get_icon();
}
} catch(e) {
if (e instanceof Gio.IOErrorEnum) {
// return a generic icon for this kind
if (_DEBUG_) global.log("PlacesInfo: getIcon error - returning generic icon");
switch (this.kind) {
case 'network':
return new Gio.ThemedIcon({ name: 'folder-remote-symbolic' });
case 'devices':
return new Gio.ThemedIcon({ name: 'drive-harddisk-symbolic' });
case 'special':
case 'bookmarks':
default:
if (!this.file.is_native())
return new Gio.ThemedIcon({ name: 'folder-remote-symbolic' });
else
return new Gio.ThemedIcon({ name: 'folder-symbolic' });
}
}
}
}
_getFileName() {
if (_DEBUG_) global.log("PlacesInfo: _getFileName");
try {
let info = this.file.query_info('standard::display-name', 0, null);
return info.get_display_name();
} catch(e) {
if (e instanceof Gio.IOErrorEnum) {
if (_DEBUG_) global.log("PlacesInfo: _getFileName error - returning basename of file");
return this.file.get_basename();
}
}
}
};
var PlaceDeviceInfo = class GnoMenu_PlaceDeviceInfo extends PlaceInfo {
_init(kind, mount) {
this._mount = mount;
super._init(kind, mount.get_root(), mount.get_name());
}
getIcon() {
if (UseSymbolicIcons)
return this._mount.get_symbolic_icon();
else
return this._mount.get_icon();
}
};
const DEFAULT_DIRECTORIES = [
GLib.UserDirectory.DIRECTORY_DOCUMENTS,
GLib.UserDirectory.DIRECTORY_DOWNLOAD,
GLib.UserDirectory.DIRECTORY_MUSIC,
GLib.UserDirectory.DIRECTORY_PICTURES,
GLib.UserDirectory.DIRECTORY_VIDEOS
];
var PlacesManager = class GnoMenu_PlacesManager {
constructor(useSymbolic) {
UseSymbolicIcons = useSymbolic;
this._places = {
special: [],
devices: [],
bookmarks: [],
network: [],
};
let homePath = GLib.get_home_dir();
if (_DEBUG_) global.log("PlacesManager: _init - homePath found at "+homePath);
this._places.special.push(new PlaceInfo('special',
Gio.File.new_for_path(homePath),
_("Home")));
if (_DEBUG_) global.log("PlacesManager: _init - homePath added to special places as Home");
for (let i = 0; i < DEFAULT_DIRECTORIES.length; i++) {
let specialPath = GLib.get_user_special_dir(DEFAULT_DIRECTORIES[i]);
if (specialPath) {
if (specialPath == homePath)
continue;
this._places.special.push(new PlaceInfo('special',
Gio.File.new_for_path(specialPath)));
}
}
if (_DEBUG_) global.log("PlacesManager: _init - default directories added to special places");
/*
* Show devices, code more or less ported from nautilus-places-sidebar.c
*/
this._volumeMonitor = Gio.VolumeMonitor.get();
this._connectVolumeMonitorSignals();
if (_DEBUG_) global.log("PlacesManager: _init - volume monitor and signals connected");
this._updateMounts();
this._bookmarksFile = this._findBookmarksFile(); // Passingthru67 - Added missing semi-colon
if (_DEBUG_) global.log("PlacesManager: _init - bookmarksFile found at "+this._bookmarksFile);
this._bookmarkTimeoutId = 0;
this._monitor = null;
if (this._bookmarksFile) {
this._monitor = this._bookmarksFile.monitor_file(Gio.FileMonitorFlags.NONE, null);
this._monitor.connect('changed', () => {
if (this._bookmarkTimeoutId > 0)
return;
/* Defensive event compression */
this._bookmarkTimeoutId = Mainloop.timeout_add(100, () => {
this._bookmarkTimeoutId = 0;
this._reloadBookmarks();
return false;
});
});
if (_DEBUG_) global.log("PlacesManager: _init - bookmarksFile signal connected");
this._reloadBookmarks();
if (_DEBUG_) global.log("PlacesManager: _init - bookmarks reloaded");
}
}
_connectVolumeMonitorSignals() {
if (_DEBUG_) global.log("PlacesManager: _connectVolumeMonitorSignals");
const signals = ['volume-added', 'volume-removed', 'volume-changed',
'mount-added', 'mount-removed', 'mount-changed',
'drive-connected', 'drive-disconnected', 'drive-changed'];
this._volumeMonitorSignals = [];
let func = this._updateMounts.bind(this);
for (let i = 0; i < signals.length; i++) {
let id = this._volumeMonitor.connect(signals[i], func);
this._volumeMonitorSignals.push(id);
}
}
destroy() {
for (let i = 0; i < this._volumeMonitorSignals.length; i++)
this._volumeMonitor.disconnect(this._volumeMonitorSignals[i]);
if (this._monitor)
this._monitor.cancel();
if (this._bookmarkTimeoutId)
Mainloop.source_remove(this._bookmarkTimeoutId);
}
_updateMounts() {
if (_DEBUG_) global.log("PlacesManager: _updateMounts");
this._places.devices = [];
this._places.network = [];
/* Add standard places */
let symbolic = "";
if (UseSymbolicIcons) {
symbolic = "-symbolic";
}
this._places.devices.push(new PlaceInfo('devices',
Gio.File.new_for_path('/'),
_("Computer"),
'drive-harddisk'+symbolic));
this._places.network.push(new PlaceInfo('network',
Gio.File.new_for_uri('network:///'),
_("Browse network"),
'network-workgroup'+symbolic));
if (_DEBUG_) global.log("PlacesManager: _updateMounts - standard places added");
/* first go through all connected drives */
let drives = this._volumeMonitor.get_connected_drives();
for (let i = 0; i < drives.length; i++) {
let volumes = drives[i].get_volumes();
for(let j = 0; j < volumes.length; j++) {
let mount = volumes[j].get_mount();
let kind = 'devices';
let identifier = volumes[j].get_identifier('class');
if (identifier && identifier.indexOf('network') >= 0)
kind = 'network';
if(mount != null)
this._addMount(kind, mount);
}
}
if (_DEBUG_) global.log("PlacesManager: _updateMounts - connected drives itterated through");
/* add all volumes that is not associated with a drive */
let volumes = this._volumeMonitor.get_volumes();
for(let i = 0; i < volumes.length; i++) {
if(volumes[i].get_drive() != null)
continue;
let kind = 'devices';
let identifier = volumes[i].get_identifier('class');
if (identifier && identifier.indexOf('network') >= 0)
kind = 'network';
let mount = volumes[i].get_mount();
if(mount != null)
this._addMount(kind, mount);
}
if (_DEBUG_) global.log("PlacesManager: _updateMounts - volumes assiated with drives added");
/* add mounts that have no volume (/etc/mtab mounts, ftp, sftp,...) */
let mounts = this._volumeMonitor.get_mounts();
for(let i = 0; i < mounts.length; i++) {
if(mounts[i].is_shadowed())
continue;
if(mounts[i].get_volume())
continue;
let root = mounts[i].get_default_location();
let kind;
if (root.is_native())
kind = 'devices';
else
kind = 'network';
this._addMount(kind, mounts[i]);
}
if (_DEBUG_) global.log("PlacesManager: _updateMounts - mounts that have no volume added");
this.emit('devices-updated');
this.emit('network-updated');
}
_findBookmarksFile() {
if (_DEBUG_) global.log("PlacesManager: _findBookmarksFile");
let paths = [
GLib.build_filenamev([GLib.get_user_config_dir(), 'gtk-3.0', 'bookmarks']),
GLib.build_filenamev([GLib.get_home_dir(), '.gtk-bookmarks']),
];
for (let i = 0; i < paths.length; i++) {
if (GLib.file_test(paths[i], GLib.FileTest.EXISTS)) {
if (_DEBUG_) global.log("PlacesManager: _findBookmarksFile - found .. returning path");
return Gio.File.new_for_path(paths[i]);
}
}
if (_DEBUG_) global.log("PlacesManager: _findBookmarksFile - not found .. returning NULL");
return null;
}
_reloadBookmarks() {
if (_DEBUG_) global.log("PlacesManager: _reloadBookmarks");
this._bookmarks = [];
let content = Shell.get_file_contents_utf8_sync(this._bookmarksFile.get_path());
let lines = content.split('\n');
let bookmarks = [];
for (let i = 0; i < lines.length; i++) {
let line = lines[i];
let components = line.split(' ');
let bookmark = components[0];
if (!bookmark)
continue;
let file = Gio.File.new_for_uri(bookmark);
if (file.is_native() && !file.query_exists(null))
continue;
let duplicate = false;
for (let i = 0; i < this._places.special.length; i++) {
if (file.equal(this._places.special[i].file)) {
duplicate = true;
break;
}
}
if (duplicate)
continue;
for (let i = 0; i < bookmarks.length; i++) {
if (file.equal(bookmarks[i].file)) {
duplicate = true;
break;
}
}
if (duplicate)
continue;
let label = null;
if (components.length > 1)
label = components.slice(1).join(' ');
bookmarks.push(new PlaceInfo('bookmarks', file, label));
}
this._places.bookmarks = bookmarks;
this.emit('bookmarks-updated');
}
_addMount(kind, mount) {
if (_DEBUG_) global.log("PlacesManager: _reloadBookmarks");
let devItem = new PlaceDeviceInfo(kind, mount);
this._places[kind].push(devItem);
}
getPlace(kind) {
return this._places[kind];
}
getAllPlaces() {
return this._places['special'].concat(this._places['bookmarks'], this._places['devices']);
}
getDefaultPlaces() {
return this._places['special'];
}
getBookmarks() {
return this._places['bookmarks'];
}
getMounts() {
return this._places['devices'];
}
};
Signals.addSignalMethods(PlacesManager.prototype);