IRCWindow.cpp 12 KB

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