Skip to content
This repository has been archived by the owner on Sep 25, 2024. It is now read-only.

Commit

Permalink
Merge pull request #31 from calexandru2018/testing
Browse files Browse the repository at this point in the history
Display server load in tray; Other code improvements
  • Loading branch information
calexandru2018 authored Apr 7, 2020
2 parents b6fc4e9 + 712e4d1 commit 5d75a89
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 56 deletions.
16 changes: 15 additions & 1 deletion protonvpn_linux_gui/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,22 @@
from protonvpn_cli.constants import VERSION as cli_version
except:
cli_version = "Not installed"
VERSION = "1.8.1"
VERSION = "1.8.2"
GITHUB_URL_RELEASE = "https://github.com/calexandru2018/protonvpn-linux-gui/releases/latest"

# Tray configuration naming
TRAY_CFG_SERVERLOAD = "display_serverload"
TRAY_CFG_SERVENAME = "display_server"
TRAY_CFG_DATA_TX = "display_data_tx"
TRAY_CFG_TIME_CONN = "display_time_conn"

TRAY_CFG_DICT = {
"tray_data_tx_combobox": TRAY_CFG_DATA_TX,
"tray_servername_combobox": TRAY_CFG_SERVENAME,
"tray_time_connected_combobox": TRAY_CFG_TIME_CONN,
"tray_serverload_combobox": TRAY_CFG_SERVERLOAD
}

SERVICE_NAME = "custompvpn-autoconnect"
PATH_AUTOCONNECT_SERVICE = "/etc/systemd/system/{}.service".format(SERVICE_NAME)
TEMPLATE ="""
Expand Down
47 changes: 44 additions & 3 deletions protonvpn_linux_gui/resources/main.glade
Original file line number Diff line number Diff line change
Expand Up @@ -1445,7 +1445,7 @@
</packing>
</child>
<child>
<object class="GtkGrid" id="purge_conf_grid1">
<object class="GtkGrid" id="tray_conf_grid">
<property name="width_request">477</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
Expand All @@ -1468,7 +1468,7 @@
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
<property name="width">6</property>
<property name="width">8</property>
</packing>
</child>
<child>
Expand All @@ -1485,7 +1485,7 @@
<packing>
<property name="left_attach">1</property>
<property name="top_attach">3</property>
<property name="width">4</property>
<property name="width">6</property>
</packing>
</child>
<child>
Expand Down Expand Up @@ -1563,6 +1563,7 @@
<property name="label" translatable="yes">Data Transmitted</property>
<attributes>
<attribute name="weight" value="medium"/>
<attribute name="size" value="12000"/>
</attributes>
</object>
<packing>
Expand Down Expand Up @@ -1607,6 +1608,46 @@
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_top">10</property>
<property name="margin_bottom">10</property>
<property name="label" translatable="yes">Server Load</property>
<attributes>
<attribute name="weight" value="medium"/>
<attribute name="size" value="12000"/>
</attributes>
</object>
<packing>
<property name="left_attach">6</property>
<property name="top_attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkComboBox" id="tray_serverload_combobox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="model">Tray_Configurations</property>
<property name="active">0</property>
<property name="has_frame">False</property>
<property name="entry_text_column">1</property>
<property name="id_column">0</property>
<child>
<object class="GtkCellRendererText"/>
<attributes>
<attribute name="text">1</attribute>
</attributes>
</child>
</object>
<packing>
<property name="left_attach">6</property>
<property name="top_attach">2</property>
<property name="width">2</property>
</packing>
</child>
<child>
<placeholder/>
</child>
Expand Down
21 changes: 8 additions & 13 deletions protonvpn_linux_gui/thread_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from .gui_logger import gui_logger

# Import constants
from .constants import VERSION, GITHUB_URL_RELEASE
from .constants import VERSION, GITHUB_URL_RELEASE, TRAY_CFG_SERVERLOAD, TRAY_CFG_SERVENAME, TRAY_CFG_DATA_TX, TRAY_CFG_TIME_CONN, TRAY_CFG_DICT

# PyGObject import
import gi
Expand Down Expand Up @@ -607,19 +607,14 @@ def tray_configurations(interface, messagedialog_label, messagedialog_spinner):
"""
gui_logger.debug(">>> Running \"tray_configurations\".")

tray_data_tx_combobox = interface.get_object("tray_data_tx_combobox").get_active()
tray_servername_combobox = interface.get_object("tray_servername_combobox").get_active()
tray_time_connected_combobox = interface.get_object("tray_time_connected_combobox").get_active()
response_list = []
custom_msg = ""
for k,v in TRAY_CFG_DICT.items():
combobox_val = interface.get_object(k).get_active()
set_config_value("USER", v, combobox_val)
response_list.append("Display" if combobox_val == 1 else "Do not display")

set_config_value("USER", "display_server", tray_servername_combobox)
set_config_value("USER", "display_user_tx", tray_data_tx_combobox)
set_config_value("USER", "display_time_conn", tray_time_connected_combobox)

data_tx_msg = "Display" if tray_data_tx_combobox == 1 else "Do not display"
servername_msg = "Display" if tray_servername_combobox == 1 else "Do not display"
time_connected_msg = "Display" if tray_time_connected_combobox == 1 else "Do not display"

custom_msg = "Data Transmitted: {0}\nServername: {1}\nTime Connected: {2}".format(data_tx_msg, servername_msg, time_connected_msg)
custom_msg = "Data Transmitted: {0}\nServername: {1}\nTime Connected: {2}\nServer load: {3}".format(*response_list)

result = "Tray configurations <b>updated</b>!\n\n" + custom_msg
messagedialog_label.set_markup(result)
Expand Down
132 changes: 115 additions & 17 deletions protonvpn_linux_gui/tray_icon.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,68 @@
import os
import sys
import time
import datetime
import gi
import subprocess

# Import GTK3 and AppIndicator3
import gi

gi.require_version('Gtk', '3.0')
gi.require_version('AppIndicator3', '0.1')

from gi.repository import Gtk, GObject
from gi.repository import AppIndicator3 as appindicator

from protonvpn_cli.utils import (
get_country_name,
get_config_value,
is_connected,
get_transferred_data
)
# Import protonvpn-cli-ng util functions
try:
from protonvpn_cli.utils import (
get_country_name,
get_config_value,
is_connected,
get_transferred_data,
pull_server_data,
get_servers,
get_server_value
)
except:
sys.exit(1)

from .constants import TRAY_CFG_SERVERLOAD, TRAY_CFG_SERVENAME, TRAY_CFG_DATA_TX, TRAY_CFG_TIME_CONN

from .gui_logger import gui_logger

CURRDIR = os.path.dirname(os.path.abspath(__file__))

class ProtonVPNIndicator:
def __init__(self):
self.gtk = Gtk
self.gobject = GObject
self.VARL = 0
self.display_serverload = False
self.serverload_msg = "Load: -"
self.menu = self.menu()
self.ind = appindicator.Indicator.new(
"ProtonVPN GUI Indicator",
"protonvpn-gui-indicator",
appindicator.IndicatorCategory.APPLICATION_STATUS)
self.ind.set_status(appindicator.IndicatorStatus.ACTIVE)
self.ind.set_menu(self.menu)
# self.gobject.timeout_add_seconds(1, self.update_label, None)
self.connection_status(None)
self.gobject.timeout_add_seconds(5, self.connection_status, None)

# Get first server load
self.update_serverload(None)
# Call main loop
self.main_loop(None)

self.gobject.timeout_add_seconds(5, self.main_loop, None)
self.gobject.timeout_add_seconds(910, self.update_serverload, None)

self.gtk.main()

def menu(self):
self.menu = self.gtk.Menu()

self.server_load = self.gtk.MenuItem(label='')
self.menu.append(self.server_load)

self.data_sent = self.gtk.MenuItem(label='')
self.menu.append(self.data_sent)

Expand Down Expand Up @@ -85,8 +109,11 @@ def menu(self):

return self.menu

def connection_status(self, _):
def main_loop(self, _):
"""Main loop that updates all labels.
"""
icon_path = "/resources/protonvpn_logo_alt.png"
self.display_serverload = False
display_data_rec = False
display_server = False
display_time_conn = False
Expand All @@ -95,54 +122,117 @@ def connection_status(self, _):
icon_path = "/resources/protonvpn_logo.png"
settings = self.get_tray_settings()

self.display_serverload = True if settings["display_serverload"] else False
display_data_rec = True if settings["display_data_tx"] else False
display_server = True if settings["display_server"] else False
display_time_conn = True if settings["display_time_conn"] else False

self.display_extra_info(
display_serverload=self.display_serverload,
display_data_rec=display_data_rec,
display_server=display_server,
display_time_conn=display_time_conn)

self.ind.set_icon_full(CURRDIR + icon_path, 'protonvpn')

return True


def update_serverload(self, _):
"""Updates server load.
"""

connected_server = False
load = False

try:
connected_server = get_config_value("metadata", "connected_server")
except KeyError:
gui_logger.debug("[!] Could not find specified key: ".format(KeyError))
return True

# force_pull servers
try:
pull_server_data(force=True)
except:
gui_logger.debug("[!] Could not pull from servers, possible due to unstable connection.")
return True

# get_servers
servers = get_servers()

# get server load
try:
load = get_server_value(connected_server, "Load", servers)
except:
gui_logger.debug("[!] Unable to get server load.")
return True

self.serverload_msg = "Load: {}%".format(load)

return True

def quick_connect(self, _):
"""Makes a quick connection by making a cli call to protonvpn-cli-ng"""
subprocess.Popen(["sudo", "protonvpn", "connect", "--fastest"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self.update_serverload(None)

def show_gui(self, _):
"""Displays the GUI."""
subprocess.Popen(["sudo", "protonvpn-gui"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

def disconnect(self, _):
"""Disconnects from a current vpn connection."""
subprocess.Popen(["sudo", "protonvpn", "disconnect"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

def get_tray_settings(self):
"""Gets and returns tray settings from config file.
Returns: dict
- Dictionary with boolean values for each display configuration.
"""

resp_dict = {
"display_serverload": False,
"display_data_tx": False,
"display_server": False,
"display_time_conn": False,
}

try:
resp_dict["display_server"] = int(get_config_value("USER", "display_server"))
resp_dict["display_serverload"] = int(get_config_value("USER", TRAY_CFG_SERVERLOAD))
except KeyError:
gui_logger.debug("[!] Could not find display_serverload in config file: ".format(KeyError))
pass

try:
resp_dict["display_server"] = int(get_config_value("USER", TRAY_CFG_SERVENAME))
except KeyError:
gui_logger.debug("[!] Could not find display_server in config file: ".format(KeyError))
pass

try:
resp_dict["display_data_tx"] = int(get_config_value("USER", "display_user_tx"))
resp_dict["display_data_tx"] = int(get_config_value("USER", TRAY_CFG_DATA_TX))
except KeyError:
gui_logger.debug("[!] Could not find display_data_tx in config file: ".format(KeyError))
pass

try:
resp_dict["display_time_conn"] = int(get_config_value("USER", "display_time_conn"))
resp_dict["display_time_conn"] = int(get_config_value("USER", TRAY_CFG_TIME_CONN))
except KeyError:
gui_logger.debug("[!] Could not find display_time_conn in config file: ".format(KeyError))
pass

return resp_dict

def display_extra_info(self, **kwrgs):
"""Either displays or hides information based on tray display configurations.
"""

if kwrgs["display_serverload"]:
self.server_load.get_child().set_text(self.serverload_msg)
self.server_load.show()
else:
self.server_load.get_child().set_text("Load: -")
self.server_load.hide()

if kwrgs["display_server"]:
server = get_config_value("metadata", "connected_server")
Expand Down Expand Up @@ -175,6 +265,9 @@ def display_extra_info(self, **kwrgs):
self.time_conn.hide()

def data_sent_received(self):
"""Get and returns ammount of sent and received data.
"""

sent_amount, received_amount = get_transferred_data()

sent_amount = sent_amount if is_connected else ""
Expand All @@ -183,6 +276,9 @@ def data_sent_received(self):
return (received_amount, sent_amount)

def time_connected(self):
"""Gets and returns the connection time length.
"""

try:
connected_time = get_config_value("metadata", "connected_time")
connection_time = time.time() - int(connected_time)
Expand All @@ -195,4 +291,6 @@ def time_connected(self):
return connection_time

def quit_indicator(self, _):
"""Quit/Stop the tray icon.
"""
self.gtk.main_quit()
Loading

0 comments on commit 5d75a89

Please sign in to comment.