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