summaryrefslogtreecommitdiff
path: root/src/window.vala
blob: bea54081ad30e46d70a248d48fa1703171cac6bd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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);
			}
	}
}