Yggtk/src/main.vala

286 lines
5.8 KiB
Vala
Raw Normal View History

2020-01-17 22:13:39 +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
* (at your option) 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;
int main (string[] args) {
Gtk.init (ref args);
2020-01-17 22:13:39 +00:00
try {
2020-01-17 22:13:39 +00:00
var builder = new Builder ();
builder.add_from_resource ("/org/yggtk/yggtk/ui/main.ui");
builder.connect_signals (null);
var window = builder.get_object ("window") as Window;
var browse = builder.get_object ("browse") as Button;
2020-01-17 22:13:39 +00:00
var yggdrasil = builder.get_object ("yggdrasil") as Button;
2020-01-17 22:13:39 +00:00
yggdrasil.clicked.connect (() => {
2020-01-17 22:13:39 +00:00
2020-01-24 22:34:51 +00:00
download ();
2020-01-17 22:13:39 +00:00
});
2020-01-17 22:13:39 +00:00
var status = builder.get_object ("status") as Switch;
2020-01-17 22:13:39 +00:00
check_status (status);
2020-01-17 22:13:39 +00:00
status.notify["active"].connect (() => {
2020-01-17 22:13:39 +00:00
if (status.get_active ()) {
2020-01-17 22:13:39 +00:00
start ();
2020-01-17 22:13:39 +00:00
} else {
2020-01-17 22:13:39 +00:00
stop ();
2020-01-17 22:13:39 +00:00
}
2020-01-17 22:13:39 +00:00
check_status (status);
2020-01-17 22:13:39 +00:00
});
2020-01-17 22:13:39 +00:00
var yggtk = builder.get_object ("yggtk") as Button;
2020-01-17 22:13:39 +00:00
yggtk.clicked.connect (() => {
2020-01-17 22:13:39 +00:00
2020-01-24 22:34:51 +00:00
update ();
});
var ip = builder.get_object ("label_ip") as Entry;
ip.notify["text"].connect (() => {
2020-01-25 15:01:47 +00:00
if (ip.text != "") {
browse.label = "OK";
} else {
browse.label = "Browse";
}
2020-01-24 22:34:51 +00:00
});
string yggconf;
Process.spawn_command_line_sync ("yggdrasil -genconf -json", out yggconf);
browse.clicked.connect (() => {
if (browse.label == "OK") {
parse (yggconf, ip);
} else {
2020-01-25 15:01:47 +00:00
Process.spawn_command_line_sync (
"xdg-open https://github.com/yggdrasil-network/public-peers"
);
2020-01-24 22:34:51 +00:00
}
2020-01-17 22:13:39 +00:00
});
2020-01-17 22:13:39 +00:00
2020-01-25 15:01:47 +00:00
window.show ();
Gtk.main ();
2020-01-17 22:13:39 +00:00
} catch (Error e) {
2020-01-17 22:13:39 +00:00
print ("Error load UI: %s\n", e.message);
2020-01-17 22:13:39 +00:00
return 1;
2020-01-17 22:13:39 +00:00
}
2020-01-17 22:13:39 +00:00
return 0;
2020-01-17 22:13:39 +00:00
}
int download () {
try {
2020-01-17 22:13:39 +00:00
string pm;
Process.spawn_command_line_sync ("pkexec pacman -S yggdrasil --noconfirm", out pm);
if (pm == "Cannot run program pacman: No such file or directory\n") {
Process.spawn_command_line_sync (
"wget https://2375-115685026-gh.circle-artifacts.com/0/yggdrasil-0.3.12-amd64.deb"
);
Process.spawn_command_line_sync ("pkexec dpkg -i yggdrasil-0.3.12-amd64.deb");
2020-01-17 22:13:39 +00:00
}
} catch (Error e) {
2020-01-17 22:13:39 +00:00
print ("Error download: %s\n", e.message);
2020-01-17 22:13:39 +00:00
return 1;
2020-01-17 22:13:39 +00:00
}
2020-01-17 22:13:39 +00:00
return 0;
2020-01-17 22:13:39 +00:00
}
int update () {
try {
2020-01-17 22:13:39 +00:00
2020-01-24 09:16:34 +00:00
unowned string repo_url = Environment.get_variable ("YGG_UPDATE_REPO");
2020-01-24 15:22:20 +00:00
string temp_dir = Utils.mktempdir ();
2020-01-24 09:16:34 +00:00
if (repo_url == null) {
2020-01-24 22:34:51 +00:00
2020-01-24 09:16:34 +00:00
repo_url = "https://git.macaw.me/plant_1312/Yggtk.git";
2020-01-24 22:34:51 +00:00
2020-01-24 09:16:34 +00:00
}
Process.spawn_command_line_sync (@"git clone $repo_url $temp_dir");
Process.spawn_command_line_sync (@"meson $temp_dir/build $temp_dir");
Process.spawn_command_line_sync (@"ninja -C $temp_dir/build");
Process.spawn_command_line_sync (@"cp $temp_dir/build/src/yggtk .");
2020-01-24 09:16:34 +00:00
Process.spawn_command_line_sync (@"rm -rf $temp_dir");
Process.spawn_command_line_sync ("./yggtk");
2020-01-17 22:13:39 +00:00
Gtk.main_quit ();
2020-01-17 22:13:39 +00:00
} catch (Error e) {
2020-01-17 22:13:39 +00:00
print ("Error update: %s\n", e.message);
2020-01-17 22:13:39 +00:00
return 1;
2020-01-17 22:13:39 +00:00
}
2020-01-17 22:13:39 +00:00
return 0;
2020-01-17 22:13:39 +00:00
}
int stop () {
try {
2020-01-17 22:13:39 +00:00
Process.spawn_command_line_sync ("pkexec rc-service yggdrasil stop");
Process.spawn_command_line_sync ("pkexec systemctl stop yggdrasil");
2020-01-17 22:13:39 +00:00
} catch (Error e) {
2020-01-17 22:13:39 +00:00
print ("Error stop Yggdrasil: %s\n", e.message);
2020-01-17 22:13:39 +00:00
return 1;
2020-01-17 22:13:39 +00:00
}
2020-01-17 22:13:39 +00:00
return 0;
2020-01-17 22:13:39 +00:00
}
int start () {
try {
2020-01-17 22:13:39 +00:00
Process.spawn_command_line_sync ("pkexec rc-service yggdrasil start");
Process.spawn_command_line_sync ("pkexec systemctl start yggdrasil");
2020-01-17 22:13:39 +00:00
} catch (Error e) {
2020-01-17 22:13:39 +00:00
print ("Error start Yggdrasil: %s\n", e.message);
2020-01-17 22:13:39 +00:00
return 1;
2020-01-17 22:13:39 +00:00
}
2020-01-17 22:13:39 +00:00
return 0;
2020-01-17 22:13:39 +00:00
}
2020-01-24 22:34:51 +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");
2020-01-17 22:13:39 +00:00
2020-01-24 22:34:51 +00:00
} catch (Error e) {
print ("Error parse JSON: %s\n", e.message);
}
2020-01-17 22:13:39 +00:00
return 0;
2020-01-17 22:13:39 +00:00
}
int check_status (Switch status) {
try {
2020-01-17 22:13:39 +00:00
string yggstatus;
Process.spawn_command_line_sync ("rc-service yggdrasil status", out yggstatus);
2020-01-17 22:13:39 +00:00
if (yggstatus == "bash: rc-service: command not found\n") {
2020-01-17 22:13:39 +00:00
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);
}
2020-01-17 22:13:39 +00:00
} else {
2020-01-17 22:13:39 +00:00
if (yggstatus == " * status: stopped\n") {
2020-01-17 22:13:39 +00:00
status.state_set (false);
2020-01-17 22:13:39 +00:00
} else {
2020-01-17 22:13:39 +00:00
status.state_set (true);
2020-01-17 22:13:39 +00:00
}
2020-01-17 22:13:39 +00:00
}
2020-01-17 22:13:39 +00:00
} catch (Error e) {
2020-01-17 22:13:39 +00:00
print ("Error check status: %s\n", e.message);
2020-01-17 22:13:39 +00:00
return 1;
2020-01-17 22:13:39 +00:00
}
2020-01-17 22:13:39 +00:00
return 0;
2020-01-17 22:13:39 +00:00
}