IRCWindow.cpp 3.8 KB

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