Skip to content

Commit f0dfe5d

Browse files
authored
Add error dialog when opening a non-text document (#28)
* Add error dialog when opening a non-text document * remove errant line * clean up indentation * clean whitespace
1 parent 441f2d7 commit f0dfe5d

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/Application.vala

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff 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

src/Utils.vala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,23 @@
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
}

0 commit comments

Comments
 (0)