Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "IRCWindowListModel.h"
28#include "IRCChannel.h"
29#include "IRCClient.h"
30#include "IRCWindow.h"
31#include <stdio.h>
32#include <time.h>
33
34IRCWindowListModel::IRCWindowListModel(IRCClient& client)
35 : m_client(client)
36{
37}
38
39IRCWindowListModel::~IRCWindowListModel()
40{
41}
42
43int IRCWindowListModel::row_count(const GUI::ModelIndex&) const
44{
45 return m_client.window_count();
46}
47
48int IRCWindowListModel::column_count(const GUI::ModelIndex&) const
49{
50 return 1;
51}
52
53String IRCWindowListModel::column_name(int column) const
54{
55 switch (column) {
56 case Column::Name:
57 return "Name";
58 }
59 ASSERT_NOT_REACHED();
60}
61
62GUI::Model::ColumnMetadata IRCWindowListModel::column_metadata(int column) const
63{
64 switch (column) {
65 case Column::Name:
66 return { 70, Gfx::TextAlignment::CenterLeft };
67 }
68 ASSERT_NOT_REACHED();
69}
70
71GUI::Variant IRCWindowListModel::data(const GUI::ModelIndex& index, Role role) const
72{
73 if (role == Role::Display) {
74 switch (index.column()) {
75 case Column::Name: {
76 auto& window = m_client.window_at(index.row());
77 if (window.unread_count())
78 return String::format("%s (%d)", window.name().characters(), window.unread_count());
79 return window.name();
80 }
81 }
82 }
83 if (role == Role::ForegroundColor) {
84 switch (index.column()) {
85 case Column::Name: {
86 auto& window = m_client.window_at(index.row());
87 if (window.unread_count())
88 return Color(Color::Red);
89 if (!window.channel().is_open())
90 return Color(Color::WarmGray);
91 return Color(Color::Black);
92 }
93 }
94 }
95 return {};
96}
97
98void IRCWindowListModel::update()
99{
100 did_update();
101}