IRCWindowListModel.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "IRCWindowListModel.h"
  7. #include "IRCChannel.h"
  8. #include "IRCClient.h"
  9. IRCWindowListModel::IRCWindowListModel(IRCClient& client)
  10. : m_client(client)
  11. {
  12. }
  13. IRCWindowListModel::~IRCWindowListModel()
  14. {
  15. }
  16. int IRCWindowListModel::row_count(const GUI::ModelIndex&) const
  17. {
  18. return m_client->window_count();
  19. }
  20. int IRCWindowListModel::column_count(const GUI::ModelIndex&) const
  21. {
  22. return 1;
  23. }
  24. String IRCWindowListModel::column_name(int column) const
  25. {
  26. switch (column) {
  27. case Column::Name:
  28. return "Name";
  29. }
  30. VERIFY_NOT_REACHED();
  31. }
  32. GUI::Variant IRCWindowListModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
  33. {
  34. if (role == GUI::ModelRole::TextAlignment)
  35. return Gfx::TextAlignment::CenterLeft;
  36. if (role == GUI::ModelRole::Display) {
  37. switch (index.column()) {
  38. case Column::Name: {
  39. auto& window = m_client->window_at(index.row());
  40. if (window.unread_count())
  41. return String::formatted("{} ({})", window.name(), window.unread_count());
  42. return window.name();
  43. }
  44. }
  45. }
  46. if (role == GUI::ModelRole::ForegroundColor) {
  47. switch (index.column()) {
  48. case Column::Name: {
  49. auto& window = m_client->window_at(index.row());
  50. if (window.unread_count())
  51. return Color(Color::Red);
  52. if (!window.channel().is_open())
  53. return Color(Color::WarmGray);
  54. return Color(Color::Black);
  55. }
  56. }
  57. }
  58. return {};
  59. }