forked from micheleg/dash-to-dock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileManager1API.js
217 lines (195 loc) · 7.14 KB
/
fileManager1API.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
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
const Gio = imports.gi.Gio;
const Signals = imports.signals;
const Me = imports.misc.extensionUtils.getCurrentExtension();
const Utils = Me.imports.utils;
const FileManager1Iface = '<node><interface name="org.freedesktop.FileManager1">\
<property name="XUbuntuOpenLocationsXids" type="a{uas}" access="read"/>\
<property name="OpenWindowsWithLocations" type="a{sas}" access="read"/>\
</interface></node>';
const FileManager1Proxy = Gio.DBusProxy.makeProxyWrapper(FileManager1Iface);
/**
* This class implements a client for the org.freedesktop.FileManager1 dbus
* interface, and specifically for the OpenWindowsWithLocations property
* which is published by Nautilus, but is not an official part of the interface.
*
* The property is a map from window identifiers to a list of locations open in
* the window.
*
* While OpeWindowsWithLocations is part of upstream Nautilus, for many years
* prior, Ubuntu patched Nautilus to publish XUbuntuOpenLocationsXids, which is
* similar but uses Xids as the window identifiers instead of gtk window paths.
*
* When an old or unpatched Nautilus is running, we will observe the properties
* to always be empty arrays, but there will not be any correctness issues.
*/
var FileManager1Client = class DashToDock_FileManager1Client {
constructor() {
this._signalsHandler = new Utils.GlobalSignalsHandler();
this._locationMap = new Map();
this._proxy = new FileManager1Proxy(Gio.DBus.session,
"org.freedesktop.FileManager1",
"/org/freedesktop/FileManager1",
(initable, error) => {
// Use async construction to avoid blocking on errors.
if (error) {
global.log(error);
} else {
this._updateLocationMap();
}
});
this._signalsHandler.add([
this._proxy,
'g-properties-changed',
this._onPropertyChanged.bind(this)
], [
// We must additionally listen for Screen events to know when to
// rebuild our location map when the set of available windows changes.
global.workspace_manager,
'workspace-switched',
this._updateLocationMap.bind(this)
], [
global.display,
'window-entered-monitor',
this._updateLocationMap.bind(this)
], [
global.display,
'window-left-monitor',
this._updateLocationMap.bind(this)
]);
}
destroy() {
this._signalsHandler.destroy();
this._proxy.run_dispose();
}
/**
* Return an array of windows that are showing a location or
* sub-directories of that location.
*/
getWindows(location) {
let ret = new Set();
for (let [k,v] of this._locationMap) {
if (k.startsWith(location)) {
for (let l of v) {
ret.add(l);
}
}
}
return Array.from(ret);
}
_onPropertyChanged(proxy, changed, invalidated) {
let property = changed.unpack();
if (property &&
('XUbuntuOpenLocationsXids' in property ||
'OpenWindowsWithLocations' in property)) {
this._updateLocationMap();
}
}
_updateLocationMap() {
let properties = this._proxy.get_cached_property_names();
if (properties == null) {
// Nothing to check yet.
return;
}
if (properties.includes('OpenWindowsWithLocations')) {
this._updateFromPaths();
} else if (properties.includes('XUbuntuOpenLocationsXids')) {
this._updateFromXids();
}
}
_updateFromPaths() {
let pathToLocations = this._proxy.OpenWindowsWithLocations;
let pathToWindow = getPathToWindow();
let locationToWindow = new Map();
for (let path in pathToLocations) {
let locations = pathToLocations[path];
for (let i = 0; i < locations.length; i++) {
let l = locations[i];
// Use a set to deduplicate when a window has a
// location open in multiple tabs.
if (!locationToWindow.has(l)) {
locationToWindow.set(l, new Set());
}
let window = pathToWindow.get(path);
if (window != null) {
locationToWindow.get(l).add(window);
}
}
}
this._locationMap = locationToWindow;
this.emit('windows-changed');
}
_updateFromXids() {
let xidToLocations = this._proxy.XUbuntuOpenLocationsXids;
let xidToWindow = getXidToWindow();
let locationToWindow = new Map();
for (let xid in xidToLocations) {
let locations = xidToLocations[xid];
for (let i = 0; i < locations.length; i++) {
let l = locations[i];
// Use a set to deduplicate when a window has a
// location open in multiple tabs.
if (!locationToWindow.has(l)) {
locationToWindow.set(l, new Set());
}
let window = xidToWindow.get(parseInt(xid));
if (window != null) {
locationToWindow.get(l).add(window);
}
}
}
this._locationMap = locationToWindow;
this.emit('windows-changed');
}
}
Signals.addSignalMethods(FileManager1Client.prototype);
/**
* Construct a map of gtk application window object paths to MetaWindows.
*/
function getPathToWindow() {
let pathToWindow = new Map();
for (let i = 0; i < global.workspace_manager.n_workspaces; i++) {
let ws = global.workspace_manager.get_workspace_by_index(i);
ws.list_windows().map(function(w) {
let path = w.get_gtk_window_object_path();
if (path != null) {
pathToWindow.set(path, w);
}
});
}
return pathToWindow;
}
/**
* Construct a map of XIDs to MetaWindows.
*
* This is somewhat annoying as you cannot lookup a window by
* XID in any way, and must iterate through all of them looking
* for a match.
*/
function getXidToWindow() {
let xidToWindow = new Map();
for (let i = 0; i < global.workspace_manager.n_workspaces; i++) {
let ws = global.workspace_manager.get_workspace_by_index(i);
ws.list_windows().map(function(w) {
let xid = guessWindowXID(w);
if (xid != null) {
xidToWindow.set(parseInt(xid), w);
}
});
}
return xidToWindow;
}
/**
* Guesses the X ID of a window.
*
* This is the basic implementation that is sufficient for Nautilus
* windows. The pixel-saver extension has a much more complex
* implementation if we ever need it.
*/
function guessWindowXID(win) {
try {
return win.get_description().match(/0x[0-9a-f]+/)[0];
} catch (err) {
return null;
}
}