diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/application.vala | 78 | ||||
-rw-r--r-- | src/config.vala | 23 | ||||
-rw-r--r-- | src/config.vapi | 9 | ||||
-rw-r--r-- | src/main.vala | 29 | ||||
-rw-r--r-- | src/meson.build | 26 | ||||
-rw-r--r-- | src/utils.vala | 46 | ||||
-rw-r--r-- | src/window.vala | 96 |
7 files changed, 307 insertions, 0 deletions
diff --git a/src/application.vala b/src/application.vala new file mode 100644 index 0000000..b4f543c --- /dev/null +++ b/src/application.vala @@ -0,0 +1,78 @@ +/* application.vala + + * + * Copyright 2023 Mubashshir + + * + * 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 <http://www.gnu.org/licenses/>. + */ + +namespace JadupcSupport +{ + public class Application : Gtk.Application + { + public Application () + { + Object (application_id: "com.jadupc.support", flags: ApplicationFlags.FLAGS_NONE); + } + + construct { + {var _ = Vte.get_major_version();} // This is useless, but removing this breaks linking with vte + ActionEntry[] action_entries = { + { "about", this.on_about_action }, + { "preferences", this.on_preferences_action }, + { "quit", this.quit } + }; + this.add_action_entries (action_entries, this); + } + + public override void activate () + { + base.activate (); + var win = this.active_window; + if (win == null) { + // Fix vte issues + win = new JadupcSupport.Window (this); + + this.set_accels_for_action ("app.quit", {"<primary>q"}); + this.set_accels_for_action ("app.about", {"<primary>question"}); + this.set_accels_for_action ("app.preferences", {"<primary>p"}); + var settings = Gtk.Settings.get_default(); + if (settings != null) + settings.gtk_dialogs_use_header = true; + } + win.present (); + } + + private void on_about_action () + { + var info = new Config.Info(); + Gtk.show_about_dialog (this.active_window, + program_name: _("JaduPc Remote Support Console"), + copyright: string.joinv("\n", info.copyright), + license_type: Gtk.License.GPL_2_0, + logo_icon_name: "com.jadupc.support", + authors: info.authors, + artists: info.artists, + website: "https://jadupc.com", + website_label: _("JaduPc Ltd."), + version: Config.VERSION); + } + + private void on_preferences_action () + { + message ("app.preferences action activated"); + } + } +} diff --git a/src/config.vala b/src/config.vala new file mode 100644 index 0000000..4a2de87 --- /dev/null +++ b/src/config.vala @@ -0,0 +1,23 @@ +namespace Config +{ + [SingleInstance] + public class Info: Object + { + public string[] authors {get; construct;} + public string[] copyright {get; construct;} + public string[] artists {get; construct;} + construct { + authors = { + _("Mubashshir <ahm@jadupc.com>"), + _("Saikat <fas@jadupc.com>"), + }; + copyright = { + _("Copyright © 2023 Mubashshir"), + _("Copyright © 2023 Saikat"), + }; + artists = { + _("Maliha <maliha.0250@gmail.com>"), + }; + } + } +} diff --git a/src/config.vapi b/src/config.vapi new file mode 100644 index 0000000..175cfb3 --- /dev/null +++ b/src/config.vapi @@ -0,0 +1,9 @@ +[CCode (cprefix = "", lower_case_cprefix = "", cheader_filename = "config.h")] +namespace Config { + public const string GETTEXT_PACKAGE; + public const string DATADIR; + public const string LOCALEDIR; + public const string APPLICATION_ID; + public const string VERSION; + public const string SECURE_PATH; +} diff --git a/src/main.vala b/src/main.vala new file mode 100644 index 0000000..0467354 --- /dev/null +++ b/src/main.vala @@ -0,0 +1,29 @@ +/* main.vala + + * + * Copyright 2023 Mubashshir + + * + * 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 <http://www.gnu.org/licenses/>. + */ + +public static int main(string[] args) +{ + Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR); + Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8"); + Intl.textdomain (Config.GETTEXT_PACKAGE); + + var app = new JadupcSupport.Application (); + return app.run (args); +} diff --git a/src/meson.build b/src/meson.build new file mode 100644 index 0000000..f002b7b --- /dev/null +++ b/src/meson.build @@ -0,0 +1,26 @@ +sources = [ + 'main.vala', + 'window.vala', + 'application.vala', + 'utils.vala', + 'config.vala', +] + +foreach file : sources + srcs += files(file) +endforeach + +deps = [ + dependency('gtk+-3.0'), + dependency('vte-2.91'), + meson.get_compiler('vala').find_library('posix'), + config_h, + config_dep +] + +executable(application_name, srcs + resources, + include_directories: config_h_dir, + vala_args: '--target-glib=2.50', dependencies: deps, + install: true, + c_args: ['-include', 'config.h'], +) diff --git a/src/utils.vala b/src/utils.vala new file mode 100644 index 0000000..04837df --- /dev/null +++ b/src/utils.vala @@ -0,0 +1,46 @@ +namespace JadupcSupport +{ + namespace utils + { + internal string get_message_icon(Gtk.MessageType type) + { + switch (type) { + case ERROR: + return "dialog-error"; + case INFO: + return "dialog-information"; + case QUESTION: + return "dialog-question"; + case WARNING: + return "dialog-warning"; + default: + return "image-missing"; + } + } + +// This function takes a visible Gtk Widget and recursively realizes +// the childrens as Gtk whines on key event if even one +// ((great)grand)children widget is not realized... + internal void realize_all(Gtk.Container container) + { + if (!container.get_realized()) + container.realize(); + + container.foreach((widget) => { + if (widget is Gtk.Container) realize_all(widget as Gtk.Container); + else if (!widget.get_realized()) widget.realize(); + }); + } + + internal string strret(int ret, string[] retstrs = {}) + { + if (0 < ret <= 64 && !(ret == 11 || ret == 28 || 3 <= ret <= 8)) + return "%s".printf(strsignal(ret) ?? _("Unknown Signal - %d").printf(ret)); + else if (128 < ret < 256) + return "%s".printf(strsignal(ret - 128) ?? _("Unknown Signal - %d").printf(ret)); + else if (ret % 256 == 0) + return _("Process returned %d").printf(ret / 256); + return _("Exit status - %d (unknown)").printf(ret); + } + } +} diff --git a/src/window.vala b/src/window.vala new file mode 100644 index 0000000..bea5408 --- /dev/null +++ b/src/window.vala @@ -0,0 +1,96 @@ +/* window.vala + + * + * Copyright 2023 Mubashshir + + * + * 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 <http://www.gnu.org/licenses/>. + */ + +namespace JadupcSupport +{ + + [GtkTemplate (ui = "/com/jadupc/support/ui/window.ui")] + public class Window : Gtk.ApplicationWindow + { + [GtkChild] private unowned Gtk.Label session; + [GtkChild] private unowned Gtk.InfoBar infobar; + [GtkChild] private unowned Gtk.Stack view; + [GtkChild] private unowned Vte.Terminal terminal; + [GtkChild] private unowned Gtk.Button sessionbutton; + [GtkChild] private unowned Gtk.Label infobarText; + [GtkChild] private unowned Gtk.Image infobarIcon; + + public Window (Gtk.Application app) + { + var lang = Environment.get_variable("LANG"); // fix locale for Vte.Terminal + Environment.set_variable("LANG", "en_US", true); // fix locale for Vte.Terminal + Object (application: app); + Environment.set_variable("LANG", lang, true); // fix locale for Vte.Terminal + + utils.realize_all(this); // Realize all children to pacify Gtk. + icon_name = "com.jadupc.support"; + + infobar.response.connect(() => infobar.set_revealed(false)); + sessionbutton.clicked.connect(() => { + if (infobar.revealed) + infobar.revealed = false; + session.label = _("Connecting..."); + view.visible_child_name = "inprogress"; + Timeout.add_once(3000, () => on_session_connect("test@tty.jadupc.com")); + }); + + terminal.child_exited.connect(status => { + if(status > 0) + show_in_infobar(ERROR, _("Error: %s").printf(utils.strret(status))); + reset_window(); + }); + + show_in_infobar(INFO, _("Welcome to JaduPc Remote Support Console!")); + } + + internal void show_in_infobar(Gtk.MessageType type, string message) + { + infobarIcon.icon_name = utils.get_message_icon(type); + + infobarText.label = message; + infobar.message_type = type; + infobar.revealed = true; + } + + private void reset_window() + { + session.label = _("Disconnected..."); + Timeout.add_once(view.transition_duration, () => terminal.reset(true, true)); + view.visible_child_name = "session"; + } + + private void spawn_async_cb(Vte.Terminal term, Pid pid, Error? error) + { + if(pid > 1) return; + reset_window(); + show_in_infobar(ERROR, error.message); + } + + private void on_session_connect(string url) + { + session.label = @"<a href=\"ssh://$url\">ssh://$url</a>"; + view.visible_child_name = "terminal"; + terminal.has_focus = true; + + terminal.spawn_async(Vte.PtyFlags.DEFAULT, "~", {"sh"}, {@"PATH=$(Config.SECURE_PATH)"}, + SpawnFlags.SEARCH_PATH_FROM_ENVP, null, -1, null, spawn_async_cb); + } + } +} |