Serenity Operating System
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 "QSWidget.h"
28#include <AK/URL.h>
29#include <LibCore/MimeData.h>
30#include <LibGUI/AboutDialog.h>
31#include <LibGUI/Action.h>
32#include <LibGUI/Application.h>
33#include <LibGUI/BoxLayout.h>
34#include <LibGUI/FilePicker.h>
35#include <LibGUI/Label.h>
36#include <LibGUI/Menu.h>
37#include <LibGUI/MenuBar.h>
38#include <LibGUI/Window.h>
39#include <LibGfx/Bitmap.h>
40#include <stdio.h>
41
42int main(int argc, char** argv)
43{
44#ifdef __serenity__
45 if (pledge("stdio shared_buffer accept rpath unix cpath fattr proc exec thread", nullptr) < 0) {
46 perror("pledge");
47 return 1;
48 }
49#endif
50
51 GUI::Application app(argc, argv);
52
53#ifdef __serenity__
54 if (pledge("stdio shared_buffer accept rpath proc exec thread", nullptr) < 0) {
55 perror("pledge");
56 return 1;
57 }
58#endif
59
60#if 0
61 if (argc != 2) {
62 printf("usage: qs <image-file>\n");
63 return 0;
64 }
65#endif
66
67 auto window = GUI::Window::construct();
68 window->set_double_buffering_enabled(true);
69 window->set_rect(200, 200, 300, 200);
70 window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-image.png"));
71
72 auto& widget = window->set_main_widget<QSWidget>();
73 if (argc > 1)
74 widget.load_from_file(argv[1]);
75
76 auto update_window_title = [&](int scale) {
77 if (widget.bitmap())
78 window->set_title(String::format("%s %s %d%% - QuickShow", widget.path().characters(), widget.bitmap()->size().to_string().characters(), scale));
79 else
80 window->set_title("QuickShow");
81 };
82
83 update_window_title(100);
84
85 widget.on_scale_change = [&](int scale) {
86 update_window_title(scale);
87 };
88
89 widget.on_drop = [&](auto& event) {
90 window->move_to_front();
91
92 if (event.mime_data().has_urls()) {
93 auto urls = event.mime_data().urls();
94
95 if (!urls.is_empty()) {
96 auto url = urls.first();
97 widget.load_from_file(url.path());
98 }
99
100 for (size_t i = 1; i < urls.size(); ++i) {
101 if (fork() == 0) {
102 execl("/bin/QuickShow", "/bin/QuickShow", urls[i].path().characters(), nullptr);
103 ASSERT_NOT_REACHED();
104 }
105 }
106 }
107 };
108
109 auto menubar = make<GUI::MenuBar>();
110
111 auto& app_menu = menubar->add_menu("QuickShow");
112 app_menu.add_action(GUI::CommonActions::make_open_action([&](auto&) {
113 Optional<String> path = GUI::FilePicker::get_open_filepath("Open image...");
114 if (path.has_value()) {
115 widget.load_from_file(path.value());
116 }
117 }));
118 app_menu.add_separator();
119 app_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) {
120 app.quit();
121 }));
122
123 auto& help_menu = menubar->add_menu("Help");
124 help_menu.add_action(GUI::Action::create("About", [&](auto&) {
125 GUI::AboutDialog::show("QuickShow", Gfx::Bitmap::load_from_file("/res/icons/32x32/filetype-image.png"), window);
126 }));
127
128 app.set_menubar(move(menubar));
129
130 window->show();
131
132 return app.exec();
133}