2020-01-25 22:31:37 +00:00
|
|
|
/**
|
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
|
2020-01-25 22:31:37 +00:00
|
|
|
* any later version.
|
2020-01-17 22:13:39 +00:00
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
2020-01-25 22:31:37 +00:00
|
|
|
namespace Yggtk {
|
2020-01-17 22:13:39 +00:00
|
|
|
|
2020-01-25 22:31:37 +00:00
|
|
|
public class Application : Gtk.Application {
|
2020-01-17 22:13:39 +00:00
|
|
|
|
2020-01-26 11:14:26 +00:00
|
|
|
private YggdrasilService yggdrasil_service = null;
|
|
|
|
|
2020-01-26 18:33:54 +00:00
|
|
|
private AppWindow window = null;
|
|
|
|
|
2020-01-25 22:31:37 +00:00
|
|
|
public Application () {
|
|
|
|
Object (
|
|
|
|
application_id: "org.yggtk.yggtk",
|
|
|
|
flags: ApplicationFlags.FLAGS_NONE
|
|
|
|
);
|
2020-01-26 11:14:26 +00:00
|
|
|
init_yggdrasil_service ();
|
2020-01-25 23:26:19 +00:00
|
|
|
add_actions ();
|
|
|
|
}
|
|
|
|
|
2020-01-26 11:14:26 +00:00
|
|
|
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 ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-25 23:26:19 +00:00
|
|
|
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);
|
2020-01-26 18:33:54 +00:00
|
|
|
|
|
|
|
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);
|
2020-01-25 23:26:19 +00:00
|
|
|
}
|
|
|
|
|
2020-01-26 18:33:54 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-26 18:56:04 +00:00
|
|
|
private bool display_current_service_state () {
|
2020-01-26 18:33:54 +00:00
|
|
|
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);
|
|
|
|
}
|
2020-01-26 18:56:04 +00:00
|
|
|
|
|
|
|
return true;
|
2020-01-26 18:33:54 +00:00
|
|
|
}
|
|
|
|
|
2020-01-25 23:26:19 +00:00
|
|
|
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);
|
|
|
|
}
|
2020-01-25 22:31:37 +00:00
|
|
|
}
|
2020-01-17 22:13:39 +00:00
|
|
|
|
2020-01-25 22:31:37 +00:00
|
|
|
protected override void activate () {
|
2020-01-26 18:33:54 +00:00
|
|
|
if (window == null) {
|
|
|
|
window = new AppWindow (this);
|
|
|
|
}
|
|
|
|
display_current_service_state ();
|
2020-01-26 18:56:04 +00:00
|
|
|
Timeout.add_seconds (1, display_current_service_state);
|
2020-01-25 22:31:37 +00:00
|
|
|
window.show ();
|
|
|
|
}
|
2020-01-17 22:13:39 +00:00
|
|
|
|
2020-01-25 22:31:37 +00:00
|
|
|
public static int main (string[] args) {
|
|
|
|
Application app = new Application ();
|
|
|
|
return app.run (args);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-01-17 22:13:39 +00:00
|
|
|
|
2020-01-25 22:13:54 +00:00
|
|
|
}
|