Yggtk/src/app-window.vala

95 lines
3.2 KiB
Vala
Raw Permalink 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]
private Gtk.Entry peer_ip_entry;
2020-01-25 22:13:54 +00:00
[GtkChild]
private Gtk.Button save_config_button;
2020-01-25 22:13:54 +00:00
[GtkCallback]
private bool on_service_state_switch_state_set (bool state) {
application.activate_action ("set-service-state", new Variant.boolean (state));
return true;
}
2020-01-25 22:13:54 +00:00
[GtkCallback]
private void on_peer_ip_entry_changed () {
save_config_button.visible = peer_ip_entry.text.length > 0;
}
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) {
Object(application: application);
2020-01-28 15:42:33 +00:00
var Parser = new Parser ();
2020-01-25 22:13:54 +00:00
2020-01-28 15:42:33 +00:00
Parser.from_json (peer_ip_entry);
string yggconfout = peer_ip_entry.text;
save_config_button.clicked.connect (() => {
2020-01-28 15:42:33 +00:00
if (peer_ip_entry.text != yggconfout) {
2020-01-29 17:23:01 +00:00
if (yggconfout == "") {
string yggconf;
Process.spawn_command_line_sync ("yggdrasil -genconf -json", out yggconf);
Parser.to_json (peer_ip_entry, yggconf);
} else {
FileUtils.get_contents ("/etc/yggdrasil.conf", out yggconfout);
Parser.to_json (peer_ip_entry, yggconfout);
}
2020-01-28 15:42:33 +00:00
}
2020-01-25 22:13:54 +00:00
});
}
2020-01-28 15:42:33 +00:00
public class Parser : Object {
2020-01-29 17:23:01 +00:00
public void to_json (Entry ip, string yggconf) {
2020-01-25 22:13:54 +00:00
var node = Json.from_string (yggconf);
var obj = node.get_object ();
obj.set_string_member ("Peers", ip.text);
2020-01-28 15:42:33 +00:00
string ptext = Json.to_string (node, true);
2020-01-25 22:13:54 +00:00
2020-02-03 16:27:20 +00:00
FileUtils.set_contents ("/tmp/yggdrasil.conf", ptext);
2020-01-25 22:13:54 +00:00
2020-02-03 16:27:20 +00:00
Process.spawn_command_line_sync ("pkexec cp /tmp/yggdrasil.conf /etc");
2020-01-25 22:13:54 +00:00
}
2020-01-28 15:42:33 +00:00
public void from_json (Entry ip) {
string yggconfout;
FileUtils.get_contents ("/etc/yggdrasil.conf", out yggconfout);
var node = Json.from_string (yggconfout);
var obj = node.get_object ();
string ptext = obj.get_string_member ("Peers");
2020-01-25 22:13:54 +00:00
2020-01-28 15:42:33 +00:00
if (yggconfout != null) {
ip.text = ptext;
}
}
2020-01-25 22:13:54 +00:00
}
}
2020-01-28 15:42:33 +00:00
}