1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 03:57:44 +00:00

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.
This commit is contained in:
Andreas Kling 2019-03-15 17:37:13 +01:00
parent ad08165a25
commit 1fc283ed7d
12 changed files with 126 additions and 23 deletions

View file

@ -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);
}

View file

@ -26,6 +26,8 @@ public:
void dump() const;
void say(const String&);
const IRCLogBuffer& log() const { return *m_log; }
IRCLogBuffer& log() { return *m_log; }

View file

@ -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);
}

View file

@ -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 };

View file

@ -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);
}

View file

@ -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;
};

View file

@ -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();

View file

@ -8,9 +8,9 @@ class IRCLogBufferModel final : public GTableModel {
public:
enum Column {
Timestamp = 0,
Prefix,
Name,
Text,
__Count,
};
explicit IRCLogBufferModel(Retained<IRCLogBuffer>&&);