-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathprefs.js
527 lines (445 loc) · 16.6 KB
/
prefs.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/*
*
* Password Calculator extension for GNOME Shell preferences
* - Creates a widget to set the preferences of the pwcalc extension
*
* Copyright (C) 2014-2023
* Thilo Maurer <tm@thilomaurer.de>
*
* This file is part of gnome-shell-extension-pwcalc.
*
* gnome-shell-extension-pwcalc 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.
*
* gnome-shell-extension-pwcalc 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 gnome-shell-extension-pwcalc. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
import Gdk from 'gi://Gdk';
import Gtk from 'gi://Gtk';
import Gio from 'gi://Gio';
import GObject from 'gi://GObject';
const RECENT_URL_KEY = 'recenturls';
const SHOW_COPY_NOTIFICATION_KEY = 'show-copy-notification';
const COPY_TO_CLIPBOARD_KEY = 'copy-to-clipboard';
const COPY_TO_PRIMARY_KEY = 'copy-to-primary-selection';
const KEEP_COPY_OF_ALIASES_IN_FILE_KEY = 'keep-copy-of-aliases-in-file';
const KEEP_COPY_OF_ALIASES_FILENAME_KEY = 'keep-copy-of-aliases-filename';
const DEFAULT_PASSWORD_LENGTH_KEY = 'default-password-length';
const PASSWORD_METHOD_KEY = 'password-method';
import { ExtensionPreferences, gettext as _ } from 'resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js';
function bind_AdwComboRow(settings, key, comboRow) {
var stringList = comboRow.get_model();
// Function to update the comboRow selection based on the GSettings value
function updateComboRowFromSettings() {
const value = settings.get_string(key);
let index = -1;
for (let i = 0; i < stringList.get_n_items(); i++) {
if (stringList.get_string(i) === value) {
index = i;
break;
}
}
if (index !== -1) {
comboRow.set_selected(index);
}
}
// Function to update the GSettings value based on the comboRow selection
function updateSettingsFromComboRow() {
const position = comboRow.get_selected();
if (position !== -1) {
const value = stringList.get_string(position);
settings.set_string(key, value);
}
}
// Connect the change signal of GSettings
settings.connect(`changed::${key}`, updateComboRowFromSettings);
// Connect the change signal of the comboRow
comboRow.connect('notify::selected', updateSettingsFromComboRow);
// Initial synchronization
updateComboRowFromSettings();
}
export default class pwcalcExtensionPreferences extends ExtensionPreferences {
fillPreferencesWindow(window) {
var self = this;
window._settings = this.getSettings();
var settings = window._settings;
var path = this.metadata.path;
//load CSS from preferences GTK Window
let provider = new Gtk.CssProvider();
provider.load_from_path(path + '/prefs.css');
Gtk.StyleContext.add_provider_for_display(Gdk.Display.get_default(), provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
this.Builder = new Gtk.Builder();
this.Builder.set_scope(new pwcalcBuilderScope());
this.Builder.add_from_file(path + "/pwcalc-settings.ui");
var spage = this.Builder.get_object("settings-page");
var apage = this.Builder.get_object("aliases-page");
window.add(spage);
window.add(apage);
settings.bind(SHOW_COPY_NOTIFICATION_KEY, this.Builder.get_object("show-notification-switch"), 'active', Gio.SettingsBindFlags.DEFAULT);
settings.bind(COPY_TO_CLIPBOARD_KEY, this.Builder.get_object("copy-to-clipboard-switch"), 'active', Gio.SettingsBindFlags.DEFAULT);
settings.bind(COPY_TO_PRIMARY_KEY, this.Builder.get_object("copy-to-primary-switch"), 'active', Gio.SettingsBindFlags.DEFAULT);
settings.bind(KEEP_COPY_OF_ALIASES_IN_FILE_KEY, this.Builder.get_object("keep-copy-of-aliases-in-file-switch"), 'active', Gio.SettingsBindFlags.DEFAULT);
settings.bind(DEFAULT_PASSWORD_LENGTH_KEY, this.Builder.get_object("default-password-length-adjustment"), 'value', Gio.SettingsBindFlags.DEFAULT);
settings.bind(KEEP_COPY_OF_ALIASES_FILENAME_KEY, this.Builder.get_object("keep-copy-of-aliases-filename-label"), 'label', Gio.SettingsBindFlags.DEFAULT);
//settings.bind(PASSWORD_METHOD_KEY, this.Builder.get_object("password-method-combobox"), 'selected-item', Gio.SettingsBindFlags.DEFAULT);
bind_AdwComboRow(settings, PASSWORD_METHOD_KEY, this.Builder.get_object("password-method-combobox"));
// Filename for keeping aliases (in addition to gsettings)
self.Builder.get_object("keep-copy-of-aliases-filename-button").connect("clicked", function () {
let dialog = new Gtk.FileChooserDialog({
title: _("Select Filename to Keep Aliases"),
transient_for: window,
action: Gtk.FileChooserAction.SAVE,
modal: true
});
dialog.add_button("Cancel", Gtk.ResponseType.CANCEL);
dialog.add_button("Save", Gtk.ResponseType.ACCEPT);
dialog.connect("response", (dlg, response) => {
if (response === Gtk.ResponseType.ACCEPT) {
const file = dlg.get_file();
if (file) {
const filename = file.get_path(); // This gets the file path as a string
settings.set_string(KEEP_COPY_OF_ALIASES_FILENAME_KEY, filename);
}
}
dialog.destroy();
});
dialog.show();
});
// Aliases Column View
//const model = this.Builder.get_object("aliases-model"); //GListStore
const selection = this.Builder.get_object("aliases-table-selection");
const columnView = this.Builder.get_object("aliases-table");
const FooRow = GObject.registerClass({
Properties: {
'alias': GObject.ParamSpec.string(
'alias',
'Alias',
'Alias of the entry',
GObject.ParamFlags.READWRITE,
''
),
'width': GObject.ParamSpec.string(
'width',
'Width',
'Width of the entry',
GObject.ParamFlags.READWRITE,
''
),
'creation-date': GObject.ParamSpec.string(
'creation-date',
'CreationDate',
'Creation Date of the entry',
GObject.ParamFlags.READWRITE,
''
)
}
}, class FooRow extends GObject.Object {
_init(props = {}) {
super._init(props);
}
});
const model = new Gio.ListStore({ item_type: FooRow.$gtype });
selection.model = model;
console.log(model);
console.log(model.item_type);
const entry = new FooRow({ alias: "0", width: "1", "creation-date": "2" });
console.log(entry);
var updateModel = function () {
var data = JSON.parse(settings.get_string(RECENT_URL_KEY));
data.forEach(row => {
console.log(row);
console.log(typeof row);
if (typeof row === "string") {
const entry = new FooRow({ alias: row, width: "default", "creation-date": "" });
model.append(entry);
}
if (typeof row === "array") {
const entry = new FooRow({ alias: row[0], width: row[1], "creation-date": row[2] });
model.append(entry);
}
});
}
updateModel();
//console.log(model);
//console.log(JSON.stringify(model));
// Alias Column
const aliasFactory = new Gtk.SignalListItemFactory();
aliasFactory.connect("setup", (factory, listItem) => {
listItem.set_child(new Gtk.Label({ xalign: 0 }));
});
aliasFactory.connect("bind", (factory, listItem) => {
const item = model.get_item(listItem.get_position());
listItem.get_child().set_label(item.alias);
});
const aliasColumn = new Gtk.ColumnViewColumn({
title: "Alias",
factory: aliasFactory,
expand: true
});
columnView.append_column(aliasColumn);
// Width Column
const widthFactory = new Gtk.SignalListItemFactory();
widthFactory.connect("setup", (factory, listItem) => {
listItem.set_child(new Gtk.Label({ xalign: 0 }));
});
widthFactory.connect("bind", (factory, listItem) => {
const item = model.get_item(listItem.get_position());
listItem.get_child().set_label(item.width);
});
const widthColumn = new Gtk.ColumnViewColumn({
title: "Width",
factory: widthFactory
});
columnView.append_column(widthColumn);
// CreationDate Column
const creationdateFactory = new Gtk.SignalListItemFactory();
creationdateFactory.connect("setup", (factory, listItem) => {
listItem.set_child(new Gtk.Label({ xalign: 0 }));
});
creationdateFactory.connect("bind", (factory, listItem) => {
const item = model.get_item(listItem.get_position());
listItem.get_child().set_label(item["creation-date"]);
});
const creationdateColumn = new Gtk.ColumnViewColumn({
title: "CreationDate",
factory: creationdateFactory
});
columnView.append_column(creationdateColumn);
model.connect('items-changed', () => {
const data = [];
for (let i = 0; i < model.get_n_items(); i++) {
let item = model.get_item(i);
data.push({
alias: item.alias,
width: item.width,
creationDate: item["creation-date"]
});
}
settings.set_string(MODEL_KEY, JSON.stringify(data));
});
return;
this.treeview = this.Builder.get_object("tree-treeview");
this.liststore = this.Builder.get_object("liststore");
this.Iter = this.liststore.get_iter_first();
this.selectedItem = null;
var updateListStore = function (aliases) {
if (typeof model != "undefined") model.clear();
let current = model.get_iter_first();
for (let i in aliases) {
current = model.append();
model.set_value(current, 0, aliases[i]);
}
}
var removeSelectedItem = function () {
var ac = self.selectedItem;
if (ac == null) return;
var l = getrecentURL();
let textDialog = _("Remove Alias '%s' ?").replace("%s", l[ac]);
let dialog = new Gtk.Dialog({
title: _("Alias Removal")
});
let label = new Gtk.Label({
label: textDialog,
css_classes: ["mylabel"]
});
dialog.set_modal(1);
dialog.set_resizable(0);
dialog.set_transient_for(self.MainWidget.get_root());
let cancelbutton = dialog.add_button("Cancel", 0);
let removebuton = dialog.add_button("Remove", 1);
let dialog_area = dialog.get_content_area();
dialog_area.prepend(label);
dialog.connect("response", function (w, response_id) {
if (response_id == 1) {
l.splice(ac, 1);
setrecentURL(l);
updateListStore(l);
}
dialog.hide();
});
dialog.show();
}
var addItemUsingInputBox = function () {
let textDialog = _("Enter new alias");
let dialog = new Gtk.Dialog({
title: _("Add new alias")
});
let entry = new Gtk.Entry({
css_classes: ["mylabel"]
});
let label = new Gtk.Label({
label: textDialog,
css_classes: ["mylabel"]
});
dialog.set_modal(1);
dialog.set_resizable(0);
dialog.set_transient_for(self.MainWidget.get_root());
dialog.add_button("Cancel", 0);
let d = dialog.add_button("OK", 1);
d.sensitive = 0;
let testAlias = function (location) {
d.sensitive = 0;
if (entry.get_text()) d.sensitive = 1;
return 0;
};
entry.connect("changed", testAlias);
entry.activates_default = true;
let dialog_area = dialog.get_content_area();
dialog_area.append(label);
dialog_area.append(entry);
dialog.connect("response", function (w, response_id) {
let alias = entry.get_text();
if (response_id == 1 && alias) {
var l = getrecentURL();
l.push(alias);
l.sort();
setrecentURL(l);
updateListStore(l);
}
dialog.destroy();
return 0;
});
dialog.show();
};
this.Builder.get_object("tree-toolbutton-add").connect("clicked", function () {
addItemUsingInputBox();
});
this.Builder.get_object("tree-toolbutton-remove").connect("clicked", function () {
removeSelectedItem();
});
var exportimport = function () {
let bld = new Gtk.Builder();
bld.add_from_file(path + "/settings-importexport.ui");
let dialog = bld.get_object("dialog");
let tb = bld.get_object("textbuffer");
let d = bld.get_object("import");
let cancel = bld.get_object("cancel");
d.sensitive = 0;
let newRecentURL;
let testAlias = function () {
try {
newRecentURL = JSON.parse(tb.text);
d.sensitive = 1;
} catch (e) {
d.sensitive = 0;
}
return 0;
};
tb.connect("changed", testAlias);
tb.text = JSON.stringify(getrecentURL());
dialog.set_default_widget(cancel);
dialog.set_transient_for(self.MainWidget.get_root());
dialog.connect("response", function (w, response_id) {
if (response_id == 1) {
setrecentURL(newRecentURL);
updateListStore(newRecentURL);
}
dialog.destroy();
return 0;
});
dialog.show_all();
}
this.Builder.get_object("tree-toolbutton-export").connect("clicked", function () {
exportimport();
});
// this.treeview.connect("key-press-event", function(sender, event) {
// var kv = event.get_keyval()[1];
// if (kv == Gdk.KEY_Delete || kv == Gdk.KEY_BackSpace) removeSelectedItem();
// });
this.Builder.get_object("treeview-selection").connect("changed", function (select) {
let a = select.get_selected_rows()[0][0];
let sens = (a != null);
if (sens) self.selectedItem = parseInt(a.to_string());
self.Builder.get_object("tree-toolbutton-remove").sensitive = sens
});
/*
this.treeview.set_model(this.liststore);
let column = new Gtk.TreeViewColumn()
this.treeview.append_column(column);
let renderer = new Gtk.CellRendererText();
column.pack_start(renderer, null);
column.set_cell_data_func(renderer, function () {
arguments[1].markup = arguments[2].get_value(arguments[3], 0);
});
if (typeof this.liststore != "undefined") this.liststore.clear();
*/
var getrecentURL = function () {
var js = getString(RECENT_URL_KEY);
var obj;
try {
obj = JSON.parse(js);
} catch (e) {
obj = [];
}
return obj;
};
var setrecentURL = function (v) {
var js = JSON.stringify(v);
setString(RECENT_URL_KEY, js);
};
var getBool = function (key) {
return settings.get_boolean(key);
};
var setBool = function (key, v) {
settings.set_boolean(key, v);
};
var getInteger = function (key) {
return settings.get_int(key);
};
var setInteger = function (key, v) {
settings.set_int(key, v);
};
var getString = function (key) {
return settings.get_string(key);
};
var setString = function (key, v) {
settings.set_string(key, v);
};
this.Builder.get_object("tree-toolbutton-remove").sensitive = false;
self.Builder.get_object("keep-copy-of-aliases-filename-button").connect("clicked", function () {
let dialog = new Gtk.dialog({
title: _("Select Filename to Keep Aliases"),
transient_for: self.MainWidget.get_root(),
action: Gtk.FileChooserAction.SAVE,
modal: true
});
dialog.add_button("Cancel", Gtk.ResponseType.CANCEL);
dialog.add_button("Save", Gtk.ResponseType.ACCEPT);
dialog.connect('response', function (res) {
if (res == Gtk.ResponseType.ACCEPT) {
var filename = dialog.get_filename();
setString("keep-copy-of-aliases-filename", filename);
}
});
dialog.show
});
/*
settings.connect("changed", function (sender, key) {
if (key == RECENT_URL_KEY)
updateListStore(getrecentURL());
});
updateListStore(getrecentURL());
*/
}
}
const pwcalcBuilderScope = GObject.registerClass({
Implements: [Gtk.BuilderScope],
}, class pwcalcBuilderScope extends GObject.Object {
vfunc_create_closure(builder, handlerName, flags, connectObject) {
if (flags & Gtk.BuilderClosureFlags.SWAPPED)
throw new Error('Unsupported template signal flag "swapped"');
if (typeof this[handlerName] === 'undefined')
throw new Error(`${handlerName} is undefined`);
return this[handlerName].bind(connectObject || this);
}
});