Yggtk/src/app-window.vala

202 lines
4.6 KiB
Vala
Raw Normal View History

2020-01-25 22:13:54 +00:00
/**
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using Gtk;
namespace Yggtk {
[GtkTemplate (ui = "/org/yggtk/yggtk/ui/main.ui")]
public class AppWindow : ApplicationWindow {
[GtkChild]
private Gtk.Switch status_switch;
[GtkChild]
private Gtk.CheckButton portable_checkbutton;
[GtkChild]
private Gtk.Button browse_button;
[GtkChild]
private Gtk.Entry ip_entry;
public AppWindow (Gtk.Application application) {
Object(application: application);
2020-01-25 22:13:54 +00:00
check_status (status_switch);
status_switch.notify["active"].connect (() => {
if (status_switch.get_active ()) {
start ();
} else {
stop ();
}
check_status (status_switch);
});
ip_entry.notify["text"].connect (() => {
if (ip_entry.text != "") {
browse_button.label = "OK";
} else {
browse_button.label = "Browse";
}
});
browse_button.clicked.connect (() => {
if (browse_button.label == "OK") {
string yggconf;
Process.spawn_command_line_sync ("yggdrasil -genconf -json", out yggconf);
parse (yggconf, ip_entry);
} else {
Process.spawn_command_line_sync (
"xdg-open https://github.com/yggdrasil-network/public-peers"
);
}
});
}
int check_status (Switch status) {
try {
string yggstatus;
Process.spawn_command_line_sync ("rc-service yggdrasil status", out yggstatus);
if (yggstatus == "bash: rc-service: command not found\n") {
Process.spawn_command_line_sync ("systemctl status yggdrasil | grep running -c", out yggstatus);
if (yggstatus == "0") {
status.state_set (false);
} else {
status.state_set (true);
}
} else {
if (yggstatus == " * status: stopped\n") {
status.state_set (false);
} else {
status.state_set (true);
}
}
} catch (Error e) {
print ("Error check status: %s\n", e.message);
return 1;
}
return 0;
}
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 {
var node = Json.from_string (yggconf);
var obj = node.get_object ();
obj.set_string_member ("Peers", ip.text);
string ptext = Json.to_string (node,true);
string file = "yggdrasil.conf";
FileUtils.set_contents (file, ptext);
Process.spawn_command_line_sync ("pkexec cp yggdrasil.conf /etc");
} catch (Error e) {
print ("Error parse JSON: %s\n", e.message);
}
return 0;
}
}
}