summaryrefslogtreecommitdiff
path: root/src/window.vala
blob: 8a9eeaa27b565657dc593b4e46f879c18dfabeba (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* 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;

			private Tmate.Session tmate;

			public Window (Gtk.Application app)
			{
				Object (application: app);

				utils.realize_all(this); // Realize all children to pacify Gtk.
				icon_name = "com.jadupc.support";
				tmate = new Tmate.Session();

				infobar.response.connect(() => infobar.set_revealed(false));
				delete_event.connect(() => {
					tmate.delete_config();
					tmate.stop();
					return false;
				});

				tmate.error.connect((message) => {
					show_in_infobar(ERROR, message);
				});

				tmate.stopped.connect(reset_window);
				tmate.address_changed.connect((stype, addr) => {
					if(Tmate.SessionType.SSH in stype)
						on_session_connect(addr);
				});

				sessionbutton.clicked.connect(() => {
					if (infobar.revealed)
						infobar.revealed = false;
					session.label = _("Connecting...");
					view.visible_child_name = "inprogress";

					tmate.start(new Tmate.Config());
				});

				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(view.transition_duration, () => {
					terminal.reset(true, true);
					return false;
				});

				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)
			{
				terminal.spawn_async(Vte.PtyFlags.DEFAULT, "~", {"tmate", "-S", tmate.socket, "attach"}, {@"PATH=$(Config.SECURE_PATH)"},
				                     SpawnFlags.SEARCH_PATH_FROM_ENVP, null, -1, null, spawn_async_cb);

				session.label = @"<a href=\"ssh://$url\">ssh://$url</a>";
				view.visible_child_name = "terminal";
				terminal.has_focus = true;
			}
	}
}