ladybird/Applications/IRCClient/IRCAppWindow.cpp
Andreas Kling 2580d4b911 IRCClient: Crash if server name resolution fails.
This is just so that I can catch it immediately. It obviously needs a bit of
work to feel right.
2019-04-02 20:42:59 +02:00

170 lines
6.0 KiB
C++

#include "IRCAppWindow.h"
#include "IRCWindow.h"
#include "IRCWindowListModel.h"
#include <LibGUI/GApplication.h>
#include <LibGUI/GStackWidget.h>
#include <LibGUI/GTableView.h>
#include <LibGUI/GBoxLayout.h>
#include <LibGUI/GToolBar.h>
#include <LibGUI/GAction.h>
#include <LibGUI/GMenu.h>
#include <LibGUI/GMenuBar.h>
#include <LibGUI/GInputBox.h>
#include <LibGUI/GSplitter.h>
#include <stdio.h>
#include <stdlib.h>
IRCAppWindow::IRCAppWindow()
{
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_connect = [this] {
m_client.join_channel("#test");
};
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"), [] (auto&) {
printf("FIXME: Implement part action\n");
});
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(GAction::create("Quit", { Mod_Alt, Key_F4 }, [] (const GAction&) {
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", [] (const GAction&) {
dbgprintf("FIXME: Implement Help/About\n");
}));
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_layout(make<GBoxLayout>(Orientation::Vertical));
auto* toolbar = new GToolBar(widget);
toolbar->add_action(*m_change_nick_action);
toolbar->add_separator();
toolbar->add_action(*m_join_action);
toolbar->add_action(*m_part_action.copy_ref());
toolbar->add_separator();
toolbar->add_action(*m_whois_action);
toolbar->add_action(*m_open_query_action);
toolbar->add_action(*m_close_query_action);
auto* horizontal_container = new GSplitter(Orientation::Horizontal, widget);
m_window_list = new GTableView(horizontal_container);
m_window_list->set_headers_visible(false);
m_window_list->set_alternating_row_colors(false);
m_window_list->set_model(m_client.client_window_list_model());
m_window_list->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
m_window_list->set_preferred_size({ 100, 0 });
m_client.client_window_list_model()->on_activation = [this] (IRCWindow& window) {
m_container->set_active_widget(&window);
window.clear_unread_count();
};
m_container = new GStackWidget(horizontal_container);
create_window(&m_client, IRCWindow::Server, "Server");
}
IRCWindow& IRCAppWindow::create_window(void* owner, IRCWindow::Type type, const String& name)
{
return *new IRCWindow(m_client, owner, type, name, m_container);
}