GFilePicker.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. #include <AK/FileSystemPath.h>
  27. #include <AK/Function.h>
  28. #include <LibDraw/PNGLoader.h>
  29. #include <LibGUI/GAction.h>
  30. #include <LibGUI/GBoxLayout.h>
  31. #include <LibGUI/GButton.h>
  32. #include <LibGUI/GFilePicker.h>
  33. #include <LibGUI/GFileSystemModel.h>
  34. #include <LibGUI/GInputBox.h>
  35. #include <LibGUI/GLabel.h>
  36. #include <LibGUI/GMessageBox.h>
  37. #include <LibGUI/GSortingProxyModel.h>
  38. #include <LibGUI/GTextBox.h>
  39. #include <LibGUI/GToolBar.h>
  40. Optional<String> GFilePicker::get_open_filepath(const String& window_title)
  41. {
  42. auto picker = GFilePicker::construct(Mode::Open);
  43. if (!window_title.is_null())
  44. picker->set_title(window_title);
  45. if (picker->exec() == GDialog::ExecOK) {
  46. String file_path = picker->selected_file().string();
  47. if (file_path.is_null())
  48. return {};
  49. return file_path;
  50. }
  51. return {};
  52. }
  53. Optional<String> GFilePicker::get_save_filepath(const String& title, const String& extension)
  54. {
  55. auto picker = GFilePicker::construct(Mode::Save, String::format("%s.%s", title.characters(), extension.characters()));
  56. if (picker->exec() == GDialog::ExecOK) {
  57. String file_path = picker->selected_file().string();
  58. if (file_path.is_null())
  59. return {};
  60. return file_path;
  61. }
  62. return {};
  63. }
  64. GFilePicker::GFilePicker(Mode mode, const StringView& file_name, const StringView& path, CObject* parent)
  65. : GDialog(parent)
  66. , m_model(GFileSystemModel::create())
  67. , m_mode(mode)
  68. {
  69. set_title(m_mode == Mode::Open ? "Open File" : "Save File");
  70. set_rect(200, 200, 700, 400);
  71. auto horizontal_container = GWidget::construct();
  72. set_main_widget(horizontal_container);
  73. horizontal_container->set_layout(make<GBoxLayout>(Orientation::Horizontal));
  74. horizontal_container->layout()->set_margins({ 4, 4, 4, 4 });
  75. horizontal_container->set_fill_with_background_color(true);
  76. auto vertical_container = GWidget::construct(horizontal_container.ptr());
  77. vertical_container->set_layout(make<GBoxLayout>(Orientation::Vertical));
  78. vertical_container->layout()->set_spacing(4);
  79. auto upper_container = GWidget::construct(vertical_container.ptr());
  80. upper_container->set_layout(make<GBoxLayout>(Orientation::Horizontal));
  81. upper_container->layout()->set_spacing(4);
  82. upper_container->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  83. upper_container->set_preferred_size(0, 26);
  84. auto toolbar = GToolBar::construct(upper_container);
  85. toolbar->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  86. toolbar->set_preferred_size(85, 0);
  87. toolbar->set_has_frame(false);
  88. auto location_textbox = GTextBox::construct(upper_container);
  89. location_textbox->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  90. location_textbox->set_preferred_size(0, 20);
  91. m_view = GTableView::construct(vertical_container);
  92. m_view->set_model(GSortingProxyModel::create(*m_model));
  93. m_view->set_column_hidden(GFileSystemModel::Column::Owner, true);
  94. m_view->set_column_hidden(GFileSystemModel::Column::Group, true);
  95. m_view->set_column_hidden(GFileSystemModel::Column::Permissions, true);
  96. m_view->set_column_hidden(GFileSystemModel::Column::Inode, true);
  97. m_model->set_root_path(path);
  98. location_textbox->on_return_pressed = [&] {
  99. m_model->set_root_path(location_textbox->text());
  100. clear_preview();
  101. };
  102. 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&) {
  103. m_model->set_root_path(String::format("%s/..", m_model->root_path().characters()));
  104. clear_preview();
  105. });
  106. toolbar->add_action(*open_parent_directory_action);
  107. auto go_home_action = GCommonActions::make_go_home_action([this](auto&) {
  108. m_model->set_root_path(get_current_user_home_path());
  109. });
  110. toolbar->add_action(go_home_action);
  111. toolbar->add_separator();
  112. auto mkdir_action = GAction::create("New directory...", GraphicsBitmap::load_from_file("/res/icons/16x16/mkdir.png"), [this](const GAction&) {
  113. auto input_box = GInputBox::construct("Enter name:", "New directory", this);
  114. if (input_box->exec() == GInputBox::ExecOK && !input_box->text_value().is_empty()) {
  115. auto new_dir_path = FileSystemPath(String::format("%s/%s",
  116. m_model->root_path().characters(),
  117. input_box->text_value().characters()))
  118. .string();
  119. int rc = mkdir(new_dir_path.characters(), 0777);
  120. if (rc < 0) {
  121. GMessageBox::show(String::format("mkdir(\"%s\") failed: %s", new_dir_path.characters(), strerror(errno)), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, this);
  122. } else {
  123. m_model->update();
  124. }
  125. }
  126. });
  127. toolbar->add_action(*mkdir_action);
  128. auto lower_container = GWidget::construct(vertical_container.ptr());
  129. lower_container->set_layout(make<GBoxLayout>(Orientation::Vertical));
  130. lower_container->layout()->set_spacing(4);
  131. lower_container->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  132. lower_container->set_preferred_size(0, 60);
  133. auto filename_container = GWidget::construct(lower_container.ptr());
  134. filename_container->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  135. filename_container->set_preferred_size(0, 20);
  136. filename_container->set_layout(make<GBoxLayout>(Orientation::Horizontal));
  137. auto filename_label = GLabel::construct("File name:", filename_container);
  138. filename_label->set_text_alignment(TextAlignment::CenterLeft);
  139. filename_label->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  140. filename_label->set_preferred_size(60, 0);
  141. m_filename_textbox = GTextBox::construct(filename_container.ptr());
  142. if (m_mode == Mode::Save) {
  143. m_filename_textbox->set_text(file_name);
  144. m_filename_textbox->set_focus(true);
  145. m_filename_textbox->select_all();
  146. }
  147. m_filename_textbox->on_return_pressed = [&] {
  148. on_file_return();
  149. };
  150. m_view->on_selection = [this](auto& index) {
  151. auto& filter_model = (GSortingProxyModel&)*m_view->model();
  152. auto local_index = filter_model.map_to_target(index);
  153. const GFileSystemModel::Node& node = m_model->node(local_index);
  154. FileSystemPath path { node.full_path(m_model) };
  155. clear_preview();
  156. if (!node.is_directory())
  157. m_filename_textbox->set_text(node.name);
  158. set_preview(path);
  159. };
  160. auto button_container = GWidget::construct(lower_container.ptr());
  161. button_container->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  162. button_container->set_preferred_size(0, 20);
  163. button_container->set_layout(make<GBoxLayout>(Orientation::Horizontal));
  164. button_container->layout()->set_spacing(4);
  165. button_container->layout()->add_spacer();
  166. auto cancel_button = GButton::construct(button_container);
  167. cancel_button->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  168. cancel_button->set_preferred_size(80, 0);
  169. cancel_button->set_text("Cancel");
  170. cancel_button->on_click = [this](auto&) {
  171. done(ExecCancel);
  172. };
  173. auto ok_button = GButton::construct(button_container);
  174. ok_button->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  175. ok_button->set_preferred_size(80, 0);
  176. ok_button->set_text(ok_button_name(m_mode));
  177. ok_button->on_click = [this](auto&) {
  178. on_file_return();
  179. };
  180. m_view->on_activation = [this](auto& index) {
  181. auto& filter_model = (GSortingProxyModel&)*m_view->model();
  182. auto local_index = filter_model.map_to_target(index);
  183. const GFileSystemModel::Node& node = m_model->node(local_index);
  184. auto path = node.full_path(m_model);
  185. if (node.is_directory()) {
  186. m_model->set_root_path(path);
  187. // NOTE: 'node' is invalid from here on
  188. } else {
  189. on_file_return();
  190. }
  191. };
  192. auto preview_container = GFrame::construct(horizontal_container);
  193. preview_container->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  194. preview_container->set_preferred_size(180, 0);
  195. preview_container->set_frame_shape(FrameShape::Container);
  196. preview_container->set_frame_shadow(FrameShadow::Sunken);
  197. preview_container->set_frame_thickness(2);
  198. preview_container->set_layout(make<GBoxLayout>(Orientation::Vertical));
  199. preview_container->layout()->set_margins({ 8, 8, 8, 8 });
  200. m_preview_image_label = GLabel::construct(preview_container);
  201. m_preview_image_label->set_should_stretch_icon(true);
  202. m_preview_image_label->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  203. m_preview_image_label->set_preferred_size(160, 160);
  204. m_preview_name_label = GLabel::construct(preview_container);
  205. m_preview_name_label->set_font(Font::default_bold_font());
  206. m_preview_name_label->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  207. m_preview_name_label->set_preferred_size(0, m_preview_name_label->font().glyph_height());
  208. m_preview_geometry_label = GLabel::construct(preview_container);
  209. m_preview_geometry_label->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  210. m_preview_geometry_label->set_preferred_size(0, m_preview_name_label->font().glyph_height());
  211. }
  212. GFilePicker::~GFilePicker()
  213. {
  214. }
  215. void GFilePicker::set_preview(const FileSystemPath& path)
  216. {
  217. if (path.has_extension(".png")) {
  218. auto bitmap = load_png(path.string());
  219. if (!bitmap) {
  220. clear_preview();
  221. return;
  222. }
  223. bool should_stretch = bitmap->width() > m_preview_image_label->width() || bitmap->height() > m_preview_image_label->height();
  224. m_preview_name_label->set_text(path.basename());
  225. m_preview_geometry_label->set_text(bitmap->size().to_string());
  226. m_preview_image_label->set_should_stretch_icon(should_stretch);
  227. m_preview_image_label->set_icon(move(bitmap));
  228. }
  229. }
  230. void GFilePicker::clear_preview()
  231. {
  232. m_preview_image_label->set_icon(nullptr);
  233. m_preview_name_label->set_text(String::empty());
  234. m_preview_geometry_label->set_text(String::empty());
  235. }
  236. void GFilePicker::on_file_return()
  237. {
  238. FileSystemPath path(String::format("%s/%s", m_model->root_path().characters(), m_filename_textbox->text().characters()));
  239. if (GFilePicker::file_exists(path.string()) && m_mode == Mode::Save) {
  240. auto result = GMessageBox::show("File already exists, overwrite?", "Existing File", GMessageBox::Type::Warning, GMessageBox::InputType::OKCancel);
  241. if (result == GMessageBox::ExecCancel)
  242. return;
  243. }
  244. m_selected_file = path;
  245. done(ExecOK);
  246. }
  247. bool GFilePicker::file_exists(const StringView& path)
  248. {
  249. struct stat st;
  250. int rc = stat(String(path).characters(), &st);
  251. if (rc < 0) {
  252. if (errno == ENOENT)
  253. return false;
  254. }
  255. if (rc == 0) {
  256. return true;
  257. }
  258. return false;
  259. }