Fix to avoid threading a lot with url pictures.

This commit is contained in:
sergiotarxz 2022-01-18 21:00:39 +01:00
parent b4372119c0
commit 46ef8f9df3
5 changed files with 54 additions and 21 deletions

View File

@ -1,6 +1,6 @@
#pragma once
#include <gtk/gtk.h>
void
GtkPicture *
create_picture_from_url (const char *const url, gint picture_size,
GAsyncReadyCallback ready, gpointer source_object,
gpointer callback_data);

View File

@ -167,8 +167,14 @@ set_image_zoomable_picture_container (ChapterVisorData *chapter_visor_data) {
strlen(url_image_not_owned) + 1, 0,
strlen (url_image_not_owned));
create_picture_from_url (url_image, 0, picture_ready_manga_page,
GtkPicture *picture = create_picture_from_url (url_image, 0, picture_ready_manga_page,
zoomable_picture_container, chapter_visor_data);
if (picture) {
chapter_visor_data->current_picture = GTK_PICTURE (picture);
g_signal_connect (G_OBJECT (picture), "map",
G_CALLBACK (image_page_show), chapter_visor_data);
gtk_scrolled_window_set_child (zoomable_picture_container, GTK_WIDGET (picture));
}
g_free (url_image);
g_clear_object (&string_util);
}

View File

@ -89,7 +89,7 @@ create_detail_view (MgManga *manga, ControlsAdwaita *controls) {
("network-transmit-receive-symbolic"));
GtkListView *chapter_list = NULL;
char *url_image = mg_manga_get_image_url(manga);
create_picture_from_url (url_image, 200,
GtkPicture *picture = create_picture_from_url (url_image, 200,
picture_ready_manga_detail, avatar_title_box, NULL);
char *manga_title_text = mg_manga_get_title (manga);
char *title_text = mg_util_xml_get_title_text (
@ -116,6 +116,9 @@ create_detail_view (MgManga *manga, ControlsAdwaita *controls) {
gtk_widget_set_size_request (GTK_WIDGET (manga_description), 200, -1);
gtk_label_set_use_markup (GTK_LABEL (manga_title), 1);
if (picture) {
gtk_box_append (avatar_title_box, GTK_WIDGET (picture));
}
gtk_box_append (avatar_title_box, GTK_WIDGET (manga_title));
gtk_box_append (foldable_manga_data, GTK_WIDGET (avatar_title_box));

View File

@ -69,10 +69,13 @@ setup_list_view_mangas (GtkSignalListItemFactory *factory,
char *image_url = mg_manga_get_image_url (manga);
GtkWidget *label = gtk_label_new (manga_title);
create_picture_from_url (image_url, 100,
GtkPicture *picture = create_picture_from_url (image_url, 100,
picture_ready_manga_preview, box, NULL);
g_object_set_property_int (G_OBJECT(box), "height-request", 100);
if (picture) {
gtk_box_append (box, GTK_WIDGET (picture));
}
gtk_box_append (box, label);
gtk_list_item_set_child (list_item, GTK_WIDGET (box));

View File

@ -16,6 +16,7 @@ const char *const IMAGE_CACHE_FORMAT_STRING =
typedef struct {
char *url;
gint picture_size;
GFile *image;
} PictureThreadAttributes;
static char *
@ -30,7 +31,7 @@ threaded_picture_recover (GTask *task, gpointer source_object,
const char *url = attrs->url;
gint picture_size = attrs->picture_size;
GFileIOStream *iostream = NULL;
GFile *image = NULL;
GFile *image = attrs->image;
GError *error = NULL;
GdkTexture *texture = NULL;
GtkPicture *picture = NULL;
@ -39,15 +40,11 @@ threaded_picture_recover (GTask *task, gpointer source_object,
char *downloaded_image = NULL;
MgUtilSoup *util_soup = mg_util_soup_new ();
downloaded_image = mg_util_soup_get_request (util_soup,
url, &size_downloaded_image);
static GMutex mutex;
g_mutex_lock (&mutex);
image = get_image_for_url (url);
g_mutex_unlock (&mutex);
if (!g_file_query_exists (image, NULL)) {
downloaded_image =
mg_util_soup_get_request
(util_soup,
url, &size_downloaded_image);
iostream = g_file_create_readwrite (image, G_FILE_CREATE_NONE,
NULL, &error);
if (error) {
@ -63,9 +60,11 @@ threaded_picture_recover (GTask *task, gpointer source_object,
goto cleanup_create_picture_from_url;
}
}
g_mutex_unlock (&mutex);
texture = gdk_texture_new_from_file (image, &error);
if (error) {
fprintf (stderr, "Texture malformed.");
g_warning ("Texture malformed.");
g_clear_error (&error);
goto cleanup_create_picture_from_url;
}
picture = GTK_PICTURE (gtk_picture_new_for_paintable (GDK_PAINTABLE (texture)));
@ -93,21 +92,43 @@ free_picture_thread_attributes (gpointer user_data) {
g_free (attrs);
}
void
GtkPicture *
create_picture_from_url (const char *const url, gint picture_size,
GAsyncReadyCallback ready, gpointer source_object,
gpointer callback_data) {
GTask *task = g_task_new (source_object, NULL, ready, callback_data);
GtkPicture *picture = NULL;
GFile *image = NULL;
GdkTexture *texture = NULL;
GError *error = NULL;
image = get_image_for_url (url);
size_t url_len = strlen (url) + 1;
PictureThreadAttributes *attrs = g_malloc (sizeof *attrs);
if (g_file_query_exists (image, NULL)) {
texture = gdk_texture_new_from_file (image, &error);
if (error) {
g_warning ("Texture malformed.");
g_clear_error (&error);
goto cleanup_create_picture_from_url;
}
picture = GTK_PICTURE (gtk_picture_new_for_paintable (GDK_PAINTABLE (texture)));
if (GTK_IS_WIDGET (picture)) {
g_object_set_property_int (G_OBJECT(picture), "height-request", picture_size);
g_object_set_property_int (G_OBJECT(picture), "width-request", picture_size);
}
} else {
GTask *task = g_task_new (source_object, NULL, ready, callback_data);
PictureThreadAttributes *attrs = g_malloc (sizeof *attrs);
attrs->url = g_malloc (url_len * sizeof *url);
snprintf (attrs->url, url_len, "%s", url);
attrs->image = image;
attrs->picture_size = picture_size;
g_task_set_task_data (task, attrs, free_picture_thread_attributes);
g_task_run_in_thread (task, threaded_picture_recover);
}
cleanup_create_picture_from_url:
return picture;
}
GFile *