Skip to content
API Documentation
BastionGuard™
FileItem.vala
Go to the documentation of this file.
1/*
2 * FileItem.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 GLib;
25using Gtk;
26using Gee;
27using Json;
28
29using TeeJee.Logging;
34using TeeJee.System;
35using TeeJee.Misc;
36
37public class FileItem : GLib.Object,Gee.Comparable<FileItem> {
38
39 public string file_name = "";
40 public string file_location = "";
41 public string file_path = "";
42 public string file_path_prefix = "";
43 public FileType file_type = FileType.REGULAR;
44 public DateTime modified;
45 public string permissions = "";
46 public string owner_user = "";
47 public string owner_group = "";
48 public string content_type = "";
49 public string file_status = "";
50
51 public bool is_selected = false;
52 public bool is_symlink = false;
53 public string symlink_target = "";
54
55 public long file_count = 0;
56 public long dir_count = 0;
57 private int64 _size = 0;
58
59 public GLib.Icon icon;
60
61 // constructors -------------------------------
62
63 public FileItem(string name) {
64 file_name = name;
65 }
66
67 public FileItem.from_disk_path_with_basic_info(string _file_path) {
68 file_path = _file_path;
69 file_name = file_basename(_file_path);
70 file_location = file_parent(_file_path);
72 }
73
74 public FileItem.from_path_and_type(string _file_path, FileType _file_type) {
75 file_path = _file_path;
76 file_name = file_basename(_file_path);
77 file_location = file_parent(_file_path);
78 file_type = _file_type;
79 }
80
81 // properties -------------------------------------------------
82
83 public int64 size {
84 get{
85 return _size;
86 }
87 }
88
89 // helpers ----------------------------------------------------
90
91 public int compare_to(FileItem b){
92 if (this.file_type != b.file_type) {
93 if (this.file_type == FileType.DIRECTORY) {
94 return -1;
95 }
96 else {
97 return +1;
98 }
99 }
100 else {
101 //if (view.sort_column_desc) {
102 return strcmp(this.file_name.down(), b.file_name.down());
103 //}
104 //else {
105 //return -1 * strcmp(a.file_name.down(), b.file_name.down());
106 //}
107 }
108 }
109
110 // instance methods -------------------------------------------
111
112 public void query_file_info() {
113
114 try {
115 FileInfo info;
116 File file = File.parse_name (file_path);
117
118 if (file.query_exists()) {
119
120 // get type without following symlinks
121
122 info = file.query_info("%s,%s,%s".printf(
123 FileAttribute.STANDARD_TYPE,
124 FileAttribute.STANDARD_ICON,
125 FileAttribute.STANDARD_SYMLINK_TARGET),
126 FileQueryInfoFlags.NOFOLLOW_SYMLINKS);
127
128 var item_file_type = info.get_file_type();
129
130 this.icon = info.get_icon();
131
132 if (item_file_type == FileType.SYMBOLIC_LINK) {
133 //this.icon = GLib.Icon.new_for_string("emblem-symbolic-link");
134 this.is_symlink = true;
135 this.symlink_target = info.get_symlink_target();
136 }
137 else {
138
139 this.is_symlink = false;
140 this.symlink_target = "";
141
142 if (item_file_type == FileType.REGULAR){
143 //log_msg(file_basename(file_path) + " (gicon): " + icon.to_string());
144
145 /*var themed_icon = (GLib.ThemedIcon) icon;
146
147 string txt = "-> ";
148 foreach(var name in themed_icon.names){
149 txt += ", " + name;
150 }
151 log_msg(txt);*/
152 }
153 }
154
155 // get file info - follow symlinks
156
157 info = file.query_info("%s,%s,%s,%s,%s,%s,%s,%s".printf(
158 FileAttribute.STANDARD_TYPE,
159 FileAttribute.STANDARD_SIZE,
160 FileAttribute.STANDARD_ICON,
161 FileAttribute.STANDARD_CONTENT_TYPE,
162 FileAttribute.TIME_MODIFIED,
163 FileAttribute.OWNER_USER,
164 FileAttribute.OWNER_GROUP,
165 FileAttribute.FILESYSTEM_FREE
166 ), 0);
167
168 if (this.is_symlink){
169 // get icon for the resolved file
170 this.icon = info.get_icon();
171 }
172
173 // file type resolved
174 this.file_type = info.get_file_type();
175
176 // content type
177 this.content_type = info.get_content_type();
178
179 // size
180 if (!this.is_symlink && (this.file_type == FileType.REGULAR)) {
181 this._size = info.get_size();
182 }
183
184 // modified
185 this.modified = info.get_modification_date_time().to_local();
186
187 // owner_user
188 this.owner_user = info.get_attribute_string(FileAttribute.OWNER_USER);
189
190 // owner_group
191 this.owner_group = info.get_attribute_string(FileAttribute.OWNER_GROUP);
192
193 }
194 }
195 catch (Error e) {
196 log_error (e.message);
197 }
198 }
199
200 public void query_file_info_basic() {
201
202 try {
203 FileInfo info;
204 File file = File.parse_name(file_path);
205
206 if (file.query_exists()) {
207
208 // get type and icon -- follow symlinks
209
210 info = file.query_info("%s,%s".printf(
211 FileAttribute.STANDARD_TYPE,
212 FileAttribute.STANDARD_ICON
213 ), 0);
214
215 this.icon = info.get_icon();
216
217 this.file_type = info.get_file_type();
218 }
219 }
220 catch (Error e) {
221 log_error (e.message);
222 }
223 }
224
225 // icons ------------------------------------------------------
226
227 public Gdk.Pixbuf? get_icon(int icon_size, bool add_transparency, bool add_emblems){
228
229 Gdk.Pixbuf? pixbuf = null;
230
231 if (icon != null) {
232 pixbuf = IconManager.lookup_gicon(icon, icon_size);
233 }
234
235 if (pixbuf == null){
236 if (file_type == FileType.DIRECTORY) {
237 pixbuf = IconManager.lookup("folder", icon_size, false);
238 }
239 else{
240 pixbuf = IconManager.lookup("text-x-preview", icon_size, false);
241 }
242 }
243
244 return pixbuf;
245 }
246}
string file_path_prefix
Definition FileItem.vala:42
string owner_user
Definition FileItem.vala:46
DateTime modified
Definition FileItem.vala:44
long file_count
Definition FileItem.vala:55
string file_status
Definition FileItem.vala:49
string symlink_target
Definition FileItem.vala:53
GLib.Icon icon
Definition FileItem.vala:59
Gdk.? Pixbuf get_icon(int icon_size, bool add_transparency, bool add_emblems)
int64 _size
Definition FileItem.vala:57
string file_location
Definition FileItem.vala:40
string permissions
Definition FileItem.vala:45
string file_name
Definition FileItem.vala:39
bool is_selected
Definition FileItem.vala:51
int64 size
Definition FileItem.vala:83
FileType file_type
Definition FileItem.vala:43
string content_type
Definition FileItem.vala:48
long dir_count
Definition FileItem.vala:56
bool is_symlink
Definition FileItem.vala:52
void query_file_info_basic()
string owner_group
Definition FileItem.vala:47
void query_file_info()
int compare_to(FileItem b)
Definition FileItem.vala:91
string file_path
Definition FileItem.vala:41
FileItem(string name)
Definition FileItem.vala:63
static Gdk.? Pixbuf lookup(string icon_name, int icon_size, bool symbolic=false, bool use_hardcoded=false, int scale=1)
static Gdk.? Pixbuf lookup_gicon(GLib.Icon gicon, int icon_size)
string file_basename(string file_path)
string file_parent(string file_path)
void log_error(string message, bool highlight=false, bool is_warning=false)