Yggtk/src/app-window.vala

106 lines
2.7 KiB
Vala

/**
* 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/app-window.ui")]
public class AppWindow : ApplicationWindow {
[GtkChild]
private Gtk.Switch service_state_switch;
[GtkChild]
private Gtk.Button browse_peers_button;
[GtkChild]
private Gtk.Entry peer_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);
peer_ip_entry.notify["text"].connect (() => {
if (peer_ip_entry.text != "") {
browse_peers_button.label = "OK";
} else {
browse_peers_button.label = "Browse";
}
});
browse_peers_button.clicked.connect (() => {
if (browse_peers_button.label == "OK") {
string yggconf;
Process.spawn_command_line_sync ("yggdrasil -genconf -json", out yggconf);
parse (yggconf, peer_ip_entry);
} else {
Process.spawn_command_line_sync (
"xdg-open https://github.com/yggdrasil-network/public-peers"
);
}
});
}
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;
}
}
}