From db7179c0cb7671d75b10d1fb088ccf2cb052facf Mon Sep 17 00:00:00 2001 From: Nathan Pratta Teodosio Date: Wed, 11 Oct 2023 16:01:23 +0200 Subject: [PATCH] Handle Thunderbird as a special case for mail with attachment. Thunderbird no longer accepts the mailto URI. This breaks Nautilus right-click > Email, for example. Creates a special case for Thunberbird then, using the -compose option. --- src/email.c | 132 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 95 insertions(+), 37 deletions(-) diff --git a/src/email.c b/src/email.c index 2d001fb9..86098f2e 100644 --- a/src/email.c +++ b/src/email.c @@ -34,66 +34,124 @@ compose_mail_mailto (GAppInfo *info, { g_autofree char *enc_subject = NULL; g_autofree char *enc_body = NULL; + g_autofree char *arg = NULL; g_autoptr(GString) url = NULL; g_autoptr(GList) uris = NULL; + GString *tb_arg = NULL; int i; gboolean success; const char *sep; + const char *is_tb_snap; + const char *old_cmdline; enc_subject = g_uri_escape_string (subject ? subject : "", NULL, FALSE); enc_body = g_uri_escape_string (body ? body : "", NULL, FALSE); - url = g_string_new ("mailto:"); - - sep = "?"; - - if (addrs) - { - for (i = 0; addrs[i]; i++) - { - if (i > 0) - g_string_append (url, ","); - g_string_append_printf (url, "%s", addrs[i]); + /* The commandline can be complicated, e.g. + * env BAMF_DESKTOP_FILE_HINT=/var/lib/snapd/.../thunderbird_thunderbird.desktop /snap/bin/thunderbird %u + * so just match for thunderbird. + */ + old_cmdline = g_app_info_get_commandline(info); + if(strstr(old_cmdline, "thunderbird")) { + /* Construct the argument to -compose. */ + tb_arg = g_string_new(""); + if (addrs) { + g_string_append_printf(tb_arg, "to='"); + for (i = 0; addrs[i]; i++){ + g_string_append_printf(tb_arg, "%s,", addrs[i]); } + g_string_append_printf(tb_arg, "',"); + } + if (cc) { + g_string_append_printf(tb_arg, "cc='"); + for (i = 0; cc[i]; i++){ + g_string_append_printf(tb_arg, "%s,", cc[i]); + } + g_string_append_printf(tb_arg, "',"); + } + if (bcc) { + g_string_append_printf(tb_arg, "bcc='"); + for (i = 0; bcc[i]; i++){ + g_string_append_printf(tb_arg, "%s,", bcc[i]); + } + g_string_append_printf(tb_arg, "',"); + } + g_string_append_printf (tb_arg, "subject='%s',", enc_subject); + g_string_append_printf (tb_arg, "body='%s',", enc_body); + if (attachments) { + g_string_append_printf(tb_arg, "attachment='"); + for (i = 0; attachments[i]; i++){ + if (i != 0) g_string_append_printf (tb_arg, ","); + gchar *enc_att = (g_uri_escape_string(attachments[i], "/", 1)); + g_string_append_printf (tb_arg, "file://%s", enc_att); + } + g_string_append_printf(tb_arg, "',"); } - if (cc) - { - g_string_append_printf (url, "%scc=", sep); - sep = "&"; - - for (i = 0; cc[i]; i++) + arg = g_string_free_and_steal(tb_arg); + is_tb_snap = strstr(old_cmdline, "/snap/bin/thunderbird"); + gchar *cmdline[] = { + is_tb_snap ? "/snap/bin/thunderbird" : "thunderbird", + "-compose", + arg, + NULL + }; + g_debug("Launching: %s %s %s", cmdline[0], cmdline[1], cmdline[2]); + success = g_spawn_async(NULL, cmdline, NULL, G_SPAWN_SEARCH_PATH, NULL, + NULL, NULL, error); + } else { + url = g_string_new ("mailto:"); + + sep = "?"; + + if (addrs) + { + for (i = 0; addrs[i]; i++) { if (i > 0) g_string_append (url, ","); - g_string_append_printf (url, "%s", cc[i]); + g_string_append_printf (url, "%s", addrs[i]); } - } + } - if (bcc) - { - g_string_append_printf (url, "%sbcc=", sep); - sep = "&"; + if (cc) + { + g_string_append_printf (url, "%scc=", sep); + sep = "&"; + + for (i = 0; cc[i]; i++) + { + if (i > 0) + g_string_append (url, ","); + g_string_append_printf (url, "%s", cc[i]); + } + } - for (i = 0; bcc[i]; i++) - { - if (i > 0) - g_string_append (url, ","); - g_string_append_printf (url, "%s", bcc[i]); - } - } + if (bcc) + { + g_string_append_printf (url, "%sbcc=", sep); + sep = "&"; + + for (i = 0; bcc[i]; i++) + { + if (i > 0) + g_string_append (url, ","); + g_string_append_printf (url, "%s", bcc[i]); + } + } - g_string_append_printf (url, "%ssubject=%s", sep, enc_subject); - g_string_append_printf (url, "&body=%s", enc_body); + g_string_append_printf (url, "%ssubject=%s", sep, enc_subject); + g_string_append_printf (url, "&body=%s", enc_body); - for (i = 0; attachments[i]; i++) - g_string_append_printf (url, "&attachment=%s", attachments[i]); + for (i = 0; attachments[i]; i++) + g_string_append_printf (url, "&attachment=%s", attachments[i]); - uris = g_list_append (uris, url->str); + uris = g_list_append (uris, url->str); - g_debug ("Launching: %s\n", url->str); + g_debug ("Launching: %s\n", url->str); - success = g_app_info_launch_uris (info, uris, NULL, error); + success = g_app_info_launch_uris (info, uris, NULL, error); + } return success; }