IRCWindow.cpp 4.7 KB

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