IRCAppWindow.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 <LibGUI/ToolBarContainer.h>
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. static IRCAppWindow* s_the;
  45. IRCAppWindow& IRCAppWindow::the()
  46. {
  47. return *s_the;
  48. }
  49. IRCAppWindow::IRCAppWindow(String server, int port)
  50. : m_client(IRCClient::construct(server, port))
  51. {
  52. ASSERT(!s_the);
  53. s_the = this;
  54. set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-irc-client.png"));
  55. update_title();
  56. set_rect(200, 200, 600, 400);
  57. setup_actions();
  58. setup_menus();
  59. setup_widgets();
  60. setup_client();
  61. }
  62. IRCAppWindow::~IRCAppWindow()
  63. {
  64. }
  65. void IRCAppWindow::update_title()
  66. {
  67. set_title(String::format("%s@%s:%d - IRC Client", m_client->nickname().characters(), m_client->hostname().characters(), m_client->port()));
  68. }
  69. void IRCAppWindow::setup_client()
  70. {
  71. m_client->aid_create_window = [this](void* owner, IRCWindow::Type type, const String& name) {
  72. return create_window(owner, type, name);
  73. };
  74. m_client->aid_get_active_window = [this] {
  75. return static_cast<IRCWindow*>(m_container->active_widget());
  76. };
  77. m_client->aid_update_window_list = [this] {
  78. m_window_list->model()->update();
  79. };
  80. m_client->on_nickname_changed = [this](const String&) {
  81. update_title();
  82. };
  83. m_client->on_part_from_channel = [this](auto&) {
  84. update_gui_actions();
  85. };
  86. if (m_client->hostname().is_empty()) {
  87. auto input_box = GUI::InputBox::construct("Enter server:", "Connect to server", this);
  88. auto result = input_box->exec();
  89. if (result == GUI::InputBox::ExecCancel)
  90. ::exit(0);
  91. m_client->set_server(input_box->text_value(), 6667);
  92. }
  93. update_title();
  94. bool success = m_client->connect();
  95. ASSERT(success);
  96. }
  97. void IRCAppWindow::setup_actions()
  98. {
  99. m_join_action = GUI::Action::create("Join channel", { Mod_Ctrl, Key_J }, Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-join.png"), [&](auto&) {
  100. auto input_box = GUI::InputBox::construct("Enter channel name:", "Join channel", this);
  101. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
  102. m_client->handle_join_action(input_box->text_value());
  103. });
  104. m_list_channels_action = GUI::Action::create("List channels", Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-list.png"), [&](auto&) {
  105. m_client->handle_list_channels_action();
  106. });
  107. 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&) {
  108. auto* window = m_client->current_window();
  109. if (!window || window->type() != IRCWindow::Type::Channel) {
  110. return;
  111. }
  112. m_client->handle_part_action(window->channel().name());
  113. });
  114. m_whois_action = GUI::Action::create("Whois user", Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-whois.png"), [&](auto&) {
  115. auto input_box = GUI::InputBox::construct("Enter nickname:", "IRC WHOIS lookup", this);
  116. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
  117. m_client->handle_whois_action(input_box->text_value());
  118. });
  119. 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&) {
  120. auto input_box = GUI::InputBox::construct("Enter nickname:", "Open IRC query with...", this);
  121. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
  122. m_client->handle_open_query_action(input_box->text_value());
  123. });
  124. 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&) {
  125. printf("FIXME: Implement close-query action\n");
  126. });
  127. m_change_nick_action = GUI::Action::create("Change nickname", Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-nick.png"), [this](auto&) {
  128. auto input_box = GUI::InputBox::construct("Enter nickname:", "Change nickname", this);
  129. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
  130. m_client->handle_change_nick_action(input_box->text_value());
  131. });
  132. m_change_topic_action = GUI::Action::create("Change topic", Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-topic.png"), [this](auto&) {
  133. auto* window = m_client->current_window();
  134. if (!window || window->type() != IRCWindow::Type::Channel) {
  135. return;
  136. }
  137. auto input_box = GUI::InputBox::construct("Enter topic:", "Change topic", this);
  138. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
  139. m_client->handle_change_topic_action(window->channel().name(), input_box->text_value());
  140. });
  141. m_invite_user_action = GUI::Action::create("Invite user", Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-invite.png"), [this](auto&) {
  142. auto* window = m_client->current_window();
  143. if (!window || window->type() != IRCWindow::Type::Channel) {
  144. return;
  145. }
  146. auto input_box = GUI::InputBox::construct("Enter nick:", "Invite user", this);
  147. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
  148. m_client->handle_invite_user_action(window->channel().name(), input_box->text_value());
  149. });
  150. m_banlist_action = GUI::Action::create("Ban list", [this](auto&) {
  151. auto* window = m_client->current_window();
  152. if (!window || window->type() != IRCWindow::Type::Channel) {
  153. return;
  154. }
  155. m_client->handle_banlist_action(window->channel().name());
  156. });
  157. m_voice_user_action = GUI::Action::create("Voice user", [this](auto&) {
  158. auto* window = m_client->current_window();
  159. if (!window || window->type() != IRCWindow::Type::Channel) {
  160. return;
  161. }
  162. auto input_box = GUI::InputBox::construct("Enter nick:", "Voice user", this);
  163. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
  164. m_client->handle_voice_user_action(window->channel().name(), input_box->text_value());
  165. });
  166. m_devoice_user_action = GUI::Action::create("DeVoice user", [this](auto&) {
  167. auto* window = m_client->current_window();
  168. if (!window || window->type() != IRCWindow::Type::Channel) {
  169. return;
  170. }
  171. auto input_box = GUI::InputBox::construct("Enter nick:", "DeVoice user", this);
  172. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
  173. m_client->handle_devoice_user_action(window->channel().name(), input_box->text_value());
  174. });
  175. m_hop_user_action = GUI::Action::create("Hop user", [this](auto&) {
  176. auto* window = m_client->current_window();
  177. if (!window || window->type() != IRCWindow::Type::Channel) {
  178. return;
  179. }
  180. auto input_box = GUI::InputBox::construct("Enter nick:", "Hop user", this);
  181. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
  182. m_client->handle_hop_user_action(window->channel().name(), input_box->text_value());
  183. });
  184. m_dehop_user_action = GUI::Action::create("DeHop user", [this](auto&) {
  185. auto* window = m_client->current_window();
  186. if (!window || window->type() != IRCWindow::Type::Channel) {
  187. return;
  188. }
  189. auto input_box = GUI::InputBox::construct("Enter nick:", "DeHop user", this);
  190. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
  191. m_client->handle_dehop_user_action(window->channel().name(), input_box->text_value());
  192. });
  193. m_op_user_action = GUI::Action::create("Op user", [this](auto&) {
  194. auto* window = m_client->current_window();
  195. if (!window || window->type() != IRCWindow::Type::Channel) {
  196. return;
  197. }
  198. auto input_box = GUI::InputBox::construct("Enter nick:", "Op user", this);
  199. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
  200. m_client->handle_op_user_action(window->channel().name(), input_box->text_value());
  201. });
  202. m_deop_user_action = GUI::Action::create("DeOp user", [this](auto&) {
  203. auto* window = m_client->current_window();
  204. if (!window || window->type() != IRCWindow::Type::Channel) {
  205. return;
  206. }
  207. auto input_box = GUI::InputBox::construct("Enter nick:", "DeOp user", this);
  208. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
  209. m_client->handle_deop_user_action(window->channel().name(), input_box->text_value());
  210. });
  211. m_kick_user_action = GUI::Action::create("Kick user", [this](auto&) {
  212. auto* window = m_client->current_window();
  213. if (!window || window->type() != IRCWindow::Type::Channel) {
  214. return;
  215. }
  216. auto input_box = GUI::InputBox::construct("Enter nick:", "Kick user", this);
  217. auto reason_box = GUI::InputBox::construct("Enter reason:", "Reason", this);
  218. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
  219. if (reason_box->exec() == GUI::InputBox::ExecOK)
  220. m_client->handle_kick_user_action(window->channel().name(), input_box->text_value(), reason_box->text_value().characters());
  221. });
  222. m_cycle_channel_action = GUI::Action::create("Cycle channel", [this](auto&) {
  223. auto* window = m_client->current_window();
  224. if (!window || window->type() != IRCWindow::Type::Channel) {
  225. return;
  226. }
  227. m_client->handle_cycle_channel_action(window->channel().name());
  228. });
  229. }
  230. void IRCAppWindow::setup_menus()
  231. {
  232. auto menubar = GUI::MenuBar::construct();
  233. auto& app_menu = menubar->add_menu("IRC Client");
  234. app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  235. dbgprintf("Terminal: Quit menu activated!\n");
  236. GUI::Application::the().quit(0);
  237. return;
  238. }));
  239. auto& server_menu = menubar->add_menu("Server");
  240. server_menu.add_action(*m_change_nick_action);
  241. server_menu.add_separator();
  242. server_menu.add_action(*m_join_action);
  243. server_menu.add_action(*m_list_channels_action);
  244. server_menu.add_separator();
  245. server_menu.add_action(*m_whois_action);
  246. server_menu.add_action(*m_open_query_action);
  247. server_menu.add_action(*m_close_query_action);
  248. auto& channel_menu = menubar->add_menu("Channel");
  249. channel_menu.add_action(*m_change_topic_action);
  250. channel_menu.add_action(*m_invite_user_action);
  251. channel_menu.add_action(*m_banlist_action);
  252. auto& channel_control_menu = channel_menu.add_submenu("Control");
  253. channel_control_menu.add_action(*m_voice_user_action);
  254. channel_control_menu.add_action(*m_devoice_user_action);
  255. channel_control_menu.add_action(*m_hop_user_action);
  256. channel_control_menu.add_action(*m_dehop_user_action);
  257. channel_control_menu.add_action(*m_op_user_action);
  258. channel_control_menu.add_action(*m_deop_user_action);
  259. channel_control_menu.add_separator();
  260. channel_control_menu.add_action(*m_kick_user_action);
  261. channel_menu.add_separator();
  262. channel_menu.add_action(*m_cycle_channel_action);
  263. channel_menu.add_action(*m_part_action);
  264. auto& help_menu = menubar->add_menu("Help");
  265. help_menu.add_action(GUI::Action::create("About", [this](auto&) {
  266. GUI::AboutDialog::show("IRC Client", Gfx::Bitmap::load_from_file("/res/icons/32x32/app-irc-client.png"), this);
  267. }));
  268. GUI::Application::the().set_menubar(move(menubar));
  269. }
  270. void IRCAppWindow::setup_widgets()
  271. {
  272. auto& widget = set_main_widget<GUI::Widget>();
  273. widget.set_fill_with_background_color(true);
  274. widget.set_layout<GUI::VerticalBoxLayout>();
  275. widget.layout()->set_spacing(0);
  276. auto& toolbar_container = widget.add<GUI::ToolBarContainer>();
  277. auto& toolbar = toolbar_container.add<GUI::ToolBar>();
  278. toolbar.set_has_frame(false);
  279. toolbar.add_action(*m_change_nick_action);
  280. toolbar.add_separator();
  281. toolbar.add_action(*m_join_action);
  282. toolbar.add_action(*m_part_action);
  283. toolbar.add_separator();
  284. toolbar.add_action(*m_whois_action);
  285. toolbar.add_action(*m_open_query_action);
  286. toolbar.add_action(*m_close_query_action);
  287. auto& outer_container = widget.add<GUI::Widget>();
  288. outer_container.set_layout<GUI::VerticalBoxLayout>();
  289. outer_container.layout()->set_margins({ 2, 0, 2, 2 });
  290. auto& horizontal_container = outer_container.add<GUI::HorizontalSplitter>();
  291. m_window_list = horizontal_container.add<GUI::TableView>();
  292. m_window_list->set_headers_visible(false);
  293. m_window_list->set_alternating_row_colors(false);
  294. m_window_list->set_size_columns_to_fit_content(true);
  295. m_window_list->set_model(m_client->client_window_list_model());
  296. m_window_list->set_activates_on_selection(true);
  297. m_window_list->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  298. m_window_list->set_preferred_size(100, 0);
  299. m_window_list->on_activation = [this](auto& index) {
  300. set_active_window(m_client->window_at(index.row()));
  301. };
  302. m_container = horizontal_container.add<GUI::StackWidget>();
  303. m_container->on_active_widget_change = [this](auto*) {
  304. update_gui_actions();
  305. };
  306. create_window(&m_client, IRCWindow::Server, "Server");
  307. }
  308. void IRCAppWindow::set_active_window(IRCWindow& window)
  309. {
  310. m_container->set_active_widget(&window);
  311. window.clear_unread_count();
  312. auto index = m_window_list->model()->index(m_client->window_index(window));
  313. m_window_list->selection().set(index);
  314. }
  315. void IRCAppWindow::update_gui_actions()
  316. {
  317. auto* window = static_cast<IRCWindow*>(m_container->active_widget());
  318. bool is_open_channel = window && window->type() == IRCWindow::Type::Channel && window->channel().is_open();
  319. m_change_topic_action->set_enabled(is_open_channel);
  320. m_invite_user_action->set_enabled(is_open_channel);
  321. m_banlist_action->set_enabled(is_open_channel);
  322. m_voice_user_action->set_enabled(is_open_channel);
  323. m_devoice_user_action->set_enabled(is_open_channel);
  324. m_hop_user_action->set_enabled(is_open_channel);
  325. m_dehop_user_action->set_enabled(is_open_channel);
  326. m_op_user_action->set_enabled(is_open_channel);
  327. m_deop_user_action->set_enabled(is_open_channel);
  328. m_kick_user_action->set_enabled(is_open_channel);
  329. m_cycle_channel_action->set_enabled(is_open_channel);
  330. m_part_action->set_enabled(is_open_channel);
  331. }
  332. NonnullRefPtr<IRCWindow> IRCAppWindow::create_window(void* owner, IRCWindow::Type type, const String& name)
  333. {
  334. return m_container->add<IRCWindow>(m_client, owner, type, name);
  335. }