diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-03-15 17:37:13 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-03-15 17:37:13 +0100 |
commit | 1fc283ed7dc504a519db57d442f498e3c803586b (patch) | |
tree | 69f383daf37a3e7d453f0a72068afd50a37af438 /Applications | |
parent | ad08165a252d096ba7e4730efce9fb96181b5373 (diff) | |
download | serenity-1fc283ed7dc504a519db57d442f498e3c803586b.zip |
IRCClient+LibGUI: Add an input box so we can send messages to channels.
Implement this using a GTextEditor with a special single-line mode.
This new mode needs some polishing, but it's already very useful.
Diffstat (limited to 'Applications')
-rw-r--r-- | Applications/IRCClient/IRCChannel.cpp | 9 | ||||
-rw-r--r-- | Applications/IRCClient/IRCChannel.h | 2 | ||||
-rw-r--r-- | Applications/IRCClient/IRCClient.cpp | 40 | ||||
-rw-r--r-- | Applications/IRCClient/IRCClient.h | 7 | ||||
-rw-r--r-- | Applications/IRCClient/IRCClientWindow.cpp | 14 | ||||
-rw-r--r-- | Applications/IRCClient/IRCClientWindow.h | 2 | ||||
-rw-r--r-- | Applications/IRCClient/IRCLogBufferModel.cpp | 14 | ||||
-rw-r--r-- | Applications/IRCClient/IRCLogBufferModel.h | 2 |
8 files changed, 73 insertions, 17 deletions
diff --git a/Applications/IRCClient/IRCChannel.cpp b/Applications/IRCClient/IRCChannel.cpp index 1c6bec841b..45e7acb0b7 100644 --- a/Applications/IRCClient/IRCChannel.cpp +++ b/Applications/IRCClient/IRCChannel.cpp @@ -40,8 +40,13 @@ void IRCChannel::add_message(char prefix, const String& name, const String& text void IRCChannel::dump() const { printf("IRCChannel{%p}: %s\n", this, m_name.characters()); - for (auto& member : m_members) { + for (auto& member : m_members) printf(" (%c)%s\n", member.prefix ? member.prefix : ' ', member.name.characters()); - } log().dump(); } + +void IRCChannel::say(const String& text) +{ + m_client.send_privmsg(m_name, text); + add_message(' ', m_client.nickname(), text); +} diff --git a/Applications/IRCClient/IRCChannel.h b/Applications/IRCClient/IRCChannel.h index d43702320b..b0c85bd132 100644 --- a/Applications/IRCClient/IRCChannel.h +++ b/Applications/IRCClient/IRCChannel.h @@ -26,6 +26,8 @@ public: void dump() const; + void say(const String&); + const IRCLogBuffer& log() const { return *m_log; } IRCLogBuffer& log() { return *m_log; } diff --git a/Applications/IRCClient/IRCClient.cpp b/Applications/IRCClient/IRCClient.cpp index b78f4c2a97..478412cfc1 100644 --- a/Applications/IRCClient/IRCClient.cpp +++ b/Applications/IRCClient/IRCClient.cpp @@ -237,6 +237,30 @@ void IRCClient::handle(const Message& msg, const String&) m_log->add_message(0, "Server", String::format("[%s] %s", msg.command.characters(), msg.arguments[1].characters())); } +void IRCClient::send_privmsg(const String& target, const String& text) +{ + send(String::format("PRIVMSG %s :%s\r\n", target.characters(), text.characters())); +} + +void IRCClient::handle_user_input_in_channel(const String& channel_name, const String& input) +{ + if (input.is_empty()) + return; + ensure_channel(channel_name).say(input); +} + +void IRCClient::handle_user_input_in_query(const String& query_name, const String& input) +{ + if (input.is_empty()) + return; +} + +void IRCClient::handle_user_input_in_server(const String& input) +{ + if (input.is_empty()) + return; +} + bool IRCClient::is_nick_prefix(char ch) const { switch (ch) { @@ -297,6 +321,17 @@ IRCQuery& IRCClient::ensure_query(const String& name) return query_reference; } +IRCChannel& IRCClient::ensure_channel(const String& name) +{ + auto it = m_channels.find(name); + if (it != m_channels.end()) + return *(*it).value; + auto channel = IRCChannel::create(*this, name); + auto& channel_reference = *channel; + m_channels.set(name, channel.copy_ref()); + return channel_reference; +} + void IRCClient::handle_ping(const Message& msg) { if (msg.arguments.size() < 0) @@ -310,10 +345,7 @@ void IRCClient::handle_join(const Message& msg) if (msg.arguments.size() != 1) return; auto& channel_name = msg.arguments[0]; - auto it = m_channels.find(channel_name); - ASSERT(it == m_channels.end()); - auto channel = IRCChannel::create(*this, channel_name); - m_channels.set(channel_name, move(channel)); + ensure_channel(channel_name); if (on_join) on_join(channel_name); } diff --git a/Applications/IRCClient/IRCClient.h b/Applications/IRCClient/IRCClient.h index fffa333aec..04771b722a 100644 --- a/Applications/IRCClient/IRCClient.h +++ b/Applications/IRCClient/IRCClient.h @@ -13,6 +13,7 @@ class IRCClientWindowListModel; class GNotifier; class IRCClient { + friend class IRCChannel; public: IRCClient(const String& address, int port = 6667); ~IRCClient(); @@ -45,6 +46,10 @@ public: const IRCClientWindow& window_at(int index) const { return *m_windows.at(index); } IRCClientWindow& window_at(int index) { return *m_windows.at(index); } + void handle_user_input_in_channel(const String& channel_name, const String&); + void handle_user_input_in_query(const String& query_name, const String&); + void handle_user_input_in_server(const String&); + private: struct Message { String prefix; @@ -57,6 +62,7 @@ private: void send_user(); void send_nick(); void send_pong(const String& server); + void send_privmsg(const String& target, const String&); void process_line(); void handle_join(const Message&); void handle_ping(const Message&); @@ -64,6 +70,7 @@ private: void handle_privmsg(const Message&); void handle(const Message&, const String& verbatim); IRCQuery& ensure_query(const String& name); + IRCChannel& ensure_channel(const String& name); String m_hostname; int m_port { 0 }; diff --git a/Applications/IRCClient/IRCClientWindow.cpp b/Applications/IRCClient/IRCClientWindow.cpp index 374b277b37..c4eb12ae80 100644 --- a/Applications/IRCClient/IRCClientWindow.cpp +++ b/Applications/IRCClient/IRCClientWindow.cpp @@ -3,6 +3,7 @@ #include "IRCLogBufferModel.h" #include <LibGUI/GBoxLayout.h> #include <LibGUI/GTableView.h> +#include <LibGUI/GTextEditor.h> #include <LibGUI/GTextBox.h> IRCClientWindow::IRCClientWindow(IRCClient& client, Type type, const String& name, GWidget* parent) @@ -16,6 +17,19 @@ IRCClientWindow::IRCClientWindow(IRCClient& client, Type type, const String& nam m_table_view->set_headers_visible(false); m_table_view->set_font(Font::default_fixed_width_font()); + m_text_editor = new GTextEditor(GTextEditor::SingleLine, this); + m_text_editor->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); + m_text_editor->set_preferred_size({ 0, 18 }); + m_text_editor->on_return_pressed = [this] (GTextEditor& editor) { + if (m_type == Channel) + m_client.handle_user_input_in_channel(m_name, editor.text()); + else if (m_type == Query) + m_client.handle_user_input_in_query(m_name, editor.text()); + else if (m_type == Server) + m_client.handle_user_input_in_server(editor.text()); + m_text_editor->clear(); + }; + m_client.register_subwindow(*this); } diff --git a/Applications/IRCClient/IRCClientWindow.h b/Applications/IRCClient/IRCClientWindow.h index a8ef445617..4a31316e47 100644 --- a/Applications/IRCClient/IRCClientWindow.h +++ b/Applications/IRCClient/IRCClientWindow.h @@ -5,6 +5,7 @@ class IRCClient; class IRCLogBuffer; class GTableView; +class GTextEditor; class IRCClientWindow : public GWidget { public: @@ -29,5 +30,6 @@ private: Type m_type; String m_name; GTableView* m_table_view { nullptr }; + GTextEditor* m_text_editor { nullptr }; RetainPtr<IRCLogBuffer> m_log_buffer; }; diff --git a/Applications/IRCClient/IRCLogBufferModel.cpp b/Applications/IRCClient/IRCLogBufferModel.cpp index 4eb170d858..69835aaee9 100644 --- a/Applications/IRCClient/IRCLogBufferModel.cpp +++ b/Applications/IRCClient/IRCLogBufferModel.cpp @@ -2,6 +2,7 @@ #include "IRCLogBuffer.h" #include <stdio.h> #include <time.h> +#include <SharedGraphics/Font.h> IRCLogBufferModel::IRCLogBufferModel(Retained<IRCLogBuffer>&& log_buffer) : m_log_buffer(move(log_buffer)) @@ -19,14 +20,13 @@ int IRCLogBufferModel::row_count() const int IRCLogBufferModel::column_count() const { - return 4; + return Column::__Count; } String IRCLogBufferModel::column_name(int column) const { switch (column) { case Column::Timestamp: return "Time"; - case Column::Prefix: return "@"; case Column::Name: return "Name"; case Column::Text: return "Text"; } @@ -37,8 +37,7 @@ GTableModel::ColumnMetadata IRCLogBufferModel::column_metadata(int column) const { switch (column) { case Column::Timestamp: return { 60, TextAlignment::CenterLeft }; - case Column::Prefix: return { 10, TextAlignment::CenterLeft }; - case Column::Name: return { 70, TextAlignment::CenterRight }; + case Column::Name: return { 70, TextAlignment::CenterRight, &Font::default_bold_font() }; case Column::Text: return { 800, TextAlignment::CenterLeft }; } ASSERT_NOT_REACHED(); @@ -52,12 +51,7 @@ GVariant IRCLogBufferModel::data(const GModelIndex& index, Role) const auto* tm = localtime(&entry.timestamp); return String::format("%02u:%02u:%02u", tm->tm_hour, tm->tm_min, tm->tm_sec); } - case Column::Prefix: { - if (!entry.prefix) - return String(""); - return String(&entry.prefix, 1); - } - case Column::Name: return entry.sender; + case Column::Name: return String::format("<%c%s>", entry.prefix ? entry.prefix : ' ', entry.sender.characters()); case Column::Text: return entry.text; } ASSERT_NOT_REACHED(); diff --git a/Applications/IRCClient/IRCLogBufferModel.h b/Applications/IRCClient/IRCLogBufferModel.h index 5f5fe30c89..7605ca3bab 100644 --- a/Applications/IRCClient/IRCLogBufferModel.h +++ b/Applications/IRCClient/IRCLogBufferModel.h @@ -8,9 +8,9 @@ class IRCLogBufferModel final : public GTableModel { public: enum Column { Timestamp = 0, - Prefix, Name, Text, + __Count, }; explicit IRCLogBufferModel(Retained<IRCLogBuffer>&&); |