Skip to content

Commit

Permalink
Cleans the code, additional commenting. Preparing the commit that int…
Browse files Browse the repository at this point in the history
…roduces the 'gSettings' and the extension's settings
  • Loading branch information
AlexPoilrouge committed Apr 18, 2019
1 parent b8654a4 commit aabdf8d
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 60 deletions.
92 changes: 45 additions & 47 deletions extension.js
Expand Up @@ -58,7 +58,7 @@ const Country_Dict= {
al: "Albania", de: "Germany", pl: "Poland",
ar: "Argentina", gr: "Greece", pt: "Portugal",
au: "Australia", hk: "Hong_Kong", ro: "Romania",
at: "Austria", hu: "Hungary", ru: "Russia",
at: "Austria", hu: "Hungary", //ru: "Russia", (russia no longer availabe due to governmental reasons)
az: "Azerbaijan", is: "Iceland", rs: "Serbia",
be: "Belgium", in: "India", sg: "Singapore",
ba: "Bosnia_And_Herzegovina", id: "Indonesia", sk: "Slovakia",
Expand Down Expand Up @@ -96,7 +96,6 @@ class PlaceItem extends PopupMenu.PopupBaseMenuItem{

let label_item= new St.Label({style_class: 'countries_submenu_label_item', text: this.placeName});
this.actor.add(label_item);
// log('[nvpn] creating placeitem for ' + str_Place);
this.checkIcon.hide();
}

Expand Down Expand Up @@ -209,7 +208,6 @@ class PlacesMenu extends PopupMenu.PopupSubMenuMenuItem{
* @method
*/
_item_select(item){
// log("[nvpn] item{pn="+item.PlaceName+"}.select("+(item==this.cur_selected).toString()+")");
/** if there is an item currently selected (data), unselect it (ui) */
if(this.cur_selected!=null){
this.cur_selected.select(false);
Expand Down Expand Up @@ -290,16 +288,36 @@ class PlacesMenu extends PopupMenu.PopupSubMenuMenuItem{
};


/** Object that will be the access holder to this extension's gSettings */
var SETTINGS;

/**
* Class that loads the core commands of this extension that are stored in the gSettings
* It connects signals to allow any exterior change on them to take effect.
**/
class Core_CMDs{
/**
*
* @param {*} parent Parent is use to directly connect this class with the instance
* of NVPNMenu using this instance of Core_CMDs (direct callbacks)
*/
constructor(parent=null){
// Store the id of the signal connections for disarding
this.SETT_SIGS= [];
this._parent= parent;
}

/**
* Initiate all the values from the gSettings, and connect signals
*
* 'SETTINGS' variable must be correctly intiated
*/
init(){
let txt= "";
/** Here the 'Unescape.convert()' method is used since, in string read from gSettings',
* special characters don't seem to be interpreted. This helps sets things right (hopefully)
* if need be.
*/
this.tool_available= (txt=Unescape.convert(SETTINGS.get_string("cmd-tool-available")))?
txt : "hash nordvpn";
this.tool_connected_check= (txt=Unescape.convert(SETTINGS.get_string("cmd-tool-connected-check")))?
Expand All @@ -321,17 +339,6 @@ class Core_CMDs{
this.vpn_online_check= (txt=Unescape.convert(SETTINGS.get_string("cmd-vpn-online-check")))?
txt : "ifconfig -a | grep tun0";

/*log("nvpn "+this.tool_available);
log("nvpn "+this.tool_connected_check);
log("nvpn "+this.tool_transition_check);
log("nvpn "+this.daemon_unreachable_check);
log("nvpn "+this.tool_logged_check);
log("nvpn "+this.current_server_get);
log("nvpn "+this.server_place_connect);
log("nvpn "+this.server_disconnect);
log("nvpn "+this.daemon_online_check);
log("nvpn "+this.vpn_online_check);*/


this.SETT_SIGS.push(SETTINGS.connect('changed::cmd-tool-available', () => {
this.tool_available= Unescape.convert(SETTINGS.get_string("cmd-tool-available"));
Expand Down Expand Up @@ -395,6 +402,9 @@ class Core_CMDs{
}));
}

/**
* Destructor; discard connected signals
*/
destroy(){
for(var i= 0; i<this.SETT_SIGS.length; ++i){
if(this.SETT_SIGS[i])
Expand All @@ -403,7 +413,10 @@ class Core_CMDs{
}
}


/**
* Since gnome-shell 3.32, this is needed on class that extends certain UI objects,
* including PanelMenu.Button
*/
let NVPNMenu = GObject.registerClass(
/** Class that implements the dedicated status area of the extension
* and contains its main menu
Expand Down Expand Up @@ -437,9 +450,11 @@ class NVPNMenu extends PanelMenu.Button{
_init(){
super._init(0.0, _("NordVPN"));

/** Create and init the gSettings's core commands manager*/
this._cmd= new Core_CMDs(this);
this._cmd.init();

/** storing signal connectio ids for later discards */
this.SETT_SIGS= [];

/** @member {boolean} nvpn_monitor
Expand Down Expand Up @@ -473,10 +488,8 @@ class NVPNMenu extends PanelMenu.Button{

/** saving this idea for later disconnection of the signal during object's destruction */
this._id_c_click1= this.connect('button-press-event',
/** when the panel is clicked, the extension performs a refresh of the menu's ui */
function(){
log("[nvpn] it works! * click! *");
if(!this.nvpn_monitor){
if((!this.nvpn_monitor) || this.currentStatus<STATUS.CONNECTED){
this._update_status_and_ui();
}
}.bind(this)
Expand All @@ -498,10 +511,6 @@ class NVPNMenu extends PanelMenu.Button{

hbox2.add_child(label1);

// log('[nvpn] is nvpn found? '+ this._is_NVPN_found().toString());
// log('[nvpn] is nvpn connected? '+ this._is_NVPN_connected().toString());
// log('[nvpn] nvpn status? '+ this._get_current_status().toString());

/** this private member is the part of the server info that is an adaptable
* text according to status */
this._label_status= new St.Label({style_class: 'label-nvpn-status'});
Expand Down Expand Up @@ -560,6 +569,10 @@ class NVPNMenu extends PanelMenu.Button{
* according to the current state provided of the 'nordvpn tool' */
this._update_status_and_ui();

/**
* Access the 'refresh-delay' gSettings and connects any change to ensure it will take effect
* within this extension
*/
this._refresh_delay= SETTINGS.get_int('refresh-delay');
this.SETT_SIGS[1]= SETTINGS.connect('changed::refresh-delay', () => {
this._refresh_delay= SETTINGS.get_int('refresh-delay');
Expand All @@ -570,8 +583,6 @@ class NVPNMenu extends PanelMenu.Button{
/** call to the private method '_vpn_survey()' to start "the monitoring loop "
* that update the ui in case of a 'norvdpn' tool state change */
this._vpn_survey();

// log('[nvpn] nvpn server? '+ this. _get_server_text_info());
}

/**
Expand Down Expand Up @@ -689,7 +700,9 @@ class NVPNMenu extends PanelMenu.Button{
this._vpn_lock= true;
/** use the '_get_current_status()' private method to set the 'currentStatus' attribute
* according to the 'nordvpn' command line tool's current state */
let oldStatus= this.currentStatus;
this.currentStatus= this._get_current_status();
if(oldStatus===this.currentStatus) return;

/** allows the ui menu to be open on user click */
this.setSensitive(true);
Expand Down Expand Up @@ -748,7 +761,6 @@ class NVPNMenu extends PanelMenu.Button{
* the country name from the found country code */
let country= Country_Dict[arr[1]];
if (country!==undefined){
// log('[nvpn] finding '+country);
/** we use the country menu object's method 'select_from_name' to update its ui
* so that the found country name is marked as selected */
this._submenu.select_from_name(country);
Expand Down Expand Up @@ -919,7 +931,6 @@ class NVPNMenu extends PanelMenu.Button{
* @method
*/
_button_clicked(){
// log('[nvpn] button clicked?');
/** the apearance and behavior of the button changes according to the current status */
switch(this.currentStatus){
/** these states are not supposed to display the button, so nothing is done */
Expand All @@ -942,14 +953,12 @@ class NVPNMenu extends PanelMenu.Button{
let strPlace= this._submenu.LastSelectedPlaceName;
if(strPlace.length===0){
this. _nordvpn_quickconnect();
// log('[nvpn] -> sh -c \"nordvpn c\"?');
}

break;
/** allows to disconnect when status is 'connected' */
case NVPNMenu.STATUS.CONNECTED:
this._nordvpn_disconnect();
// log('[nvpn] -> sh -c \"nordvpn d\"?');


break;
Expand Down Expand Up @@ -1003,7 +1012,6 @@ class NVPNMenu extends PanelMenu.Button{
* @param {string} placeName - the callback is supposed to give the name of the selected place as argument
*/
_place_menu_new_selection(placeName){
// log("[nvpn] Clicked on " + placeName + " s= "+this.currentStatus.toString());
/** Connection to this placeName if the current status is 'Disconnected' */
if(this.currentStatus===NVPNMenu.STATUS.DISCONNECTED){
this._nordvpn_quickconnect(placeName);
Expand Down Expand Up @@ -1049,10 +1057,6 @@ class NVPNMenu extends PanelMenu.Button{
this._vpn_timeout= null;
}
}

// var today = new Date();
// var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
// log("[nvpn] date: "+time);

/** recall itself, creating a separate loop, in 2 second (=timeout) */
this._vpn_timeout= Mainloop.timeout_add_seconds(this._refresh_delay,this._vpn_survey.bind(this));
Expand All @@ -1076,11 +1080,6 @@ class NVPNMenu extends PanelMenu.Button{
}
};


// var today = new Date();
// var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
// log("[nvpn] curr status "+ this.currentStatus + " @ "+time);

switch(this.currentStatus){
/** when state is 'logged out', check for login state */
case NVPNMenu.STATUS.LOGGED_OUT:
Expand Down Expand Up @@ -1144,11 +1143,9 @@ class NVPNMenu extends PanelMenu.Button{

break;
}
// log("[nvpn] change? "+change);

/** if a change has been detected, a ui update is needed */
if (change){
// log("[nvpn] Change detected from "+this.currentStatus.toString());
this._update_status_and_ui();
}
}
Expand Down Expand Up @@ -1188,19 +1185,20 @@ let _indicator;
* @function
*/
function enable() {
SETTINGS = Convenience.getSettings();

/** creating main object and attaching it to the top pannel */
_indicator= new NVPNMenu;
Main.panel.addToStatusArea('nvpn-menu', _indicator);
/** Iniating the gSettings access */
SETTINGS = Convenience.getSettings();

/** creating main object and attaching it to the top pannel */
_indicator= new NVPNMenu;
Main.panel.addToStatusArea('nvpn-menu', _indicator);
}

/**
* Called when extension is disabled
* @function
*/
function disable() {
/** destruction of the main object */
_indicator.destroy();
/** destruction of the main object */
_indicator.destroy();
}

45 changes: 41 additions & 4 deletions prefs.js
Expand Up @@ -11,9 +11,18 @@ const Gettext = imports.gettext.domain('gnome-shell-extensions-nvpnconnect');
const _ = Gettext.gettext;


/** Iniating gSettings access */
let SETTINGS= Convenience.getSettings();

/** Class that encapsulates most of the 'NordVPN_Connect' extension's settings
* page.
*/
class NVPN_Settings{
/**
* Constructor.
*
* Doesn't do much. Prepares fields and variables.
*/
constructor() {
this._objects= {
NVPN_Sett_Tabs: null,
Expand Down Expand Up @@ -46,9 +55,15 @@ class NVPN_Settings{

this._id_cmd_change_triggering= null;

/** Contained for signal connection IDs for later discard of the gSettings elements */
this.SETT_SIGS=[];
}

/**
* Destructor.
*
* Discards connections.
*/
destroy(){
if(this._id_compact_toggle){
this._objects.NVPN_Sett_Toggle_Compact.disconnect(this._id_compact_toggle);
Expand Down Expand Up @@ -81,30 +96,53 @@ class NVPN_Settings{
}
}

/**
* Builds the UI
*
* @returns the main GTX widget to display in extension's 'settings' window
*/
build(){
/** This pages uses an UI file ('prefs.ui') generated with 'Glade'
* Here, we need an instance of the Gtk Builder that will load the file
* and build the UI from there.
*/
this.builder= new Gtk.Builder();
this.builder.add_from_file(
Me.dir.get_child("prefs.ui").get_path()
);

/** Care was taken so that all the elements in the '_objects' dictionary field,
* all have the same name has the relevant corresponding objects in the '.ui' file.
* This loop uses this to ensure that the fields in '_objects' are refering to the
* UI elements of correcponding names.
*/
for (let o in this._objects) {
this._objects[o] = this.builder.get_object(o);
}

/** Calibrating the UI according to current gSettings state */
this._initFromSettings();

/** Connecting UI elements with appropriate callbacks */
this._initConnections();

/** Fill the UI 'Text entries' according to current gSettings state */
this._preapreUI();

return this._objects.NVPN_Sett_Tabs;
}

/**
* Method that calibrates UI elements according to current gSettings state
*/
_initFromSettings(){
this._objects.NVPN_Sett_Toggle_Compact.set_state(SETTINGS.get_boolean('compact-icon'));
this._objects.NVPN_Sett_Spin_Refresh.set_value(SETTINGS.get_int('refresh-delay'));
}

/**
* Method that connects UI elements signals to appropriate callbacks
*/
_initConnections(){
this._id_compact_toggle=
this._objects.NVPN_Sett_Toggle_Compact.connect(
Expand Down Expand Up @@ -149,6 +187,9 @@ class NVPN_Settings{
);
}

/**
* Method that fills the 'Text Entries' elements according to current gSettings state'
*/
_preapreUI(){
let fillEntries= (gEntry, gsKey) => {
gEntry.set_text(
Expand Down Expand Up @@ -261,7 +302,3 @@ function init() {
function buildPrefsWidget() {
return ui.build();
}

function reset_settings(b) {
SETTINGS.reset('compact-icon');
}
9 changes: 0 additions & 9 deletions stylesheet.css
@@ -1,13 +1,4 @@

.helloworld-label {
font-size: 24px;
font-weight: bold;
color: #ffffff;
background-color: rgba(10,10,10,0.6);
border-radius: 5px;
padding: .5em;
}

.panel-status-menu-hbox{
font-size: 17px;
border-radius: 5px;
Expand Down

0 comments on commit aabdf8d

Please sign in to comment.