Yggtk/src/app-window.vala

100 lines
2.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 {
2020-01-28 00:32:36 +00:00
[GtkTemplate (ui = "/org/yggtk/yggtk/ui/app-window.ui")]
2020-01-25 22:13:54 +00:00
public class AppWindow : ApplicationWindow {
[GtkChild]
private Gtk.Switch service_state_switch;
2020-01-25 22:13:54 +00:00
[GtkChild]
2020-01-28 00:32:36 +00:00
private Gtk.Button browse_peers_button;
2020-01-25 22:13:54 +00:00
[GtkChild]
2020-01-28 00:32:36 +00:00
private Gtk.Entry peer_ip_entry;
2020-01-25 22:13:54 +00:00
[GtkCallback]
private bool on_service_state_switch_state_set (bool state) {
this.application.activate_action ("set-service-state", new Variant.boolean (state));
return true;
}
2020-01-25 22:13:54 +00:00
public void set_service_state (bool state) {
service_state_switch.set_state (state);
}
2020-01-25 22:13:54 +00:00
public AppWindow (Gtk.Application application) {
2020-01-25 22:13:54 +00:00
Object(application: application);
2020-01-25 22:13:54 +00:00
2020-01-28 00:32:36 +00:00
peer_ip_entry.notify["text"].connect (() => {
2020-01-25 22:13:54 +00:00
2020-01-28 00:32:36 +00:00
if (peer_ip_entry.text != "") {
2020-01-25 22:13:54 +00:00
2020-01-28 00:32:36 +00:00
browse_peers_button.label = "OK";
2020-01-25 22:13:54 +00:00
} else {
2020-01-28 00:32:36 +00:00
browse_peers_button.label = "Browse";
2020-01-25 22:13:54 +00:00
}
});
2020-01-28 00:32:36 +00:00
browse_peers_button.clicked.connect (() => {
2020-01-25 22:13:54 +00:00
2020-01-28 00:32:36 +00:00
if (browse_peers_button.label == "OK") {
2020-01-25 22:13:54 +00:00
string yggconf;
Process.spawn_command_line_sync ("yggdrasil -genconf -json", out yggconf);
2020-01-28 00:32:36 +00:00
parse (yggconf, peer_ip_entry);
2020-01-25 22:13:54 +00:00
}
});
}
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;
}
}
}