Skip to content

Commit

Permalink
Merge pull request #7 from torikulhabib/master
Browse files Browse the repository at this point in the history
Host
  • Loading branch information
torikulhabib authored Nov 20, 2021
2 parents 4e7b132 + 63bb96c commit 9877b59
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 20 deletions.
6 changes: 4 additions & 2 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace Gabut {
get_css_online.begin ("https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css", create_folder (".bootstrap.min.css"));
}
var gabutserver = new GabutServer ();
gabutserver.set_listent_all.begin (int.parse (get_dbsetting (DBSettings.PORTLOCAL)));
gabutserver.set_listent.begin (int.parse (get_dbsetting (DBSettings.PORTLOCAL)));
gabutserver.send_post_data.connect (dialog_server);
gabutserver.address_url.connect ((url, options, later, linkmode)=> {
gabutwindow.add_url_box (url, options, later, linkmode);
Expand All @@ -78,11 +78,13 @@ namespace Gabut {
gabutserver.stop_server ();
});
gabutwindow.get_host.connect (()=> {
gabutserver.stop_server ();
gabutserver.set_listent.begin (int.parse (get_dbsetting (DBSettings.PORTLOCAL)));
return gabutserver.get_address ();
});
gabutwindow.restart_server.connect (()=> {
gabutserver.stop_server ();
gabutserver.set_listent_all.begin (int.parse (get_dbsetting (DBSettings.PORTLOCAL)));
gabutserver.set_listent.begin (int.parse (get_dbsetting (DBSettings.PORTLOCAL)));
});
downloaders = new GLib.List<Downloader> ();
succesdls = new GLib.List<SuccesDialog> ();
Expand Down
30 changes: 19 additions & 11 deletions src/GabutServer.vala
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,20 @@ namespace Gabut {
public signal GLib.List<string> get_dl_row (int status);
private SourceFunc callback;

public async void set_listent_all (int port) throws Error {
callback = set_listent_all.callback;
public async void set_listent (int port) throws Error {
callback = set_listent.callback;
this.add_handler ("/", home_handler);
this.add_handler ("/Upload", upload_handler);
this.add_handler ("/Downloading", gabut_handler);
this.add_handler ("/Paused", gabut_handler);
this.add_handler ("/Complete", gabut_handler);
this.add_handler ("/Waiting", gabut_handler);
this.add_handler ("/Error", gabut_handler);
this.listen_all (port, Soup.ServerListenOptions.IPV4_ONLY);
if (!bool.parse (get_dbsetting (DBSettings.IPLOCAL))) {
this.listen_all (port, Soup.ServerListenOptions.IPV4_ONLY);
} else {
this.listen_local (port, Soup.ServerListenOptions.IPV4_ONLY);
}
yield;
}

Expand Down Expand Up @@ -171,14 +175,18 @@ namespace Gabut {
}

public string get_address () {
try {
var resolver = Resolver.get_default ();
var addresses = resolver.lookup_by_name ("www.google.com");
var client = new SocketClient ();
var conn = client.connect (new InetSocketAddress (addresses.nth_data (0), 80));
InetSocketAddress local = conn.get_local_address () as InetSocketAddress;
return @"http://$(local.get_address ()):$(get_uris ().nth_data (0).port)";
} catch (Error e) {
if (!bool.parse (get_dbsetting (DBSettings.IPLOCAL))) {
try {
var resolver = Resolver.get_default ();
var addresses = resolver.lookup_by_name ("www.google.com");
var client = new SocketClient ();
var conn = client.connect (new InetSocketAddress (addresses.nth_data (0), 80));
InetSocketAddress local = conn.get_local_address () as InetSocketAddress;
return @"http://$(local.get_address ()):$(get_uris ().nth_data (0).port)";
} catch (Error e) {
return @"$(e.message)";
}
} else {
return @"http://$(get_listeners ().nth_data (0).local_address)";
}
}
Expand Down
23 changes: 17 additions & 6 deletions src/QrCode.vala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ namespace Gabut {
public signal string get_host ();
private Gtk.Image imageqr;
private Gtk.LinkButton linkbutton;
private Gtk.Button host_button;
private bool local_server;

public QrCode (Gtk.Application application) {
Object (application: application,
Expand Down Expand Up @@ -84,7 +86,7 @@ namespace Gabut {
header.pack_start (header_grid);

imageqr = new Gtk.Image ();
linkbutton = new Gtk.LinkButton (get_dbsetting (DBSettings.IPLOCAL));
linkbutton = new Gtk.LinkButton ("");

var link_box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0) {
margin_top = 10,
Expand All @@ -98,8 +100,8 @@ namespace Gabut {
destroy ();
});

var host_button = new Gtk.Button.with_label (_("Reload"));
host_button.clicked.connect (reload_host);
host_button = new Gtk.Button.with_label (_("Share Host"));
host_button.clicked.connect (share_server);

var box_action = new Gtk.Grid () {
width_request = 200,
Expand Down Expand Up @@ -131,14 +133,23 @@ namespace Gabut {

public override void show () {
base.show ();
create_qrcode (get_dbsetting (DBSettings.IPLOCAL));
local_server = bool.parse (get_dbsetting (DBSettings.IPLOCAL));
Idle.add (load_host);
}

private void reload_host () {
private void share_server () {
local_server = !local_server;
set_dbsetting (DBSettings.IPLOCAL, @"$(local_server)");
load_host ();
}

private bool load_host () {
host_button.label = local_server? _("Share Host") : _("Local Host");
string host = get_host ();
create_qrcode (set_dbsetting (DBSettings.IPLOCAL, host));
create_qrcode (host);
linkbutton.uri = host;
linkbutton.label = host;
return false;
}

private void create_qrcode (string strinput) {
Expand Down
2 changes: 1 addition & 1 deletion src/Utils.vala
Original file line number Diff line number Diff line change
Expand Up @@ -1433,7 +1433,7 @@ namespace Gabut {
portlocal TEXT NOT NULL,
seedtime TEXT NOT NULL);
INSERT INTO settings (id, rpcport, maxtries, connserver, timeout, dir, retry, rpcsize, btmaxpeers, diskcache, maxactive, bttimeouttrack, split, maxopenfile, dialognotif, systemnotif, onbackground, iplocal, portlocal, seedtime)
VALUES (1, \"6807\", \"5\", \"6\", \"60\", \"$(dir)\", \"0\", \"2097152\", \"55\", \"16777216\", \"5\", \"60\", \"5\", \"100\", \"true\", \"true\", \"true\", \"127.0.0.1\", \"2021\", \"0\");", null, null);
VALUES (1, \"6807\", \"5\", \"6\", \"60\", \"$(dir)\", \"0\", \"2097152\", \"55\", \"16777216\", \"5\", \"60\", \"5\", \"100\", \"true\", \"true\", \"true\", \"true\", \"2021\", \"0\");", null, null);
return opendb;
}

Expand Down

0 comments on commit 9877b59

Please sign in to comment.