add YggdrasilService interface and implementors

This commit is contained in:
Nikolay Brovko 2020-01-26 14:14:26 +03:00
parent 1b720e4b5d
commit c3ca4e0055
No known key found for this signature in database
GPG Key ID: 32258A3DEC9B6F07
6 changed files with 120 additions and 3 deletions

View File

@ -19,14 +19,30 @@ namespace Yggtk {
public class Application : Gtk.Application {
private YggdrasilService yggdrasil_service = 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);

View File

@ -2,6 +2,9 @@ yggtk_sources = [
'application.vala',
'app-window.vala',
'utils.vala',
'yggdrasil-service.vala',
'yggdrasil-service-openrc.vala',
'yggdrasil-service-systemd.vala',
]
yggtk = executable(

View File

@ -13,14 +13,26 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace Utils {
namespace Yggtk.Utils {
string mktempdir () throws Error {
public bool is_command_available (string command) {
try {
string cmd_stdout, cmd_stderr;
int cmd_status = -1;
Process.spawn_command_line_sync (@"which $command", out cmd_stdout, out cmd_stderr, out cmd_status);
if (cmd_status == 0 && cmd_stderr.length == 0 && cmd_stdout.length > 0) {
return true;
}
} catch (SpawnError e) {
return false;
}
return false;
}
public string mktempdir () throws SpawnError {
string temp_dir = null;
Process.spawn_command_line_sync ("mktemp -d", out temp_dir);
return temp_dir.strip ();
}
}

View File

@ -0,0 +1,30 @@
/**
* 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/>.
*/
namespace Yggtk {
public class YggdrasilServiceOpenRC : Object, YggdrasilService {
public bool get_status () {
return false;
}
public void start () {}
public void stop () {}
}
}

View File

@ -0,0 +1,30 @@
/**
* 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/>.
*/
namespace Yggtk {
public class YggdrasilServiceSystemd : Object, YggdrasilService {
public bool get_status () {
return false;
}
public void start () {}
public void stop () {}
}
}

View File

@ -0,0 +1,26 @@
/**
* 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/>.
*/
namespace Yggtk {
public interface YggdrasilService : Object {
public abstract bool get_status ();
public abstract void start ();
public abstract void stop ();
}
}