| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  |  * modification, are permitted provided that the following conditions are met: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 1. Redistributions of source code must retain the above copyright notice, this | 
					
						
							|  |  |  |  *    list of conditions and the following disclaimer. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 2. Redistributions in binary form must reproduce the above copyright notice, | 
					
						
							|  |  |  |  *    this list of conditions and the following disclaimer in the documentation | 
					
						
							|  |  |  |  *    and/or other materials provided with the distribution. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
					
						
							|  |  |  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
					
						
							|  |  |  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
					
						
							|  |  |  |  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | 
					
						
							|  |  |  |  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
					
						
							|  |  |  |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 
					
						
							|  |  |  |  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
					
						
							|  |  |  |  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
					
						
							|  |  |  |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
					
						
							|  |  |  |  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-29 17:03:30 +01:00
										 |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-10 18:58:00 +03:00
										 |  |  | #include <AK/HashMap.h>
 | 
					
						
							|  |  |  | #include <AK/NonnullOwnPtrVector.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-11 20:05:44 +01:00
										 |  |  | #include <LibCore/DateTime.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-06 15:04:03 +01:00
										 |  |  | #include <LibCore/Notifier.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-06 20:33:02 +01:00
										 |  |  | #include <LibGUI/Model.h>
 | 
					
						
							| 
									
										
										
										
											2020-04-21 21:26:17 +02:00
										 |  |  | #include <string.h>
 | 
					
						
							| 
									
										
										
										
											2020-01-10 18:58:00 +03:00
										 |  |  | #include <sys/stat.h>
 | 
					
						
							|  |  |  | #include <time.h>
 | 
					
						
							| 
									
										
										
										
											2019-03-29 17:03:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | namespace GUI { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class FileSystemModel | 
					
						
							|  |  |  |     : public Model | 
					
						
							|  |  |  |     , public Weakable<FileSystemModel> { | 
					
						
							| 
									
										
										
										
											2020-01-10 18:58:00 +03:00
										 |  |  |     friend struct Node; | 
					
						
							| 
									
										
										
										
											2019-05-28 11:53:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-29 17:03:30 +01:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2019-06-07 17:13:23 +02:00
										 |  |  |     enum Mode { | 
					
						
							| 
									
										
										
										
											2019-05-28 11:53:16 +02:00
										 |  |  |         Invalid, | 
					
						
							|  |  |  |         DirectoriesOnly, | 
					
						
							|  |  |  |         FilesAndDirectories | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2019-03-29 17:14:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-10 18:58:00 +03:00
										 |  |  |     enum Column { | 
					
						
							|  |  |  |         Icon = 0, | 
					
						
							|  |  |  |         Name, | 
					
						
							|  |  |  |         Size, | 
					
						
							|  |  |  |         Owner, | 
					
						
							|  |  |  |         Group, | 
					
						
							|  |  |  |         Permissions, | 
					
						
							|  |  |  |         ModificationTime, | 
					
						
							|  |  |  |         Inode, | 
					
						
							| 
									
										
										
										
											2020-01-27 22:19:05 +01:00
										 |  |  |         SymlinkTarget, | 
					
						
							| 
									
										
										
										
											2020-01-10 18:58:00 +03:00
										 |  |  |         __Count, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     struct Node { | 
					
						
							|  |  |  |         ~Node() { close(m_watch_fd); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         String name; | 
					
						
							| 
									
										
										
										
											2020-01-27 22:19:05 +01:00
										 |  |  |         String symlink_target; | 
					
						
							| 
									
										
										
										
											2020-01-10 18:58:00 +03:00
										 |  |  |         size_t size { 0 }; | 
					
						
							|  |  |  |         mode_t mode { 0 }; | 
					
						
							|  |  |  |         uid_t uid { 0 }; | 
					
						
							|  |  |  |         gid_t gid { 0 }; | 
					
						
							|  |  |  |         ino_t inode { 0 }; | 
					
						
							|  |  |  |         time_t mtime { 0 }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         size_t total_size { 0 }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  |         mutable RefPtr<Gfx::Bitmap> thumbnail; | 
					
						
							| 
									
										
										
										
											2020-01-10 18:58:00 +03:00
										 |  |  |         bool is_directory() const { return S_ISDIR(mode); } | 
					
						
							| 
									
										
										
										
											2020-01-28 16:23:59 +03:00
										 |  |  |         bool is_executable() const { return mode & (S_IXUSR | S_IXGRP | S_IXOTH); } | 
					
						
							| 
									
										
										
										
											2020-01-10 18:58:00 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-10 09:51:15 -04:00
										 |  |  |         bool is_selected() const { return m_selected; } | 
					
						
							|  |  |  |         void set_selected(bool selected); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-21 21:26:17 +02:00
										 |  |  |         bool has_error() const { return m_error != 0; } | 
					
						
							|  |  |  |         int error() const { return m_error; } | 
					
						
							|  |  |  |         const char* error_string() const { return strerror(m_error); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |         String full_path(const FileSystemModel&) const; | 
					
						
							| 
									
										
										
										
											2020-01-10 18:58:00 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     private: | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |         friend class FileSystemModel; | 
					
						
							| 
									
										
										
										
											2020-01-10 18:58:00 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         Node* parent { nullptr }; | 
					
						
							|  |  |  |         NonnullOwnPtrVector<Node> children; | 
					
						
							|  |  |  |         bool has_traversed { false }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-10 09:51:15 -04:00
										 |  |  |         bool m_selected { false }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-10 18:58:00 +03:00
										 |  |  |         int m_watch_fd { -1 }; | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |         RefPtr<Core::Notifier> m_notifier; | 
					
						
							| 
									
										
										
										
											2020-01-10 18:58:00 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-21 21:26:17 +02:00
										 |  |  |         int m_error { 0 }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |         ModelIndex index(const FileSystemModel&, int column) const; | 
					
						
							|  |  |  |         void traverse_if_needed(const FileSystemModel&); | 
					
						
							|  |  |  |         void reify_if_needed(const FileSystemModel&); | 
					
						
							| 
									
										
										
										
											2020-01-27 22:10:19 +01:00
										 |  |  |         bool fetch_data(const String& full_path, bool is_root); | 
					
						
							| 
									
										
										
										
											2020-01-10 18:58:00 +03:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     static NonnullRefPtr<FileSystemModel> create(const StringView& root_path = "/", Mode mode = Mode::FilesAndDirectories) | 
					
						
							| 
									
										
										
										
											2019-03-29 17:03:30 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |         return adopt(*new FileSystemModel(root_path, mode)); | 
					
						
							| 
									
										
										
										
											2019-03-29 17:03:30 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     virtual ~FileSystemModel() override; | 
					
						
							| 
									
										
										
										
											2019-03-29 17:03:30 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     String root_path() const { return m_root_path; } | 
					
						
							| 
									
										
										
										
											2020-01-10 18:58:00 +03:00
										 |  |  |     void set_root_path(const StringView&); | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     String full_path(const ModelIndex&) const; | 
					
						
							|  |  |  |     ModelIndex index(const StringView& path, int column) const; | 
					
						
							| 
									
										
										
										
											2020-01-10 18:58:00 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-10 09:51:15 -04:00
										 |  |  |     void update_node_on_selection(const ModelIndex&, const bool); | 
					
						
							|  |  |  |     ModelIndex m_previously_selected_index {}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     const Node& node(const ModelIndex& index) const; | 
					
						
							| 
									
										
										
										
											2020-01-10 18:58:00 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Function<void(int done, int total)> on_thumbnail_progress; | 
					
						
							| 
									
										
										
										
											2020-04-21 21:26:17 +02:00
										 |  |  |     Function<void()> on_complete; | 
					
						
							|  |  |  |     Function<void(int error, const char* error_string)> on_error; | 
					
						
							| 
									
										
										
										
											2019-03-29 17:03:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     virtual int tree_column() const override { return Column::Name; } | 
					
						
							|  |  |  |     virtual int row_count(const ModelIndex& = ModelIndex()) const override; | 
					
						
							|  |  |  |     virtual int column_count(const ModelIndex& = ModelIndex()) const override; | 
					
						
							| 
									
										
										
										
											2020-01-10 18:58:00 +03:00
										 |  |  |     virtual String column_name(int column) const override; | 
					
						
							| 
									
										
										
										
											2020-08-16 16:00:07 +02:00
										 |  |  |     virtual Variant data(const ModelIndex&, ModelRole = ModelRole::Display) const override; | 
					
						
							| 
									
										
										
										
											2019-03-29 17:03:30 +01:00
										 |  |  |     virtual void update() override; | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     virtual ModelIndex parent_index(const ModelIndex&) const override; | 
					
						
							|  |  |  |     virtual ModelIndex index(int row, int column = 0, const ModelIndex& parent = ModelIndex()) const override; | 
					
						
							| 
									
										
										
										
											2020-02-14 13:18:34 +01:00
										 |  |  |     virtual StringView drag_data_type() const override { return "text/uri-list"; } | 
					
						
							| 
									
										
										
										
											2020-02-13 21:46:52 +01:00
										 |  |  |     virtual bool accepts_drag(const ModelIndex&, const StringView& data_type) override; | 
					
						
							| 
									
										
										
										
											2020-05-21 19:45:15 +02:00
										 |  |  |     virtual bool is_column_sortable(int column_index) const override { return column_index != Column::Icon; } | 
					
						
							| 
									
										
										
										
											2019-03-29 17:03:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-10 18:58:00 +03:00
										 |  |  |     static String timestamp_string(time_t timestamp) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-11 20:05:44 +01:00
										 |  |  |         return Core::DateTime::from_timestamp(timestamp).to_string(); | 
					
						
							| 
									
										
										
										
											2020-01-10 18:58:00 +03:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-18 18:46:37 +02:00
										 |  |  |     bool should_show_dotfiles() const { return m_should_show_dotfiles; } | 
					
						
							|  |  |  |     void set_should_show_dotfiles(bool); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-29 17:03:30 +01:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     FileSystemModel(const StringView& root_path, Mode); | 
					
						
							| 
									
										
										
										
											2019-03-29 17:03:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-10 18:58:00 +03:00
										 |  |  |     String name_for_uid(uid_t) const; | 
					
						
							|  |  |  |     String name_for_gid(gid_t) const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     HashMap<uid_t, String> m_user_names; | 
					
						
							|  |  |  |     HashMap<gid_t, String> m_group_names; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool fetch_thumbnail_for(const Node& node); | 
					
						
							| 
									
										
										
										
											2020-03-07 12:02:21 +13:00
										 |  |  |     GUI::Icon icon_for(const Node& node) const; | 
					
						
							| 
									
										
										
										
											2020-01-10 18:58:00 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-29 17:03:30 +01:00
										 |  |  |     String m_root_path; | 
					
						
							| 
									
										
										
										
											2019-03-29 17:14:03 +01:00
										 |  |  |     Mode m_mode { Invalid }; | 
					
						
							| 
									
										
										
										
											2020-01-10 18:58:00 +03:00
										 |  |  |     OwnPtr<Node> m_root { nullptr }; | 
					
						
							| 
									
										
										
										
											2019-03-29 17:03:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-10 18:58:00 +03:00
										 |  |  |     unsigned m_thumbnail_progress { 0 }; | 
					
						
							|  |  |  |     unsigned m_thumbnail_progress_total { 0 }; | 
					
						
							| 
									
										
										
										
											2020-07-18 18:46:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bool m_should_show_dotfiles { false }; | 
					
						
							| 
									
										
										
										
											2019-03-29 17:03:30 +01:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | } |