Use Unicode characters when cleaning up attachment filename

Improve the function ``cleanup_filename()`` of ``lib/mu-msg-part.c`` to
use Unicode characters when replacing the control characters, slashes
and colons with ``-``.

Originally, this function just use plain C characters (i.e., assuming
ASCII string) when checking each character is or not a control character,
slash or colon.  However, when the attachment filename contains non-ASCII
(e.g., Chinese characters), all the non-ASCII characters are replaced
with ``-``.

For example:
* Before:
```
> mu view test_chinese_attachment_filename.eml
From: Tester <tester@example.com>
To: Example <example@example.com>
Subject: Test email with attachment of Chinese filename
Date: Mon 23 May 2016 05:22:09 PM CST
Attachments: 'attachment-test.txt', '------------.txt', '-------test.txt'
Hello,

This is a simple test email with three attachments:

1. `attachment:test.txt`: filename is all English;
2. `测试附件.txt`: filename is all Chinese (exclude the extension);
3. `附件-test.txt`: filename mixes Chinese and English.
```

* After:
```
> ./build/mu/mu/mu view test_chinese_attachment_filename.eml
From: Tester <tester@example.com>
To: Example <example@example.com>
Subject: Test email with attachment of Chinese filename
Date: Mon 23 May 2016 05:22:09 PM CST
Attachments: 'attachment-test.txt', '测试附件.txt', '附件-test.txt'
Hello,

This is a simple test email with three attachments:

1. `attachment:test.txt`: filename is all English;
2. `测试附件.txt`: filename is all Chinese (exclude the extension);
3. `附件-test.txt`: filename mixes Chinese and English.
```
This commit is contained in:
Aaron LI 2016-05-23 19:26:04 +08:00
parent 54b6230adf
commit 297df938d6
1 changed files with 19 additions and 8 deletions

View File

@ -239,15 +239,26 @@ get_part_size (GMimePart *part)
}
static void
static char*
cleanup_filename (char *fname)
{
GString *gstr;
gchar *cur;
gunichar uc;
/* replace control characters, slashes,colons by a '-' */
for (cur = fname; *cur; ++cur)
if (*cur < ' ' || *cur == '/' || *cur == ':')
*cur = '-';
gstr = g_string_sized_new (strlen (fname));
/* replace control characters, slashes, and colons by '-' */
for (cur = fname; cur && *cur; cur = g_utf8_next_char (cur)) {
uc = g_utf8_get_char (cur);
if (g_unichar_iscntrl (uc) || uc == '/' || uc == ':')
g_string_append_unichar (gstr, '-');
else
g_string_append_unichar (gstr, uc);
}
g_free (fname);
return g_string_free (gstr, FALSE);
}
@ -304,9 +315,9 @@ mime_part_get_filename (GMimeObject *mobj, unsigned index,
if (!fname)
fname = guess_file_name (mobj, index);
/* remove slashes, spaces, colons... */
cleanup_filename (fname);
/* replace control characters, slashes, and colons */
fname = cleanup_filename (fname);
return fname;
}