1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 05:47:35 +00:00

Applets/ClipboardHistory: Add persistent storage

Clipboard entries are now preserved upon reboot :^). Unfortunately, it
only supports data with the mimetype "text/".

This is done by writing all entries as a JSON object in a file located
in ~/.data.

Co-authored-by: Sagittarius-a <sagittarius-a@users.noreply.github.com>
This commit is contained in:
Lucas CHOLLET 2023-03-08 16:13:42 -05:00 committed by Andrew Kaster
parent c09d0c4816
commit 07c6cebbab
5 changed files with 143 additions and 7 deletions

View file

@ -124,6 +124,29 @@ RefPtr<Gfx::Bitmap> Clipboard::DataAndType::as_bitmap() const
return bitmap;
}
ErrorOr<Clipboard::DataAndType> Clipboard::DataAndType::from_json(JsonObject const& object)
{
if (!object.has("data"sv) && !object.has("mime_type"sv))
return Error::from_string_literal("JsonObject does not contain necessary fields");
DataAndType result;
result.data = object.get_deprecated_string("data"sv)->to_byte_buffer();
result.mime_type = *object.get_deprecated_string("mime_type"sv);
// FIXME: Also read metadata
return result;
}
ErrorOr<JsonObject> Clipboard::DataAndType::to_json() const
{
JsonObject object;
object.set("data", TRY(DeprecatedString::from_utf8(data.bytes())));
object.set("mime_type", mime_type);
// FIXME: Also write metadata
return object;
}
void Clipboard::set_data(ReadonlyBytes data, DeprecatedString const& type, HashMap<DeprecatedString, DeprecatedString> const& metadata)
{
if (data.is_empty()) {