-
-
Notifications
You must be signed in to change notification settings - Fork 118
Cleanup: FolderManager File object #1770
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
Merged
+161
−190
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
33372eb
Lose unused PluginManager functions
jeremypw 172c106
Revert "Lose unused PluginManager functions"
jeremypw 581638a
Cleanup
jeremypw fce8bff
Missing null check
jeremypw db0ad58
Merge branch 'master' into jeremypw/foldermanager/file-cleanup
danirabbit 40bdac9
Use SPDX header
jeremypw 44ba3ff
Merge branch 'master' into jeremypw/foldermanager/file-cleanup
danirabbit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,216 +1,202 @@ | ||
| /*- | ||
| * Copyright (c) 2017-2025 elementary LLC. (https://elementary.io), | ||
| * 2013 Julien Spautz <spautz.julien@gmail.com> | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Lesser General Public License version 3 | ||
| * as published by the Free Software Foundation, either version 3 of the | ||
| * License, or (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranties of | ||
| * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR | ||
| * PURPOSE. See the GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| * | ||
| * Authored by: Julien Spautz <spautz.julien@gmail.com>, Andrei-Costin Zisu <matzipan@gmail.com> | ||
| * Copyright (c) 2017-2025 elementary LLC. (https://elementary.io), | ||
| * 2013 Julien Spautz <spautz.julien@gmail.com> | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Lesser General Public License version 3 | ||
| * as published by the Free Software Foundation, either version 3 of the | ||
| * License, or (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranties of | ||
| * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR | ||
| * PURPOSE. See the GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| * | ||
| * Authored by: Julien Spautz <spautz.julien@gmail.com>, Andrei-Costin Zisu <matzipan@gmail.com> | ||
| */ | ||
|
|
||
| /** | ||
| * Class for easily dealing with files. | ||
| */ | ||
|
|
||
| namespace Scratch.FolderManager { | ||
|
|
||
| /** | ||
| * Class for easily dealing with files. | ||
| */ | ||
| public class File : GLib.Object { | ||
| public GLib.File file { get; private set; } | ||
| private GLib.FileInfo? info = null; // Non-null after loading | ||
|
|
||
| public File (string path) { | ||
| Object (path: path); | ||
| public class Scratch.FolderManager.File : Object { | ||
| public static int compare (File a, File b) { | ||
| if (a.is_valid_directory && b.is_valid_textfile) { | ||
| return -1; | ||
| } | ||
|
|
||
| // returns the path the file | ||
| public string path { | ||
| owned get { | ||
| return file.get_path (); | ||
| } | ||
| set construct { | ||
| load_file_for_path (value); | ||
| } | ||
| if (a.is_valid_textfile && b.is_valid_directory) { | ||
| return 1; | ||
| } | ||
|
|
||
| // returns the basename of the file | ||
| private string _name; | ||
| public string name { | ||
| get { | ||
| if (_name != null) { | ||
| return _name; | ||
| } | ||
|
|
||
| if (info == null) { | ||
| _name = file.get_basename (); | ||
| } else { | ||
| _name = info.get_display_name (); | ||
| } | ||
|
|
||
| return strcmp (a.path.collate_key_for_filename (), | ||
| b.path.collate_key_for_filename ()); | ||
| } | ||
| // returns the path the file | ||
| public string path { get; construct; } | ||
| public GLib.File file { get; private set; } | ||
|
danirabbit marked this conversation as resolved.
|
||
|
|
||
| // returns the name of the file for display | ||
| public string name { | ||
| get { | ||
| if (_name != null) { | ||
| return _name; | ||
| } | ||
| } | ||
|
|
||
| // returns the icon of the file's content type | ||
| private GLib.Icon? _icon = null; | ||
| public GLib.Icon icon { | ||
| get { | ||
| if (_icon != null) { | ||
| return _icon; | ||
| } | ||
|
|
||
| if (info != null) { | ||
| _icon = GLib.ContentType.get_icon (info.get_content_type ()); | ||
| } else { | ||
| _icon = new ThemedIcon ("missing-image"); | ||
| } | ||
|
|
||
| return _icon; | ||
| if (info == null) { | ||
| _name = file.get_basename (); | ||
| } else { | ||
| _name = info.get_display_name (); | ||
| } | ||
| } | ||
|
|
||
| // checks if file exists | ||
| public bool exists { | ||
| get { return file.query_exists (); } | ||
| return _name; | ||
| } | ||
| } | ||
|
|
||
| // Checks if we're dealing with a non-backup directory | ||
| // 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; | ||
| } | ||
| // returns the icon of the file's content type | ||
| public GLib.Icon icon { | ||
| get { | ||
| if (_icon != null) { | ||
| return _icon; | ||
| } | ||
|
|
||
| return _is_valid_directory; | ||
| if (info != null) { | ||
| _icon = GLib.ContentType.get_icon (info.get_content_type ()); | ||
| } else { | ||
| _icon = new ThemedIcon ("missing-image"); | ||
| } | ||
|
|
||
| return _icon; | ||
| } | ||
| } | ||
|
|
||
| // checks if we're dealing with a textfile | ||
| private bool? _is_valid_textfile = null; | ||
| public bool is_valid_textfile { | ||
| get { | ||
| if (_is_valid_textfile == null) { | ||
| _is_valid_textfile = !path.has_suffix ("~") && Utils.check_if_valid_text_file (path, info); | ||
| } | ||
| // checks if file exists | ||
| public bool exists { | ||
| get { return file.query_exists (); } | ||
| } | ||
|
|
||
| return _is_valid_textfile; | ||
| // Checks if we're dealing with a non-backup directory | ||
| // If parent is hidden then inherit validity from parent | ||
| 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; | ||
| } | ||
|
|
||
| return _is_valid_directory; | ||
| } | ||
| } | ||
|
|
||
| // Files can be executed and folders can be cd'd into | ||
| public bool is_executable { | ||
| get { | ||
| try { | ||
| return get_boolean_file_attribute (GLib.FileAttribute.ACCESS_CAN_EXECUTE); | ||
| } catch (GLib.Error error) { | ||
| return false; | ||
| } | ||
| // checks if we're dealing with a textfile | ||
| public bool is_valid_textfile { | ||
| get { | ||
| if (_is_valid_textfile == null) { | ||
| _is_valid_textfile = !path.has_suffix ("~") && Utils.check_if_valid_text_file (path, info); | ||
| } | ||
| } | ||
|
|
||
| // returns a list of all children of a directory | ||
| private bool children_valid = false; | ||
| private Gee.ArrayList <File> _children = new Gee.ArrayList <File> (); | ||
| public Gee.Collection <File> children { | ||
| owned get { | ||
| if (children_valid) { | ||
| return _children; | ||
| } | ||
| return _is_valid_textfile; | ||
| } | ||
| } | ||
|
|
||
| _children.clear (); | ||
|
|
||
| try { | ||
| var enumerator = file.enumerate_children ( | ||
| GLib.FileAttribute.STANDARD_NAME, | ||
| FileQueryInfoFlags.NONE | ||
| ); | ||
|
|
||
| var file_info = new FileInfo (); | ||
| 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) { | ||
| _children.add (child_file); | ||
| } | ||
| } | ||
| // Files can be executed and folders can be cd'd into | ||
| public bool is_executable { | ||
| get { | ||
| // We queried this attribute on construction | ||
| if (info != null && info.has_attribute (FileAttribute.ACCESS_CAN_EXECUTE)) { | ||
| return info.get_attribute_boolean (FileAttribute.ACCESS_CAN_EXECUTE); | ||
| } | ||
|
|
||
| children_valid = true; | ||
| } catch (GLib.Error error) { | ||
| warning (error.message); | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| // returns a list of all children of a directory | ||
| public Gee.Collection <File> children { | ||
| owned get { | ||
| if (children_valid) { | ||
| return _children; | ||
| } | ||
| } | ||
|
|
||
| private bool get_boolean_file_attribute (string attribute) throws GLib.Error { | ||
| var info = file.query_info (attribute, GLib.FileQueryInfoFlags.NONE); | ||
|
|
||
| return info.get_attribute_boolean (attribute); | ||
| } | ||
|
|
||
| private void load_file_for_path (string path) { | ||
| file = GLib.File.new_for_path (path); | ||
| _children.clear (); | ||
|
|
||
| info = new FileInfo (); | ||
| try { | ||
| var query_string = GLib.FileAttribute.STANDARD_CONTENT_TYPE + "," + | ||
| GLib.FileAttribute.STANDARD_IS_BACKUP + "," + | ||
| GLib.FileAttribute.STANDARD_IS_HIDDEN + "," + | ||
| GLib.FileAttribute.STANDARD_DISPLAY_NAME + "," + | ||
| GLib.FileAttribute.STANDARD_TYPE; | ||
| var enumerator = file.enumerate_children ( | ||
| GLib.FileAttribute.STANDARD_NAME, | ||
| FileQueryInfoFlags.NONE | ||
| ); | ||
|
|
||
| var file_info = new FileInfo (); | ||
| 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) { | ||
| _children.add (child_file); | ||
| } | ||
| } | ||
|
|
||
| info = file.query_info (query_string, FileQueryInfoFlags.NONE); | ||
| children_valid = true; | ||
| } catch (GLib.Error error) { | ||
| info = null; | ||
| warning (error.message); | ||
| } | ||
|
|
||
| return _children; | ||
| } | ||
| } | ||
|
|
||
| public void rename (string name) { | ||
| try { | ||
| if (exists) { | ||
| file.set_display_name (name); | ||
| } | ||
| } catch (GLib.Error error) { | ||
| warning (error.message); | ||
| } | ||
| private GLib.FileInfo? info = null; // Non-null after loading | ||
| private string _name; | ||
| private GLib.Icon? _icon = null; | ||
| private bool? _is_valid_directory = null; | ||
| private bool? _is_valid_textfile = null; | ||
| private bool children_valid = false; | ||
| private Gee.ArrayList <File> _children = new Gee.ArrayList <File> (); | ||
|
|
||
| public File (string path) { | ||
| Object (path: path); | ||
| } | ||
|
|
||
| construct { | ||
| file = GLib.File.new_for_path (path); | ||
| info = new FileInfo (); | ||
| try { | ||
| var query_string = FileAttribute.STANDARD_CONTENT_TYPE + "," + | ||
| FileAttribute.STANDARD_IS_BACKUP + "," + | ||
| FileAttribute.STANDARD_IS_HIDDEN + "," + | ||
| FileAttribute.STANDARD_DISPLAY_NAME + "," + | ||
| FileAttribute.STANDARD_TYPE + "," + | ||
| FileAttribute.ACCESS_CAN_EXECUTE; | ||
|
|
||
| // We assume the attributes do not change during the lifetime of this object | ||
| info = file.query_info (query_string, FileQueryInfoFlags.NONE); | ||
| } catch (GLib.Error error) { | ||
| info = null; | ||
| warning (error.message); | ||
| } | ||
| } | ||
|
|
||
| public void trash () { | ||
| try { | ||
| file.trash (); | ||
| } catch (GLib.Error error) { | ||
| warning (error.message); | ||
| public void rename (string name) { | ||
| try { | ||
| if (exists) { | ||
| file.set_display_name (name); | ||
| } | ||
| } catch (GLib.Error error) { | ||
| warning (error.message); | ||
| } | ||
| } | ||
|
|
||
| public static int compare (File a, File b) { | ||
| if (a.is_valid_directory && b.is_valid_textfile) { | ||
| return -1; | ||
| } | ||
| if (a.is_valid_textfile && b.is_valid_directory) { | ||
| return 1; | ||
| } | ||
|
|
||
| return strcmp (a.path.collate_key_for_filename (), | ||
| b.path.collate_key_for_filename ()); | ||
| public void trash () { | ||
| try { | ||
| file.trash (); | ||
| } catch (GLib.Error error) { | ||
| warning (error.message); | ||
| } | ||
| } | ||
|
|
||
| public void invalidate_cache () { | ||
| children_valid = false; | ||
| } | ||
| public void invalidate_cache () { | ||
| children_valid = false; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.