From dca3fd2c862ae15f47c60e50aaa11bbdf158a101 Mon Sep 17 00:00:00 2001 From: Mubashshir Date: Tue, 3 Oct 2023 09:38:12 +0600 Subject: Finalize UI Signed-off-by: Mubashshir --- .../hicolor/scalable/apps/com.jadupc.support.svg | 148 +++++++++++++++++++++ .../symbolic/apps/com.jadupc.support-symbolic.svg | 1 + data/ui/window.ui | 2 +- meson.build | 55 ++++++++ meson_options.txt | 1 + src/application.vala | 78 +++++++++++ src/config.vala | 23 ++++ src/config.vapi | 9 ++ src/main.vala | 29 ++++ src/meson.build | 26 ++++ src/utils.vala | 46 +++++++ src/window.vala | 96 +++++++++++++ 12 files changed, 513 insertions(+), 1 deletion(-) create mode 100644 data/icons/hicolor/scalable/apps/com.jadupc.support.svg create mode 100644 data/icons/hicolor/symbolic/apps/com.jadupc.support-symbolic.svg create mode 100644 meson.build create mode 100644 meson_options.txt create mode 100644 src/application.vala create mode 100644 src/config.vala create mode 100644 src/config.vapi create mode 100644 src/main.vala create mode 100644 src/meson.build create mode 100644 src/utils.vala create mode 100644 src/window.vala diff --git a/data/icons/hicolor/scalable/apps/com.jadupc.support.svg b/data/icons/hicolor/scalable/apps/com.jadupc.support.svg new file mode 100644 index 0000000..34f2bc6 --- /dev/null +++ b/data/icons/hicolor/scalable/apps/com.jadupc.support.svg @@ -0,0 +1,148 @@ + + diff --git a/data/icons/hicolor/symbolic/apps/com.jadupc.support-symbolic.svg b/data/icons/hicolor/symbolic/apps/com.jadupc.support-symbolic.svg new file mode 100644 index 0000000..0444828 --- /dev/null +++ b/data/icons/hicolor/symbolic/apps/com.jadupc.support-symbolic.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/data/ui/window.ui b/data/ui/window.ui index 2a67dbc..3ce7cc3 100644 --- a/data/ui/window.ui +++ b/data/ui/window.ui @@ -177,7 +177,7 @@ - + True False True diff --git a/meson.build b/meson.build new file mode 100644 index 0000000..faa901e --- /dev/null +++ b/meson.build @@ -0,0 +1,55 @@ +project('support', + ['c', 'vala'], + version: '0.1.0', + meson_version: '>= 0.52.0', + license: 'GPL-3.0-or-later', + default_options: [ + 'warning_level=2', + 'werror=false', + ], +) + +description = '''JaduPc Remote Support Console''' +desktop_name = 'JaduPc Remote Support Console' + + +if get_option('debug') + add_project_arguments('-DG_LOG_USE_STRUCTURED=1', language : 'c') + add_project_arguments('--debug', language : 'vala') +endif + +maintainer_rname = 'com.jadupc' +project_name = meson.project_name () +application_id = '@0@.@1@'.format (maintainer_rname, project_name) +application_path = '/'.join([''] + application_id.split('.')) +application_name = '@0@-@1@'.format(maintainer_rname.split('.').get(-1), project_name) + +i18n = import('i18n') +gnome = import('gnome') + +valac = meson.get_compiler ('vala') +conf = configuration_data () + +srcs = [] +i18n_data = [] + +conf.set_quoted ('GETTEXT_PACKAGE', application_name) +conf.set_quoted ('DATADIR', get_option ('prefix') / get_option ('datadir')) +conf.set_quoted ('LOCALEDIR', get_option ('prefix') / get_option ('localedir')) +conf.set_quoted ('APPLICATION_ID', application_id) +conf.set_quoted ('VERSION', meson.project_version ()) +conf.set_quoted ('SECURE_PATH', get_option('secure-path')) + +config_h = declare_dependency ( + sources: configure_file ( + output: 'config.h', + configuration: conf + ) +) + +config_h_dir = include_directories ('.') +config_dep = valac.find_library ('config', dirs: meson.current_source_dir() / 'src') + +subdir('data') +subdir('src') +subdir('po') diff --git a/meson_options.txt b/meson_options.txt new file mode 100644 index 0000000..8d0af00 --- /dev/null +++ b/meson_options.txt @@ -0,0 +1 @@ +option('secure-path', type : 'string', value : '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', description : 'Secure $PATH') 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 . + */ + +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", {"q"}); + this.set_accels_for_action ("app.about", {"question"}); + this.set_accels_for_action ("app.preferences", {"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 "), + _("Saikat "), + }; + copyright = { + _("Copyright © 2023 Mubashshir"), + _("Copyright © 2023 Saikat"), + }; + artists = { + _("Maliha "), + }; + } + } +} 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 . + */ + +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 . + */ + +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 = @"ssh://$url"; + 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); + } + } +} -- cgit v1.2.3