Skip to content
API Documentation
BastionGuard™
RestoreBox.vala
Go to the documentation of this file.
1/*
2 * RestoreBox.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
24using Gtk;
25using Gee;
26
27#if XAPP
28using XApp;
29#endif
30
31using TeeJee.Logging;
36using TeeJee.System;
37using TeeJee.Misc;
38
39class RestoreBox : Gtk.Box{
40
41 public Gtk.Label lbl_header;
42
43 private Gtk.Spinner spinner;
44 public Gtk.Label lbl_msg;
45 public Gtk.Label lbl_status;
46 public Gtk.Label lbl_remaining;
47 public Gtk.ProgressBar progressbar;
48 public Gtk.Label lbl_unchanged;
49 public Gtk.Label lbl_created;
50 public Gtk.Label lbl_deleted;
51 public Gtk.Label lbl_modified;
52 public Gtk.Label lbl_checksum;
53 public Gtk.Label lbl_size;
54 public Gtk.Label lbl_timestamp;
55 public Gtk.Label lbl_permissions;
56 public Gtk.Label lbl_owner;
57 public Gtk.Label lbl_group;
58
59 private Gtk.Window parent_window;
60
61 private bool thread_is_running = false;
62
63 public RestoreBox(Gtk.Window _parent_window) {
64
65 log_debug("RestoreBox: RestoreBox()");
66
67 //base(Gtk.Orientation.VERTICAL, 6); // issue with vala
68 GLib.Object(orientation: Gtk.Orientation.VERTICAL, spacing: 6); // work-around
69 parent_window = _parent_window;
70 margin_top = 12;
71 margin_bottom = 12;
72 margin_start = 12;
73 margin_end = 12;
74
75 // header
76 if (App.dry_run){
77 lbl_header = add_label_header(this, _("Comparing Files (Dry Run)..."), true);
78 }
79 else{
80 lbl_header = add_label_header(this, _("Restoring Snapshot..."), true);
81 }
82
83 var hbox_status = new Gtk.Box(Orientation.HORIZONTAL, 6);
84 append(hbox_status);
85
86 spinner = new Gtk.Spinner();
87 spinner.start();
88 hbox_status.append(spinner);
89
90 //lbl_msg
91 lbl_msg = add_label(hbox_status, _("Preparing..."));
92 lbl_msg.hexpand = true;
93 lbl_msg.ellipsize = Pango.EllipsizeMode.END;
94 lbl_msg.max_width_chars = 50;
95
96 lbl_remaining = add_label(hbox_status, "");
97
98 //progressbar
99 progressbar = new Gtk.ProgressBar();
100 //progressbar.set_size_request(-1,25);
101 //progressbar.show_text = true;
102 //progressbar.pulse_step = 0.1;
103 append(progressbar);
104
105 //lbl_status
106
107 lbl_status = add_label(this, "");
108 lbl_status.ellipsize = Pango.EllipsizeMode.MIDDLE;
109 lbl_status.max_width_chars = 45;
110 lbl_status.margin_bottom = 12;
111
112 // TODO: Add move to background button
113
114 var label = add_label(this, "");
115 label.vexpand = true;
116
117 // add count labels ---------------------------------
118
119 Gtk.SizeGroup sg_label = null;
120 Gtk.SizeGroup sg_value = null;
121
122 label = add_label(this, _("Files and directory counts:"), true);
123 label.margin_bottom = 6;
124
125 lbl_unchanged = add_count_label(this, _("No Change"), ref sg_label, ref sg_value);
126 lbl_created = add_count_label(this, _("Created"), ref sg_label, ref sg_value);
127 lbl_deleted = add_count_label(this, _("Deleted"), ref sg_label, ref sg_value);
128 lbl_modified = add_count_label(this, _("Changed"), ref sg_label, ref sg_value, 12);
129
130 label = add_label(this, _("Changed items:"), true);
131 label.margin_bottom = 6;
132
133 lbl_checksum = add_count_label(this, _("Checksum"), ref sg_label, ref sg_value);
134 lbl_size = add_count_label(this, _("Size"), ref sg_label, ref sg_value);
135 lbl_timestamp = add_count_label(this, _("Timestamp"), ref sg_label, ref sg_value);
136 lbl_permissions = add_count_label(this, _("Permissions"), ref sg_label, ref sg_value);
137 lbl_owner = add_count_label(this, _("Owner"), ref sg_label, ref sg_value);
138 lbl_group = add_count_label(this, _("Group"), ref sg_label, ref sg_value, 24);
139
140 log_debug("RestoreBox: RestoreBox(): exit");
141 }
142
143 private Gtk.Label add_count_label(Gtk.Box box, string text,
144 ref Gtk.SizeGroup? sg_label, ref Gtk.SizeGroup? sg_value,
145 int add_margin_bottom = 0){
146
147 var hbox = new Gtk.Box(Orientation.HORIZONTAL, 6);
148 box.append(hbox);
149
150 var label = add_label(hbox, text + ":");
151 label.xalign = (float) 1.0;
152 label.margin_start = 12;
153 label.margin_end = 6;
154
155 if (add_margin_bottom > 0){
156 label.margin_bottom = add_margin_bottom;
157 }
158
159 // add to size group
160 if (sg_label == null){
161 sg_label = new Gtk.SizeGroup(SizeGroupMode.HORIZONTAL);
162 }
163 sg_label.add_widget(label);
164
165 label = add_label(hbox, "");
166 label.xalign = (float) 0.0;
167
168 if (add_margin_bottom > 0){
169 label.margin_bottom = add_margin_bottom;
170 }
171
172 // add to size group
173 if (sg_value == null){
174 sg_value = new Gtk.SizeGroup(SizeGroupMode.HORIZONTAL);
175 }
176 sg_value.add_widget(label);
177
178 return label;
179 }
180
181 public bool restore(){
182
183 log_debug("RestoreBox: restore()");
184
186 parent_window.hide();
187 }
188
189 if (App.dry_run){
190 lbl_header.label = format_text(_("Comparing Files (Dry Run)..."), true, false, true);
191 }
192 else{
193 lbl_header.label = format_text(_("Restoring Snapshot..."), true, false, true);
194 }
195
196 try {
197 thread_is_running = true;
198 new Thread<void>.try ("restore", () => {restore_thread();});
199 }
200 catch (Error e) {
201 log_error (e.message);
202 }
203
204 //string last_message = "";
205 int wait_interval_millis = 100;
206 int status_line_counter = 0;
207 int status_line_counter_default = 1000 / wait_interval_millis;
208 string status_line = "";
209 string last_status_line = "";
210 int remaining_counter = 10;
211
212 while (thread_is_running){
213
214 status_line = escape_html(App.task.status_line);
215
216 if (status_line != last_status_line){
217
218 lbl_status.label = status_line;
219 last_status_line = status_line;
220 status_line_counter = status_line_counter_default;
221 }
222 else{
223 status_line_counter--;
224
225 if (status_line_counter < 0){
226 status_line_counter = status_line_counter_default;
227 lbl_status.label = "";
228 }
229 }
230
231 // TODO: show estimated time remaining and file counts
232
233 double fraction = App.task.progress;
234
235 // time remaining
236 remaining_counter--;
237
238 if (remaining_counter == 0){
239
240 lbl_remaining.label = App.task.stat_time_remaining + " " + _("remaining");
241
242 remaining_counter = 10;
243 }
244
245 if (fraction < 0.99){
246
247 progressbar.fraction = fraction;
248
249 #if XAPP
250 XApp.set_window_progress(parent_window, (int)(fraction * 100.0));
251 #endif
252 }
253
254 lbl_msg.label = App.progress_text;
255
256 lbl_unchanged.label = "%'d".printf(App.task.count_unchanged);
257 lbl_created.label = "%'d".printf(App.task.count_created);
258 lbl_deleted.label = "%'d".printf(App.task.count_deleted);
259 lbl_modified.label = "%'d".printf(App.task.count_modified);
260 lbl_checksum.label = "%'d".printf(App.task.count_checksum);
261 lbl_size.label = "%'d".printf(App.task.count_size);
262 lbl_timestamp.label = "%'d".printf(App.task.count_timestamp);
263 lbl_permissions.label = "%'d".printf(App.task.count_permissions);
264 lbl_owner.label = "%'d".printf(App.task.count_owner);
265 lbl_group.label = "%'d".printf(App.task.count_group);
266
268
269 sleep(100);
270 //gtk_do_events();
271 }
272
273 #if XAPP
274 XApp.set_window_progress(parent_window, 0);
275 #endif
276
278 parent_window.show();
279 }
280
281 log_debug("RestoreBox: restore(): exit");
282
283 return (App.task.exit_code == 0);
284 }
285
286 private void restore_thread(){
287
288 log_debug("RestoreBox: restore_thread()");
290 thread_is_running = false;
291 log_debug("RestoreBox: restore_thread(): exit");
292 }
293}
Main App
#define _(s)
double progress
int exit_code
string status_line
bool restore_current_system
Definition Main.vala:2024
string progress_text
Definition Main.vala:156
RsyncTask task
Definition Main.vala:160
bool dry_run
Definition Main.vala:100
bool restore_snapshot(Gtk.Window? parent_win)
Definition Main.vala:2298
Gtk.Label lbl_group
Gtk.Label lbl_permissions
Gtk.Label lbl_size
RestoreBox(Gtk.Window _parent_window)
Gtk.ProgressBar progressbar
Gtk.Label lbl_remaining
Gtk.Label lbl_checksum
Gtk.Label lbl_timestamp
Gtk.Label lbl_msg
Gtk.Label lbl_header
bool thread_is_running
Gtk.Label lbl_status
Gtk.Label add_count_label(Gtk.Box box, string text, ref Gtk.SizeGroup? sg_label, ref Gtk.SizeGroup? sg_value, int add_margin_bottom=0)
Gtk.Label lbl_created
Gtk.Label lbl_deleted
void restore_thread()
bool restore()
Gtk.Spinner spinner
Gtk.Label lbl_unchanged
Gtk.Window parent_window
Gtk.Label lbl_modified
Gtk.Label lbl_owner
int64 count_group
int64 count_deleted
int64 count_owner
int64 count_modified
int64 count_permissions
int64 count_size
int64 count_created
int64 count_timestamp
int64 count_unchanged
int64 count_checksum
Gtk.Label add_label(Gtk.Box box, string text, bool bold=false, bool italic=false, bool large=false)
void log_debug(string message)
void log_error(string message, bool highlight=false, bool is_warning=false)
string escape_html(string html)
void sleep(int milliseconds)