IRCAppWindow.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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(String server, int port)
  49. : m_client(IRCClient::construct(server, port))
  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("%s@%s:%d - IRC Client", 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_gui_actions();
  84. };
  85. if (m_client->hostname().is_empty()) {
  86. auto input_box = GUI::InputBox::construct("Enter server:", "Connect to server", this);
  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 = GUI::InputBox::construct("Enter channel name:", "Join channel", this);
  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_list_channels_action = GUI::Action::create("List channels", Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-list.png"), [&](auto&) {
  104. m_client->handle_list_channels_action();
  105. });
  106. 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&) {
  107. auto* window = m_client->current_window();
  108. if (!window || window->type() != IRCWindow::Type::Channel) {
  109. return;
  110. }
  111. m_client->handle_part_action(window->channel().name());
  112. });
  113. m_whois_action = GUI::Action::create("Whois user", Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-whois.png"), [&](auto&) {
  114. auto input_box = GUI::InputBox::construct("Enter nickname:", "IRC WHOIS lookup", this);
  115. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
  116. m_client->handle_whois_action(input_box->text_value());
  117. });
  118. 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&) {
  119. auto input_box = GUI::InputBox::construct("Enter nickname:", "Open IRC query with...", this);
  120. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
  121. m_client->handle_open_query_action(input_box->text_value());
  122. });
  123. 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&) {
  124. printf("FIXME: Implement close-query action\n");
  125. });
  126. m_change_nick_action = GUI::Action::create("Change nickname", Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-nick.png"), [this](auto&) {
  127. auto input_box = GUI::InputBox::construct("Enter nickname:", "Change nickname", this);
  128. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
  129. m_client->handle_change_nick_action(input_box->text_value());
  130. });
  131. m_change_topic_action = GUI::Action::create("Change topic", Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-topic.png"), [this](auto&) {
  132. auto* window = m_client->current_window();
  133. if (!window || window->type() != IRCWindow::Type::Channel) {
  134. return;
  135. }
  136. auto input_box = GUI::InputBox::construct("Enter topic:", "Change topic", this);
  137. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
  138. m_client->handle_change_topic_action(window->channel().name(), input_box->text_value());
  139. });
  140. m_invite_user_action = GUI::Action::create("Invite user", Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-invite.png"), [this](auto&) {
  141. auto* window = m_client->current_window();
  142. if (!window || window->type() != IRCWindow::Type::Channel) {
  143. return;
  144. }
  145. auto input_box = GUI::InputBox::construct("Enter nick:", "Invite user", this);
  146. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
  147. m_client->handle_invite_user_action(window->channel().name(), input_box->text_value());
  148. });
  149. m_banlist_action = GUI::Action::create("Ban list", [this](auto&) {
  150. auto* window = m_client->current_window();
  151. if (!window || window->type() != IRCWindow::Type::Channel) {
  152. return;
  153. }
  154. m_client->handle_banlist_action(window->channel().name());
  155. });
  156. m_voice_user_action = GUI::Action::create("Voice user", [this](auto&) {
  157. auto* window = m_client->current_window();
  158. if (!window || window->type() != IRCWindow::Type::Channel) {
  159. return;
  160. }
  161. auto input_box = GUI::InputBox::construct("Enter nick:", "Voice user", this);
  162. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
  163. m_client->handle_voice_user_action(window->channel().name(), input_box->text_value());
  164. });
  165. m_devoice_user_action = GUI::Action::create("DeVoice user", [this](auto&) {
  166. auto* window = m_client->current_window();
  167. if (!window || window->type() != IRCWindow::Type::Channel) {
  168. return;
  169. }
  170. auto input_box = GUI::InputBox::construct("Enter nick:", "DeVoice user", this);
  171. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
  172. m_client->handle_devoice_user_action(window->channel().name(), input_box->text_value());
  173. });
  174. m_hop_user_action = GUI::Action::create("Hop user", [this](auto&) {
  175. auto* window = m_client->current_window();
  176. if (!window || window->type() != IRCWindow::Type::Channel) {
  177. return;
  178. }
  179. auto input_box = GUI::InputBox::construct("Enter nick:", "Hop user", this);
  180. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
  181. m_client->handle_hop_user_action(window->channel().name(), input_box->text_value());
  182. });
  183. m_dehop_user_action = GUI::Action::create("DeHop user", [this](auto&) {
  184. auto* window = m_client->current_window();
  185. if (!window || window->type() != IRCWindow::Type::Channel) {
  186. return;
  187. }
  188. auto input_box = GUI::InputBox::construct("Enter nick:", "DeHop user", this);
  189. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
  190. m_client->handle_dehop_user_action(window->channel().name(), input_box->text_value());
  191. });
  192. m_op_user_action = GUI::Action::create("Op user", [this](auto&) {
  193. auto* window = m_client->current_window();
  194. if (!window || window->type() != IRCWindow::Type::Channel) {
  195. return;
  196. }
  197. auto input_box = GUI::InputBox::construct("Enter nick:", "Op user", this);
  198. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
  199. m_client->handle_op_user_action(window->channel().name(), input_box->text_value());
  200. });
  201. m_deop_user_action = GUI::Action::create("DeOp user", [this](auto&) {
  202. auto* window = m_client->current_window();
  203. if (!window || window->type() != IRCWindow::Type::Channel) {
  204. return;
  205. }
  206. auto input_box = GUI::InputBox::construct("Enter nick:", "DeOp user", this);
  207. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
  208. m_client->handle_deop_user_action(window->channel().name(), input_box->text_value());
  209. });
  210. m_kick_user_action = GUI::Action::create("Kick user", [this](auto&) {
  211. auto* window = m_client->current_window();
  212. if (!window || window->type() != IRCWindow::Type::Channel) {
  213. return;
  214. }
  215. auto input_box = GUI::InputBox::construct("Enter nick:", "Kick user", this);
  216. auto reason_box = GUI::InputBox::construct("Enter reason:", "Reason", this);
  217. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
  218. if (reason_box->exec() == GUI::InputBox::ExecOK)
  219. m_client->handle_kick_user_action(window->channel().name(), input_box->text_value(), reason_box->text_value().characters());
  220. });
  221. m_cycle_channel_action = GUI::Action::create("Cycle channel", [this](auto&) {
  222. auto* window = m_client->current_window();
  223. if (!window || window->type() != IRCWindow::Type::Channel) {
  224. return;
  225. }
  226. m_client->handle_cycle_channel_action(window->channel().name());
  227. });
  228. }
  229. void IRCAppWindow::setup_menus()
  230. {
  231. auto menubar = GUI::MenuBar::construct();
  232. auto& app_menu = menubar->add_menu("IRC Client");
  233. app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  234. dbgprintf("Terminal: Quit menu activated!\n");
  235. GUI::Application::the().quit(0);
  236. return;
  237. }));
  238. auto& server_menu = menubar->add_menu("Server");
  239. server_menu.add_action(*m_change_nick_action);
  240. server_menu.add_separator();
  241. server_menu.add_action(*m_join_action);
  242. server_menu.add_action(*m_list_channels_action);
  243. server_menu.add_separator();
  244. server_menu.add_action(*m_whois_action);
  245. server_menu.add_action(*m_open_query_action);
  246. server_menu.add_action(*m_close_query_action);
  247. auto& channel_menu = menubar->add_menu("Channel");
  248. channel_menu.add_action(*m_change_topic_action);
  249. channel_menu.add_action(*m_invite_user_action);
  250. channel_menu.add_action(*m_banlist_action);
  251. RefPtr<GUI::Menu> channel_control_menu = GUI::Menu::construct("Control");
  252. channel_menu.add_submenu(*channel_control_menu);
  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 = widget.add<GUI::ToolBar>();
  277. toolbar.set_has_frame(false);
  278. toolbar.add_action(*m_change_nick_action);
  279. toolbar.add_separator();
  280. toolbar.add_action(*m_join_action);
  281. toolbar.add_action(*m_part_action);
  282. toolbar.add_separator();
  283. toolbar.add_action(*m_whois_action);
  284. toolbar.add_action(*m_open_query_action);
  285. toolbar.add_action(*m_close_query_action);
  286. auto& outer_container = widget.add<GUI::Widget>();
  287. outer_container.set_layout<GUI::VerticalBoxLayout>();
  288. outer_container.layout()->set_margins({ 2, 0, 2, 2 });
  289. auto& horizontal_container = outer_container.add<GUI::HorizontalSplitter>();
  290. m_window_list = horizontal_container.add<GUI::TableView>();
  291. m_window_list->set_headers_visible(false);
  292. m_window_list->set_alternating_row_colors(false);
  293. m_window_list->set_size_columns_to_fit_content(true);
  294. m_window_list->set_model(m_client->client_window_list_model());
  295. m_window_list->set_activates_on_selection(true);
  296. m_window_list->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  297. m_window_list->set_preferred_size(100, 0);
  298. m_window_list->on_activation = [this](auto& index) {
  299. set_active_window(m_client->window_at(index.row()));
  300. };
  301. m_container = horizontal_container.add<GUI::StackWidget>();
  302. m_container->on_active_widget_change = [this](auto*) {
  303. update_gui_actions();
  304. };
  305. create_window(&m_client, IRCWindow::Server, "Server");
  306. }
  307. void IRCAppWindow::set_active_window(IRCWindow& window)
  308. {
  309. m_container->set_active_widget(&window);
  310. window.clear_unread_count();
  311. auto index = m_window_list->model()->index(m_client->window_index(window));
  312. m_window_list->selection().set(index);
  313. }
  314. void IRCAppWindow::update_gui_actions()
  315. {
  316. auto* window = static_cast<IRCWindow*>(m_container->active_widget());
  317. bool is_open_channel = window && window->type() == IRCWindow::Type::Channel && window->channel().is_open();
  318. m_change_topic_action->set_enabled(is_open_channel);
  319. m_invite_user_action->set_enabled(is_open_channel);
  320. m_banlist_action->set_enabled(is_open_channel);
  321. m_voice_user_action->set_enabled(is_open_channel);
  322. m_devoice_user_action->set_enabled(is_open_channel);
  323. m_hop_user_action->set_enabled(is_open_channel);
  324. m_dehop_user_action->set_enabled(is_open_channel);
  325. m_op_user_action->set_enabled(is_open_channel);
  326. m_deop_user_action->set_enabled(is_open_channel);
  327. m_kick_user_action->set_enabled(is_open_channel);
  328. m_cycle_channel_action->set_enabled(is_open_channel);
  329. m_part_action->set_enabled(is_open_channel);
  330. }
  331. NonnullRefPtr<IRCWindow> IRCAppWindow::create_window(void* owner, IRCWindow::Type type, const String& name)
  332. {
  333. return m_container->add<IRCWindow>(m_client, owner, type, name);
  334. }