~teejee2008/timeshift/trunk

« back to all changes in this revision

Viewing changes to src/Utility/TerminalWindow.vala

  • Committer: Tony George
  • Date: 2016-10-16 13:42:12 UTC
  • Revision ID: tony.george.kol@gmail.com-20161016134212-ibjsptmgbha7a2mw
Fixed scheduled backups; Initialize display for the scheduled cron task;

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * TerminalWindow.vala
 
3
 *
 
4
 * Copyright 2016 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
 
 
25
using Gtk;
 
26
using Gee;
 
27
 
 
28
using TeeJee.Logging;
 
29
using TeeJee.FileSystem;
 
30
using TeeJee.JsonHelper;
 
31
using TeeJee.ProcessHelper;
 
32
using TeeJee.GtkHelper;
 
33
using TeeJee.System;
 
34
using TeeJee.Misc;
 
35
 
 
36
public class TerminalWindow : Gtk.Window {
 
37
        private Gtk.Box vbox_main;
 
38
        private Vte.Terminal term;
 
39
        
 
40
        private int def_width = 800;
 
41
        private int def_height = 600;
 
42
 
 
43
        private Pid child_pid;
 
44
        private Gtk.Window parent_win = null;
 
45
        public bool is_running = false;
 
46
        
 
47
        // init
 
48
        
 
49
        public TerminalWindow.with_parent(Gtk.Window? parent) {
 
50
                if (parent != null){
 
51
                        set_transient_for(parent);
 
52
                        parent_win = parent;
 
53
                }
 
54
                set_modal(true);
 
55
                fullscreen();
 
56
 
 
57
                this.delete_event.connect(()=>{
 
58
                        // do not allow window to close 
 
59
                        return true;
 
60
                });
 
61
                
 
62
                init_window();
 
63
        }
 
64
 
 
65
        public void init_window () {
 
66
                title = "";
 
67
                icon = get_app_icon(16);
 
68
                resizable = true;
 
69
                deletable = false;
 
70
                
 
71
                // vbox_main ---------------
 
72
                
 
73
                vbox_main = new Box (Orientation.VERTICAL, 6);
 
74
                vbox_main.set_size_request (def_width, def_height);
 
75
                add (vbox_main);
 
76
 
 
77
                // terminal ----------------------
 
78
                
 
79
                term = new Vte.Terminal();
 
80
                term.expand = true;
 
81
                vbox_main.add(term);
 
82
 
 
83
                #if VTE_291
 
84
                
 
85
                term.input_enabled = true;
 
86
                term.backspace_binding = Vte.EraseBinding.AUTO;
 
87
                term.cursor_blink_mode = Vte.CursorBlinkMode.SYSTEM;
 
88
                term.cursor_shape = Vte.CursorShape.UNDERLINE;
 
89
                term.rewrap_on_resize = true;
 
90
                
 
91
                #endif
 
92
                
 
93
                term.scroll_on_keystroke = true;
 
94
                term.scroll_on_output = true;
 
95
 
 
96
                // colors -----------------------------
 
97
                
 
98
                #if VTE_291
 
99
                
 
100
                var color = Gdk.RGBA();
 
101
                color.parse("#FFFFFF");
 
102
                term.set_color_foreground(color);
 
103
 
 
104
                color.parse("#404040");
 
105
                term.set_color_background(color);
 
106
                
 
107
                #else
 
108
                
 
109
                Gdk.Color color;
 
110
                Gdk.Color.parse("#FFFFFF", out color);
 
111
                term.set_color_foreground(color);
 
112
 
 
113
                Gdk.Color.parse("#404040", out color);
 
114
                term.set_color_background(color);
 
115
 
 
116
                #endif
 
117
                
 
118
                // grab focus ----------------
 
119
                
 
120
                term.grab_focus();
 
121
                
 
122
                show_all();
 
123
        }
 
124
 
 
125
        public void start_shell(){
 
126
                string[] argv = new string[1];
 
127
                argv[0] = "/bin/sh";
 
128
 
 
129
                string[] env = Environ.get();
 
130
                
 
131
                try{
 
132
 
 
133
                        is_running = true;
 
134
                        
 
135
                        #if VTE_291
 
136
                        
 
137
                        term.spawn_sync(
 
138
                                Vte.PtyFlags.DEFAULT, //pty_flags
 
139
                                TEMP_DIR, //working_directory
 
140
                                argv, //argv
 
141
                                env, //env
 
142
                                GLib.SpawnFlags.SEARCH_PATH, //spawn_flags
 
143
                                null, //child_setup
 
144
                                out child_pid,
 
145
                                null
 
146
                        );
 
147
 
 
148
                        #else
 
149
 
 
150
                        term.fork_command_full(
 
151
                                Vte.PtyFlags.DEFAULT, //pty_flags
 
152
                                TEMP_DIR, //working_directory
 
153
                                argv, //argv
 
154
                                env, //env
 
155
                                GLib.SpawnFlags.SEARCH_PATH, //spawn_flags
 
156
                                null, //child_setup
 
157
                                out child_pid
 
158
                        );
 
159
 
 
160
                        #endif
 
161
                }
 
162
                catch (Error e) {
 
163
                        log_error (e.message);
 
164
                }
 
165
        }
 
166
 
 
167
        public void execute_command(string command){
 
168
                term.feed_child("%s\n".printf(command), -1);
 
169
        }
 
170
 
 
171
        public void execute_script(string script_path, bool wait = false){
 
172
                string[] argv = new string[1];
 
173
                argv[0] = script_path;
 
174
                
 
175
                string[] env = Environ.get();
 
176
 
 
177
                try{
 
178
 
 
179
                        is_running = true;
 
180
                        
 
181
                        #if VTE_291
 
182
                        
 
183
                        term.spawn_sync(
 
184
                                Vte.PtyFlags.DEFAULT, //pty_flags
 
185
                                TEMP_DIR, //working_directory
 
186
                                argv, //argv
 
187
                                env, //env
 
188
                                GLib.SpawnFlags.SEARCH_PATH, //spawn_flags
 
189
                                null, //child_setup
 
190
                                out child_pid,
 
191
                                null
 
192
                        );
 
193
 
 
194
                        #else
 
195
 
 
196
                        term.fork_command_full(
 
197
                                Vte.PtyFlags.DEFAULT, //pty_flags
 
198
                                TEMP_DIR, //working_directory
 
199
                                argv, //argv
 
200
                                env, //env
 
201
                                GLib.SpawnFlags.SEARCH_PATH, //spawn_flags
 
202
                                null, //child_setup
 
203
                                out child_pid
 
204
                        );
 
205
 
 
206
                        #endif
 
207
 
 
208
                        term.watch_child(child_pid);
 
209
        
 
210
                        term.child_exited.connect(script_exit);
 
211
 
 
212
                        if (wait){
 
213
                                while (is_running){
 
214
                                        sleep(200);
 
215
                                        gtk_do_events();
 
216
                                }
 
217
                        }
 
218
                }
 
219
                catch (Error e) {
 
220
                        log_error (e.message);
 
221
                }
 
222
        }
 
223
 
 
224
        #if VTE_291
 
225
        public void script_exit(int status){
 
226
        #else
 
227
        public void script_exit(){
 
228
        #endif
 
229
 
 
230
                is_running = false;
 
231
                
 
232
                this.hide();
 
233
 
 
234
                //no need to check status again
 
235
                
 
236
                //destroying parent will display main window
 
237
                if (parent != null){
 
238
                        parent_win.destroy();
 
239
                }
 
240
        }
 
241
}
 
242
 
 
243