/* * Copyright (c) 2018-2020, Andreas Kling * 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. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace GUI { Optional FilePicker::get_open_filepath(Window* parent_window, const String& window_title, Options options) { auto picker = FilePicker::construct(parent_window, Mode::Open, options); if (!window_title.is_null()) picker->set_title(window_title); if (picker->exec() == Dialog::ExecOK) { String file_path = picker->selected_file().string(); if (file_path.is_null()) return {}; return file_path; } return {}; } Optional FilePicker::get_save_filepath(Window* parent_window, const String& title, const String& extension, Options options) { auto picker = FilePicker::construct(parent_window, Mode::Save, options, String::format("%s.%s", title.characters(), extension.characters())); if (picker->exec() == Dialog::ExecOK) { String file_path = picker->selected_file().string(); if (file_path.is_null()) return {}; return file_path; } return {}; } FilePicker::FilePicker(Window* parent_window, Mode mode, Options options, const StringView& file_name, const StringView& path) : Dialog(parent_window) , m_model(FileSystemModel::create()) , m_mode(mode) { switch (m_mode) { case Mode::Open: set_title("Open File"); break; case Mode::OpenMultiple: set_title("Open Files"); break; case Mode::Save: set_title("Save File"); break; } set_title(m_mode == Mode::Open ? "Open File" : "Save File"); set_rect(200, 200, 700, 400); auto& horizontal_container = set_main_widget(); horizontal_container.set_layout(); horizontal_container.layout()->set_margins({ 4, 4, 4, 4 }); horizontal_container.set_fill_with_background_color(true); auto& vertical_container = horizontal_container.add(); vertical_container.set_layout(); vertical_container.layout()->set_spacing(4); auto& upper_container = vertical_container.add(); upper_container.set_layout(); upper_container.layout()->set_spacing(2); upper_container.set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); upper_container.set_preferred_size(0, 26); auto& toolbar = upper_container.add(); toolbar.set_size_policy(SizePolicy::Fixed, SizePolicy::Fill); #ifdef MULTIVIEW_WITH_COLUMNSVIEW toolbar.set_preferred_size(165, 0); #else toolbar.set_preferred_size(140, 0); #endif toolbar.set_has_frame(false); m_location_textbox = upper_container.add(); m_location_textbox->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); m_location_textbox->set_preferred_size(0, 22); m_location_textbox->set_text(path); m_view = vertical_container.add(); m_view->set_multi_select(m_mode == Mode::OpenMultiple); m_view->set_model(SortingProxyModel::create(*m_model)); m_view->set_model_column(FileSystemModel::Column::Name); m_view->model()->set_key_column_and_sort_order(GUI::FileSystemModel::Column::Name, GUI::SortOrder::Ascending); m_view->set_column_hidden(FileSystemModel::Column::Owner, true); m_view->set_column_hidden(FileSystemModel::Column::Group, true); m_view->set_column_hidden(FileSystemModel::Column::Permissions, true); m_view->set_column_hidden(FileSystemModel::Column::Inode, true); m_view->set_column_hidden(FileSystemModel::Column::SymlinkTarget, true); set_path(path); m_model->register_client(*this); m_location_textbox->on_return_pressed = [this] { set_path(m_location_textbox->text()); }; 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&) { set_path(String::format("%s/..", m_model->root_path().characters())); }); toolbar.add_action(*open_parent_directory_action); auto go_home_action = CommonActions::make_go_home_action([this](auto&) { set_path(Core::StandardPaths::home_directory()); }); toolbar.add_action(go_home_action); toolbar.add_separator(); auto mkdir_action = Action::create("New directory...", Gfx::Bitmap::load_from_file("/res/icons/16x16/mkdir.png"), [this](const Action&) { String value; if (InputBox::show(value, this, "Enter name:", "New directory") == InputBox::ExecOK && !value.is_empty()) { auto new_dir_path = LexicalPath(String::format("%s/%s", m_model->root_path().characters(), value.characters())) .string(); int rc = mkdir(new_dir_path.characters(), 0777); if (rc < 0) { MessageBox::show(this, String::format("mkdir(\"%s\") failed: %s", new_dir_path.characters(), strerror(errno)), "Error", MessageBox::Type::Error); } else { m_model->update(); } } }); toolbar.add_action(*mkdir_action); toolbar.add_separator(); toolbar.add_action(m_view->view_as_icons_action()); toolbar.add_action(m_view->view_as_table_action()); #ifdef MULTIVIEW_WITH_COLUMNSVIEW m_view->view_as_columns_action().set_enabled(false); toolbar.add_action(m_view->view_as_columns_action()); #endif auto& lower_container = vertical_container.add(); lower_container.set_layout(); lower_container.layout()->set_spacing(4); lower_container.set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); lower_container.set_preferred_size(0, 45); auto& filename_container = lower_container.add(); filename_container.set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); filename_container.set_preferred_size(0, 20); filename_container.set_layout(); auto& filename_label = filename_container.add