FileManager+LibGUI+Userland: Switch clipboard to MIME types

We will now actually use MIME types for clipboard. The default type is now
"text/plain" (instead of just "text").

This also fixes some issues in copy(1) and paste(1).
This commit is contained in:
Sergey Bugaev 2020-05-15 22:35:03 +03:00 committed by Andreas Kling
parent de7827faf7
commit acc107a44f
Notes: sideshowbarker 2024-07-19 06:37:46 +09:00
4 changed files with 27 additions and 19 deletions

View file

@ -420,9 +420,10 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
StringBuilder copy_text; StringBuilder copy_text;
for (auto& path : paths) { for (auto& path : paths) {
copy_text.appendf("%s\n", path.characters()); 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(), "file-list"); GUI::Clipboard::the().set_data(copy_text.build(), "text/uri-list");
}, },
window); window);
copy_action->set_enabled(false); copy_action->set_enabled(false);
@ -462,7 +463,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
auto do_paste = [&](const GUI::Action& action) { auto do_paste = [&](const GUI::Action& action) {
auto data_and_type = GUI::Clipboard::the().data_and_type(); auto data_and_type = GUI::Clipboard::the().data_and_type();
if (data_and_type.type != "file-list") { if (data_and_type.type != "text/uri-list") {
dbg() << "Cannot paste clipboard type " << data_and_type.type; dbg() << "Cannot paste clipboard type " << data_and_type.type;
return; return;
} }
@ -478,16 +479,18 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
else else
target_directory = directory_view.path(); target_directory = directory_view.path();
for (auto& current_path : copied_lines) { for (auto& uri_as_string : copied_lines) {
if (current_path.is_empty()) if (uri_as_string.is_empty())
continue; continue;
URL url = uri_as_string;
if (!url.is_valid() || url.protocol() != "file") {
dbg() << "Cannot paste URI " << uri_as_string;
continue;
}
auto new_path = String::format("%s/%s", auto new_path = String::format("%s/%s", target_directory.characters(), url.basename().characters());
target_directory.characters(), if (!FileUtils::copy_file_or_directory(url.path(), new_path)) {
FileSystemPath(current_path).basename().characters()); auto error_message = String::format("Could not paste %s.", url.path().characters());
if (!FileUtils::copy_file_or_directory(current_path, new_path)) {
auto error_message = String::format("Could not paste %s.",
current_path.characters());
GUI::MessageBox::show(error_message, "File Manager", GUI::MessageBox::Type::Error); GUI::MessageBox::show(error_message, "File Manager", GUI::MessageBox::Type::Error);
} else { } else {
refresh_tree_view(); refresh_tree_view();
@ -609,7 +612,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
GUI::Clipboard::the().on_change = [&](const String& data_type) { GUI::Clipboard::the().on_change = [&](const String& data_type) {
auto current_location = directory_view.path(); auto current_location = directory_view.path();
paste_action->set_enabled(data_type == "file-list" && access(current_location.characters(), W_OK) == 0); paste_action->set_enabled(data_type == "text/uri-list" && access(current_location.characters(), W_OK) == 0);
}; };
auto menubar = GUI::MenuBar::construct(); auto menubar = GUI::MenuBar::construct();
@ -678,7 +681,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
auto can_write_in_path = access(new_path.characters(), W_OK) == 0; auto can_write_in_path = access(new_path.characters(), W_OK) == 0;
mkdir_action->set_enabled(can_write_in_path); mkdir_action->set_enabled(can_write_in_path);
paste_action->set_enabled(can_write_in_path && GUI::Clipboard::the().type() == "file-list"); paste_action->set_enabled(can_write_in_path && GUI::Clipboard::the().type() == "text/uri-list");
go_forward_action->set_enabled(directory_view.path_history_position() < directory_view.path_history_size() - 1); 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); go_back_action->set_enabled(directory_view.path_history_position() > 0);
open_parent_directory_action->set_enabled(new_path != "/"); open_parent_directory_action->set_enabled(new_path != "/");
@ -758,7 +761,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
auto& node = directory_view.model().node(index); auto& node = directory_view.model().node(index);
if (node.is_directory()) { if (node.is_directory()) {
auto should_get_enabled = access(node.full_path(directory_view.model()).characters(), W_OK) == 0 && GUI::Clipboard::the().type() == "file-list"; auto should_get_enabled = access(node.full_path(directory_view.model()).characters(), W_OK) == 0 && GUI::Clipboard::the().type() == "text/uri-list";
folder_specific_paste_action->set_enabled(should_get_enabled); folder_specific_paste_action->set_enabled(should_get_enabled);
directory_context_menu->popup(event.screen_position()); directory_context_menu->popup(event.screen_position());
} else { } else {
@ -823,7 +826,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
directory_view.open(initial_location); directory_view.open(initial_location);
directory_view.set_focus(true); directory_view.set_focus(true);
paste_action->set_enabled(GUI::Clipboard::the().type() == "file-list" && access(initial_location.characters(), W_OK) == 0); paste_action->set_enabled(GUI::Clipboard::the().type() == "text/uri-list" && access(initial_location.characters(), W_OK) == 0);
window->show(); window->show();

View file

@ -38,7 +38,7 @@ public:
String data() const { return data_and_type().data; } String data() const { return data_and_type().data; }
String type() const { return data_and_type().type; } String type() const { return data_and_type().type; }
void set_data(const StringView&, const String& data_type = "text"); void set_data(const StringView&, const String& data_type = "text/plain");
struct DataAndType { struct DataAndType {
String data; String data;

View file

@ -36,12 +36,12 @@
struct Options { struct Options {
String data; String data;
StringView type { "text" }; StringView type;
}; };
Options parse_options(int argc, char* argv[]) Options parse_options(int argc, char* argv[])
{ {
const char* type = nullptr; const char* type = "text/plain";
Vector<const char*> text; Vector<const char*> text;
Core::ArgsParser args_parser; Core::ArgsParser args_parser;

View file

@ -46,11 +46,16 @@ int main(int argc, char* argv[])
auto& clipboard = GUI::Clipboard::the(); auto& clipboard = GUI::Clipboard::the();
auto data_and_type = clipboard.data_and_type(); auto data_and_type = clipboard.data_and_type();
if (data_and_type.type.is_null()) {
fprintf(stderr, "Nothing copied\n");
return 1;
}
if (!print_type) { if (!print_type) {
printf("%s", data_and_type.data.characters()); printf("%s", data_and_type.data.characters());
// Append a newline to text contents, but // Append a newline to text contents, but
// only if we're not asked not to do this. // only if we're not asked not to do this.
if (data_and_type.type == "text" && !no_newline) if (data_and_type.type == "text/plain" && !no_newline)
putchar('\n'); putchar('\n');
} else { } else {
printf("%s\n", data_and_type.type.characters()); printf("%s\n", data_and_type.type.characters());