main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 "QSWidget.h"
  27. #include <AK/URL.h>
  28. #include <LibCore/ArgsParser.h>
  29. #include <LibCore/MimeData.h>
  30. #include <LibDesktop/Launcher.h>
  31. #include <LibGUI/Action.h>
  32. #include <LibGUI/Application.h>
  33. #include <LibGUI/BoxLayout.h>
  34. #include <LibGUI/Clipboard.h>
  35. #include <LibGUI/Desktop.h>
  36. #include <LibGUI/FilePicker.h>
  37. #include <LibGUI/Label.h>
  38. #include <LibGUI/Menu.h>
  39. #include <LibGUI/MenuBar.h>
  40. #include <LibGUI/MessageBox.h>
  41. #include <LibGUI/ToolBar.h>
  42. #include <LibGUI/ToolBarContainer.h>
  43. #include <LibGUI/Window.h>
  44. #include <LibGfx/Bitmap.h>
  45. #include <LibGfx/Palette.h>
  46. #include <LibGfx/Rect.h>
  47. #include <serenity.h>
  48. #include <stdio.h>
  49. #include <string.h>
  50. int main(int argc, char** argv)
  51. {
  52. if (pledge("stdio recvfd sendfd accept rpath wpath cpath unix fattr thread", nullptr) < 0) {
  53. perror("pledge");
  54. return 1;
  55. }
  56. auto app = GUI::Application::construct(argc, argv);
  57. if (pledge("stdio recvfd sendfd accept cpath rpath wpath unix thread", nullptr) < 0) {
  58. perror("pledge");
  59. return 1;
  60. }
  61. if (!Desktop::Launcher::add_allowed_handler_with_any_url("/bin/QuickShow")) {
  62. warnln("Failed to set up allowed launch URLs");
  63. return 1;
  64. }
  65. if (!Desktop::Launcher::add_allowed_handler_with_only_specific_urls(
  66. "/bin/Help", { URL::create_with_file_protocol("/usr/share/man/man1/QuickShow.md") })) {
  67. warnln("Failed to set up allowed launch URLs");
  68. return 1;
  69. }
  70. if (!Desktop::Launcher::seal_allowlist()) {
  71. warnln("Failed to seal allowed launch URLs");
  72. return 1;
  73. }
  74. auto app_icon = GUI::Icon::default_icon("filetype-image");
  75. const char* path = nullptr;
  76. Core::ArgsParser args_parser;
  77. args_parser.add_positional_argument(path, "The image file to be displayed.", "file", Core::ArgsParser::Required::No);
  78. args_parser.parse(argc, argv);
  79. auto window = GUI::Window::construct();
  80. window->set_double_buffering_enabled(true);
  81. window->resize(300, 200);
  82. window->set_icon(app_icon.bitmap_for_size(16));
  83. window->set_title("QuickShow");
  84. auto& root_widget = window->set_main_widget<GUI::Widget>();
  85. root_widget.set_fill_with_background_color(true);
  86. root_widget.set_layout<GUI::VerticalBoxLayout>();
  87. root_widget.layout()->set_spacing(2);
  88. auto& toolbar_container = root_widget.add<GUI::ToolBarContainer>();
  89. auto& main_toolbar = toolbar_container.add<GUI::ToolBar>();
  90. auto& widget = root_widget.add<QSWidget>();
  91. widget.on_scale_change = [&](int scale, Gfx::IntRect rect) {
  92. if (!widget.bitmap()) {
  93. window->set_title("QuickShow");
  94. return;
  95. }
  96. window->set_title(String::formatted("{} {} {}% - QuickShow", widget.path(), widget.bitmap()->size().to_string(), scale));
  97. if (window->is_fullscreen())
  98. return;
  99. if (window->is_maximized())
  100. return;
  101. auto w = max(window->width(), rect.width() + 4);
  102. auto h = max(window->height(), rect.height() + widget.toolbar_height() + 6);
  103. window->resize(w, h);
  104. };
  105. widget.on_drop = [&](auto& event) {
  106. window->move_to_front();
  107. if (!event.mime_data().has_urls())
  108. return;
  109. auto urls = event.mime_data().urls();
  110. if (urls.is_empty())
  111. return;
  112. widget.load_from_file(urls.first().path());
  113. for (size_t i = 1; i < urls.size(); ++i) {
  114. Desktop::Launcher::open(URL::create_with_file_protocol(urls[i].path().characters()), "/bin/QuickShow");
  115. }
  116. };
  117. widget.on_doubleclick = [&] {
  118. window->set_fullscreen(!window->is_fullscreen());
  119. toolbar_container.set_visible(!window->is_fullscreen());
  120. };
  121. // Actions
  122. auto open_action = GUI::CommonActions::make_open_action(
  123. [&](auto&) {
  124. Optional<String> path = GUI::FilePicker::get_open_filepath(window, "Open image...");
  125. if (path.has_value()) {
  126. widget.load_from_file(path.value());
  127. }
  128. });
  129. auto delete_action = GUI::CommonActions::make_delete_action(
  130. [&](auto&) {
  131. auto path = widget.path();
  132. if (path.is_empty())
  133. return;
  134. auto msgbox_result = GUI::MessageBox::show(window,
  135. String::formatted("Really delete {}?", path),
  136. "Confirm deletion",
  137. GUI::MessageBox::Type::Warning,
  138. GUI::MessageBox::InputType::OKCancel);
  139. if (msgbox_result == GUI::MessageBox::ExecCancel)
  140. return;
  141. auto unlink_result = unlink(widget.path().characters());
  142. dbgln("unlink_result::{}", unlink_result);
  143. if (unlink_result < 0) {
  144. int saved_errno = errno;
  145. GUI::MessageBox::show(window,
  146. String::formatted("unlink({}) failed: {}", path, strerror(saved_errno)),
  147. "Delete failed",
  148. GUI::MessageBox::Type::Error);
  149. return;
  150. }
  151. widget.clear();
  152. });
  153. auto quit_action = GUI::CommonActions::make_quit_action(
  154. [&](auto&) {
  155. app->quit();
  156. });
  157. auto rotate_left_action = GUI::Action::create("Rotate Left", { Mod_None, Key_L },
  158. [&](auto&) {
  159. widget.rotate(Gfx::RotationDirection::Left);
  160. });
  161. auto rotate_right_action = GUI::Action::create("Rotate Right", { Mod_None, Key_R },
  162. [&](auto&) {
  163. widget.rotate(Gfx::RotationDirection::Right);
  164. });
  165. auto vertical_flip_action = GUI::Action::create("Vertical Flip", { Mod_None, Key_V },
  166. [&](auto&) {
  167. widget.flip(Gfx::Orientation::Vertical);
  168. });
  169. auto horizontal_flip_action = GUI::Action::create("Horizontal Flip", { Mod_None, Key_H },
  170. [&](auto&) {
  171. widget.flip(Gfx::Orientation::Horizontal);
  172. });
  173. auto desktop_wallpaper_action = GUI::Action::create("Set as desktop wallpaper",
  174. [&](auto&) {
  175. GUI::Desktop::the().set_wallpaper(widget.path());
  176. });
  177. auto go_first_action = GUI::Action::create("First", { Mod_None, Key_Home }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-first.png"),
  178. [&](auto&) {
  179. widget.navigate(QSWidget::Directions::First);
  180. });
  181. auto go_back_action = GUI::Action::create("Back", { Mod_None, Key_Left }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-back.png"),
  182. [&](auto&) {
  183. widget.navigate(QSWidget::Directions::Back);
  184. });
  185. auto go_forward_action = GUI::Action::create("Forward", { Mod_None, Key_Right }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png"),
  186. [&](auto&) {
  187. widget.navigate(QSWidget::Directions::Forward);
  188. });
  189. auto go_last_action = GUI::Action::create("Last", { Mod_None, Key_End }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-last.png"),
  190. [&](auto&) {
  191. widget.navigate(QSWidget::Directions::Last);
  192. });
  193. auto full_sceen_action = GUI::CommonActions::make_fullscreen_action(
  194. [&](auto&) {
  195. widget.on_doubleclick();
  196. });
  197. auto zoom_in_action = GUI::Action::create("Zoom In", { Mod_None, Key_Plus }, Gfx::Bitmap::load_from_file("/res/icons/16x16/zoom-in.png"),
  198. [&](auto&) {
  199. widget.set_scale(widget.scale() + 10);
  200. });
  201. auto zoom_reset_action = GUI::Action::create("Zoom 100%", { Mod_None, Key_0 }, Gfx::Bitmap::load_from_file("/res/icons/16x16/zoom-reset.png"),
  202. [&](auto&) {
  203. widget.set_scale(100);
  204. });
  205. auto zoom_out_action = GUI::Action::create("Zoom Out", { Mod_None, Key_Minus }, Gfx::Bitmap::load_from_file("/res/icons/16x16/zoom-out.png"),
  206. [&](auto&) {
  207. widget.set_scale(widget.scale() - 10);
  208. });
  209. auto hide_show_toolbar_action = GUI::Action::create("Hide/Show Toolbar", { Mod_Ctrl, Key_T },
  210. [&](auto&) {
  211. toolbar_container.set_visible(!toolbar_container.is_visible());
  212. });
  213. auto copy_action = GUI::CommonActions::make_copy_action([&](auto&) {
  214. if (widget.bitmap())
  215. GUI::Clipboard::the().set_bitmap(*widget.bitmap());
  216. });
  217. main_toolbar.add_action(open_action);
  218. main_toolbar.add_action(delete_action);
  219. main_toolbar.add_separator();
  220. main_toolbar.add_action(go_first_action);
  221. main_toolbar.add_action(go_back_action);
  222. main_toolbar.add_action(go_forward_action);
  223. main_toolbar.add_action(go_last_action);
  224. main_toolbar.add_separator();
  225. main_toolbar.add_action(zoom_in_action);
  226. main_toolbar.add_action(zoom_reset_action);
  227. main_toolbar.add_action(zoom_out_action);
  228. auto menubar = GUI::MenuBar::construct();
  229. auto& app_menu = menubar->add_menu("QuickShow");
  230. app_menu.add_action(open_action);
  231. app_menu.add_action(delete_action);
  232. app_menu.add_separator();
  233. app_menu.add_action(quit_action);
  234. auto& image_menu = menubar->add_menu("Image");
  235. image_menu.add_action(rotate_left_action);
  236. image_menu.add_action(rotate_right_action);
  237. image_menu.add_action(vertical_flip_action);
  238. image_menu.add_action(horizontal_flip_action);
  239. image_menu.add_separator();
  240. image_menu.add_action(desktop_wallpaper_action);
  241. auto& navigate_menu = menubar->add_menu("Navigate");
  242. navigate_menu.add_action(go_first_action);
  243. navigate_menu.add_action(go_back_action);
  244. navigate_menu.add_action(go_forward_action);
  245. navigate_menu.add_action(go_last_action);
  246. auto& view_menu = menubar->add_menu("View");
  247. view_menu.add_action(full_sceen_action);
  248. view_menu.add_separator();
  249. view_menu.add_action(zoom_in_action);
  250. view_menu.add_action(zoom_reset_action);
  251. view_menu.add_action(zoom_out_action);
  252. view_menu.add_separator();
  253. view_menu.add_action(hide_show_toolbar_action);
  254. auto& help_menu = menubar->add_menu("Help");
  255. help_menu.add_action(GUI::CommonActions::make_help_action([](auto&) {
  256. Desktop::Launcher::open(URL::create_with_file_protocol("/usr/share/man/man1/QuickShow.md"), "/bin/Help");
  257. }));
  258. help_menu.add_action(GUI::CommonActions::make_about_action("QuickShow", app_icon, window));
  259. app->set_menubar(move(menubar));
  260. if (path != nullptr) {
  261. widget.load_from_file(path);
  262. }
  263. window->show();
  264. return app->exec();
  265. }