| 
									
										
										
										
											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-05-26 13:09:31 +01:00
										 |  |  | #include <AK/FileSystemPath.h>
 | 
					
						
							| 
									
										
										
										
											2019-07-13 19:58:04 -05:00
										 |  |  | #include <AK/Optional.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-06 15:04:03 +01:00
										 |  |  | #include <LibCore/UserInfo.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-08 22:10:00 +02:00
										 |  |  | #include <LibGUI/GDialog.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-09 01:24:37 +02:00
										 |  |  | #include <LibGUI/GTableView.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | namespace GUI { | 
					
						
							| 
									
										
										
										
											2019-03-09 21:38:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | class FileSystemModel; | 
					
						
							|  |  |  | class Label; | 
					
						
							|  |  |  | class TextBox; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class FilePicker final : public Dialog { | 
					
						
							|  |  |  |     C_OBJECT(FilePicker) | 
					
						
							| 
									
										
										
										
											2019-03-09 21:38:13 +01:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2019-07-13 19:58:04 -05:00
										 |  |  |     enum class Mode { | 
					
						
							|  |  |  |         Open, | 
					
						
							|  |  |  |         Save | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-26 21:42:34 +02:00
										 |  |  |     static Optional<String> get_open_filepath(const String& window_title = {}); | 
					
						
							| 
									
										
										
										
											2019-07-28 23:45:50 -05:00
										 |  |  |     static Optional<String> get_save_filepath(const String& title, const String& extension); | 
					
						
							| 
									
										
										
										
											2019-07-13 19:58:04 -05:00
										 |  |  |     static bool file_exists(const StringView& path); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     virtual ~FilePicker() override; | 
					
						
							| 
									
										
										
										
											2019-03-09 21:38:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-26 13:09:31 +01:00
										 |  |  |     FileSystemPath selected_file() const { return m_selected_file; } | 
					
						
							| 
									
										
										
										
											2019-05-09 04:56:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-16 12:57:04 +01:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2019-05-26 22:33:54 +02:00
										 |  |  |     void set_preview(const FileSystemPath&); | 
					
						
							|  |  |  |     void clear_preview(); | 
					
						
							| 
									
										
										
										
											2019-07-29 15:50:06 -05:00
										 |  |  |     void on_file_return(); | 
					
						
							| 
									
										
										
										
											2019-05-26 22:33:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     FilePicker(Mode type = Mode::Open, const StringView& file_name = "Untitled", const StringView& path = String(get_current_user_home_path()), Core::Object* parent = nullptr); | 
					
						
							| 
									
										
										
										
											2019-10-01 00:18:20 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-13 19:58:04 -05:00
										 |  |  |     static String ok_button_name(Mode mode) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         switch (mode) { | 
					
						
							|  |  |  |         case Mode::Open: | 
					
						
							|  |  |  |             return "Open"; | 
					
						
							|  |  |  |         case Mode::Save: | 
					
						
							|  |  |  |             return "Save"; | 
					
						
							|  |  |  |         default: | 
					
						
							|  |  |  |             return "OK"; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     RefPtr<TableView> m_view; | 
					
						
							|  |  |  |     NonnullRefPtr<FileSystemModel> m_model; | 
					
						
							| 
									
										
										
										
											2019-05-26 13:09:31 +01:00
										 |  |  |     FileSystemPath m_selected_file; | 
					
						
							| 
									
										
										
										
											2019-05-26 22:33:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     RefPtr<TextBox> m_filename_textbox; | 
					
						
							|  |  |  |     RefPtr<Label> m_preview_image_label; | 
					
						
							|  |  |  |     RefPtr<Label> m_preview_name_label; | 
					
						
							|  |  |  |     RefPtr<Label> m_preview_geometry_label; | 
					
						
							| 
									
										
										
										
											2019-07-13 19:58:04 -05:00
										 |  |  |     Mode m_mode { Mode::Open }; | 
					
						
							| 
									
										
										
										
											2019-07-25 19:49:28 +02:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | } |