summaryrefslogtreecommitdiff
path: root/Applications/IRCClient/IRCLogBufferModel.cpp
blob: 69835aaee92603213d1dfc0bae6c6927f2cd6049 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "IRCLogBufferModel.h"
#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))
{
}

IRCLogBufferModel::~IRCLogBufferModel()
{
}

int IRCLogBufferModel::row_count() const
{
    return m_log_buffer->count();
}

int IRCLogBufferModel::column_count() const
{
    return Column::__Count;
}

String IRCLogBufferModel::column_name(int column) const
{
    switch (column) {
    case Column::Timestamp: return "Time";
    case Column::Name: return "Name";
    case Column::Text: return "Text";
    }
    ASSERT_NOT_REACHED();
}

GTableModel::ColumnMetadata IRCLogBufferModel::column_metadata(int column) const
{
    switch (column) {
    case Column::Timestamp: return { 60, TextAlignment::CenterLeft };
    case Column::Name: return { 70, TextAlignment::CenterRight, &Font::default_bold_font() };
    case Column::Text: return { 800, TextAlignment::CenterLeft };
    }
    ASSERT_NOT_REACHED();
}

GVariant IRCLogBufferModel::data(const GModelIndex& index, Role) const
{
    auto& entry = m_log_buffer->at(index.row());
    switch (index.column()) {
    case Column::Timestamp: {
        auto* tm = localtime(&entry.timestamp);
        return String::format("%02u:%02u:%02u", tm->tm_hour, tm->tm_min, tm->tm_sec);
    }
    case Column::Name: return String::format("<%c%s>", entry.prefix ? entry.prefix : ' ', entry.sender.characters());
    case Column::Text: return entry.text;
    }
    ASSERT_NOT_REACHED();
}

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

void IRCLogBufferModel::activate(const GModelIndex&)
{
}