-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathextension.js
More file actions
376 lines (350 loc) · 15.7 KB
/
Copy pathextension.js
File metadata and controls
376 lines (350 loc) · 15.7 KB
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
/* -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*- */
/**
* Remmina Search Provider for GNOME Shell
*
* Copyright (c) 2020 Alex Murray <murray.alex@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import GLib from 'gi://GLib';
import Gio from 'gi://Gio';
import Shell from 'gi://Shell';
import St from 'gi://St';
import * as Util from 'resource:///org/gnome/shell/misc/util.js';
import * as Search from 'resource:///org/gnome/shell/ui/search.js';
let remminaApp = null;
const emblems = {
'NX': ['remmina-nx', 'org.remmina.Remmina-nx'],
'RDP': ['remmina-rdp', 'org.remmina.Remmina-rdp'],
'SFTP': ['remmina-sftp', 'org.remmina.Remmina-sftp'],
'SPICE': ['remmina-spice', 'org.remmina.Remmina-spice'],
'SSH': ['remmina-ssh', 'org.remmina.Remmina-ssh', 'gnome-terminal', 'org.gnome.Terminal', 'x-term'],
'VNC': ['remmina-vnc', 'org.remmina.Remmina-vnc'],
'XDMCP': ['remmina-xdmcp', 'org.remmina.Remmina-xdmcp']
};
let provider = null;
var RemminaSearchProvider = class RemminaSearchProvider_SearchProvider {
_getDataDirPaths() {
// remmina stores its configuration in ~/.config/remmina/remmina.pref -
// this may also be within a snap -
// ie. ~/snap/remmina/current/.config/remmina/remmina.pref or flatpak -
// ~/.var/app/org.remmina.Remmina/config/remmina/remmina.pref - from
// each of these files parse it as a gkeyfile and get the
// remmina_pref.datadir_path value from each - however if the value is
// not set then we need to try and use the same default that remmina
// itself uses
let dirs = new Map([
[GLib.build_filenamev([GLib.get_home_dir(), '.config', 'remmina']), GLib.build_filenamev([GLib.get_home_dir(), '.local', 'share', 'remmina'])],
[GLib.build_filenamev([GLib.get_home_dir(), 'snap', 'remmina', 'current', '.config', 'remmina']), GLib.build_filenamev([GLib.get_home_dir(), 'snap', 'remmina', 'current', '.local', 'share', 'remmina'])],
[GLib.build_filenamev([GLib.get_home_dir(), '.var', 'app', 'org.remmina.Remmina', 'config', 'remmina'] ), GLib.build_filenamev([GLib.get_home_dir(), '.var', 'app', 'org.remmina.Remmina', 'data', 'remmina'])]
]);
let paths = [];
dirs.forEach((default_datadir, dir_path, _) => {
let pref_file_path = GLib.build_filenamev([dir_path, 'remmina.pref']);
let pref_file = Gio.file_new_for_path(pref_file_path);
if (pref_file.query_exists(null)) {
let keyfile = new GLib.KeyFile();
try {
keyfile.load_from_file(pref_file_path, 0);
if (keyfile.has_group('remmina_pref')) {
let data_dir = keyfile.get_string('remmina_pref', 'datadir_path');
// if data_dir is empty then default to data/remmina in the
if (!data_dir || data_dir == "") {
console.debug("remmina pref datadir_path empty - using default: " + default_datadir);
data_dir = default_datadir;
}
if (paths.indexOf(data_dir) < 0) {
paths.push(data_dir);
}
} else {
throw new Error("remmina_pref group not found in remmina pref file: " + pref_file_path);
}
keyfile = null;
} catch (e) {
console.warn("Failed to load remmina pref file: " + pref_file_path + ": " + e.toString());
}
}
}
, this);
return paths;
}
_monitorRemminaDir(path) {
let dir = Gio.file_new_for_path(path);
let monitor = dir.monitor_directory(Gio.FileMonitorFlags.NONE, null);
let id = monitor.connect('changed', (monitor, file, other_file, type) => {
this._onMonitorChanged(monitor, file, other_file, type);
});
/* save a reference so we can cancel it on disable */
this._remminaMonitors.set(id, monitor);
this._listDirAsync(dir, (files) => {
files.map((f) => {
let name = f.get_name();
let file_path = GLib.build_filenamev([path, name]);
let file = Gio.file_new_for_path(file_path);
this._onMonitorChanged(monitor, file,
null, Gio.FileMonitorEvent.CREATED);
}, this);
});
}
constructor(name) {
this.id = 'remmina';
this.theme = new St.IconTheme();
// add snap icon search path
let snapIconPath = GLib.build_filenamev(['/snap', 'remmina', 'current', 'usr', 'share', 'icons']);
if (GLib.file_test(snapIconPath, GLib.FileTest.IS_DIR)) {
this.theme.append_search_path(snapIconPath);
}
this._sessions = [];
// map of signal id to monitor so we can disconnect on disable
this._remminaMonitors = new Map();
let paths = this._getDataDirPaths();
for (let i = 0; i < paths.length; i++) {
this._monitorRemminaDir(paths[i]);
}
}
destroy() {
this.theme = null;
this._sessions = [];
for (let id of this._remminaMonitors.keys()) {
let monitor = this._remminaMonitors.get(id);
monitor.disconnect(id);
monitor.cancel();
this._remminaMonitors.set(id, null);
}
this._remminaMonitors = new Map();
}
_onMonitorChanged(monitor, file, other_file, type) {
let path = file.get_path();
if (type == Gio.FileMonitorEvent.CREATED ||
type == Gio.FileMonitorEvent.CHANGED ||
type == Gio.FileMonitorEvent.CHANGES_DONE_HINT) {
let keyfile = new GLib.KeyFile();
try {
keyfile.load_from_file(path, 0);
} catch (e) {
console.warn("Failed to load remmina session file: " + path + ": " + e.toString());
return;
}
if (!keyfile.has_group('remmina')) {
return;
}
let name = keyfile.get_string('remmina', 'name');
if (name) {
// get the type of session so we can use different
// icons for each
let protocol = keyfile.get_string('remmina', 'protocol');
let server = keyfile.get_string('remmina', 'server');
let group = keyfile.get_string('remmina', 'group');
let session = { name: name,
protocol: protocol,
server: server,
group: group,
file: path };
// if this session already exists in _sessions then
// delete and add again to update it
for (let i = 0; i < this._sessions.length; i++) {
let s = this._sessions[i];
if (s.file == session.file) {
this._sessions.splice(i, 1);
break;
}
}
this._sessions.push(session);
} else {
console.warn("Remmina session missing name in file: " + path);
}
keyfile = null;
} else if (type == Gio.FileMonitorEvent.DELETED) {
for (let i = 0; i < this._sessions.length; i++) {
let s = this._sessions[i];
if (s.file == path) {
/* remove the current element from _sessions */
this._sessions.splice(i, 1);
break;
}
}
}
}
// steal from FileUtils since doesn't exist in FileUtils anymore
// since GNOME 3.12
_listDirAsync(file, callback) {
let allFiles = [];
file.enumerate_children_async('standard::name,standard::type',
Gio.FileQueryInfoFlags.NONE,
GLib.PRIORITY_LOW, null,
function (obj, res) {
let enumerator = obj.enumerate_children_finish(res);
function onNextFileComplete(obj, res) {
let files = obj.next_files_finish(res);
if (files.length) {
allFiles = allFiles.concat(files);
enumerator.next_files_async(100, GLib.PRIORITY_LOW, null, onNextFileComplete);
} else {
enumerator.close(null);
callback(allFiles);
}
}
enumerator.next_files_async(100, GLib.PRIORITY_LOW, null, onNextFileComplete);
});
}
createResultObject(metaInfo, terms) {
metaInfo.createIcon = (size) => {
let box = new St.BoxLayout();
let icon = null;
icon = remminaApp.create_icon_texture(size);
box.add_child(icon);
if (metaInfo.protocol in emblems) {
// remmina emblems are fixed size of 22 pixels
let size = 22;
let names = emblems[metaInfo.protocol];
let name = null;
let found = false;
for (let n = 0; !found && n < names.length; n++) {
name = names[n];
found = this.theme.has_icon(name);
if (!found) {
// also try with -symbolic suffix
name = name + '-symbolic';
found = this.theme.has_icon(name);
}
}
if (found) {
let emblem = new St.Icon({ gicon: new Gio.ThemedIcon({name: name}),
icon_size: size });
box.add_child(emblem);
} else {
console.debug("Failed to find emblem icon for protocol: " + metaInfo.protocol);
}
} else {
console.debug("No emblem defined for protocol: " + metaInfo.protocol);
}
return box;
};
return new Search.GridSearchResult(provider, metaInfo, Main.overview.searchController._searchResults);
}
filterResults(results, max) {
return results.slice(0, max);
}
_wrapText(str, maxWidth) {
return str.replace(
new RegExp(`(?![^\\n]{1,${maxWidth}}$)([^\\n]{1,${maxWidth}})\\s`, 'g'),
'$1\n');
}
async getResultMetas(ids, cancellable) {
let metas = [];
for (let i = 0; i < ids.length; i++) {
let id = ids[i];
let session = null;
// find session details
for (let j = 0; !session && j < this._sessions.length; j++) {
let _session = this._sessions[j];
if (_session.file == id)
session = _session;
}
if (session != null) {
let prefix = ((session.group && session.group != "") ?
("[" + session.group + "] ") : "");
let name = this._wrapText(prefix + session.name + ' (' + session.protocol + ')',
// TODO: Wrap at max label width
15);
metas.push({ id: id,
protocol: session.protocol,
description: session.server,
name: name });
} else {
console.warn("failed to find session with id: " + id);
}
}
return metas;
}
activateResult(id, terms) {
if (remminaApp) {
remminaApp.app_info.launch([Gio.file_new_for_path(id)], null);
} else {
Util.spawn(['remmina', '-c', id]);
}
// specifically hide the overview -
// https://github.com/alexmurray/remmina-search-provider/issues/19
Main.overview.hide();
}
_getResultSet(sessions, terms, cancellable) {
let results = [];
// search for terms ignoring case - create re's once only for
// each term and make sure matches all terms
let res = terms.map(function (term) { return new RegExp(term, 'i'); });
for (let i = 0; i < sessions.length; i++) {
let session = sessions[i];
let failed = false;
for (let j = 0; !failed && j < res.length; j++) {
let re = res[j];
// search on name, group, protocol or the term remmina
failed |= (session.name.search(re) < 0 &&
session.group.search(re) < 0 &&
session.protocol.search(re) < 0 &&
'remmina'.search(re) < 0);
}
if (!failed) {
results.push(session.file);
}
}
return results;
}
async getInitialResultSet(terms, cancellable) {
let realResults = this._getResultSet(this._sessions, terms, cancellable);
return realResults;
}
async getSubsearchResultSet(results, terms, cancellable) {
return this.getInitialResultSet(terms, cancellable);
}
};
export default class RemminaSearchProviderExtension {
enable () {
if (!remminaApp) {
// desktop id changed in recent releases and make sure to include
// snap/flatpak ids too
let ids = ["org.remmina.Remmina", "remmina_remmina", "remmina", "remmina-file"];
for (let i = 0; i < ids.length; i++) {
remminaApp = Shell.AppSystem.get_default().lookup_app(ids[i] + ".desktop");
if (remminaApp) {
break;
}
}
if (!remminaApp) {
console.warn("Failed to find remmina application");
let installed = Shell.AppSystem.get_default().get_installed();
installed.forEach((app) => {
// log all installed apps for debugging in case one of them is a new variant of remmina
console.debug("Installed app: " + app.get_id());
});
}
}
// only create the provider if remmina app is found otherwise we can't
// show icons or launch remmina so there is no point
if (!provider && remminaApp) {
provider = new RemminaSearchProvider();
Main.overview.searchController.addProvider(provider);
}
}
disable() {
if (provider) {
Main.overview.searchController.removeProvider(provider);
provider.destroy();
provider = null;
}
if (remminaApp) {
remminaApp = null;
}
}
}