-
Notifications
You must be signed in to change notification settings - Fork 8
/
prefs.js
681 lines (516 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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
/*
* walNUT: A Gnome Shell Extension for NUT (Network UPS Tools)
*
* Copyright (C)
* 2013 Daniele Pezzini <[email protected]>
* Based on prefs.js from gnome-shell-extensions-mediaplayer - Copyright (C)
* 2011-2013 Jean-Philippe Braun <[email protected]>
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
const Gtk = imports.gi.Gtk,
GObject = imports.gi.GObject;
// Gettext
const Gettext = imports.gettext.domain('gnome-shell-extensions-walnut'),
_ = Gettext.gettext;
const Me = imports.misc.extensionUtils.getCurrentExtension(),
Convenience = Me.imports.convenience;
// Settings
let gsettings, // Stored settings
settings_general, // General settings
settings_panel, // Panel settings
settings_menu, // Menu settings
settings_cmd_desc; // Children settings of 'device commands' option
// Children boxes of 'device commands' option
let cmd_desc_box;
function init() {
Convenience.initTranslations();
gsettings = Convenience.getSettings();
// General settings
settings_general = {
// Update time chooser
update_time: {
type: 'r',
// TRANSLATORS: Label of setting @ preferences
label: _("Seconds before next update"),
// TRANSLATORS: Hint text of setting @ preferences
help: _("The seconds after walNUT updates the data from the device. (default: 15)"),
min: 5,
max: 100,
step: 1,
default: 15
},
// Temperature unit
temp_unit: {
type: 'es',
// TRANSLATORS: Label of setting @ preferences
label: _("Temperature unit"),
// TRANSLATORS: Hint text of setting @ preferences
help: _("The unit (Centigrade or Fahrenheit) walNUT should display the temperature in. (default: Centigrade)"),
list: [
{
nick: 'Centigrade',
// TRANSLATORS: Temperature unit @ preferences
name: _("Centigrade"),
id: 0
},
{
nick: 'Fahrenheit',
// TRANSLATORS: Temperature unit @ preferences
name: _("Fahrenheit"),
id: 1
}
]
}
};
// Panel settings
settings_panel = {
// Panel button options
panel_icon_display_load: {
type: 'b',
// TRANSLATORS: Label of setting @ preferences
label: _("Display load in the icon"),
// TRANSLATORS: Hint text of setting @ preferences
help: _("Whether the device load should be displayed in panel icon or not. (default: OFF)")
},
panel_text_display_load: {
type: 'b',
// TRANSLATORS: Label of setting @ preferences
label: _("Display load in the label"),
// TRANSLATORS: Hint text of setting @ preferences
help: _("Whether the device load should be displayed in panel label or not. (default: OFF)")
},
panel_text_display_charge: {
type: 'b',
// TRANSLATORS: Label of setting @ preferences
label: _("Display charge in the label"),
// TRANSLATORS: Hint text of setting @ preferences
help: _("Whether the battery charge should be displayed in panel label or not. (default: OFF)")
}
};
// Menu settings
settings_menu = {
// Device List Options
display_na: {
type: 'b',
// TRANSLATORS: Label of setting @ preferences
label: _("Display not available devices"),
// TRANSLATORS: Hint text of setting @ preferences
help: _("Display also not available devices in the submenu in panel menu (chosen device will be always displayed, also if not available, in spite of this option). (default: OFF)")
},
// Device model
display_device_model: {
type: 'b',
// TRANSLATORS: Label of setting @ preferences
label: _("Display device model"),
// TRANSLATORS: Hint text of setting @ preferences
help: _("Show also device model ('manufacturer - model'), if available, in the panel menu. (default: ON)")
},
// Data table options
display_battery_charge: {
type: 'b',
// TRANSLATORS: Label of setting @ preferences
label: _("Display battery charge"),
// TRANSLATORS: Hint text of setting @ preferences
help: _("Show also battery charge, if available, in the panel menu. (default: ON)")
},
display_load_level: {
type: 'b',
// TRANSLATORS: Label of setting @ preferences
label: _("Display load level"),
// TRANSLATORS: Hint text of setting @ preferences
help: _("Show also load level, if available, in the panel menu. (default: ON)")
},
display_backup_time: {
type: 'b',
// TRANSLATORS: Label of setting @ preferences
label: _("Display backup time"),
// TRANSLATORS: Hint text of setting @ preferences
help: _("Show also backup time, if available, in the panel menu. (default: ON)")
},
display_device_temperature: {
type: 'b',
// TRANSLATORS: Label of setting @ preferences
label: _("Display device temperature"),
// TRANSLATORS: Hint text of setting @ preferences
help: _("Show also device temperature, if available, in the panel menu. (default: ON)")
},
// Raw Data
display_raw: {
type: 'b',
// TRANSLATORS: Label of setting @ preferences
label: _("Display raw data"),
// TRANSLATORS: Hint text of setting @ preferences
help: _("Show also raw data in a submenu. (default: OFF)")
},
// UPS Commands
display_cmd: {
type: 'b',
// TRANSLATORS: Label of setting @ preferences
label: _("Display device commands"),
// TRANSLATORS: Hint text of setting @ preferences
help: _("Display available device commands. Requires upsd user and password to execute them. (default: OFF)"),
// cb: callback function -> set children sensitivity
cb: function(status) {
cmd_desc_box.set_sensitive(status);
}
},
// Credentials Box Options
hide_pw: {
type: 'b',
// TRANSLATORS: Label of setting @ preferences
label: _("Hide password at credentials box"),
// TRANSLATORS: Hint text of setting @ preferences
help: _("Whether the password at credentials box should be hidden or not. (default: ON)")
}
};
// Child setting of 'device commands' option
settings_cmd_desc = {
display_cmd_desc: {
type: 'b',
// TRANSLATORS: Label of setting @ preferences
label: _("Display description of device commands"),
// TRANSLATORS: Hint text of setting @ preferences
help: _("Display also a localized description of available device commands in the submenu. (default: ON)"),
}
};
}
// General/Panel settings
const walNUTPrefsGeneral = new GObject.Class({
Name: 'walNUTPrefsGeneral',
GTypeName: 'walNUTPrefsGeneral',
Extends: Gtk.Box,
_init: function() {
this.parent({
orientation: Gtk.Orientation.VERTICAL,
margin: 10,
spacing: 10
});
// TRANSLATORS: Tab's label @ preferences widget
this.label = new Gtk.Label({ label: _("General/Panel") });
// Horizontal boxes
let hbox;
// General options
let general = new Gtk.Box({
orientation: Gtk.Orientation.VERTICAL,
margin_bottom: 10,
margin_left: 10,
margin_right: 10,
margin_top: 0,
spacing: 5
});
for (let setting in settings_general) {
hbox = buildHbox(settings_general, setting);
general.add(hbox);
}
// TRANSLATORS: Label @ preferences widget
let generalFrame = new Gtk.Frame({ label: _("General options") });
generalFrame.add(general);
this.add(generalFrame);
// Panel options
let panel = new Gtk.Box({
orientation: Gtk.Orientation.VERTICAL,
margin_bottom: 10,
margin_left: 10,
margin_right: 10,
margin_top: 0,
spacing: 5
});
for (let setting in settings_panel) {
hbox = buildHbox(settings_panel, setting);
panel.add(hbox);
}
// TRANSLATORS: Label @ preferences widget
let panelFrame = new Gtk.Frame({ label: _("Panel options") });
panelFrame.add(panel);
this.add(panelFrame);
}
});
// Menu settings
const walNUTPrefsMenu = new GObject.Class({
Name: 'walNUTPrefsMenu',
GTypeName: 'walNUTPrefsMenu',
Extends: Gtk.Box,
_init: function() {
this.parent({
orientation: Gtk.Orientation.VERTICAL,
margin: 10,
spacing: 5
});
// TRANSLATORS: Tab's label @ preferences widget
this.label = new Gtk.Label({ label: _("Menu") });
// Horizontal boxes
let hbox;
cmd_desc_box = new Gtk.VBox();
if (cmd_desc_box.get_direction() == Gtk.TextDirection.RTL)
cmd_desc_box.margin_right = 30;
else
cmd_desc_box.margin_left = 30;
// Child setting of 'device commands' option
for (let setting in settings_cmd_desc) {
hbox = buildHbox(settings_cmd_desc, setting);
cmd_desc_box.add(hbox);
}
// Child's sensitivity
cmd_desc_box.set_sensitive(gsettings.get_boolean('display-cmd'));
// All other settings
for (let setting in settings_menu) {
hbox = buildHbox(settings_menu, setting);
this.add(hbox);
if (setting == 'display_cmd') {
this.add(cmd_desc_box);
}
}
}
});
// Preferences widget
const walNUTPrefsWidget = new GObject.Class({
Name: 'walNUTPrefsWidget',
GTypeName: 'walNUTPrefsWidget',
Extends: Gtk.Box,
_init: function() {
this.parent();
let notebook = new Gtk.Notebook({
margin_left: 5,
margin_top: 5,
margin_bottom: 5,
margin_right: 5,
expand: true
});
let general = new walNUTPrefsGeneral();
let menu = new walNUTPrefsMenu();
notebook.append_page(general, general.label);
notebook.append_page(menu, menu.label);
this.add(notebook);
}
});
// Build preferences widget
function buildPrefsWidget() {
let preferences = new walNUTPrefsWidget();
preferences.show_all();
return preferences;
}
// Build horizontal box for each setting
function buildHbox(settings, setting) {
let hbox;
if (settings[setting].type == 's')
hbox = createStringSetting(settings, setting);
if (settings[setting].type == 'i')
hbox = createIntSetting(settings, setting);
if (settings[setting].type == 'b')
hbox = createBoolSetting(settings, setting);
if (settings[setting].type == 'r')
hbox = createRangeSetting(settings, setting);
if (settings[setting].type == 'e')
hbox = createEnumSetting(settings, setting);
if (settings[setting].type == 'es')
hbox = createEnumStringSetting(settings, setting);
return hbox;
}
// Enum setting from string setting
function createEnumStringSetting(settings, setting) {
let hbox = new Gtk.Box({ orientation: Gtk.Orientation.HORIZONTAL });
// Label
let setting_label = new Gtk.Label({
label: settings[setting].label,
xalign: 0
});
// Combobox
let model = new Gtk.ListStore();
model.set_column_types([ GObject.TYPE_STRING, GObject.TYPE_STRING ]);
let setting_enum = new Gtk.ComboBox({ model: model });
setting_enum.get_style_context().add_class(Gtk.STYLE_CLASS_RAISED);
let renderer = new Gtk.CellRendererText();
setting_enum.pack_start(renderer, true);
setting_enum.add_attribute(renderer, 'text', 1);
for (let i = 0; i < settings[setting].list.length; i++) {
let item = settings[setting].list[i];
let iter = model.append();
model.set(iter, [ 0, 1 ], [ item.nick, item.name ]);
if (item.nick == gsettings.get_string(setting.replace(/_/g, '-'))) {
setting_enum.set_active(item.id);
}
}
setting_enum.connect('changed', function(entry) {
let [ success, iter ] = setting_enum.get_active_iter();
if (!success)
return;
let nick = model.get_value(iter, 0)
gsettings.set_string(setting.replace(/_/g, '-'), nick);
});
// Tip
if (settings[setting].help) {
setting_label.set_tooltip_text(settings[setting].help)
setting_enum.set_tooltip_text(settings[setting].help)
}
hbox.pack_start(setting_label, true, true, 0);
hbox.add(setting_enum);
return hbox;
}
// Enum setting
function createEnumSetting(settings, setting) {
let hbox = new Gtk.Box({
orientation: Gtk.Orientation.HORIZONTAL,
margin_top: 5
});
// Label
let setting_label = new Gtk.Label({
label: settings[setting].label,
xalign: 0
});
// Combobox
let model = new Gtk.ListStore();
model.set_column_types([ GObject.TYPE_INT, GObject.TYPE_STRING ]);
let setting_enum = new Gtk.ComboBox({ model: model });
setting_enum.get_style_context().add_class(Gtk.STYLE_CLASS_RAISED);
let renderer = new Gtk.CellRendererText();
setting_enum.pack_start(renderer, true);
setting_enum.add_attribute(renderer, 'text', 1);
for (let i = 0; i < settings[setting].list.length; i++) {
let item = settings[setting].list[i];
let iter = model.append();
model.set(iter, [ 0, 1 ], [ item.id, item.name ]);
if (item.id == gsettings.get_enum(setting.replace(/_/g, '-'))) {
setting_enum.set_active(item.id);
}
}
setting_enum.connect('changed', function(entry) {
let [ success, iter ] = setting_enum.get_active_iter();
if (!success)
return;
let id = model.get_value(iter, 0)
gsettings.set_enum(setting.replace(/_/g, '-'), id);
});
// Tip
if (settings[setting].help) {
setting_label.set_tooltip_text(settings[setting].help)
setting_enum.set_tooltip_text(settings[setting].help)
}
hbox.pack_start(setting_label, true, true, 0);
hbox.add(setting_enum);
return hbox;
}
// String setting
function createStringSetting(settings, setting) {
let hbox = new Gtk.Box({
orientation: Gtk.Orientation.HORIZONTAL,
margin_top: 0
});
// Label
let setting_label = new Gtk.Label({
label: settings[setting].label,
xalign: 0
});
// Entry
let setting_string = new Gtk.Entry({ text: gsettings.get_string(setting.replace(/_/g, '-')) });
setting_string.set_width_chars(30);
setting_string.connect('notify::text', function(entry) {
gsettings.set_string(setting.replace(/_/g, '-'), entry.text);
});
if (settings[setting].mode == 'passwd') {
setting_string.set_visibility(false);
}
// Tip
if (settings[setting].help) {
setting_label.set_tooltip_text(settings[setting].help)
setting_string.set_tooltip_text(settings[setting].help)
}
hbox.pack_start(setting_label, true, true, 0);
hbox.add(setting_string);
return hbox;
}
// Integer setting
function createIntSetting(settings, setting) {
let hbox = new Gtk.Box({ orientation: Gtk.Orientation.HORIZONTAL });
// Label
let setting_label = new Gtk.Label({
label: settings[setting].label,
xalign: 0
});
// Numbers
let adjustment = new Gtk.Adjustment({
lower: 1,
upper: 65535,
step_increment: 1
});
let setting_int = new Gtk.SpinButton({
adjustment: adjustment,
snap_to_ticks: true
});
setting_int.set_value(gsettings.get_int(setting.replace(/_/g, '-')));
setting_int.connect('value-changed', function(entry) {
gsettings.set_int(setting.replace(/_/g, '-'), entry.value);
});
// Tip
if (settings[setting].help) {
setting_label.set_tooltip_text(settings[setting].help)
setting_int.set_tooltip_text(settings[setting].help)
}
hbox.pack_start(setting_label, true, true, 0);
hbox.add(setting_int);
return hbox;
}
// Boolean setting
function createBoolSetting(settings, setting) {
let hbox = new Gtk.Box({ orientation: Gtk.Orientation.HORIZONTAL });
// Label
let setting_label = new Gtk.Label({
label: settings[setting].label,
xalign: 0
});
// Switch
let setting_switch = new Gtk.Switch({ active: gsettings.get_boolean(setting.replace(/_/g, '-')) });
setting_switch.connect('notify::active', function(button) {
gsettings.set_boolean(setting.replace(/_/g, '-'), button.active);
// Callback function
if (settings[setting].cb)
settings[setting].cb(button.active)
});
// Tip
if (settings[setting].help) {
setting_label.set_tooltip_text(settings[setting].help)
setting_switch.set_tooltip_text(settings[setting].help)
}
hbox.pack_start(setting_label, true, true, 0);
hbox.add(setting_switch);
return hbox;
}
// Range setting
function createRangeSetting(settings, setting) {
let hbox = new Gtk.Box({ orientation: Gtk.Orientation.HORIZONTAL });
// Label
let setting_label = new Gtk.Label({
label: settings[setting].label,
xalign: 0
});
// Scale
let setting_range = Gtk.HScale.new_with_range(settings[setting].min, settings[setting].max, settings[setting].step);
setting_range.set_value(gsettings.get_int(setting.replace(/_/g, '-')));
// without numbers
//setting_range.set_draw_value(false);
setting_range.add_mark(settings[setting].default, Gtk.PositionType.BOTTOM, null);
setting_range.set_size_request(200, -1);
setting_range.connect('value-changed', function(slider) {
gsettings.set_int(setting.replace(/_/g, '-'), slider.get_value());
});
// Tip
if (settings[setting].help) {
setting_label.set_tooltip_text(settings[setting].help)
setting_range.set_tooltip_text(settings[setting].help)
}
hbox.pack_start(setting_label, true, true, 0);
hbox.add(setting_range);
return hbox;
}