summaryrefslogtreecommitdiff
path: root/Applications/IRCClient/IRCWindowListModel.cpp
blob: 2adb2ef360e73ae301c9cd44d2389d06139a058d (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
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "IRCWindowListModel.h"
#include "IRCWindow.h"
#include "IRCClient.h"
#include "IRCChannel.h"
#include <stdio.h>
#include <time.h>

IRCWindowListModel::IRCWindowListModel(IRCClient& client)
    : m_client(client)
{
    set_activates_on_selection(true);
}

IRCWindowListModel::~IRCWindowListModel()
{
}

int IRCWindowListModel::row_count(const GModelIndex&) const
{
    return m_client.window_count();
}

int IRCWindowListModel::column_count(const GModelIndex&) const
{
    return 1;
}

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

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

GVariant IRCWindowListModel::data(const GModelIndex& index, Role role) const
{
    if (role == Role::Display) {
        switch (index.column()) {
        case Column::Name: {
            auto& window = m_client.window_at(index.row());
            if (window.unread_count())
                return String::format("%s (%d)", window.name().characters(), window.unread_count());
            return window.name();
        }
        }
    }
    if (role == Role::ForegroundColor) {
        switch (index.column()) {
        case Column::Name: {
            auto& window = m_client.window_at(index.row());
            if (window.unread_count())
                return Color(Color::Red);
            if (!window.channel().is_open())
                return Color(Color::LightGray);
            return Color(Color::Black);
        }
        }
    }
    return { };
}

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

void IRCWindowListModel::activate(const GModelIndex& index)
{
    if (on_activation)
        on_activation(m_client.window_at(index.row()));
}