ClipboardHistoryModel.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@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. #include "ClipboardHistoryModel.h"
  27. #include <AK/NumberFormat.h>
  28. #include <AK/StringBuilder.h>
  29. NonnullRefPtr<ClipboardHistoryModel> ClipboardHistoryModel::create()
  30. {
  31. return adopt(*new ClipboardHistoryModel());
  32. }
  33. ClipboardHistoryModel::~ClipboardHistoryModel()
  34. {
  35. }
  36. String ClipboardHistoryModel::column_name(int column) const
  37. {
  38. switch (column) {
  39. case Column::Data:
  40. return "Data";
  41. case Column::Type:
  42. return "Type";
  43. case Column::Size:
  44. return "Size";
  45. default:
  46. VERIFY_NOT_REACHED();
  47. }
  48. }
  49. static const char* bpp_for_format_resilient(String format)
  50. {
  51. unsigned format_uint = format.to_uint().value_or(static_cast<unsigned>(Gfx::BitmapFormat::Invalid));
  52. // Cannot use Gfx::Bitmap::bpp_for_format here, as we have to accept invalid enum values.
  53. switch (static_cast<Gfx::BitmapFormat>(format_uint)) {
  54. case Gfx::BitmapFormat::Indexed1:
  55. return "1";
  56. case Gfx::BitmapFormat::Indexed2:
  57. return "2";
  58. case Gfx::BitmapFormat::Indexed4:
  59. return "4";
  60. case Gfx::BitmapFormat::Indexed8:
  61. return "8";
  62. case Gfx::BitmapFormat::BGRx8888:
  63. case Gfx::BitmapFormat::BGRA8888:
  64. return "32";
  65. case Gfx::BitmapFormat::Invalid:
  66. /* fall-through */
  67. default:
  68. return "?";
  69. }
  70. }
  71. GUI::Variant ClipboardHistoryModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
  72. {
  73. if (role != GUI::ModelRole::Display)
  74. return {};
  75. auto& data_and_type = m_history_items[index.row()];
  76. switch (index.column()) {
  77. case Column::Data:
  78. if (data_and_type.mime_type.starts_with("text/"))
  79. return String::copy(data_and_type.data);
  80. if (data_and_type.mime_type == "image/x-serenityos") {
  81. StringBuilder builder;
  82. builder.append("[");
  83. builder.append(data_and_type.metadata.get("width").value_or("?"));
  84. builder.append('x');
  85. builder.append(data_and_type.metadata.get("height").value_or("?"));
  86. builder.append('x');
  87. builder.append(bpp_for_format_resilient(data_and_type.metadata.get("height").value_or("0")));
  88. builder.append(" bitmap");
  89. builder.append("]");
  90. return builder.to_string();
  91. }
  92. if (data_and_type.mime_type.starts_with("glyph/")) {
  93. StringBuilder builder;
  94. builder.append("[");
  95. builder.append(data_and_type.metadata.get("width").value_or("?"));
  96. builder.append("x");
  97. builder.append(data_and_type.metadata.get("height").value_or("?"));
  98. builder.append("] ");
  99. builder.append("(");
  100. builder.append(data_and_type.metadata.get("char").value_or(""));
  101. builder.append(")");
  102. return builder.to_string();
  103. }
  104. return "<...>";
  105. case Column::Type:
  106. return data_and_type.mime_type;
  107. case Column::Size:
  108. return AK::human_readable_size(data_and_type.data.size());
  109. default:
  110. VERIFY_NOT_REACHED();
  111. }
  112. }
  113. void ClipboardHistoryModel::update()
  114. {
  115. did_update();
  116. }
  117. void ClipboardHistoryModel::add_item(const GUI::Clipboard::DataAndType& item)
  118. {
  119. m_history_items.remove_first_matching([&](GUI::Clipboard::DataAndType& existing) {
  120. return existing.data == item.data && existing.mime_type == item.mime_type;
  121. });
  122. if (m_history_items.size() == m_history_limit)
  123. m_history_items.take_last();
  124. m_history_items.prepend(item);
  125. update();
  126. }
  127. void ClipboardHistoryModel::remove_item(int index)
  128. {
  129. m_history_items.remove(index);
  130. }