Skip to content

Commit

Permalink
basic functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Jose Munoz committed Aug 23, 2022
1 parent 9ad9f2e commit edaa180
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 41 deletions.
37 changes: 27 additions & 10 deletions src/NordVPN.vala
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public class NordVPN.Touple<T> {
}

public class NordVPN.Controller {
public signal void connection_changed ();

public Controller () {}

private string MESH_WARNING = "New feature - Meshnet! Link remote devices in Meshnet to connect to them directly over encrypted private tunnels, and route your traffic through another device. Use the `nordvpn meshnet --help` command to get started. Learn more: https://nordvpn.com/features/meshnet/";
Expand All @@ -63,14 +65,14 @@ public class NordVPN.Controller {
return buffer.strip ();
}

public void connect (string server) {
Posix.system ("nordvpn c");

public void connect (string ? server = "") {
Posix.system ("nordvpn c " + server);
connection_changed ();
}

public void disconnect () {
Posix.system ("nordvpn d");

connection_changed ();
}

public NordVPN.State get_state () {
Expand Down Expand Up @@ -137,11 +139,15 @@ public class NordVPN.Model : GLib.Object {
public NordVPN.State state;
public bool is_connected;
public Gtk.TreeStore store;
public Gtk.TreePath active_path { get; set; default = null; }

public Model () {
this.controller = new NordVPN.Controller ();
this.refresh_status ();
this.controller.connection_changed.connect (() => {
this.refresh_status ();
});

this.refresh_status ();
this.store = this.get_all_connection_options ();
}

Expand All @@ -167,13 +173,13 @@ public class NordVPN.Model : GLib.Object {
store.set (root, 0, "Specialty Servers", 1, false, -1);
Gtk.TreeIter groups_iterator;

foreach (Touple<string> country in this.controller.get_groups ()) {
foreach (Touple<string> group in this.controller.get_groups ()) {
store.append (out groups_iterator, root);
store.set (groups_iterator,
0, country.get (0),
0, group.get (0),
1, true,
2, false,
3, country.get (1),
3, group.get (1),
-1
);
}
Expand All @@ -184,28 +190,39 @@ public class NordVPN.Model : GLib.Object {
Gtk.TreeIter country_iterator;

foreach (Touple<string> country in this.controller.get_countries ()) {
bool country_is_active = is_connected && country.get (0) == this.state.country;

store.append (out country_iterator, root);
store.set (country_iterator,
0, country.get (0),
1, true,
2, country.get (0) == this.state.country,
2, country_is_active,
3, country.get (1),
-1
);

if (country_is_active) {
this.active_path = store.get_path (country_iterator);
}

// Add Cities
Touple<string>[] cities = this.controller.get_cities (country.get (1));
if (cities.length > 1) {
Gtk.TreeIter cities_iterator;
foreach (Touple<string> city in cities) {
bool city_is_active = is_connected && city.get (0) == this.state.city;
store.append (out cities_iterator, country_iterator);
store.set (cities_iterator,
0, city.get (0),
1, true,
2, city.get (0) == this.state.city,
2, city_is_active,
3, city.get (1),
-1
);

if (city_is_active) {
this.active_path = store.get_path (cities_iterator);
}
}
}

Expand Down
37 changes: 22 additions & 15 deletions src/PopOverWidget.vala
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
public class NordVPN.PopOverWidget : Gtk.Box {
public signal void vpn_changed (bool is_active);

private string current_key = "";

public PopOverWidget (NordVPN.Model nordvpn) {
NordVPN.State connection = nordvpn.state;
RevealerSwitch connection_toggle = new RevealerSwitch (connection.status, nordvpn.is_connected);
Gtk.Button settings_icon_button = new Gtk.Button.from_icon_name ("open-menu-symbolic", Gtk.IconSize.MENU);
NordVPN.ServersTreeWidget servers_tree_view = new NordVPN.ServersTreeWidget (nordvpn.store, nordvpn.active_path);
Gtk.Label connection_label = new Gtk.Label (connection.country + ", " + connection.city) {
halign = Gtk.Align.START,
};
Gtk.Box row = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0) {
margin_top = 10,
margin_bottom = 10,
margin_end = 10,
};

connection_toggle.toggled.connect ((is_active) => {
vpn_changed (is_active);
connection_toggle.set_active (is_active);

connection_toggle.text = is_active ? "Connected" : "Disconnected";
if (is_active) {

nordvpn.controller.connect (current_key);
} else {
nordvpn.controller.disconnect();
nordvpn.controller.disconnect ();
}
});

nordvpn.state_changed.connect((next_state) => {
connection_label.set_text(next_state.country + ", " + next_state.city);
nordvpn.state_changed.connect ((next_state) => {
connection_label.set_text (next_state.country + ", " + next_state.city);
});

var settings_icon_button = new Gtk.Button.from_icon_name ("open-menu-symbolic", Gtk.IconSize.MENU);


var row = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0) {
margin_top = 10,
margin_bottom = 10,
margin_end = 10,
};

connection_toggle.add (row);
connection_label.get_style_context ().add_class (Granite.STYLE_CLASS_H4_LABEL);
row.pack_start (connection_label, true, true, 0);
Expand All @@ -43,9 +44,15 @@ public class NordVPN.PopOverWidget : Gtk.Box {
window.show ();
});

NordVPN.ServersTreeWidget servers_tree_view = new NordVPN.ServersTreeWidget (nordvpn.store);

servers_tree_view.selection_changed.connect ((next_label, next_value, next_path) => {
current_key = next_value;
connection_label.set_text (next_label);

if (nordvpn.is_connected) {
nordvpn.controller.connect (next_value);
nordvpn.active_path = next_path;
}
});

this.pack_start (connection_toggle);
this.pack_start (new Gtk.Separator (Gtk.Orientation.HORIZONTAL));
Expand Down
4 changes: 4 additions & 0 deletions src/RevealerSwitch.vala
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@ class NordVPN.RevealerSwitch : Gtk.Box {
this.revealer_content.pack_start (child);
}

public void set_active(bool next_state) {
this.main_switch.set_active(next_state);
}

}
75 changes: 60 additions & 15 deletions src/ServersTreeWidget.vala
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
public class NordVPN.ServersTreeWidget : Gtk.Box {
private string current_selection;
public signal void selection_changed (string next_label, string next_value, Gtk.TreePath next_path);

private Gtk.SearchEntry search_bar;
private Gtk.TreePath active_path;

public ServersTreeWidget (Gtk.TreeStore nordvpn_model, Gtk.TreePath ? initial_path = null) {
if (initial_path != null) {
active_path = initial_path;

}

public ServersTreeWidget (Gtk.TreeStore nordvpn_model) {
search_bar = new Gtk.SearchEntry ();

Gtk.TreeView servers_tree_view = new Gtk.TreeView ();
Expand Down Expand Up @@ -49,26 +56,60 @@ public class NordVPN.ServersTreeWidget : Gtk.Box {
servers_tree_view.expand_all ();
});

cell_renderer_radio.toggled.connect ((path) => {
// GLib.Value prev_state;
// Gtk.TreeIter iterator;
cell_renderer_radio.toggled.connect ((next_path_string) => {
GLib.Value buffer;
Gtk.TreeIter iterator;
string next_label = "";
string next_value = "";
Gtk.TreePath next_path = new Gtk.TreePath.from_string (next_path_string);

if ((bool)active_path) {
nordvpn_model.get_iter (out iterator, this.active_path);
nordvpn_model.set_value (iterator, 2, false);
set_parent (nordvpn_model, iterator, 2, false);

if (active_path.to_string () != next_path.to_string ()) {
this.active_path = next_path;
}
}

// if (this.current_selection != null) {
// nordvpn_model.get_iter (out iterator, new Gtk.TreePath.from_string (this.current_selection));
// nordvpn_model.set_value (iterator, 2, false);
// }

// if (this.current_selection != path) {
// this.current_selection = path;
// }
nordvpn_model.get_iter (out iterator, next_path);
nordvpn_model.set_value (iterator, 2, true);
set_parent (nordvpn_model, iterator, 2, true);

// nordvpn_model.get_iter (out iterator, new Gtk.TreePath.from_string (path));
// nordvpn_model.get_value (iterator, 2, out prev_state);
// nordvpn_model.set_value (iterator, 2, !(bool) prev_state);
nordvpn_model.get_value (iterator, 3, out buffer);
next_value = (string) buffer;

nordvpn_model.get_value (iterator, 0, out buffer);
next_label += (string) buffer;
get_parent (nordvpn_model, iterator, 1, out buffer);
if ((bool) buffer) {
get_parent (nordvpn_model, iterator, 0, out buffer);
next_label = (string) buffer + ", " + next_label;
}

selection_changed (next_label, next_value, next_path);
});

}

private void get_parent (Gtk.TreeStore model, Gtk.TreeIter iterator, int column, out GLib.Value buffer) {
Gtk.TreeIter iterator_parent;

if (model.iter_parent (out iterator_parent, iterator)) {
model.get_value (iterator_parent, column, out buffer);
}
}

private void set_parent (Gtk.TreeStore model, Gtk.TreeIter iterator, int column, bool next_value) {
Gtk.TreeIter iterator_parent;

if (model.iter_parent (out iterator_parent, iterator)) {
model.set_value (iterator_parent, column, next_value);
}
}

private bool filter_tree (Gtk.TreeModel model, Gtk.TreeIter iterator) {
GLib.Value buffer;
model.get_value (iterator, 0, out buffer);
Expand Down Expand Up @@ -101,4 +142,8 @@ public class NordVPN.ServersTreeWidget : Gtk.Box {
return result;
}

public void set_active_path (Gtk.TreePath next_path) {
this.active_path = next_path;
}

}
2 changes: 1 addition & 1 deletion src/SettingsView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class NordVPN.SettingsView : Gtk.Window {
this.set_default_size (640, 480);
}

public void show () {
public new void show () {
this.visible = true;
}

Expand Down

0 comments on commit edaa180

Please sign in to comment.