ClipboardHistoryModel.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. * Copyright (c) 2021, Mustafa Quraish <mustafa@cs.toronto.edu>
  4. * Copyright (c) 2022-2023, the SerenityOS developers.
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include "ClipboardHistoryModel.h"
  9. #include <AK/NumberFormat.h>
  10. #include <AK/StringBuilder.h>
  11. #include <LibConfig/Client.h>
  12. NonnullRefPtr<ClipboardHistoryModel> ClipboardHistoryModel::create()
  13. {
  14. return adopt_ref(*new ClipboardHistoryModel());
  15. }
  16. ClipboardHistoryModel::ClipboardHistoryModel()
  17. : m_history_limit(Config::read_i32("ClipboardHistory"sv, "ClipboardHistory"sv, "NumHistoryItems"sv, 20))
  18. {
  19. }
  20. DeprecatedString ClipboardHistoryModel::column_name(int column) const
  21. {
  22. switch (column) {
  23. case Column::Data:
  24. return "Data";
  25. case Column::Type:
  26. return "Type";
  27. case Column::Size:
  28. return "Size";
  29. case Column::Time:
  30. return "Time";
  31. default:
  32. VERIFY_NOT_REACHED();
  33. }
  34. }
  35. static StringView bpp_for_format_resilient(DeprecatedString format)
  36. {
  37. unsigned format_uint = format.to_uint().value_or(static_cast<unsigned>(Gfx::BitmapFormat::Invalid));
  38. // Cannot use Gfx::Bitmap::bpp_for_format here, as we have to accept invalid enum values.
  39. switch (static_cast<Gfx::BitmapFormat>(format_uint)) {
  40. case Gfx::BitmapFormat::Indexed1:
  41. return "1"sv;
  42. case Gfx::BitmapFormat::Indexed2:
  43. return "2"sv;
  44. case Gfx::BitmapFormat::Indexed4:
  45. return "4"sv;
  46. case Gfx::BitmapFormat::Indexed8:
  47. return "8"sv;
  48. case Gfx::BitmapFormat::BGRx8888:
  49. case Gfx::BitmapFormat::BGRA8888:
  50. return "32"sv;
  51. case Gfx::BitmapFormat::Invalid:
  52. /* fall-through */
  53. default:
  54. return "?"sv;
  55. }
  56. }
  57. GUI::Variant ClipboardHistoryModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
  58. {
  59. if (role != GUI::ModelRole::Display)
  60. return {};
  61. auto& item = m_history_items[index.row()];
  62. auto& data_and_type = item.data_and_type;
  63. auto& time = item.time;
  64. switch (index.column()) {
  65. case Column::Data:
  66. if (data_and_type.mime_type.starts_with("text/"sv))
  67. return DeprecatedString::copy(data_and_type.data);
  68. if (data_and_type.mime_type == "image/x-serenityos") {
  69. StringBuilder builder;
  70. builder.append('[');
  71. builder.append(data_and_type.metadata.get("width").value_or("?"));
  72. builder.append('x');
  73. builder.append(data_and_type.metadata.get("height").value_or("?"));
  74. builder.append('x');
  75. builder.append(bpp_for_format_resilient(data_and_type.metadata.get("format").value_or("0")));
  76. builder.append(']');
  77. builder.append(" bitmap"sv);
  78. return builder.to_deprecated_string();
  79. }
  80. if (data_and_type.mime_type.starts_with("glyph/"sv)) {
  81. StringBuilder builder;
  82. auto count = data_and_type.metadata.get("count").value().to_uint().value_or(0);
  83. auto start = data_and_type.metadata.get("start").value().to_uint().value_or(0);
  84. auto width = data_and_type.metadata.get("width").value().to_uint().value_or(0);
  85. auto height = data_and_type.metadata.get("height").value().to_uint().value_or(0);
  86. if (count > 1) {
  87. builder.appendff("U+{:04X}..U+{:04X} ({} glyphs) [{}x{}]", start, start + count - 1, count, width, height);
  88. } else {
  89. builder.appendff("U+{:04X} (", start);
  90. builder.append_code_point(start);
  91. builder.appendff(") [{}x{}]", width, height);
  92. }
  93. return builder.to_deprecated_string();
  94. }
  95. return "<...>";
  96. case Column::Type:
  97. return data_and_type.mime_type;
  98. case Column::Size:
  99. return AK::human_readable_size(data_and_type.data.size());
  100. case Column::Time:
  101. return time.to_deprecated_string();
  102. default:
  103. VERIFY_NOT_REACHED();
  104. }
  105. }
  106. void ClipboardHistoryModel::clipboard_content_did_change(DeprecatedString const&)
  107. {
  108. auto data_and_type = GUI::Clipboard::the().fetch_data_and_type();
  109. if (!(data_and_type.data.is_empty() && data_and_type.mime_type.is_empty() && data_and_type.metadata.is_empty()))
  110. add_item(data_and_type);
  111. }
  112. void ClipboardHistoryModel::add_item(const GUI::Clipboard::DataAndType& item)
  113. {
  114. m_history_items.remove_first_matching([&](ClipboardItem& existing) {
  115. return existing.data_and_type.data == item.data && existing.data_and_type.mime_type == item.mime_type;
  116. });
  117. if (m_history_items.size() == m_history_limit)
  118. m_history_items.take_last();
  119. m_history_items.prepend({ item, Core::DateTime::now() });
  120. invalidate();
  121. }
  122. void ClipboardHistoryModel::remove_item(int index)
  123. {
  124. m_history_items.remove(index);
  125. invalidate();
  126. }
  127. void ClipboardHistoryModel::clear()
  128. {
  129. m_history_items.clear();
  130. invalidate();
  131. }
  132. void ClipboardHistoryModel::config_string_did_change(DeprecatedString const& domain, DeprecatedString const& group, DeprecatedString const& key, DeprecatedString const& value_string)
  133. {
  134. if (domain != "ClipboardHistory" || group != "ClipboardHistory")
  135. return;
  136. // FIXME: Once we can get notified for `i32` changes, we can use that instead of this hack.
  137. if (key == "NumHistoryItems") {
  138. auto value_or_error = value_string.to_int();
  139. if (!value_or_error.has_value())
  140. return;
  141. auto value = value_or_error.value();
  142. if (value < (int)m_history_items.size()) {
  143. m_history_items.remove(value, m_history_items.size() - value);
  144. invalidate();
  145. }
  146. m_history_limit = value;
  147. return;
  148. }
  149. }