Serenity Operating System
at hosted 315 lines 12 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <AK/FileSystemPath.h> 28#include <AK/Function.h> 29#include <LibGUI/Action.h> 30#include <LibGUI/BoxLayout.h> 31#include <LibGUI/Button.h> 32#include <LibGUI/FilePicker.h> 33#include <LibGUI/FileSystemModel.h> 34#include <LibGUI/InputBox.h> 35#include <LibGUI/Label.h> 36#include <LibGUI/MessageBox.h> 37#include <LibGUI/MultiView.h> 38#include <LibGUI/SortingProxyModel.h> 39#include <LibGUI/TextBox.h> 40#include <LibGUI/ToolBar.h> 41#include <string.h> 42 43namespace GUI { 44 45Optional<String> FilePicker::get_open_filepath(const String& window_title) 46{ 47 auto picker = FilePicker::construct(Mode::Open); 48 49 if (!window_title.is_null()) 50 picker->set_title(window_title); 51 52 if (picker->exec() == Dialog::ExecOK) { 53 String file_path = picker->selected_file().string(); 54 55 if (file_path.is_null()) 56 return {}; 57 58 return file_path; 59 } 60 return {}; 61} 62 63Optional<String> FilePicker::get_save_filepath(const String& title, const String& extension) 64{ 65 auto picker = FilePicker::construct(Mode::Save, String::format("%s.%s", title.characters(), extension.characters())); 66 67 if (picker->exec() == Dialog::ExecOK) { 68 String file_path = picker->selected_file().string(); 69 70 if (file_path.is_null()) 71 return {}; 72 73 return file_path; 74 } 75 return {}; 76} 77 78FilePicker::FilePicker(Mode mode, const StringView& file_name, const StringView& path, Window* parent_window) 79 : Dialog(parent_window) 80 , m_model(FileSystemModel::create()) 81 , m_mode(mode) 82{ 83 set_title(m_mode == Mode::Open ? "Open File" : "Save File"); 84 set_rect(200, 200, 700, 400); 85 auto& horizontal_container = set_main_widget<Widget>(); 86 horizontal_container.set_layout<HorizontalBoxLayout>(); 87 horizontal_container.layout()->set_margins({ 4, 4, 4, 4 }); 88 horizontal_container.set_fill_with_background_color(true); 89 90 auto& vertical_container = horizontal_container.add<Widget>(); 91 vertical_container.set_layout<VerticalBoxLayout>(); 92 vertical_container.layout()->set_spacing(4); 93 94 auto& upper_container = vertical_container.add<Widget>(); 95 upper_container.set_layout<HorizontalBoxLayout>(); 96 upper_container.layout()->set_spacing(4); 97 upper_container.set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); 98 upper_container.set_preferred_size(0, 26); 99 100 auto& toolbar = upper_container.add<ToolBar>(); 101 toolbar.set_size_policy(SizePolicy::Fixed, SizePolicy::Fill); 102 toolbar.set_preferred_size(165, 0); 103 toolbar.set_has_frame(false); 104 105 auto& location_textbox = upper_container.add<TextBox>(); 106 location_textbox.set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); 107 location_textbox.set_preferred_size(0, 20); 108 109 m_view = vertical_container.add<MultiView>(); 110 m_view->set_model(SortingProxyModel::create(*m_model)); 111 m_view->set_model_column(FileSystemModel::Column::Name); 112 m_view->set_column_hidden(FileSystemModel::Column::Owner, true); 113 m_view->set_column_hidden(FileSystemModel::Column::Group, true); 114 m_view->set_column_hidden(FileSystemModel::Column::Permissions, true); 115 m_view->set_column_hidden(FileSystemModel::Column::Inode, true); 116 m_view->set_column_hidden(FileSystemModel::Column::SymlinkTarget, true); 117 m_model->set_root_path(path); 118 119 location_textbox.on_return_pressed = [&] { 120 m_model->set_root_path(location_textbox.text()); 121 clear_preview(); 122 }; 123 124 auto open_parent_directory_action = Action::create("Open parent directory", { Mod_Alt, Key_Up }, Gfx::Bitmap::load_from_file("/res/icons/16x16/open-parent-directory.png"), [this](const Action&) { 125 m_model->set_root_path(String::format("%s/..", m_model->root_path().characters())); 126 clear_preview(); 127 }); 128 toolbar.add_action(*open_parent_directory_action); 129 130 auto go_home_action = CommonActions::make_go_home_action([this](auto&) { 131 m_model->set_root_path(get_current_user_home_path()); 132 }); 133 toolbar.add_action(go_home_action); 134 toolbar.add_separator(); 135 136 auto mkdir_action = Action::create("New directory...", Gfx::Bitmap::load_from_file("/res/icons/16x16/mkdir.png"), [this](const Action&) { 137 auto& input_box = add<InputBox>("Enter name:", "New directory"); 138 if (input_box.exec() == InputBox::ExecOK && !input_box.text_value().is_empty()) { 139 auto new_dir_path = FileSystemPath(String::format("%s/%s", 140 m_model->root_path().characters(), 141 input_box.text_value().characters())) 142 .string(); 143 int rc = mkdir(new_dir_path.characters(), 0777); 144 if (rc < 0) { 145 MessageBox::show(String::format("mkdir(\"%s\") failed: %s", new_dir_path.characters(), strerror(errno)), "Error", MessageBox::Type::Error, MessageBox::InputType::OK, this); 146 } else { 147 m_model->update(); 148 } 149 } 150 }); 151 152 toolbar.add_action(*mkdir_action); 153 154 toolbar.add_separator(); 155 156 toolbar.add_action(m_view->view_as_icons_action()); 157 toolbar.add_action(m_view->view_as_table_action()); 158 159#ifdef MULTIVIEW_WITH_COLUMNSVIEW 160 m_view->view_as_columns_action().set_enabled(false); 161 toolbar.add_action(m_view->view_as_columns_action()); 162#endif 163 164 auto& lower_container = vertical_container.add<Widget>(); 165 lower_container.set_layout<VerticalBoxLayout>(); 166 lower_container.layout()->set_spacing(4); 167 lower_container.set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); 168 lower_container.set_preferred_size(0, 60); 169 170 auto& filename_container = lower_container.add<Widget>(); 171 filename_container.set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); 172 filename_container.set_preferred_size(0, 20); 173 filename_container.set_layout<HorizontalBoxLayout>(); 174 auto& filename_label = filename_container.add<Label>("File name:"); 175 filename_label.set_text_alignment(Gfx::TextAlignment::CenterLeft); 176 filename_label.set_size_policy(SizePolicy::Fixed, SizePolicy::Fill); 177 filename_label.set_preferred_size(60, 0); 178 m_filename_textbox = filename_container.add<TextBox>(); 179 if (m_mode == Mode::Save) { 180 m_filename_textbox->set_text(file_name); 181 m_filename_textbox->set_focus(true); 182 m_filename_textbox->select_all(); 183 } 184 m_filename_textbox->on_return_pressed = [&] { 185 on_file_return(); 186 }; 187 188 m_view->on_selection_change = [this] { 189 auto index = m_view->selection().first(); 190 auto& filter_model = (SortingProxyModel&)*m_view->model(); 191 auto local_index = filter_model.map_to_target(index); 192 const FileSystemModel::Node& node = m_model->node(local_index); 193 FileSystemPath path { node.full_path(m_model) }; 194 195 clear_preview(); 196 197 if (!node.is_directory()) 198 m_filename_textbox->set_text(node.name); 199 set_preview(path); 200 }; 201 202 auto& button_container = lower_container.add<Widget>(); 203 button_container.set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); 204 button_container.set_preferred_size(0, 20); 205 button_container.set_layout<HorizontalBoxLayout>(); 206 button_container.layout()->set_spacing(4); 207 button_container.layout()->add_spacer(); 208 209 auto& cancel_button = button_container.add<Button>(); 210 cancel_button.set_size_policy(SizePolicy::Fixed, SizePolicy::Fill); 211 cancel_button.set_preferred_size(80, 0); 212 cancel_button.set_text("Cancel"); 213 cancel_button.on_click = [this] { 214 done(ExecCancel); 215 }; 216 217 auto& ok_button = button_container.add<Button>(); 218 ok_button.set_size_policy(SizePolicy::Fixed, SizePolicy::Fill); 219 ok_button.set_preferred_size(80, 0); 220 ok_button.set_text(ok_button_name(m_mode)); 221 ok_button.on_click = [this] { 222 on_file_return(); 223 }; 224 225 m_view->on_activation = [this](auto& index) { 226 auto& filter_model = (SortingProxyModel&)*m_view->model(); 227 auto local_index = filter_model.map_to_target(index); 228 const FileSystemModel::Node& node = m_model->node(local_index); 229 auto path = node.full_path(m_model); 230 231 if (node.is_directory()) { 232 m_model->set_root_path(path); 233 // NOTE: 'node' is invalid from here on 234 } else { 235 on_file_return(); 236 } 237 }; 238 239 auto& preview_container = horizontal_container.add<Frame>(); 240 preview_container.set_size_policy(SizePolicy::Fixed, SizePolicy::Fill); 241 preview_container.set_preferred_size(180, 0); 242 preview_container.set_layout<VerticalBoxLayout>(); 243 preview_container.layout()->set_margins({ 8, 8, 8, 8 }); 244 245 m_preview_image_label = preview_container.add<Label>(); 246 m_preview_image_label->set_should_stretch_icon(true); 247 m_preview_image_label->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed); 248 m_preview_image_label->set_preferred_size(160, 160); 249 250 m_preview_name_label = preview_container.add<Label>(); 251 m_preview_name_label->set_font(Gfx::Font::default_bold_font()); 252 m_preview_name_label->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); 253 m_preview_name_label->set_preferred_size(0, m_preview_name_label->font().glyph_height()); 254 255 m_preview_geometry_label = preview_container.add<Label>(); 256 m_preview_geometry_label->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); 257 m_preview_geometry_label->set_preferred_size(0, m_preview_name_label->font().glyph_height()); 258} 259 260FilePicker::~FilePicker() 261{ 262} 263 264void FilePicker::set_preview(const FileSystemPath& path) 265{ 266 if (path.has_extension(".png")) { 267 auto bitmap = Gfx::Bitmap::load_from_file(path.string()); 268 if (!bitmap) { 269 clear_preview(); 270 return; 271 } 272 bool should_stretch = bitmap->width() > m_preview_image_label->width() || bitmap->height() > m_preview_image_label->height(); 273 m_preview_name_label->set_text(path.basename()); 274 m_preview_geometry_label->set_text(bitmap->size().to_string()); 275 m_preview_image_label->set_should_stretch_icon(should_stretch); 276 m_preview_image_label->set_icon(move(bitmap)); 277 } 278} 279 280void FilePicker::clear_preview() 281{ 282 m_preview_image_label->set_icon(nullptr); 283 m_preview_name_label->set_text(String::empty()); 284 m_preview_geometry_label->set_text(String::empty()); 285} 286 287void FilePicker::on_file_return() 288{ 289 FileSystemPath path(String::format("%s/%s", m_model->root_path().characters(), m_filename_textbox->text().characters())); 290 291 if (FilePicker::file_exists(path.string()) && m_mode == Mode::Save) { 292 auto result = MessageBox::show("File already exists, overwrite?", "Existing File", MessageBox::Type::Warning, MessageBox::InputType::OKCancel); 293 if (result == MessageBox::ExecCancel) 294 return; 295 } 296 297 m_selected_file = path; 298 done(ExecOK); 299} 300 301bool FilePicker::file_exists(const StringView& path) 302{ 303 struct stat st; 304 int rc = stat(String(path).characters(), &st); 305 if (rc < 0) { 306 if (errno == ENOENT) 307 return false; 308 } 309 if (rc == 0) { 310 return true; 311 } 312 return false; 313} 314 315}