/* 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;
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 = @"ssh://$url";
view.visible_child_name = "terminal";
terminal.has_focus = true;
}
}
}