| 
									
										
										
										
											2019-05-26 22:33:54 +02:00
										 |  |  | #include <AK/FileSystemPath.h>
 | 
					
						
							| 
									
										
										
										
											2019-07-13 19:58:04 -05:00
										 |  |  | #include <AK/Function.h>
 | 
					
						
							| 
									
										
										
										
											2019-07-28 23:45:50 -05:00
										 |  |  | #include <LibDraw/PNGLoader.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-26 22:33:54 +02:00
										 |  |  | #include <LibGUI/GAction.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-09 01:24:37 +02:00
										 |  |  | #include <LibGUI/GBoxLayout.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-26 22:33:54 +02:00
										 |  |  | #include <LibGUI/GButton.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-09 01:24:37 +02:00
										 |  |  | #include <LibGUI/GDirectoryModel.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-26 22:33:54 +02:00
										 |  |  | #include <LibGUI/GFilePicker.h>
 | 
					
						
							|  |  |  | #include <LibGUI/GInputBox.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-09 01:24:37 +02:00
										 |  |  | #include <LibGUI/GLabel.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-26 22:33:54 +02:00
										 |  |  | #include <LibGUI/GMessageBox.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-09 04:56:52 +02:00
										 |  |  | #include <LibGUI/GSortingProxyModel.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-26 22:33:54 +02:00
										 |  |  | #include <LibGUI/GTextBox.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-09 15:51:57 +02:00
										 |  |  | #include <LibGUI/GToolBar.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-09 01:24:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-13 19:58:04 -05:00
										 |  |  | Optional<String> GFilePicker::get_open_filepath() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     GFilePicker picker(Mode::Open); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (picker.exec() == GDialog::ExecOK) { | 
					
						
							|  |  |  |         String file_path = picker.selected_file().string(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (file_path.is_null()) | 
					
						
							|  |  |  |             return {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return file_path; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-28 23:45:50 -05:00
										 |  |  | Optional<String> GFilePicker::get_save_filepath(const String& title, const String& extension) | 
					
						
							| 
									
										
										
										
											2019-07-13 19:58:04 -05:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-07-28 23:45:50 -05:00
										 |  |  |     GFilePicker picker(Mode::Save, String::format("%s.%s", title.characters(), extension.characters())); | 
					
						
							| 
									
										
										
										
											2019-07-13 19:58:04 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (picker.exec() == GDialog::ExecOK) { | 
					
						
							|  |  |  |         String file_path = picker.selected_file().string(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (file_path.is_null()) | 
					
						
							|  |  |  |             return {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return file_path; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-28 23:45:50 -05:00
										 |  |  | GFilePicker::GFilePicker(Mode mode, const StringView& file_name, const StringView& path, CObject* parent) | 
					
						
							| 
									
										
										
										
											2019-05-09 01:24:37 +02:00
										 |  |  |     : GDialog(parent) | 
					
						
							|  |  |  |     , m_model(GDirectoryModel::create()) | 
					
						
							| 
									
										
										
										
											2019-07-13 19:58:04 -05:00
										 |  |  |     , m_mode(mode) | 
					
						
							| 
									
										
										
										
											2019-05-09 01:24:37 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-07-28 13:04:57 -05:00
										 |  |  |     set_title(m_mode == Mode::Open ? "Open File" : "Save File"); | 
					
						
							| 
									
										
										
										
											2019-05-26 22:33:54 +02:00
										 |  |  |     set_rect(200, 200, 700, 400); | 
					
						
							| 
									
										
										
										
											2019-09-21 17:05:35 +02:00
										 |  |  |     auto horizontal_container = GWidget::construct(); | 
					
						
							| 
									
										
										
										
											2019-05-26 22:33:54 +02:00
										 |  |  |     set_main_widget(horizontal_container); | 
					
						
							|  |  |  |     horizontal_container->set_layout(make<GBoxLayout>(Orientation::Horizontal)); | 
					
						
							|  |  |  |     horizontal_container->layout()->set_margins({ 4, 4, 4, 4 }); | 
					
						
							|  |  |  |     horizontal_container->set_fill_with_background_color(true); | 
					
						
							| 
									
										
										
										
											2019-06-30 09:23:16 +02:00
										 |  |  |     horizontal_container->set_background_color(Color::WarmGray); | 
					
						
							| 
									
										
										
										
											2019-05-26 22:33:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-21 17:05:35 +02:00
										 |  |  |     auto vertical_container = GWidget::construct(horizontal_container); | 
					
						
							| 
									
										
										
										
											2019-05-26 22:33:54 +02:00
										 |  |  |     vertical_container->set_layout(make<GBoxLayout>(Orientation::Vertical)); | 
					
						
							|  |  |  |     vertical_container->layout()->set_spacing(4); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-21 17:05:35 +02:00
										 |  |  |     auto upper_container = GWidget::construct(vertical_container); | 
					
						
							| 
									
										
										
										
											2019-05-09 16:47:45 +02:00
										 |  |  |     upper_container->set_layout(make<GBoxLayout>(Orientation::Horizontal)); | 
					
						
							| 
									
										
										
										
											2019-05-09 15:51:57 +02:00
										 |  |  |     upper_container->layout()->set_spacing(4); | 
					
						
							| 
									
										
										
										
											2019-05-09 16:47:45 +02:00
										 |  |  |     upper_container->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); | 
					
						
							| 
									
										
										
										
											2019-07-20 22:39:24 +02:00
										 |  |  |     upper_container->set_preferred_size(0, 26); | 
					
						
							| 
									
										
										
										
											2019-05-09 15:51:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-21 16:27:54 +02:00
										 |  |  |     auto toolbar = GToolBar::construct(upper_container); | 
					
						
							| 
									
										
										
										
											2019-05-09 16:47:45 +02:00
										 |  |  |     toolbar->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill); | 
					
						
							| 
									
										
										
										
											2019-07-28 13:04:57 -05:00
										 |  |  |     toolbar->set_preferred_size(85, 0); | 
					
						
							| 
									
										
										
										
											2019-05-16 14:06:55 +02:00
										 |  |  |     toolbar->set_has_frame(false); | 
					
						
							| 
									
										
										
										
											2019-05-09 15:51:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-21 15:43:52 +02:00
										 |  |  |     auto location_textbox = GTextBox::construct(upper_container); | 
					
						
							| 
									
										
										
										
											2019-05-09 16:47:45 +02:00
										 |  |  |     location_textbox->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); | 
					
						
							| 
									
										
										
										
											2019-07-20 22:39:24 +02:00
										 |  |  |     location_textbox->set_preferred_size(0, 20); | 
					
						
							| 
									
										
										
										
											2019-05-09 16:47:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-21 16:03:59 +02:00
										 |  |  |     m_view = GTableView::construct(vertical_container); | 
					
						
							| 
									
										
										
										
											2019-05-09 04:56:52 +02:00
										 |  |  |     m_view->set_model(GSortingProxyModel::create(*m_model)); | 
					
						
							| 
									
										
										
										
											2019-05-09 16:47:45 +02:00
										 |  |  |     m_view->set_column_hidden(GDirectoryModel::Column::Owner, true); | 
					
						
							|  |  |  |     m_view->set_column_hidden(GDirectoryModel::Column::Group, true); | 
					
						
							|  |  |  |     m_view->set_column_hidden(GDirectoryModel::Column::Permissions, true); | 
					
						
							|  |  |  |     m_view->set_column_hidden(GDirectoryModel::Column::Inode, true); | 
					
						
							| 
									
										
										
										
											2019-05-09 04:56:52 +02:00
										 |  |  |     m_model->open(path); | 
					
						
							| 
									
										
										
										
											2019-05-09 01:24:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-09 16:47:45 +02:00
										 |  |  |     location_textbox->on_return_pressed = [&] { | 
					
						
							|  |  |  |         m_model->open(location_textbox->text()); | 
					
						
							| 
									
										
										
										
											2019-05-26 22:33:54 +02:00
										 |  |  |         clear_preview(); | 
					
						
							| 
									
										
										
										
											2019-05-09 16:47:45 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |     auto open_parent_directory_action = GAction::create("Open parent directory", { Mod_Alt, Key_Up }, GraphicsBitmap::load_from_file("/res/icons/16x16/open-parent-directory.png"), [this](const GAction&) { | 
					
						
							| 
									
										
										
										
											2019-05-09 15:51:57 +02:00
										 |  |  |         m_model->open(String::format("%s/..", m_model->path().characters())); | 
					
						
							| 
									
										
										
										
											2019-05-26 22:33:54 +02:00
										 |  |  |         clear_preview(); | 
					
						
							| 
									
										
										
										
											2019-05-09 15:51:57 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  |     toolbar->add_action(*open_parent_directory_action); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-28 13:04:57 -05:00
										 |  |  |     auto go_home_action = GAction::create("Go to Home Directory", GraphicsBitmap::load_from_file("/res/icons/16x16/go-home.png"), [this](auto&) { | 
					
						
							|  |  |  |         m_model->open(get_current_user_home_path()); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     toolbar->add_action(go_home_action); | 
					
						
							|  |  |  |     toolbar->add_separator(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |     auto mkdir_action = GAction::create("New directory...", GraphicsBitmap::load_from_file("/res/icons/16x16/mkdir.png"), [this](const GAction&) { | 
					
						
							| 
									
										
										
										
											2019-05-09 18:45:33 +02:00
										 |  |  |         GInputBox input_box("Enter name:", "New directory", this); | 
					
						
							|  |  |  |         if (input_box.exec() == GInputBox::ExecOK && !input_box.text_value().is_empty()) { | 
					
						
							|  |  |  |             auto new_dir_path = FileSystemPath(String::format("%s/%s", | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |                                                    m_model->path().characters(), | 
					
						
							|  |  |  |                                                    input_box.text_value().characters())) | 
					
						
							|  |  |  |                                     .string(); | 
					
						
							| 
									
										
										
										
											2019-05-09 18:45:33 +02:00
										 |  |  |             int rc = mkdir(new_dir_path.characters(), 0777); | 
					
						
							|  |  |  |             if (rc < 0) { | 
					
						
							| 
									
										
										
										
											2019-07-16 21:32:10 +02:00
										 |  |  |                 GMessageBox::show(String::format("mkdir(\"%s\") failed: %s", new_dir_path.characters(), strerror(errno)), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, this); | 
					
						
							| 
									
										
										
										
											2019-05-09 18:45:33 +02:00
										 |  |  |             } else { | 
					
						
							|  |  |  |                 m_model->update(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     toolbar->add_action(*mkdir_action); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-21 17:05:35 +02:00
										 |  |  |     auto lower_container = GWidget::construct(vertical_container); | 
					
						
							| 
									
										
										
										
											2019-05-09 01:24:37 +02:00
										 |  |  |     lower_container->set_layout(make<GBoxLayout>(Orientation::Vertical)); | 
					
						
							|  |  |  |     lower_container->layout()->set_spacing(4); | 
					
						
							|  |  |  |     lower_container->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); | 
					
						
							| 
									
										
										
										
											2019-07-20 22:39:24 +02:00
										 |  |  |     lower_container->set_preferred_size(0, 60); | 
					
						
							| 
									
										
										
										
											2019-05-09 01:24:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-21 17:05:35 +02:00
										 |  |  |     auto filename_container = GWidget::construct(lower_container); | 
					
						
							| 
									
										
										
										
											2019-05-09 01:24:37 +02:00
										 |  |  |     filename_container->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); | 
					
						
							| 
									
										
										
										
											2019-07-20 22:39:24 +02:00
										 |  |  |     filename_container->set_preferred_size(0, 20); | 
					
						
							| 
									
										
										
										
											2019-05-09 01:24:37 +02:00
										 |  |  |     filename_container->set_layout(make<GBoxLayout>(Orientation::Horizontal)); | 
					
						
							| 
									
										
										
										
											2019-09-21 14:19:05 +02:00
										 |  |  |     auto filename_label = GLabel::construct("File name:", filename_container); | 
					
						
							| 
									
										
										
										
											2019-05-09 01:24:37 +02:00
										 |  |  |     filename_label->set_text_alignment(TextAlignment::CenterLeft); | 
					
						
							|  |  |  |     filename_label->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill); | 
					
						
							| 
									
										
										
										
											2019-07-20 22:39:24 +02:00
										 |  |  |     filename_label->set_preferred_size(60, 0); | 
					
						
							| 
									
										
										
										
											2019-09-21 15:43:52 +02:00
										 |  |  |     m_filename_textbox = GTextBox::construct(filename_container); | 
					
						
							| 
									
										
										
										
											2019-07-13 19:58:04 -05:00
										 |  |  |     if (m_mode == Mode::Save) { | 
					
						
							| 
									
										
										
										
											2019-07-29 15:50:06 -05:00
										 |  |  |         m_filename_textbox->set_text(file_name); | 
					
						
							|  |  |  |         m_filename_textbox->set_focus(true); | 
					
						
							|  |  |  |         m_filename_textbox->select_all(); | 
					
						
							| 
									
										
										
										
											2019-07-13 19:58:04 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-07-29 15:50:06 -05:00
										 |  |  |     m_filename_textbox->on_return_pressed = [&] { | 
					
						
							|  |  |  |         on_file_return(); | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2019-05-09 01:24:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-09 23:45:04 +02:00
										 |  |  |     m_view->on_selection = [this](auto& index) { | 
					
						
							| 
									
										
										
										
											2019-05-09 04:56:52 +02:00
										 |  |  |         auto& filter_model = (GSortingProxyModel&)*m_view->model(); | 
					
						
							|  |  |  |         auto local_index = filter_model.map_to_target(index); | 
					
						
							|  |  |  |         const GDirectoryModel::Entry& entry = m_model->entry(local_index.row()); | 
					
						
							|  |  |  |         FileSystemPath path(String::format("%s/%s", m_model->path().characters(), entry.name.characters())); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-26 22:33:54 +02:00
										 |  |  |         clear_preview(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-09 23:45:04 +02:00
										 |  |  |         if (!entry.is_directory()) | 
					
						
							| 
									
										
										
										
											2019-07-29 15:50:06 -05:00
										 |  |  |             m_filename_textbox->set_text(entry.name); | 
					
						
							| 
									
										
										
										
											2019-08-09 23:45:04 +02:00
										 |  |  |         set_preview(path); | 
					
						
							| 
									
										
										
										
											2019-05-09 04:56:52 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-21 17:05:35 +02:00
										 |  |  |     auto button_container = GWidget::construct(lower_container); | 
					
						
							| 
									
										
										
										
											2019-05-09 01:24:37 +02:00
										 |  |  |     button_container->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); | 
					
						
							| 
									
										
										
										
											2019-07-20 22:39:24 +02:00
										 |  |  |     button_container->set_preferred_size(0, 20); | 
					
						
							| 
									
										
										
										
											2019-05-09 01:24:37 +02:00
										 |  |  |     button_container->set_layout(make<GBoxLayout>(Orientation::Horizontal)); | 
					
						
							|  |  |  |     button_container->layout()->set_spacing(4); | 
					
						
							| 
									
										
										
										
											2019-05-09 03:06:20 +02:00
										 |  |  |     button_container->layout()->add_spacer(); | 
					
						
							| 
									
										
										
										
											2019-05-09 01:24:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-21 19:28:28 +02:00
										 |  |  |     auto cancel_button = GButton::construct(button_container); | 
					
						
							| 
									
										
										
										
											2019-05-09 01:24:37 +02:00
										 |  |  |     cancel_button->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill); | 
					
						
							| 
									
										
										
										
											2019-07-20 22:39:24 +02:00
										 |  |  |     cancel_button->set_preferred_size(80, 0); | 
					
						
							| 
									
										
										
										
											2019-05-24 16:32:20 +02:00
										 |  |  |     cancel_button->set_text("Cancel"); | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |     cancel_button->on_click = [this](auto&) { | 
					
						
							| 
									
										
										
										
											2019-05-09 01:24:37 +02:00
										 |  |  |         done(ExecCancel); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-21 19:28:28 +02:00
										 |  |  |     auto ok_button = GButton::construct(button_container); | 
					
						
							| 
									
										
										
										
											2019-05-09 01:24:37 +02:00
										 |  |  |     ok_button->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill); | 
					
						
							| 
									
										
										
										
											2019-07-20 22:39:24 +02:00
										 |  |  |     ok_button->set_preferred_size(80, 0); | 
					
						
							| 
									
										
										
										
											2019-07-13 19:58:04 -05:00
										 |  |  |     ok_button->set_text(ok_button_name(m_mode)); | 
					
						
							| 
									
										
										
										
											2019-07-29 15:50:06 -05:00
										 |  |  |     ok_button->on_click = [this](auto&) { | 
					
						
							|  |  |  |         on_file_return(); | 
					
						
							| 
									
										
										
										
											2019-05-09 01:24:37 +02:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2019-05-26 22:33:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-09 23:45:04 +02:00
										 |  |  |     m_view->on_activation = [this](auto& index) { | 
					
						
							|  |  |  |         auto& filter_model = (GSortingProxyModel&)*m_view->model(); | 
					
						
							|  |  |  |         auto local_index = filter_model.map_to_target(index); | 
					
						
							|  |  |  |         const GDirectoryModel::Entry& entry = m_model->entry(local_index.row()); | 
					
						
							|  |  |  |         FileSystemPath path(String::format("%s/%s", m_model->path().characters(), entry.name.characters())); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (entry.is_directory()) { | 
					
						
							|  |  |  |             m_model->open(path.string()); | 
					
						
							|  |  |  |             // NOTE: 'entry' is invalid from here on
 | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             on_file_return(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-21 19:21:36 +02:00
										 |  |  |     auto preview_container = GFrame::construct(horizontal_container); | 
					
						
							| 
									
										
										
										
											2019-05-26 22:33:54 +02:00
										 |  |  |     preview_container->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill); | 
					
						
							| 
									
										
										
										
											2019-07-20 22:39:24 +02:00
										 |  |  |     preview_container->set_preferred_size(180, 0); | 
					
						
							| 
									
										
										
										
											2019-05-26 22:33:54 +02:00
										 |  |  |     preview_container->set_frame_shape(FrameShape::Container); | 
					
						
							|  |  |  |     preview_container->set_frame_shadow(FrameShadow::Sunken); | 
					
						
							|  |  |  |     preview_container->set_frame_thickness(2); | 
					
						
							|  |  |  |     preview_container->set_layout(make<GBoxLayout>(Orientation::Vertical)); | 
					
						
							|  |  |  |     preview_container->layout()->set_margins({ 8, 8, 8, 8 }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-21 14:19:05 +02:00
										 |  |  |     m_preview_image_label = GLabel::construct(preview_container); | 
					
						
							| 
									
										
										
										
											2019-05-26 22:33:54 +02:00
										 |  |  |     m_preview_image_label->set_should_stretch_icon(true); | 
					
						
							|  |  |  |     m_preview_image_label->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed); | 
					
						
							| 
									
										
										
										
											2019-07-20 22:39:24 +02:00
										 |  |  |     m_preview_image_label->set_preferred_size(160, 160); | 
					
						
							| 
									
										
										
										
											2019-05-26 22:33:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-21 14:19:05 +02:00
										 |  |  |     m_preview_name_label = GLabel::construct(preview_container); | 
					
						
							| 
									
										
										
										
											2019-05-26 22:33:54 +02:00
										 |  |  |     m_preview_name_label->set_font(Font::default_bold_font()); | 
					
						
							|  |  |  |     m_preview_name_label->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); | 
					
						
							| 
									
										
										
										
											2019-07-20 22:39:24 +02:00
										 |  |  |     m_preview_name_label->set_preferred_size(0, m_preview_name_label->font().glyph_height()); | 
					
						
							| 
									
										
										
										
											2019-05-26 22:33:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-21 14:19:05 +02:00
										 |  |  |     m_preview_geometry_label = GLabel::construct(preview_container); | 
					
						
							| 
									
										
										
										
											2019-05-26 22:33:54 +02:00
										 |  |  |     m_preview_geometry_label->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); | 
					
						
							| 
									
										
										
										
											2019-07-20 22:39:24 +02:00
										 |  |  |     m_preview_geometry_label->set_preferred_size(0, m_preview_name_label->font().glyph_height()); | 
					
						
							| 
									
										
										
										
											2019-05-09 01:24:37 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GFilePicker::~GFilePicker() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-05-26 22:33:54 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | void GFilePicker::set_preview(const FileSystemPath& path) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (path.has_extension(".png")) { | 
					
						
							|  |  |  |         auto bitmap = load_png(path.string()); | 
					
						
							|  |  |  |         if (!bitmap) { | 
					
						
							|  |  |  |             clear_preview(); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         bool should_stretch = bitmap->width() > m_preview_image_label->width() || bitmap->height() > m_preview_image_label->height(); | 
					
						
							|  |  |  |         m_preview_name_label->set_text(path.basename()); | 
					
						
							|  |  |  |         m_preview_geometry_label->set_text(bitmap->size().to_string()); | 
					
						
							|  |  |  |         m_preview_image_label->set_should_stretch_icon(should_stretch); | 
					
						
							|  |  |  |         m_preview_image_label->set_icon(move(bitmap)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GFilePicker::clear_preview() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_preview_image_label->set_icon(nullptr); | 
					
						
							|  |  |  |     m_preview_name_label->set_text(String::empty()); | 
					
						
							|  |  |  |     m_preview_geometry_label->set_text(String::empty()); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-07-13 19:58:04 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-29 15:50:06 -05:00
										 |  |  | void GFilePicker::on_file_return() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     FileSystemPath path(String::format("%s/%s", m_model->path().characters(), m_filename_textbox->text().characters())); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (GFilePicker::file_exists(path.string()) && m_mode == Mode::Save) { | 
					
						
							|  |  |  |         GMessageBox box("File already exists, overwrite?", "Existing File", GMessageBox::Type::Warning, GMessageBox::InputType::OKCancel); | 
					
						
							|  |  |  |         if (box.exec() == GMessageBox::ExecCancel) | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_selected_file = path; | 
					
						
							|  |  |  |     done(ExecOK); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-13 19:58:04 -05:00
										 |  |  | bool GFilePicker::file_exists(const StringView& path) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     struct stat st; | 
					
						
							|  |  |  |     int rc = stat(String(path).characters(), &st); | 
					
						
							|  |  |  |     if (rc < 0) { | 
					
						
							|  |  |  |         if (errno == ENOENT) | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (rc == 0) { | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return false; | 
					
						
							| 
									
										
										
										
											2019-07-16 21:32:10 +02:00
										 |  |  | } |