IRCAppWindow.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 "IRCAppWindow.h"
  27. #include "IRCChannel.h"
  28. #include "IRCWindow.h"
  29. #include "IRCWindowListModel.h"
  30. #include <LibGUI/AboutDialog.h>
  31. #include <LibGUI/Action.h>
  32. #include <LibGUI/Application.h>
  33. #include <LibGUI/BoxLayout.h>
  34. #include <LibGUI/InputBox.h>
  35. #include <LibGUI/Menu.h>
  36. #include <LibGUI/MenuBar.h>
  37. #include <LibGUI/Splitter.h>
  38. #include <LibGUI/StackWidget.h>
  39. #include <LibGUI/TableView.h>
  40. #include <LibGUI/ToolBar.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. static IRCAppWindow* s_the;
  44. IRCAppWindow& IRCAppWindow::the()
  45. {
  46. return *s_the;
  47. }
  48. IRCAppWindow::IRCAppWindow()
  49. : m_client(IRCClient::construct())
  50. {
  51. ASSERT(!s_the);
  52. s_the = this;
  53. set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-irc-client.png"));
  54. update_title();
  55. set_rect(200, 200, 600, 400);
  56. setup_actions();
  57. setup_menus();
  58. setup_widgets();
  59. setup_client();
  60. }
  61. IRCAppWindow::~IRCAppWindow()
  62. {
  63. }
  64. void IRCAppWindow::update_title()
  65. {
  66. set_title(String::format("IRC Client: %s@%s:%d", m_client->nickname().characters(), m_client->hostname().characters(), m_client->port()));
  67. }
  68. void IRCAppWindow::setup_client()
  69. {
  70. m_client->aid_create_window = [this](void* owner, IRCWindow::Type type, const String& name) {
  71. return create_window(owner, type, name);
  72. };
  73. m_client->aid_get_active_window = [this] {
  74. return static_cast<IRCWindow*>(m_container->active_widget());
  75. };
  76. m_client->aid_update_window_list = [this] {
  77. m_window_list->model()->update();
  78. };
  79. m_client->on_nickname_changed = [this](const String&) {
  80. update_title();
  81. };
  82. m_client->on_part_from_channel = [this](auto&) {
  83. update_part_action();
  84. };
  85. if (m_client->hostname().is_empty()) {
  86. auto input_box = add<GUI::InputBox>("Enter server:", "Connect to server");
  87. auto result = input_box->exec();
  88. if (result == GUI::InputBox::ExecCancel)
  89. ::exit(0);
  90. m_client->set_server(input_box->text_value(), 6667);
  91. }
  92. update_title();
  93. bool success = m_client->connect();
  94. ASSERT(success);
  95. }
  96. void IRCAppWindow::setup_actions()
  97. {
  98. m_join_action = GUI::Action::create("Join channel", { Mod_Ctrl, Key_J }, Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-join.png"), [&](auto&) {
  99. auto input_box = add<GUI::InputBox>("Enter channel name:", "Join channel");
  100. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
  101. m_client->handle_join_action(input_box->text_value());
  102. });
  103. m_part_action = GUI::Action::create("Part from channel", { Mod_Ctrl, Key_P }, Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-part.png"), [this](auto&) {
  104. auto* window = m_client->current_window();
  105. if (!window || window->type() != IRCWindow::Type::Channel) {
  106. // FIXME: Perhaps this action should have been disabled instead of allowing us to activate it.
  107. return;
  108. }
  109. m_client->handle_part_action(window->channel().name());
  110. });
  111. m_whois_action = GUI::Action::create("Whois user", Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-whois.png"), [&](auto&) {
  112. auto input_box = GUI::InputBox::construct("Enter nickname:", "IRC WHOIS lookup", this);
  113. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
  114. m_client->handle_whois_action(input_box->text_value());
  115. });
  116. m_open_query_action = GUI::Action::create("Open query", { Mod_Ctrl, Key_O }, Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-open-query.png"), [&](auto&) {
  117. auto input_box = GUI::InputBox::construct("Enter nickname:", "Open IRC query with...", this);
  118. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
  119. m_client->handle_open_query_action(input_box->text_value());
  120. });
  121. m_close_query_action = GUI::Action::create("Close query", { Mod_Ctrl, Key_D }, Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-close-query.png"), [](auto&) {
  122. printf("FIXME: Implement close-query action\n");
  123. });
  124. m_change_nick_action = GUI::Action::create("Change nickname", Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-nick.png"), [this](auto&) {
  125. auto input_box = GUI::InputBox::construct("Enter nickname:", "Change nickname", this);
  126. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
  127. m_client->handle_change_nick_action(input_box->text_value());
  128. });
  129. }
  130. void IRCAppWindow::setup_menus()
  131. {
  132. auto menubar = make<GUI::MenuBar>();
  133. auto app_menu = GUI::Menu::construct("IRC Client");
  134. app_menu->add_action(GUI::CommonActions::make_quit_action([](auto&) {
  135. dbgprintf("Terminal: Quit menu activated!\n");
  136. GUI::Application::the().quit(0);
  137. return;
  138. }));
  139. menubar->add_menu(move(app_menu));
  140. auto server_menu = GUI::Menu::construct("Server");
  141. server_menu->add_action(*m_change_nick_action);
  142. server_menu->add_separator();
  143. server_menu->add_action(*m_join_action);
  144. server_menu->add_action(*m_part_action);
  145. server_menu->add_separator();
  146. server_menu->add_action(*m_whois_action);
  147. server_menu->add_action(*m_open_query_action);
  148. server_menu->add_action(*m_close_query_action);
  149. menubar->add_menu(move(server_menu));
  150. auto help_menu = GUI::Menu::construct("Help");
  151. help_menu->add_action(GUI::Action::create("About", [this](const GUI::Action&) {
  152. GUI::AboutDialog::show("IRC Client", Gfx::Bitmap::load_from_file("/res/icons/32x32/app-irc-client.png"), this);
  153. }));
  154. menubar->add_menu(move(help_menu));
  155. GUI::Application::the().set_menubar(move(menubar));
  156. }
  157. void IRCAppWindow::setup_widgets()
  158. {
  159. auto& widget = set_main_widget<GUI::Widget>();
  160. widget.set_fill_with_background_color(true);
  161. widget.set_layout<GUI::VerticalBoxLayout>();
  162. widget.layout()->set_spacing(0);
  163. auto toolbar = widget.add<GUI::ToolBar>();
  164. toolbar->set_has_frame(false);
  165. toolbar->add_action(*m_change_nick_action);
  166. toolbar->add_separator();
  167. toolbar->add_action(*m_join_action);
  168. toolbar->add_action(*m_part_action);
  169. toolbar->add_separator();
  170. toolbar->add_action(*m_whois_action);
  171. toolbar->add_action(*m_open_query_action);
  172. toolbar->add_action(*m_close_query_action);
  173. auto outer_container = widget.add<GUI::Widget>();
  174. outer_container->set_layout<GUI::VerticalBoxLayout>();
  175. outer_container->layout()->set_margins({ 2, 0, 2, 2 });
  176. auto horizontal_container = outer_container->add<GUI::HorizontalSplitter>();
  177. m_window_list = horizontal_container->add<GUI::TableView>();
  178. m_window_list->set_headers_visible(false);
  179. m_window_list->set_alternating_row_colors(false);
  180. m_window_list->set_size_columns_to_fit_content(true);
  181. m_window_list->set_model(m_client->client_window_list_model());
  182. m_window_list->set_activates_on_selection(true);
  183. m_window_list->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  184. m_window_list->set_preferred_size(100, 0);
  185. m_window_list->on_activation = [this](auto& index) {
  186. set_active_window(m_client->window_at(index.row()));
  187. };
  188. m_container = horizontal_container->add<GUI::StackWidget>();
  189. m_container->on_active_widget_change = [this](auto*) {
  190. update_part_action();
  191. };
  192. create_window(&m_client, IRCWindow::Server, "Server");
  193. }
  194. void IRCAppWindow::set_active_window(IRCWindow& window)
  195. {
  196. m_container->set_active_widget(&window);
  197. window.clear_unread_count();
  198. auto index = m_window_list->model()->index(m_client->window_index(window));
  199. m_window_list->selection().set(index);
  200. }
  201. void IRCAppWindow::update_part_action()
  202. {
  203. auto* window = static_cast<IRCWindow*>(m_container->active_widget());
  204. bool is_open_channel = window && window->type() == IRCWindow::Type::Channel && window->channel().is_open();
  205. m_part_action->set_enabled(is_open_channel);
  206. }
  207. NonnullRefPtr<IRCWindow> IRCAppWindow::create_window(void* owner, IRCWindow::Type type, const String& name)
  208. {
  209. return m_container->add<IRCWindow>(m_client, owner, type, name);
  210. }