IRCWindowListModel.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "IRCWindowListModel.h"
  2. #include "IRCWindow.h"
  3. #include "IRCClient.h"
  4. #include "IRCChannel.h"
  5. #include <stdio.h>
  6. #include <time.h>
  7. IRCWindowListModel::IRCWindowListModel(IRCClient& client)
  8. : m_client(client)
  9. {
  10. set_activates_on_selection(true);
  11. }
  12. IRCWindowListModel::~IRCWindowListModel()
  13. {
  14. }
  15. int IRCWindowListModel::row_count(const GModelIndex&) const
  16. {
  17. return m_client.window_count();
  18. }
  19. int IRCWindowListModel::column_count(const GModelIndex&) const
  20. {
  21. return 1;
  22. }
  23. String IRCWindowListModel::column_name(int column) const
  24. {
  25. switch (column) {
  26. case Column::Name: return "Name";
  27. }
  28. ASSERT_NOT_REACHED();
  29. }
  30. GModel::ColumnMetadata IRCWindowListModel::column_metadata(int column) const
  31. {
  32. switch (column) {
  33. case Column::Name: return { 70, TextAlignment::CenterLeft };
  34. }
  35. ASSERT_NOT_REACHED();
  36. }
  37. GVariant IRCWindowListModel::data(const GModelIndex& index, Role role) const
  38. {
  39. if (role == Role::Display) {
  40. switch (index.column()) {
  41. case Column::Name: {
  42. auto& window = m_client.window_at(index.row());
  43. if (window.unread_count())
  44. return String::format("%s (%d)", window.name().characters(), window.unread_count());
  45. return window.name();
  46. }
  47. }
  48. }
  49. if (role == Role::ForegroundColor) {
  50. switch (index.column()) {
  51. case Column::Name: {
  52. auto& window = m_client.window_at(index.row());
  53. if (window.unread_count())
  54. return Color(Color::Red);
  55. if (!window.channel().is_open())
  56. return Color(Color::LightGray);
  57. return Color(Color::Black);
  58. }
  59. }
  60. }
  61. return { };
  62. }
  63. void IRCWindowListModel::update()
  64. {
  65. did_update();
  66. }
  67. void IRCWindowListModel::activate(const GModelIndex& index)
  68. {
  69. if (on_activation)
  70. on_activation(m_client.window_at(index.row()));
  71. }