aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2022-09-10 00:11:47 +0200
committerdec05eba <dec05eba@protonmail.com>2022-09-10 00:11:47 +0200
commitf3e7dc127b3d0446bbee2106df110bbfc4df2171 (patch)
tree433b987073874e633f1c48e8b6b26d1624c727d0 /src
parent541180dd46c53f8d80273a92543f2d217a36bf81 (diff)
Matrix: support uploading files from clipboard (direct image data, png, jpg, gif)
Diffstat (limited to 'src')
-rw-r--r--src/QuickMedia.cpp44
-rw-r--r--src/SearchBar.cpp2
-rw-r--r--src/Text.cpp2
3 files changed, 44 insertions, 4 deletions
diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp
index 9cacfdd..1a67509 100644
--- a/src/QuickMedia.cpp
+++ b/src/QuickMedia.cpp
@@ -6654,7 +6654,7 @@ namespace QuickMedia {
matrix_chat_page->rooms_page->body->select_previous_item(true);
move_room = true;
goto chat_page_end;
- } else if(event.key.code == mgl::Keyboard::Down || event.key.code == mgl::Keyboard::J)) {
+ } else if(event.key.code == mgl::Keyboard::Down || event.key.code == mgl::Keyboard::J) {
matrix_chat_page->rooms_page->body->select_next_item(true);
move_room = true;
goto chat_page_end;
@@ -6758,7 +6758,47 @@ namespace QuickMedia {
if(event.key.control && event.key.code == mgl::Keyboard::V) {
frame_skip_text_entry = true;
// TODO: Upload multiple files.
- upload_file(window.get_clipboard());
+
+ bool first_part = true;
+ std::string clipboard_text;
+ char tmp_filename[] = "/tmp/quickmedia_clipboard_XXXXXX";
+ int tmp_file = -1;
+
+ const bool clipboard_success = window.get_clipboard([&first_part, &clipboard_text, &tmp_filename, &tmp_file](const unsigned char *data, size_t size, mgl_clipboard_type clipboard_type) {
+ if(first_part) {
+ first_part = false;
+ if(clipboard_type != MGL_CLIPBOARD_TYPE_STRING) {
+ tmp_file = mkstemp(tmp_filename);
+ if(tmp_file == -1) {
+ show_notification("QuickMedia", "Failed to create temporary file " + std::string(tmp_filename) + " from clipboard (failed to create file)", Urgency::CRITICAL);
+ return false;
+ }
+ }
+ }
+
+ if(tmp_file == -1) {
+ clipboard_text.append((char*)data, size);
+ } else {
+ const ssize_t bytes_written = write(tmp_file, data, size);
+ if(bytes_written != (ssize_t)size) {
+ show_notification("QuickMedia", "Failed to create temporary file " + std::string(tmp_filename) + " from clipboard (failed to write data to file)", Urgency::CRITICAL);
+ return false;
+ }
+ }
+
+ return true;
+ });
+
+ if(tmp_file != -1)
+ clipboard_text = tmp_filename;
+
+ if(clipboard_success)
+ upload_file(clipboard_text);
+
+ if(tmp_file != -1) {
+ close(tmp_file);
+ remove(tmp_filename);
+ }
}
if(event.key.code == mgl::Keyboard::R && tabs[selected_tab].body->get_selected_shared()) {
diff --git a/src/SearchBar.cpp b/src/SearchBar.cpp
index b10ed6a..52e75e6 100644
--- a/src/SearchBar.cpp
+++ b/src/SearchBar.cpp
@@ -119,7 +119,7 @@ namespace QuickMedia {
else if(event.type == mgl::Event::KeyReleased && event.key.code == mgl::Keyboard::Backspace)
backspace_pressed = false;
if(event.type == mgl::Event::KeyPressed && event.key.code == mgl::Keyboard::V && event.key.control) {
- append_text(window.get_clipboard());
+ append_text(window.get_clipboard_string());
}
if(event.type == mgl::Event::KeyPressed && event.key.code == mgl::Keyboard::D && event.key.control) {
diff --git a/src/Text.cpp b/src/Text.cpp
index 1f34067..d29c9b5 100644
--- a/src/Text.cpp
+++ b/src/Text.cpp
@@ -1077,7 +1077,7 @@ namespace QuickMedia
std::string stringToAdd;
if(event.text.codepoint == 22) // ctrl+v
{
- stringToAdd = window.get_clipboard();
+ stringToAdd = window.get_clipboard_string();
}
else if(event.text.codepoint >= 32 || (event.text.codepoint == '\t' && !single_line_edit))
stringToAdd.assign(event.text.str, event.text.size);