Skip to content
API Documentation
BastionGuard™
AppExcludeEntry.vala
Go to the documentation of this file.
1/*
2 * AppExcludeEntry.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 TeeJee.Logging;
29using TeeJee.System;
30using TeeJee.Misc;
31
32public class AppExcludeEntry : GLib.Object{
33
34 public string name = "";
35 public bool is_include = false;
36 public bool is_file = false;
37 public bool enabled = false;
38 public Gee.ArrayList<string> items;
39 public Gee.ArrayList<string> patterns;
40
41 //static
42 public static Gee.HashMap<string, AppExcludeEntry> app_map;
43
44 public AppExcludeEntry(string _name, bool _is_include = false){
45
46 name = _name;
47 is_include = _is_include;
48
49 items = new Gee.ArrayList<string>();
50 patterns = new Gee.ArrayList<string>();
51 }
52
53 public string tooltip_text(){
54
55 string txt = "";
56 foreach(var item in items){
57 txt += "%s\n".printf(item);
58 }
59 if (txt.has_suffix("\n")){
60 txt = txt[0:txt.length - 1];
61 }
62 return txt;
63 }
64
65 // static
66
67 public static void clear(){
68
69 log_debug("AppExcludeEntry: clear()");
70
71 if (app_map == null){
72 app_map = new Gee.HashMap<string, AppExcludeEntry>();
73 }
74 else{
75 app_map.clear();
76 }
77 }
78
79 public static void add_app_exclude_entries_from_home(string home){
80
81 log_debug("AppExcludeEntry: add_app_exclude_entries_from_home()");
82 log_debug("path=%s".printf(home));
83
84 try
85 {
86 File f_home = File.new_for_path (home);
87 if (!f_home.query_exists()){
88 return;
89 }
90
91 FileEnumerator enumerator = f_home.enumerate_children ("standard::*", 0);
92 FileInfo file;
93 while ((file = enumerator.next_file ()) != null) {
94 string name = file.get_name();
95 string item = home + "/" + name;
96 if (name.has_suffix(".lock")){ continue; }
97 if (name.has_suffix(".log")){ continue; }
98 if (name.has_suffix(".old")){ continue; }
99 if (name.has_suffix("~")){ continue; }
100
102 }
103 }
104 catch(Error e){
105 log_error (e.message);
106 }
107 }
108
109 public static void add_app_exclude_entries_from_path(string user_home){
110
111 log_debug("AppExcludeEntry: add_app_exclude_entries_from_path()");
112 log_debug("path=%s".printf(user_home));
113
114 try
115 {
116 File f_home = File.new_for_path (user_home);
117 if (!f_home.query_exists()){
118 return;
119 }
120
121 FileEnumerator enumerator = f_home.enumerate_children ("standard::*", 0);
122 FileInfo file;
123 while ((file = enumerator.next_file ()) != null) {
124 string name = file.get_name();
125 string item = user_home + "/" + name;
126 if (!name.has_prefix(".")){ continue; }
127 if (name.has_suffix("~")){ continue; }
128 if (name == ".config"){ continue; }
129 if (name == ".local"){ continue; }
130 if (name == ".gvfs"){ continue; }
131 if (name == ".thumbnails"){ continue; }
132 if (name == ".cache"){ continue; }
133 if (name == ".temp"){ continue; }
134 if (name == ".sudo_as_admin_successful"){ continue; }
135 if (name.has_suffix(".lock")){ continue; }
136 if (name.has_suffix(".log")){ continue; }
137 if (name.has_suffix(".old")){ continue; }
138 if (name.has_suffix("~")){ continue; }
139
140 var relpath = "~/%s".printf(name);
141 add_item(relpath, !dir_exists(item), false);
142 }
143
144 File f_home_config = File.new_for_path (user_home + "/.config");
145 if (f_home_config.query_exists()){
146 enumerator = f_home_config.enumerate_children ("standard::*", 0);
147 while ((file = enumerator.next_file ()) != null) {
148 string name = file.get_name();
149 string item = user_home + "/.config/" + name;
150 if (name.has_suffix(".lock")){ continue; }
151 if (name.has_suffix(".log")){ continue; }
152 if (name.has_suffix(".old")){ continue; }
153 if (name.has_suffix("~")){ continue; }
154
155 var relpath = "~/.config/%s".printf(name);
156 add_item(relpath, !dir_exists(item), false);
157 }
158 }
159
160 File f_home_local = File.new_for_path (user_home + "/.local/share");
161 if (f_home_local.query_exists()){
162 enumerator = f_home_local.enumerate_children ("standard::*", 0);
163 while ((file = enumerator.next_file ()) != null) {
164 string name = file.get_name();
165 string item = user_home + "/.local/share/" + name;
166 if (name.has_suffix(".lock")){ continue; }
167 if (name.has_suffix(".log")){ continue; }
168 if (name.has_suffix(".old")){ continue; }
169 if (name.has_suffix("~")){ continue; }
170 if (name == "applications"){ continue; }
171 if (name == "Trash"){ continue; }
172
173 var relpath = "~/.local/share/%s".printf(name);
174 add_item(relpath, !dir_exists(item), false);
175 }
176 }
177 }
178 catch(Error e){
179 log_error (e.message);
180 }
181 }
182
183 public static void add_item(string item_path, bool is_file, bool is_include){
184
185 if (app_map == null){
186 app_map = new Gee.HashMap<string, AppExcludeEntry>();
187 }
188
189 var name = file_basename(item_path);
190
191 if (name.has_suffix(".ini")){
192 name = name[0:name.length - ".ini".length];
193 }
194 else if (name.has_suffix(".sh")){
195 name = name[0:name.length - ".sh".length];
196 }
197 else if (name.has_suffix(".json")){
198 name = name[0:name.length - ".json".length];
199 }
200 else if (name.has_suffix(".conf")){
201 name = name[0:name.length - ".conf".length];
202 }
203 else if (name.has_suffix(".list")){
204 name = name[0:name.length - ".list".length];
205 }
206 else if (name.has_suffix(".xbel")){
207 name = name[0:name.length - ".xbel".length];
208 }
209 else if (name.has_suffix(".xbel.tbcache")){
210 name = name[0:name.length - ".xbel.tbcache".length];
211 }
212 else if (name.has_suffix(".bz2")){
213 name = name[0:name.length - ".bz2".length];
214 }
215 else if (name.has_suffix(".old")){
216 name = name[0:name.length - ".old".length];
217 }
218 else if (name.has_suffix(".dirs")){
219 name = name[0:name.length - ".dirs".length];
220 }
221 else if (name.has_suffix(".locale")){
222 name = name[0:name.length - ".locale".length];
223 }
224 else if (name.has_suffix(".dockitem")){
225 name = name[0:name.length - ".dockitem".length];
226 }
227 else if (name.has_suffix(".xml")){
228 name = name[0:name.length - ".xml".length];
229 }
230 else if (name.has_suffix(".log")){
231 name = name[0:name.length - ".log".length];
232 }
233 else if (name.has_suffix(".txt")){
234 name = name[0:name.length - ".txt".length];
235 }
236
237 if (name.has_prefix(".")){
238 name = name[1:name.length];
239 }
240
241 name = name.strip();
242
243 if (name.length == 0){
244 return;
245 }
246
247 AppExcludeEntry entry = null;
248 if (app_map.has_key(name)){
249 entry = app_map[name];
250 }
251 else if (app_map.has_key(name.down())){
252 entry = app_map[name.down()];
253 }
254 else{
255 entry = new AppExcludeEntry(name, is_include);
256 app_map[name] = entry;
257 }
258
259 if (!entry.items.contains(item_path)){
260 entry.items.add(item_path);
261 }
262
263 foreach(bool root_user in new bool[] { true, false } ){
264 string str = (is_include) ? "+ " : "";
265 str += (root_user) ? "/root" : "/home/*";
266 str += item_path[1:item_path.length];
267 //str += (is_file) ? "" : "/**";
268 entry.patterns.add(str);
269 }
270 }
271
272 public static Gee.ArrayList<AppExcludeEntry> get_apps_list(
273 Gee.ArrayList<string> selected_app_names){
274
275 if (app_map == null){
276 app_map = new Gee.HashMap<string, AppExcludeEntry>();
277 }
278
279 foreach(var selected_name in selected_app_names){
280 if (app_map.has_key(selected_name)){
281 app_map[selected_name].enabled = true;
282 }
283 else{
284 app_map[selected_name].enabled = false;
285 }
286 }
287
288 var list = new Gee.ArrayList<AppExcludeEntry>();
289 foreach(var key in app_map.keys){
290 list.add(app_map[key]);
291 }
292
293 //sort the list
294 GLib.CompareDataFunc<AppExcludeEntry> entry_compare = (a, b) => {
295 return strcmp(a.name.down(),b.name.down());
296 };
297
298 list.sort((owned) entry_compare);
299
300 log_debug("apps: %d".printf(list.size));
301
302 return list;
303 }
304}
static void clear()
Gee.ArrayList< string > patterns
static void add_app_exclude_entries_from_home(string home)
static Gee.ArrayList< AppExcludeEntry > get_apps_list(Gee.ArrayList< string > selected_app_names)
static Gee.HashMap< string, AppExcludeEntry > app_map
AppExcludeEntry(string _name, bool _is_include=false)
Gee.ArrayList< string > items
static void add_item(string item_path, bool is_file, bool is_include)
static void add_app_exclude_entries_from_path(string user_home)
string file_basename(string file_path)
bool dir_exists(string dir_path)
void log_debug(string message)
void log_error(string message, bool highlight=false, bool is_warning=false)