From bb67130d5e0ada6703148d1bc850013a7a4e45ca Mon Sep 17 00:00:00 2001 From: Nikolay Brovko Date: Sun, 26 Jan 2020 21:33:54 +0300 Subject: [PATCH] use YggdrasilService to control service state --- data/ui/main.ui | 3 +- src/app-window.vala | 68 ++++++++------------------------------------ src/application.vala | 39 ++++++++++++++++++++++++- 3 files changed, 52 insertions(+), 58 deletions(-) diff --git a/data/ui/main.ui b/data/ui/main.ui index 0a42215..2b784b4 100644 --- a/data/ui/main.ui +++ b/data/ui/main.ui @@ -17,9 +17,10 @@ True False - + True True + 275 diff --git a/src/app-window.vala b/src/app-window.vala index 61b886a..1440abc 100644 --- a/src/app-window.vala +++ b/src/app-window.vala @@ -21,7 +21,7 @@ namespace Yggtk { public class AppWindow : ApplicationWindow { [GtkChild] - private Gtk.Switch status_switch; + private Gtk.Switch service_state_switch; [GtkChild] private Gtk.CheckButton portable_checkbutton; @@ -32,27 +32,21 @@ namespace Yggtk { [GtkChild] private Gtk.Entry ip_entry; + [GtkCallback] + private bool on_service_state_switch_state_set (bool state) { + this.application.activate_action ("set-service-state", new Variant.boolean (state)); + return true; + } + + public void set_service_state (bool state) { + service_state_switch.set_state (state); + } + public AppWindow (Gtk.Application application) { Object(application: application); - check_status (status_switch); - - status_switch.notify["active"].connect (() => { - - if (status_switch.get_active ()) { - - start (); - - } else { - - stop (); - - } - - check_status (status_switch); - - }); + check_status (service_state_switch); ip_entry.notify["text"].connect (() => { @@ -135,44 +129,6 @@ namespace Yggtk { } - int stop () { - - try { - - Process.spawn_command_line_sync ("pkexec rc-service yggdrasil stop"); - Process.spawn_command_line_sync ("pkexec systemctl stop yggdrasil"); - - } catch (Error e) { - - print ("Error stop Yggdrasil: %s\n", e.message); - - return 1; - - } - - return 0; - - } - - int start () { - - try { - - Process.spawn_command_line_sync ("pkexec rc-service yggdrasil start"); - Process.spawn_command_line_sync ("pkexec systemctl start yggdrasil"); - - } catch (Error e) { - - print ("Error start Yggdrasil: %s\n", e.message); - - return 1; - - } - - return 0; - - } - int parse (string yggconf, Entry ip) { try { diff --git a/src/application.vala b/src/application.vala index bf3aece..2d5e868 100644 --- a/src/application.vala +++ b/src/application.vala @@ -21,6 +21,8 @@ namespace Yggtk { private YggdrasilService yggdrasil_service = null; + private AppWindow window = null; + public Application () { Object ( application_id: "org.yggtk.yggtk", @@ -51,8 +53,40 @@ namespace Yggtk { SimpleAction update_yggdrasil_action = new SimpleAction ("update-yggdrasil", null); update_yggdrasil_action.activate.connect (update_yggdrasil); add_action (update_yggdrasil_action); + + SimpleAction set_service_state_action = new SimpleAction ("set-service-state", VariantType.BOOLEAN); + set_service_state_action.activate.connect (set_service_state); + add_action (set_service_state_action); } + private void set_service_state (SimpleAction action, Variant? state) { + try { + if (yggdrasil_service.get_status () == state.get_boolean ()) { + return; + } + + if (state.get_boolean ()) { + yggdrasil_service.start (); + } else { + yggdrasil_service.stop (); + } + + display_current_service_state (); + } catch (SpawnError e) { + stderr.printf ("Unable to change service state: %s\n", e.message); + } + } + + private void display_current_service_state () { + try { + if (window != null) { + window.set_service_state (yggdrasil_service.get_status ()); + } + } catch (SpawnError e) { + stderr.printf ("Unable to get service state: %s\n", e.message); + } + } + private void update_yggtk () { try { unowned string repo_url = Environment.get_variable ("YGG_UPDATE_REPO"); @@ -92,7 +126,10 @@ namespace Yggtk { } protected override void activate () { - AppWindow window = new AppWindow (this); + if (window == null) { + window = new AppWindow (this); + } + display_current_service_state (); window.show (); }