[evolution-patches] #312370: When loading images from an html message the percentage on the status bar is wrong



Hi,

Here's a proposed patch that fixes the problem.

It tries to look for Content-Length HTTP header and calculates the
percentage accordingly. If no Content-Length header is found or
Content-Length is 0, no download status information is shown (I
couldn't validate this, since I don't know of any broken web servers
:) )

Srinidhi.
--
ASCII ribbon campaign ( )            B S Srinidhi
 - against HTML email  X             http://www.srinidhi-is.in
                    & vCards / \            Bangalore
Index: mail/em-format-html.c
===================================================================
--- mail/em-format-html.c	(revision 33499)
+++ mail/em-format-html.c	(working copy)
@@ -449,8 +449,11 @@
 {
 	CamelStream *cistream = NULL, *costream = NULL, *instream = NULL;
 	CamelURL *url;
-	ssize_t n, total = 0;
+	CamelContentType *content_type;
+	CamelHttpStream *tmp_stream;
+	ssize_t n, total = 0, pc_complete = 0, nread = 0;
 	char buffer[1500];
+	const char *length;
 
 	if (cancelled
 	    || (url = camel_url_new(job->u.uri, NULL)) == NULL)
@@ -481,6 +484,13 @@
 		camel_http_stream_set_proxy((CamelHttpStream *)instream, proxy);
 		g_free(proxy);
 		camel_operation_start(NULL, _("Retrieving `%s'"), job->u.uri);
+		tmp_stream = (CamelHttpStream *)instream;
+		content_type = camel_http_stream_get_content_type(tmp_stream);
+		length = camel_header_raw_find(&tmp_stream->headers, "Content-Length", NULL);
+		d(printf("  Content-Length: %s\n", length));
+		if (length != NULL)
+			total = atoi(length);
+		camel_content_type_unref(content_type);
 	} else
 		camel_operation_start_transient(NULL, _("Retrieving `%s'"), job->u.uri);
 
@@ -496,8 +506,12 @@
 		/* FIXME: progress reporting in percentage, can we get the length always?  do we care? */
 		n = camel_stream_read(instream, buffer, sizeof (buffer));
 		if (n > 0) {
-			camel_operation_progress_count(NULL, total);
-			total += n;
+			nread += n;
+			/* If we didn't get a valid Content-Length header, do not try to calculate percentage */
+			if (total != 0) {
+				pc_complete = ((nread * 100) / total);
+				camel_operation_progress(NULL, pc_complete);
+			}
 			d(printf("  read %d bytes\n", n));
 			if (costream && camel_stream_write(costream, buffer, n) == -1) {
 				camel_data_cache_remove(emfh_http_cache, EMFH_HTTP_CACHE_PATH, job->u.uri, NULL);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]