Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor FolderManager code improvements #1489

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 27 additions & 24 deletions src/FolderManager/File.vala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace Scratch.FolderManager {
*/
public class File : GLib.Object {
public GLib.File file { get; private set; }
private GLib.FileInfo info;
private GLib.FileInfo? info = null; // Non-null after loading

public File (string path) {
Object (path: path);
Expand Down Expand Up @@ -67,8 +67,13 @@ namespace Scratch.FolderManager {
return _icon;
}

_icon = GLib.ContentType.get_icon (info.get_content_type ());
return _icon;
if (info != null) {
_icon = GLib.ContentType.get_icon (info.get_content_type ());
return _icon;
} else {
_icon = new ThemedIcon ("missing-image");
return _icon;
}
}
}

Expand All @@ -78,31 +83,29 @@ namespace Scratch.FolderManager {
}

// Checks if we're dealing with a non-backup directory
// Hidden subfolders are not shown by default, but we need to allow hidden top-level folder
public bool is_valid_directory (bool allow_hidden = true) {
if ((!allow_hidden && name.has_prefix (".")) || // If parent is hidden then inherit validity from parent
info.get_is_backup ()) {

return false;
}
// If parent is hidden then inherit validity from parent
private bool? _is_valid_directory = null;
public bool is_valid_directory {
get {
if (_is_valid_directory == null) {
_is_valid_directory = info != null &&
!info.get_is_backup () &&
info.get_file_type () == FileType.DIRECTORY;
}

if (info.get_file_type () == FileType.DIRECTORY) {
return true;
return _is_valid_directory;
}

return false;
}

public bool is_temporary {
get {
return path.has_suffix ("~");
}
}

// checks if we're dealing with a textfile
private bool? _is_valid_textfile = null;
public bool is_valid_textfile {
get {
return Utils.check_if_valid_text_file (path, info);
if (_is_valid_textfile == null) {
_is_valid_textfile = !path.has_suffix ("~") && Utils.check_if_valid_text_file (path, info);
}

return _is_valid_textfile;
}
}

Expand Down Expand Up @@ -138,7 +141,7 @@ namespace Scratch.FolderManager {
while ((file_info = enumerator.next_file ()) != null) {
var child = file.get_child (file_info.get_name ());
var child_file = new File (child.get_path ());
if (child_file.is_valid_directory () || child_file.is_valid_textfile) {
if (child_file.is_valid_directory || child_file.is_valid_textfile) {
_children.add (child_file);
}
}
Expand Down Expand Up @@ -195,10 +198,10 @@ namespace Scratch.FolderManager {
}

public static int compare (File a, File b) {
if (a.is_valid_directory () && b.is_valid_textfile) {
if (a.is_valid_directory && b.is_valid_textfile) {
return -1;
}
if (a.is_valid_textfile && b.is_valid_directory ()) {
if (a.is_valid_textfile && b.is_valid_directory) {
return 1;
}

Expand Down
2 changes: 1 addition & 1 deletion src/FolderManager/FileView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
if (is_open (folder)) {
warning ("Folder '%s' is already open.", folder.path);
return;
} else if (!folder.is_valid_directory (true)) { // Allow hidden top-level folders
} else if (!folder.is_valid_directory) {
warning ("Cannot open invalid directory.");
return;
}
Expand Down
19 changes: 8 additions & 11 deletions src/FolderManager/FolderItem.vala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace Scratch.FolderManager {
private bool has_dummy;
private Code.Widgets.SourceList.Item dummy; /* Blank item for expanded empty folders */

public FolderItem (File file, FileView view) requires (file.is_valid_directory) {
public FolderItem (File file, FileView view) {
Object (file: file, view: view);
}

Expand Down Expand Up @@ -63,16 +63,14 @@ namespace Scratch.FolderManager {
file.children.size > 0) {

foreach (var child in file.children) {
Code.Widgets.SourceList.Item item = null;
if (child.is_valid_directory ()) {
Code.Widgets.SourceList.Item? item = null;
if (child.is_valid_directory) {
item = new FolderItem (child, view);
} else if (child.is_valid_textfile) {
item = new FileItem (child, view);
}

if (item != null) {
add (item);
}
add (item); // ignores null parameter
}

children_loaded = true;
Expand Down Expand Up @@ -304,15 +302,14 @@ namespace Scratch.FolderManager {
var path_item = find_item_for_path (source.get_path ());
if (path_item == null) {
var file = new File (source.get_path ());
if (file.is_valid_directory ()) {
if (file.is_valid_directory) {
path_item = new FolderItem (file, view);
} else if (!file.is_temporary) {

} else if (file.is_valid_textfile) {
path_item = new FileItem (file, view);
} else {
break;
}

add (path_item);
add (path_item); // null parameter is silently ignored
}

break;
Expand Down
Loading