Skip to content
API Documentation
BastionGuard™
ExcludeBox.vala
Go to the documentation of this file.
1/*
2 * ExcludeBox.vala
3 */
4
5using Gtk;
6using Gee;
7
13using TeeJee.System;
14using TeeJee.Misc;
15
16class ExcludeBox : Gtk.Box{
17 private Gtk.ListBox listbox;
18 private Gtk.Window parent_window;
20 private Gtk.Label lbl_message;
21 private Gtk.ScrolledWindow scrolled;
22
23 public ExcludeBox (Gtk.Window _parent_window) {
24 log_debug("ExcludeBox: ExcludeBox()");
25 GLib.Object(orientation: Gtk.Orientation.VERTICAL, spacing: 6);
26 parent_window = _parent_window;
27 margin_top = 12;
28 margin_bottom = 12;
29 margin_start = 12;
30 margin_end = 12;
31
32 var header = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 6);
33 append(header);
34 add_label_header(header, _("Include / Exclude Patterns"), true);
35
36 var buffer = add_label(header, "");
37 buffer.hexpand = true;
38
39 lbl_message = add_label(header, _("Click to edit. Drag-drop to re-order."), false, true);
40
44 log_debug("ExcludeBox: ExcludeBox(): exit");
45 }
46
47 public void set_users_box(UsersBox _users_box){
48 users_box = _users_box;
49 }
50
51 private void init_listbox(){
52 listbox = new Gtk.ListBox();
53 listbox.selection_mode = Gtk.SelectionMode.NONE;
54 listbox.add_css_class("boxed-list");
55
56 scrolled = new Gtk.ScrolledWindow();
57 scrolled.hexpand = true;
58 scrolled.vexpand = true;
59 scrolled.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
60 scrolled.set_child(listbox);
61 append(scrolled);
62 }
63
64 private Gtk.Widget create_pattern_row(string pattern){
65 bool include = pattern.has_prefix("+ ");
66 string clean_pattern = include ? pattern[2:pattern.length] : pattern;
67
68 var row = new Gtk.ListBoxRow();
69 var outer = new Gtk.Box(Gtk.Orientation.VERTICAL, 6);
70 outer.margin_start = 8;
71 outer.margin_end = 8;
72 outer.margin_top = 8;
73 outer.margin_bottom = 8;
74 row.set_child(outer);
75
76 var top = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 12);
77 outer.append(top);
78
79 var toggles = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 12);
80 top.append(toggles);
81
82 var btn_include = new Gtk.CheckButton.with_label("+");
83 var btn_exclude = new Gtk.CheckButton.with_label("-");
84 btn_exclude.set_group(btn_include);
85 btn_include.active = include;
86 btn_exclude.active = !include;
87 toggles.append(btn_include);
88 toggles.append(btn_exclude);
89
90 var entry = new Gtk.Entry();
91 entry.hexpand = true;
92 entry.text = clean_pattern;
93 entry.placeholder_text = _("Pattern");
94 top.append(entry);
95
96 var icon = IconManager.lookup_image(include ? "list-add" : "list-remove", 16);
97 top.append(icon);
98
99 var actions = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 6);
100 actions.halign = Gtk.Align.END;
101 outer.append(actions);
102
103 var btn_up = IconManager.button_from_icon("go-up-symbolic", 16);
104 btn_up.tooltip_text = _("Move up");
105 actions.append(btn_up);
106
107 var btn_down = IconManager.button_from_icon("go-down-symbolic", 16);
108 btn_down.tooltip_text = _("Move down");
109 actions.append(btn_down);
110
111 var btn_remove = IconManager.button_from_icon("user-trash-symbolic", 16);
112 btn_remove.tooltip_text = _("Remove");
113 actions.append(btn_remove);
114
115 btn_include.toggled.connect(()=>{
116 if (!btn_include.active){ return; }
117 icon = IconManager.lookup_image("list-add", 16);
118 update_row_pattern(row, entry.text, true);
119 });
120
121 btn_exclude.toggled.connect(()=>{
122 if (!btn_exclude.active){ return; }
123 icon = IconManager.lookup_image("list-remove", 16);
124 update_row_pattern(row, entry.text, false);
125 });
126
127 entry.changed.connect(()=>{
128 update_row_pattern(row, entry.text, btn_include.active);
129 });
130
131 btn_up.clicked.connect(()=>{
132 move_row(row, -1);
133 });
134
135 btn_down.clicked.connect(()=>{
136 move_row(row, 1);
137 });
138
139 btn_remove.clicked.connect(()=>{
140 remove_row(row);
141 });
142
143 row.set_data<string>("pattern", pattern);
144 return row;
145 }
146
147 private void update_row_pattern(Gtk.ListBoxRow row, string text, bool include){
148 string value = text;
149 if (include){
150 if (!value.has_prefix("+ ")){
151 value = "+ %s".printf(value);
152 }
153 }
154 else if (value.has_prefix("+ ")){
155 value = value[2:value.length];
156 }
157 row.set_data<string>("pattern", value);
158 save_changes();
159 }
160
161 private void clear_listbox(){
162 Gtk.Widget? child = listbox.get_first_child();
163 while (child != null){
164 Gtk.Widget? next = child.get_next_sibling();
165 listbox.remove(child);
166 child = next;
167 }
168 }
169
170 private void move_row(Gtk.ListBoxRow row, int delta){
171 var patterns = collect_patterns();
172 string? current = row.get_data<string>("pattern");
173 if (current == null){ return; }
174 int index = -1;
175 for (int i = 0; i < patterns.size; i++){
176 if (patterns[i] == current){ index = i; break; }
177 }
178 if (index < 0){ return; }
179 int target = index + delta;
180 if (target < 0 || target >= patterns.size){ return; }
181 string tmp = patterns[index];
182 patterns[index] = patterns[target];
183 patterns[target] = tmp;
184 apply_patterns(patterns);
185 }
186
187 private void remove_row(Gtk.ListBoxRow row){
188 string? pattern = row.get_data<string>("pattern");
189 if (pattern == null){ return; }
190 App.exclude_list_user.remove(pattern);
192 save_changes();
193 }
194
195 private Gee.ArrayList<string> collect_patterns(){
196 var patterns = new Gee.ArrayList<string>();
197 Gtk.Widget? child = listbox.get_first_child();
198 while (child != null){
199 var row = child as Gtk.ListBoxRow;
200 if (row != null){
201 string? pattern = row.get_data<string>("pattern");
202 if ((pattern != null) && (pattern.strip().length > 0)){
203 patterns.add(pattern);
204 }
205 }
206 child = child.get_next_sibling();
207 }
208 return patterns;
209 }
210
211 private void apply_patterns(Gee.ArrayList<string> patterns){
212 App.exclude_list_user.clear();
213 foreach(string pattern in patterns){
214 if (pattern.strip().length == 0){ continue; }
215 if (!App.exclude_list_user.contains(pattern)){
216 App.exclude_list_user.add(pattern);
217 }
218 }
220 save_changes();
221 }
222
223 private void init_actions(){
224 var hbox = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 6);
225 append(hbox);
226
227 var size_group = new Gtk.SizeGroup(SizeGroupMode.HORIZONTAL);
228 var button = add_button(hbox, _("Add"), _("Add custom pattern"), size_group, null);
229 button.clicked.connect(()=>{
230 string pattern = gtk_inputbox(
231 _("Exclude Pattern"),
232 _("Enter the pattern to exclude (Ex: *.mp3, *.bak)"),
233 parent_window, false);
234
235 if ((pattern != null) && (pattern.strip().length > 0)){
236 App.exclude_list_user.add(pattern);
239 }
240 save_changes();
241 });
242
243 button = add_button(hbox, _("Add Files"), _("Add files"), size_group, null);
244 button.clicked.connect(()=>{ add_files_clicked(); });
245
246 button = add_button(hbox, _("Add Folders"), _("Add directories"), size_group, null);
247 button.clicked.connect(()=>{ add_folder_clicked(); });
248
249 button = add_button(hbox, _("Remove"), "", size_group, null);
250 button.clicked.connect(()=>{ remove_clicked(); });
251
252 button = add_button(hbox, _("Summary"), "", size_group, null);
253 button.clicked.connect(()=>{
254 save_changes();
255 new ExcludeListSummaryWindow(false);
256 });
257 }
258
259 private void remove_clicked(){
260 string title = _("Remove Pattern");
261 string message = _("Use the trash icon on each row to remove patterns in the GTK4 list view.");
262 gtk_messagebox(title, message, parent_window, true);
263 }
264
265 private void add_files_clicked(){
266 var list = browse_files();
267 if (list.length() > 0){
268 foreach(string item in list){
269 string pattern = item;
270 if (!App.exclude_list_user.contains(pattern)){
271 App.exclude_list_user.add(pattern);
273 }
274 }
275 }
277 save_changes();
278 }
279
280 private void add_folder_clicked(){
281 var list = browse_folder();
282 if (list.length() > 0){
283 foreach(string item in list){
284 string pattern = item;
285 if (!pattern.has_suffix("/***")){
286 pattern = "%s/***".printf(pattern);
287 }
288 if (!App.exclude_list_user.contains(pattern)){
289 App.exclude_list_user.add(pattern);
291 }
292 }
293 }
295 save_changes();
296 }
297
298 private SList<string> browse_files(){
299 var list = new SList<string>();
300 return list;
301 }
302
303 private SList<string> browse_folder(){
304 var list = new SList<string>();
305 return list;
306 }
307
308 public void refresh_treeview(){
310 foreach(string pattern in App.exclude_list_user){
311 listbox.append(create_pattern_row(pattern));
312 }
313 show();
314 }
315
316 public void save_changes(){
317 App.exclude_list_user.clear();
318 foreach(string pattern in collect_patterns()){
319 if (!App.exclude_list_user.contains(pattern)
320 && !App.exclude_list_default.contains(pattern)
321 && !App.exclude_list_home.contains(pattern)){
322 App.exclude_list_user.add(pattern);
323 }
324 }
325
326 log_debug("save_changes(): exclude_list_user:");
327 foreach(var item in App.exclude_list_user){
328 log_debug(item);
329 }
330 log_debug("");
331
332 if (users_box != null){
334 }
335 }
336}
Main App
#define _(s)
void save_changes()
void add_files_clicked()
SList< string > browse_files()
Gtk.ScrolledWindow scrolled
void init_actions()
Gtk.Widget create_pattern_row(string pattern)
Gtk.Label lbl_message
SList< string > browse_folder()
void update_row_pattern(Gtk.ListBoxRow row, string text, bool include)
ExcludeBox(Gtk.Window _parent_window)
void add_folder_clicked()
Gtk.Window parent_window
void clear_listbox()
void remove_clicked()
UsersBox users_box
void refresh_treeview()
void remove_row(Gtk.ListBoxRow row)
void init_listbox()
Gee.ArrayList< string > collect_patterns()
Gtk.ListBox listbox
void move_row(Gtk.ListBoxRow row, int delta)
void set_users_box(UsersBox _users_box)
void apply_patterns(Gee.ArrayList< string > patterns)
ExcludeListSummaryWindow(bool _for_restore)
static Gtk.Button button_from_icon(string icon_name, int icon_size, string? tooltip=null)
static Gtk.Image lookup_image(string icon_name, int icon_size, bool symbolic=false, bool use_hardcoded=false)
static uint64 first_snapshot_size
Definition Main.vala:123
Gee.ArrayList< string > exclude_list_default
Definition Main.vala:62
Gee.ArrayList< string > exclude_list_home
Definition Main.vala:64
Gee.ArrayList< string > exclude_list_user
Definition Main.vala:61
void refresh()
string? gtk_inputbox(string title, string message, Gtk.Window? parent_win, bool mask_password=false)
Gtk.Label add_label(Gtk.Box box, string text, bool bold=false, bool italic=false, bool large=false)
void log_debug(string message)