~elementary-apps/pantheon-mail/master

« back to all changes in this revision

Viewing changes to src/MessageList/MessageListItem.vala

  • Committer: Corentin Noël
  • Author(s): Julian Raschke
  • Date: 2018-09-11 11:39:09 UTC
  • Revision ID: git-v1:6c29e54206f32769156a01a4b55ae23dc5312e66
MessageListItem: Correct encoding, display plain text emails (#202)

Show diffs side-by-side

added added

removed removed

Lines of Context:
353
353
        if (message_is_html) {
354
354
            web_view.load_html (message_content);
355
355
        } else {
356
 
            web_view.load_plain_text (message_content);
 
356
            /*
 
357
             * Instead of calling web_view.load_plain_text, use Camel's ToHTML
 
358
             * filter to convert text to HTML. This gives us some niceties like
 
359
             * clickable URLs and email addresses for free.
 
360
             *
 
361
             * Explanation of MimeFilterToHTMLFlags:
 
362
             * https://wiki.gnome.org/Apps/Evolution/Camel.MimeFilter#Camel.MimeFilterToHtml
 
363
             */
 
364
            var flags = Camel.MimeFilterToHTMLFlags.CONVERT_NL |
 
365
                Camel.MimeFilterToHTMLFlags.CONVERT_SPACES |
 
366
                Camel.MimeFilterToHTMLFlags.CONVERT_URLS |
 
367
                Camel.MimeFilterToHTMLFlags.CONVERT_ADDRESSES;
 
368
            var html = Camel.text_to_html (message_content, flags, 0);
 
369
            web_view.load_html (html);
357
370
        }
358
371
    }
359
372
 
360
 
    private async void parse_mime_content (Camel.DataWrapper message_content) {
361
 
        if (message_content is Camel.Multipart) {
362
 
            var content = message_content as Camel.Multipart;
 
373
    private async void parse_mime_content (Camel.DataWrapper mime_content) {
 
374
        if (mime_content is Camel.Multipart) {
 
375
            var content = mime_content as Camel.Multipart;
363
376
            for (uint i = 0; i < content.get_number (); i++) {
364
377
                var part = content.get_part (i);
365
378
                var field = part.get_mime_type_field ();
372
385
                }
373
386
            }
374
387
        } else {
375
 
            yield handle_text_mime (message_content);
 
388
            yield handle_text_mime (mime_content);
376
389
        }
377
390
    }
378
391
 
388
401
                return;
389
402
            }
390
403
 
391
 
            message_content = (string) os.steal_data ();
 
404
            // Convert the message to UTF-8 to ensure we have a valid GLib string.
 
405
            message_content = convert_to_utf8 (os, field.param ("charset"));
 
406
 
392
407
            if (field.subtype == "html") {
393
408
                message_is_html = true;
394
409
            }
395
410
        }
396
411
    }
397
412
 
 
413
    private static string convert_to_utf8 (GLib.MemoryOutputStream os, string? encoding) {
 
414
        var num_bytes = (int) os.get_data_size ();
 
415
        var bytes = (string) os.steal_data ();
 
416
 
 
417
        string? utf8 = null;
 
418
 
 
419
        if (encoding != null) {
 
420
            string? iconv_encoding = Camel.iconv_charset_name (encoding);
 
421
            if (iconv_encoding != null) {
 
422
                try {
 
423
                    utf8 = GLib.convert (bytes, num_bytes, "UTF-8", iconv_encoding);
 
424
                } catch (ConvertError e) {
 
425
                    // Nothing to do - result will be assigned below.
 
426
                }
 
427
            }
 
428
        }
 
429
 
 
430
        if (utf8 == null || !utf8.validate ()) {
 
431
            /*
 
432
             * If message_content is not valid UTF-8 at this point, assume that
 
433
             * it is ISO-8859-1 encoded by default, and convert it to UTF-8.
 
434
             */
 
435
            try {
 
436
                utf8 = GLib.convert (bytes, num_bytes, "UTF-8", "ISO-8859-1");
 
437
            } catch (ConvertError e) {
 
438
                critical ("Every string should be valid ISO-8859-1. ConvertError: %s", e.message);
 
439
                utf8 = "";
 
440
            }
 
441
        }
 
442
 
 
443
        return utf8;
 
444
    }
 
445
 
398
446
    private async void handle_inline_mime (Camel.MimePart part) {
399
447
        var byte_array = new ByteArray ();
400
448
        var os = new Camel.StreamMem ();
406
454
            return;
407
455
        }
408
456
 
409
 
        Bytes bytes;
410
 
        bytes = ByteArray.free_to_bytes (byte_array);
 
457
        Bytes bytes = ByteArray.free_to_bytes (byte_array);
411
458
        var inline_stream = new MemoryInputStream.from_bytes (bytes);
412
459
        web_view.add_internal_resource (part.get_content_id (), inline_stream);
413
460
    }