summaryrefslogtreecommitdiff
path: root/Applications/IRCClient/IRCAppWindow.cpp
blob: faec839ccdcb4fddbdb75ae4a4d99af8ff146b06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "IRCAppWindow.h"
#include "IRCChannel.h"
#include "IRCWindow.h"
#include "IRCWindowListModel.h"
#include <LibDraw/PNGLoader.h>
#include <LibGUI/GAboutDialog.h>
#include <LibGUI/GAction.h>
#include <LibGUI/GApplication.h>
#include <LibGUI/GBoxLayout.h>
#include <LibGUI/GInputBox.h>
#include <LibGUI/GMenu.h>
#include <LibGUI/GMenuBar.h>
#include <LibGUI/GSplitter.h>
#include <LibGUI/GStackWidget.h>
#include <LibGUI/GTableView.h>
#include <LibGUI/GToolBar.h>
#include <stdio.h>
#include <stdlib.h>

static IRCAppWindow* s_the;

IRCAppWindow& IRCAppWindow::the()
{
    return *s_the;
}

IRCAppWindow::IRCAppWindow()
{
    ASSERT(!s_the);
    s_the = this;

    set_icon(load_png("/res/icons/16x16/app-irc-client.png"));

    update_title();
    set_rect(200, 200, 600, 400);
    setup_actions();
    setup_menus();
    setup_widgets();

    setup_client();
}

IRCAppWindow::~IRCAppWindow()
{
}

void IRCAppWindow::update_title()
{
    set_title(String::format("IRC Client: %s@%s:%d", m_client.nickname().characters(), m_client.hostname().characters(), m_client.port()));
}

void IRCAppWindow::setup_client()
{
    m_client.aid_create_window = [this](void* owner, IRCWindow::Type type, const String& name) {
        return &create_window(owner, type, name);
    };
    m_client.aid_get_active_window = [this] {
        return static_cast<IRCWindow*>(m_container->active_widget());
    };
    m_client.aid_update_window_list = [this] {
        m_window_list->model()->update();
    };
    m_client.on_nickname_changed = [this](const String&) {
        update_title();
    };
    m_client.on_part_from_channel = [this](auto&) {
        update_part_action();
    };

    if (m_client.hostname().is_empty()) {
        GInputBox input_box("Enter server:", "Connect to server", this);
        auto result = input_box.exec();
        if (result == GInputBox::ExecCancel)
            ::exit(0);

        m_client.set_server(input_box.text_value(), 6667);
    }
    update_title();
    bool success = m_client.connect();
    ASSERT(success);
}

void IRCAppWindow::setup_actions()
{
    m_join_action = GAction::create("Join channel", GraphicsBitmap::load_from_file("/res/icons/16x16/irc-join.png"), [&](auto&) {
        GInputBox input_box("Enter channel name:", "Join channel", this);
        if (input_box.exec() == GInputBox::ExecOK && !input_box.text_value().is_empty())
            m_client.handle_join_action(input_box.text_value());
    });

    m_part_action = GAction::create("Part from channel", GraphicsBitmap::load_from_file("/res/icons/16x16/irc-part.png"), [this](auto&) {
        auto* window = m_client.current_window();
        if (!window || window->type() != IRCWindow::Type::Channel) {
            // FIXME: Perhaps this action should have been disabled instead of allowing us to activate it.
            return;
        }
        m_client.handle_part_action(window->channel().name());
    });

    m_whois_action = GAction::create("Whois user", GraphicsBitmap::load_from_file("/res/icons/16x16/irc-whois.png"), [&](auto&) {
        GInputBox input_box("Enter nickname:", "IRC WHOIS lookup", this);
        if (input_box.exec() == GInputBox::ExecOK && !input_box.text_value().is_empty())
            m_client.handle_whois_action(input_box.text_value());
    });

    m_open_query_action = GAction::create("Open query", GraphicsBitmap::load_from_file("/res/icons/16x16/irc-open-query.png"), [&](auto&) {
        GInputBox input_box("Enter nickname:", "Open IRC query with...", this);
        if (input_box.exec() == GInputBox::ExecOK && !input_box.text_value().is_empty())
            m_client.handle_open_query_action(input_box.text_value());
    });

    m_close_query_action = GAction::create("Close query", GraphicsBitmap::load_from_file("/res/icons/16x16/irc-close-query.png"), [](auto&) {
        printf("FIXME: Implement close-query action\n");
    });

    m_change_nick_action = GAction::create("Change nickname", GraphicsBitmap::load_from_file("/res/icons/16x16/irc-nick.png"), [this](auto&) {
        GInputBox input_box("Enter nickname:", "Change nickname", this);
        if (input_box.exec() == GInputBox::ExecOK && !input_box.text_value().is_empty())
            m_client.handle_change_nick_action(input_box.text_value());
    });
}

void IRCAppWindow::setup_menus()
{
    auto menubar = make<GMenuBar>();
    auto app_menu = make<GMenu>("IRC Client");
    app_menu->add_action(GCommonActions::make_quit_action([](auto&) {
        dbgprintf("Terminal: Quit menu activated!\n");
        GApplication::the().quit(0);
        return;
    }));
    menubar->add_menu(move(app_menu));

    auto server_menu = make<GMenu>("Server");
    server_menu->add_action(*m_change_nick_action);
    server_menu->add_separator();
    server_menu->add_action(*m_join_action);
    server_menu->add_action(*m_part_action);
    server_menu->add_separator();
    server_menu->add_action(*m_whois_action);
    server_menu->add_action(*m_open_query_action);
    server_menu->add_action(*m_close_query_action);
    menubar->add_menu(move(server_menu));

    auto help_menu = make<GMenu>("Help");
    help_menu->add_action(GAction::create("About", [this](const GAction&) {
        GAboutDialog::show("IRC Client", load_png("/res/icons/32x32/app-irc-client.png"), this);
    }));
    menubar->add_menu(move(help_menu));

    GApplication::the().set_menubar(move(menubar));
}

void IRCAppWindow::setup_widgets()
{
    auto* widget = new GWidget(nullptr);
    set_main_widget(widget);
    widget->set_fill_with_background_color(true);
    widget->set_background_color(Color::WarmGray);
    widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
    widget->layout()->set_spacing(0);

    auto* toolbar = new GToolBar(widget);
    toolbar->set_has_frame(false);
    toolbar->add_action(*m_change_nick_action);
    toolbar->add_separator();
    toolbar->add_action(*m_join_action);
    toolbar->add_action(*m_part_action);
    toolbar->add_separator();
    toolbar->add_action(*m_whois_action);
    toolbar->add_action(*m_open_query_action);
    toolbar->add_action(*m_close_query_action);

    auto* outer_container = new GWidget(widget);
    outer_container->set_layout(make<GBoxLayout>(Orientation::Vertical));
    outer_container->layout()->set_margins({ 2, 0, 2, 2 });

    auto horizontal_container = GSplitter::construct(Orientation::Horizontal, outer_container);

    m_window_list = GTableView::construct(horizontal_container);
    m_window_list->set_headers_visible(false);
    m_window_list->set_alternating_row_colors(false);
    m_window_list->set_size_columns_to_fit_content(true);
    m_window_list->set_model(m_client.client_window_list_model());
    m_window_list->set_activates_on_selection(true);
    m_window_list->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
    m_window_list->set_preferred_size(100, 0);
    m_window_list->on_activation = [this](auto& index) {
        set_active_window(m_client.window_at(index.row()));
    };

    m_container = new GStackWidget(horizontal_container);
    m_container->on_active_widget_change = [this](auto*) {
        update_part_action();
    };

    create_window(&m_client, IRCWindow::Server, "Server");
}

void IRCAppWindow::set_active_window(IRCWindow& window)
{
    m_container->set_active_widget(&window);
    window.clear_unread_count();
    auto index = m_window_list->model()->index(m_client.window_index(window));
    m_window_list->selection().set(index);
}

void IRCAppWindow::update_part_action()
{
    auto* window = static_cast<IRCWindow*>(m_container->active_widget());
    bool is_open_channel = window && window->type() == IRCWindow::Type::Channel && window->channel().is_open();
    m_part_action->set_enabled(is_open_channel);
}

IRCWindow& IRCAppWindow::create_window(void* owner, IRCWindow::Type type, const String& name)
{
    return *new IRCWindow(m_client, owner, type, name, m_container);
}