Skip to content

Commit aa19735

Browse files
committed
IRCClient: Start working on a simple graphical IRC client.
This will be a nice way to exercise both LibGUI and the TCP/IP support. :^)
1 parent f87dec1 commit aa19735

18 files changed

+779
-1
lines changed

Applications/IRCClient/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.o
2+
*.d
3+
IRCClient
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include "IRCAppWindow.h"
2+
#include "IRCSubWindow.h"
3+
#include <LibGUI/GListBox.h>
4+
#include <LibGUI/GBoxLayout.h>
5+
6+
IRCAppWindow::IRCAppWindow()
7+
: GWindow()
8+
, m_client("127.0.0.1", 6667)
9+
{
10+
set_title(String::format("IRC Client: %s:%d", m_client.hostname().characters(), m_client.port()));
11+
set_rect(200, 200, 600, 400);
12+
setup_client();
13+
setup_widgets();
14+
}
15+
16+
IRCAppWindow::~IRCAppWindow()
17+
{
18+
}
19+
20+
void IRCAppWindow::setup_client()
21+
{
22+
m_client.on_connect = [this] {
23+
m_client.join_channel("#test");
24+
};
25+
26+
m_client.on_query_message = [this] (const String& name) {
27+
// FIXME: Update query view.
28+
};
29+
30+
m_client.on_channel_message = [this] (const String& channel_name) {
31+
// FIXME: Update channel view.
32+
};
33+
34+
m_client.connect();
35+
}
36+
37+
void IRCAppWindow::setup_widgets()
38+
{
39+
auto* widget = new GWidget(nullptr);
40+
widget->set_fill_with_background_color(true);
41+
set_main_widget(widget);
42+
widget->set_layout(make<GBoxLayout>(Orientation::Horizontal));
43+
44+
auto* subwindow_list = new GListBox(widget);
45+
subwindow_list->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
46+
subwindow_list->set_preferred_size({ 120, 0 });
47+
subwindow_list->add_item("test1");
48+
subwindow_list->add_item("test2");
49+
subwindow_list->add_item("test3");
50+
51+
auto* container = new GWidget(widget);
52+
53+
auto* subwindow = new IRCSubWindow("Server", container);
54+
}

Applications/IRCClient/IRCAppWindow.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#include <LibGUI/GWindow.h>
4+
#include <LibGUI/GWidget.h>
5+
#include "IRCClient.h"
6+
7+
class IRCAppWindow : public GWindow {
8+
public:
9+
IRCAppWindow();
10+
virtual ~IRCAppWindow() override;
11+
12+
private:
13+
void setup_client();
14+
void setup_widgets();
15+
16+
IRCClient m_client;
17+
};

Applications/IRCClient/IRCChannel.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "IRCChannel.h"
2+
#include "IRCClient.h"
3+
#include <stdio.h>
4+
#include <time.h>
5+
6+
IRCChannel::IRCChannel(IRCClient& client, const String& name)
7+
: m_client(client)
8+
, m_name(name)
9+
, m_log(IRCLogBuffer::create())
10+
{
11+
}
12+
13+
IRCChannel::~IRCChannel()
14+
{
15+
}
16+
17+
Retained<IRCChannel> IRCChannel::create(IRCClient& client, const String& name)
18+
{
19+
return adopt(*new IRCChannel(client, name));
20+
}
21+
22+
void IRCChannel::add_member(const String& name, char prefix)
23+
{
24+
for (auto& member : m_members) {
25+
if (member.name == name) {
26+
member.prefix = prefix;
27+
return;
28+
}
29+
}
30+
m_members.append({ name, prefix });
31+
dump();
32+
}
33+
34+
void IRCChannel::add_message(char prefix, const String& name, const String& text)
35+
{
36+
log().add_message(prefix, name, text);
37+
dump();
38+
}
39+
40+
void IRCChannel::dump() const
41+
{
42+
printf("IRCChannel{%p}: %s\n", this, m_name.characters());
43+
for (auto& member : m_members) {
44+
printf(" (%c)%s\n", member.prefix ? member.prefix : ' ', member.name.characters());
45+
}
46+
log().dump();
47+
}

Applications/IRCClient/IRCChannel.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#pragma once
2+
3+
#include <AK/AKString.h>
4+
#include <AK/CircularQueue.h>
5+
#include <AK/Vector.h>
6+
#include <AK/Retainable.h>
7+
#include <AK/RetainPtr.h>
8+
#include "IRCLogBuffer.h"
9+
10+
class IRCClient;
11+
12+
class IRCChannel : public Retainable<IRCChannel> {
13+
public:
14+
static Retained<IRCChannel> create(IRCClient&, const String&);
15+
~IRCChannel();
16+
17+
bool is_open() const { return m_open; }
18+
void set_open(bool b) { m_open = b; }
19+
20+
String name() const { return m_name; }
21+
22+
void add_member(const String& name, char prefix);
23+
void remove_member(const String& name);
24+
25+
void add_message(char prefix, const String& name, const String& text);
26+
27+
void dump() const;
28+
29+
const IRCLogBuffer& log() const { return *m_log; }
30+
IRCLogBuffer& log() { return *m_log; }
31+
32+
private:
33+
IRCChannel(IRCClient&, const String&);
34+
35+
IRCClient& m_client;
36+
String m_name;
37+
struct Member {
38+
String name;
39+
char prefix { 0 };
40+
};
41+
Vector<Member> m_members;
42+
bool m_open { false };
43+
44+
Retained<IRCLogBuffer> m_log;
45+
};

0 commit comments

Comments
 (0)