2020-05-15 19:37:31 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
2021-08-27 02:24:09 +00:00
|
|
|
* Copyright (c) 2021, Mustafa Quraish <mustafa@cs.toronto.edu>
|
2023-01-15 14:09:10 +00:00
|
|
|
* Copyright (c) 2022-2023, the SerenityOS developers.
|
2020-05-15 19:37:31 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-15 19:37:31 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ClipboardHistoryModel.h"
|
2023-03-08 21:13:42 +00:00
|
|
|
#include <AK/JsonParser.h>
|
2020-09-05 15:02:25 +00:00
|
|
|
#include <AK/NumberFormat.h>
|
2024-01-31 16:33:55 +00:00
|
|
|
#include <AK/QuickSort.h>
|
2020-09-05 15:05:22 +00:00
|
|
|
#include <AK/StringBuilder.h>
|
2021-08-27 03:08:06 +00:00
|
|
|
#include <LibConfig/Client.h>
|
2023-05-09 10:48:29 +00:00
|
|
|
#include <LibCore/File.h>
|
2020-05-15 19:37:31 +00:00
|
|
|
|
|
|
|
NonnullRefPtr<ClipboardHistoryModel> ClipboardHistoryModel::create()
|
|
|
|
{
|
2021-04-23 14:46:57 +00:00
|
|
|
return adopt_ref(*new ClipboardHistoryModel());
|
2020-05-15 19:37:31 +00:00
|
|
|
}
|
|
|
|
|
2021-08-27 02:24:09 +00:00
|
|
|
ClipboardHistoryModel::ClipboardHistoryModel()
|
2022-07-11 17:32:29 +00:00
|
|
|
: m_history_limit(Config::read_i32("ClipboardHistory"sv, "ClipboardHistory"sv, "NumHistoryItems"sv, 20))
|
2021-08-27 02:24:09 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-06-13 15:30:15 +00:00
|
|
|
ErrorOr<String> ClipboardHistoryModel::column_name(int column) const
|
2020-05-15 19:37:31 +00:00
|
|
|
{
|
|
|
|
switch (column) {
|
|
|
|
case Column::Data:
|
2023-08-08 02:26:17 +00:00
|
|
|
return "Data"_string;
|
2020-05-15 19:37:31 +00:00
|
|
|
case Column::Type:
|
2023-08-08 02:26:17 +00:00
|
|
|
return "Type"_string;
|
2020-09-05 15:02:25 +00:00
|
|
|
case Column::Size:
|
2023-08-08 02:26:17 +00:00
|
|
|
return "Size"_string;
|
2022-02-22 02:07:28 +00:00
|
|
|
case Column::Time:
|
2023-08-08 02:26:17 +00:00
|
|
|
return "Time"_string;
|
2020-05-15 19:37:31 +00:00
|
|
|
default:
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-05-15 19:37:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
static StringView bpp_for_format_resilient(ByteString format)
|
2020-12-05 19:24:27 +00:00
|
|
|
{
|
2023-12-23 02:59:14 +00:00
|
|
|
unsigned format_uint = format.to_number<unsigned>().value_or(static_cast<unsigned>(Gfx::BitmapFormat::Invalid));
|
2020-12-05 19:24:27 +00:00
|
|
|
// Cannot use Gfx::Bitmap::bpp_for_format here, as we have to accept invalid enum values.
|
|
|
|
switch (static_cast<Gfx::BitmapFormat>(format_uint)) {
|
2021-03-16 10:48:42 +00:00
|
|
|
case Gfx::BitmapFormat::BGRx8888:
|
|
|
|
case Gfx::BitmapFormat::BGRA8888:
|
2022-07-11 17:32:29 +00:00
|
|
|
return "32"sv;
|
2020-12-05 19:24:27 +00:00
|
|
|
case Gfx::BitmapFormat::Invalid:
|
|
|
|
/* fall-through */
|
|
|
|
default:
|
2022-07-11 17:32:29 +00:00
|
|
|
return "?"sv;
|
2020-12-05 19:24:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-16 14:00:07 +00:00
|
|
|
GUI::Variant ClipboardHistoryModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
|
2020-05-15 19:37:31 +00:00
|
|
|
{
|
2020-08-16 14:00:07 +00:00
|
|
|
if (role != GUI::ModelRole::Display)
|
|
|
|
return {};
|
2022-02-22 02:07:28 +00:00
|
|
|
auto& item = m_history_items[index.row()];
|
|
|
|
auto& data_and_type = item.data_and_type;
|
|
|
|
auto& time = item.time;
|
2020-05-15 19:37:31 +00:00
|
|
|
switch (index.column()) {
|
|
|
|
case Column::Data:
|
2022-07-11 17:32:29 +00:00
|
|
|
if (data_and_type.mime_type.starts_with("text/"sv))
|
2023-12-16 14:19:34 +00:00
|
|
|
return ByteString::copy(data_and_type.data);
|
2020-09-05 15:05:22 +00:00
|
|
|
if (data_and_type.mime_type == "image/x-serenityos") {
|
|
|
|
StringBuilder builder;
|
2022-07-11 20:10:18 +00:00
|
|
|
builder.append('[');
|
2020-09-05 15:05:22 +00:00
|
|
|
builder.append(data_and_type.metadata.get("width").value_or("?"));
|
|
|
|
builder.append('x');
|
|
|
|
builder.append(data_and_type.metadata.get("height").value_or("?"));
|
|
|
|
builder.append('x');
|
2021-11-20 14:57:08 +00:00
|
|
|
builder.append(bpp_for_format_resilient(data_and_type.metadata.get("format").value_or("0")));
|
2022-07-11 20:10:18 +00:00
|
|
|
builder.append(']');
|
2022-07-11 17:32:29 +00:00
|
|
|
builder.append(" bitmap"sv);
|
2023-12-16 14:19:34 +00:00
|
|
|
return builder.to_byte_string();
|
2020-09-05 15:05:22 +00:00
|
|
|
}
|
2022-07-11 17:32:29 +00:00
|
|
|
if (data_and_type.mime_type.starts_with("glyph/"sv)) {
|
2021-04-07 16:07:46 +00:00
|
|
|
StringBuilder builder;
|
2023-12-23 02:59:14 +00:00
|
|
|
auto count = data_and_type.metadata.get("count").value().to_number<unsigned>().value_or(0);
|
|
|
|
auto start = data_and_type.metadata.get("start").value().to_number<unsigned>().value_or(0);
|
|
|
|
auto width = data_and_type.metadata.get("width").value().to_number<unsigned>().value_or(0);
|
|
|
|
auto height = data_and_type.metadata.get("height").value().to_number<unsigned>().value_or(0);
|
2022-03-17 12:47:26 +00:00
|
|
|
if (count > 1) {
|
|
|
|
builder.appendff("U+{:04X}..U+{:04X} ({} glyphs) [{}x{}]", start, start + count - 1, count, width, height);
|
|
|
|
} else {
|
|
|
|
builder.appendff("U+{:04X} (", start);
|
|
|
|
builder.append_code_point(start);
|
|
|
|
builder.appendff(") [{}x{}]", width, height);
|
|
|
|
}
|
2023-12-16 14:19:34 +00:00
|
|
|
return builder.to_byte_string();
|
2021-04-07 16:07:46 +00:00
|
|
|
}
|
2020-05-15 19:37:31 +00:00
|
|
|
return "<...>";
|
|
|
|
case Column::Type:
|
2020-09-05 14:16:01 +00:00
|
|
|
return data_and_type.mime_type;
|
2020-09-05 15:02:25 +00:00
|
|
|
case Column::Size:
|
|
|
|
return AK::human_readable_size(data_and_type.data.size());
|
2022-02-22 02:07:28 +00:00
|
|
|
case Column::Time:
|
2023-12-16 14:19:34 +00:00
|
|
|
return time.to_byte_string();
|
2020-05-15 19:37:31 +00:00
|
|
|
default:
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-05-15 19:37:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
void ClipboardHistoryModel::clipboard_content_did_change(ByteString const&)
|
2023-01-15 14:09:10 +00:00
|
|
|
{
|
|
|
|
auto data_and_type = GUI::Clipboard::the().fetch_data_and_type();
|
|
|
|
if (!(data_and_type.data.is_empty() && data_and_type.mime_type.is_empty() && data_and_type.metadata.is_empty()))
|
|
|
|
add_item(data_and_type);
|
|
|
|
}
|
|
|
|
|
2024-02-07 12:33:26 +00:00
|
|
|
ErrorOr<void> ClipboardHistoryModel::invalidate_model_and_file()
|
2023-03-08 21:13:42 +00:00
|
|
|
{
|
|
|
|
invalidate();
|
|
|
|
|
2024-02-07 12:33:26 +00:00
|
|
|
TRY(write_to_file());
|
2023-03-08 21:13:42 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2024-01-31 16:33:55 +00:00
|
|
|
void ClipboardHistoryModel::add_item(GUI::Clipboard::DataAndType const& item)
|
2020-05-15 19:37:31 +00:00
|
|
|
{
|
2022-02-22 02:07:28 +00:00
|
|
|
m_history_items.remove_first_matching([&](ClipboardItem& existing) {
|
|
|
|
return existing.data_and_type.data == item.data && existing.data_and_type.mime_type == item.mime_type;
|
2020-11-17 08:50:39 +00:00
|
|
|
});
|
|
|
|
|
2024-01-31 16:33:55 +00:00
|
|
|
if (m_history_items.size() >= m_history_limit) {
|
|
|
|
m_history_items.resize(m_history_limit - 1);
|
2023-03-08 21:13:42 +00:00
|
|
|
}
|
2020-11-17 08:50:39 +00:00
|
|
|
|
2022-02-22 02:07:28 +00:00
|
|
|
m_history_items.prepend({ item, Core::DateTime::now() });
|
2024-02-07 12:33:26 +00:00
|
|
|
invalidate_model_and_file().release_value_but_fixme_should_propagate_errors();
|
2020-05-15 19:37:31 +00:00
|
|
|
}
|
2020-11-24 20:37:04 +00:00
|
|
|
|
|
|
|
void ClipboardHistoryModel::remove_item(int index)
|
|
|
|
{
|
|
|
|
m_history_items.remove(index);
|
2024-02-07 12:33:26 +00:00
|
|
|
invalidate_model_and_file().release_value_but_fixme_should_propagate_errors();
|
2020-11-24 20:37:04 +00:00
|
|
|
}
|
2021-08-27 03:08:06 +00:00
|
|
|
|
2023-01-14 14:52:16 +00:00
|
|
|
void ClipboardHistoryModel::clear()
|
|
|
|
{
|
|
|
|
m_history_items.clear();
|
2024-02-07 12:33:26 +00:00
|
|
|
invalidate_model_and_file().release_value_but_fixme_should_propagate_errors();
|
2023-01-14 14:52:16 +00:00
|
|
|
}
|
|
|
|
|
2023-06-26 19:07:58 +00:00
|
|
|
void ClipboardHistoryModel::config_i32_did_change(StringView domain, StringView group, StringView key, i32 value)
|
2021-08-27 03:08:06 +00:00
|
|
|
{
|
|
|
|
if (domain != "ClipboardHistory" || group != "ClipboardHistory")
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (key == "NumHistoryItems") {
|
|
|
|
if (value < (int)m_history_items.size()) {
|
|
|
|
m_history_items.remove(value, m_history_items.size() - value);
|
2024-02-07 12:33:26 +00:00
|
|
|
invalidate_model_and_file().release_value_but_fixme_should_propagate_errors();
|
2021-08-27 03:08:06 +00:00
|
|
|
}
|
|
|
|
m_history_limit = value;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2023-03-08 21:13:42 +00:00
|
|
|
|
|
|
|
ErrorOr<ClipboardHistoryModel::ClipboardItem> ClipboardHistoryModel::ClipboardItem::from_json(JsonObject const& object)
|
|
|
|
{
|
|
|
|
if (!object.has("data_and_type"sv) && !object.has("time"sv))
|
|
|
|
return Error::from_string_literal("JsonObject does not contain necessary fields");
|
|
|
|
|
|
|
|
ClipboardItem result;
|
|
|
|
result.data_and_type = TRY(GUI::Clipboard::DataAndType::from_json(*object.get_object("data_and_type"sv)));
|
|
|
|
result.time = Core::DateTime::from_timestamp(*object.get_integer<time_t>("time"sv));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<JsonObject> ClipboardHistoryModel::ClipboardItem::to_json() const
|
|
|
|
{
|
|
|
|
JsonObject object;
|
|
|
|
|
|
|
|
object.set("data_and_type", TRY(data_and_type.to_json()));
|
|
|
|
object.set("time", time.timestamp());
|
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ErrorOr<void> ClipboardHistoryModel::read_from_file(ByteString const& path)
|
2023-03-08 21:13:42 +00:00
|
|
|
{
|
|
|
|
m_path = path;
|
|
|
|
|
|
|
|
auto read_from_file_impl = [this]() -> ErrorOr<void> {
|
|
|
|
auto file = TRY(Core::File::open(m_path, Core::File::OpenMode::Read));
|
2024-02-07 12:33:26 +00:00
|
|
|
auto contents = TRY(file->read_until_eof());
|
|
|
|
auto json = TRY(JsonValue::from_string(contents));
|
|
|
|
if (!json.is_array())
|
|
|
|
return Error::from_string_literal("File contents is not a JSON array.");
|
|
|
|
auto& json_array = json.as_array();
|
|
|
|
for (auto const& item : json_array.values()) {
|
|
|
|
if (!item.is_object())
|
|
|
|
return Error::from_string_literal("JSON entry is not an object.");
|
|
|
|
TRY(m_history_items.try_append(TRY(ClipboardItem::from_json(item.as_object()))));
|
2023-03-08 21:13:42 +00:00
|
|
|
}
|
2024-01-31 16:33:55 +00:00
|
|
|
// Ensure the data is how we expect: Limited to m_history_limit and sorted newest-to-oldest.
|
|
|
|
quick_sort(m_history_items, [](ClipboardItem const& a, ClipboardItem const& b) -> bool {
|
|
|
|
return a.time > b.time;
|
|
|
|
});
|
|
|
|
if (m_history_items.size() > m_history_limit)
|
|
|
|
m_history_items.resize(m_history_limit);
|
|
|
|
|
2023-03-08 21:13:42 +00:00
|
|
|
return {};
|
|
|
|
};
|
|
|
|
|
|
|
|
auto maybe_error = read_from_file_impl();
|
|
|
|
if (maybe_error.is_error())
|
|
|
|
dbgln("Unable to load clipboard history: {}", maybe_error.release_error());
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2024-02-07 12:33:26 +00:00
|
|
|
ErrorOr<void> ClipboardHistoryModel::write_to_file()
|
2023-03-08 21:13:42 +00:00
|
|
|
{
|
2024-02-07 12:33:26 +00:00
|
|
|
auto file = TRY(Core::File::open(m_path, Core::File::OpenMode::Write | Core::File::OpenMode::Truncate));
|
|
|
|
JsonArray array;
|
|
|
|
array.ensure_capacity(m_history_items.size());
|
|
|
|
for (auto const& item : m_history_items) {
|
2023-03-08 21:13:42 +00:00
|
|
|
if (!item.data_and_type.mime_type.starts_with("text/"sv))
|
2024-02-07 12:33:26 +00:00
|
|
|
continue;
|
|
|
|
TRY(array.append(TRY(item.to_json())));
|
2023-03-08 21:13:42 +00:00
|
|
|
}
|
2024-02-07 12:33:26 +00:00
|
|
|
auto json_string = array.to_byte_string();
|
|
|
|
TRY(file->write_until_depleted(json_string.bytes()));
|
2023-03-08 21:13:42 +00:00
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|