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 <AK/StringBuilder.h>
32#include <LibGUI/Action.h>
33#include <LibGUI/BoxLayout.h>
34#include <LibGUI/Menu.h>
35#include <LibGUI/InputBox.h>
36#include <LibGUI/Notification.h>
37#include <LibGUI/Splitter.h>
38#include <LibGUI/TableView.h>
39#include <LibGUI/TextBox.h>
40#include <LibGUI/TextEditor.h>
41#include <LibGUI/Window.h>
42#include <LibWeb/HtmlView.h>
43
44IRCWindow::IRCWindow(IRCClient& client, void* owner, Type type, const String& name)
45 : m_client(client)
46 , m_owner(owner)
47 , m_type(type)
48 , m_name(name)
49{
50 set_layout<GUI::VerticalBoxLayout>();
51
52 // Make a container for the log buffer view + (optional) member list.
53 auto& container = add<GUI::HorizontalSplitter>();
54
55 m_html_view = container.add<Web::HtmlView>();
56
57 if (m_type == Channel) {
58 auto& member_view = container.add<GUI::TableView>();
59 member_view.set_headers_visible(false);
60 member_view.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
61 member_view.set_preferred_size(100, 0);
62 member_view.set_alternating_row_colors(false);
63 member_view.set_model(channel().member_model());
64 member_view.set_activates_on_selection(true);
65 member_view.on_activation = [&](auto& index) {
66 if (!index.is_valid())
67 return;
68 auto nick = channel().member_model()->nick_at(member_view.selection().first());
69 if (nick.is_empty())
70 return;
71 m_client.handle_open_query_action(m_client.nick_without_prefix(nick.characters()));
72 };
73 member_view.on_context_menu_request = [&](const GUI::ModelIndex& index, const GUI::ContextMenuEvent& event) {
74 if (!index.is_valid())
75 return;
76
77 m_context_menu = GUI::Menu::construct();
78
79 m_context_menu->add_action(GUI::Action::create("Open query", Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-open-query.png"), [&](const GUI::Action&) {
80 auto nick = channel().member_model()->nick_at(member_view.selection().first());
81 if (nick.is_empty())
82 return;
83 m_client.handle_open_query_action(m_client.nick_without_prefix(nick.characters()));
84 }));
85
86 m_context_menu->add_action(GUI::Action::create("Whois", Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-whois.png"), [&](const GUI::Action&) {
87 auto nick = channel().member_model()->nick_at(member_view.selection().first());
88 if (nick.is_empty())
89 return;
90 m_client.handle_whois_action(m_client.nick_without_prefix(nick.characters()));
91 }));
92
93 m_context_menu->add_separator();
94
95 m_context_menu->add_action(GUI::Action::create("Voice", [&](const GUI::Action&) {
96 auto nick = channel().member_model()->nick_at(member_view.selection().first());
97 if (nick.is_empty())
98 return;
99 auto input_box = GUI::InputBox::construct("Enter reason:", "Reason");
100 m_client.handle_voice_user_action(m_name.characters(), m_client.nick_without_prefix(nick.characters()));
101 }));
102
103 m_context_menu->add_action(GUI::Action::create("DeVoice", [&](const GUI::Action&) {
104 auto nick = channel().member_model()->nick_at(member_view.selection().first());
105 if (nick.is_empty())
106 return;
107 m_client.handle_devoice_user_action(m_name.characters(), m_client.nick_without_prefix(nick.characters()));
108 }));
109
110 m_context_menu->add_action(GUI::Action::create("Hop", [&](const GUI::Action&) {
111 auto nick = channel().member_model()->nick_at(member_view.selection().first());
112 if (nick.is_empty())
113 return;
114 m_client.handle_hop_user_action(m_name.characters(), m_client.nick_without_prefix(nick.characters()));
115 }));
116
117 m_context_menu->add_action(GUI::Action::create("DeHop", [&](const GUI::Action&) {
118 auto nick = channel().member_model()->nick_at(member_view.selection().first());
119 if (nick.is_empty())
120 return;
121 m_client.handle_dehop_user_action(m_name.characters(), m_client.nick_without_prefix(nick.characters()));
122 }));
123
124 m_context_menu->add_action(GUI::Action::create("Op", [&](const GUI::Action&) {
125 auto nick = channel().member_model()->nick_at(member_view.selection().first());
126 if (nick.is_empty())
127 return;
128 m_client.handle_op_user_action(m_name.characters(), m_client.nick_without_prefix(nick.characters()));
129 }));
130
131 m_context_menu->add_action(GUI::Action::create("DeOp", [&](const GUI::Action&) {
132 auto nick = channel().member_model()->nick_at(member_view.selection().first());
133 if (nick.is_empty())
134 return;
135 m_client.handle_deop_user_action(m_name.characters(), m_client.nick_without_prefix(nick.characters()));
136 }));
137
138 m_context_menu->add_separator();
139
140 m_context_menu->add_action(GUI::Action::create("Kick", [&](const GUI::Action&) {
141 auto nick = channel().member_model()->nick_at(member_view.selection().first());
142 if (nick.is_empty())
143 return;
144 if (IRCClient::is_nick_prefix(nick[0]))
145 nick = nick.substring(1, nick.length() - 1);
146 auto input_box = GUI::InputBox::construct("Enter reason:", "Reason");
147 if (input_box->exec() == GUI::InputBox::ExecOK)
148 m_client.handle_kick_user_action(m_name.characters(), m_client.nick_without_prefix(nick.characters()), input_box->text_value());
149 }));
150
151 m_context_menu->popup(event.screen_position());
152 };
153 }
154
155 m_text_editor = add<GUI::TextBox>();
156 m_text_editor->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
157 m_text_editor->set_preferred_size(0, 19);
158 m_text_editor->on_return_pressed = [this] {
159 if (m_type == Channel)
160 m_client.handle_user_input_in_channel(m_name, m_text_editor->text());
161 else if (m_type == Query)
162 m_client.handle_user_input_in_query(m_name, m_text_editor->text());
163 else if (m_type == Server)
164 m_client.handle_user_input_in_server(m_text_editor->text());
165 m_text_editor->clear();
166 };
167
168 m_client.register_subwindow(*this);
169}
170
171IRCWindow::~IRCWindow()
172{
173 m_client.unregister_subwindow(*this);
174}
175
176void IRCWindow::set_log_buffer(const IRCLogBuffer& log_buffer)
177{
178 m_log_buffer = &log_buffer;
179 m_html_view->set_document(const_cast<Web::Document*>(&log_buffer.document()));
180}
181
182bool IRCWindow::is_active() const
183{
184 return m_client.current_window() == this;
185}
186
187void IRCWindow::post_notification_if_needed(const String& name, const String& message)
188{
189 if (name.is_null() || message.is_null())
190 return;
191 if (is_active() && window()->is_active())
192 return;
193
194 auto notification = GUI::Notification::construct();
195
196 if (type() == Type::Channel) {
197
198 if (!message.contains(m_client.nickname()))
199 return;
200
201 StringBuilder builder;
202 builder.append(name);
203 builder.append(" in ");
204 builder.append(m_name);
205 notification->set_title(builder.to_string());
206 } else {
207 notification->set_title(name);
208 }
209
210 notification->set_icon(Gfx::Bitmap::load_from_file("/res/icons/32x32/app-irc-client.png"));
211 notification->set_text(message);
212 notification->show();
213}
214
215void IRCWindow::did_add_message(const String& name, const String& message)
216{
217 post_notification_if_needed(name, message);
218
219 if (!is_active()) {
220 ++m_unread_count;
221 m_client.aid_update_window_list();
222 return;
223 }
224 m_html_view->scroll_to_bottom();
225}
226
227void IRCWindow::clear_unread_count()
228{
229 if (!m_unread_count)
230 return;
231 m_unread_count = 0;
232 m_client.aid_update_window_list();
233}
234
235int IRCWindow::unread_count() const
236{
237 return m_unread_count;
238}