diff --git a/Applications/Browser/Tab.cpp b/Applications/Browser/Tab.cpp index 300aa1922c..8f48c448fe 100644 --- a/Applications/Browser/Tab.cpp +++ b/Applications/Browser/Tab.cpp @@ -160,7 +160,7 @@ Tab::Tab(Type type) hooks().on_link_click(m_link_context_menu_url, "_blank", 0); })); m_link_context_menu->add_action(GUI::Action::create("Copy link", [this](auto&) { - GUI::Clipboard::the().set_data(m_link_context_menu_url.to_string()); + GUI::Clipboard::the().set_plain_text(m_link_context_menu_url.to_string()); })); m_link_context_menu->add_separator(); m_link_context_menu->add_action(GUI::Action::create("Download", [this](auto&) { diff --git a/Applications/FileManager/main.cpp b/Applications/FileManager/main.cpp index a187d9e1b1..6ceb449c21 100644 --- a/Applications/FileManager/main.cpp +++ b/Applications/FileManager/main.cpp @@ -368,7 +368,7 @@ int run_in_windowed_mode(RefPtr config, String initial_locatio auto url = URL::create_with_file_protocol(path); copy_text.appendf("%s\n", url.to_string().characters()); } - GUI::Clipboard::the().set_data(copy_text.build(), "text/uri-list"); + GUI::Clipboard::the().set_data(copy_text.build().bytes(), "text/uri-list"); }, window); copy_action->set_enabled(false); @@ -407,11 +407,11 @@ int run_in_windowed_mode(RefPtr config, String initial_locatio auto do_paste = [&](const GUI::Action& action) { auto data_and_type = GUI::Clipboard::the().data_and_type(); - if (data_and_type.type != "text/uri-list") { - dbg() << "Cannot paste clipboard type " << data_and_type.type; + if (data_and_type.mime_type != "text/uri-list") { + dbg() << "Cannot paste clipboard type " << data_and_type.mime_type; return; } - auto copied_lines = data_and_type.data.split('\n'); + auto copied_lines = String::copy(data_and_type.data).split('\n'); if (copied_lines.is_empty()) { dbg() << "No files to paste"; return; @@ -638,7 +638,7 @@ int run_in_windowed_mode(RefPtr config, String initial_locatio return; } - paste_action->set_enabled(can_write_in_path && GUI::Clipboard::the().type() == "text/uri-list"); + paste_action->set_enabled(can_write_in_path && GUI::Clipboard::the().mime_type() == "text/uri-list"); go_forward_action->set_enabled(directory_view.path_history_position() < directory_view.path_history_size() - 1); go_back_action->set_enabled(directory_view.path_history_position() > 0); open_parent_directory_action->set_enabled(new_path != "/"); @@ -705,7 +705,7 @@ int run_in_windowed_mode(RefPtr config, String initial_locatio auto& node = directory_view.node(index); if (node.is_directory()) { - auto should_get_enabled = access(node.full_path().characters(), W_OK) == 0 && GUI::Clipboard::the().type() == "text/uri-list"; + auto should_get_enabled = access(node.full_path().characters(), W_OK) == 0 && GUI::Clipboard::the().mime_type() == "text/uri-list"; folder_specific_paste_action->set_enabled(should_get_enabled); directory_context_menu->popup(event.screen_position()); } else { @@ -822,7 +822,7 @@ int run_in_windowed_mode(RefPtr config, String initial_locatio directory_view.open(initial_location); directory_view.set_focus(true); - paste_action->set_enabled(GUI::Clipboard::the().type() == "text/uri-list" && access(initial_location.characters(), W_OK) == 0); + paste_action->set_enabled(GUI::Clipboard::the().mime_type() == "text/uri-list" && access(initial_location.characters(), W_OK) == 0); window->show(); diff --git a/Applications/HexEditor/HexEditor.cpp b/Applications/HexEditor/HexEditor.cpp index 24985f0de3..fc041d47ad 100644 --- a/Applications/HexEditor/HexEditor.cpp +++ b/Applications/HexEditor/HexEditor.cpp @@ -139,7 +139,7 @@ bool HexEditor::copy_selected_hex_to_clipboard() output_string_builder.appendf("%02X ", m_buffer.data()[i]); } - GUI::Clipboard::the().set_data(output_string_builder.to_string()); + GUI::Clipboard::the().set_plain_text(output_string_builder.to_string()); return true; } @@ -153,7 +153,7 @@ bool HexEditor::copy_selected_text_to_clipboard() output_string_builder.appendf("%c", isprint(m_buffer.data()[i]) ? m_buffer[i] : '.'); } - GUI::Clipboard::the().set_data(output_string_builder.to_string()); + GUI::Clipboard::the().set_plain_text(output_string_builder.to_string()); return true; } @@ -176,7 +176,7 @@ bool HexEditor::copy_selected_hex_to_clipboard_as_c_code() } output_string_builder.append("\n};\n"); - GUI::Clipboard::the().set_data(output_string_builder.to_string()); + GUI::Clipboard::the().set_plain_text(output_string_builder.to_string()); return true; } diff --git a/Libraries/LibGUI/Clipboard.cpp b/Libraries/LibGUI/Clipboard.cpp index f596df1b7a..8545febdaa 100644 --- a/Libraries/LibGUI/Clipboard.cpp +++ b/Libraries/LibGUI/Clipboard.cpp @@ -90,26 +90,26 @@ Clipboard::DataAndType Clipboard::data_and_type() const dbgprintf("GUI::Clipboard::data() clipping contents size is greater than shared buffer size\n"); return {}; } - auto data = String((const char*)shared_buffer->data(), response->data_size()); + auto data = ByteBuffer::copy(shared_buffer->data(), response->data_size()); auto type = response->mime_type(); return { data, type }; } -void Clipboard::set_data(const StringView& data, const String& type) +void Clipboard::set_data(ReadonlyBytes data, const String& type) { - auto shared_buffer = SharedBuffer::create_with_size(data.length() + 1); + auto shared_buffer = SharedBuffer::create_with_size(data.size() + 1); if (!shared_buffer) { dbgprintf("GUI::Clipboard::set_data() failed to create a shared buffer\n"); return; } if (!data.is_empty()) - memcpy(shared_buffer->data(), data.characters_without_null_termination(), data.length() + 1); + memcpy(shared_buffer->data(), data.data(), data.size() + 1); else ((u8*)shared_buffer->data())[0] = '\0'; shared_buffer->seal(); shared_buffer->share_with(connection().server_pid()); - connection().send_sync(shared_buffer->shbuf_id(), data.length(), type); + connection().send_sync(shared_buffer->shbuf_id(), data.size(), type); } void ClipboardServerConnection::handle(const Messages::ClipboardClient::ClipboardDataChanged& message) diff --git a/Libraries/LibGUI/Clipboard.h b/Libraries/LibGUI/Clipboard.h index cab0a52a45..96786baa2c 100644 --- a/Libraries/LibGUI/Clipboard.h +++ b/Libraries/LibGUI/Clipboard.h @@ -36,18 +36,23 @@ class Clipboard { public: static Clipboard& the(); - String data() const { return data_and_type().data; } - String type() const { return data_and_type().type; } - void set_data(const StringView&, const String& data_type = "text/plain"); + ByteBuffer data() const { return data_and_type().data; } + String mime_type() const { return data_and_type().mime_type; } + void set_data(ReadonlyBytes, const String& mime_type = "text/plain"); + + void set_plain_text(const String& text) + { + set_data(text.bytes()); + } struct DataAndType { - String data; - String type; + ByteBuffer data; + String mime_type; }; DataAndType data_and_type() const; - Function on_change; + Function on_change; static void initialize(Badge); diff --git a/Libraries/LibGUI/TextEditor.cpp b/Libraries/LibGUI/TextEditor.cpp index 310e9e60ad..8384358d4b 100644 --- a/Libraries/LibGUI/TextEditor.cpp +++ b/Libraries/LibGUI/TextEditor.cpp @@ -1300,7 +1300,7 @@ void TextEditor::cut() return; auto selected_text = this->selected_text(); printf("Cut: \"%s\"\n", selected_text.characters()); - Clipboard::the().set_data(selected_text); + Clipboard::the().set_plain_text(selected_text); delete_selection(); } @@ -1308,7 +1308,7 @@ void TextEditor::copy() { auto selected_text = this->selected_text(); printf("Copy: \"%s\"\n", selected_text.characters()); - Clipboard::the().set_data(selected_text); + Clipboard::the().set_plain_text(selected_text); } void TextEditor::paste() @@ -1317,7 +1317,7 @@ void TextEditor::paste() return; auto paste_text = Clipboard::the().data(); - printf("Paste: \"%s\"\n", paste_text.characters()); + printf("Paste: \"%s\"\n", String::copy(paste_text).characters()); TemporaryChange change(m_automatic_indentation_enabled, false); insert_at_cursor_or_replace_selection(paste_text); diff --git a/Libraries/LibVT/TerminalWidget.cpp b/Libraries/LibVT/TerminalWidget.cpp index 115459a9a8..973cf7293d 100644 --- a/Libraries/LibVT/TerminalWidget.cpp +++ b/Libraries/LibVT/TerminalWidget.cpp @@ -550,7 +550,7 @@ void TerminalWidget::paste() auto text = GUI::Clipboard::the().data(); if (text.is_empty()) return; - int nwritten = write(m_ptm_fd, text.characters(), text.length()); + int nwritten = write(m_ptm_fd, text.data(), text.size()); if (nwritten < 0) { perror("write"); ASSERT_NOT_REACHED(); @@ -560,7 +560,7 @@ void TerminalWidget::paste() void TerminalWidget::copy() { if (has_selection()) - GUI::Clipboard::the().set_data(selected_text()); + GUI::Clipboard::the().set_plain_text(selected_text()); } void TerminalWidget::mouseup_event(GUI::MouseEvent& event) @@ -829,7 +829,7 @@ void TerminalWidget::context_menu_event(GUI::ContextMenuEvent& event) m_context_menu_for_hyperlink->add_action(action); } m_context_menu_for_hyperlink->add_action(GUI::Action::create("Copy URL", [this](auto&) { - GUI::Clipboard::the().set_data(m_context_menu_href); + GUI::Clipboard::the().set_plain_text(m_context_menu_href); })); m_context_menu_for_hyperlink->add_separator(); m_context_menu_for_hyperlink->add_action(copy_action()); diff --git a/Libraries/LibWeb/InProcessWebView.cpp b/Libraries/LibWeb/InProcessWebView.cpp index 8b9c149b5d..765b76a6b7 100644 --- a/Libraries/LibWeb/InProcessWebView.cpp +++ b/Libraries/LibWeb/InProcessWebView.cpp @@ -66,7 +66,7 @@ InProcessWebView::InProcessWebView() set_background_role(ColorRole::Base); m_copy_action = GUI::CommonActions::make_copy_action([this](auto&) { - GUI::Clipboard::the().set_data(selected_text(), "text/plain"); + GUI::Clipboard::the().set_plain_text(selected_text()); }); m_select_all_action = GUI::CommonActions::make_select_all_action([this](auto&) { diff --git a/MenuApplets/ClipboardHistory/ClipboardHistoryModel.cpp b/MenuApplets/ClipboardHistory/ClipboardHistoryModel.cpp index acea0a45c8..537e6c74dc 100644 --- a/MenuApplets/ClipboardHistory/ClipboardHistoryModel.cpp +++ b/MenuApplets/ClipboardHistory/ClipboardHistoryModel.cpp @@ -54,11 +54,11 @@ GUI::Variant ClipboardHistoryModel::data(const GUI::ModelIndex& index, GUI::Mode auto& data_and_type = m_history_items[index.row()]; switch (index.column()) { case Column::Data: - if (data_and_type.type.starts_with("text/")) - return data_and_type.data; + if (data_and_type.mime_type.starts_with("text/")) + return String::copy(data_and_type.data); return "<...>"; case Column::Type: - return data_and_type.type; + return data_and_type.mime_type; default: ASSERT_NOT_REACHED(); } diff --git a/MenuApplets/ClipboardHistory/main.cpp b/MenuApplets/ClipboardHistory/main.cpp index 17400eb97c..53148c8562 100644 --- a/MenuApplets/ClipboardHistory/main.cpp +++ b/MenuApplets/ClipboardHistory/main.cpp @@ -70,7 +70,7 @@ int main(int argc, char* argv[]) table_view.on_activation = [&](const GUI::ModelIndex& index) { auto& data_and_type = model->item_at(index.row()); - GUI::Clipboard::the().set_data(data_and_type.data, data_and_type.type); + GUI::Clipboard::the().set_data(data_and_type.data, data_and_type.mime_type); }; auto applet_window = GUI::Window::construct(); diff --git a/Userland/copy.cpp b/Userland/copy.cpp index 8ef129bece..237789f124 100644 --- a/Userland/copy.cpp +++ b/Userland/copy.cpp @@ -80,7 +80,7 @@ int main(int argc, char* argv[]) Options options = parse_options(argc, argv); auto& clipboard = GUI::Clipboard::the(); - clipboard.set_data(options.data, options.type); + clipboard.set_data(options.data.bytes(), options.type); return 0; } diff --git a/Userland/paste.cpp b/Userland/paste.cpp index 2a15dc00cd..57486ae383 100644 --- a/Userland/paste.cpp +++ b/Userland/paste.cpp @@ -46,19 +46,19 @@ int main(int argc, char* argv[]) auto& clipboard = GUI::Clipboard::the(); auto data_and_type = clipboard.data_and_type(); - if (data_and_type.type.is_null()) { + if (data_and_type.mime_type.is_null()) { fprintf(stderr, "Nothing copied\n"); return 1; } if (!print_type) { - printf("%s", data_and_type.data.characters()); + printf("%s", data_and_type.data.data()); // Append a newline to text contents, but // only if we're not asked not to do this. - if (data_and_type.type.starts_with("text/") && !no_newline) + if (data_and_type.mime_type.starts_with("text/") && !no_newline) putchar('\n'); } else { - printf("%s\n", data_and_type.type.characters()); + printf("%s\n", data_and_type.mime_type.characters()); } return 0;