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 "IRCWindow.h"
28#include "IRCChannel.h"
29#include "IRCChannelMemberListModel.h"
30#include "IRCClient.h"
31#include <LibGUI/BoxLayout.h>
32#include <LibGUI/Splitter.h>
33#include <LibGUI/TableView.h>
34#include <LibGUI/TextBox.h>
35#include <LibGUI/TextEditor.h>
36#include <LibHTML/HtmlView.h>
37
38IRCWindow::IRCWindow(IRCClient& client, void* owner, Type type, const String& name)
39 : m_client(client)
40 , m_owner(owner)
41 , m_type(type)
42 , m_name(name)
43{
44 set_layout(make<GUI::VerticalBoxLayout>());
45
46 // Make a container for the log buffer view + (optional) member list.
47 auto container = add<GUI::HorizontalSplitter>();
48
49 m_html_view = container->add<HtmlView>();
50
51 if (m_type == Channel) {
52 auto member_view = container->add<GUI::TableView>();
53 member_view->set_headers_visible(false);
54 member_view->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
55 member_view->set_preferred_size(100, 0);
56 member_view->set_alternating_row_colors(false);
57 member_view->set_model(channel().member_model());
58 member_view->set_activates_on_selection(true);
59 }
60
61 m_text_editor = add<GUI::TextBox>();
62 m_text_editor->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
63 m_text_editor->set_preferred_size(0, 19);
64 m_text_editor->on_return_pressed = [this] {
65 if (m_type == Channel)
66 m_client.handle_user_input_in_channel(m_name, m_text_editor->text());
67 else if (m_type == Query)
68 m_client.handle_user_input_in_query(m_name, m_text_editor->text());
69 else if (m_type == Server)
70 m_client.handle_user_input_in_server(m_text_editor->text());
71 m_text_editor->clear();
72 };
73
74 m_client.register_subwindow(*this);
75}
76
77IRCWindow::~IRCWindow()
78{
79 m_client.unregister_subwindow(*this);
80}
81
82void IRCWindow::set_log_buffer(const IRCLogBuffer& log_buffer)
83{
84 m_log_buffer = &log_buffer;
85 m_html_view->set_document(const_cast<Document*>(&log_buffer.document()));
86}
87
88bool IRCWindow::is_active() const
89{
90 return m_client.current_window() == this;
91}
92
93void IRCWindow::did_add_message()
94{
95 if (!is_active()) {
96 ++m_unread_count;
97 m_client.aid_update_window_list();
98 return;
99 }
100 m_html_view->scroll_to_bottom();
101}
102
103void IRCWindow::clear_unread_count()
104{
105 if (!m_unread_count)
106 return;
107 m_unread_count = 0;
108 m_client.aid_update_window_list();
109}
110
111int IRCWindow::unread_count() const
112{
113 return m_unread_count;
114}