File tree Expand file tree Collapse file tree 2 files changed +36
-2
lines changed Expand file tree Collapse file tree 2 files changed +36
-2
lines changed Original file line number Diff line number Diff line change @@ -141,8 +141,23 @@ public class Application : Gtk.Application {
141141 }
142142
143143 public bool open_window_with_file (File file ) {
144- if (file. query_file_type (FileQueryInfoFlags . NONE ) != FileType . REGULAR ) {
145- warning (" Couldn't open, not a regular file." );
144+
145+ if (! Utils . check_if_valid_text_file (file)) {
146+ var window = get_windows (). first (). data;
147+ var error_dialog = new
148+ Granite .MessageDialog .with_image_from_icon_name (
149+ " Couldn't open file" ,
150+ " The specified file is not a valid text file" ,
151+ " dialog-error"
152+ ) {
153+ transient_for = window
154+ };
155+
156+ error_dialog. response. connect ((response_id) = > {
157+ error_dialog. destroy ();
158+ });
159+
160+ error_dialog. show ();
146161 return false ;
147162 }
148163
Original file line number Diff line number Diff line change 3333 warning (" Failed to prepare target data directory %s\n " , err. message);
3434 }
3535 }
36+
37+ public static bool check_if_valid_text_file (File file ) {
38+ try {
39+ FileInfo info = file. query_info (" *" , NOFOLLOW_SYMLINKS );
40+
41+ var content_type = info. get_content_type ();
42+ if (info. get_file_type () == FileType . REGULAR &&
43+ ContentType . is_a (content_type, " text/*" ) ||
44+ ContentType . is_a (content_type, " application/x-zerosize" )
45+ ) {
46+ return true ;
47+ }
48+
49+ } catch (Error err) {
50+ warning (" Failed to get file information: %s " , err. message);
51+ }
52+
53+ return false ;
54+ }
3655 }
You can’t perform that action at this time.
0 commit comments