use YggdrasilService to control service state

This commit is contained in:
Nikolay Brovko 2020-01-26 21:33:54 +03:00
parent 6f7cfb10d0
commit bb67130d5e
No known key found for this signature in database
GPG Key ID: 32258A3DEC9B6F07
3 changed files with 52 additions and 58 deletions

View File

@ -17,9 +17,10 @@
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkSwitch" id="status_switch">
<object class="GtkSwitch" id="service_state_switch">
<property name="visible">True</property>
<property name="can_focus">True</property>
<signal name="state-set" handler="on_service_state_switch_state_set" swapped="no"/>
</object>
<packing>
<property name="x">275</property>

View File

@ -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 {

View File

@ -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 ();
}