Skip to content

Commit 2e6d59b

Browse files
committed
Clipboard: Add a key-value map alongside the clipboard storage
A clipping now consists of three things: - The raw clip data - A MIME type - A key-value map (String, String) for anything you like
1 parent 51146e3 commit 2e6d59b

File tree

7 files changed

+34
-11
lines changed

7 files changed

+34
-11
lines changed

Libraries/LibGUI/Clipboard.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,11 @@ Clipboard::DataAndType Clipboard::data_and_type() const
9292
}
9393
auto data = ByteBuffer::copy(shared_buffer->data(), response->data_size());
9494
auto type = response->mime_type();
95-
return { data, type };
95+
auto metadata = response->metadata().entries();
96+
return { data, type, metadata };
9697
}
9798

98-
void Clipboard::set_data(ReadonlyBytes data, const String& type)
99+
void Clipboard::set_data(ReadonlyBytes data, const String& type, const HashMap<String, String>& metadata)
99100
{
100101
auto shared_buffer = SharedBuffer::create_with_size(data.size() + 1);
101102
if (!shared_buffer) {
@@ -109,7 +110,7 @@ void Clipboard::set_data(ReadonlyBytes data, const String& type)
109110
shared_buffer->seal();
110111
shared_buffer->share_with(connection().server_pid());
111112

112-
connection().send_sync<Messages::ClipboardServer::SetClipboardData>(shared_buffer->shbuf_id(), data.size(), type);
113+
connection().send_sync<Messages::ClipboardServer::SetClipboardData>(shared_buffer->shbuf_id(), data.size(), type, metadata);
113114
}
114115

115116
void ClipboardServerConnection::handle(const Messages::ClipboardClient::ClipboardDataChanged& message)
@@ -119,4 +120,9 @@ void ClipboardServerConnection::handle(const Messages::ClipboardClient::Clipboar
119120
clipboard.on_change(message.mime_type());
120121
}
121122

123+
void Clipboard::set_bitmap(const Gfx::Bitmap& bitmap)
124+
{
125+
(void) bitmap;
126+
}
127+
122128
}

Libraries/LibGUI/Clipboard.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
#pragma once
2828

2929
#include <AK/Function.h>
30+
#include <AK/HashMap.h>
3031
#include <AK/String.h>
3132
#include <LibGUI/Forward.h>
33+
#include <LibGfx/Forward.h>
3234

3335
namespace GUI {
3436

@@ -38,16 +40,19 @@ class Clipboard {
3840

3941
ByteBuffer data() const { return data_and_type().data; }
4042
String mime_type() const { return data_and_type().mime_type; }
41-
void set_data(ReadonlyBytes, const String& mime_type = "text/plain");
43+
void set_data(ReadonlyBytes, const String& mime_type = "text/plain", const HashMap<String, String>& metadata = {});
4244

4345
void set_plain_text(const String& text)
4446
{
4547
set_data(text.bytes());
4648
}
4749

50+
void set_bitmap(const Gfx::Bitmap&);
51+
4852
struct DataAndType {
4953
ByteBuffer data;
5054
String mime_type;
55+
HashMap<String, String> metadata;
5156
};
5257

5358
DataAndType data_and_type() const;

Libraries/LibIPC/Dictionary.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ namespace IPC {
3333

3434
class Dictionary {
3535
public:
36-
Dictionary() {}
36+
Dictionary() { }
37+
38+
Dictionary(const HashMap<String, String>& initial_entries)
39+
: m_entries(initial_entries)
40+
{
41+
}
3742

3843
bool is_empty() const { return m_entries.is_empty(); }
3944
size_t size() const { return m_entries.size(); }

Services/Clipboard/ClientConnection.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ OwnPtr<Messages::ClipboardServer::SetClipboardDataResponse> ClientConnection::ha
6868
did_misbehave("SetClipboardData: Bad shared buffer ID");
6969
return nullptr;
7070
}
71-
Storage::the().set_data(*shared_buffer, message.data_size(), message.mime_type());
71+
Storage::the().set_data(*shared_buffer, message.data_size(), message.mime_type(), message.metadata().entries());
7272
return make<Messages::ClipboardServer::SetClipboardDataResponse>();
7373
}
7474

@@ -92,7 +92,7 @@ OwnPtr<Messages::ClipboardServer::GetClipboardDataResponse> ClientConnection::ha
9292
// After we respond to GetClipboardData, we have to wait for the client to ref the buffer on his side.
9393
m_last_sent_buffer = move(shared_buffer);
9494
}
95-
return make<Messages::ClipboardServer::GetClipboardDataResponse>(shbuf_id, storage.data_size(), storage.mime_type());
95+
return make<Messages::ClipboardServer::GetClipboardDataResponse>(shbuf_id, storage.data_size(), storage.mime_type(), storage.metadata());
9696
}
9797

9898
void ClientConnection::notify_about_clipboard_change()

Services/Clipboard/ClipboardServer.ipc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ endpoint ClipboardServer = 802
22
{
33
Greet() => (i32 client_id)
44

5-
GetClipboardData() => (i32 shbuf_id, i32 data_size, [UTF8] String mime_type)
6-
SetClipboardData(i32 shbuf_id, i32 data_size, [UTF8] String mime_type) => ()
5+
GetClipboardData() => (i32 shbuf_id, i32 data_size, [UTF8] String mime_type, IPC::Dictionary metadata)
6+
SetClipboardData(i32 shbuf_id, i32 data_size, [UTF8] String mime_type, IPC::Dictionary metadata) => ()
77
}

Services/Clipboard/Storage.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,16 @@ Storage::~Storage()
4444
{
4545
}
4646

47-
void Storage::set_data(NonnullRefPtr<SharedBuffer> data, size_t data_size, const String& mime_type)
47+
void Storage::set_data(NonnullRefPtr<SharedBuffer> data, size_t data_size, const String& mime_type, const HashMap<String, String>& metadata)
4848
{
4949
dbg() << "Storage::set_data <- [" << mime_type << "] " << data->data() << " (" << data_size << " bytes)";
50+
for (auto& it : metadata) {
51+
dbg() << " " << it.key << ": " << it.value;
52+
}
5053
m_shared_buffer = move(data);
5154
m_data_size = data_size;
5255
m_mime_type = mime_type;
56+
m_metadata = metadata;
5357

5458
if (on_content_change)
5559
on_content_change();

Services/Clipboard/Storage.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#pragma once
2828

2929
#include <AK/Function.h>
30+
#include <AK/HashMap.h>
3031
#include <AK/SharedBuffer.h>
3132
#include <AK/String.h>
3233

@@ -40,6 +41,7 @@ class Storage {
4041
bool has_data() const { return m_shared_buffer; }
4142

4243
const String& mime_type() const { return m_mime_type; }
44+
const HashMap<String, String>& metadata() const { return m_metadata; }
4345

4446
const u8* data() const
4547
{
@@ -55,7 +57,7 @@ class Storage {
5557
return 0;
5658
}
5759

58-
void set_data(NonnullRefPtr<SharedBuffer>, size_t data_size, const String& mime_type);
60+
void set_data(NonnullRefPtr<SharedBuffer>, size_t data_size, const String& mime_type, const HashMap<String, String>& metadata);
5961

6062
Function<void()> on_content_change;
6163

@@ -65,6 +67,7 @@ class Storage {
6567
String m_mime_type;
6668
RefPtr<SharedBuffer> m_shared_buffer;
6769
size_t m_data_size { 0 };
70+
HashMap<String, String> m_metadata;
6871
};
6972

7073
}

0 commit comments

Comments
 (0)