Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ RUNNER_FLAGS := --with-appdir \
--env=AT_SPI_BUS_ADDRESS=unix:path=/run/flatpak/at-spi-bus \
--env=DESKTOP_SESSION=$(DESKTOP_SESSION) \
--env=LANG=$(LANG) \
--env=G_MESSAGES_DEBUG=Tarug \
--env=WAYLAND_DISPLAY=$(WAYLAND_DISPLAY) \
--env=XDG_CURRENT_DESKTOP=$(XDG_CURRENT_DESKTOP) \
--env=XDG_SESSION_DESKTOP=$(XDG_SESSION_DESKTOP) \
Expand Down Expand Up @@ -108,6 +109,9 @@ lsp:
meson:
flatpak build $(RUNNER_FLAGS) $(REPO_DIR) /usr/bin/meson

shell:
flatpak build $(RUNNER_FLAGS) $(REPO_DIR) /usr/bin/bash

test:
flatpak build $(RUNNER_FLAGS) $(REPO_DIR) /usr/bin/meson test -C $(BUILD_DIR)

Expand Down
28 changes: 3 additions & 25 deletions resources/gtk/connection-view.blp
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,6 @@ template $TarugConnectionView: Adw.Bin {
}
}

// $TarugConnectionSidebar sidebar {
// width-request: 300;
// connections: bind template.viewmodel as <$TarugConnectionViewModel>.connections;
// selected-connection: bind template.viewmodel as <$TarugConnectionViewModel>.selected-connection;
// request-new-connection => $add_new_connection();
// request_dup_connection => $dup_connection();
// request_remove_connection => $remove_connection();
// request_connect_database => $active_connection();
// }

[end]
Adw.Bin {
child: WindowHandle {
Expand Down Expand Up @@ -279,7 +269,7 @@ template $TarugConnectionView: Adw.Bin {
}

Entry host_entry {
placeholder-text: "localhost";
placeholder-text: "Example: localhost";
activate => $on_entry_activated();
changed => $on_text_changed();

Expand All @@ -301,7 +291,7 @@ template $TarugConnectionView: Adw.Bin {
}

Entry port_entry {
placeholder-text: "5432";
placeholder-text: "i.e 5432";
activate => $on_entry_activated();
changed => $on_text_changed();

Expand All @@ -313,7 +303,6 @@ template $TarugConnectionView: Adw.Bin {
}

Entry user_entry {
placeholder-text: "postgres";
activate => $on_entry_activated();
changed => $on_text_changed();

Expand All @@ -325,7 +314,6 @@ template $TarugConnectionView: Adw.Bin {
}

PasswordEntry password_entry {
placeholder-text: "";
show-peek-icon: true;
activate => $on_entry_activated();
changed => $on_text_changed();
Expand Down Expand Up @@ -413,7 +401,7 @@ template $TarugConnectionView: Adw.Bin {
spacing: 8;

Spinner spinner {
spinning: bind template.viewmodel as <$TarugConnectionViewModel>.is-connectting;
spinning: bind template.viewmodel as <$TarugConnectionViewModel>.is_pending;
}

Button connect_btn {
Expand All @@ -431,15 +419,5 @@ template $TarugConnectionView: Adw.Bin {
}
};
}

// $TarugConnectionForm form {
// width-request: 800;
// selected-connection: bind sidebar.selected-connection;
// is-connectting: bind template.viewmodel as <$TarugConnectionViewModel>.is-connectting;
// current-state: bind template.viewmodel as <$TarugConnectionViewModel>.current-state;
// menu-model: primary_menu;
// request-database => $active_connection ();
// connections-changed => $save_connections ();
// }
}
}
51 changes: 47 additions & 4 deletions src/services/SQLService.vala
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ namespace Tarug {
var db_url = build_connection_string(conn);
debug("Connecting to %s", db_url);
start_connect (db_url);

/*
* Begin the polling loop to keep checking the connection is good
Reference: https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PQCONNECTSTARTPARAMS
Expand Down Expand Up @@ -122,12 +121,56 @@ namespace Tarug {
}
}

private string build_connection_string(Connection conn) {
private string build_connection_string(Connection conn) throws TarugError {

long port = long.parse (conn.port);
if (port == 0) {
throw new TarugError.CONNECTION_ERROR("Port `%s` must be a number".printf (conn.port));
} else if (port <= 0 || port >= 65535) {
throw new TarugError.CONNECTION_ERROR("Port `%ld` not in range [0-65535]".printf (port));
}

string user = conn.user.strip();
if (user == "") {
throw new TarugError.CONNECTION_ERROR("User must not be blank");
}


string password = conn.password.strip();
if (password == "") {
throw new TarugError.CONNECTION_ERROR("Password must not be blank");
}

string host = conn.host.strip();
if (host == "") {
throw new TarugError.CONNECTION_ERROR("Host must not be blank");
}

string dbname = conn.database.strip();
if (dbname == "") {
throw new TarugError.CONNECTION_ERROR("Database must not be blank");
}

var connection_timeout = settings.get_int("connection-timeout");
var query_timeout = settings.get_int("query-timeout");
string db_url = conn.connection_string(connection_timeout, query_timeout);
var options = @"\'-c statement_timeout=$(query_timeout * 1000)\'";

var builder = new StringBuilder("");
builder.append_printf ("user=%s ", user);
builder.append_printf ("password=%s ", password);
builder.append_printf ("sslmode=%s ", conn.use_ssl ? "verify-full" : "disable");
builder.append_printf ("host=%s ", host);
builder.append_printf ("port=%ld ", port);
builder.append_printf ("dbname=%s ", dbname);
builder.append_printf ("application_name=%s ", Config.APP_NAME);
builder.append_printf ("connect_timeout=%d ", connection_timeout);
builder.append_printf ("options=%s ", options);
if (conn.use_ssl) {
builder.append(@" sslrootcert=$(conn.cert_path)");
}


return db_url;
return(builder.free_and_steal());
}

public async Relation exec_query (Query query) throws TarugError {
Expand Down
26 changes: 9 additions & 17 deletions src/ui/views/ConnectionView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ namespace Tarug {
construct {
setup_paned(paned);
this.viewmodel = autowire<ConnectionViewModel> ();

this.err_dialog = new Adw.AlertDialog("Connection Failed", null) {
default_response = "okay",
};
this.err_dialog.add_response("okay", "OK");

var action_group = new SimpleActionGroup();
action_group.add_action_entries(ACTION_ENTRIES, this);
this.insert_action_group("conn", action_group);
Expand Down Expand Up @@ -162,14 +156,8 @@ namespace Tarug {
// Save ref so it does not be cleaned by GC
this.bindings = create_form_bind_group();

viewmodel.bind_property("selected-connection", this.bindings, "source", BindingFlags.SYNC_CREATE);
viewmodel.bind_property("is-connectting", connect_btn, "sensitive", INVERT_BOOLEAN | SYNC_CREATE);
viewmodel.bind_property("err-msg", this.err_dialog, "body", SYNC_CREATE);
viewmodel.notify["current-state"].connect(() => {
if (viewmodel.current_state == ConnectionViewModel.State.ERROR) {
this.err_dialog.present(this);
}
});
viewmodel.bind_property("selected_connection", this.bindings, "source", BindingFlags.SYNC_CREATE);
viewmodel.bind_property("is_pending", connect_btn, "sensitive", INVERT_BOOLEAN | SYNC_CREATE);

selection_model.bind_property("selected", viewmodel, "selected-connection",
DEFAULT | BIDIRECTIONAL, from_selected, to_selected);
Expand All @@ -183,11 +171,17 @@ namespace Tarug {

return(true);
});

viewmodel.connect_database_failed.connect((error) => {
var d = new Gtk.AlertDialog ("Connection Failed") {
detail = error
};
d.show (get_parrent_window (this));
});
}

private BindingGroup create_form_bind_group (){
var binddings = new BindingGroup();
// debug ("set_up connection form bindings group");
binddings.bind("name", name_entry, "text", SYNC_CREATE | BIDIRECTIONAL);
binddings.bind("host", host_entry, "text", SYNC_CREATE | BIDIRECTIONAL);
binddings.bind("port", port_entry, "text", SYNC_CREATE | BIDIRECTIONAL);
Expand All @@ -196,8 +190,6 @@ namespace Tarug {
binddings.bind("database", database_entry, "text", SYNC_CREATE | BIDIRECTIONAL);
binddings.bind("use_ssl", ssl_switch, "active", SYNC_CREATE | BIDIRECTIONAL);
binddings.bind("cert_path", cert_path, "text", SYNC_CREATE | BIDIRECTIONAL);
// debug ("set_up binddings done");


return(binddings);
}
Expand Down
10 changes: 10 additions & 0 deletions src/utils/types.vala
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ namespace Tarug {
return result.error == null;
}

public async void sleep(int milliseconds) {
Source source = new TimeoutSource (milliseconds);
source.set_callback (() => {
sleep.callback ();
return false; // run once
});
source.attach (MainContext.default ());
yield;
}

public class Vec<T>: Object {
static int DEFAULT_CAPACITY = 64;

Expand Down
46 changes: 10 additions & 36 deletions src/viewmodels/ConnectionViewModel.vala
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
namespace Tarug {
public class ConnectionViewModel : BaseViewModel {
public enum State {
IDLE,
CONNECTING,
ERROR
}


uint timeout_id = 0;
public ConnectionRepository repository { get; private set; }
public SQLService sql_service { get; private set; }
public NavigationService navigation_service { get; private set; }



// States

public State current_state { get; private set; default = State.IDLE; }
public string err_msg { get; private set; default = "hello world"; }
// Props
public bool is_pending { get; private set; default = false;}
public ObservableList<Connection> connections { get; private set; default = new ObservableList<Connection> (); }
public Connection ? selected_connection { get; set; }

/** True when trying to establish a connection util know results. */
public bool is_connectting { get; set; default = false; }
// Signals
public signal void connect_database_failed(string error_message);


public ConnectionViewModel(ConnectionRepository repository, SQLService sql_service, NavigationService navigation_service){
base();
Expand All @@ -36,14 +28,6 @@ namespace Tarug {
if (connections.empty()) {
// new_connection();
}

this.bind_property("current-state", this, "is-connectting", SYNC_CREATE, from_state_to_connecting);

// Auto save data each 10 secs in case app crash.
// Timeout.add_seconds (10, () => {
// repository.save (connections.to_list ());
// return Source.CONTINUE;
// }, Priority.LOW);
}

public void new_connection (){
Expand Down Expand Up @@ -87,20 +71,21 @@ namespace Tarug {
}

public async void active_connection (Connection connection){
this.current_state = State.CONNECTING;
try {
this.is_pending = true;
yield sql_service.connect_db (connection);

EventBus.instance().connection_active(connection);
} catch (TarugError err) {
this.err_msg = err.message.dup();
debug("Error: %s", err.message);
this.current_state = State.ERROR;
this.connect_database_failed(err.message.dup ());
this.is_pending = false;
return;
}
this.current_state = State.IDLE;
this.is_pending = false;
}


public List<Connection> export_connections (){
return(repository.find_all());
}
Expand All @@ -116,16 +101,5 @@ namespace Tarug {
return(Source.REMOVE);
});
}

private bool from_state_to_connecting (Binding binding, Value from, ref Value to){
ConnectionViewModel.State state = (ConnectionViewModel.State) from.get_enum();
if (state == ConnectionViewModel.State.CONNECTING) {
to.set_boolean(true);
} else {
to.set_boolean(false);
}

return(true);
}
}
}