| 
									
										
										
										
											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-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-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 }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         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-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-02-02 15:07:41 +01:00
										 |  |  |     const Node& node(const ModelIndex& index) const; | 
					
						
							| 
									
										
										
										
											2020-01-10 18:58:00 +03:00
										 |  |  |     GIcon icon_for_file(const mode_t mode, const String& name) const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Function<void(int done, int total)> on_thumbnail_progress; | 
					
						
							|  |  |  |     Function<void()> on_root_path_change; | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  |     virtual ColumnMetadata column_metadata(int column) const override; | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     virtual Variant data(const ModelIndex&, Role = Role::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-01-22 21:15:46 +03:00
										 |  |  |     virtual StringView drag_data_type() const override { return "url-list"; } | 
					
						
							| 
									
										
										
										
											2020-02-13 21:46:52 +01:00
										 |  |  |     virtual bool accepts_drag(const ModelIndex&, const StringView& data_type) override; | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  |     GIcon icon_for(const Node& node) const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     GIcon m_directory_icon; | 
					
						
							| 
									
										
										
										
											2019-03-30 04:20:28 +01:00
										 |  |  |     GIcon m_file_icon; | 
					
						
							| 
									
										
										
										
											2020-01-10 18:58:00 +03:00
										 |  |  |     GIcon m_symlink_icon; | 
					
						
							|  |  |  |     GIcon m_socket_icon; | 
					
						
							|  |  |  |     GIcon m_executable_icon; | 
					
						
							|  |  |  |     GIcon m_filetype_image_icon; | 
					
						
							|  |  |  |     GIcon m_filetype_sound_icon; | 
					
						
							|  |  |  |     GIcon m_filetype_html_icon; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     unsigned m_thumbnail_progress { 0 }; | 
					
						
							|  |  |  |     unsigned m_thumbnail_progress_total { 0 }; | 
					
						
							| 
									
										
										
										
											2019-03-29 17:03:30 +01:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | } |