-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpasswordManagerSearchProvider.js
201 lines (181 loc) · 6.66 KB
/
passwordManagerSearchProvider.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
const Clutter = imports.gi.Clutter;
const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
const St = imports.gi.St;
const Util = imports.misc.util;
const Me = imports.misc.extensionUtils.getCurrentExtension();
const PasswordManagers = Me.imports.passwordManagers;
const Convenience = Me.imports.convenience;
var PasswordManagerSearchProvider = class PMSPasswordManagerSearchProvider {
constructor() {
this._settings = Convenience.getSettings();
this._manager = this._settings.get_string('manager');
this._prefixUsername = this._settings.get_string('username-prefix');
this._prefixPassword = this._settings.get_string('password-prefix');
this._syncInterval = this._settings.get_int('sync-interval');
this._clipboardSetter = this._settings.get_string('clipboard-setter');
this._currentPrefix = '';
this.enabled = true;
let icon;
switch (this._manager) {
case 'LASTPASS':
this._passwordManager = new PasswordManagers.LastPass();
icon = Gio.icon_new_for_string(`${Me.path}/icons/lastpass.png`);
break;
case '1PASSWORD':
this._passwordManager = new PasswordManagers.OnePassword();
icon = Gio.icon_new_for_string(
`${Me.path}/icons/1password.png`,
);
break;
case 'BITWARDEN':
this._passwordManager = new PasswordManagers.Bitwarden();
icon = Gio.icon_new_for_string(
`${Me.path}/icons/bitwarden.png`,
);
break;
default:
this.enabled = false;
return;
}
// application info
this.appInfo = {
get_name: () => { return 'Password Manager Search' },
get_id: () => { return 'password-manager-search' },
get_icon: () => { return icon },
should_show: () => { return true }
};
// Do one initial sync
GLib.timeout_add_seconds(GLib.PRIORITY_LOW, 4, () => {
this._passwordManager.sync();
});
// Sync at regular intervalls
GLib.timeout_add_seconds(
GLib.PRIORITY_LOW,
this._syncInterval * 60,
() => {
if (this.enabled) {
this._passwordManager.sync();
return true;
}
},
);
}
/**
* Used in getResultMetas callback to create icons for the entries.
* @param {int} size - Size of icon.
* @returns {Box} - Container containing the created icon.
*/
_createIcon(size) {
const icon = new St.Icon({
gicon: new Gio.ThemedIcon({ name: 'dialog-password' }),
icon_size: size,
});
return icon;
}
/**
* Called by GS when an result is choosen in the overview.
* Save the selected result to clipboard.
* @param {string} id - Identity of the selected item.
* @param {[string]} _terms - The search string split by space.
* @param {string} _timestamp - Time when called.
*/
activateResult(id, _terms, _timestamp) {
var item = '';
if (this._currentPrefix === this._prefixUsername)
item = this._passwordManager.getAccountUsername(id);
else
item = this._passwordManager.getAccountPassword(id);
let clipboard;
switch (this._clipboardSetter) {
case 'NATIVE':
clipboard = St.Clipboard.get_default();
clipboard.set_text(St.ClipboardType.CLIPBOARD, item);
break;
case 'XSEL':
Util.spawn([
'bash',
'-c',
`echo -n "${item}" | xsel --clipboard`,
]);
break;
case 'XCLIP':
Util.spawn([
'bash',
'-c',
`echo -n "${item}" | xclip -selection clipboard`,
]);
}
}
/**
* Called by GS to limit the amount of results it shows.
* @param {[string]} results - results
* @param {int} max - maximum amount of results it expects to get back.
* @returns {[string]} - Results to be displayed.
*/
filterResults(results, max) {
return results.slice(0, max);
}
/**
* Called by GS when a new search is started.
* @param {[string]} terms - The search string split by space.
* @param {callable} callback - Function that handles the results.
* @param {bool} _cancellable - If the search can be cancelled?.
*/
getInitialResultSet(terms, callback, _cancellable) {
this.getResult(terms, callback);
}
/**
* Called by getResults and getSubsearchResultSet. Sets results for seaches.
* @param {[string]} terms - The search string split by space.
* @param {callable} callback - Function that handles the results.
*/
getResult(terms, callback) {
let fullSearch = terms.join(' ');
let fullTerm = '';
let results = [];
this._currentPrefix = '';
if (fullSearch.startsWith(`${this._prefixUsername} `)) {
this._currentPrefix = this._prefixUsername;
fullTerm = fullSearch.substring(this._prefixUsername.length + 1);
}
if (fullSearch.startsWith(`${this._prefixPassword} `)) {
this._currentPrefix = this._prefixPassword;
fullTerm = fullSearch.substring(this._prefixPassword.length + 1);
}
if (this._currentPrefix) {
let accounts = this._passwordManager.getAccountNames();
let regExp = new RegExp(fullTerm, 'i');
results = accounts.filter(account => regExp.test(account));
}
callback(results);
}
/**
* Called by GS to obtain information about the search results.
* @param {[string]} ids - Search ids.
* @param {callable} callback - Function that handles the resulting
* information.
*/
getResultMetas(ids, callback) {
const metas = [];
for (let i = 0; i < ids.length; i++) {
metas.push({
id: ids[i],
name: ids[i],
createIcon: this._createIcon,
});
}
callback(metas);
}
/**
* Called by GS to refine the initial search results whe nthe user types
* more characters in the search entry.
* @param {[string]} _results - Current results.
* @param {[string]} terms - The search string split by space.
* @param {callable} callback - Function that handles the results.
* @param {bool} _cancellable - If the search can be cancelled?.
*/
getSubsearchResultSet(_results, terms, callback, _cancellable) {
this.getResult(terms, callback);
}
};