Skip to content
API Documentation
BastionGuard™
TerminalWindow.vala
Go to the documentation of this file.
1/*
2 * TerminalWindow.vala
3 *
4 * Copyright 2012-2018 Tony George <teejeetech@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 *
21 *
22 */
23
24
25using Gtk;
26using Gee;
27
28using TeeJee.Logging;
33using TeeJee.System;
34using TeeJee.Misc;
35
36public class TerminalWindow : Gtk.Window {
37
38 private Gtk.Box vbox_main;
39 private Vte.Terminal term;
40
41 private int def_width = 800;
42 private int def_height = 600;
43
44 private Pid child_pid;
45 private Gtk.Window parent_win = null;
46 public bool is_running = false;
47
48 // init
49
50 public TerminalWindow.with_parent(Gtk.Window? parent) {
51
52 if (parent != null){
53 set_transient_for(parent);
54 parent_win = parent;
55 }
56
57 set_modal(true);
58 fullscreen();
59
60 this.close_request.connect(() => {
61 // do not allow window to close
62 return true;
63 });
64
66 }
67
68 public void init_window () {
69
70 this.title = "";
71 this.set_icon_name("bastionguard-backup");
72 this.resizable = true;
73 this.deletable = false;
74
75 // vbox_main ---------------
76
77 vbox_main = new Gtk.Box(Orientation.VERTICAL, 6);
78 vbox_main.set_size_request (def_width, def_height);
79 set_child(vbox_main);
80
81 // terminal ----------------------
82
83 term = new Vte.Terminal();
84 term.hexpand = true;
85 term.vexpand = true;
86 vbox_main.append(term);
87
88 term.input_enabled = true;
89 term.backspace_binding = Vte.EraseBinding.AUTO;
90 term.cursor_blink_mode = Vte.CursorBlinkMode.SYSTEM;
91 term.cursor_shape = Vte.CursorShape.UNDERLINE;
92 //term.rewrap_on_resize = true;
93
94 term.scroll_on_keystroke = true;
95 term.scroll_on_output = true;
96
97 // colors -----------------------------
98
99 var color = Gdk.RGBA();
100 color.parse("#FFFFFF");
101 term.set_color_foreground(color);
102
103 color.parse("#404040");
104 term.set_color_background(color);
105
106 // grab focus ----------------
107
108 term.grab_focus();
109
110 show();
111 }
112
113 public void start_shell(){
114
115 string[] argv = new string[1];
116 argv[0] = "/bin/sh";
117
118 string[] env = Environ.get();
119
120 try{
121
122 is_running = true;
123
124 term.spawn_sync(
125 Vte.PtyFlags.DEFAULT, //pty_flags
126 TEMP_DIR, //working_directory
127 argv, //argv
128 env, //env
129 GLib.SpawnFlags.SEARCH_PATH, //spawn_flags
130 null, //child_setup
131 out child_pid,
132 null
133 );
134 }
135 catch (Error e) {
136 log_error (e.message);
137 }
138 }
139
140 public void execute_script(string script_path, bool wait = false){
141
142 string[] argv = new string[1];
143 argv[0] = script_path;
144
145 string[] env = Environ.get();
146
147 try{
148
149 is_running = true;
150
151 term.spawn_sync(
152 Vte.PtyFlags.DEFAULT, //pty_flags
153 TEMP_DIR, //working_directory
154 argv, //argv
155 env, //env
156 GLib.SpawnFlags.SEARCH_PATH, //spawn_flags
157 null, //child_setup
158 out child_pid,
159 null
160 );
161
162 term.watch_child(child_pid);
163
164 term.child_exited.connect(script_exit);
165
166 if (wait){
167 while (is_running){
168 sleep(200);
170 }
171 }
172 }
173 catch (Error e) {
174 log_error (e.message);
175 }
176 }
177
178 public void script_exit(int status){
179
180 is_running = false;
181
182 this.hide();
183
184 //no need to check status again
185
186 //destroying parent will display main window
187 if (parent != null){
188 parent_win.destroy();
189 }
190 }
191}
192
193
void script_exit(int status)
TerminalWindow. with_parent(Gtk.Window? parent)
Gtk.Window parent_win
Vte.Terminal term
void execute_script(string script_path, bool wait=false)
void log_error(string message, bool highlight=false, bool is_warning=false)
void sleep(int milliseconds)