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 "BoardListModel.h"
28#include "ThreadCatalogModel.h"
29#include <LibGUI/AboutDialog.h>
30#include <LibGUI/Action.h>
31#include <LibGUI/Application.h>
32#include <LibGUI/BoxLayout.h>
33#include <LibGUI/ComboBox.h>
34#include <LibGUI/Menu.h>
35#include <LibGUI/MenuBar.h>
36#include <LibGUI/StatusBar.h>
37#include <LibGUI/TableView.h>
38#include <LibGUI/Window.h>
39#include <stdio.h>
40
41int main(int argc, char** argv)
42{
43#ifdef __serenity__
44 if (pledge("stdio dns inet shared_buffer rpath cpath fattr", nullptr) < 0) {
45 perror("pledge");
46 return 1;
47 }
48#endif
49
50 GUI::Application app(argc, argv);
51
52#ifdef __serenity__
53 if (pledge("stdio dns inet shared_buffer rpath", nullptr) < 0) {
54 perror("pledge");
55 return 1;
56 }
57#endif
58
59 auto window = GUI::Window::construct();
60 window->set_title("ChanViewer");
61 window->set_rect(100, 100, 800, 500);
62 window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-chanviewer.png"));
63
64 auto& widget = window->set_main_widget<GUI::Widget>();
65 widget.set_fill_with_background_color(true);
66 widget.set_layout<GUI::VerticalBoxLayout>();
67
68 auto& board_combo = widget.add<GUI::ComboBox>();
69 board_combo.set_only_allow_values_from_model(true);
70 board_combo.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
71 board_combo.set_preferred_size(0, 20);
72 board_combo.set_model(BoardListModel::create());
73
74 auto& catalog_view = widget.add<GUI::TableView>();
75 catalog_view.set_model(ThreadCatalogModel::create());
76 auto& catalog_model = *static_cast<ThreadCatalogModel*>(catalog_view.model());
77
78 auto& statusbar = widget.add<GUI::StatusBar>();
79
80 board_combo.on_change = [&] (auto&, const GUI::ModelIndex& index) {
81 auto selected_board = board_combo.model()->data(index, GUI::Model::Role::Custom);
82 ASSERT(selected_board.is_string());
83 catalog_model.set_board(selected_board.to_string());
84 };
85
86 catalog_model.on_load_started = [&] {
87 statusbar.set_text(String::format("Loading /%s/...", catalog_model.board().characters()));
88 };
89
90 catalog_model.on_load_finished = [&](bool success) {
91 statusbar.set_text(success ? "Load finished" : "Load failed");
92 if (success) {
93 window->set_title(String::format("/%s/ - ChanViewer", catalog_model.board().characters()));
94 }
95 };
96
97 window->show();
98
99 auto menubar = make<GUI::MenuBar>();
100
101 auto& app_menu = menubar->add_menu("ChanViewer");
102 app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
103 GUI::Application::the().quit(0);
104 return;
105 }));
106
107 auto& help_menu = menubar->add_menu("Help");
108 help_menu.add_action(GUI::Action::create("About", [&](const GUI::Action&) {
109 GUI::AboutDialog::show("ChanViewer", Gfx::Bitmap::load_from_file("/res/icons/32x32/app-chanviewer.png"), window);
110 }));
111
112 app.set_menubar(move(menubar));
113
114 return app.exec();
115}