Skip to content

Commit

Permalink
Merge pull request #2 from jdmg94/develop
Browse files Browse the repository at this point in the history
adds settings widgets
  • Loading branch information
José Muñoz authored Aug 30, 2022
2 parents 3fec1ac + 275371c commit 1164c31
Show file tree
Hide file tree
Showing 9 changed files with 318 additions and 28 deletions.
15 changes: 15 additions & 0 deletions assets/nordvpn-logo-symbolic.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes
7 changes: 4 additions & 3 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ project(
'wingpanel-nordvpn',
'vala',
'c',
version : '0.0.2'
version : '0.1.0'
)

i18n = import('i18n')
Expand All @@ -17,8 +17,9 @@ posix_dep = meson.get_compiler('vala').find_library('posix')

install_data(
[
join_paths('assets', 'nordvpn-original-symbolic.svg'),
join_paths('assets', 'nordvpn-positive-symbolic.svg')
join_paths('assets', 'nordvpn-symbolic.svg'),
join_paths('assets', 'nordvpn-off-symbolic.svg'),
join_paths('assets', 'nordvpn-logo-symbolic.svg')
],
install_dir: join_paths(
get_option('prefix'), get_option('datadir'), 'icons', 'hicolor', 'scalable', 'status'
Expand Down
12 changes: 10 additions & 2 deletions src/Indicator.vala
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public class NordVPN.Indicator : Wingpanel.Indicator {
public override Gtk.Widget get_display_widget () {
if (display_widget == null) {
display_widget = new Gtk.Image.from_icon_name (
nordvpn.state.is_connected ? "nordvpn-original-symbolic" : "nordvpn-positive-symbolic",
nordvpn.state.is_connected ? "nordvpn-symbolic" : "nordvpn-off-symbolic",
Gtk.IconSize.LARGE_TOOLBAR
);

display_widget.set_has_tooltip (true);
nordvpn.state_changed.connect ((next_state) => {
display_widget.tooltip_markup = derive_tooltip_markup (next_state);
display_widget.icon_name = next_state.is_connected ? "nordvpn-original-symbolic" : "nordvpn-positive-symbolic";
display_widget.icon_name = next_state.is_connected ? "nordvpn-symbolic" : "nordvpn-off-symbolic";
});
}

Expand All @@ -38,6 +38,10 @@ public class NordVPN.Indicator : Wingpanel.Indicator {
public override Gtk.Widget ? get_widget () {
if (popover_widget == null) {
popover_widget = new NordVPN.PopOverWidget (nordvpn);

popover_widget.opened_dialog.connect(() => {
this.close();
});
}

return popover_widget;
Expand All @@ -50,6 +54,9 @@ public class NordVPN.Indicator : Wingpanel.Indicator {

StringBuilder status_tooltip = new StringBuilder ();
string key_markup = "<b><span>%s:</span></b> ";
status_tooltip.append (key_markup.printf ("Location"));
status_tooltip.append ("%s, %s".printf (connection.city, connection.country));
status_tooltip.append ("\n");
status_tooltip.append (key_markup.printf ("Current Server"));
status_tooltip.append (connection.current_server);
status_tooltip.append ("\n");
Expand All @@ -67,6 +74,7 @@ public class NordVPN.Indicator : Wingpanel.Indicator {

return status_tooltip.str;
}

}

public Wingpanel.Indicator ? get_indicator (Module module, Wingpanel.IndicatorManager.ServerType server_type) {
Expand Down
10 changes: 6 additions & 4 deletions src/InfoBar.vala
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
public class NordVPN.InfoBar : Gtk.Box {
private Gtk.Label connection_label;
private Gtk.Button settings_icon_button;
private NordVPN.State connection;

public InfoBar (string label) {
public signal void clicked ();

public InfoBar (string label, NordVPN.Model ? model = null) {
settings_icon_button = new Gtk.Button.from_icon_name ("open-menu-symbolic", Gtk.IconSize.MENU);
connection_label = new Gtk.Label (label) {
halign = Gtk.Align.START,
};

connection_label.get_style_context ().add_class (Granite.STYLE_CLASS_H4_LABEL);
settings_icon_button.clicked.connect (() => {
var window = new NordVPN.SettingsView ();
var window = new NordVPN.SettingsView (model);

window.show ();
clicked ();
window.show_all ();
});

this.pack_start (connection_label, true, true, 0);
Expand Down
92 changes: 82 additions & 10 deletions src/NordVPN.vala
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ public class NordVPN.Settings : GLib.Object {
public string technology { get; set; }
public string firewall { get; set; }
public string kill_switch { get; set; }
public string threat_protection { get; set; }
public string notifications { get; set; }
public string auto_connect { get; set; }
public string threat_protection_lite { get; set; }
public string autoconnect { get; set; }
public string obfuscate { get; set; }
public string ipv6 { get; set; }
public string notify { get; set; }
public string meshnet { get; set; }
public string dns { get; set; }
public string whitelisted_subnets { get; set; }
public string[] whitelisted_subnets { get; set; }
public string[] dns { get; set; }

public string[] technologies { get; default = { "OPENVPN", "NORDLYNX" }; }
public string[] protocols { get; default = { "TCP", "UDP" }; }

public Settings () {}
}
Expand Down Expand Up @@ -56,13 +60,12 @@ public class NordVPN.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/";
private string sanitize (string value) {
string buffer = value;
string[] chars_to_remove = { MESH_WARNING, "\\r", "\\n", "-" };
string[] chars_to_remove = { MESH_WARNING, "\\r", "\\n", "-", "/", "|", "\\" };

foreach (unowned string item in chars_to_remove) {
buffer = buffer.replace (item, "");
}


return buffer.strip ();
}

Expand All @@ -76,6 +79,10 @@ public class NordVPN.Controller {
connection_changed ();
}

public void update_setting (string key, string value) {
Posix.system ("nordvpn set %s %s".printf (key, value));
}

public NordVPN.State get_state () {
string buffer;
NordVPN.State result = new NordVPN.State ();
Expand All @@ -100,6 +107,71 @@ public class NordVPN.Controller {
return result;
}

public NordVPN.Settings get_settings () {
string buffer;
NordVPN.Settings result = new NordVPN.Settings ();
Process.spawn_command_line_sync ("nordvpn settings", out buffer, null, null);

if (buffer.contains (MESH_WARNING)) {
buffer = sanitize (buffer);
}

string[] parts = buffer.split ("\n");
foreach (unowned string item in parts) {
if (":" in item) {
string[] key_value = item.split (":");
string key = key_value[0].down ().replace (" ", "_");
string value = key_value[1].strip ();


if (value != "") {
if (key == "dns") {
if ("," in value) {
string[] array_value = value.split (",");

for (var i = 0 ; i < array_value.length ; i++) {
array_value[i] = array_value[i].strip ();
}

result.set (key, array_value);
continue;
} else {
string[] array_value = { value };
result.set (key, array_value);
}
} else {
result.set (key, value);
continue;
}
}

buffer = key;

} else {
try {
GLib.Value array_buffer;
result.get (buffer, out array_buffer, null);
string[] array_value = (string[]) array_buffer;
array_value += item.strip ();
result.set (buffer, array_value);
} catch {}
}
}

return result;
}

public string get_account_information () {
string buffer;
Touple<string>[] result = {};
Process.spawn_command_line_sync ("nordvpn account", out buffer, null, null);

buffer = sanitize (buffer);
buffer = buffer.replace ("Account Information:\n", "");

return buffer;
}

public NordVPN.Touple<string>[] get_request (string cmd) {
string buffer;
NordVPN.Touple<string>[] result = {};
Expand Down Expand Up @@ -138,21 +210,21 @@ public class NordVPN.Controller {
public class NordVPN.Model : GLib.Object {
public signal void state_changed (NordVPN.State next_state);

private NordVPN.Settings settings;
public NordVPN.Settings settings;
public NordVPN.Controller controller;
public NordVPN.State state;
public Gtk.TreeStore store;
public Gtk.TreePath active_path { get; set; default = null; }

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

this.refresh_status ();
this.store = this.get_all_connection_options ();
this.settings = controller.get_settings ();
this.store = get_all_connection_options ();
}

public void refresh_status () {
Expand Down
14 changes: 10 additions & 4 deletions src/PopOverWidget.vala
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ public class NordVPN.PopOverWidget : Gtk.Box {
private NordVPN.InfoBar info_bar;
private RevealerSwitch connection_toggle;

public signal void opened_dialog ();

public PopOverWidget (NordVPN.Model nordvpn) {
NordVPN.State connection = nordvpn.state;
string location_string = "%s, %s".printf (connection.city, connection.country);
NordVPN.ServersTreeWidget servers_tree_view = new NordVPN.ServersTreeWidget (nordvpn.store, nordvpn.active_path);

this.connection_toggle = new RevealerSwitch (connection.status, connection.is_connected);
this.info_bar = new NordVPN.InfoBar ("%s, %s".printf (connection.country, connection.city));
this.info_bar = new NordVPN.InfoBar (location_string, nordvpn);

this.set_margin_top (5);
this.orientation = Gtk.Orientation.VERTICAL;

this.pack_start (connection_toggle);
this.pack_start (new Gtk.Separator (Gtk.Orientation.HORIZONTAL));
this.pack_start (servers_tree_view);

update_connection_label (connection);

connection_toggle.add (info_bar);
connection_toggle.toggled.connect ((is_active) => {
connection_toggle.text = is_active ? "Connected" : "Disconnected";
Expand All @@ -28,6 +30,10 @@ public class NordVPN.PopOverWidget : Gtk.Box {
}
});

this.info_bar.clicked.connect (() => {
opened_dialog ();
});

nordvpn.state_changed.connect (update_connection_label);
servers_tree_view.selection_changed.connect ((next_label, next_value, next_path) => {
current_key = next_value;
Expand All @@ -44,7 +50,7 @@ public class NordVPN.PopOverWidget : Gtk.Box {
}

private void update_connection_label (NordVPN.State next_state) {
info_bar.set_label ("%s, %s".printf (next_state.country, next_state.city));
info_bar.set_label ("%s, %s".printf (next_state.city, next_state.country));
}

}
Loading

0 comments on commit 1164c31

Please sign in to comment.