Skip to content
API Documentation
BastionGuard™
BackupBox.vala
Go to the documentation of this file.
1/*
2 * BackupBox.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 BackupBox : Gtk.Box{
40 private Gtk.Box details_box;
41 private Gtk.Spinner spinner;
42 public Gtk.Label lbl_msg;
43 public Gtk.Label lbl_status;
44 public Gtk.Label lbl_remaining;
45 public Gtk.ProgressBar progressbar;
46 public Gtk.Label lbl_unchanged;
47 public Gtk.Label lbl_created;
48 public Gtk.Label lbl_deleted;
49 public Gtk.Label lbl_modified;
50 public Gtk.Label lbl_checksum;
51 public Gtk.Label lbl_size;
52 public Gtk.Label lbl_timestamp;
53 public Gtk.Label lbl_permissions;
54 public Gtk.Label lbl_owner;
55 public Gtk.Label lbl_group;
56
57 private Gtk.Window parent_window;
58
59 private bool thread_is_running = false;
60 private bool thread_status_success = false;
61
62 public BackupBox (Gtk.Window _parent_window) {
63
64 log_debug("BackupBox: BackupBox()");
65
66 //base(Gtk.Orientation.VERTICAL, 6); // issue with vala
67 GLib.Object(orientation: Gtk.Orientation.VERTICAL, spacing: 6); // work-around
68 parent_window = _parent_window;
69 margin_top = 12;
70 margin_bottom = 12;
71 margin_start = 12;
72 margin_end = 12;
73
74 add_label_header(this, _("Creating Snapshot..."), true);
75
77
78 // add count labels ---------------------------------
79
80 Gtk.SizeGroup sg_label = null;
81 Gtk.SizeGroup sg_value = null;
82
83 details_box = new Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6);
84 append(details_box);
85
86 var label = add_label(details_box, _("File and directory counts:"), true);
87 label.margin_bottom = 6;
88 label.margin_top = 12;
89
90 lbl_unchanged = add_count_label(details_box, _("No Change"), ref sg_label, ref sg_value);
91 lbl_created = add_count_label(details_box, _("Created"), ref sg_label, ref sg_value);
92 lbl_deleted = add_count_label(details_box, _("Deleted"), ref sg_label, ref sg_value);
93 lbl_modified = add_count_label(details_box, _("Changed"), ref sg_label, ref sg_value, 12);
94
95 label = add_label(details_box, _("Changed items:"), true);
96 label.margin_bottom = 6;
97
98 lbl_checksum = add_count_label(details_box, _("Checksum"), ref sg_label, ref sg_value);
99 lbl_size = add_count_label(details_box, _("Size"), ref sg_label, ref sg_value);
100 lbl_timestamp = add_count_label(details_box, _("Timestamp"), ref sg_label, ref sg_value);
101 lbl_permissions = add_count_label(details_box, _("Permissions"), ref sg_label, ref sg_value);
102 lbl_owner = add_count_label(details_box, _("Owner"), ref sg_label, ref sg_value);
103 lbl_group = add_count_label(details_box, _("Group"), ref sg_label, ref sg_value, 24);
104
105 lbl_deleted.sensitive = false;
106
107 log_debug("BackupBox: BackupBox(): exit");
108 }
109
110 private void add_progress_area(){
111
112 var hbox_status = new Gtk.Box(Orientation.HORIZONTAL, 6);
113 append(hbox_status);
114
115 spinner = new Gtk.Spinner();
116 spinner.start();
117 hbox_status.append(spinner);
118
119 //lbl_msg
120 lbl_msg = add_label(hbox_status, _("Preparing..."));
121 lbl_msg.hexpand = true;
122 lbl_msg.ellipsize = Pango.EllipsizeMode.END;
123 lbl_msg.max_width_chars = 50;
124
125 lbl_remaining = add_label(hbox_status, "");
126
127 //progressbar
128 progressbar = new Gtk.ProgressBar();
129 append(progressbar);
130
131 //lbl_status
132 lbl_status = add_label(this, "");
133 lbl_status.ellipsize = Pango.EllipsizeMode.MIDDLE;
134 lbl_status.max_width_chars = 45;
135 lbl_status.margin_bottom = 6;
136 }
137
138 public void pause() {
139 this.spinner.stop();
140 this.lbl_msg.set_label(_("Paused"));
141 }
142
143 public void resume() {
144 this.spinner.start();
145 this.lbl_msg.set_label("");
146 }
147
148 private Gtk.Label add_count_label(Gtk.Box box, string text,
149 ref Gtk.SizeGroup? sg_label, ref Gtk.SizeGroup? sg_value,
150 int add_margin_bottom = 0){
151
152 var hbox = new Gtk.Box(Orientation.HORIZONTAL, 6);
153 box.append(hbox);
154
155 var label = add_label(hbox, text + ":");
156 label.xalign = (float) 1.0;
157 label.margin_start = 12;
158 label.margin_end = 6;
159 var text_label = label;
160
161 if (add_margin_bottom > 0){
162 label.margin_bottom = add_margin_bottom;
163 }
164
165 // add to size group
166 if (sg_label == null){
167 sg_label = new Gtk.SizeGroup(SizeGroupMode.HORIZONTAL);
168 }
169 sg_label.add_widget(label);
170
171 label = add_label(hbox, "");
172 label.xalign = (float) 0.0;
173
174 if (add_margin_bottom > 0){
175 label.margin_bottom = add_margin_bottom;
176 }
177
178 // add to size group
179 if (sg_value == null){
180 sg_value = new Gtk.SizeGroup(SizeGroupMode.HORIZONTAL);
181 }
182 sg_value.add_widget(label);
183
184 label.notify["sensitive"].connect(()=>{
185 text_label.sensitive = label.sensitive;
186 });
187
188 return label;
189 }
190
191 public bool take_snapshot(){
192
193 try {
194 thread_is_running = true;
195 new Thread<void>.try ("snapshot-taker", () => {take_snapshot_thread();});
196 }
197 catch (Error e) {
198 log_error (e.message);
199 return false;
200 }
201
202 if (App.btrfs_mode){
203
204 while (thread_is_running){
205
207 sleep(200);
208
209 #if XAPP
210 XApp.set_window_progress_pulse(parent_window, true);
211 #endif
212 }
213
214 #if XAPP
215 XApp.set_window_progress_pulse(parent_window, false);
216 #endif
217 }
218 else{
219
220 //string last_message = "";
221 int wait_interval_millis = 100;
222 int status_line_counter = 0;
223 int status_line_counter_default = 1000 / wait_interval_millis;
224 string status_line = "";
225 string last_status_line = "";
226 int remaining_counter = 10;
227
228 while (thread_is_running){
229 string task_status_line;
230 double fraction;
231 string task_stat_time_remaining;
232
233 bool checking = App.space_check_task != null;
234
235 details_box.visible = !checking;
236
237 if (checking)
238 {
239 task_status_line = App.space_check_task.status_line;
240 fraction = App.space_check_task.progress;
241 task_stat_time_remaining = App.space_check_task.stat_time_remaining;
242 }
243 else
244 {
245 task_status_line = App.task.status_line;
246 fraction = App.task.progress;
247 task_stat_time_remaining = App.task.stat_time_remaining;
248 }
249
250 status_line = escape_html(task_status_line);
251 if (status_line != last_status_line){
252 lbl_status.label = status_line;
253 last_status_line = status_line;
254 status_line_counter = status_line_counter_default;
255 }
256 else{
257 status_line_counter--;
258 if (status_line_counter < 0){
259 status_line_counter = status_line_counter_default;
260 lbl_status.label = "";
261 }
262 }
263
264 // time remaining
265 remaining_counter--;
266 if (remaining_counter == 0){
267 lbl_remaining.label =
268 task_stat_time_remaining + " " + _("remaining");
269
270 remaining_counter = 10;
271 }
272
273 if (fraction < 0.99){
274 progressbar.fraction = fraction;
275 #if XAPP
276 XApp.set_window_progress(parent_window, (int)(fraction * 100.0));
277 #endif
278 }
279
280 if(App.task.status == AppStatus.PAUSED) {
281 lbl_msg.label = _("Paused");
282 } else {
283 lbl_msg.label = escape_html(App.progress_text);
284 }
285
286 if (!checking)
287 {
288 lbl_unchanged.label = "%'d".printf(App.task.count_unchanged);
289 lbl_created.label = "%'d".printf(App.task.count_created);
290 lbl_deleted.label = "%'d".printf(App.task.count_deleted);
291 lbl_modified.label = "%'d".printf(App.task.count_modified);
292 lbl_checksum.label = "%'d".printf(App.task.count_checksum);
293 lbl_size.label = "%'d".printf(App.task.count_size);
294 lbl_timestamp.label = "%'d".printf(App.task.count_timestamp);
295 lbl_permissions.label = "%'d".printf(App.task.count_permissions);
296 lbl_owner.label = "%'d".printf(App.task.count_owner);
297 lbl_group.label = "%'d".printf(App.task.count_group);
298 }
299
301
302 sleep(100);
303 //gtk_do_events();
304 }
305
306 #if XAPP
307 XApp.set_window_progress(parent_window, 0);
308 #endif
309 }
310
312
313 //TODO: low: check if snapshot was created successfully.
314 }
315
321}
Main App
AppStatus
#define _(s)
string??? stat_time_remaining
AppStatus status
double progress
string status_line
Gtk.Label lbl_unchanged
Gtk.Label lbl_size
Gtk.Label lbl_remaining
Gtk.Window parent_window
Gtk.Label lbl_deleted
void add_progress_area()
bool thread_is_running
Gtk.Label lbl_checksum
Gtk.Box details_box
void take_snapshot_thread()
Gtk.Label lbl_modified
void resume()
Gtk.Label lbl_status
Gtk.Label lbl_created
bool take_snapshot()
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_owner
Gtk.Label lbl_permissions
bool thread_status_success
Gtk.Spinner spinner
BackupBox(Gtk.Window _parent_window)
Gtk.Label lbl_group
Gtk.Label lbl_timestamp
Gtk.Label lbl_msg
void pause()
Gtk.ProgressBar progressbar
string progress_text
Definition Main.vala:156
RsyncTask task
Definition Main.vala:160
bool btrfs_mode
Definition Main.vala:52
RsyncSpaceCheckTask space_check_task
Definition Main.vala:162
bool create_snapshot(bool is_ondemand, Gtk.Window? parent_win)
Definition Main.vala:1082
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)