~martijnbraam/readerview

83fe8d2a0209ec3de30bd15cccf2fa7993a1ead1 — Martijn Braam 3 years ago a77c40f
Support images
1 files changed, 61 insertions(+), 15 deletions(-)

M readerviewwin.c
M readerviewwin.c => readerviewwin.c +61 -15
@@ 432,21 432,6 @@ readerview_app_window_update_tofu_icon()
}

void
readerview_app_window_display_response(struct gemini_response *resp, GtkBox *target)
{
	if (strcmp(resp->meta, "text/gemini") == 0 
			|| strncmp(resp->meta, "text/gemini;", 12) == 0) {
		readerview_app_window_display_response_text_gemini(resp, target);
	} else if (strncmp(resp->meta, "text/", 5) == 0 ) {
		readerview_app_window_display_response_text_plain(resp, target);
	} else if (strncmp(resp->meta, "image/", 6) == 0 ) {
		// TODO: fetch image
	} else {
		// TODO: download
	}
}

void
readerview_app_window_display_response_text_gemini(struct gemini_response *resp, GtkBox *target)
{
	GtkWidget *widget, *child, *fixed_box, *fixed_scroll;


@@ 613,6 598,67 @@ readerview_app_window_display_response_text_plain(struct gemini_response *resp, 
}

void
readerview_app_window_display_response_image(struct gemini_response *resp, GtkBox *target)
{
	GtkWidget *widget, *child, *fixed_box, *fixed_scroll;

	char *contents = malloc(4096);
	int contents_size = 4096;
	int contents_length = 0;

	for (int n = 1; n > 0;) {
		n = BIO_read(resp->bio, &contents[contents_length], contents_size - contents_length - 1);
		if (n == -1) {
			fprintf(stderr, "Error: read");
			return;
		}
		contents_length += n;
		if(contents_length > contents_size - 2) {
			contents_size *= 2;
			contents = realloc(contents, contents_size);
			if(!contents) {
				readerview_app_window_display_error("Ran out of memory while rendering page", target);
			}
		}
	}
	contents[contents_length] = 0;

	// Load in-memory image into a pixbuf
	GdkPixbufLoader *loader = gdk_pixbuf_loader_new();
	gdk_pixbuf_loader_write(loader, contents, contents_length, NULL);
	GdkPixbuf *pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);

	// Clear drawing area
	for(child = gtk_widget_get_first_child(GTK_WIDGET(target));
			child != NULL;
			child = gtk_widget_get_first_child(GTK_WIDGET(target)))
	{
		gtk_box_remove(target, child);
	}
	
	// Make GtkPicture and add it to drawing area
	widget = gtk_picture_new_for_pixbuf(pixbuf);
	gtk_widget_add_css_class(widget, "img");
	gtk_widget_set_margin_top(GTK_WIDGET(widget), 16);
	gtk_box_append(target, widget);
}

void
readerview_app_window_display_response(struct gemini_response *resp, GtkBox *target)
{
	if (strcmp(resp->meta, "text/gemini") == 0 
			|| strncmp(resp->meta, "text/gemini;", 12) == 0) {
		readerview_app_window_display_response_text_gemini(resp, target);
	} else if (strncmp(resp->meta, "text/", 5) == 0 ) {
		readerview_app_window_display_response_text_plain(resp, target);
	} else if (strncmp(resp->meta, "image/", 6) == 0 ) {
		readerview_app_window_display_response_image(resp, target);
	} else {
		// TODO: download
	}
}

void
readerview_app_window_load_uri(char *uri, GtkBox *target, GtkEntry *urlbar, ReaderviewAppWindow *win)
{
	char url[1025];