#include "clipboard_files.h" #include #include namespace { FlMethodChannel* channel = nullptr; // The MIME type of [path] from its name and contents, or "". gchar* mime_of(const gchar* path, const guchar* data, gsize length) { gboolean uncertain = FALSE; g_autofree gchar* type = g_content_type_guess(path, data, MIN(length, (gsize)4096), &uncertain); gchar* mime = type == nullptr ? nullptr : g_content_type_get_mime_type(type); return mime == nullptr ? g_strdup("") : mime; } FlValue* file_entry(const gchar* name, const gchar* mime, const guchar* data, gsize length) { FlValue* entry = fl_value_new_map(); fl_value_set_string_take(entry, "name", fl_value_new_string(name)); fl_value_set_string_take(entry, "mime", fl_value_new_string(mime)); fl_value_set_string_take(entry, "bytes", fl_value_new_uint8_list(data, length)); return entry; } // Adds each copied local file; folders and unreadable files become errors. gboolean add_uris(GtkClipboard* clipboard, FlValue* files, FlValue* errors) { g_auto(GStrv) uris = gtk_clipboard_wait_for_uris(clipboard); if (uris == nullptr || uris[0] == nullptr) return FALSE; gboolean any = FALSE; for (gchar** uri = uris; *uri != nullptr; uri++) { g_autofree gchar* path = g_filename_from_uri(*uri, nullptr, nullptr); if (path == nullptr) continue; any = TRUE; g_autofree gchar* name = g_path_get_basename(path); if (g_file_test(path, G_FILE_TEST_IS_DIR)) { g_autofree gchar* message = g_strdup_printf("%s is a folder; paste its files instead.", name); fl_value_append_take(errors, fl_value_new_string(message)); continue; } g_autofree gchar* contents = nullptr; gsize length = 0; g_autoptr(GError) error = nullptr; if (!g_file_get_contents(path, &contents, &length, &error)) { g_autofree gchar* message = g_strdup_printf("Could not read %s: %s", name, error->message); fl_value_append_take(errors, fl_value_new_string(message)); continue; } g_autofree gchar* mime = mime_of(path, reinterpret_cast(contents), length); fl_value_append_take( files, file_entry(name, mime, reinterpret_cast(contents), length)); } return any; } // The clipboard's own PNG (a screenshot tool's), else any image GTK can read // converted to PNG. void add_image(GtkClipboard* clipboard, FlValue* files) { g_autoptr(GtkSelectionData) png = gtk_clipboard_wait_for_contents( clipboard, gdk_atom_intern_static_string("image/png")); if (png != nullptr && gtk_selection_data_get_length(png) > 0) { fl_value_append_take( files, file_entry("", "image/png", gtk_selection_data_get_data(png), gtk_selection_data_get_length(png))); return; } g_autoptr(GdkPixbuf) image = gtk_clipboard_wait_for_image(clipboard); if (image == nullptr) return; g_autofree gchar* buffer = nullptr; gsize length = 0; if (gdk_pixbuf_save_to_buffer(image, &buffer, &length, "png", nullptr, nullptr)) { fl_value_append_take( files, file_entry("", "image/png", reinterpret_cast(buffer), length)); } } FlValue* read_clipboard() { GtkClipboard* clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD); FlValue* files = fl_value_new_list(); FlValue* errors = fl_value_new_list(); FlValue* types = fl_value_new_list(); GdkAtom* targets = nullptr; gint count = 0; if (gtk_clipboard_wait_for_targets(clipboard, &targets, &count)) { for (gint i = 0; i < count; i++) { g_autofree gchar* name = gdk_atom_name(targets[i]); fl_value_append_take(types, fl_value_new_string(name)); } g_free(targets); } if (!add_uris(clipboard, files, errors)) add_image(clipboard, files); FlValue* reply = fl_value_new_map(); fl_value_set_string_take(reply, "files", files); fl_value_set_string_take(reply, "errors", errors); fl_value_set_string_take(reply, "types", types); return reply; } void method_call_cb(FlMethodChannel* channel, FlMethodCall* call, gpointer user_data) { g_autoptr(FlMethodResponse) response = nullptr; if (g_strcmp0(fl_method_call_get_name(call), "read") == 0) { g_autoptr(FlValue) reply = read_clipboard(); response = FL_METHOD_RESPONSE(fl_method_success_response_new(reply)); } else { response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new()); } g_autoptr(GError) error = nullptr; if (!fl_method_call_respond(call, response, &error)) { g_warning("Failed to answer a clipboard read: %s", error->message); } } } // namespace void clipboard_files_register(FlPluginRegistry* registry) { g_autoptr(FlPluginRegistrar) registrar = fl_plugin_registry_get_registrar_for_plugin(registry, "ClipboardFiles"); g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new(); g_clear_object(&channel); channel = fl_method_channel_new(fl_plugin_registrar_get_messenger(registrar), "pipelines/clipboard_files", FL_METHOD_CODEC(codec)); fl_method_channel_set_method_call_handler(channel, method_call_cb, nullptr, nullptr); }