Commit bb7a9de2 authored by Danial Behzadi's avatar Danial Behzadi
Browse files

some refactors and lintings

parent 8631e1f5
......@@ -2,23 +2,36 @@
# Released under GPLv3+ License
# Danial Behzadi<dani.behzi@ubuntu.com>, 2020-2021.
import gi
'''
actions for carburetor
'''
import os
import re
from subprocess import PIPE, Popen
import gi
gi.require_versions({'Notify': '0.7'})
from gi.repository import Gio, GLib, Notify
from subprocess import PIPE, Popen
from . import config
from . import functions
from . import ui
def add(name, function, app):
'''
adds functions to app as actions
'''
action = Gio.SimpleAction.new(name, None)
action.connect('activate', function, app)
app.add_action(action)
def do_startup(app):
'''
actions to do when starting the app up
'''
add('preferences', on_preferences, app)
add('about', on_about, app)
add('quit', on_quit, app)
......@@ -29,14 +42,21 @@ def do_startup(app):
def on_preferences(action, params, app):
'''
show the preferences window
'''
if not app.prefs:
prefs_window = ui.get('PreferencesWindow')
prefs_window.set_transient_for(app.window)
app.prefs = prefs_window
app.prefs.connect('delete-event', functions.hide_window)
response = app.prefs.show()
app.prefs.show()
def on_about(action, param, app):
'''
show the about window
'''
if not app.about:
about_dialog = ui.get('AboutDialog')
about_dialog.set_transient_for(app.window)
......@@ -45,10 +65,18 @@ def on_about(action, param, app):
if -6 < response < -3:
app.about.hide()
def on_quit(action, param, app):
'''
exit the app
'''
app.quit()
def on_connect(action, param, app):
'''
clicking on connect button
'''
button = ui.get('ConnectButton')
button.set_sensitive(False)
menu_button = ui.get('ActionMenuButton')
......@@ -67,41 +95,60 @@ def on_connect(action, param, app):
text_starting = config._("Connecting…")
main_label.set_label(text_starting)
action = 'start'
app.task = Popen(config.command + action, stdout=PIPE, shell=True,
preexec_fn=os.setsid)
app.io_id = GLib.io_add_watch(app.task.stdout, GLib.IO_IN,
functions.set_progress)
GLib.io_add_watch(app.task.stdout, GLib.IO_HUP, functions.thread_finished,
button, app)
app.task = Popen(
config.COMMAND + action,
stdout=PIPE, shell=True,
preexec_fn=os.setsid)
app.io_id = GLib.io_add_watch(
app.task.stdout, GLib.IO_IN,
functions.set_progress)
GLib.io_add_watch(
app.task.stdout, GLib.IO_HUP,
functions.thread_finished, button, app)
def on_new_id(action, param, app):
'''
clicking on new id button
'''
if functions.is_running():
newid = Popen(config.command + 'newid', stdout=PIPE, shell=True)
newid = Popen(config.COMMAND + 'newid', stdout=PIPE, shell=True)
newid.wait()
notif = Notify.Notification.new(config.app_name,
config._("You have a new identity!"))
notif = Notify.Notification.new(
config.app_name,
config._("You have a new identity!"))
else:
notif = Notify.Notification.new(config.app_name,
config._("Tractor is not running!"))
notif = Notify.Notification.new(
config.app_name,
config._("Tractor is not running!"))
notif.set_timeout(Notify.EXPIRES_DEFAULT)
notif.show()
def on_set_proxy(action, param, app):
'''
clicking on proxy button
'''
checkbox = ui.get('SetProxyItem')
if checkbox.get_active():
action = 'set'
else:
action = 'unset'
task = Popen(config.command + action, stdout=PIPE, shell=True)
task = Popen(config.COMMAND + action, stdout=PIPE, shell=True)
task.wait()
def on_save(action, param, app):
'''
clicking on save button in bridges
'''
textview = ui.get('TextO4Bridge')
buff = textview.get_buffer()
text = buff.get_text(buff.get_start_iter(), buff.get_end_iter(), 0)
regex = re.compile(r'^( )*([Bb][Rr][Ii][Dd][Gg][Ee])?( )*', re.MULTILINE)
get_bridges = Popen(config.command + "bridgesfile", stdout=PIPE,
shell=True)
get_bridges = Popen(
config.COMMAND + "bridgesfile",
stdout=PIPE, shell=True)
bridges = get_bridges.stdout.read()
bridges_file = bridges.decode("utf-8").strip('\n')
if text == regex.sub('Bridge ', text):
......
......@@ -2,18 +2,28 @@
# Released under GPLv3+ License
# Danial Behzadi<dani.behzi@ubuntu.com>, 2019-2021.
"""
Main module for carburetor
"""
from sys import argv
import gi
gi.require_versions({'Gtk': '3.0'})
from gi.repository import Gtk
from sys import argv
from . import ui
from . import actions
class Application(Gtk.Application):
'''
main window of carburetor
'''
def __init__(self, *args, **kwargs):
super().__init__(*args, application_id='org.tractor.carburetor',
**kwargs)
super().__init__(
*args, application_id='org.tractor.carburetor',
**kwargs)
self.window = None
self.prefs = None
self.about = None
......@@ -24,21 +34,21 @@ class Application(Gtk.Application):
def do_activate(self):
if not self.window:
window = self.get_window('MainWindow')
ui.css()
window = ui.get('MainWindow')
self.add_window(window)
self.window = window
self.window.present()
def get_window(self, element):
ui.css()
window = ui.get(element)
return window
def main():
'''
main entrance of app
'''
ui.initialize_builder()
app = Application()
app.run(argv)
if __name__ == '__main__':
main()
......@@ -2,10 +2,16 @@
# Released under GPLv3+ License
# Danial Behzadi<dani.behzi@ubuntu.com>, 2020-2021.
"""
main configurations for carburetor
"""
import gettext
import gi
import locale
import os
import gi
gi.require_versions({'Notify': '0.7', 'Handy': '1'})
from gi.repository import Gio, GLib, Notify, Handy
......@@ -17,10 +23,10 @@ locale.bindtextdomain('messages', locale_dir)
gettext.bindtextdomain('messages', locale_dir)
gettext.textdomain('messages')
_ = gettext.gettext
command = "tractor "
COMMAND = "tractor "
app_name = _("Carburetor")
app_comment = _("Settings for Tractor")
version = "3.3"
VERSION = "3.4"
GLib.set_application_name(app_name)
Notify.init(app_name)
......
......@@ -2,22 +2,35 @@
# Released under GPLv3+ License
# Danial Behzadi<dani.behzi@ubuntu.com>, 2020-2021.
import gi
"""
ui actions
"""
import re
gi.require_versions({'Notify': '0.7'})
from gi.repository import Gio, GLib, Gtk, Notify
from distutils.util import strtobool
from subprocess import PIPE, Popen
import gi
gi.require_versions({'Notify': '0.7'})
from gi.repository import Gio, GLib, Gtk, Notify
from . import config
from . import ui
def is_running():
check = Popen(config.command + 'isrunning', stdout=PIPE, shell=True)
'''
check if tractor is running or not
'''
check = Popen(config.COMMAND + 'isrunning', stdout=PIPE, shell=True)
output = strtobool(check.stdout.read().decode('utf-8').strip('\n'))
return output
def set_progress (stdout, condition):
def set_progress(stdout, condition):
'''
set progress output on main label
'''
try:
line = stdout.readline().decode('utf-8')
label = ui.get('MainLabel')
......@@ -29,7 +42,11 @@ def set_progress (stdout, condition):
pass
return True
def thread_finished (stdout, condition, button, app):
def thread_finished(stdout, condition, button, app):
'''
things to do after process finished
'''
GLib.source_remove(app.io_id)
stdout.close()
app.spinner.stop()
......@@ -39,13 +56,20 @@ def thread_finished (stdout, condition, button, app):
notify(app)
return False
def notify(app):
'''
show notification
'''
notif = Notify.Notification.new(config.app_name, app.notification)
notif.set_timeout(Notify.EXPIRES_DEFAULT)
notif.show()
def set_to_stopped(app):
'''
set status to stopped
'''
image = ui.get('MainImage')
image.set_from_icon_name('process-stop', Gtk.IconSize.DIALOG)
image.show()
......@@ -62,7 +86,11 @@ def set_to_stopped(app):
if app:
app.notification = config._("Tractor is stopped")
def set_to_running(app):
'''
set status to connected
'''
image = ui.get('MainImage')
image.set_from_icon_name('security-high', Gtk.IconSize.DIALOG)
image.show()
......@@ -79,35 +107,50 @@ def set_to_running(app):
if app:
app.notification = config._("Tractor is running")
def set_run_status(app):
'''
set status of conection
'''
if is_running():
set_to_running(app)
else:
set_to_stopped(app)
set_to_stopped(app)
def hide_window(window, data):
'''
hide the window
'''
window.hide()
return True
def is_proxy_set():
'''
checks if proxy was already set
'''
proxy = Gio.Settings.new('org.gnome.system.proxy')
socks = Gio.Settings.new('org.gnome.system.proxy.socks')
if config.dconf.get_boolean('accept-connection'):
ip = '0.0.0.0'
myip = '0.0.0.0'
else:
ip = '127.0.0.1'
myip = '127.0.0.1'
proxy_mode = proxy.get_string('mode')
current_port = socks.get_int('port')
tractor_port = config.dconf.get_int('socks-port')
current_ip = socks.get_string('host')
if proxy_mode=='manual' and current_port==tractor_port and current_ip==ip:
if (proxy_mode == 'manual' and
current_port == tractor_port and
current_ip == myip):
return True
else:
return False
return False
def set_checkbox():
'''
set status of proxy checkbox
'''
checkbox = ui.get('SetProxyItem')
if is_proxy_set():
checkbox.props.active = True
else:
checkbox.props.active = False
checkbox.props.active = False
......@@ -2,16 +2,23 @@
# Released under GPLv3+ License
# Danial Behzadi<dani.behzi@ubuntu.com>, 2020-2021.
import gi
from gi.repository import Gio, Gtk
"""
handlers for ui events
"""
from subprocess import PIPE, Popen
from gi.repository import Gio, Gtk
from . import actions
from . import config
from . import functions
from . import ui
class Handler:
'''
main handler class
'''
def on_MainBox_realize(self, box):
functions.set_run_status(None)
......@@ -20,87 +27,100 @@ class Handler:
def on_ActionExitNode_realize(self, combo):
_ = config._
nodes = {_("Austria"): "au",
_("Bulgaria"): "bg",
_("Canada"): "ca",
_("Czech"): "cz",
_("Finland"): "fi",
_("France"): "fr",
_("Germany"): "de",
_("Ireland"): "ie",
_("Moldova"): "md",
_("Netherlands"): "nl",
_("Norway"): "no",
_("Poland"): "pl",
_("Romania"): "ro",
_("Russia"): "su",
_("Seychelles"): "sc",
_("Singapore"): "sg",
_("Spain"): "es",
_("Sweden"): "se",
_("Switzerland"): "ch",
_("Ukraine"): "ua",
_("United Kingdom"): "uk",
_("United States"): "us"}
nodes = {
_("Austria"): "au",
_("Bulgaria"): "bg",
_("Canada"): "ca",
_("Czech"): "cz",
_("Finland"): "fi",
_("France"): "fr",
_("Germany"): "de",
_("Ireland"): "ie",
_("Moldova"): "md",
_("Netherlands"): "nl",
_("Norway"): "no",
_("Poland"): "pl",
_("Romania"): "ro",
_("Russia"): "su",
_("Seychelles"): "sc",
_("Singapore"): "sg",
_("Spain"): "es",
_("Sweden"): "se",
_("Switzerland"): "ch",
_("Ukraine"): "ua",
_("United Kingdom"): "uk",
_("United States"): "us"}
combo.append("ww", _("Auto (Best)"))
for node in sorted(nodes.keys()):
combo.append(nodes[node], node)
config.dconf.bind("exit-node", combo, "active-id",
Gio.SettingsBindFlags.DEFAULT)
config.dconf.bind(
"exit-node", combo, "active-id",
Gio.SettingsBindFlags.DEFAULT)
def on_ActionAcceptConnection_realize(self, switch):
config.dconf.bind("accept-connection", switch, "active",
Gio.SettingsBindFlags.DEFAULT)
config.dconf.bind(
"accept-connection", switch, "active",
Gio.SettingsBindFlags.DEFAULT)
def on_ActionSocksPort_realize(self, spin):
port = config.dconf.get_int("socks-port")
adjustment = Gtk.Adjustment(value=port, lower=1, upper=65535,
step_increment=1, page_increment=1, page_size=0)
adjustment = Gtk.Adjustment(
value=port, lower=1, upper=65535,
step_increment=1, page_increment=1, page_size=0)
spin.set_adjustment(adjustment)
spin.set_text(str(port))
config.dconf.bind("socks-port", spin, "value",
Gio.SettingsBindFlags.DEFAULT)
config.dconf.bind(
"socks-port", spin, "value",
Gio.SettingsBindFlags.DEFAULT)
def on_ActionDNSPort_realize(self, spin):
port = config.dconf.get_int("dns-port")
adjustment = Gtk.Adjustment(value=port, lower=1, upper=65535,
step_increment=1, page_increment=1, page_size=0)
adjustment = Gtk.Adjustment(
value=port, lower=1, upper=65535,
step_increment=1, page_increment=1, page_size=0)
spin.set_adjustment(adjustment)
spin.set_text(str(port))
config.dconf.bind("dns-port", spin, "value",
Gio.SettingsBindFlags.DEFAULT)
config.dconf.bind(
"dns-port", spin, "value",
Gio.SettingsBindFlags.DEFAULT)
def on_ActionHTTPPort_realize(self, spin):
port = config.dconf.get_int("http-port")
adjustment = Gtk.Adjustment(value=port, lower=1, upper=65535,
step_increment=1, page_increment=1, page_size=0)
adjustment = Gtk.Adjustment(
value=port, lower=1, upper=65535,
step_increment=1, page_increment=1, page_size=0)
spin.set_adjustment(adjustment)
spin.set_text(str(port))
config.dconf.bind("http-port", spin, "value",
Gio.SettingsBindFlags.DEFAULT)
config.dconf.bind(
"http-port", spin, "value",
Gio.SettingsBindFlags.DEFAULT)
def on_ActionBridgeType_realize(self, switch):
config.dconf.bind("use-bridges", switch, "active",
Gio.SettingsBindFlags.DEFAULT)
config.dconf.bind(
"use-bridges", switch, "active",
Gio.SettingsBindFlags.DEFAULT)
def on_TextO4Bridge_realize(self, view):
buff = view.get_buffer()
get_bridges = Popen(config.command + "bridgesfile", stdout=PIPE,
shell=True)
get_bridges = Popen(
config.COMMAND + "bridgesfile", stdout=PIPE,
shell=True)
bridges = get_bridges.stdout.read()
bridges_file = bridges.decode("utf-8").strip('\n')
with open(bridges_file) as file:
text = file.read()
buff.set_text(str(text))
def on_ButtonO4Bridge_clicked(self, button): #TODO: Move this to actions
def on_ButtonO4Bridge_clicked(self, button): # TODO: Move this to actions
actions.on_save(button, None, self)
def on_ActionBridgePlugin_realize(self, chooser):
dconf = config.dconf
def on_file_set(chooser):
filename = chooser.get_filename()
dconf.set_string("obfs4-path", filename)
current_file = Gio.File.new_for_path(dconf.get_string("obfs4-path"))
chooser.set_file(current_file)
chooser.connect("file-set", on_file_set)
......
......@@ -2,10 +2,14 @@
# Released under GPLv3+ License
# Danial Behzadi<dani.behzi@ubuntu.com>, 2020-2021.
"""
handle ui related stuff
"""
import gi
import os
gi.require_versions({'Gtk': '3.0'})
from gi.repository import Gdk, GLib, Gtk
from gi.repository import Gdk, Gtk
from . import config
from . import handler
......@@ -13,19 +17,31 @@ builder = Gtk.Builder()
def initialize_builder():
s_data_dir=config.s_data_dir
'''
connect builder to files and handlers
'''
s_data_dir = config.s_data_dir
builder.add_from_file(s_data_dir+'/about.ui')
builder.add_from_file(s_data_dir+'/main.ui')
builder.add_from_file(s_data_dir+'/preferences.ui')
builder.connect_signals(handler.Handler())
def get(obj):
'''
get object from ui
'''
return builder.get_object(obj)
def css():
'''
apply css to ui
'''
css_provider = Gtk.CssProvider()
css_provider.load_from_path(config.s_data_dir+'/style.css')
screen = Gdk.Screen.get_default()
context = Gtk.StyleContext()
context.add_provider_for_screen(screen, css_provider,
Gtk.STYLE_PROVIDER_PRIORITY_USER)
context.add_provider_for_screen(
screen, css_provider,
Gtk.STYLE_PROVIDER_PRIORITY_USER)
carburetor (3.4) hirsute; urgency=medium
* Some refactors and lintings
-- Danial Behzadi <dani.behzi@ubuntu.com> Tue, 18 May 2021 17:45:00 +0430
carburetor (3.3) groovy; urgency=medium