Skip to content

Commit

Permalink
image caching in lite version now takes size into account
Browse files Browse the repository at this point in the history
  • Loading branch information
martinrotter committed Sep 30, 2024
1 parent 58a8fb7 commit 4e698c9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
38 changes: 31 additions & 7 deletions src/librssguard/gui/webviewers/qtextbrowser/textbrowserviewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,34 @@ QVariant TextBrowserViewer::loadOneResource(int type, const QUrl& name) {
}

// Resources are enabled and we already have the resource.
QByteArray resource_data = m_loadedResources.value(resolved_name);
int acceptable_width = int(width() * ACCEPTABLE_IMAGE_PERCENTUAL_WIDTH);

QMap<int, QByteArray>& resource_data_all_sizes = m_loadedResources[resolved_name];
QImage img;

if (resource_data.isEmpty()) {
qDebugNN << LOGSEC_GUI << "Picture" << QUOTE_W_SPACE(name)
<< "has these sizes cached:" << NONQUOTE_W_SPACE_DOT(resource_data_all_sizes.keys());

if (resource_data_all_sizes.isEmpty()) {
img = m_placeholderImageError.toImage();
}
else {
img = QImage::fromData(m_loadedResources.value(resolved_name));
// Now, we either select specifically sized picture, or default one.
QByteArray resource_data;

if (resource_data_all_sizes.contains(acceptable_width)) {
// We have picture with this exact size. The picture was likely downsized
// to this size before.
resource_data = resource_data_all_sizes.value(acceptable_width);
}
else {
// We only have default size or not desired size. Return initial picture.
resource_data = resource_data_all_sizes.value(0);
}

img = QImage::fromData(resource_data);
}

int acceptable_width = int(width() * ACCEPTABLE_IMAGE_PERCENTUAL_WIDTH);
int img_width = img.width();

if (img_width > acceptable_width) {
Expand All @@ -111,7 +128,7 @@ QVariant TextBrowserViewer::loadOneResource(int type, const QUrl& name) {

if (img.save(&save_buf, "PNG", 100)) {
save_buf.close();
m_loadedResources.insert(resolved_name, save_arr);
resource_data_all_sizes.insert(acceptable_width, save_arr);
}
else {
qWarningNN << LOGSEC_GUI << "Failed to save modified image" << QUOTE_W_SPACE(name) << "to cache.";
Expand Down Expand Up @@ -544,12 +561,19 @@ void TextBrowserViewer::resourceDownloaded(const QUrl& url,
int http_code,
const QByteArray& contents) {
Q_UNUSED(http_code)
if (!m_loadedResources.contains(url)) {
m_loadedResources.insert(url, QMap<int, QByteArray>());
}

QMap<int, QByteArray>& resource_data_all_sizes = m_loadedResources[url];

resource_data_all_sizes.clear();

if (status == QNetworkReply::NetworkError::NoError) {
m_loadedResources.insert(url, contents);
resource_data_all_sizes.insert(0, contents);
}
else {
m_loadedResources.insert(url, {});
resource_data_all_sizes.insert(0, {});
}

downloadNextNeededResource();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ class RSSGUARD_DLLSPEC TextBrowserViewer : public QTextBrowser, public WebViewer
QList<QUrl> m_neededResources; // All URLs here must be resolved.
Downloader* m_resourceDownloader;
QThread* m_resourceDownloaderThread;
QMap<QUrl, QByteArray> m_loadedResources; // All URLs here must be resolved.

// Contains list of precisely sized images per each url.
QMap<QUrl, QMap<int, QByteArray>> m_loadedResources; // All URLs here must be resolved.
QPixmap m_placeholderImage;
QPixmap m_placeholderImageError;
QUrl m_currentUrl;
Expand Down

0 comments on commit 4e698c9

Please sign in to comment.