浏览代码

IRCClient: Modernize Core::Object usage

Andreas Kling 5 年之前
父节点
当前提交
8efafdfc12

+ 19 - 18
Applications/IRCClient/IRCAppWindow.cpp

@@ -50,6 +50,7 @@ IRCAppWindow& IRCAppWindow::the()
 }
 }
 
 
 IRCAppWindow::IRCAppWindow()
 IRCAppWindow::IRCAppWindow()
+    : m_client(IRCClient::construct())
 {
 {
     ASSERT(!s_the);
     ASSERT(!s_the);
     s_the = this;
     s_the = this;
@@ -71,37 +72,37 @@ IRCAppWindow::~IRCAppWindow()
 
 
 void IRCAppWindow::update_title()
 void IRCAppWindow::update_title()
 {
 {
-    set_title(String::format("IRC Client: %s@%s:%d", m_client.nickname().characters(), m_client.hostname().characters(), m_client.port()));
+    set_title(String::format("IRC Client: %s@%s:%d", m_client->nickname().characters(), m_client->hostname().characters(), m_client->port()));
 }
 }
 
 
 void IRCAppWindow::setup_client()
 void IRCAppWindow::setup_client()
 {
 {
-    m_client.aid_create_window = [this](void* owner, IRCWindow::Type type, const String& name) {
+    m_client->aid_create_window = [this](void* owner, IRCWindow::Type type, const String& name) {
         return &create_window(owner, type, name);
         return &create_window(owner, type, name);
     };
     };
-    m_client.aid_get_active_window = [this] {
+    m_client->aid_get_active_window = [this] {
         return static_cast<IRCWindow*>(m_container->active_widget());
         return static_cast<IRCWindow*>(m_container->active_widget());
     };
     };
-    m_client.aid_update_window_list = [this] {
+    m_client->aid_update_window_list = [this] {
         m_window_list->model()->update();
         m_window_list->model()->update();
     };
     };
-    m_client.on_nickname_changed = [this](const String&) {
+    m_client->on_nickname_changed = [this](const String&) {
         update_title();
         update_title();
     };
     };
-    m_client.on_part_from_channel = [this](auto&) {
+    m_client->on_part_from_channel = [this](auto&) {
         update_part_action();
         update_part_action();
     };
     };
 
 
-    if (m_client.hostname().is_empty()) {
+    if (m_client->hostname().is_empty()) {
         auto input_box = GUI::InputBox::construct("Enter server:", "Connect to server", this);
         auto input_box = GUI::InputBox::construct("Enter server:", "Connect to server", this);
         auto result = input_box->exec();
         auto result = input_box->exec();
         if (result == GUI::InputBox::ExecCancel)
         if (result == GUI::InputBox::ExecCancel)
             ::exit(0);
             ::exit(0);
 
 
-        m_client.set_server(input_box->text_value(), 6667);
+        m_client->set_server(input_box->text_value(), 6667);
     }
     }
     update_title();
     update_title();
-    bool success = m_client.connect();
+    bool success = m_client->connect();
     ASSERT(success);
     ASSERT(success);
 }
 }
 
 
@@ -110,28 +111,28 @@ void IRCAppWindow::setup_actions()
     m_join_action = GUI::Action::create("Join channel", { Mod_Ctrl, Key_J }, Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-join.png"), [&](auto&) {
     m_join_action = GUI::Action::create("Join channel", { Mod_Ctrl, Key_J }, Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-join.png"), [&](auto&) {
         auto input_box = GUI::InputBox::construct("Enter channel name:", "Join channel", this);
         auto input_box = GUI::InputBox::construct("Enter channel name:", "Join channel", this);
         if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
         if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
-            m_client.handle_join_action(input_box->text_value());
+            m_client->handle_join_action(input_box->text_value());
     });
     });
 
 
     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&) {
     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&) {
-        auto* window = m_client.current_window();
+        auto* window = m_client->current_window();
         if (!window || window->type() != IRCWindow::Type::Channel) {
         if (!window || window->type() != IRCWindow::Type::Channel) {
             // FIXME: Perhaps this action should have been disabled instead of allowing us to activate it.
             // FIXME: Perhaps this action should have been disabled instead of allowing us to activate it.
             return;
             return;
         }
         }
-        m_client.handle_part_action(window->channel().name());
+        m_client->handle_part_action(window->channel().name());
     });
     });
 
 
     m_whois_action = GUI::Action::create("Whois user", Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-whois.png"), [&](auto&) {
     m_whois_action = GUI::Action::create("Whois user", Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-whois.png"), [&](auto&) {
         auto input_box = GUI::InputBox::construct("Enter nickname:", "IRC WHOIS lookup", this);
         auto input_box = GUI::InputBox::construct("Enter nickname:", "IRC WHOIS lookup", this);
         if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
         if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
-            m_client.handle_whois_action(input_box->text_value());
+            m_client->handle_whois_action(input_box->text_value());
     });
     });
 
 
     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&) {
     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&) {
         auto input_box = GUI::InputBox::construct("Enter nickname:", "Open IRC query with...", this);
         auto input_box = GUI::InputBox::construct("Enter nickname:", "Open IRC query with...", this);
         if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
         if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
-            m_client.handle_open_query_action(input_box->text_value());
+            m_client->handle_open_query_action(input_box->text_value());
     });
     });
 
 
     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&) {
     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&) {
@@ -141,7 +142,7 @@ void IRCAppWindow::setup_actions()
     m_change_nick_action = GUI::Action::create("Change nickname", Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-nick.png"), [this](auto&) {
     m_change_nick_action = GUI::Action::create("Change nickname", Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-nick.png"), [this](auto&) {
         auto input_box = GUI::InputBox::construct("Enter nickname:", "Change nickname", this);
         auto input_box = GUI::InputBox::construct("Enter nickname:", "Change nickname", this);
         if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
         if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
-            m_client.handle_change_nick_action(input_box->text_value());
+            m_client->handle_change_nick_action(input_box->text_value());
     });
     });
 }
 }
 
 
@@ -205,12 +206,12 @@ void IRCAppWindow::setup_widgets()
     m_window_list->set_headers_visible(false);
     m_window_list->set_headers_visible(false);
     m_window_list->set_alternating_row_colors(false);
     m_window_list->set_alternating_row_colors(false);
     m_window_list->set_size_columns_to_fit_content(true);
     m_window_list->set_size_columns_to_fit_content(true);
-    m_window_list->set_model(m_client.client_window_list_model());
+    m_window_list->set_model(m_client->client_window_list_model());
     m_window_list->set_activates_on_selection(true);
     m_window_list->set_activates_on_selection(true);
     m_window_list->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
     m_window_list->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
     m_window_list->set_preferred_size(100, 0);
     m_window_list->set_preferred_size(100, 0);
     m_window_list->on_activation = [this](auto& index) {
     m_window_list->on_activation = [this](auto& index) {
-        set_active_window(m_client.window_at(index.row()));
+        set_active_window(m_client->window_at(index.row()));
     };
     };
 
 
     m_container = GUI::StackWidget::construct(horizontal_container);
     m_container = GUI::StackWidget::construct(horizontal_container);
@@ -225,7 +226,7 @@ void IRCAppWindow::set_active_window(IRCWindow& window)
 {
 {
     m_container->set_active_widget(&window);
     m_container->set_active_widget(&window);
     window.clear_unread_count();
     window.clear_unread_count();
-    auto index = m_window_list->model()->index(m_client.window_index(window));
+    auto index = m_window_list->model()->index(m_client->window_index(window));
     m_window_list->selection().set(index);
     m_window_list->selection().set(index);
 }
 }
 
 

+ 4 - 2
Applications/IRCClient/IRCAppWindow.h

@@ -32,8 +32,8 @@
 #include <LibGUI/Window.h>
 #include <LibGUI/Window.h>
 
 
 class IRCAppWindow : public GUI::Window {
 class IRCAppWindow : public GUI::Window {
+    C_OBJECT(IRCAppWindow);
 public:
 public:
-    IRCAppWindow();
     virtual ~IRCAppWindow() override;
     virtual ~IRCAppWindow() override;
 
 
     static IRCAppWindow& the();
     static IRCAppWindow& the();
@@ -41,6 +41,8 @@ public:
     void set_active_window(IRCWindow&);
     void set_active_window(IRCWindow&);
 
 
 private:
 private:
+    IRCAppWindow();
+
     void setup_client();
     void setup_client();
     void setup_actions();
     void setup_actions();
     void setup_menus();
     void setup_menus();
@@ -49,7 +51,7 @@ private:
     void update_part_action();
     void update_part_action();
 
 
     IRCWindow& create_window(void* owner, IRCWindow::Type, const String& name);
     IRCWindow& create_window(void* owner, IRCWindow::Type, const String& name);
-    IRCClient m_client;
+    NonnullRefPtr<IRCClient> m_client;
     RefPtr<GUI::StackWidget> m_container;
     RefPtr<GUI::StackWidget> m_container;
     RefPtr<GUI::TableView> m_window_list;
     RefPtr<GUI::TableView> m_window_list;
     RefPtr<GUI::Action> m_join_action;
     RefPtr<GUI::Action> m_join_action;

+ 2 - 1
Applications/IRCClient/IRCClient.h

@@ -45,7 +45,6 @@ class IRCClient final : public Core::Object {
     friend class IRCQuery;
     friend class IRCQuery;
 
 
 public:
 public:
-    IRCClient();
     virtual ~IRCClient() override;
     virtual ~IRCClient() override;
 
 
     void set_server(const String& hostname, int port = 6667);
     void set_server(const String& hostname, int port = 6667);
@@ -114,6 +113,8 @@ public:
     void add_server_message(const String&, Color = Color::Black);
     void add_server_message(const String&, Color = Color::Black);
 
 
 private:
 private:
+    IRCClient();
+
     struct Message {
     struct Message {
         String prefix;
         String prefix;
         String command;
         String command;

+ 2 - 4
Applications/IRCClient/main.cpp

@@ -43,9 +43,7 @@ int main(int argc, char** argv)
         return 1;
         return 1;
     }
     }
 
 
-    IRCAppWindow app_window;
-    app_window.show();
-
-    printf("Entering main loop...\n");
+    auto app_window = IRCAppWindow::construct();
+    app_window->show();
     return app.exec();
     return app.exec();
 }
 }