-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkeyman.js
334 lines (278 loc) · 9.05 KB
/
keyman.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
/*
* KeyMan - A gnome shell extension to access the keyring in a convenient way
* (c) 2014 David Poetzsch-Heffter <[email protected]>
* This file is distributed under the same licence as the KeyMan package.
* See file LICENSE for details.
*/
const St = imports.gi.St;
const Gtk = imports.gi.Gtk;
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const Main = imports.ui.main;
const Shell = imports.gi.Shell;
const Meta = imports.gi.Meta;
const Mainloop = imports.mainloop;
const GLib = imports.gi.GLib;
const _ = imports.gettext.domain("keyman").gettext;
const Me = imports.misc.extensionUtils.getCurrentExtension();
const Clipboard = Me.imports.clipboard;
const Data = Me.imports.data;
const KeyringConnection = Me.imports.keyringDbus.KeyringConnection;
const Utils = Me.imports.utils;
const Settings = Me.imports.settings;
const MAX_LENGTH = 100;
const KEY_RETURN = 65293;
const KEY_ENTER = 65421;
//const keyOpen = 'open-keyman'; // Schema key for key binding
const dataDir = Utils.joinPaths([GLib.get_user_data_dir(), "KeyMan"]);
class CollectionItem {
constructor(keyring, collection, changeCallback = null) {
this.item = new PopupMenu.PopupMenuItem(collection.label);
this.keyring = keyring;
this.collection = collection;
this.changeCallback = changeCallback;
this._lockedIcon = new St.Icon({
icon_name: "changes-prevent-symbolic",
style_class: "system-status-icon",
reactive: true,
track_hover: true,
});
this._unlockedIcon = new St.Icon({
icon_name: "changes-allow-symbolic",
style_class: "system-status-icon",
reactive: true,
track_hover: true,
});
this._addIcon();
this.item.connect("activate", () => this._toggle());
}
_addIcon() {
if (this.collection.locked) {
this.item.actor.add_actor(this._lockedIcon);
} else {
this.item.actor.add_actor(this._unlockedIcon);
}
}
_toggle() {
if (this.collection.locked) {
this.keyring.unlockObject(this.collection.path, wasLockedBefore => {
this.collection.locked = false;
this._callChangeCallback();
});
} else {
this.keyring.lockObject(this.collection.path);
this.collection.locked = true;
this._callChangeCallback();
}
}
_callChangeCallback() {
if (this.changeCallback) {
this.changeCallback();
}
}
}
var KeyMan = class KeyMan {
constructor() {
this.settings = imports.misc.extensionUtils.getSettings();
this.theButton = new PanelMenu.Button(St.Align.START);
// connect to keyring
this.keyring = new KeyringConnection();
// initialize history
this.history = new Data.History(this.settings, dataDir);
// remember timeouts
this.timeouts = [];
const icon = new St.Icon({
icon_name: "dialog-password",
style_class: "system-status-icon",
reactive: true,
track_hover: true,
});
this.theButton.add_child(icon);
// Add keybinding
Main.wm.addKeybinding(
Settings.KEY_OPEN_KEYMAN_MENU_KEYBINDING,
this.settings,
Meta.KeyBindingFlags.NONE,
Shell.ActionMode.NORMAL | Shell.ActionMode.OVERVIEW,
() => this.theButton.menu.open(),
);
// Auto focus
this.theButton.menu.connect("open-state-changed", (menu, open) => {
// this is triggered when the keymanager menu is opened
if (open) {
// if we do it immediately the focus gets lost again
this.timeouts.push(
Mainloop.timeout_add_seconds(0, () => {
this.searchEntry.grab_key_focus();
}),
);
} else {
this.searchEntry.get_stage().set_key_focus(null);
}
});
this._createLayout();
}
_getSecretCallback(label, secret) {
// there is a weird bug that sometimes we don't get the secret;
// thus, we add some debug output in this case
if (!secret || secret === "") {
print('KEYMAN: fetched secret but it was: "' + secret + '"');
} else {
print("KEYMAN: fetched secret and it was neither empty nor undefined");
}
this._removeTimeouts();
const settings = this.settings;
Clipboard.set(settings, secret);
let duration = settings.get_int(Settings.KEY_CLIPBOARD_DURATION);
this.timeouts.push(
Mainloop.timeout_add(duration, function() {
Clipboard.empty(settings);
}),
);
}
_copySecret(path) {
this.keyring.getSecretFromPath(path, (label, secret) =>
this._getSecretCallback(label, secret),
);
}
_createSecretMenuItem(item) {
let pmi = new PopupMenu.PopupMenuItem(item.label);
pmi.connect("activate", () => {
this._copySecret(item.path);
this._clearSearchResults();
this.history.add(item);
this._populateHistoryMenu();
this.theButton.menu.close();
});
return pmi;
}
_populateHistoryMenu() {
this.historySection.removeAll();
for (const elem of this.history) {
this.historySection.addMenuItem(this._createSecretMenuItem(elem));
}
}
_clearSearchResults() {
this.searchResultsSection.removeAll();
}
_populateCollectionsMenu() {
this.collectionsMenu.menu.removeAll();
let collections = this.keyring.getCollections();
for (let i in collections) {
let col = collections[i];
if (col.path != "/org/freedesktop/secrets/collection/session") {
// we don't add the item via addMenuItem because we do not
// want the menu to close if the item is clicked
this.collectionsMenu.menu.box.add(
new CollectionItem(this.keyring, col, () =>
this._repopulateSearchResults(),
).item.actor,
);
}
}
}
_createLayout() {
// Create unlock menu
this.collectionsMenu = new PopupMenu.PopupSubMenuMenuItem(
_("Keyrings"),
true,
);
this._populateCollectionsMenu();
this.theButton.menu.addMenuItem(this.collectionsMenu);
// when collections change refill collections menu
this.keyring.connectCollectionChangedSignal(() => {
this._populateCollectionsMenu();
});
let separator1 = new PopupMenu.PopupSeparatorMenuItem();
this.theButton.menu.addMenuItem(separator1);
// Create history section
this.historySection = new PopupMenu.PopupMenuSection();
this._populateHistoryMenu();
this.theButton.menu.addMenuItem(this.historySection);
// Separator
let separator2 = new PopupMenu.PopupSeparatorMenuItem();
this.theButton.menu.addMenuItem(separator2);
// Bottom section: Search
let bottomSection = new PopupMenu.PopupMenuSection();
this.searchResultsSection = new PopupMenu.PopupMenuSection();
this.searchHint = _("Search...");
this.searchEntry = new St.Entry({
name: "searchEntry",
hint_text: this.searchHint,
track_hover: true,
can_focus: true,
});
this.entrySearch = this.searchEntry.clutter_text;
this.entrySearch.set_max_length(MAX_LENGTH);
this.entrySearch.connect("text-changed", (obj, event) => {
const text1 = obj.get_text();
const text1Len = text1.trim().length;
if (text1Len == 0 || text1Len >= 3) {
this._repopulateSearchResults();
} else {
// here we want to wait a while because there
// are more chars comming for sure.
// However, if there is not more input comming we
// still activate the search in order to not
// confuse the user!
this.timeouts.push(
Mainloop.timeout_add_seconds(1, () => {
const text2 = obj.get_text();
// if the text already changed again we
// don't need to search
if (text1 === text2) {
this._repopulateSearchResults();
}
}),
);
}
});
bottomSection.actor.add_actor(this.searchEntry);
bottomSection.addMenuItem(this.searchResultsSection);
bottomSection.actor.add_style_class_name("searchSection");
this.theButton.menu.addMenuItem(bottomSection);
}
_repopulateSearchResults() {
const text = this.entrySearch.get_text();
if (text !== this.searchHint) {
this._updateSearchResults(text);
}
}
_updateSearchResults(text) {
this._clearSearchResults();
if (text.trim().length === 0) {
return;
}
//this.menu.close();
let searchStrs = text.trim().split(/\s+/);
searchStrs = searchStrs.filter(s => s != "");
if (searchStrs.length > 0) {
let items = this.keyring.getItems(searchStrs);
if (items.length > 0) {
for (let i in items) {
let item = items[i];
let mi = this._createSecretMenuItem(item);
this.searchResultsSection.addMenuItem(mi);
}
} else {
let it = new PopupMenu.PopupMenuItem(_("Nothing found."));
this.searchResultsSection.addMenuItem(it);
}
}
}
_enable() {}
_removeTimeouts() {
while (this.timeouts.length > 0) {
Mainloop.source_remove(this.timeouts.pop());
}
}
_disable() {
this.keyring.close();
this._removeTimeouts();
this.history.close();
}
destroy() {
this.theButton.destroy();
Main.wm.removeKeybinding(Settings.KEY_OPEN_KEYMAN_MENU_KEYBINDING);
}
};