summaryrefslogtreecommitdiff
path: root/Applications/IRCClient/IRCWindow.cpp
blob: 627d03c57847d33efb3a78ea5788d6285cfa3e01 (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
#include "IRCWindow.h"
#include "IRCClient.h"
#include "IRCChannel.h"
#include "IRCChannelMemberListModel.h"
#include "IRCLogBufferModel.h"
#include <LibGUI/GBoxLayout.h>
#include <LibGUI/GTableView.h>
#include <LibGUI/GTextEditor.h>
#include <LibGUI/GTextBox.h>

IRCWindow::IRCWindow(IRCClient& client, void* owner, Type type, const String& name, GWidget* parent)
    : GWidget(parent)
    , m_client(client)
    , m_owner(owner)
    , m_type(type)
    , m_name(name)
{
    set_layout(make<GBoxLayout>(Orientation::Vertical));

    // Make a container for the log buffer view + (optional) member list.
    GWidget* container = new GWidget(this);
    container->set_layout(make<GBoxLayout>(Orientation::Horizontal));

    m_table_view = new GTableView(container);
    m_table_view->set_headers_visible(false);
    m_table_view->set_font(Font::default_fixed_width_font());
    m_table_view->set_alternating_row_colors(false);

    if (m_type == Channel) {
        auto* member_view = new GTableView(container);
        member_view->set_headers_visible(false);
        member_view->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
        member_view->set_preferred_size({ 100, 0 });
        member_view->set_alternating_row_colors(false);
        member_view->set_model(channel().member_model());
    }

    m_text_editor = new GTextEditor(GTextEditor::SingleLine, this);
    m_text_editor->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
    m_text_editor->set_preferred_size({ 0, 15 });
    m_text_editor->on_return_pressed = [this] (GTextEditor& editor) {
        if (m_type == Channel)
            m_client.handle_user_input_in_channel(m_name, editor.text());
        else if (m_type == Query)
            m_client.handle_user_input_in_query(m_name, editor.text());
        else if (m_type == Server)
            m_client.handle_user_input_in_server(editor.text());
        m_text_editor->clear();
    };

    m_client.register_subwindow(*this);
}

IRCWindow::~IRCWindow()
{
    m_client.unregister_subwindow(*this);
}

void IRCWindow::set_log_buffer(const IRCLogBuffer& log_buffer)
{
    m_log_buffer = &log_buffer;
    m_table_view->set_model(log_buffer.model());
}

bool IRCWindow::is_active() const
{
    return m_client.current_window() == this;
}

void IRCWindow::did_add_message()
{
    if (!is_active()) {
        ++m_unread_count;
        m_client.aid_update_window_list();
        return;
    }
    m_table_view->scroll_to_bottom();
}

void IRCWindow::clear_unread_count()
{
    if (!m_unread_count)
        return;
    m_unread_count = 0;
    m_client.aid_update_window_list();
}

int IRCWindow::unread_count() const
{
    return m_unread_count;
}