Yggtk/src/application.vala

160 lines
5.8 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 {
public class Application : Gtk.Application {
private YggdrasilService yggdrasil_service = null;
private AppWindow window = null;
public Application () {
Object (
application_id: "org.yggtk.yggtk",
flags: ApplicationFlags.FLAGS_NONE
);
init_yggdrasil_service ();
add_actions ();
}
private void init_yggdrasil_service () {
if (Utils.is_command_available ("rc-service")) {
stdout.printf ("Found OpenRC init system\n");
yggdrasil_service = new YggdrasilServiceOpenRC ();
} else if (Utils.is_command_available ("systemctl")) {
stdout.printf ("Found Systemd init system\n");
yggdrasil_service = new YggdrasilServiceSystemd ();
} else {
stderr.printf ("Unable to determine init system");
Gtk.main_quit ();
}
}
private void add_actions () {
SimpleAction update_yggtk_action = new SimpleAction ("update-yggtk", null);
update_yggtk_action.activate.connect (update_yggtk);
add_action (update_yggtk_action);
SimpleAction update_yggdrasil_action = new SimpleAction ("update-yggdrasil", null);
update_yggdrasil_action.activate.connect (update_yggdrasil);
add_action (update_yggdrasil_action);
SimpleAction set_service_state_action = new SimpleAction ("set-service-state", VariantType.BOOLEAN);
set_service_state_action.activate.connect (set_service_state);
add_action (set_service_state_action);
SimpleAction browse_peers_action = new SimpleAction ("browse-peers", null);
browse_peers_action.activate.connect (browse_peers);
add_action (browse_peers_action);
}
private void set_service_state (SimpleAction action, Variant? state) {
try {
if (yggdrasil_service.get_status () == state.get_boolean ()) {
return;
}
if (state.get_boolean ()) {
yggdrasil_service.start ();
} else {
yggdrasil_service.stop ();
}
display_current_service_state ();
} catch (SpawnError e) {
stderr.printf ("Unable to change service state: %s\n", e.message);
}
}
private bool display_current_service_state () {
try {
if (window != null) {
window.set_service_state (yggdrasil_service.get_status ());
}
} catch (SpawnError e) {
stderr.printf ("Unable to get service state: %s\n", e.message);
}
return true;
}
private void update_yggtk () {
try {
unowned string repo_url = Environment.get_variable ("YGG_UPDATE_REPO");
string temp_dir = Utils.mktempdir ();
if (repo_url == null) {
repo_url = "https://git.macaw.me/plant_1312/Yggtk.git";
}
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 .");
Process.spawn_command_line_sync (@"rm -rf $temp_dir");
Process.spawn_command_line_sync ("./yggtk");
Gtk.main_quit ();
} catch (Error e) {
print ("Error update: %s\n", e.message);
}
}
private void update_yggdrasil () {
try {
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");
}
} catch (Error e) {
print ("Error download: %s\n", e.message);
}
}
private void browse_peers () {
try {
Process.spawn_command_line_sync (
"xdg-open https://github.com/yggdrasil-network/public-peers"
);
} catch (SpawnError e) {
stderr.printf ("Unable to open link with xdg-open: %s\n", e.message);
}
}
protected override void activate () {
if (window == null) {
window = new AppWindow (this);
}
display_current_service_state ();
Timeout.add_seconds (1, display_current_service_state);
window.show ();
}
public static int main (string[] args) {
Application app = new Application ();
return app.run (args);
}
}
}