Skip to content

Commit

Permalink
IRClient: Add a member list to channel views.
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomekling committed Mar 15, 2019
1 parent 08c15be commit 491aa11
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Applications/IRCClient/IRCChannel.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "IRCChannel.h"
#include "IRCClient.h"
#include "IRCChannelMemberListModel.h"
#include <stdio.h>
#include <time.h>

Expand All @@ -8,6 +9,7 @@ IRCChannel::IRCChannel(IRCClient& client, const String& name)
, m_name(name)
, m_log(IRCLogBuffer::create())
{
m_member_model = new IRCChannelMemberListModel(*this);
}

IRCChannel::~IRCChannel()
Expand Down
8 changes: 8 additions & 0 deletions Applications/IRCClient/IRCChannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "IRCLogBuffer.h"

class IRCClient;
class IRCChannelMemberListModel;

class IRCChannel : public Retainable<IRCChannel> {
public:
Expand All @@ -31,6 +32,12 @@ class IRCChannel : public Retainable<IRCChannel> {
const IRCLogBuffer& log() const { return *m_log; }
IRCLogBuffer& log() { return *m_log; }

IRCChannelMemberListModel* member_model() { return m_member_model; }
const IRCChannelMemberListModel* member_model() const { return m_member_model; }

int member_count() const { return m_members.size(); }
String member_at(int i) { return m_members[i].name; }

private:
IRCChannel(IRCClient&, const String&);

Expand All @@ -44,4 +51,5 @@ class IRCChannel : public Retainable<IRCChannel> {
bool m_open { false };

Retained<IRCLogBuffer> m_log;
IRCChannelMemberListModel* m_member_model { nullptr };
};
59 changes: 59 additions & 0 deletions Applications/IRCClient/IRCChannelMemberListModel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "IRCChannelMemberListModel.h"
#include "IRCChannel.h"
#include <stdio.h>
#include <time.h>

IRCChannelMemberListModel::IRCChannelMemberListModel(IRCChannel& channel)
: m_channel(channel)
{
set_activates_on_selection(true);
}

IRCChannelMemberListModel::~IRCChannelMemberListModel()
{
}

int IRCChannelMemberListModel::row_count() const
{
return m_channel.member_count();
}

int IRCChannelMemberListModel::column_count() const
{
return 1;
}

String IRCChannelMemberListModel::column_name(int column) const
{
switch (column) {
case Column::Name: return "Name";
}
ASSERT_NOT_REACHED();
}

GTableModel::ColumnMetadata IRCChannelMemberListModel::column_metadata(int column) const
{
switch (column) {
case Column::Name: return { 70, TextAlignment::CenterLeft };
}
ASSERT_NOT_REACHED();
}

GVariant IRCChannelMemberListModel::data(const GModelIndex& index, Role) const
{
switch (index.column()) {
case Column::Name: return m_channel.member_at(index.row());
}
ASSERT_NOT_REACHED();
}

void IRCChannelMemberListModel::update()
{
did_update();
}

void IRCChannelMemberListModel::activate(const GModelIndex& index)
{
if (on_activation)
on_activation(m_channel.member_at(index.row()));
}
26 changes: 26 additions & 0 deletions Applications/IRCClient/IRCChannelMemberListModel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <LibGUI/GTableModel.h>
#include <AK/Function.h>

class IRCChannel;

class IRCChannelMemberListModel final : public GTableModel {
public:
enum Column { Name };
explicit IRCChannelMemberListModel(IRCChannel&);
virtual ~IRCChannelMemberListModel() override;

virtual int row_count() const override;
virtual int column_count() const override;
virtual String column_name(int column) const override;
virtual ColumnMetadata column_metadata(int column) const override;
virtual GVariant data(const GModelIndex&, Role = Role::Display) const override;
virtual void update() override;
virtual void activate(const GModelIndex&) override;

Function<void(const String&)> on_activation;

private:
IRCChannel& m_channel;
};
5 changes: 3 additions & 2 deletions Applications/IRCClient/IRCClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class IRCClient {
void handle_user_input_in_query(const String& query_name, const String&);
void handle_user_input_in_server(const String&);

IRCQuery& ensure_query(const String& name);
IRCChannel& ensure_channel(const String& name);

private:
struct Message {
String prefix;
Expand All @@ -70,8 +73,6 @@ class IRCClient {
void handle_namreply(const Message&);
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 };
Expand Down
17 changes: 16 additions & 1 deletion Applications/IRCClient/IRCClientWindow.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "IRCClientWindow.h"
#include "IRCClient.h"
#include "IRCChannel.h"
#include "IRCChannelMemberListModel.h"
#include "IRCLogBufferModel.h"
#include <LibGUI/GBoxLayout.h>
#include <LibGUI/GTableView.h>
Expand All @@ -13,10 +15,23 @@ IRCClientWindow::IRCClientWindow(IRCClient& client, Type type, const String& nam
, m_name(name)
{
set_layout(make<GBoxLayout>(Orientation::Vertical));
m_table_view = new GTableView(this);

// Make a container for the log buffer view + optional member list.
GWidget* container = new GWidget(this);
container->set_layout(make<GBoxLayout>(Orientation::Horizontal));

m_table_view = new GTableView(container);
m_table_view->set_headers_visible(false);
m_table_view->set_font(Font::default_fixed_width_font());

if (m_type == Channel) {
auto* member_view = new GTableView(container);
member_view->set_headers_visible(false);
member_view->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
member_view->set_preferred_size({ 100, 0 });
member_view->set_model(OwnPtr<IRCChannelMemberListModel>(m_client.ensure_channel(m_name).member_model()));
}

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 });
Expand Down
1 change: 1 addition & 0 deletions Applications/IRCClient/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ OBJS = \
IRCAppWindow.o \
IRCClientWindow.o \
IRCClientWindowListModel.o \
IRCChannelMemberListModel.o \
main.o

APP = IRCClient
Expand Down

0 comments on commit 491aa11

Please sign in to comment.