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/InputBox.h>
  34. #include <LibGUI/Menu.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/InProcessWebView.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_page_view = container.add<Web::InProcessWebView>();
  52. if (m_type == Channel) {
  53. auto& member_view = container.add<GUI::TableView>();
  54. member_view.set_column_headers_visible(false);
  55. member_view.set_fixed_width(100);
  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. member_view.on_activation = [&](auto& index) {
  60. if (!index.is_valid())
  61. return;
  62. auto nick = channel().member_model()->nick_at(member_view.selection().first());
  63. if (nick.is_empty())
  64. return;
  65. m_client->handle_open_query_action(m_client->nick_without_prefix(nick.characters()));
  66. };
  67. member_view.on_context_menu_request = [&](const GUI::ModelIndex& index, const GUI::ContextMenuEvent& event) {
  68. if (!index.is_valid())
  69. return;
  70. m_context_menu = GUI::Menu::construct();
  71. 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&) {
  72. auto nick = channel().member_model()->nick_at(member_view.selection().first());
  73. if (nick.is_empty())
  74. return;
  75. m_client->handle_open_query_action(m_client->nick_without_prefix(nick.characters()));
  76. }));
  77. m_context_menu->add_action(GUI::Action::create("Whois", Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-whois.png"), [&](const GUI::Action&) {
  78. auto nick = channel().member_model()->nick_at(member_view.selection().first());
  79. if (nick.is_empty())
  80. return;
  81. m_client->handle_whois_action(m_client->nick_without_prefix(nick.characters()));
  82. }));
  83. auto& context_control_menu = m_context_menu->add_submenu("Control");
  84. context_control_menu.add_action(GUI::Action::create("Voice", [&](const GUI::Action&) {
  85. auto nick = channel().member_model()->nick_at(member_view.selection().first());
  86. if (nick.is_empty())
  87. return;
  88. m_client->handle_voice_user_action(m_name.characters(), m_client->nick_without_prefix(nick.characters()));
  89. }));
  90. context_control_menu.add_action(GUI::Action::create("DeVoice", [&](const GUI::Action&) {
  91. auto nick = channel().member_model()->nick_at(member_view.selection().first());
  92. if (nick.is_empty())
  93. return;
  94. m_client->handle_devoice_user_action(m_name.characters(), m_client->nick_without_prefix(nick.characters()));
  95. }));
  96. context_control_menu.add_action(GUI::Action::create("Hop", [&](const GUI::Action&) {
  97. auto nick = channel().member_model()->nick_at(member_view.selection().first());
  98. if (nick.is_empty())
  99. return;
  100. m_client->handle_hop_user_action(m_name.characters(), m_client->nick_without_prefix(nick.characters()));
  101. }));
  102. context_control_menu.add_action(GUI::Action::create("DeHop", [&](const GUI::Action&) {
  103. auto nick = channel().member_model()->nick_at(member_view.selection().first());
  104. if (nick.is_empty())
  105. return;
  106. m_client->handle_dehop_user_action(m_name.characters(), m_client->nick_without_prefix(nick.characters()));
  107. }));
  108. context_control_menu.add_action(GUI::Action::create("Op", [&](const GUI::Action&) {
  109. auto nick = channel().member_model()->nick_at(member_view.selection().first());
  110. if (nick.is_empty())
  111. return;
  112. m_client->handle_op_user_action(m_name.characters(), m_client->nick_without_prefix(nick.characters()));
  113. }));
  114. context_control_menu.add_action(GUI::Action::create("DeOp", [&](const GUI::Action&) {
  115. auto nick = channel().member_model()->nick_at(member_view.selection().first());
  116. if (nick.is_empty())
  117. return;
  118. m_client->handle_deop_user_action(m_name.characters(), m_client->nick_without_prefix(nick.characters()));
  119. }));
  120. context_control_menu.add_separator();
  121. context_control_menu.add_action(GUI::Action::create("Kick", [&](const GUI::Action&) {
  122. auto nick = channel().member_model()->nick_at(member_view.selection().first());
  123. if (nick.is_empty())
  124. return;
  125. if (IRCClient::is_nick_prefix(nick[0]))
  126. nick = nick.substring(1, nick.length() - 1);
  127. String value;
  128. if (GUI::InputBox::show(window(), value, "Enter reason:", "Reason") == GUI::InputBox::ExecOK)
  129. m_client->handle_kick_user_action(m_name.characters(), m_client->nick_without_prefix(nick.characters()), value);
  130. }));
  131. auto& context_ctcp_menu = m_context_menu->add_submenu("CTCP");
  132. context_ctcp_menu.add_action(GUI::Action::create("User info", [&](const GUI::Action&) {
  133. auto nick = channel().member_model()->nick_at(member_view.selection().first());
  134. if (nick.is_empty())
  135. return;
  136. m_client->handle_ctcp_user_action(m_client->nick_without_prefix(nick.characters()), "USERINFO");
  137. }));
  138. context_ctcp_menu.add_action(GUI::Action::create("Finger", [&](const GUI::Action&) {
  139. auto nick = channel().member_model()->nick_at(member_view.selection().first());
  140. if (nick.is_empty())
  141. return;
  142. m_client->handle_ctcp_user_action(m_client->nick_without_prefix(nick.characters()), "FINGER");
  143. }));
  144. context_ctcp_menu.add_action(GUI::Action::create("Time", [&](const GUI::Action&) {
  145. auto nick = channel().member_model()->nick_at(member_view.selection().first());
  146. if (nick.is_empty())
  147. return;
  148. m_client->handle_ctcp_user_action(m_client->nick_without_prefix(nick.characters()), "TIME");
  149. }));
  150. context_ctcp_menu.add_action(GUI::Action::create("Version", [&](const GUI::Action&) {
  151. auto nick = channel().member_model()->nick_at(member_view.selection().first());
  152. if (nick.is_empty())
  153. return;
  154. m_client->handle_ctcp_user_action(m_client->nick_without_prefix(nick.characters()), "VERSION");
  155. }));
  156. context_ctcp_menu.add_action(GUI::Action::create("Client info", [&](const GUI::Action&) {
  157. auto nick = channel().member_model()->nick_at(member_view.selection().first());
  158. if (nick.is_empty())
  159. return;
  160. m_client->handle_ctcp_user_action(m_client->nick_without_prefix(nick.characters()), "CLIENTINFO");
  161. }));
  162. m_context_menu->popup(event.screen_position());
  163. };
  164. }
  165. m_text_box = add<GUI::TextBox>();
  166. m_text_box->set_fixed_height(19);
  167. m_text_box->on_return_pressed = [this] {
  168. if (m_type == Channel)
  169. m_client->handle_user_input_in_channel(m_name, m_text_box->text());
  170. else if (m_type == Query)
  171. m_client->handle_user_input_in_query(m_name, m_text_box->text());
  172. else if (m_type == Server)
  173. m_client->handle_user_input_in_server(m_text_box->text());
  174. m_text_box->add_current_text_to_history();
  175. m_text_box->clear();
  176. };
  177. m_text_box->set_history_enabled(true);
  178. m_text_box->set_placeholder("Message");
  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_page_view->set_document(const_cast<Web::DOM::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_page_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. }