Skip to content

Commit

Permalink
[76_1] Improve copy and paste for web images
Browse files Browse the repository at this point in the history
  • Loading branch information
da-liii authored Nov 23, 2024
1 parent 4092a03 commit a262011
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 20 deletions.
16 changes: 9 additions & 7 deletions src/Plugins/Qt/qt_utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -521,15 +521,17 @@ to_color (const QColor& c) {
bool
qt_supports (url u) {
static QList<QByteArray> formats= QImageReader::supportedImageFormats ();
/* if (DEBUG_CONVERT) {
debug_convert <<"QT valid formats:";
foreach (QString _format, formats) debug_convert <<", "<<
from_qstring(_format); debug_convert <<LF;
} */
if (DEBUG_CONVERT) {
debug_convert << "QT valid formats:";
foreach (QString _format, formats)
debug_convert << ", " << from_qstring (_format);
debug_convert << LF;
}
string suf= suffix (u);
bool ans= (bool) formats.contains ((QByteArray) as_charp (suf));
// if (DEBUG_CONVERT) {debug_convert <<"QT valid
// format:"<<((ans)?"yes":"no")<<LF;}
if (DEBUG_CONVERT) {
debug_convert << "QT valid format:" << ((ans) ? "yes" : "no") << LF;
}
return ans;
}

Expand Down
16 changes: 10 additions & 6 deletions src/System/Files/image_files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "hashmap.hpp"
#include "sys_utils.hpp"
#include "tm_file.hpp"
#include "tm_url.hpp"
#include "web_files.hpp"

#include "Xml/xml.hpp"
Expand Down Expand Up @@ -315,11 +316,12 @@ image_size (url image, int& w, int& h) {
}

void
image_size_sub (url image, int& w,
image_size_sub (url p_image, int& w,
int& h) { // returns w,h in units of pt (1/72 inch)
if (DEBUG_CONVERT)
debug_convert << "image_size not cached for :" << image << LF;
string suf= suffix (image);
debug_convert << "image_size not cached for :" << p_image << LF;
url image= concretize (p_image);
string suf = suffix (image);
if (suf == "pdf") {
pdf_image_size (image, w, h);
return;
Expand Down Expand Up @@ -358,7 +360,7 @@ image_size_sub (url image, int& w,
}

convert_error
<< "could not determine size of '" << concretize (image) << "'\n"
<< "could not determine size of '" << image << "'\n"
<< "you may consider :\n"
<< " - checking the file is valid,\n"
<< " - converting to a more standard format,\n"
Expand Down Expand Up @@ -446,7 +448,9 @@ image_to_pdf (url image, url pdf, int w_pt, int h_pt, int dpi) {
}

void
image_to_png (url image, url png, int w, int h) { // IN PIXEL UNITS!
image_to_png (url p_image, url png, int w, int h) { // IN PIXEL UNITS!
url image= concretize (p_image);

#ifdef QTTEXMACS
if (qt_supports (image)) {
if (DEBUG_CONVERT) debug_convert << "image_to_png using qt " << LF;
Expand All @@ -456,7 +460,7 @@ image_to_png (url image, url png, int w, int h) { // IN PIXEL UNITS!
#endif
if (call_scm_converter (image, png, w, h)) return;
if (!exists (png)) {
convert_error << image << " could not be converted to png" << LF;
convert_error << p_image << " could not be converted to png" << LF;
copy ("$TEXMACS_PATH/misc/pixmaps/unknown.png", png);
}
}
Expand Down
31 changes: 24 additions & 7 deletions src/System/Files/web_files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "file.hpp"
#include "hashmap.hpp"
#include "sys_utils.hpp"
#include "tm_debug.hpp"
#include "tm_file.hpp"
#include "tree_helper.hpp"

Expand All @@ -24,7 +25,9 @@
#include <lolly/io/http.hpp>

using lolly::io::http_head;
using lolly::io::http_headers;
using lolly::io::http_label;
using lolly::io::http_tree;

#define MAX_CACHED 25

Expand Down Expand Up @@ -88,25 +91,39 @@ url
get_from_web (url name) {
if (!is_rooted_web (name)) return url_none ();

long status_code= open_box<long> (
http_response_ref (http_head (name), http_label::STATUS_CODE)->data);
http_tree head_res = http_head (name);
long status_code= open_box<long> (
http_response_ref (head_res, http_label::STATUS_CODE)->data);
http_headers head_headers= open_box<hashmap<string, string>> (
http_response_ref (head_res, http_label::HEADER)->data);
string content_type= head_headers ("content-type");

if (status_code != 200) {
if (status_code != 200 && status_code != 403) {
if (DEBUG_IO) {
debug_io << "HEAD status code " << status_code << " " << name << LF;
}
return url_none ();
}

url res= get_cache (name);
if (!is_none (res)) return res;

string suf= suffix (name);
if (!is_empty (suf)) suf= string (".") * suf;
if (is_empty (suf)) {
if (starts (content_type, "image/")) {
suf= replace (content_type, "image/", "");
}
}

url tmp = url_temp (suf);
lolly::io::http_headers headers= lolly::io::http_headers ();
headers ("User-Agent") = string ("Mogan/") * XMACS_VERSION * " (" *
url tmp = url_temp (suf);
http_headers headers = http_headers ();
headers ("User-Agent")= string ("Mogan/") * XMACS_VERSION * " (" *
get_pretty_os_name () * "; " *
get_current_cpu_arch () * ")";
lolly::io::download (name, tmp, headers);
if (DEBUG_IO) {
debug_io << "Download from " << name << "=> " << tmp << LF;
}

if (!exists (tmp)) {
return url_none ();
Expand Down

0 comments on commit a262011

Please sign in to comment.