Serenity Operating System
at portability 314 lines 13 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 42namespace GUI { 43 44Optional<String> FilePicker::get_open_filepath(const String& window_title) 45{ 46 auto picker = FilePicker::construct(Mode::Open); 47 48 if (!window_title.is_null()) 49 picker->set_title(window_title); 50 51 if (picker->exec() == Dialog::ExecOK) { 52 String file_path = picker->selected_file().string(); 53 54 if (file_path.is_null()) 55 return {}; 56 57 return file_path; 58 } 59 return {}; 60} 61 62Optional<String> FilePicker::get_save_filepath(const String& title, const String& extension) 63{ 64 auto picker = FilePicker::construct(Mode::Save, String::format("%s.%s", title.characters(), extension.characters())); 65 66 if (picker->exec() == Dialog::ExecOK) { 67 String file_path = picker->selected_file().string(); 68 69 if (file_path.is_null()) 70 return {}; 71 72 return file_path; 73 } 74 return {}; 75} 76 77FilePicker::FilePicker(Mode mode, const StringView& file_name, const StringView& path, Core::Object* parent) 78 : Dialog(parent) 79 , m_model(FileSystemModel::create()) 80 , m_mode(mode) 81{ 82 set_title(m_mode == Mode::Open ? "Open File" : "Save File"); 83 set_rect(200, 200, 700, 400); 84 auto horizontal_container = Widget::construct(); 85 set_main_widget(horizontal_container); 86 horizontal_container->set_layout(make<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(make<VerticalBoxLayout>()); 92 vertical_container->layout()->set_spacing(4); 93 94 auto upper_container = vertical_container->add<Widget>(); 95 upper_container->set_layout(make<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 // FIXME: Enable this once GUI::ColumnsView doesn't crash when used here. 160 m_view->view_as_columns_action().set_enabled(false); 161 toolbar->add_action(m_view->view_as_columns_action()); 162 163 auto lower_container = vertical_container->add<Widget>(); 164 lower_container->set_layout(make<VerticalBoxLayout>()); 165 lower_container->layout()->set_spacing(4); 166 lower_container->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); 167 lower_container->set_preferred_size(0, 60); 168 169 auto filename_container = lower_container->add<Widget>(); 170 filename_container->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); 171 filename_container->set_preferred_size(0, 20); 172 filename_container->set_layout(make<HorizontalBoxLayout>()); 173 auto filename_label = filename_container->add<Label>("File name:"); 174 filename_label->set_text_alignment(Gfx::TextAlignment::CenterLeft); 175 filename_label->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill); 176 filename_label->set_preferred_size(60, 0); 177 m_filename_textbox = filename_container->add<TextBox>(); 178 if (m_mode == Mode::Save) { 179 m_filename_textbox->set_text(file_name); 180 m_filename_textbox->set_focus(true); 181 m_filename_textbox->select_all(); 182 } 183 m_filename_textbox->on_return_pressed = [&] { 184 on_file_return(); 185 }; 186 187 m_view->on_selection_change = [this] { 188 auto index = m_view->selection().first(); 189 auto& filter_model = (SortingProxyModel&)*m_view->model(); 190 auto local_index = filter_model.map_to_target(index); 191 const FileSystemModel::Node& node = m_model->node(local_index); 192 FileSystemPath path { node.full_path(m_model) }; 193 194 clear_preview(); 195 196 if (!node.is_directory()) 197 m_filename_textbox->set_text(node.name); 198 set_preview(path); 199 }; 200 201 auto button_container = lower_container->add<Widget>(); 202 button_container->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); 203 button_container->set_preferred_size(0, 20); 204 button_container->set_layout(make<HorizontalBoxLayout>()); 205 button_container->layout()->set_spacing(4); 206 button_container->layout()->add_spacer(); 207 208 auto cancel_button = button_container->add<Button>(); 209 cancel_button->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill); 210 cancel_button->set_preferred_size(80, 0); 211 cancel_button->set_text("Cancel"); 212 cancel_button->on_click = [this](auto&) { 213 done(ExecCancel); 214 }; 215 216 auto ok_button = button_container->add<Button>(); 217 ok_button->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill); 218 ok_button->set_preferred_size(80, 0); 219 ok_button->set_text(ok_button_name(m_mode)); 220 ok_button->on_click = [this](auto&) { 221 on_file_return(); 222 }; 223 224 m_view->on_activation = [this](auto& index) { 225 auto& filter_model = (SortingProxyModel&)*m_view->model(); 226 auto local_index = filter_model.map_to_target(index); 227 const FileSystemModel::Node& node = m_model->node(local_index); 228 auto path = node.full_path(m_model); 229 230 if (node.is_directory()) { 231 m_model->set_root_path(path); 232 // NOTE: 'node' is invalid from here on 233 } else { 234 on_file_return(); 235 } 236 }; 237 238 auto preview_container = horizontal_container->add<Frame>(); 239 preview_container->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill); 240 preview_container->set_preferred_size(180, 0); 241 preview_container->set_layout(make<VerticalBoxLayout>()); 242 preview_container->layout()->set_margins({ 8, 8, 8, 8 }); 243 244 m_preview_image_label = preview_container->add<Label>(); 245 m_preview_image_label->set_should_stretch_icon(true); 246 m_preview_image_label->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed); 247 m_preview_image_label->set_preferred_size(160, 160); 248 249 m_preview_name_label = preview_container->add<Label>(); 250 m_preview_name_label->set_font(Gfx::Font::default_bold_font()); 251 m_preview_name_label->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); 252 m_preview_name_label->set_preferred_size(0, m_preview_name_label->font().glyph_height()); 253 254 m_preview_geometry_label = preview_container->add<Label>(); 255 m_preview_geometry_label->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); 256 m_preview_geometry_label->set_preferred_size(0, m_preview_name_label->font().glyph_height()); 257} 258 259FilePicker::~FilePicker() 260{ 261} 262 263void FilePicker::set_preview(const FileSystemPath& path) 264{ 265 if (path.has_extension(".png")) { 266 auto bitmap = Gfx::Bitmap::load_from_file(path.string()); 267 if (!bitmap) { 268 clear_preview(); 269 return; 270 } 271 bool should_stretch = bitmap->width() > m_preview_image_label->width() || bitmap->height() > m_preview_image_label->height(); 272 m_preview_name_label->set_text(path.basename()); 273 m_preview_geometry_label->set_text(bitmap->size().to_string()); 274 m_preview_image_label->set_should_stretch_icon(should_stretch); 275 m_preview_image_label->set_icon(move(bitmap)); 276 } 277} 278 279void FilePicker::clear_preview() 280{ 281 m_preview_image_label->set_icon(nullptr); 282 m_preview_name_label->set_text(String::empty()); 283 m_preview_geometry_label->set_text(String::empty()); 284} 285 286void FilePicker::on_file_return() 287{ 288 FileSystemPath path(String::format("%s/%s", m_model->root_path().characters(), m_filename_textbox->text().characters())); 289 290 if (FilePicker::file_exists(path.string()) && m_mode == Mode::Save) { 291 auto result = MessageBox::show("File already exists, overwrite?", "Existing File", MessageBox::Type::Warning, MessageBox::InputType::OKCancel); 292 if (result == MessageBox::ExecCancel) 293 return; 294 } 295 296 m_selected_file = path; 297 done(ExecOK); 298} 299 300bool FilePicker::file_exists(const StringView& path) 301{ 302 struct stat st; 303 int rc = stat(String(path).characters(), &st); 304 if (rc < 0) { 305 if (errno == ENOENT) 306 return false; 307 } 308 if (rc == 0) { 309 return true; 310 } 311 return false; 312} 313 314}