forked from AlexPoilrouge/NordVPN-connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subMenus.js
2663 lines (2269 loc) · 74.8 KB
/
subMenus.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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const Atk= imports.gi.Atk;
const St = imports.gi.St;
const PopupMenu = imports.ui.popupMenu;
const Pango = imports.gi.Pango;
const Gtk = imports.gi.Gtk;
const GObject = imports.gi.GObject;
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const ByteArray = imports.byteArray;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const PersistentData = Me.imports.persistentData;
const Convenience = Me.imports.convenience;
const MyUtils= Convenience.MyUtils;
/** Object that will be the access holder to this extension's gSettings */
var SETTINGS;
function init(){
/** Iniating the gSettings access */
SETTINGS = Convenience.getSettings();
}
/** Enumerator for instructions on how locations item should be displayed in a menu:
* Either a 'AVAILABLE_ONLY', a 'DISCRIMINATE_DISPLAY' or a 'SHOW_ALL'
* (according to value of the 'displayMenu' option)
* @readonly
* @enum {number}
*/
const LOCATIONS_DISPLAY_MODE={
AVAILABLE_ONLY: 0,
DISCRIMINATE_DISPLAY: 1,
SHOW_ALL: 2,
};
/** Enumerator for the possible states of item inserted in a potentioal menu:
* Either a 'DEFAULT', a 'UNAVAILABLE' or a 'FORCED'
* (according to their availability in the CLI (or not= default))
* @readonly
* @enum {number}
*/
const LOCATION_ITEM_STATE= {
DEFAULT: 0,
UNAVAILABLE: 1,
FORCED: 2,
};
/** Enumerator for the possible type of item insertable in menus
* Either a 'country', a 'city' or a 'group'
* (according to the possibilities offered by the NordVPN CLI Tool)
* @readonly
* @enum {number}
*/
const LOCATION_TYPE= {
COUNTRY: 0,
CITY: 1,
GROUP: 2,
};
/**
* Class that implements the generic class for 'invisible' submenus.
*
* The menu are meant to be unseeable (no title or space), and only become
* visible when their content unfolds.
* Meant to be a generic class to extend from.
*/
var HiddenSubMenuMenuItemBase = GObject.registerClass(
class HiddenSubMenuMenuItemBase extends PopupMenu.PopupSubMenuMenuItem{
//constructor(){
_init(){
super._init("",false);
/** no title… */
this.actor.remove_child(this.label);
this.actor.remove_child(this._triangleBin);
/** … with no allocated space (no height) */
this.actor.height= 0;
}
/** Destructor
* @method
*/
_onDestroy(){
//destroy(){
super._onDestroy();
}
});
/**
* Class that implements an item to be inserted int the 'PlaceMenu' menu.
*/
var PlaceItem = GObject.registerClass(
{
Signals: {
'group-select-toggled': {
flags: GObject.SignalFlags.RUN_FIRST,
param_types: [ GObject.TYPE_STRING, GObject.TYPE_BOOLEAN]
}
}
},
class PlaceItem extends PopupMenu.PopupBaseMenuItem{
/**
* Constructor, also initializes the UI for the item
*
* @param {string} str_Place name of location
* @param {enum} type the type of location (city, country, group)
* @param {enum} state current state of location (unavailable, force, default)
*/
_init(str_Place, type=LOCATION_TYPE.COUNTRY, state=LOCATION_ITEM_STATE.DEFAULT){
//constructor(str_Place, type=LOCATION_TYPE.COUNTRY){
super._init();
/** the actual place name stored by this item
* i.e.: the actual string to be used when calling the 'connect' command of the CLI Tool
*/
this.placeName= str_Place;
/** type of item (according to LOCATION_TYPE)*/
this.type= type;
/** the state of display of the item (according to LOCATION_ITEM_STATE) */
this.state= state;
this.checkIcon = new St.Icon({ icon_name: 'network-vpn-symbolic',
style_class: 'countries_submenu_label_item_selected_icon' });
this.actor.add(this.checkIcon);
let label_item= new St.Label({
style_class: ((this.type===LOCATION_TYPE.GROUP)?
'groups-submenu-label-item'
:'countries-submenu-label-item')
+ ((this.state===LOCATION_ITEM_STATE.UNAVAILABLE)?
' submenu-label-state-unavailable'
: (this.state===LOCATION_ITEM_STATE.FORCED)?
' submenu-label-state-forced'
: ''),
text: this.placeName.replace(/_/gi,' ')}
);
label_item.x_expand= true;
this.actor.add(label_item);
this.checkIcon.hide();
this._groupSelect=false;
this._idc1= null;
this._button= null;
if(type===LOCATION_TYPE.GROUP){
let btnIcon = new St.Icon({ icon_name: 'emoji-flags-symbolic',
style_class: 'system-status-icon nvpn-submenu-icon' });
this._button= new St.Button({
child: btnIcon,
reactive: true,
can_focus: true,
track_hover: true,
style_class: 'system-menu-action fav-delete-btn',
});
this._statusBin = new St.Bin({ x_align: St.Align.END, });
this.actor.add(this._statusBin)
this._statusBin.child= this._button;
this._idc1= this._button.connect('clicked', () => {
this._groupSelect= (type===LOCATION_TYPE.GROUP) && (!this._groupSelect);
if(this._groupSelect){
this.style_class= this.style_class + ' group-selected'
}
else{
this.style_class= this.style_class.replace(/ group-selected/g,'');
}
this.emit('group-select-toggled', this.placeName, this._groupSelect);
})
}
}
/**
* destructor
*/
_onDestroy(){
if(Boolean(this._button) && Boolean(this._idc1)){
this._button.disconnect(this._idc1);
}
super._onDestroy();
}
/**
* method that marks the item (ui) as (un)selected
* @method
* @param {boolean} b - select if ture, unselect otherwise
*/
select(b=true){
if (b){
this.checkIcon.show();
}
else{
this.checkIcon.hide();
}
}
/**
* Getter for the (displayed) text of this item
* @method
* @returns {string} - the text displayed by this item, as a string
*/
get PlaceName(){
return this.placeName;
}
/**
* Getter to determine if group is selected as a co-joint group for connexion
* @method
* @returns {boolean} - whether or not is selected
*/
get isGroupSelected(){
return (this.type===LOCATION_TYPE.GROUP) && this._groupSelect;
}
/**
* Unselect group, as co-joint group for connexion
* @method
*/
unselectGroup(){
this._groupSelect= false;
if(Boolean(this.style_class))
this.style_class= this.style_class.replace(/ group-selected/g,'');
}
});
/**
* Class that implements the submenu use to pick a server by clicking
* on a country name
*/
var LocationsMenu = GObject.registerClass(
class LocationsMenu extends HiddenSubMenuMenuItemBase{
/**
* Initiate the attributes needed to maintain this menu
* @method
*/
//constructor(){
_init(){
super._init();
/** this attribute will be use to store the 'callback' function
* that will be called whenever an item (i.e. country) of this submenu
* is selected */
this.select_cb= null;
/** attribute used to point on the item that is currently 'selected' */
this.cur_selected=null;
/** this attribute is a list that will be used to store the 'ids' generated
* when a signal connection is made with an item (in private method 'add_place()')
* so that these ids may be reused later to handle all the signal disonnections for
* all the items during this menu's destruction */
this._ids_c_items= [];
let separator= new PopupMenu.PopupSeparatorMenuItem();
this.menu.addMenuItem(separator);
this._groupSelected= null;
}
/**
* Destructor. Makes sure to disconnect all the signal connection made
* for all the added items
* @method
*/
_onDestroy(){
//destroy(){
let children= this.menu._getMenuItems();
let diff= 0;
for(let i=0; i<this.menu.length; ++i){
let item= children[i];
if( (item instanceof PlaceItem) && (item!==undefined) ){
item.disconnect(this._ids_c_items[i-diff]);
}
else{
++diff;
}
}
super._onDestroy();
}
/**
* Method to add a new location as an item to this menu
* @method
* @param {string} str_Place - the displayed name of the item (i.e. the country's name)
* @param {enum} type - type of location. Can be a LOCATION_TYPE.COUNTRY or a
* LOCATION_TYPE.COUNTRY.GROUP
* @param {enum} state - state of location. Unavailabe, force, or default
*/
add_place(str_Place, type=LOCATION_TYPE.COUNTRY, state=LOCATION_ITEM_STATE.DEFAULT){
/** creating the new item as an instance of 'PlaceItem' class */
let item= new PlaceItem(str_Place, type, state);
/** adding this item to the menu */
this.menu.addMenuItem(item,
(type===LOCATION_TYPE.GROUP)?0:undefined
);
/** setting the callback for when our item is click by the user, while not forgetting
* to store the 'connect id' for a later disconnection before destruction */
let t= this;
this._ids_c_items.push(
item.connect('activate', this._item_select.bind(this,item))
);
this._ids_c_items.push(
item.connect('group-select-toggled', this._group_select_toggle.bind(this))
);
}
/**
* Method to set the function that will be used as callback when an item is clicked.
* @param {function} func - a function respecting this signature: void func(string)
* during the callbakc, the parameter passed to func will be the clicked item text
* (i.e. country name) passed as a string
*/
select_callback(func= null){
this.select_cb= func;
}
/** If there is a currently selected group, as co-joint group for connexion,
* then unselects it
* @method
*/
unselect_current(){
if(this.cur_selected!=null){
this.cur_selected.select(false);
}
}
/**
* Private method that is called when an item is click.
* When the item is clicked, a callback to this method is made, with the said item
* passed as argument. The method makes the necessary ui update and the menu's data
* required adjustments, before calling the real callback function (pointed via the
* attribute 'select_cb').
* @method
*/
_item_select(item){
/** if there is an item currently selected (data), unselect it (ui) */
this.unselect_current();
/** if the item clicked is the one currently selected (data), then it is deselected (ui & data) */
if(item===this.cur_selected){
this.cur_selected= null;
item.select(false);
}
/** otherwise, if the clicked is different from the one currently selected (data),
* make it the currently selected item (data) and select it (ui) */
else{
this.cur_selected= item;
item.select(item.type!==LOCATION_TYPE.GROUP);
}
/** make the requested call back (function pointed by attribute 'select_db'),
* with the currently selected item as string argument (empty string if nothing
* currently selected */
this.select_cb(this.LastSelectedPlaceName);
}
/**
* Private method that toggles a given item, as a co-joint location for connexion
* attempt.
*
* @param {PlaceItem} item - the item on which to apply toggling
* @param {string} placeName - uhhhh
* @param {boolean} toggle - the toggle, to select or unselect
*/
_group_select_toggle(item, placeName, toggle){
if(Boolean(this._groupSelected) &&
!(toggle && item===this._groupSelected))
{
this._groupSelected.unselectGroup();
}
this._groupSelected= toggle?item:null;
}
/**
* Private method to unselects the currently selected group, for co-joint connexion attempt,
* is any.
* @method
*/
unselectGroup(){
if(Boolean(this._groupSelected)){
this._groupSelected.unselectGroup();
}
this._groupSelected= null;
}
/**
* Getter to the currenctly selected, for co-joint connexion attemps, group, if any
* @method
*
* @returns {string} the location or name of the currently selected item
*/
get SelectedGroupName(){
return Boolean(this._groupSelected)?this._groupSelected.PlaceName:"";
}
/**
* Getter to access the currently selected item in this country menu.
* @method
* @returns {string} coutnry name currently selected, empty string if nothing selected */
get LastSelectedPlaceName(){
if (this.cur_selected!=null){
return this.cur_selected.PlaceName;
}
else {
return "";
}
}
/**
* Method that unselects (ui and data) the currently selected country item
* (if there is one)
* @method
*/
unselect_no_cb(){
if (this.cur_selected!=null){
this.cur_selected.select(false);
this.cur_selected= null;
}
}
/**
* Method that allows to select an item by passing its (diplayed) text/name as argument.
* @method
* @param {string} placeName - the text of the item to be selected (data & ui). Of course it must
* be exactly matching, if no item with matching name in this menu, nothing will be selected.
*/
select_from_name(placeName){
/** browse every item in the menu */
let children= this.menu._getMenuItems();
for(let i=0; i<this.menu.length; ++i){
let item= children[i];
/** if this item text matches the 'placeName' argument,
* it is selected (ui & data) */
if( (item!==undefined) &&
(item instanceof PlaceItem) && (item.PlaceName===placeName) )
{
/** if there already is a current selected item, firstly deselect it */
if(this.cur_select!=null){
this.cur_selected.select(false);
}
item.select();
this.cur_selected= item;
/** can't select multiple items, therefore once a match is met, ends the loop */
break;
}
else if (item===undefined) {
log("[nvpn] Error: got item (n=" + i.toString() + ") undefined looking for \"" + placeName + "\"...");
}
}
}
/** Method to empty LocationMenu of all location items
* @method
*/
clearAllLocations(){
this.unselectGroup();
let children= this.menu._getMenuItems();
for(var i=0; i<children.length; ++i){
let item= children[i];
if( (item!==undefined) && (item instanceof PlaceItem) ){
item.destroy();
}
}
}
});
let StackerBase = GObject.registerClass(
{
},
/**
* Base class to implement 'stackers'.
*
* Used to keep coherent a set of static items, followed
* by some dynamic items.
* To use inside a submenu.
*
* To use as a class parent.
*/
class StackerBase extends GObject.Object{
/**
* Initialization method
*
* @param {string} title the title of the stacker; will be
* displayed as a static label
* @param {PopupMenu.PopupSubMenuMenuItem} submenu the parent submenu
* @param {integer} startPos positive integer that indicates the position
* within the submenu where to insert the stacker
*/
_init(title, submenu, startPos= undefined){
super._init();
this._parentMenu= submenu;
this.ITEM_SIGS= [];
var b= Boolean(startPos);
this._startPos= (b)? startPos
: this._parentMenu.menu._getMenuItems().length;
let label= new St.Label({text: "----- "+title+" -----",});
let hbox= new St.BoxLayout();
this._startItem= new PopupMenu.PopupBaseMenuItem({reactive: false});
hbox.add(label);
hbox.x_expand= true;
hbox.x_fill= true;
hbox.x_align= St.Align.END;
this._startItem.actor.add(hbox);
if(b){
this._parentMenu.menu.addMenuItem(this._startItem);
}
else{
this._parentMenu.menu.addMenuItem(this._startItem, startPos);
}
this.ITEM_SIGS.push([this._startItem, this._startItem.connect('destroy', () => {
--this._sl;
if(this._sl>0){
this._startItem= this._parentMenu.menu._getMenuItems()[this._startPos+1];
}
else{
this._startItem= null;
}
})]);
this._sl= 1;
}
/**
* Called on destruction
*/
_onDestroy(){
for(var i= 0; i<this.ITEM_SIGS.length; ++i){
let t= this.ITEM_SIGS[i];
if(t && t[0] && t[1]){
t[0].disconnect(t[1]);
}
}
super._onDestroy();
}
/**
* Private method that recomputes the index of the first item of the stacker
* relatively to the parent submenu
*/
_computeStartPos(){
if(Boolean(this._startItem)){
var tmp= this._parentMenu.menu._getMenuItems().indexOf(this._startItem);
if(tmp>=0){
this._startPos= tmp;
}
}
}
/**
* Getter property to access the current (after acutalization) index of the stacker
* within th eparent submenu
*/
get actualizedStartPos(){
this._computeStartPos();
return this._startPos;
}
/**
* Add a static item to the stacker
*
* @param {PopupSubMenuItem} item an item to add as the static part of the stacker
*/
addNonDynamicFrontItem(item){
if(Boolean(item)){
this._parentMenu.menu.addMenuItem(item, this._startPos+this._sl);
++this._sl;
this.ITEM_SIGS.push([item, item.connect('destroy', () => {
--this._sl;
})]);
}
}
/**
* Getter property to access the current (after acutalization) index of the first
* dynamic item within the stacker
*/
get acutalizedDynamicItemStartPos(){ return (this.actualizedStartPos+this._sl)}
});
/**
* Class that implements faved server as a GUI menu item
*/
let FavedServerItem = GObject.registerClass(
{
Signals: {
'delete-fav': {
flags: GObject.SignalFlags.RUN_FIRST,
param_types: [ GObject.TYPE_STRING ]
}
}
},
class FavedServerItem extends PopupMenu.PopupBaseMenuItem{
/**
* Constructor
*
* @param {string} servName the faved server name
* @param {string} infos infos about server
*/
//constructor(servName, infos){
_init(servName, infos){
super._init({style_class: 'server-fav',});
this.tLabel= new St.Label({text: servName+" - "+infos.slice(0,13)+((infos.length>13)?'…':'')});
this.tLabel.x_expand= true;
this.actor.add(this.tLabel);
this._servName= servName;
this._infos= infos;
let btnIcon = new St.Icon({ icon_name: 'edit-delete-symbolic',
style_class: 'system-status-icon nvpn-submenu-icon' });
this._button= new St.Button({
child: btnIcon,
reactive: true,
can_focus: true,
track_hover: true,
style_class: 'system-menu-action fav-delete-btn',
});
this._statusBin = new St.Bin({ x_align: St.Align.END, });
this.actor.add(this._statusBin);
this._statusBin.child= this._button;
/** signal 'delete-fav' is emmited when the delete button is clicked */
this._idc1= this._button.connect('clicked', () => {
this.emit('delete-fav', this._servName);
})
}
/**
* destructor
*/
//destroy(){
_onDestroy(){
this._button.disconnect(this._idc1);
super._onDestroy();
}
get key(){
return this._servName;
}
get infos(){
return this._infos;
}
set infos(infos){
this._infos= infos;
this.tLabel.text= this._servName+" - "+this._infos;
}
});
/** Regisering this item as a GObject in order
* to use signals via the 'emit' method
*/
let FavoriteStacker = GObject.registerClass(
{
Signals: {
'server-fav-connect': {
flags: GObject.SignalFlags.RUN_FIRST,
param_types: [ GObject.TYPE_STRING ]
}
}
},
/**
* Class that implements the part of the gui menu
* where the faved servers are displayed
*/
class FavoriteStacker extends StackerBase{
/**
*
* @param {object} submenu the parent submenu
* @param {string} persistentDataHandler the object that implement the handling of
* persitent data.
*
* Note that the data within persistentDataHandler should be loaded prior to this
* object creation
*/
_init(submenu, persistentDataHandler){
super._init("Favorite servers",submenu);
this.FAV_SIGS= [];
this._currentServInfo= {server: "", city: "", country: ""};
/** button to add current server as favourite */
let btnLabel= new St.Label({text: "Fav' current server",});
this._button= new St.Button({
child: btnLabel,
reactive: true,
can_focus: true,
track_hover: true,
style_class: 'system-menu-action add-fav-button',
});
this._button.x_align= St.Align.END;
this._button.set_toggle_mode(true);
/** hidden by default */
this._button.hide();
let btnItem= new PopupMenu.PopupBaseMenuItem({reactive: false});
this._button.x_expand= true;
this._button.x_fill= false;
btnItem.actor.add(this._button);
this.addNonDynamicFrontItem(btnItem);
/** loading from disk and handling the favs */
this._favHandler= new PersistentData.FavHandler(persistentDataHandler);
//this._favHandler.load();
/** fill menu with existing favs */
this._generateItemList();
/** when 'add favourite' button is clicked,
* add the server (with its infos) as a fav
*/
this._idc1= this._button.connect('clicked', () => {
let serv= this._currentServInfo;
if(serv && serv.server){
if(!this._favHandler.isFaved(serv.server)){
this._favHandler.add(serv.server, serv.city, serv.country);
this._favHandler.save();
this._addFavItem(serv.server, serv.city+", "+serv.country);
/** only to update the display state of the button
* (if the current serv is added, it should diseappear)*/
this.currentServer= serv;
}
else{
let r= this._parentMenu.menu._getMenuItems().find((item)=>{
if(item instanceof FavedServerItem){
return (item instanceof FavedServerItem) &&
item.key===serv.server;
}
});
if(r){
r.infos= "????, ????";
}
}
}
});
}
/**
* Called on destruction
*
* cleans signals
*/
_onDestroy(){
for(var i= 0; i<this.FAV_SIGS.length; ++i){
let t= this.FAV_SIGS[i];
if(t && t[0] && t[1]){
t[0].disconnect(t[1]);
}
}
if(this._idc1){
this._button.disconnect(this._idc1);
this._idc1= 0;
}
super._onDestroy();
}
/**
* Private method that adds all loaded & stored faved servers as gui items
*/
_generateItemList(){
if(this._favHandler){
for(var t=this._favHandler.first(); t!==undefined; t=this._favHandler.next()){
this._addFavItem(t[0], t[1]);
}
}
}
/**
* Private method that adds a faved server as a gui item
*
* @param {string} serv the server name
* @param {string} info the server infos
*/
_addFavItem(serv, info){
let favItem= new FavedServerItem(serv, info);
var disp= this.acutalizedDynamicItemStartPos;
this._parentMenu.menu.addMenuItem(favItem, disp);
/** whenever a fav server's menu item is clicked,
* the signal 'server-fav-connect' is emitted
*/
this.FAV_SIGS.push([favItem, favItem.connect('activate', () => {
this.emit('server-fav-connect', favItem.key);
})]);
/** whenever a fav server's menu item's delete button is clicked,
* it is removed from the fav data handler (changes written on disk)
* the menu item is removed and its signal disconnected
*/
this.FAV_SIGS.push([favItem, favItem.connect('delete-fav', (item, servName) => {
this._favHandler.remove(servName);
this._favHandler.save();
var i= -1;
do{
i= this.FAV_SIGS.findIndex((e) => {
return e[0]==item;
});
if(i>=0){
let elmt= this.FAV_SIGS[i];
if(elmt && elmt[0] && elmt[1]){
elmt[0].disconnect(elmt[1]);
}
this.FAV_SIGS[i]= undefined;
this.FAV_SIGS= this.FAV_SIGS.slice(0,i).concat(
(this.FAV_SIGS.length>(i+1))?
this.FAV_SIGS.slice((i+1))
: []
);
}
}
while(i>=0);
item.destroy();
/** only to update the display state of the button
* (if the current serv was fav, and has been delstyle_classete, it should reappear)*/
this.currentServer= this._currentServInfo;
})]);
}
/**
* accessor (write) to change the current server.
* if this server is already faved, we hide the 'add as favourite' button
* that becomes, de facto, redundant
*
* @param {object} serverInfo object that contains a 'server' field, for the server name
* (and a 'infos' field)
*/
set currentServer(serverInfo){
if(this._favHandler.isFaved(serverInfo.server) || !this._currentServInfo){
this._button.hide();
}
else{
this._button.show();
}
this._currentServInfo= serverInfo;
}
get currentServer(){
return this._currentServInfo;
}
}
);
/**
* Class that implements a recent location as a GUI menu item
*/
var RecentLocationItem = GObject.registerClass(
{
Signals: {
'pin': {
flags: GObject.SignalFlags.RUN_FIRST,
param_types: [ GObject.TYPE_STRING ]
},
'unpin': {
flags: GObject.SignalFlags.RUN_FIRST,
param_types: [ GObject.TYPE_STRING ]
}
}
},
class RecentLocationItem extends PopupMenu.PopupBaseMenuItem{
/**
* Constructor
*
* @param {string} location the location
* @param {boolean} isPin whether or not the location is 'pinned'
*/
//constructor(location, isPin){
_init(location, isPin){
super._init({style_class: 'recent-location'});
this.tLabel= new St.Label({
style_class: 'recent-location-label' +((isPin)?' pinned':''),
text: location.replace(/_/gi,' ')
});
this.tLabel.x_expand= true;
this.actor.add(this.tLabel);
this._location= location;
this._pin= isPin;
let btnIcon = new St.Icon({ icon_name: (isPin)?'zoom-out-symbolic':'view-pin-symbolic',
style_class: 'system-status-icon nvpn-submenu-icon' });
this._button= new St.Button({
child: btnIcon,
reactive: true,
can_focus: true,
track_hover: true,
style_class: 'system-menu-action pin-btn',
});
this._statusBin = new St.Bin({ x_align: St.Align.MIDDLE, });
this.actor.add(this._statusBin);
this._statusBin.child= this._button;
/** signal 'unpin'/'pin' is emmited when the pin button is clicked */
this._idc1= this._button.connect('clicked', () => {
this.emit(((isPin)?'unpin':'pin'), location);
})
}
get isPin(){ return this._pin;}
get location(){ return this._location;}
set location(l){
this._location= l;
/** also updating the displayed location, ofc */
this.tLabel.text= l.replace(/_/gi,' ')
}
/** Method to update style of displayed item according to a given state
*
* @param {enum} state - style from state to apply to item - forced, unavailable or default
*/
updateStyle(state){
this.tLabel.style_class=
'recent-location-label' +
((this.isPin)?' pinned':'') +
((state===LOCATION_ITEM_STATE.FORCED)?
' submenu-label-state-forced' :
'' ) +
((state===LOCATION_ITEM_STATE.UNAVAILABLE)?
' submenu-label-state-unavailable':
'' )
;
}
});
let RecentLocationStacker = GObject.registerClass(
{
Signals: {
'location-connect': {
flags: GObject.SignalFlags.RUN_FIRST,
param_types: [ GObject.TYPE_STRING ]
}
}
},
/**
* Class that implements the part of the gui menu
* where the faved servers are displayed
*/
class RecentLocationStacker extends StackerBase{
/**
*
* @param {PopupMenu.PopupSubMenuMenuItem} submenu the parent submenu
* @param {PersistentDataHandler} persistentDataHandler the instance of the
* persistentdatahandler that handles the data storage
* @param {integer} capacity positive integer that represents how many locations